blob: e65904bf02ab960a77c9931e14f518bf6201bbdd [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
19import os
20import shutil
James E. Blair217b10d2015-01-08 16:25:28 -080021import time
Antoine Musso45dd2cb2014-01-29 17:17:43 +010022
23import git
24
25import zuul.lib.cloner
26
27from tests.base import ZuulTestCase
Antoine Musso45dd2cb2014-01-29 17:17:43 +010028
Antoine Musso45dd2cb2014-01-29 17:17:43 +010029
30class TestCloner(ZuulTestCase):
31
32 log = logging.getLogger("zuul.test.cloner")
33 workspace_root = None
34
35 def setUp(self):
James E. Blair509cada2015-12-08 16:31:16 -080036 self.skip("Disabled for early v3 development")
37
Antoine Musso45dd2cb2014-01-29 17:17:43 +010038 super(TestCloner, self).setUp()
39 self.workspace_root = os.path.join(self.test_root, 'workspace')
40
James E. Blairf84026c2015-12-08 16:11:46 -080041 self.updateConfigLayout(
42 'tests/fixtures/layout-cloner.yaml')
Antoine Musso45dd2cb2014-01-29 17:17:43 +010043 self.sched.reconfigure(self.config)
44 self.registerJobs()
45
James E. Blair97d902e2014-08-21 13:25:56 -070046 def getWorkspaceRepos(self, projects):
47 repos = {}
48 for project in projects:
49 repos[project] = git.Repo(
50 os.path.join(self.workspace_root, project))
51 return repos
52
53 def getUpstreamRepos(self, projects):
54 repos = {}
55 for project in projects:
56 repos[project] = git.Repo(
57 os.path.join(self.upstream_root, project))
58 return repos
59
James E. Blair48ff4d52014-08-25 15:07:21 -070060 def test_cache_dir(self):
61 projects = ['org/project1', 'org/project2']
62 cache_root = os.path.join(self.test_root, "cache")
63 for project in projects:
64 upstream_repo_path = os.path.join(self.upstream_root, project)
65 cache_repo_path = os.path.join(cache_root, project)
66 git.Repo.clone_from(upstream_repo_path, cache_repo_path)
67
68 self.worker.hold_jobs_in_build = True
69 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
70 A.addApproval('CRVW', 2)
71 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
72
73 self.waitUntilSettled()
74
75 self.assertEquals(1, len(self.builds), "One build is running")
76
77 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
78 B.setMerged()
79
80 upstream = self.getUpstreamRepos(projects)
Joshua Hesketh29d99b72014-08-19 16:27:42 +100081 states = [{
82 'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
83 'org/project2': str(upstream['org/project2'].commit('master')),
84 }]
James E. Blair48ff4d52014-08-25 15:07:21 -070085
86 for number, build in enumerate(self.builds):
87 self.log.debug("Build parameters: %s", build.parameters)
88 cloner = zuul.lib.cloner.Cloner(
89 git_base_url=self.upstream_root,
90 projects=projects,
91 workspace=self.workspace_root,
James E. Blairbfd85fd2016-10-21 14:18:11 -070092 zuul_project=build.parameters.get('ZUUL_PROJECT', None),
James E. Blair48ff4d52014-08-25 15:07:21 -070093 zuul_branch=build.parameters['ZUUL_BRANCH'],
94 zuul_ref=build.parameters['ZUUL_REF'],
Monty Taylord642d852017-02-23 14:05:42 -050095 zuul_url=self.src_root,
James E. Blair48ff4d52014-08-25 15:07:21 -070096 cache_dir=cache_root,
Joshua Hesketh29d99b72014-08-19 16:27:42 +100097 )
James E. Blair48ff4d52014-08-25 15:07:21 -070098 cloner.execute()
99 work = self.getWorkspaceRepos(projects)
100 state = states[number]
101
102 for project in projects:
103 self.assertEquals(state[project],
104 str(work[project].commit('HEAD')),
105 'Project %s commit for build %s should '
106 'be correct' % (project, number))
107
108 work = self.getWorkspaceRepos(projects)
James E. Blair8cce42e2016-10-18 08:18:36 -0700109 # project1 is the zuul_project so the origin should be set to the
110 # zuul_url since that is the most up to date.
111 cache_repo_path = os.path.join(cache_root, 'org/project1')
112 self.assertNotEqual(
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000113 work['org/project1'].remotes.origin.url,
James E. Blair8cce42e2016-10-18 08:18:36 -0700114 cache_repo_path,
115 'workspace repo origin should not be the cache'
116 )
117 zuul_url_repo_path = os.path.join(self.git_root, 'org/project1')
118 self.assertEqual(
119 work['org/project1'].remotes.origin.url,
120 zuul_url_repo_path,
121 'workspace repo origin should be the zuul url'
122 )
123
124 # project2 is not the zuul_project so the origin should be set
125 # to upstream since that is the best we can do
126 cache_repo_path = os.path.join(cache_root, 'org/project2')
127 self.assertNotEqual(
128 work['org/project2'].remotes.origin.url,
129 cache_repo_path,
130 'workspace repo origin should not be the cache'
131 )
132 upstream_repo_path = os.path.join(self.upstream_root, 'org/project2')
133 self.assertEqual(
134 work['org/project2'].remotes.origin.url,
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000135 upstream_repo_path,
James E. Blair8cce42e2016-10-18 08:18:36 -0700136 'workspace repo origin should be the upstream url'
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000137 )
James E. Blair48ff4d52014-08-25 15:07:21 -0700138
139 self.worker.hold_jobs_in_build = False
140 self.worker.release()
141 self.waitUntilSettled()
142
James E. Blair97d902e2014-08-21 13:25:56 -0700143 def test_one_branch(self):
Antoine Musso45dd2cb2014-01-29 17:17:43 +0100144 self.worker.hold_jobs_in_build = True
145
James E. Blair97d902e2014-08-21 13:25:56 -0700146 projects = ['org/project1', 'org/project2']
Antoine Musso45dd2cb2014-01-29 17:17:43 +0100147 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
148 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Antoine Musso45dd2cb2014-01-29 17:17:43 +0100149 A.addApproval('CRVW', 2)
150 B.addApproval('CRVW', 2)
151 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
152 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
153
154 self.waitUntilSettled()
155
156 self.assertEquals(2, len(self.builds), "Two builds are running")
157
James E. Blair97d902e2014-08-21 13:25:56 -0700158 upstream = self.getUpstreamRepos(projects)
159 states = [
160 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
161 'org/project2': str(upstream['org/project2'].commit('master')),
162 },
163 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
164 'org/project2': self.builds[1].parameters['ZUUL_COMMIT'],
165 },
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000166 ]
James E. Blair97d902e2014-08-21 13:25:56 -0700167
168 for number, build in enumerate(self.builds):
Antoine Musso45dd2cb2014-01-29 17:17:43 +0100169 self.log.debug("Build parameters: %s", build.parameters)
Antoine Musso45dd2cb2014-01-29 17:17:43 +0100170 cloner = zuul.lib.cloner.Cloner(
171 git_base_url=self.upstream_root,
James E. Blair97d902e2014-08-21 13:25:56 -0700172 projects=projects,
Antoine Musso45dd2cb2014-01-29 17:17:43 +0100173 workspace=self.workspace_root,
James E. Blairbfd85fd2016-10-21 14:18:11 -0700174 zuul_project=build.parameters.get('ZUUL_PROJECT', None),
James E. Blair97d902e2014-08-21 13:25:56 -0700175 zuul_branch=build.parameters['ZUUL_BRANCH'],
James E. Blair82318922014-08-21 11:05:32 -0700176 zuul_ref=build.parameters['ZUUL_REF'],
Monty Taylord642d852017-02-23 14:05:42 -0500177 zuul_url=self.src_root,
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000178 )
Antoine Musso45dd2cb2014-01-29 17:17:43 +0100179 cloner.execute()
James E. Blair97d902e2014-08-21 13:25:56 -0700180 work = self.getWorkspaceRepos(projects)
181 state = states[number]
182
183 for project in projects:
184 self.assertEquals(state[project],
185 str(work[project].commit('HEAD')),
186 'Project %s commit for build %s should '
187 'be correct' % (project, number))
188
189 shutil.rmtree(self.workspace_root)
190
191 self.worker.hold_jobs_in_build = False
192 self.worker.release()
193 self.waitUntilSettled()
194
195 def test_multi_branch(self):
196 self.worker.hold_jobs_in_build = True
197 projects = ['org/project1', 'org/project2',
198 'org/project3', 'org/project4']
199
200 self.create_branch('org/project2', 'stable/havana')
201 self.create_branch('org/project4', 'stable/havana')
202 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000203 B = self.fake_gerrit.addFakeChange('org/project2', 'stable/havana',
204 'B')
James E. Blair97d902e2014-08-21 13:25:56 -0700205 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
206 A.addApproval('CRVW', 2)
207 B.addApproval('CRVW', 2)
208 C.addApproval('CRVW', 2)
209 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
210 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
211 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
212
213 self.waitUntilSettled()
214
215 self.assertEquals(3, len(self.builds), "Three builds are running")
216
217 upstream = self.getUpstreamRepos(projects)
218 states = [
219 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
220 'org/project2': str(upstream['org/project2'].commit('master')),
221 'org/project3': str(upstream['org/project3'].commit('master')),
222 'org/project4': str(upstream['org/project4'].
223 commit('master')),
224 },
225 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
226 'org/project2': self.builds[1].parameters['ZUUL_COMMIT'],
227 'org/project3': str(upstream['org/project3'].commit('master')),
228 'org/project4': str(upstream['org/project4'].
229 commit('stable/havana')),
230 },
231 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
232 'org/project2': str(upstream['org/project2'].commit('master')),
233 'org/project3': self.builds[2].parameters['ZUUL_COMMIT'],
234 'org/project4': str(upstream['org/project4'].
235 commit('master')),
236 },
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000237 ]
James E. Blair97d902e2014-08-21 13:25:56 -0700238
239 for number, build in enumerate(self.builds):
240 self.log.debug("Build parameters: %s", build.parameters)
241 cloner = zuul.lib.cloner.Cloner(
242 git_base_url=self.upstream_root,
243 projects=projects,
244 workspace=self.workspace_root,
James E. Blairbfd85fd2016-10-21 14:18:11 -0700245 zuul_project=build.parameters.get('ZUUL_PROJECT', None),
James E. Blair97d902e2014-08-21 13:25:56 -0700246 zuul_branch=build.parameters['ZUUL_BRANCH'],
247 zuul_ref=build.parameters['ZUUL_REF'],
Monty Taylord642d852017-02-23 14:05:42 -0500248 zuul_url=self.src_root,
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000249 )
James E. Blair97d902e2014-08-21 13:25:56 -0700250 cloner.execute()
251 work = self.getWorkspaceRepos(projects)
252 state = states[number]
253
254 for project in projects:
255 self.assertEquals(state[project],
256 str(work[project].commit('HEAD')),
257 'Project %s commit for build %s should '
258 'be correct' % (project, number))
Antoine Musso45dd2cb2014-01-29 17:17:43 +0100259 shutil.rmtree(self.workspace_root)
James E. Blair82318922014-08-21 11:05:32 -0700260
261 self.worker.hold_jobs_in_build = False
262 self.worker.release()
263 self.waitUntilSettled()
James E. Blairbce35e12014-08-21 14:31:17 -0700264
265 def test_upgrade(self):
266 # Simulates an upgrade test
267 self.worker.hold_jobs_in_build = True
268 projects = ['org/project1', 'org/project2', 'org/project3',
269 'org/project4', 'org/project5', 'org/project6']
270
271 self.create_branch('org/project2', 'stable/havana')
272 self.create_branch('org/project3', 'stable/havana')
273 self.create_branch('org/project4', 'stable/havana')
274 self.create_branch('org/project5', 'stable/havana')
275 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
276 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000277 C = self.fake_gerrit.addFakeChange('org/project3', 'stable/havana',
278 'C')
James E. Blairbce35e12014-08-21 14:31:17 -0700279 D = self.fake_gerrit.addFakeChange('org/project3', 'master', 'D')
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000280 E = self.fake_gerrit.addFakeChange('org/project4', 'stable/havana',
281 'E')
James E. Blairbce35e12014-08-21 14:31:17 -0700282 A.addApproval('CRVW', 2)
283 B.addApproval('CRVW', 2)
284 C.addApproval('CRVW', 2)
285 D.addApproval('CRVW', 2)
286 E.addApproval('CRVW', 2)
287 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
288 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
289 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
290 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
291 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
292
293 self.waitUntilSettled()
294
295 self.assertEquals(5, len(self.builds), "Five builds are running")
296
297 # Check the old side of the upgrade first
298 upstream = self.getUpstreamRepos(projects)
299 states = [
300 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000301 'org/project2': str(upstream['org/project2'].commit(
302 'stable/havana')),
303 'org/project3': str(upstream['org/project3'].commit(
304 'stable/havana')),
305 'org/project4': str(upstream['org/project4'].commit(
306 'stable/havana')),
307 'org/project5': str(upstream['org/project5'].commit(
308 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700309 'org/project6': str(upstream['org/project6'].commit('master')),
310 },
311 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000312 'org/project2': str(upstream['org/project2'].commit(
313 'stable/havana')),
314 'org/project3': str(upstream['org/project3'].commit(
315 'stable/havana')),
316 'org/project4': str(upstream['org/project4'].commit(
317 'stable/havana')),
318 'org/project5': str(upstream['org/project5'].commit(
319 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700320 'org/project6': str(upstream['org/project6'].commit('master')),
321 },
322 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000323 'org/project2': str(upstream['org/project2'].commit(
324 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700325 'org/project3': self.builds[2].parameters['ZUUL_COMMIT'],
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000326 'org/project4': str(upstream['org/project4'].commit(
327 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700328
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000329 'org/project5': str(upstream['org/project5'].commit(
330 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700331 'org/project6': str(upstream['org/project6'].commit('master')),
332 },
333 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000334 'org/project2': str(upstream['org/project2'].commit(
335 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700336 'org/project3': self.builds[2].parameters['ZUUL_COMMIT'],
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000337 'org/project4': str(upstream['org/project4'].commit(
338 'stable/havana')),
339 'org/project5': str(upstream['org/project5'].commit(
340 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700341 'org/project6': str(upstream['org/project6'].commit('master')),
342 },
343 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000344 'org/project2': str(upstream['org/project2'].commit(
345 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700346 'org/project3': self.builds[2].parameters['ZUUL_COMMIT'],
347 'org/project4': self.builds[4].parameters['ZUUL_COMMIT'],
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000348 'org/project5': str(upstream['org/project5'].commit(
349 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700350 'org/project6': str(upstream['org/project6'].commit('master')),
351 },
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000352 ]
James E. Blairbce35e12014-08-21 14:31:17 -0700353
354 for number, build in enumerate(self.builds):
355 self.log.debug("Build parameters: %s", build.parameters)
James E. Blairbce35e12014-08-21 14:31:17 -0700356 cloner = zuul.lib.cloner.Cloner(
357 git_base_url=self.upstream_root,
358 projects=projects,
359 workspace=self.workspace_root,
James E. Blairbfd85fd2016-10-21 14:18:11 -0700360 zuul_project=build.parameters.get('ZUUL_PROJECT', None),
James E. Blairbce35e12014-08-21 14:31:17 -0700361 zuul_branch=build.parameters['ZUUL_BRANCH'],
362 zuul_ref=build.parameters['ZUUL_REF'],
Monty Taylord642d852017-02-23 14:05:42 -0500363 zuul_url=self.src_root,
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000364 branch='stable/havana', # Old branch for upgrade
365 )
James E. Blairbce35e12014-08-21 14:31:17 -0700366 cloner.execute()
367 work = self.getWorkspaceRepos(projects)
368 state = states[number]
369
370 for project in projects:
371 self.assertEquals(state[project],
372 str(work[project].commit('HEAD')),
373 'Project %s commit for build %s should '
374 'be correct on old side of upgrade' %
375 (project, number))
376 shutil.rmtree(self.workspace_root)
377
378 # Check the new side of the upgrade
379 states = [
380 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
381 'org/project2': str(upstream['org/project2'].commit('master')),
382 'org/project3': str(upstream['org/project3'].commit('master')),
383 'org/project4': str(upstream['org/project4'].commit('master')),
384 'org/project5': str(upstream['org/project5'].commit('master')),
385 'org/project6': str(upstream['org/project6'].commit('master')),
386 },
387 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
388 'org/project2': self.builds[1].parameters['ZUUL_COMMIT'],
389 'org/project3': str(upstream['org/project3'].commit('master')),
390 'org/project4': str(upstream['org/project4'].commit('master')),
391 'org/project5': str(upstream['org/project5'].commit('master')),
392 'org/project6': str(upstream['org/project6'].commit('master')),
393 },
394 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
395 'org/project2': self.builds[1].parameters['ZUUL_COMMIT'],
396 'org/project3': str(upstream['org/project3'].commit('master')),
397 'org/project4': str(upstream['org/project4'].commit('master')),
398 'org/project5': str(upstream['org/project5'].commit('master')),
399 'org/project6': str(upstream['org/project6'].commit('master')),
400 },
401 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
402 'org/project2': self.builds[1].parameters['ZUUL_COMMIT'],
403 'org/project3': self.builds[3].parameters['ZUUL_COMMIT'],
404 'org/project4': str(upstream['org/project4'].commit('master')),
405 'org/project5': str(upstream['org/project5'].commit('master')),
406 'org/project6': str(upstream['org/project6'].commit('master')),
407 },
408 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
409 'org/project2': self.builds[1].parameters['ZUUL_COMMIT'],
410 'org/project3': self.builds[3].parameters['ZUUL_COMMIT'],
411 'org/project4': str(upstream['org/project4'].commit('master')),
412 'org/project5': str(upstream['org/project5'].commit('master')),
413 'org/project6': str(upstream['org/project6'].commit('master')),
414 },
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000415 ]
James E. Blairbce35e12014-08-21 14:31:17 -0700416
417 for number, build in enumerate(self.builds):
418 self.log.debug("Build parameters: %s", build.parameters)
James E. Blairbce35e12014-08-21 14:31:17 -0700419 cloner = zuul.lib.cloner.Cloner(
420 git_base_url=self.upstream_root,
421 projects=projects,
422 workspace=self.workspace_root,
James E. Blairbfd85fd2016-10-21 14:18:11 -0700423 zuul_project=build.parameters.get('ZUUL_PROJECT', None),
James E. Blairbce35e12014-08-21 14:31:17 -0700424 zuul_branch=build.parameters['ZUUL_BRANCH'],
425 zuul_ref=build.parameters['ZUUL_REF'],
Monty Taylord642d852017-02-23 14:05:42 -0500426 zuul_url=self.src_root,
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000427 branch='master', # New branch for upgrade
428 )
James E. Blairbce35e12014-08-21 14:31:17 -0700429 cloner.execute()
430 work = self.getWorkspaceRepos(projects)
431 state = states[number]
432
433 for project in projects:
434 self.assertEquals(state[project],
435 str(work[project].commit('HEAD')),
436 'Project %s commit for build %s should '
437 'be correct on old side of upgrade' %
438 (project, number))
439 shutil.rmtree(self.workspace_root)
440
441 self.worker.hold_jobs_in_build = False
442 self.worker.release()
443 self.waitUntilSettled()
James E. Blairf0420222014-08-21 16:02:17 -0700444
445 def test_project_override(self):
446 self.worker.hold_jobs_in_build = True
447 projects = ['org/project1', 'org/project2', 'org/project3',
448 'org/project4', 'org/project5', 'org/project6']
449
450 self.create_branch('org/project3', 'stable/havana')
451 self.create_branch('org/project4', 'stable/havana')
452 self.create_branch('org/project6', 'stable/havana')
453 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
454 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
455 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000456 D = self.fake_gerrit.addFakeChange('org/project3', 'stable/havana',
457 'D')
James E. Blairf0420222014-08-21 16:02:17 -0700458 A.addApproval('CRVW', 2)
459 B.addApproval('CRVW', 2)
460 C.addApproval('CRVW', 2)
461 D.addApproval('CRVW', 2)
462 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
463 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
464 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
465 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
466
467 self.waitUntilSettled()
468
469 self.assertEquals(4, len(self.builds), "Four builds are running")
470
471 upstream = self.getUpstreamRepos(projects)
472 states = [
473 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
474 'org/project2': str(upstream['org/project2'].commit('master')),
475 'org/project3': str(upstream['org/project3'].commit('master')),
476 'org/project4': str(upstream['org/project4'].commit('master')),
477 'org/project5': str(upstream['org/project5'].commit('master')),
478 'org/project6': str(upstream['org/project6'].commit('master')),
479 },
480 {'org/project1': self.builds[1].parameters['ZUUL_COMMIT'],
481 'org/project2': str(upstream['org/project2'].commit('master')),
482 'org/project3': str(upstream['org/project3'].commit('master')),
483 'org/project4': str(upstream['org/project4'].commit('master')),
484 'org/project5': str(upstream['org/project5'].commit('master')),
485 'org/project6': str(upstream['org/project6'].commit('master')),
486 },
487 {'org/project1': self.builds[1].parameters['ZUUL_COMMIT'],
488 'org/project2': self.builds[2].parameters['ZUUL_COMMIT'],
489 'org/project3': str(upstream['org/project3'].commit('master')),
490 'org/project4': str(upstream['org/project4'].commit('master')),
491 'org/project5': str(upstream['org/project5'].commit('master')),
492 'org/project6': str(upstream['org/project6'].commit('master')),
493 },
494 {'org/project1': self.builds[1].parameters['ZUUL_COMMIT'],
495 'org/project2': self.builds[2].parameters['ZUUL_COMMIT'],
496 'org/project3': self.builds[3].parameters['ZUUL_COMMIT'],
497 'org/project4': str(upstream['org/project4'].commit('master')),
498 'org/project5': str(upstream['org/project5'].commit('master')),
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000499 'org/project6': str(upstream['org/project6'].commit(
500 'stable/havana')),
James E. Blairf0420222014-08-21 16:02:17 -0700501 },
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000502 ]
James E. Blairf0420222014-08-21 16:02:17 -0700503
504 for number, build in enumerate(self.builds):
505 self.log.debug("Build parameters: %s", build.parameters)
James E. Blairf0420222014-08-21 16:02:17 -0700506 cloner = zuul.lib.cloner.Cloner(
507 git_base_url=self.upstream_root,
508 projects=projects,
509 workspace=self.workspace_root,
James E. Blairbfd85fd2016-10-21 14:18:11 -0700510 zuul_project=build.parameters.get('ZUUL_PROJECT', None),
James E. Blairf0420222014-08-21 16:02:17 -0700511 zuul_branch=build.parameters['ZUUL_BRANCH'],
512 zuul_ref=build.parameters['ZUUL_REF'],
Monty Taylord642d852017-02-23 14:05:42 -0500513 zuul_url=self.src_root,
James E. Blairf0420222014-08-21 16:02:17 -0700514 project_branches={'org/project4': 'master'},
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000515 )
James E. Blairf0420222014-08-21 16:02:17 -0700516 cloner.execute()
517 work = self.getWorkspaceRepos(projects)
518 state = states[number]
519
520 for project in projects:
521 self.assertEquals(state[project],
522 str(work[project].commit('HEAD')),
523 'Project %s commit for build %s should '
524 'be correct' % (project, number))
525 shutil.rmtree(self.workspace_root)
526
527 self.worker.hold_jobs_in_build = False
528 self.worker.release()
529 self.waitUntilSettled()
James E. Blair217b10d2015-01-08 16:25:28 -0800530
531 def test_periodic(self):
532 self.worker.hold_jobs_in_build = True
533 self.create_branch('org/project', 'stable/havana')
James E. Blairf84026c2015-12-08 16:11:46 -0800534 self.updateConfigLayout(
535 'tests/fixtures/layout-timer.yaml')
James E. Blair217b10d2015-01-08 16:25:28 -0800536 self.sched.reconfigure(self.config)
537 self.registerJobs()
538
539 # The pipeline triggers every second, so we should have seen
540 # several by now.
541 time.sleep(5)
542 self.waitUntilSettled()
543
544 builds = self.builds[:]
545
546 self.worker.hold_jobs_in_build = False
547 # Stop queuing timer triggered jobs so that the assertions
548 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -0800549 self.updateConfigLayout(
550 'tests/fixtures/layout-no-timer.yaml')
James E. Blair217b10d2015-01-08 16:25:28 -0800551 self.sched.reconfigure(self.config)
552 self.registerJobs()
553 self.worker.release()
554 self.waitUntilSettled()
555
556 projects = ['org/project']
557
558 self.assertEquals(2, len(builds), "Two builds are running")
559
560 upstream = self.getUpstreamRepos(projects)
561 states = [
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000562 {'org/project':
563 str(upstream['org/project'].commit('stable/havana')),
James E. Blair217b10d2015-01-08 16:25:28 -0800564 },
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000565 {'org/project':
566 str(upstream['org/project'].commit('stable/havana')),
James E. Blair217b10d2015-01-08 16:25:28 -0800567 },
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000568 ]
James E. Blair217b10d2015-01-08 16:25:28 -0800569
570 for number, build in enumerate(builds):
571 self.log.debug("Build parameters: %s", build.parameters)
572 cloner = zuul.lib.cloner.Cloner(
573 git_base_url=self.upstream_root,
574 projects=projects,
575 workspace=self.workspace_root,
James E. Blairbfd85fd2016-10-21 14:18:11 -0700576 zuul_project=build.parameters.get('ZUUL_PROJECT', None),
James E. Blair217b10d2015-01-08 16:25:28 -0800577 zuul_branch=build.parameters.get('ZUUL_BRANCH', None),
578 zuul_ref=build.parameters.get('ZUUL_REF', None),
Monty Taylord642d852017-02-23 14:05:42 -0500579 zuul_url=self.src_root,
James E. Blair217b10d2015-01-08 16:25:28 -0800580 branch='stable/havana',
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000581 )
James E. Blair217b10d2015-01-08 16:25:28 -0800582 cloner.execute()
583 work = self.getWorkspaceRepos(projects)
584 state = states[number]
585
586 for project in projects:
587 self.assertEquals(state[project],
588 str(work[project].commit('HEAD')),
589 'Project %s commit for build %s should '
590 'be correct' % (project, number))
591
592 shutil.rmtree(self.workspace_root)
593
594 self.worker.hold_jobs_in_build = False
595 self.worker.release()
596 self.waitUntilSettled()
Sachi King9f16d522016-03-16 12:20:45 +1100597
James E. Blairbfd85fd2016-10-21 14:18:11 -0700598 def test_periodic_update(self):
599 # Test that the merger correctly updates its local repository
600 # before running a periodic job.
601
602 # Prime the merger with the current state
603 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
604 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
605 self.waitUntilSettled()
606
607 # Merge a different change
608 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
609 B.setMerged()
610
611 # Start a periodic job
612 self.worker.hold_jobs_in_build = True
Paul Belanger174a8272017-03-14 13:20:10 -0400613 self.executor.negative_function_cache_ttl = 0
James E. Blairbfd85fd2016-10-21 14:18:11 -0700614 self.config.set('zuul', 'layout_config',
615 'tests/fixtures/layout-timer.yaml')
616 self.sched.reconfigure(self.config)
617 self.registerJobs()
618
619 # The pipeline triggers every second, so we should have seen
620 # several by now.
621 time.sleep(5)
622 self.waitUntilSettled()
623
624 builds = self.builds[:]
625
626 self.worker.hold_jobs_in_build = False
627 # Stop queuing timer triggered jobs so that the assertions
628 # below don't race against more jobs being queued.
629 self.config.set('zuul', 'layout_config',
630 'tests/fixtures/layout-no-timer.yaml')
631 self.sched.reconfigure(self.config)
632 self.registerJobs()
633 self.worker.release()
634 self.waitUntilSettled()
635
636 projects = ['org/project']
637
638 self.assertEquals(2, len(builds), "Two builds are running")
639
640 upstream = self.getUpstreamRepos(projects)
641 self.assertEqual(upstream['org/project'].commit('master').hexsha,
642 B.patchsets[0]['revision'])
643 states = [
644 {'org/project':
645 str(upstream['org/project'].commit('master')),
646 },
647 {'org/project':
648 str(upstream['org/project'].commit('master')),
649 },
650 ]
651
652 for number, build in enumerate(builds):
653 self.log.debug("Build parameters: %s", build.parameters)
654 cloner = zuul.lib.cloner.Cloner(
655 git_base_url=self.upstream_root,
656 projects=projects,
657 workspace=self.workspace_root,
658 zuul_project=build.parameters.get('ZUUL_PROJECT', None),
659 zuul_branch=build.parameters.get('ZUUL_BRANCH', None),
660 zuul_ref=build.parameters.get('ZUUL_REF', None),
661 zuul_url=self.git_root,
662 )
663 cloner.execute()
664 work = self.getWorkspaceRepos(projects)
665 state = states[number]
666
667 for project in projects:
668 self.assertEquals(state[project],
669 str(work[project].commit('HEAD')),
670 'Project %s commit for build %s should '
671 'be correct' % (project, number))
672
673 shutil.rmtree(self.workspace_root)
674
675 self.worker.hold_jobs_in_build = False
676 self.worker.release()
677 self.waitUntilSettled()
678
Sachi King9f16d522016-03-16 12:20:45 +1100679 def test_post_checkout(self):
James E. Blair8cce42e2016-10-18 08:18:36 -0700680 self.worker.hold_jobs_in_build = True
681 project = "org/project1"
682
683 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
684 event = A.getRefUpdatedEvent()
685 A.setMerged()
686 self.fake_gerrit.addEvent(event)
687 self.waitUntilSettled()
688
689 build = self.builds[0]
690 state = {'org/project1': build.parameters['ZUUL_COMMIT']}
691
692 build.release()
693 self.waitUntilSettled()
Sachi King9f16d522016-03-16 12:20:45 +1100694
695 cloner = zuul.lib.cloner.Cloner(
696 git_base_url=self.upstream_root,
697 projects=[project],
698 workspace=self.workspace_root,
James E. Blair8cce42e2016-10-18 08:18:36 -0700699 zuul_project=build.parameters.get('ZUUL_PROJECT', None),
700 zuul_branch=build.parameters.get('ZUUL_BRANCH', None),
701 zuul_ref=build.parameters.get('ZUUL_REF', None),
702 zuul_newrev=build.parameters.get('ZUUL_NEWREV', None),
Sachi King9f16d522016-03-16 12:20:45 +1100703 zuul_url=self.git_root,
Sachi King9f16d522016-03-16 12:20:45 +1100704 )
705 cloner.execute()
James E. Blair8cce42e2016-10-18 08:18:36 -0700706 work = self.getWorkspaceRepos([project])
707 self.assertEquals(state[project],
708 str(work[project].commit('HEAD')),
709 'Project %s commit for build %s should '
710 'be correct' % (project, 0))
711 shutil.rmtree(self.workspace_root)
Sachi King9f16d522016-03-16 12:20:45 +1100712
713 def test_post_and_master_checkout(self):
James E. Blair8cce42e2016-10-18 08:18:36 -0700714 self.worker.hold_jobs_in_build = True
715 projects = ["org/project1", "org/project2"]
716
717 A = self.fake_gerrit.addFakeChange(projects[0], 'master', 'A')
718 event = A.getRefUpdatedEvent()
719 A.setMerged()
720 self.fake_gerrit.addEvent(event)
721 self.waitUntilSettled()
722
723 build = self.builds[0]
724 upstream = self.getUpstreamRepos(projects)
725 state = {'org/project1':
726 build.parameters['ZUUL_COMMIT'],
727 'org/project2':
728 str(upstream['org/project2'].commit('master')),
729 }
730
731 build.release()
732 self.waitUntilSettled()
Sachi King9f16d522016-03-16 12:20:45 +1100733
734 cloner = zuul.lib.cloner.Cloner(
735 git_base_url=self.upstream_root,
James E. Blair8cce42e2016-10-18 08:18:36 -0700736 projects=projects,
Sachi King9f16d522016-03-16 12:20:45 +1100737 workspace=self.workspace_root,
James E. Blair8cce42e2016-10-18 08:18:36 -0700738 zuul_project=build.parameters.get('ZUUL_PROJECT', None),
739 zuul_branch=build.parameters.get('ZUUL_BRANCH', None),
740 zuul_ref=build.parameters.get('ZUUL_REF', None),
741 zuul_newrev=build.parameters.get('ZUUL_NEWREV', None),
Sachi King9f16d522016-03-16 12:20:45 +1100742 zuul_url=self.git_root,
Sachi King9f16d522016-03-16 12:20:45 +1100743 )
744 cloner.execute()
James E. Blair8cce42e2016-10-18 08:18:36 -0700745 work = self.getWorkspaceRepos(projects)
746
747 for project in projects:
748 self.assertEquals(state[project],
749 str(work[project].commit('HEAD')),
750 'Project %s commit for build %s should '
751 'be correct' % (project, 0))
752 shutil.rmtree(self.workspace_root)