blob: 166c486666e84bbf96a4284cdf3dcee9cf76f141 [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
21
22import git
23
24import zuul.lib.cloner
25
26from tests.base import ZuulTestCase
27from tests.base import FIXTURE_DIR
28
29logging.basicConfig(level=logging.DEBUG,
30 format='%(asctime)s %(name)-32s '
31 '%(levelname)-8s %(message)s')
32
33
34class TestCloner(ZuulTestCase):
35
36 log = logging.getLogger("zuul.test.cloner")
37 workspace_root = None
38
39 def setUp(self):
40 super(TestCloner, self).setUp()
41 self.workspace_root = os.path.join(self.test_root, 'workspace')
42
43 self.config.set('zuul', 'layout_config',
James E. Blair97d902e2014-08-21 13:25:56 -070044 'tests/fixtures/layout-cloner.yaml')
Antoine Musso45dd2cb2014-01-29 17:17:43 +010045 self.sched.reconfigure(self.config)
46 self.registerJobs()
47
James E. Blair97d902e2014-08-21 13:25:56 -070048 def getWorkspaceRepos(self, projects):
49 repos = {}
50 for project in projects:
51 repos[project] = git.Repo(
52 os.path.join(self.workspace_root, project))
53 return repos
54
55 def getUpstreamRepos(self, projects):
56 repos = {}
57 for project in projects:
58 repos[project] = git.Repo(
59 os.path.join(self.upstream_root, project))
60 return repos
61
62 def test_one_branch(self):
Antoine Musso45dd2cb2014-01-29 17:17:43 +010063 self.worker.hold_jobs_in_build = True
64
James E. Blair97d902e2014-08-21 13:25:56 -070065 projects = ['org/project1', 'org/project2']
Antoine Musso45dd2cb2014-01-29 17:17:43 +010066 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
67 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Antoine Musso45dd2cb2014-01-29 17:17:43 +010068 A.addApproval('CRVW', 2)
69 B.addApproval('CRVW', 2)
70 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
71 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
72
73 self.waitUntilSettled()
74
75 self.assertEquals(2, len(self.builds), "Two builds are running")
76
James E. Blair97d902e2014-08-21 13:25:56 -070077 upstream = self.getUpstreamRepos(projects)
78 states = [
79 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
80 'org/project2': str(upstream['org/project2'].commit('master')),
81 },
82 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
83 'org/project2': self.builds[1].parameters['ZUUL_COMMIT'],
84 },
85 ]
86
87 for number, build in enumerate(self.builds):
Antoine Musso45dd2cb2014-01-29 17:17:43 +010088 self.log.debug("Build parameters: %s", build.parameters)
Antoine Musso45dd2cb2014-01-29 17:17:43 +010089 cloner = zuul.lib.cloner.Cloner(
90 git_base_url=self.upstream_root,
James E. Blair97d902e2014-08-21 13:25:56 -070091 projects=projects,
Antoine Musso45dd2cb2014-01-29 17:17:43 +010092 workspace=self.workspace_root,
James E. Blair97d902e2014-08-21 13:25:56 -070093 zuul_branch=build.parameters['ZUUL_BRANCH'],
James E. Blair82318922014-08-21 11:05:32 -070094 zuul_ref=build.parameters['ZUUL_REF'],
Antoine Musso45dd2cb2014-01-29 17:17:43 +010095 zuul_url=self.git_root,
James E. Blair97d902e2014-08-21 13:25:56 -070096 )
Antoine Musso45dd2cb2014-01-29 17:17:43 +010097 cloner.execute()
James E. Blair97d902e2014-08-21 13:25:56 -070098 work = self.getWorkspaceRepos(projects)
99 state = states[number]
100
101 for project in projects:
102 self.assertEquals(state[project],
103 str(work[project].commit('HEAD')),
104 'Project %s commit for build %s should '
105 'be correct' % (project, number))
106
107 shutil.rmtree(self.workspace_root)
108
109 self.worker.hold_jobs_in_build = False
110 self.worker.release()
111 self.waitUntilSettled()
112
113 def test_multi_branch(self):
114 self.worker.hold_jobs_in_build = True
115 projects = ['org/project1', 'org/project2',
116 'org/project3', 'org/project4']
117
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')
121 B = self.fake_gerrit.addFakeChange('org/project2', 'stable/havana', 'B')
122 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
123 A.addApproval('CRVW', 2)
124 B.addApproval('CRVW', 2)
125 C.addApproval('CRVW', 2)
126 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
127 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
128 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
129
130 self.waitUntilSettled()
131
132 self.assertEquals(3, len(self.builds), "Three builds are running")
133
134 upstream = self.getUpstreamRepos(projects)
135 states = [
136 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
137 'org/project2': str(upstream['org/project2'].commit('master')),
138 'org/project3': str(upstream['org/project3'].commit('master')),
139 'org/project4': str(upstream['org/project4'].
140 commit('master')),
141 },
142 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
143 'org/project2': self.builds[1].parameters['ZUUL_COMMIT'],
144 'org/project3': str(upstream['org/project3'].commit('master')),
145 'org/project4': str(upstream['org/project4'].
146 commit('stable/havana')),
147 },
148 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
149 'org/project2': str(upstream['org/project2'].commit('master')),
150 'org/project3': self.builds[2].parameters['ZUUL_COMMIT'],
151 'org/project4': str(upstream['org/project4'].
152 commit('master')),
153 },
154 ]
155
156 for number, build in enumerate(self.builds):
157 self.log.debug("Build parameters: %s", build.parameters)
158 cloner = zuul.lib.cloner.Cloner(
159 git_base_url=self.upstream_root,
160 projects=projects,
161 workspace=self.workspace_root,
162 zuul_branch=build.parameters['ZUUL_BRANCH'],
163 zuul_ref=build.parameters['ZUUL_REF'],
164 zuul_url=self.git_root,
165 )
166 cloner.execute()
167 work = self.getWorkspaceRepos(projects)
168 state = states[number]
169
170 for project in projects:
171 self.assertEquals(state[project],
172 str(work[project].commit('HEAD')),
173 'Project %s commit for build %s should '
174 'be correct' % (project, number))
Antoine Musso45dd2cb2014-01-29 17:17:43 +0100175 shutil.rmtree(self.workspace_root)
James E. Blair82318922014-08-21 11:05:32 -0700176
177 self.worker.hold_jobs_in_build = False
178 self.worker.release()
179 self.waitUntilSettled()
James E. Blairbce35e12014-08-21 14:31:17 -0700180
181 def test_upgrade(self):
182 # Simulates an upgrade test
183 self.worker.hold_jobs_in_build = True
184 projects = ['org/project1', 'org/project2', 'org/project3',
185 'org/project4', 'org/project5', 'org/project6']
186
187 self.create_branch('org/project2', 'stable/havana')
188 self.create_branch('org/project3', 'stable/havana')
189 self.create_branch('org/project4', 'stable/havana')
190 self.create_branch('org/project5', 'stable/havana')
191 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
192 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
193 C = self.fake_gerrit.addFakeChange('org/project3', 'stable/havana', 'C')
194 D = self.fake_gerrit.addFakeChange('org/project3', 'master', 'D')
195 E = self.fake_gerrit.addFakeChange('org/project4', 'stable/havana', 'E')
196 A.addApproval('CRVW', 2)
197 B.addApproval('CRVW', 2)
198 C.addApproval('CRVW', 2)
199 D.addApproval('CRVW', 2)
200 E.addApproval('CRVW', 2)
201 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
202 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
203 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
204 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
205 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
206
207 self.waitUntilSettled()
208
209 self.assertEquals(5, len(self.builds), "Five builds are running")
210
211 # Check the old side of the upgrade first
212 upstream = self.getUpstreamRepos(projects)
213 states = [
214 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
215 'org/project2': str(upstream['org/project2'].commit('stable/havana')),
216 'org/project3': str(upstream['org/project3'].commit('stable/havana')),
217 'org/project4': str(upstream['org/project4'].commit('stable/havana')),
218 'org/project5': str(upstream['org/project5'].commit('stable/havana')),
219 'org/project6': str(upstream['org/project6'].commit('master')),
220 },
221 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
222 'org/project2': str(upstream['org/project2'].commit('stable/havana')),
223 'org/project3': str(upstream['org/project3'].commit('stable/havana')),
224 'org/project4': str(upstream['org/project4'].commit('stable/havana')),
225 'org/project5': str(upstream['org/project5'].commit('stable/havana')),
226 'org/project6': str(upstream['org/project6'].commit('master')),
227 },
228 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
229 'org/project2': str(upstream['org/project2'].commit('stable/havana')),
230 'org/project3': self.builds[2].parameters['ZUUL_COMMIT'],
231 'org/project4': str(upstream['org/project4'].commit('stable/havana')),
232
233 'org/project5': str(upstream['org/project5'].commit('stable/havana')),
234 'org/project6': str(upstream['org/project6'].commit('master')),
235 },
236 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
237 'org/project2': str(upstream['org/project2'].commit('stable/havana')),
238 'org/project3': self.builds[2].parameters['ZUUL_COMMIT'],
239 'org/project4': str(upstream['org/project4'].commit('stable/havana')),
240 'org/project5': str(upstream['org/project5'].commit('stable/havana')),
241 'org/project6': str(upstream['org/project6'].commit('master')),
242 },
243 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
244 'org/project2': str(upstream['org/project2'].commit('stable/havana')),
245 'org/project3': self.builds[2].parameters['ZUUL_COMMIT'],
246 'org/project4': self.builds[4].parameters['ZUUL_COMMIT'],
247 'org/project5': str(upstream['org/project5'].commit('stable/havana')),
248 'org/project6': str(upstream['org/project6'].commit('master')),
249 },
250 ]
251
252 for number, build in enumerate(self.builds):
253 self.log.debug("Build parameters: %s", build.parameters)
254 change_number = int(build.parameters['ZUUL_CHANGE'])
255 cloner = zuul.lib.cloner.Cloner(
256 git_base_url=self.upstream_root,
257 projects=projects,
258 workspace=self.workspace_root,
259 zuul_branch=build.parameters['ZUUL_BRANCH'],
260 zuul_ref=build.parameters['ZUUL_REF'],
261 zuul_url=self.git_root,
262 branch='stable/havana', # Old branch for upgrade
263 )
264 cloner.execute()
265 work = self.getWorkspaceRepos(projects)
266 state = states[number]
267
268 for project in projects:
269 self.assertEquals(state[project],
270 str(work[project].commit('HEAD')),
271 'Project %s commit for build %s should '
272 'be correct on old side of upgrade' %
273 (project, number))
274 shutil.rmtree(self.workspace_root)
275
276 # Check the new side of the upgrade
277 states = [
278 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
279 'org/project2': str(upstream['org/project2'].commit('master')),
280 'org/project3': str(upstream['org/project3'].commit('master')),
281 'org/project4': str(upstream['org/project4'].commit('master')),
282 'org/project5': str(upstream['org/project5'].commit('master')),
283 'org/project6': str(upstream['org/project6'].commit('master')),
284 },
285 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
286 'org/project2': self.builds[1].parameters['ZUUL_COMMIT'],
287 'org/project3': str(upstream['org/project3'].commit('master')),
288 'org/project4': str(upstream['org/project4'].commit('master')),
289 'org/project5': str(upstream['org/project5'].commit('master')),
290 'org/project6': str(upstream['org/project6'].commit('master')),
291 },
292 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
293 'org/project2': self.builds[1].parameters['ZUUL_COMMIT'],
294 'org/project3': str(upstream['org/project3'].commit('master')),
295 'org/project4': str(upstream['org/project4'].commit('master')),
296 'org/project5': str(upstream['org/project5'].commit('master')),
297 'org/project6': str(upstream['org/project6'].commit('master')),
298 },
299 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
300 'org/project2': self.builds[1].parameters['ZUUL_COMMIT'],
301 'org/project3': self.builds[3].parameters['ZUUL_COMMIT'],
302 'org/project4': str(upstream['org/project4'].commit('master')),
303 'org/project5': str(upstream['org/project5'].commit('master')),
304 'org/project6': str(upstream['org/project6'].commit('master')),
305 },
306 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
307 'org/project2': self.builds[1].parameters['ZUUL_COMMIT'],
308 'org/project3': self.builds[3].parameters['ZUUL_COMMIT'],
309 'org/project4': str(upstream['org/project4'].commit('master')),
310 'org/project5': str(upstream['org/project5'].commit('master')),
311 'org/project6': str(upstream['org/project6'].commit('master')),
312 },
313 ]
314
315 for number, build in enumerate(self.builds):
316 self.log.debug("Build parameters: %s", build.parameters)
317 change_number = int(build.parameters['ZUUL_CHANGE'])
318 cloner = zuul.lib.cloner.Cloner(
319 git_base_url=self.upstream_root,
320 projects=projects,
321 workspace=self.workspace_root,
322 zuul_branch=build.parameters['ZUUL_BRANCH'],
323 zuul_ref=build.parameters['ZUUL_REF'],
324 zuul_url=self.git_root,
325 branch='master', # New branch for upgrade
326 )
327 cloner.execute()
328 work = self.getWorkspaceRepos(projects)
329 state = states[number]
330
331 for project in projects:
332 self.assertEquals(state[project],
333 str(work[project].commit('HEAD')),
334 'Project %s commit for build %s should '
335 'be correct on old side of upgrade' %
336 (project, number))
337 shutil.rmtree(self.workspace_root)
338
339 self.worker.hold_jobs_in_build = False
340 self.worker.release()
341 self.waitUntilSettled()
James E. Blairf0420222014-08-21 16:02:17 -0700342
343 def test_project_override(self):
344 self.worker.hold_jobs_in_build = True
345 projects = ['org/project1', 'org/project2', 'org/project3',
346 'org/project4', 'org/project5', 'org/project6']
347
348 self.create_branch('org/project3', 'stable/havana')
349 self.create_branch('org/project4', 'stable/havana')
350 self.create_branch('org/project6', 'stable/havana')
351 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
352 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
353 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
354 D = self.fake_gerrit.addFakeChange('org/project3', 'stable/havana', 'D')
355 A.addApproval('CRVW', 2)
356 B.addApproval('CRVW', 2)
357 C.addApproval('CRVW', 2)
358 D.addApproval('CRVW', 2)
359 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
360 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
361 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
362 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
363
364 self.waitUntilSettled()
365
366 self.assertEquals(4, len(self.builds), "Four builds are running")
367
368 upstream = self.getUpstreamRepos(projects)
369 states = [
370 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
371 'org/project2': str(upstream['org/project2'].commit('master')),
372 'org/project3': str(upstream['org/project3'].commit('master')),
373 'org/project4': str(upstream['org/project4'].commit('master')),
374 'org/project5': str(upstream['org/project5'].commit('master')),
375 'org/project6': str(upstream['org/project6'].commit('master')),
376 },
377 {'org/project1': self.builds[1].parameters['ZUUL_COMMIT'],
378 'org/project2': str(upstream['org/project2'].commit('master')),
379 'org/project3': str(upstream['org/project3'].commit('master')),
380 'org/project4': str(upstream['org/project4'].commit('master')),
381 'org/project5': str(upstream['org/project5'].commit('master')),
382 'org/project6': str(upstream['org/project6'].commit('master')),
383 },
384 {'org/project1': self.builds[1].parameters['ZUUL_COMMIT'],
385 'org/project2': self.builds[2].parameters['ZUUL_COMMIT'],
386 'org/project3': str(upstream['org/project3'].commit('master')),
387 'org/project4': str(upstream['org/project4'].commit('master')),
388 'org/project5': str(upstream['org/project5'].commit('master')),
389 'org/project6': str(upstream['org/project6'].commit('master')),
390 },
391 {'org/project1': self.builds[1].parameters['ZUUL_COMMIT'],
392 'org/project2': self.builds[2].parameters['ZUUL_COMMIT'],
393 'org/project3': self.builds[3].parameters['ZUUL_COMMIT'],
394 'org/project4': str(upstream['org/project4'].commit('master')),
395 'org/project5': str(upstream['org/project5'].commit('master')),
396 'org/project6': str(upstream['org/project6'].commit('stable/havana')),
397 },
398 ]
399
400 for number, build in enumerate(self.builds):
401 self.log.debug("Build parameters: %s", build.parameters)
402 change_number = int(build.parameters['ZUUL_CHANGE'])
403 cloner = zuul.lib.cloner.Cloner(
404 git_base_url=self.upstream_root,
405 projects=projects,
406 workspace=self.workspace_root,
407 zuul_branch=build.parameters['ZUUL_BRANCH'],
408 zuul_ref=build.parameters['ZUUL_REF'],
409 zuul_url=self.git_root,
410 project_branches={'org/project4': 'master'},
411 )
412 cloner.execute()
413 work = self.getWorkspaceRepos(projects)
414 state = states[number]
415
416 for project in projects:
417 self.assertEquals(state[project],
418 str(work[project].commit('HEAD')),
419 'Project %s commit for build %s should '
420 'be correct' % (project, number))
421 shutil.rmtree(self.workspace_root)
422
423 self.worker.hold_jobs_in_build = False
424 self.worker.release()
425 self.waitUntilSettled()