blob: 7c058194cf79eafa55893cc7fea06f98ee9c278b [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
James E. Blaird8af5422017-05-24 13:59:40 -070022from unittest import skip
Antoine Musso45dd2cb2014-01-29 17:17:43 +010023
24import git
25
26import zuul.lib.cloner
27
James E. Blaird8af5422017-05-24 13:59:40 -070028from tests.base import ZuulTestCase, simple_layout
Antoine Musso45dd2cb2014-01-29 17:17:43 +010029
Antoine Musso45dd2cb2014-01-29 17:17:43 +010030
31class TestCloner(ZuulTestCase):
James E. Blaird8af5422017-05-24 13:59:40 -070032 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Musso45dd2cb2014-01-29 17:17:43 +010033
34 log = logging.getLogger("zuul.test.cloner")
Antoine Musso45dd2cb2014-01-29 17:17:43 +010035
James E. Blairaf3d01e2017-05-26 14:30:18 -070036 def assertRepoState(self, repo, state, project, build, number):
James E. Blairbcaa2d42017-05-26 15:44:23 -070037 if 'branch' in state:
38 self.assertFalse(repo.head.is_detached,
39 'Project %s commit for build %s #%s should '
40 'not have a detached HEAD' % (
41 project, build, number))
42 self.assertEquals(repo.active_branch.name,
43 state['branch'],
44 'Project %s commit for build %s #%s should '
45 'be on the correct branch' % (
46 project, build, number))
47 if 'commit' in state:
48 self.assertEquals(state['commit'],
James E. Blairaf3d01e2017-05-26 14:30:18 -070049 str(repo.commit('HEAD')),
50 'Project %s commit for build %s #%s should '
51 'be correct' % (
52 project, build, number))
James E. Blairbcaa2d42017-05-26 15:44:23 -070053 ref = repo.commit('HEAD')
54 repo_messages = set(
55 [c.message.strip() for c in repo.iter_commits(ref)])
56 if 'present' in state:
57 for change in state['present']:
James E. Blairaf3d01e2017-05-26 14:30:18 -070058 msg = '%s-1' % change.subject
59 self.assertTrue(msg in repo_messages,
60 'Project %s for build %s #%s should '
61 'have change %s' % (
62 project, build, number, change.subject))
James E. Blairbcaa2d42017-05-26 15:44:23 -070063 if 'absent' in state:
64 for change in state['absent']:
James E. Blairaf3d01e2017-05-26 14:30:18 -070065 msg = '%s-1' % change.subject
66 self.assertTrue(msg not in repo_messages,
67 'Project %s for build %s #%s should '
68 'not have change %s' % (
69 project, build, number, change.subject))
70
James E. Blaird8af5422017-05-24 13:59:40 -070071 @skip("Disabled for early v3 development")
James E. Blair48ff4d52014-08-25 15:07:21 -070072 def test_cache_dir(self):
73 projects = ['org/project1', 'org/project2']
74 cache_root = os.path.join(self.test_root, "cache")
75 for project in projects:
76 upstream_repo_path = os.path.join(self.upstream_root, project)
77 cache_repo_path = os.path.join(cache_root, project)
78 git.Repo.clone_from(upstream_repo_path, cache_repo_path)
79
80 self.worker.hold_jobs_in_build = True
81 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
82 A.addApproval('CRVW', 2)
83 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
84
85 self.waitUntilSettled()
86
87 self.assertEquals(1, len(self.builds), "One build is running")
88
89 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
90 B.setMerged()
91
92 upstream = self.getUpstreamRepos(projects)
Joshua Hesketh29d99b72014-08-19 16:27:42 +100093 states = [{
94 'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
95 'org/project2': str(upstream['org/project2'].commit('master')),
96 }]
James E. Blair48ff4d52014-08-25 15:07:21 -070097
98 for number, build in enumerate(self.builds):
99 self.log.debug("Build parameters: %s", build.parameters)
100 cloner = zuul.lib.cloner.Cloner(
101 git_base_url=self.upstream_root,
102 projects=projects,
103 workspace=self.workspace_root,
James E. Blairbfd85fd2016-10-21 14:18:11 -0700104 zuul_project=build.parameters.get('ZUUL_PROJECT', None),
James E. Blair48ff4d52014-08-25 15:07:21 -0700105 zuul_branch=build.parameters['ZUUL_BRANCH'],
106 zuul_ref=build.parameters['ZUUL_REF'],
Monty Taylord642d852017-02-23 14:05:42 -0500107 zuul_url=self.src_root,
James E. Blair48ff4d52014-08-25 15:07:21 -0700108 cache_dir=cache_root,
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000109 )
James E. Blair48ff4d52014-08-25 15:07:21 -0700110 cloner.execute()
111 work = self.getWorkspaceRepos(projects)
112 state = states[number]
113
114 for project in projects:
115 self.assertEquals(state[project],
116 str(work[project].commit('HEAD')),
117 'Project %s commit for build %s should '
118 'be correct' % (project, number))
119
120 work = self.getWorkspaceRepos(projects)
James E. Blair8cce42e2016-10-18 08:18:36 -0700121 # project1 is the zuul_project so the origin should be set to the
122 # zuul_url since that is the most up to date.
123 cache_repo_path = os.path.join(cache_root, 'org/project1')
124 self.assertNotEqual(
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000125 work['org/project1'].remotes.origin.url,
James E. Blair8cce42e2016-10-18 08:18:36 -0700126 cache_repo_path,
127 'workspace repo origin should not be the cache'
128 )
129 zuul_url_repo_path = os.path.join(self.git_root, 'org/project1')
130 self.assertEqual(
131 work['org/project1'].remotes.origin.url,
132 zuul_url_repo_path,
133 'workspace repo origin should be the zuul url'
134 )
135
136 # project2 is not the zuul_project so the origin should be set
137 # to upstream since that is the best we can do
138 cache_repo_path = os.path.join(cache_root, 'org/project2')
139 self.assertNotEqual(
140 work['org/project2'].remotes.origin.url,
141 cache_repo_path,
142 'workspace repo origin should not be the cache'
143 )
144 upstream_repo_path = os.path.join(self.upstream_root, 'org/project2')
145 self.assertEqual(
146 work['org/project2'].remotes.origin.url,
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000147 upstream_repo_path,
James E. Blair8cce42e2016-10-18 08:18:36 -0700148 'workspace repo origin should be the upstream url'
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000149 )
James E. Blair48ff4d52014-08-25 15:07:21 -0700150
151 self.worker.hold_jobs_in_build = False
152 self.worker.release()
153 self.waitUntilSettled()
154
James E. Blaird8af5422017-05-24 13:59:40 -0700155 @simple_layout('layouts/repo-checkout-two-project.yaml')
James E. Blair97d902e2014-08-21 13:25:56 -0700156 def test_one_branch(self):
James E. Blaird8af5422017-05-24 13:59:40 -0700157 self.executor_server.hold_jobs_in_build = True
Antoine Musso45dd2cb2014-01-29 17:17:43 +0100158
James E. Blaird8af5422017-05-24 13:59:40 -0700159 p1 = 'review.example.com/org/project1'
160 p2 = 'review.example.com/org/project2'
161 projects = [p1, p2]
Antoine Musso45dd2cb2014-01-29 17:17:43 +0100162 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
163 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blaird8af5422017-05-24 13:59:40 -0700164 A.addApproval('code-review', 2)
165 B.addApproval('code-review', 2)
166 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
167 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Antoine Musso45dd2cb2014-01-29 17:17:43 +0100168
169 self.waitUntilSettled()
170
171 self.assertEquals(2, len(self.builds), "Two builds are running")
172
James E. Blair97d902e2014-08-21 13:25:56 -0700173 upstream = self.getUpstreamRepos(projects)
174 states = [
James E. Blairbcaa2d42017-05-26 15:44:23 -0700175 {p1: dict(present=[A], absent=[B], branch='master'),
176 p2: dict(commit=str(upstream[p2].commit('master')),
177 branch='master'),
James E. Blair97d902e2014-08-21 13:25:56 -0700178 },
James E. Blairbcaa2d42017-05-26 15:44:23 -0700179 {p1: dict(present=[A], absent=[B], branch='master'),
180 p2: dict(present=[B], absent=[A], branch='master'),
James E. Blair97d902e2014-08-21 13:25:56 -0700181 },
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000182 ]
James E. Blair97d902e2014-08-21 13:25:56 -0700183
184 for number, build in enumerate(self.builds):
Antoine Musso45dd2cb2014-01-29 17:17:43 +0100185 self.log.debug("Build parameters: %s", build.parameters)
James E. Blaird8af5422017-05-24 13:59:40 -0700186 work = build.getWorkspaceRepos(projects)
James E. Blair97d902e2014-08-21 13:25:56 -0700187 state = states[number]
188
189 for project in projects:
James E. Blairaf3d01e2017-05-26 14:30:18 -0700190 self.assertRepoState(work[project], state[project],
191 project, build, number)
James E. Blair97d902e2014-08-21 13:25:56 -0700192
James E. Blaird8af5422017-05-24 13:59:40 -0700193 self.executor_server.hold_jobs_in_build = False
194 self.executor_server.release()
James E. Blair97d902e2014-08-21 13:25:56 -0700195 self.waitUntilSettled()
196
James E. Blairaf3d01e2017-05-26 14:30:18 -0700197 @simple_layout('layouts/repo-checkout-four-project.yaml')
James E. Blair97d902e2014-08-21 13:25:56 -0700198 def test_multi_branch(self):
James E. Blairaf3d01e2017-05-26 14:30:18 -0700199 self.executor_server.hold_jobs_in_build = True
200
201 p1 = 'review.example.com/org/project1'
202 p2 = 'review.example.com/org/project2'
203 p3 = 'review.example.com/org/project3'
204 p4 = 'review.example.com/org/project4'
205 projects = [p1, p2, p3, p4]
James E. Blair97d902e2014-08-21 13:25:56 -0700206
207 self.create_branch('org/project2', 'stable/havana')
208 self.create_branch('org/project4', 'stable/havana')
209 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000210 B = self.fake_gerrit.addFakeChange('org/project2', 'stable/havana',
211 'B')
James E. Blair97d902e2014-08-21 13:25:56 -0700212 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
James E. Blairaf3d01e2017-05-26 14:30:18 -0700213 A.addApproval('code-review', 2)
214 B.addApproval('code-review', 2)
215 C.addApproval('code-review', 2)
216 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
217 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
218 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair97d902e2014-08-21 13:25:56 -0700219
220 self.waitUntilSettled()
221
222 self.assertEquals(3, len(self.builds), "Three builds are running")
223
224 upstream = self.getUpstreamRepos(projects)
225 states = [
James E. Blairbcaa2d42017-05-26 15:44:23 -0700226 {p1: dict(present=[A], absent=[B, C], branch='master'),
227 p2: dict(commit=str(upstream[p2].commit('master')),
228 branch='master'),
229 p3: dict(commit=str(upstream[p3].commit('master')),
230 branch='master'),
231 p4: dict(commit=str(upstream[p4].commit('master')),
232 branch='master'),
James E. Blair97d902e2014-08-21 13:25:56 -0700233 },
James E. Blairbcaa2d42017-05-26 15:44:23 -0700234 {p1: dict(present=[A], absent=[B, C], branch='master'),
235 p2: dict(present=[B], absent=[A, C], branch='stable/havana'),
236 p3: dict(commit=str(upstream[p3].commit('master')),
237 branch='master'),
238 p4: dict(commit=str(upstream[p4].commit('stable/havana')),
239 branch='stable/havana'),
James E. Blair97d902e2014-08-21 13:25:56 -0700240 },
James E. Blairbcaa2d42017-05-26 15:44:23 -0700241 {p1: dict(present=[A], absent=[B, C], branch='master'),
242 p2: dict(commit=str(upstream[p2].commit('master')),
243 branch='master'),
244 p3: dict(present=[C], absent=[A, B], branch='master'),
245 p4: dict(commit=str(upstream[p4].commit('master')),
246 branch='master'),
James E. Blair97d902e2014-08-21 13:25:56 -0700247 },
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000248 ]
James E. Blair97d902e2014-08-21 13:25:56 -0700249
250 for number, build in enumerate(self.builds):
251 self.log.debug("Build parameters: %s", build.parameters)
James E. Blairaf3d01e2017-05-26 14:30:18 -0700252 work = build.getWorkspaceRepos(projects)
James E. Blair97d902e2014-08-21 13:25:56 -0700253 state = states[number]
254
255 for project in projects:
James E. Blairaf3d01e2017-05-26 14:30:18 -0700256 self.assertRepoState(work[project], state[project],
257 project, build, number)
James E. Blair82318922014-08-21 11:05:32 -0700258
James E. Blairaf3d01e2017-05-26 14:30:18 -0700259 self.executor_server.hold_jobs_in_build = False
260 self.executor_server.release()
James E. Blair82318922014-08-21 11:05:32 -0700261 self.waitUntilSettled()
James E. Blairbce35e12014-08-21 14:31:17 -0700262
James E. Blaird8af5422017-05-24 13:59:40 -0700263 @skip("Disabled for early v3 development")
James E. Blairbce35e12014-08-21 14:31:17 -0700264 def test_upgrade(self):
265 # Simulates an upgrade test
266 self.worker.hold_jobs_in_build = True
267 projects = ['org/project1', 'org/project2', 'org/project3',
268 'org/project4', 'org/project5', 'org/project6']
269
270 self.create_branch('org/project2', 'stable/havana')
271 self.create_branch('org/project3', 'stable/havana')
272 self.create_branch('org/project4', 'stable/havana')
273 self.create_branch('org/project5', 'stable/havana')
274 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
275 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000276 C = self.fake_gerrit.addFakeChange('org/project3', 'stable/havana',
277 'C')
James E. Blairbce35e12014-08-21 14:31:17 -0700278 D = self.fake_gerrit.addFakeChange('org/project3', 'master', 'D')
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000279 E = self.fake_gerrit.addFakeChange('org/project4', 'stable/havana',
280 'E')
James E. Blairbce35e12014-08-21 14:31:17 -0700281 A.addApproval('CRVW', 2)
282 B.addApproval('CRVW', 2)
283 C.addApproval('CRVW', 2)
284 D.addApproval('CRVW', 2)
285 E.addApproval('CRVW', 2)
286 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
287 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
288 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
289 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
290 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
291
292 self.waitUntilSettled()
293
294 self.assertEquals(5, len(self.builds), "Five builds are running")
295
296 # Check the old side of the upgrade first
297 upstream = self.getUpstreamRepos(projects)
298 states = [
299 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000300 'org/project2': str(upstream['org/project2'].commit(
301 'stable/havana')),
302 'org/project3': str(upstream['org/project3'].commit(
303 'stable/havana')),
304 'org/project4': str(upstream['org/project4'].commit(
305 'stable/havana')),
306 'org/project5': str(upstream['org/project5'].commit(
307 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700308 'org/project6': str(upstream['org/project6'].commit('master')),
309 },
310 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000311 'org/project2': str(upstream['org/project2'].commit(
312 'stable/havana')),
313 'org/project3': str(upstream['org/project3'].commit(
314 'stable/havana')),
315 'org/project4': str(upstream['org/project4'].commit(
316 'stable/havana')),
317 'org/project5': str(upstream['org/project5'].commit(
318 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700319 'org/project6': str(upstream['org/project6'].commit('master')),
320 },
321 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000322 'org/project2': str(upstream['org/project2'].commit(
323 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700324 'org/project3': self.builds[2].parameters['ZUUL_COMMIT'],
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000325 'org/project4': str(upstream['org/project4'].commit(
326 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700327
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000328 'org/project5': str(upstream['org/project5'].commit(
329 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700330 'org/project6': str(upstream['org/project6'].commit('master')),
331 },
332 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000333 'org/project2': str(upstream['org/project2'].commit(
334 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700335 'org/project3': self.builds[2].parameters['ZUUL_COMMIT'],
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000336 'org/project4': str(upstream['org/project4'].commit(
337 'stable/havana')),
338 'org/project5': str(upstream['org/project5'].commit(
339 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700340 'org/project6': str(upstream['org/project6'].commit('master')),
341 },
342 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000343 'org/project2': str(upstream['org/project2'].commit(
344 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700345 'org/project3': self.builds[2].parameters['ZUUL_COMMIT'],
346 'org/project4': self.builds[4].parameters['ZUUL_COMMIT'],
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000347 'org/project5': str(upstream['org/project5'].commit(
348 'stable/havana')),
James E. Blairbce35e12014-08-21 14:31:17 -0700349 'org/project6': str(upstream['org/project6'].commit('master')),
350 },
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000351 ]
James E. Blairbce35e12014-08-21 14:31:17 -0700352
353 for number, build in enumerate(self.builds):
354 self.log.debug("Build parameters: %s", build.parameters)
James E. Blairbce35e12014-08-21 14:31:17 -0700355 cloner = zuul.lib.cloner.Cloner(
356 git_base_url=self.upstream_root,
357 projects=projects,
358 workspace=self.workspace_root,
James E. Blairbfd85fd2016-10-21 14:18:11 -0700359 zuul_project=build.parameters.get('ZUUL_PROJECT', None),
James E. Blairbce35e12014-08-21 14:31:17 -0700360 zuul_branch=build.parameters['ZUUL_BRANCH'],
361 zuul_ref=build.parameters['ZUUL_REF'],
Monty Taylord642d852017-02-23 14:05:42 -0500362 zuul_url=self.src_root,
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000363 branch='stable/havana', # Old branch for upgrade
364 )
James E. Blairbce35e12014-08-21 14:31:17 -0700365 cloner.execute()
366 work = self.getWorkspaceRepos(projects)
367 state = states[number]
368
369 for project in projects:
370 self.assertEquals(state[project],
371 str(work[project].commit('HEAD')),
372 'Project %s commit for build %s should '
373 'be correct on old side of upgrade' %
374 (project, number))
375 shutil.rmtree(self.workspace_root)
376
377 # Check the new side of the upgrade
378 states = [
379 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
380 'org/project2': str(upstream['org/project2'].commit('master')),
381 'org/project3': str(upstream['org/project3'].commit('master')),
382 'org/project4': str(upstream['org/project4'].commit('master')),
383 'org/project5': str(upstream['org/project5'].commit('master')),
384 'org/project6': str(upstream['org/project6'].commit('master')),
385 },
386 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
387 'org/project2': self.builds[1].parameters['ZUUL_COMMIT'],
388 'org/project3': str(upstream['org/project3'].commit('master')),
389 'org/project4': str(upstream['org/project4'].commit('master')),
390 'org/project5': str(upstream['org/project5'].commit('master')),
391 'org/project6': str(upstream['org/project6'].commit('master')),
392 },
393 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
394 'org/project2': self.builds[1].parameters['ZUUL_COMMIT'],
395 'org/project3': str(upstream['org/project3'].commit('master')),
396 'org/project4': str(upstream['org/project4'].commit('master')),
397 'org/project5': str(upstream['org/project5'].commit('master')),
398 'org/project6': str(upstream['org/project6'].commit('master')),
399 },
400 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
401 'org/project2': self.builds[1].parameters['ZUUL_COMMIT'],
402 'org/project3': self.builds[3].parameters['ZUUL_COMMIT'],
403 'org/project4': str(upstream['org/project4'].commit('master')),
404 'org/project5': str(upstream['org/project5'].commit('master')),
405 'org/project6': str(upstream['org/project6'].commit('master')),
406 },
407 {'org/project1': self.builds[0].parameters['ZUUL_COMMIT'],
408 'org/project2': self.builds[1].parameters['ZUUL_COMMIT'],
409 'org/project3': self.builds[3].parameters['ZUUL_COMMIT'],
410 'org/project4': str(upstream['org/project4'].commit('master')),
411 'org/project5': str(upstream['org/project5'].commit('master')),
412 'org/project6': str(upstream['org/project6'].commit('master')),
413 },
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000414 ]
James E. Blairbce35e12014-08-21 14:31:17 -0700415
416 for number, build in enumerate(self.builds):
417 self.log.debug("Build parameters: %s", build.parameters)
James E. Blairbce35e12014-08-21 14:31:17 -0700418 cloner = zuul.lib.cloner.Cloner(
419 git_base_url=self.upstream_root,
420 projects=projects,
421 workspace=self.workspace_root,
James E. Blairbfd85fd2016-10-21 14:18:11 -0700422 zuul_project=build.parameters.get('ZUUL_PROJECT', None),
James E. Blairbce35e12014-08-21 14:31:17 -0700423 zuul_branch=build.parameters['ZUUL_BRANCH'],
424 zuul_ref=build.parameters['ZUUL_REF'],
Monty Taylord642d852017-02-23 14:05:42 -0500425 zuul_url=self.src_root,
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000426 branch='master', # New branch for upgrade
427 )
James E. Blairbce35e12014-08-21 14:31:17 -0700428 cloner.execute()
429 work = self.getWorkspaceRepos(projects)
430 state = states[number]
431
432 for project in projects:
433 self.assertEquals(state[project],
434 str(work[project].commit('HEAD')),
435 'Project %s commit for build %s should '
436 'be correct on old side of upgrade' %
437 (project, number))
438 shutil.rmtree(self.workspace_root)
439
440 self.worker.hold_jobs_in_build = False
441 self.worker.release()
442 self.waitUntilSettled()
James E. Blairf0420222014-08-21 16:02:17 -0700443
James E. Blair4de8d9e2017-06-01 15:41:40 -0700444 @simple_layout('layouts/repo-checkout-six-project.yaml')
James E. Blairf0420222014-08-21 16:02:17 -0700445 def test_project_override(self):
James E. Blair4de8d9e2017-06-01 15:41:40 -0700446 self.executor_server.hold_jobs_in_build = True
447
448 p1 = 'review.example.com/org/project1'
449 p2 = 'review.example.com/org/project2'
450 p3 = 'review.example.com/org/project3'
451 p4 = 'review.example.com/org/project4'
452 p5 = 'review.example.com/org/project5'
453 p6 = 'review.example.com/org/project6'
454 projects = [p1, p2, p3, p4, p5, p6]
James E. Blairf0420222014-08-21 16:02:17 -0700455
456 self.create_branch('org/project3', 'stable/havana')
457 self.create_branch('org/project4', 'stable/havana')
458 self.create_branch('org/project6', 'stable/havana')
459 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
460 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
461 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000462 D = self.fake_gerrit.addFakeChange('org/project3', 'stable/havana',
463 'D')
James E. Blair4de8d9e2017-06-01 15:41:40 -0700464 A.addApproval('code-review', 2)
465 B.addApproval('code-review', 2)
466 C.addApproval('code-review', 2)
467 D.addApproval('code-review', 2)
468 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
469 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
470 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
471 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blairf0420222014-08-21 16:02:17 -0700472
473 self.waitUntilSettled()
474
475 self.assertEquals(4, len(self.builds), "Four builds are running")
476
477 upstream = self.getUpstreamRepos(projects)
478 states = [
James E. Blair4de8d9e2017-06-01 15:41:40 -0700479 {p1: dict(present=[A], absent=[B, C, D], branch='master'),
480 p2: dict(commit=str(upstream[p2].commit('master')),
481 branch='master'),
482 p3: dict(commit=str(upstream[p3].commit('master')),
483 branch='master'),
484 p4: dict(commit=str(upstream[p4].commit('master')),
485 branch='master'),
486 p5: dict(commit=str(upstream[p5].commit('master')),
487 branch='master'),
488 p6: dict(commit=str(upstream[p6].commit('master')),
489 branch='master'),
James E. Blairf0420222014-08-21 16:02:17 -0700490 },
James E. Blair4de8d9e2017-06-01 15:41:40 -0700491 {p1: dict(present=[A, B], absent=[C, D], branch='master'),
492 p2: dict(commit=str(upstream[p2].commit('master')),
493 branch='master'),
494 p3: dict(commit=str(upstream[p3].commit('master')),
495 branch='master'),
496 p4: dict(commit=str(upstream[p4].commit('master')),
497 branch='master'),
498 p5: dict(commit=str(upstream[p5].commit('master')),
499 branch='master'),
500 p6: dict(commit=str(upstream[p6].commit('master')),
501 branch='master'),
James E. Blairf0420222014-08-21 16:02:17 -0700502 },
James E. Blair4de8d9e2017-06-01 15:41:40 -0700503 {p1: dict(present=[A, B], absent=[C, D], branch='master'),
504 p2: dict(present=[C], absent=[A, B, D], branch='master'),
505 p3: dict(commit=str(upstream[p3].commit('master')),
506 branch='master'),
507 p4: dict(commit=str(upstream[p4].commit('master')),
508 branch='master'),
509 p5: dict(commit=str(upstream[p5].commit('master')),
510 branch='master'),
511 p6: dict(commit=str(upstream[p6].commit('master')),
512 branch='master'),
James E. Blairf0420222014-08-21 16:02:17 -0700513 },
James E. Blair4de8d9e2017-06-01 15:41:40 -0700514 {p1: dict(present=[A, B], absent=[C, D], branch='master'),
515 p2: dict(present=[C], absent=[A, B, D], branch='master'),
516 p3: dict(present=[D], absent=[A, B, C],
517 branch='stable/havana'),
518 p4: dict(commit=str(upstream[p4].commit('master')),
519 branch='master'),
520 p5: dict(commit=str(upstream[p5].commit('master')),
521 branch='master'),
522 p6: dict(commit=str(upstream[p6].commit('stable/havana')),
523 branch='stable/havana'),
James E. Blairf0420222014-08-21 16:02:17 -0700524 },
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000525 ]
James E. Blairf0420222014-08-21 16:02:17 -0700526
527 for number, build in enumerate(self.builds):
528 self.log.debug("Build parameters: %s", build.parameters)
James E. Blair4de8d9e2017-06-01 15:41:40 -0700529 work = build.getWorkspaceRepos(projects)
James E. Blairf0420222014-08-21 16:02:17 -0700530 state = states[number]
531
532 for project in projects:
James E. Blair4de8d9e2017-06-01 15:41:40 -0700533 self.assertRepoState(work[project], state[project],
534 project, build, number)
James E. Blairf0420222014-08-21 16:02:17 -0700535
James E. Blair4de8d9e2017-06-01 15:41:40 -0700536 self.executor_server.hold_jobs_in_build = False
537 self.executor_server.release()
James E. Blairf0420222014-08-21 16:02:17 -0700538 self.waitUntilSettled()
James E. Blair217b10d2015-01-08 16:25:28 -0800539
540 def test_periodic(self):
James E. Blair95ae4fc2017-06-01 15:54:53 -0700541 # This test can not use simple_layout because it must start
542 # with a configuration which does not include a
543 # timer-triggered job so that we have an opportunity to set
544 # the hold flag before the first job.
545 self.executor_server.hold_jobs_in_build = True
546 # Start timer trigger - also org/project
547 self.commitConfigUpdate('common-config',
548 'layouts/repo-checkout-timer.yaml')
James E. Blair217b10d2015-01-08 16:25:28 -0800549 self.sched.reconfigure(self.config)
James E. Blair95ae4fc2017-06-01 15:54:53 -0700550
551 p1 = 'review.example.com/org/project1'
552 projects = [p1]
553 self.create_branch('org/project1', 'stable/havana')
James E. Blair217b10d2015-01-08 16:25:28 -0800554
555 # The pipeline triggers every second, so we should have seen
556 # several by now.
557 time.sleep(5)
558 self.waitUntilSettled()
559
James E. Blair217b10d2015-01-08 16:25:28 -0800560 # Stop queuing timer triggered jobs so that the assertions
561 # below don't race against more jobs being queued.
James E. Blair95ae4fc2017-06-01 15:54:53 -0700562 self.commitConfigUpdate('common-config',
563 'layouts/repo-checkout-no-timer.yaml')
James E. Blair217b10d2015-01-08 16:25:28 -0800564 self.sched.reconfigure(self.config)
James E. Blair217b10d2015-01-08 16:25:28 -0800565
James E. Blair95ae4fc2017-06-01 15:54:53 -0700566 self.assertEquals(1, len(self.builds), "One build is running")
James E. Blair217b10d2015-01-08 16:25:28 -0800567
568 upstream = self.getUpstreamRepos(projects)
569 states = [
James E. Blair95ae4fc2017-06-01 15:54:53 -0700570 {p1: dict(commit=str(upstream[p1].commit('stable/havana')),
571 branch='stable/havana'),
James E. Blair217b10d2015-01-08 16:25:28 -0800572 },
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000573 ]
James E. Blair217b10d2015-01-08 16:25:28 -0800574
James E. Blair95ae4fc2017-06-01 15:54:53 -0700575 for number, build in enumerate(self.builds):
James E. Blair217b10d2015-01-08 16:25:28 -0800576 self.log.debug("Build parameters: %s", build.parameters)
James E. Blair95ae4fc2017-06-01 15:54:53 -0700577 work = build.getWorkspaceRepos(projects)
James E. Blair217b10d2015-01-08 16:25:28 -0800578 state = states[number]
579
580 for project in projects:
James E. Blair95ae4fc2017-06-01 15:54:53 -0700581 self.assertRepoState(work[project], state[project],
582 project, build, number)
James E. Blair217b10d2015-01-08 16:25:28 -0800583
James E. Blair95ae4fc2017-06-01 15:54:53 -0700584 self.executor_server.hold_jobs_in_build = False
585 self.executor_server.release()
James E. Blair217b10d2015-01-08 16:25:28 -0800586 self.waitUntilSettled()
Sachi King9f16d522016-03-16 12:20:45 +1100587
James E. Blaird8af5422017-05-24 13:59:40 -0700588 @skip("Disabled for early v3 development")
James E. Blairbfd85fd2016-10-21 14:18:11 -0700589 def test_periodic_update(self):
590 # Test that the merger correctly updates its local repository
591 # before running a periodic job.
592
593 # Prime the merger with the current state
594 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
595 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
596 self.waitUntilSettled()
597
598 # Merge a different change
599 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
600 B.setMerged()
601
602 # Start a periodic job
603 self.worker.hold_jobs_in_build = True
Paul Belanger174a8272017-03-14 13:20:10 -0400604 self.executor.negative_function_cache_ttl = 0
James E. Blairbfd85fd2016-10-21 14:18:11 -0700605 self.config.set('zuul', 'layout_config',
606 'tests/fixtures/layout-timer.yaml')
607 self.sched.reconfigure(self.config)
608 self.registerJobs()
609
610 # The pipeline triggers every second, so we should have seen
611 # several by now.
612 time.sleep(5)
613 self.waitUntilSettled()
614
615 builds = self.builds[:]
616
James E. Blairbfd85fd2016-10-21 14:18:11 -0700617 # Stop queuing timer triggered jobs so that the assertions
618 # below don't race against more jobs being queued.
619 self.config.set('zuul', 'layout_config',
620 'tests/fixtures/layout-no-timer.yaml')
621 self.sched.reconfigure(self.config)
622 self.registerJobs()
623 self.worker.release()
624 self.waitUntilSettled()
625
626 projects = ['org/project']
627
628 self.assertEquals(2, len(builds), "Two builds are running")
629
630 upstream = self.getUpstreamRepos(projects)
631 self.assertEqual(upstream['org/project'].commit('master').hexsha,
632 B.patchsets[0]['revision'])
633 states = [
634 {'org/project':
635 str(upstream['org/project'].commit('master')),
636 },
637 {'org/project':
638 str(upstream['org/project'].commit('master')),
639 },
640 ]
641
642 for number, build in enumerate(builds):
643 self.log.debug("Build parameters: %s", build.parameters)
644 cloner = zuul.lib.cloner.Cloner(
645 git_base_url=self.upstream_root,
646 projects=projects,
647 workspace=self.workspace_root,
648 zuul_project=build.parameters.get('ZUUL_PROJECT', None),
649 zuul_branch=build.parameters.get('ZUUL_BRANCH', None),
650 zuul_ref=build.parameters.get('ZUUL_REF', None),
651 zuul_url=self.git_root,
652 )
653 cloner.execute()
654 work = self.getWorkspaceRepos(projects)
655 state = states[number]
656
657 for project in projects:
658 self.assertEquals(state[project],
659 str(work[project].commit('HEAD')),
660 'Project %s commit for build %s should '
661 'be correct' % (project, number))
662
663 shutil.rmtree(self.workspace_root)
664
665 self.worker.hold_jobs_in_build = False
666 self.worker.release()
667 self.waitUntilSettled()
668
James E. Blairbbe811e2017-06-02 10:47:44 -0700669 @simple_layout('layouts/repo-checkout-post.yaml')
Sachi King9f16d522016-03-16 12:20:45 +1100670 def test_post_checkout(self):
James E. Blairbbe811e2017-06-02 10:47:44 -0700671 self.executor_server.hold_jobs_in_build = True
672 p1 = "review.example.com/org/project1"
673 projects = [p1]
James E. Blair8cce42e2016-10-18 08:18:36 -0700674
James E. Blairbbe811e2017-06-02 10:47:44 -0700675 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
James E. Blair8cce42e2016-10-18 08:18:36 -0700676 event = A.getRefUpdatedEvent()
677 A.setMerged()
678 self.fake_gerrit.addEvent(event)
679 self.waitUntilSettled()
680
James E. Blairbbe811e2017-06-02 10:47:44 -0700681 upstream = self.getUpstreamRepos(projects)
682 states = [
683 {p1: dict(commit=str(upstream[p1].commit('master')),
684 present=[A], branch='master'),
685 },
686 ]
James E. Blair8cce42e2016-10-18 08:18:36 -0700687
James E. Blairbbe811e2017-06-02 10:47:44 -0700688 for number, build in enumerate(self.builds):
689 self.log.debug("Build parameters: %s", build.parameters)
690 work = build.getWorkspaceRepos(projects)
691 state = states[number]
692
693 for project in projects:
694 self.assertRepoState(work[project], state[project],
695 project, build, number)
696
697 self.executor_server.hold_jobs_in_build = False
698 self.executor_server.release()
James E. Blair8cce42e2016-10-18 08:18:36 -0700699 self.waitUntilSettled()
Sachi King9f16d522016-03-16 12:20:45 +1100700
James E. Blaird8af5422017-05-24 13:59:40 -0700701 @skip("Disabled for early v3 development")
Sachi King9f16d522016-03-16 12:20:45 +1100702 def test_post_and_master_checkout(self):
James E. Blair8cce42e2016-10-18 08:18:36 -0700703 self.worker.hold_jobs_in_build = True
704 projects = ["org/project1", "org/project2"]
705
706 A = self.fake_gerrit.addFakeChange(projects[0], 'master', 'A')
707 event = A.getRefUpdatedEvent()
708 A.setMerged()
709 self.fake_gerrit.addEvent(event)
710 self.waitUntilSettled()
711
712 build = self.builds[0]
713 upstream = self.getUpstreamRepos(projects)
714 state = {'org/project1':
715 build.parameters['ZUUL_COMMIT'],
716 'org/project2':
717 str(upstream['org/project2'].commit('master')),
718 }
719
720 build.release()
721 self.waitUntilSettled()
Sachi King9f16d522016-03-16 12:20:45 +1100722
723 cloner = zuul.lib.cloner.Cloner(
724 git_base_url=self.upstream_root,
James E. Blair8cce42e2016-10-18 08:18:36 -0700725 projects=projects,
Sachi King9f16d522016-03-16 12:20:45 +1100726 workspace=self.workspace_root,
James E. Blair8cce42e2016-10-18 08:18:36 -0700727 zuul_project=build.parameters.get('ZUUL_PROJECT', None),
728 zuul_branch=build.parameters.get('ZUUL_BRANCH', None),
729 zuul_ref=build.parameters.get('ZUUL_REF', None),
730 zuul_newrev=build.parameters.get('ZUUL_NEWREV', None),
Sachi King9f16d522016-03-16 12:20:45 +1100731 zuul_url=self.git_root,
Sachi King9f16d522016-03-16 12:20:45 +1100732 )
733 cloner.execute()
James E. Blair8cce42e2016-10-18 08:18:36 -0700734 work = self.getWorkspaceRepos(projects)
735
736 for project in projects:
737 self.assertEquals(state[project],
738 str(work[project].commit('HEAD')),
739 'Project %s commit for build %s should '
740 'be correct' % (project, 0))
741 shutil.rmtree(self.workspace_root)