blob: 39b6070f6a47ab7f33290ca7ea536840375da679 [file] [log] [blame]
Antoine Musso45dd2cb2014-01-29 17:17:43 +01001#!/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
18import logging
James E. Blair217b10d2015-01-08 16:25:28 -080019import time
Antoine Musso45dd2cb2014-01-29 17:17:43 +010020
James E. Blaird8af5422017-05-24 13:59:40 -070021from tests.base import ZuulTestCase, simple_layout
Antoine Musso45dd2cb2014-01-29 17:17:43 +010022
Antoine Musso45dd2cb2014-01-29 17:17:43 +010023
James E. Blairb3b5b382017-06-02 11:29:57 -070024class TestExecutorRepos(ZuulTestCase):
James E. Blaird8af5422017-05-24 13:59:40 -070025 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Musso45dd2cb2014-01-29 17:17:43 +010026
James E. Blairb3b5b382017-06-02 11:29:57 -070027 log = logging.getLogger("zuul.test.executor")
Antoine Musso45dd2cb2014-01-29 17:17:43 +010028
James E. Blairaf3d01e2017-05-26 14:30:18 -070029 def assertRepoState(self, repo, state, project, build, number):
James E. Blairbcaa2d42017-05-26 15:44:23 -070030 if 'branch' in state:
31 self.assertFalse(repo.head.is_detached,
32 'Project %s commit for build %s #%s should '
33 'not have a detached HEAD' % (
34 project, build, number))
35 self.assertEquals(repo.active_branch.name,
36 state['branch'],
37 'Project %s commit for build %s #%s should '
38 'be on the correct branch' % (
39 project, build, number))
40 if 'commit' in state:
41 self.assertEquals(state['commit'],
James E. Blairaf3d01e2017-05-26 14:30:18 -070042 str(repo.commit('HEAD')),
43 'Project %s commit for build %s #%s should '
44 'be correct' % (
45 project, build, number))
James E. Blairbcaa2d42017-05-26 15:44:23 -070046 ref = repo.commit('HEAD')
47 repo_messages = set(
48 [c.message.strip() for c in repo.iter_commits(ref)])
49 if 'present' in state:
50 for change in state['present']:
James E. Blairaf3d01e2017-05-26 14:30:18 -070051 msg = '%s-1' % change.subject
52 self.assertTrue(msg in repo_messages,
53 'Project %s for build %s #%s should '
54 'have change %s' % (
55 project, build, number, change.subject))
James E. Blairbcaa2d42017-05-26 15:44:23 -070056 if 'absent' in state:
57 for change in state['absent']:
James E. Blairaf3d01e2017-05-26 14:30:18 -070058 msg = '%s-1' % change.subject
59 self.assertTrue(msg not in repo_messages,
60 'Project %s for build %s #%s should '
61 'not have change %s' % (
62 project, build, number, change.subject))
63
James E. Blair987855f2017-06-06 14:41:58 -070064 def assertBuildStates(self, states, projects):
65 for number, build in enumerate(self.builds):
66 work = build.getWorkspaceRepos(projects)
67 state = states[number]
68
69 for project in projects:
70 self.assertRepoState(work[project], state[project],
71 project, build, number)
72
73 self.executor_server.hold_jobs_in_build = False
74 self.executor_server.release()
75 self.waitUntilSettled()
76
James E. Blaird8af5422017-05-24 13:59:40 -070077 @simple_layout('layouts/repo-checkout-two-project.yaml')
James E. Blair97d902e2014-08-21 13:25:56 -070078 def test_one_branch(self):
James E. Blaird8af5422017-05-24 13:59:40 -070079 self.executor_server.hold_jobs_in_build = True
Antoine Musso45dd2cb2014-01-29 17:17:43 +010080
James E. Blaird8af5422017-05-24 13:59:40 -070081 p1 = 'review.example.com/org/project1'
82 p2 = 'review.example.com/org/project2'
83 projects = [p1, p2]
Antoine Musso45dd2cb2014-01-29 17:17:43 +010084 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
85 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blaird8af5422017-05-24 13:59:40 -070086 A.addApproval('code-review', 2)
87 B.addApproval('code-review', 2)
88 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
89 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Antoine Musso45dd2cb2014-01-29 17:17:43 +010090
91 self.waitUntilSettled()
92
93 self.assertEquals(2, len(self.builds), "Two builds are running")
94
James E. Blair97d902e2014-08-21 13:25:56 -070095 upstream = self.getUpstreamRepos(projects)
96 states = [
James E. Blairbcaa2d42017-05-26 15:44:23 -070097 {p1: dict(present=[A], absent=[B], branch='master'),
98 p2: dict(commit=str(upstream[p2].commit('master')),
99 branch='master'),
James E. Blair97d902e2014-08-21 13:25:56 -0700100 },
James E. Blairbcaa2d42017-05-26 15:44:23 -0700101 {p1: dict(present=[A], absent=[B], branch='master'),
102 p2: dict(present=[B], absent=[A], branch='master'),
James E. Blair97d902e2014-08-21 13:25:56 -0700103 },
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000104 ]
James E. Blair97d902e2014-08-21 13:25:56 -0700105
James E. Blair987855f2017-06-06 14:41:58 -0700106 self.assertBuildStates(states, projects)
James E. Blair97d902e2014-08-21 13:25:56 -0700107
James E. Blairaf3d01e2017-05-26 14:30:18 -0700108 @simple_layout('layouts/repo-checkout-four-project.yaml')
James E. Blair97d902e2014-08-21 13:25:56 -0700109 def test_multi_branch(self):
James E. Blairaf3d01e2017-05-26 14:30:18 -0700110 self.executor_server.hold_jobs_in_build = True
111
112 p1 = 'review.example.com/org/project1'
113 p2 = 'review.example.com/org/project2'
114 p3 = 'review.example.com/org/project3'
115 p4 = 'review.example.com/org/project4'
116 projects = [p1, p2, p3, p4]
James E. Blair97d902e2014-08-21 13:25:56 -0700117
118 self.create_branch('org/project2', 'stable/havana')
119 self.create_branch('org/project4', 'stable/havana')
120 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000121 B = self.fake_gerrit.addFakeChange('org/project2', 'stable/havana',
122 'B')
James E. Blair97d902e2014-08-21 13:25:56 -0700123 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
James E. Blairaf3d01e2017-05-26 14:30:18 -0700124 A.addApproval('code-review', 2)
125 B.addApproval('code-review', 2)
126 C.addApproval('code-review', 2)
127 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
128 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
129 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair97d902e2014-08-21 13:25:56 -0700130
131 self.waitUntilSettled()
132
133 self.assertEquals(3, len(self.builds), "Three builds are running")
134
135 upstream = self.getUpstreamRepos(projects)
136 states = [
James E. Blairbcaa2d42017-05-26 15:44:23 -0700137 {p1: dict(present=[A], absent=[B, C], branch='master'),
138 p2: dict(commit=str(upstream[p2].commit('master')),
139 branch='master'),
140 p3: dict(commit=str(upstream[p3].commit('master')),
141 branch='master'),
142 p4: dict(commit=str(upstream[p4].commit('master')),
143 branch='master'),
James E. Blair97d902e2014-08-21 13:25:56 -0700144 },
James E. Blairbcaa2d42017-05-26 15:44:23 -0700145 {p1: dict(present=[A], absent=[B, C], branch='master'),
146 p2: dict(present=[B], absent=[A, C], branch='stable/havana'),
147 p3: dict(commit=str(upstream[p3].commit('master')),
148 branch='master'),
149 p4: dict(commit=str(upstream[p4].commit('stable/havana')),
150 branch='stable/havana'),
James E. Blair97d902e2014-08-21 13:25:56 -0700151 },
James E. Blairbcaa2d42017-05-26 15:44:23 -0700152 {p1: dict(present=[A], absent=[B, C], branch='master'),
153 p2: dict(commit=str(upstream[p2].commit('master')),
154 branch='master'),
155 p3: dict(present=[C], absent=[A, B], branch='master'),
156 p4: dict(commit=str(upstream[p4].commit('master')),
157 branch='master'),
James E. Blair97d902e2014-08-21 13:25:56 -0700158 },
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000159 ]
James E. Blair97d902e2014-08-21 13:25:56 -0700160
James E. Blair987855f2017-06-06 14:41:58 -0700161 self.assertBuildStates(states, projects)
James E. Blairbce35e12014-08-21 14:31:17 -0700162
James E. Blair4de8d9e2017-06-01 15:41:40 -0700163 @simple_layout('layouts/repo-checkout-six-project.yaml')
James E. Blairf0420222014-08-21 16:02:17 -0700164 def test_project_override(self):
James E. Blair4de8d9e2017-06-01 15:41:40 -0700165 self.executor_server.hold_jobs_in_build = True
166
167 p1 = 'review.example.com/org/project1'
168 p2 = 'review.example.com/org/project2'
169 p3 = 'review.example.com/org/project3'
170 p4 = 'review.example.com/org/project4'
171 p5 = 'review.example.com/org/project5'
172 p6 = 'review.example.com/org/project6'
173 projects = [p1, p2, p3, p4, p5, p6]
James E. Blairf0420222014-08-21 16:02:17 -0700174
175 self.create_branch('org/project3', 'stable/havana')
176 self.create_branch('org/project4', 'stable/havana')
177 self.create_branch('org/project6', 'stable/havana')
178 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
179 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
180 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000181 D = self.fake_gerrit.addFakeChange('org/project3', 'stable/havana',
182 'D')
James E. Blair4de8d9e2017-06-01 15:41:40 -0700183 A.addApproval('code-review', 2)
184 B.addApproval('code-review', 2)
185 C.addApproval('code-review', 2)
186 D.addApproval('code-review', 2)
187 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
188 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
189 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
190 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blairf0420222014-08-21 16:02:17 -0700191
192 self.waitUntilSettled()
193
194 self.assertEquals(4, len(self.builds), "Four builds are running")
195
196 upstream = self.getUpstreamRepos(projects)
197 states = [
James E. Blair4de8d9e2017-06-01 15:41:40 -0700198 {p1: dict(present=[A], absent=[B, C, D], branch='master'),
199 p2: dict(commit=str(upstream[p2].commit('master')),
200 branch='master'),
201 p3: dict(commit=str(upstream[p3].commit('master')),
202 branch='master'),
203 p4: dict(commit=str(upstream[p4].commit('master')),
204 branch='master'),
205 p5: dict(commit=str(upstream[p5].commit('master')),
206 branch='master'),
207 p6: dict(commit=str(upstream[p6].commit('master')),
208 branch='master'),
James E. Blairf0420222014-08-21 16:02:17 -0700209 },
James E. Blair4de8d9e2017-06-01 15:41:40 -0700210 {p1: dict(present=[A, B], absent=[C, D], branch='master'),
211 p2: dict(commit=str(upstream[p2].commit('master')),
212 branch='master'),
213 p3: dict(commit=str(upstream[p3].commit('master')),
214 branch='master'),
215 p4: dict(commit=str(upstream[p4].commit('master')),
216 branch='master'),
217 p5: dict(commit=str(upstream[p5].commit('master')),
218 branch='master'),
219 p6: dict(commit=str(upstream[p6].commit('master')),
220 branch='master'),
James E. Blairf0420222014-08-21 16:02:17 -0700221 },
James E. Blair4de8d9e2017-06-01 15:41:40 -0700222 {p1: dict(present=[A, B], absent=[C, D], branch='master'),
223 p2: dict(present=[C], absent=[A, B, D], branch='master'),
224 p3: dict(commit=str(upstream[p3].commit('master')),
225 branch='master'),
226 p4: dict(commit=str(upstream[p4].commit('master')),
227 branch='master'),
228 p5: dict(commit=str(upstream[p5].commit('master')),
229 branch='master'),
230 p6: dict(commit=str(upstream[p6].commit('master')),
231 branch='master'),
James E. Blairf0420222014-08-21 16:02:17 -0700232 },
James E. Blair4de8d9e2017-06-01 15:41:40 -0700233 {p1: dict(present=[A, B], absent=[C, D], branch='master'),
234 p2: dict(present=[C], absent=[A, B, D], branch='master'),
235 p3: dict(present=[D], absent=[A, B, C],
236 branch='stable/havana'),
237 p4: dict(commit=str(upstream[p4].commit('master')),
238 branch='master'),
239 p5: dict(commit=str(upstream[p5].commit('master')),
240 branch='master'),
241 p6: dict(commit=str(upstream[p6].commit('stable/havana')),
242 branch='stable/havana'),
James E. Blairf0420222014-08-21 16:02:17 -0700243 },
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000244 ]
James E. Blairf0420222014-08-21 16:02:17 -0700245
James E. Blair987855f2017-06-06 14:41:58 -0700246 self.assertBuildStates(states, projects)
James E. Blair217b10d2015-01-08 16:25:28 -0800247
248 def test_periodic(self):
James E. Blair95ae4fc2017-06-01 15:54:53 -0700249 # This test can not use simple_layout because it must start
250 # with a configuration which does not include a
251 # timer-triggered job so that we have an opportunity to set
252 # the hold flag before the first job.
253 self.executor_server.hold_jobs_in_build = True
254 # Start timer trigger - also org/project
255 self.commitConfigUpdate('common-config',
256 'layouts/repo-checkout-timer.yaml')
James E. Blair217b10d2015-01-08 16:25:28 -0800257 self.sched.reconfigure(self.config)
James E. Blair95ae4fc2017-06-01 15:54:53 -0700258
259 p1 = 'review.example.com/org/project1'
260 projects = [p1]
261 self.create_branch('org/project1', 'stable/havana')
James E. Blair217b10d2015-01-08 16:25:28 -0800262
263 # The pipeline triggers every second, so we should have seen
264 # several by now.
265 time.sleep(5)
266 self.waitUntilSettled()
267
James E. Blair217b10d2015-01-08 16:25:28 -0800268 # Stop queuing timer triggered jobs so that the assertions
269 # below don't race against more jobs being queued.
James E. Blair95ae4fc2017-06-01 15:54:53 -0700270 self.commitConfigUpdate('common-config',
271 'layouts/repo-checkout-no-timer.yaml')
James E. Blair217b10d2015-01-08 16:25:28 -0800272 self.sched.reconfigure(self.config)
James E. Blair217b10d2015-01-08 16:25:28 -0800273
James E. Blair95ae4fc2017-06-01 15:54:53 -0700274 self.assertEquals(1, len(self.builds), "One build is running")
James E. Blair217b10d2015-01-08 16:25:28 -0800275
276 upstream = self.getUpstreamRepos(projects)
277 states = [
James E. Blair95ae4fc2017-06-01 15:54:53 -0700278 {p1: dict(commit=str(upstream[p1].commit('stable/havana')),
279 branch='stable/havana'),
James E. Blair217b10d2015-01-08 16:25:28 -0800280 },
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000281 ]
James E. Blair217b10d2015-01-08 16:25:28 -0800282
James E. Blair987855f2017-06-06 14:41:58 -0700283 self.assertBuildStates(states, projects)
Sachi King9f16d522016-03-16 12:20:45 +1100284
James E. Blairbbe811e2017-06-02 10:47:44 -0700285 @simple_layout('layouts/repo-checkout-post.yaml')
Sachi King9f16d522016-03-16 12:20:45 +1100286 def test_post_and_master_checkout(self):
James E. Blair1de2e492017-06-02 11:04:42 -0700287 self.executor_server.hold_jobs_in_build = True
288 p1 = "review.example.com/org/project1"
289 p2 = "review.example.com/org/project2"
290 projects = [p1, p2]
James E. Blair8cce42e2016-10-18 08:18:36 -0700291
James E. Blair1de2e492017-06-02 11:04:42 -0700292 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
James E. Blair8cce42e2016-10-18 08:18:36 -0700293 event = A.getRefUpdatedEvent()
294 A.setMerged()
295 self.fake_gerrit.addEvent(event)
296 self.waitUntilSettled()
297
James E. Blair8cce42e2016-10-18 08:18:36 -0700298 upstream = self.getUpstreamRepos(projects)
James E. Blair1de2e492017-06-02 11:04:42 -0700299 states = [
300 {p1: dict(commit=str(upstream[p1].commit('master')),
301 present=[A], branch='master'),
302 p2: dict(commit=str(upstream[p2].commit('master')),
303 absent=[A], branch='master'),
304 },
305 ]
James E. Blair8cce42e2016-10-18 08:18:36 -0700306
James E. Blair987855f2017-06-06 14:41:58 -0700307 self.assertBuildStates(states, projects)