Antoine Musso | 45dd2cb | 2014-01-29 17:17:43 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright 2012 Hewlett-Packard Development Company, L.P. |
| 4 | # Copyright 2014 Wikimedia Foundation Inc. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
| 18 | import logging |
James E. Blair | 217b10d | 2015-01-08 16:25:28 -0800 | [diff] [blame] | 19 | import time |
Antoine Musso | 45dd2cb | 2014-01-29 17:17:43 +0100 | [diff] [blame] | 20 | |
Tristan Cacqueray | 8095440 | 2017-05-28 00:33:55 +0000 | [diff] [blame] | 21 | import zuul.executor.server |
| 22 | import zuul.model |
| 23 | |
James E. Blair | d8af542 | 2017-05-24 13:59:40 -0700 | [diff] [blame] | 24 | from tests.base import ZuulTestCase, simple_layout |
Antoine Musso | 45dd2cb | 2014-01-29 17:17:43 +0100 | [diff] [blame] | 25 | |
Antoine Musso | 45dd2cb | 2014-01-29 17:17:43 +0100 | [diff] [blame] | 26 | |
James E. Blair | b3b5b38 | 2017-06-02 11:29:57 -0700 | [diff] [blame] | 27 | class TestExecutorRepos(ZuulTestCase): |
James E. Blair | d8af542 | 2017-05-24 13:59:40 -0700 | [diff] [blame] | 28 | tenant_config_file = 'config/single-tenant/main.yaml' |
Antoine Musso | 45dd2cb | 2014-01-29 17:17:43 +0100 | [diff] [blame] | 29 | |
James E. Blair | b3b5b38 | 2017-06-02 11:29:57 -0700 | [diff] [blame] | 30 | log = logging.getLogger("zuul.test.executor") |
Antoine Musso | 45dd2cb | 2014-01-29 17:17:43 +0100 | [diff] [blame] | 31 | |
James E. Blair | af3d01e | 2017-05-26 14:30:18 -0700 | [diff] [blame] | 32 | def assertRepoState(self, repo, state, project, build, number): |
James E. Blair | bcaa2d4 | 2017-05-26 15:44:23 -0700 | [diff] [blame] | 33 | if 'branch' in state: |
| 34 | self.assertFalse(repo.head.is_detached, |
| 35 | 'Project %s commit for build %s #%s should ' |
| 36 | 'not have a detached HEAD' % ( |
| 37 | project, build, number)) |
zhangyangyang | c3e786f | 2017-09-13 10:47:52 +0800 | [diff] [blame] | 38 | self.assertEqual(repo.active_branch.name, |
| 39 | state['branch'], |
| 40 | 'Project %s commit for build %s #%s should ' |
| 41 | 'be on the correct branch' % ( |
| 42 | project, build, number)) |
James E. Blair | bcaa2d4 | 2017-05-26 15:44:23 -0700 | [diff] [blame] | 43 | if 'commit' in state: |
zhangyangyang | c3e786f | 2017-09-13 10:47:52 +0800 | [diff] [blame] | 44 | self.assertEqual(state['commit'], |
| 45 | str(repo.commit('HEAD')), |
| 46 | 'Project %s commit for build %s #%s should ' |
| 47 | 'be correct' % ( |
| 48 | project, build, number)) |
James E. Blair | bcaa2d4 | 2017-05-26 15:44:23 -0700 | [diff] [blame] | 49 | ref = repo.commit('HEAD') |
| 50 | repo_messages = set( |
| 51 | [c.message.strip() for c in repo.iter_commits(ref)]) |
| 52 | if 'present' in state: |
| 53 | for change in state['present']: |
James E. Blair | af3d01e | 2017-05-26 14:30:18 -0700 | [diff] [blame] | 54 | msg = '%s-1' % change.subject |
| 55 | self.assertTrue(msg in repo_messages, |
| 56 | 'Project %s for build %s #%s should ' |
| 57 | 'have change %s' % ( |
| 58 | project, build, number, change.subject)) |
James E. Blair | bcaa2d4 | 2017-05-26 15:44:23 -0700 | [diff] [blame] | 59 | if 'absent' in state: |
| 60 | for change in state['absent']: |
James E. Blair | af3d01e | 2017-05-26 14:30:18 -0700 | [diff] [blame] | 61 | msg = '%s-1' % change.subject |
| 62 | self.assertTrue(msg not in repo_messages, |
| 63 | 'Project %s for build %s #%s should ' |
| 64 | 'not have change %s' % ( |
| 65 | project, build, number, change.subject)) |
| 66 | |
James E. Blair | 987855f | 2017-06-06 14:41:58 -0700 | [diff] [blame] | 67 | def assertBuildStates(self, states, projects): |
| 68 | for number, build in enumerate(self.builds): |
| 69 | work = build.getWorkspaceRepos(projects) |
| 70 | state = states[number] |
| 71 | |
| 72 | for project in projects: |
| 73 | self.assertRepoState(work[project], state[project], |
| 74 | project, build, number) |
| 75 | |
| 76 | self.executor_server.hold_jobs_in_build = False |
| 77 | self.executor_server.release() |
| 78 | self.waitUntilSettled() |
| 79 | |
James E. Blair | d8af542 | 2017-05-24 13:59:40 -0700 | [diff] [blame] | 80 | @simple_layout('layouts/repo-checkout-two-project.yaml') |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 81 | def test_one_branch(self): |
James E. Blair | d8af542 | 2017-05-24 13:59:40 -0700 | [diff] [blame] | 82 | self.executor_server.hold_jobs_in_build = True |
Antoine Musso | 45dd2cb | 2014-01-29 17:17:43 +0100 | [diff] [blame] | 83 | |
James E. Blair | d8af542 | 2017-05-24 13:59:40 -0700 | [diff] [blame] | 84 | p1 = 'review.example.com/org/project1' |
| 85 | p2 = 'review.example.com/org/project2' |
| 86 | projects = [p1, p2] |
Antoine Musso | 45dd2cb | 2014-01-29 17:17:43 +0100 | [diff] [blame] | 87 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 88 | B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B') |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 89 | A.addApproval('Code-Review', 2) |
| 90 | B.addApproval('Code-Review', 2) |
| 91 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
| 92 | self.fake_gerrit.addEvent(B.addApproval('Approved', 1)) |
Antoine Musso | 45dd2cb | 2014-01-29 17:17:43 +0100 | [diff] [blame] | 93 | |
| 94 | self.waitUntilSettled() |
| 95 | |
zhangyangyang | c3e786f | 2017-09-13 10:47:52 +0800 | [diff] [blame] | 96 | self.assertEqual(2, len(self.builds), "Two builds are running") |
Antoine Musso | 45dd2cb | 2014-01-29 17:17:43 +0100 | [diff] [blame] | 97 | |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 98 | upstream = self.getUpstreamRepos(projects) |
| 99 | states = [ |
James E. Blair | bcaa2d4 | 2017-05-26 15:44:23 -0700 | [diff] [blame] | 100 | {p1: dict(present=[A], absent=[B], branch='master'), |
| 101 | p2: dict(commit=str(upstream[p2].commit('master')), |
| 102 | branch='master'), |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 103 | }, |
James E. Blair | bcaa2d4 | 2017-05-26 15:44:23 -0700 | [diff] [blame] | 104 | {p1: dict(present=[A], absent=[B], branch='master'), |
| 105 | p2: dict(present=[B], absent=[A], branch='master'), |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 106 | }, |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 107 | ] |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 108 | |
James E. Blair | 987855f | 2017-06-06 14:41:58 -0700 | [diff] [blame] | 109 | self.assertBuildStates(states, projects) |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 110 | |
James E. Blair | af3d01e | 2017-05-26 14:30:18 -0700 | [diff] [blame] | 111 | @simple_layout('layouts/repo-checkout-four-project.yaml') |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 112 | def test_multi_branch(self): |
James E. Blair | af3d01e | 2017-05-26 14:30:18 -0700 | [diff] [blame] | 113 | self.executor_server.hold_jobs_in_build = True |
| 114 | |
| 115 | p1 = 'review.example.com/org/project1' |
| 116 | p2 = 'review.example.com/org/project2' |
| 117 | p3 = 'review.example.com/org/project3' |
| 118 | p4 = 'review.example.com/org/project4' |
| 119 | projects = [p1, p2, p3, p4] |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 120 | |
| 121 | self.create_branch('org/project2', 'stable/havana') |
| 122 | self.create_branch('org/project4', 'stable/havana') |
| 123 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 124 | B = self.fake_gerrit.addFakeChange('org/project2', 'stable/havana', |
| 125 | 'B') |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 126 | C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C') |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 127 | A.addApproval('Code-Review', 2) |
| 128 | B.addApproval('Code-Review', 2) |
| 129 | C.addApproval('Code-Review', 2) |
| 130 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
| 131 | self.fake_gerrit.addEvent(B.addApproval('Approved', 1)) |
| 132 | self.fake_gerrit.addEvent(C.addApproval('Approved', 1)) |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 133 | |
| 134 | self.waitUntilSettled() |
| 135 | |
zhangyangyang | c3e786f | 2017-09-13 10:47:52 +0800 | [diff] [blame] | 136 | self.assertEqual(3, len(self.builds), "Three builds are running") |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 137 | |
| 138 | upstream = self.getUpstreamRepos(projects) |
| 139 | states = [ |
James E. Blair | bcaa2d4 | 2017-05-26 15:44:23 -0700 | [diff] [blame] | 140 | {p1: dict(present=[A], absent=[B, C], branch='master'), |
| 141 | p2: dict(commit=str(upstream[p2].commit('master')), |
| 142 | branch='master'), |
| 143 | p3: dict(commit=str(upstream[p3].commit('master')), |
| 144 | branch='master'), |
| 145 | p4: dict(commit=str(upstream[p4].commit('master')), |
| 146 | branch='master'), |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 147 | }, |
James E. Blair | bcaa2d4 | 2017-05-26 15:44:23 -0700 | [diff] [blame] | 148 | {p1: dict(present=[A], absent=[B, C], branch='master'), |
| 149 | p2: dict(present=[B], absent=[A, C], branch='stable/havana'), |
| 150 | p3: dict(commit=str(upstream[p3].commit('master')), |
| 151 | branch='master'), |
| 152 | p4: dict(commit=str(upstream[p4].commit('stable/havana')), |
| 153 | branch='stable/havana'), |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 154 | }, |
James E. Blair | bcaa2d4 | 2017-05-26 15:44:23 -0700 | [diff] [blame] | 155 | {p1: dict(present=[A], absent=[B, C], branch='master'), |
| 156 | p2: dict(commit=str(upstream[p2].commit('master')), |
| 157 | branch='master'), |
| 158 | p3: dict(present=[C], absent=[A, B], branch='master'), |
| 159 | p4: dict(commit=str(upstream[p4].commit('master')), |
| 160 | branch='master'), |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 161 | }, |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 162 | ] |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 163 | |
James E. Blair | 987855f | 2017-06-06 14:41:58 -0700 | [diff] [blame] | 164 | self.assertBuildStates(states, projects) |
James E. Blair | bce35e1 | 2014-08-21 14:31:17 -0700 | [diff] [blame] | 165 | |
James E. Blair | 4de8d9e | 2017-06-01 15:41:40 -0700 | [diff] [blame] | 166 | @simple_layout('layouts/repo-checkout-six-project.yaml') |
James E. Blair | f042022 | 2014-08-21 16:02:17 -0700 | [diff] [blame] | 167 | def test_project_override(self): |
James E. Blair | 4de8d9e | 2017-06-01 15:41:40 -0700 | [diff] [blame] | 168 | self.executor_server.hold_jobs_in_build = True |
| 169 | |
| 170 | p1 = 'review.example.com/org/project1' |
| 171 | p2 = 'review.example.com/org/project2' |
| 172 | p3 = 'review.example.com/org/project3' |
| 173 | p4 = 'review.example.com/org/project4' |
| 174 | p5 = 'review.example.com/org/project5' |
| 175 | p6 = 'review.example.com/org/project6' |
| 176 | projects = [p1, p2, p3, p4, p5, p6] |
James E. Blair | f042022 | 2014-08-21 16:02:17 -0700 | [diff] [blame] | 177 | |
| 178 | self.create_branch('org/project3', 'stable/havana') |
| 179 | self.create_branch('org/project4', 'stable/havana') |
| 180 | self.create_branch('org/project6', 'stable/havana') |
| 181 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 182 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 183 | C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C') |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 184 | D = self.fake_gerrit.addFakeChange('org/project3', 'stable/havana', |
| 185 | 'D') |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 186 | A.addApproval('Code-Review', 2) |
| 187 | B.addApproval('Code-Review', 2) |
| 188 | C.addApproval('Code-Review', 2) |
| 189 | D.addApproval('Code-Review', 2) |
| 190 | self.fake_gerrit.addEvent(A.addApproval('Approved', 1)) |
| 191 | self.fake_gerrit.addEvent(B.addApproval('Approved', 1)) |
| 192 | self.fake_gerrit.addEvent(C.addApproval('Approved', 1)) |
| 193 | self.fake_gerrit.addEvent(D.addApproval('Approved', 1)) |
James E. Blair | f042022 | 2014-08-21 16:02:17 -0700 | [diff] [blame] | 194 | |
| 195 | self.waitUntilSettled() |
| 196 | |
zhangyangyang | c3e786f | 2017-09-13 10:47:52 +0800 | [diff] [blame] | 197 | self.assertEqual(4, len(self.builds), "Four builds are running") |
James E. Blair | f042022 | 2014-08-21 16:02:17 -0700 | [diff] [blame] | 198 | |
| 199 | upstream = self.getUpstreamRepos(projects) |
| 200 | states = [ |
James E. Blair | 4de8d9e | 2017-06-01 15:41:40 -0700 | [diff] [blame] | 201 | {p1: dict(present=[A], absent=[B, C, D], branch='master'), |
| 202 | p2: dict(commit=str(upstream[p2].commit('master')), |
| 203 | branch='master'), |
| 204 | p3: dict(commit=str(upstream[p3].commit('master')), |
| 205 | branch='master'), |
| 206 | p4: dict(commit=str(upstream[p4].commit('master')), |
| 207 | branch='master'), |
| 208 | p5: dict(commit=str(upstream[p5].commit('master')), |
| 209 | branch='master'), |
| 210 | p6: dict(commit=str(upstream[p6].commit('master')), |
| 211 | branch='master'), |
James E. Blair | f042022 | 2014-08-21 16:02:17 -0700 | [diff] [blame] | 212 | }, |
James E. Blair | 4de8d9e | 2017-06-01 15:41:40 -0700 | [diff] [blame] | 213 | {p1: dict(present=[A, B], absent=[C, D], branch='master'), |
| 214 | p2: dict(commit=str(upstream[p2].commit('master')), |
| 215 | branch='master'), |
| 216 | p3: dict(commit=str(upstream[p3].commit('master')), |
| 217 | branch='master'), |
| 218 | p4: dict(commit=str(upstream[p4].commit('master')), |
| 219 | branch='master'), |
| 220 | p5: dict(commit=str(upstream[p5].commit('master')), |
| 221 | branch='master'), |
| 222 | p6: dict(commit=str(upstream[p6].commit('master')), |
| 223 | branch='master'), |
James E. Blair | f042022 | 2014-08-21 16:02:17 -0700 | [diff] [blame] | 224 | }, |
James E. Blair | 4de8d9e | 2017-06-01 15:41:40 -0700 | [diff] [blame] | 225 | {p1: dict(present=[A, B], absent=[C, D], branch='master'), |
| 226 | p2: dict(present=[C], absent=[A, B, D], branch='master'), |
| 227 | p3: dict(commit=str(upstream[p3].commit('master')), |
| 228 | branch='master'), |
| 229 | p4: dict(commit=str(upstream[p4].commit('master')), |
| 230 | branch='master'), |
| 231 | p5: dict(commit=str(upstream[p5].commit('master')), |
| 232 | branch='master'), |
| 233 | p6: dict(commit=str(upstream[p6].commit('master')), |
| 234 | branch='master'), |
James E. Blair | f042022 | 2014-08-21 16:02:17 -0700 | [diff] [blame] | 235 | }, |
James E. Blair | 4de8d9e | 2017-06-01 15:41:40 -0700 | [diff] [blame] | 236 | {p1: dict(present=[A, B], absent=[C, D], branch='master'), |
| 237 | p2: dict(present=[C], absent=[A, B, D], branch='master'), |
| 238 | p3: dict(present=[D], absent=[A, B, C], |
| 239 | branch='stable/havana'), |
| 240 | p4: dict(commit=str(upstream[p4].commit('master')), |
| 241 | branch='master'), |
| 242 | p5: dict(commit=str(upstream[p5].commit('master')), |
| 243 | branch='master'), |
| 244 | p6: dict(commit=str(upstream[p6].commit('stable/havana')), |
| 245 | branch='stable/havana'), |
James E. Blair | f042022 | 2014-08-21 16:02:17 -0700 | [diff] [blame] | 246 | }, |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 247 | ] |
James E. Blair | f042022 | 2014-08-21 16:02:17 -0700 | [diff] [blame] | 248 | |
James E. Blair | 987855f | 2017-06-06 14:41:58 -0700 | [diff] [blame] | 249 | self.assertBuildStates(states, projects) |
James E. Blair | 217b10d | 2015-01-08 16:25:28 -0800 | [diff] [blame] | 250 | |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 251 | def test_periodic_override(self): |
| 252 | # This test can not use simple_layout because it must start |
| 253 | # with a configuration which does not include a |
| 254 | # timer-triggered job so that we have an opportunity to set |
| 255 | # the hold flag before the first job. |
| 256 | |
| 257 | # This tests that we can override the branch in a timer |
| 258 | # trigger (mostly to ensure backwards compatability for jobs). |
| 259 | self.executor_server.hold_jobs_in_build = True |
| 260 | # Start timer trigger - also org/project |
| 261 | self.commitConfigUpdate('common-config', |
| 262 | 'layouts/repo-checkout-timer-override.yaml') |
| 263 | self.sched.reconfigure(self.config) |
| 264 | |
| 265 | p1 = 'review.example.com/org/project1' |
| 266 | projects = [p1] |
| 267 | self.create_branch('org/project1', 'stable/havana') |
| 268 | |
| 269 | # The pipeline triggers every second, so we should have seen |
| 270 | # several by now. |
| 271 | time.sleep(5) |
| 272 | self.waitUntilSettled() |
| 273 | |
| 274 | # Stop queuing timer triggered jobs so that the assertions |
| 275 | # below don't race against more jobs being queued. |
| 276 | self.commitConfigUpdate('common-config', |
James E. Blair | 0b137b4 | 2017-07-27 08:51:25 -0700 | [diff] [blame] | 277 | 'layouts/repo-checkout-no-timer-override.yaml') |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 278 | self.sched.reconfigure(self.config) |
James E. Blair | 0b137b4 | 2017-07-27 08:51:25 -0700 | [diff] [blame] | 279 | self.waitUntilSettled() |
James E. Blair | 78ae478 | 2017-08-02 14:19:10 -0700 | [diff] [blame] | 280 | # If APScheduler is in mid-event when we remove the job, we |
| 281 | # can end up with one more event firing, so give it an extra |
| 282 | # second to settle. |
| 283 | time.sleep(1) |
| 284 | self.waitUntilSettled() |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 285 | |
zhangyangyang | c3e786f | 2017-09-13 10:47:52 +0800 | [diff] [blame] | 286 | self.assertEqual(1, len(self.builds), "One build is running") |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 287 | |
| 288 | upstream = self.getUpstreamRepos(projects) |
| 289 | states = [ |
| 290 | {p1: dict(commit=str(upstream[p1].commit('stable/havana')), |
| 291 | branch='stable/havana'), |
| 292 | }, |
| 293 | ] |
| 294 | |
| 295 | self.assertBuildStates(states, projects) |
| 296 | |
James E. Blair | 217b10d | 2015-01-08 16:25:28 -0800 | [diff] [blame] | 297 | def test_periodic(self): |
James E. Blair | 95ae4fc | 2017-06-01 15:54:53 -0700 | [diff] [blame] | 298 | # This test can not use simple_layout because it must start |
| 299 | # with a configuration which does not include a |
| 300 | # timer-triggered job so that we have an opportunity to set |
| 301 | # the hold flag before the first job. |
| 302 | self.executor_server.hold_jobs_in_build = True |
| 303 | # Start timer trigger - also org/project |
| 304 | self.commitConfigUpdate('common-config', |
| 305 | 'layouts/repo-checkout-timer.yaml') |
James E. Blair | 217b10d | 2015-01-08 16:25:28 -0800 | [diff] [blame] | 306 | self.sched.reconfigure(self.config) |
James E. Blair | 95ae4fc | 2017-06-01 15:54:53 -0700 | [diff] [blame] | 307 | |
| 308 | p1 = 'review.example.com/org/project1' |
| 309 | projects = [p1] |
| 310 | self.create_branch('org/project1', 'stable/havana') |
James E. Blair | 217b10d | 2015-01-08 16:25:28 -0800 | [diff] [blame] | 311 | |
| 312 | # The pipeline triggers every second, so we should have seen |
| 313 | # several by now. |
| 314 | time.sleep(5) |
| 315 | self.waitUntilSettled() |
| 316 | |
James E. Blair | 217b10d | 2015-01-08 16:25:28 -0800 | [diff] [blame] | 317 | # Stop queuing timer triggered jobs so that the assertions |
| 318 | # below don't race against more jobs being queued. |
James E. Blair | 95ae4fc | 2017-06-01 15:54:53 -0700 | [diff] [blame] | 319 | self.commitConfigUpdate('common-config', |
| 320 | 'layouts/repo-checkout-no-timer.yaml') |
James E. Blair | 217b10d | 2015-01-08 16:25:28 -0800 | [diff] [blame] | 321 | self.sched.reconfigure(self.config) |
James E. Blair | 0b137b4 | 2017-07-27 08:51:25 -0700 | [diff] [blame] | 322 | self.waitUntilSettled() |
James E. Blair | 78ae478 | 2017-08-02 14:19:10 -0700 | [diff] [blame] | 323 | # If APScheduler is in mid-event when we remove the job, we |
| 324 | # can end up with one more event firing, so give it an extra |
| 325 | # second to settle. |
| 326 | time.sleep(1) |
| 327 | self.waitUntilSettled() |
James E. Blair | 217b10d | 2015-01-08 16:25:28 -0800 | [diff] [blame] | 328 | |
zhangyangyang | c3e786f | 2017-09-13 10:47:52 +0800 | [diff] [blame] | 329 | self.assertEqual(2, len(self.builds), "Two builds are running") |
James E. Blair | 217b10d | 2015-01-08 16:25:28 -0800 | [diff] [blame] | 330 | |
| 331 | upstream = self.getUpstreamRepos(projects) |
| 332 | states = [ |
James E. Blair | 95ae4fc | 2017-06-01 15:54:53 -0700 | [diff] [blame] | 333 | {p1: dict(commit=str(upstream[p1].commit('stable/havana')), |
| 334 | branch='stable/havana'), |
James E. Blair | 217b10d | 2015-01-08 16:25:28 -0800 | [diff] [blame] | 335 | }, |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 336 | {p1: dict(commit=str(upstream[p1].commit('master')), |
| 337 | branch='master'), |
| 338 | }, |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 339 | ] |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 340 | if self.builds[0].parameters['zuul']['ref'] == 'refs/heads/master': |
| 341 | states = list(reversed(states)) |
James E. Blair | 217b10d | 2015-01-08 16:25:28 -0800 | [diff] [blame] | 342 | |
James E. Blair | 987855f | 2017-06-06 14:41:58 -0700 | [diff] [blame] | 343 | self.assertBuildStates(states, projects) |
Sachi King | 9f16d52 | 2016-03-16 12:20:45 +1100 | [diff] [blame] | 344 | |
James E. Blair | bbe811e | 2017-06-02 10:47:44 -0700 | [diff] [blame] | 345 | @simple_layout('layouts/repo-checkout-post.yaml') |
Sachi King | 9f16d52 | 2016-03-16 12:20:45 +1100 | [diff] [blame] | 346 | def test_post_and_master_checkout(self): |
James E. Blair | 1de2e49 | 2017-06-02 11:04:42 -0700 | [diff] [blame] | 347 | self.executor_server.hold_jobs_in_build = True |
| 348 | p1 = "review.example.com/org/project1" |
| 349 | p2 = "review.example.com/org/project2" |
| 350 | projects = [p1, p2] |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 351 | upstream = self.getUpstreamRepos(projects) |
James E. Blair | 8cce42e | 2016-10-18 08:18:36 -0700 | [diff] [blame] | 352 | |
James E. Blair | 1de2e49 | 2017-06-02 11:04:42 -0700 | [diff] [blame] | 353 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
James E. Blair | 8cce42e | 2016-10-18 08:18:36 -0700 | [diff] [blame] | 354 | event = A.getRefUpdatedEvent() |
| 355 | A.setMerged() |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 356 | A_commit = str(upstream[p1].commit('master')) |
| 357 | self.log.debug("A commit: %s" % A_commit) |
| 358 | |
| 359 | # Add another commit to the repo that merged right after this |
| 360 | # one to make sure that our post job runs with the one that we |
| 361 | # intended rather than simply the current repo state. |
| 362 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B', |
| 363 | parent='refs/changes/1/1/1') |
| 364 | B.setMerged() |
| 365 | B_commit = str(upstream[p1].commit('master')) |
| 366 | self.log.debug("B commit: %s" % B_commit) |
| 367 | |
James E. Blair | 8cce42e | 2016-10-18 08:18:36 -0700 | [diff] [blame] | 368 | self.fake_gerrit.addEvent(event) |
| 369 | self.waitUntilSettled() |
| 370 | |
James E. Blair | 1de2e49 | 2017-06-02 11:04:42 -0700 | [diff] [blame] | 371 | states = [ |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 372 | {p1: dict(commit=A_commit, |
| 373 | present=[A], absent=[B], branch='master'), |
James E. Blair | 1de2e49 | 2017-06-02 11:04:42 -0700 | [diff] [blame] | 374 | p2: dict(commit=str(upstream[p2].commit('master')), |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 375 | absent=[A, B], branch='master'), |
James E. Blair | 1de2e49 | 2017-06-02 11:04:42 -0700 | [diff] [blame] | 376 | }, |
| 377 | ] |
James E. Blair | 8cce42e | 2016-10-18 08:18:36 -0700 | [diff] [blame] | 378 | |
James E. Blair | 987855f | 2017-06-06 14:41:58 -0700 | [diff] [blame] | 379 | self.assertBuildStates(states, projects) |
Tristan Cacqueray | 8095440 | 2017-05-28 00:33:55 +0000 | [diff] [blame] | 380 | |
| 381 | |
| 382 | class TestAnsibleJob(ZuulTestCase): |
| 383 | tenant_config_file = 'config/ansible/main.yaml' |
| 384 | |
| 385 | def setUp(self): |
| 386 | super(TestAnsibleJob, self).setUp() |
| 387 | job = zuul.model.Job('test') |
| 388 | job.unique = 'test' |
| 389 | self.test_job = zuul.executor.server.AnsibleJob(self.executor_server, |
| 390 | job) |
| 391 | |
| 392 | def test_getHostList_host_keys(self): |
| 393 | # Test without ssh_port set |
| 394 | node = {'name': 'fake-host', |
| 395 | 'host_keys': ['fake-host-key'], |
| 396 | 'interface_ip': 'localhost'} |
| 397 | keys = self.test_job.getHostList({'nodes': [node]})[0]['host_keys'] |
| 398 | self.assertEqual(keys[0], 'localhost fake-host-key') |
| 399 | |
| 400 | # Test with custom ssh_port set |
| 401 | node['ssh_port'] = 22022 |
| 402 | keys = self.test_job.getHostList({'nodes': [node]})[0]['host_keys'] |
| 403 | self.assertEqual(keys[0], '[localhost]:22022 fake-host-key') |