blob: 8c7b211f473abe990329c30734a96fd1702673a9 [file] [log] [blame]
James E. Blairb0fcae42012-07-17 11:12:10 -07001#!/usr/bin/env python
2
3# Copyright 2012 Hewlett-Packard Development Company, L.P.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
17import unittest
18import ConfigParser
19import os
20import Queue
James E. Blair8cc15a82012-08-01 11:17:57 -070021import hashlib
James E. Blairb0fcae42012-07-17 11:12:10 -070022import logging
James E. Blair8cc15a82012-08-01 11:17:57 -070023import random
James E. Blairb0fcae42012-07-17 11:12:10 -070024import json
25import threading
26import time
27import pprint
28import re
James E. Blair8cc15a82012-08-01 11:17:57 -070029import urllib2
30import urlparse
James E. Blair4886cc12012-07-18 15:39:41 -070031import shutil
James E. Blair4886f282012-11-15 09:27:33 -080032import string
James E. Blair4886cc12012-07-18 15:39:41 -070033import git
James E. Blairb0fcae42012-07-17 11:12:10 -070034
35import zuul
36import zuul.scheduler
37import zuul.launcher.jenkins
38import zuul.trigger.gerrit
39
40FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
41 'fixtures')
42CONFIG = ConfigParser.ConfigParser()
43CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
44
45CONFIG.set('zuul', 'layout_config',
46 os.path.join(FIXTURE_DIR, "layout.yaml"))
47
James E. Blair1dbd5082012-08-23 15:12:15 -070048TMP_ROOT = os.environ.get("ZUUL_TEST_ROOT", "/tmp")
49TEST_ROOT = os.path.join(TMP_ROOT, "zuul-test")
50UPSTREAM_ROOT = os.path.join(TEST_ROOT, "upstream")
51GIT_ROOT = os.path.join(TEST_ROOT, "git")
52
53CONFIG.set('zuul', 'git_dir', GIT_ROOT)
54
James E. Blairb0fcae42012-07-17 11:12:10 -070055logging.basicConfig(level=logging.DEBUG)
56
57
James E. Blair8cc15a82012-08-01 11:17:57 -070058def random_sha1():
59 return hashlib.sha1(str(random.random())).hexdigest()
60
61
James E. Blair4886cc12012-07-18 15:39:41 -070062class ChangeReference(git.Reference):
63 _common_path_default = "refs/changes"
64 _points_to_commits_only = True
65
66
67def init_repo(project):
68 parts = project.split('/')
James E. Blair1dbd5082012-08-23 15:12:15 -070069 path = os.path.join(UPSTREAM_ROOT, *parts[:-1])
James E. Blair4886cc12012-07-18 15:39:41 -070070 if not os.path.exists(path):
71 os.makedirs(path)
James E. Blair1dbd5082012-08-23 15:12:15 -070072 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -070073 repo = git.Repo.init(path)
74
75 fn = os.path.join(path, 'README')
76 f = open(fn, 'w')
77 f.write("test\n")
78 f.close()
79 repo.index.add([fn])
80 repo.index.commit('initial commit')
James E. Blairc6294a52012-08-17 10:19:48 -070081 master = repo.create_head('master')
James E. Blair4886cc12012-07-18 15:39:41 -070082 repo.create_tag('init')
83
James E. Blairc6294a52012-08-17 10:19:48 -070084 mp = repo.create_head('mp')
85 repo.head.reference = mp
86 f = open(fn, 'a')
87 f.write("test mp\n")
88 f.close()
89 repo.index.add([fn])
90 repo.index.commit('mp commit')
91
92 repo.head.reference = master
93 repo.head.reset(index=True, working_tree=True)
94 repo.git.clean('-x', '-f', '-d')
95
James E. Blair4886cc12012-07-18 15:39:41 -070096
James E. Blair4886f282012-11-15 09:27:33 -080097def add_fake_change_to_repo(project, branch, change_num, patchset, msg, fn,
98 large):
James E. Blair1dbd5082012-08-23 15:12:15 -070099 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -0700100 repo = git.Repo(path)
101 ref = ChangeReference.create(repo, '1/%s/%s' % (change_num,
102 patchset),
103 'refs/tags/init')
104 repo.head.reference = ref
105 repo.head.reset(index=True, working_tree=True)
106 repo.git.clean('-x', '-f', '-d')
107
James E. Blair1dbd5082012-08-23 15:12:15 -0700108 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair4886f282012-11-15 09:27:33 -0800109 if not large:
110 fn = os.path.join(path, fn)
111 f = open(fn, 'w')
112 f.write("test %s %s %s\n" % (branch, change_num, patchset))
113 f.close()
114 repo.index.add([fn])
115 else:
116 for fni in range(100):
117 fn = os.path.join(path, str(fni))
118 f = open(fn, 'w')
119 for ci in range(4096):
120 f.write(random.choice(string.printable))
121 f.close()
122 repo.index.add([fn])
123
James E. Blairdaabed22012-08-15 15:38:57 -0700124 return repo.index.commit(msg)
James E. Blair4886cc12012-07-18 15:39:41 -0700125
126
127def ref_has_change(ref, change):
James E. Blair1dbd5082012-08-23 15:12:15 -0700128 path = os.path.join(GIT_ROOT, change.project)
James E. Blair4886cc12012-07-18 15:39:41 -0700129 repo = git.Repo(path)
130 for commit in repo.iter_commits(ref):
131 if commit.message.strip() == ('%s-1' % change.subject):
132 return True
133 return False
134
135
136def job_has_changes(*args):
137 job = args[0]
138 commits = args[1:]
139 project = job.parameters['ZUUL_PROJECT']
James E. Blair1dbd5082012-08-23 15:12:15 -0700140 path = os.path.join(GIT_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -0700141 repo = git.Repo(path)
142 ref = job.parameters['ZUUL_REF']
James E. Blair81515ad2012-10-01 18:29:08 -0700143 sha = job.parameters['ZUUL_COMMIT']
James E. Blair4886cc12012-07-18 15:39:41 -0700144 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
James E. Blair81515ad2012-10-01 18:29:08 -0700145 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
James E. Blair4886cc12012-07-18 15:39:41 -0700146 commit_messages = ['%s-1' % commit.subject for commit in commits]
James E. Blair4886cc12012-07-18 15:39:41 -0700147 for msg in commit_messages:
148 if msg not in repo_messages:
149 return False
James E. Blair81515ad2012-10-01 18:29:08 -0700150 if repo_shas[0] != sha:
151 return False
James E. Blair4886cc12012-07-18 15:39:41 -0700152 return True
153
154
James E. Blairb0fcae42012-07-17 11:12:10 -0700155class FakeChange(object):
James E. Blair8c803f82012-07-31 16:25:42 -0700156 categories = {'APRV': ('Approved', -1, 1),
157 'CRVW': ('Code-Review', -2, 2),
158 'VRFY': ('Verified', -2, 2)}
James E. Blairb0fcae42012-07-17 11:12:10 -0700159
James E. Blair8cc15a82012-08-01 11:17:57 -0700160 def __init__(self, gerrit, number, project, branch, subject, status='NEW'):
161 self.gerrit = gerrit
James E. Blaird466dc42012-07-31 10:42:56 -0700162 self.reported = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700163 self.queried = 0
James E. Blairb0fcae42012-07-17 11:12:10 -0700164 self.patchsets = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700165 self.number = number
166 self.project = project
167 self.branch = branch
168 self.subject = subject
169 self.latest_patchset = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700170 self.depends_on_change = None
171 self.needed_by_changes = []
James E. Blair127bc182012-08-28 15:55:15 -0700172 self.fail_merge = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700173 self.data = {
174 'branch': branch,
175 'comments': [],
176 'commitMessage': subject,
177 'createdOn': time.time(),
James E. Blair8cc15a82012-08-01 11:17:57 -0700178 'id': 'I' + random_sha1(),
James E. Blairb0fcae42012-07-17 11:12:10 -0700179 'lastUpdated': time.time(),
180 'number': str(number),
181 'open': True,
182 'owner': {'email': 'user@example.com',
183 'name': 'User Name',
184 'username': 'username'},
185 'patchSets': self.patchsets,
186 'project': project,
187 'status': status,
188 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700189 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700190 'url': 'https://hostname/%s' % number}
191
192 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700193 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700194
James E. Blair4886f282012-11-15 09:27:33 -0800195 def addPatchset(self, files=None, large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700196 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700197 if files:
198 fn = files[0]
199 else:
200 fn = '%s-%s' % (self.branch, self.number)
201 msg = self.subject + '-' + str(self.latest_patchset)
202 c = add_fake_change_to_repo(self.project, self.branch,
203 self.number, self.latest_patchset,
James E. Blair4886f282012-11-15 09:27:33 -0800204 msg, fn, large)
James E. Blairb0fcae42012-07-17 11:12:10 -0700205 d = {'approvals': [],
206 'createdOn': time.time(),
207 'files': [{'file': '/COMMIT_MSG',
208 'type': 'ADDED'},
209 {'file': 'README',
210 'type': 'MODIFIED'}],
James E. Blair8c803f82012-07-31 16:25:42 -0700211 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700212 'ref': 'refs/changes/1/%s/%s' % (self.number,
213 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700214 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700215 'uploader': {'email': 'user@example.com',
216 'name': 'User name',
217 'username': 'user'}}
218 self.data['currentPatchSet'] = d
219 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700220 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700221
James E. Blaire0487072012-08-29 17:38:31 -0700222 def getPatchsetCreatedEvent(self, patchset):
223 event = {"type": "patchset-created",
224 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800225 "branch": self.branch,
226 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
227 "number": str(self.number),
228 "subject": self.subject,
229 "owner": {"name": "User Name"},
230 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700231 "patchSet": self.patchsets[patchset - 1],
232 "uploader": {"name": "User Name"}}
233 return event
234
James E. Blairb0fcae42012-07-17 11:12:10 -0700235 def addApproval(self, category, value):
James E. Blair8c803f82012-07-31 16:25:42 -0700236 approval = {'description': self.categories[category][0],
237 'type': category,
238 'value': str(value)}
239 self.patchsets[-1]['approvals'].append(approval)
240 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700241 'author': {'email': 'user@example.com',
242 'name': 'User Name',
243 'username': 'username'},
244 'change': {'branch': self.branch,
245 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
246 'number': str(self.number),
247 'owner': {'email': 'user@example.com',
248 'name': 'User Name',
249 'username': 'username'},
250 'project': self.project,
251 'subject': self.subject,
252 'topic': 'master',
253 'url': 'https://hostname/459'},
254 'comment': '',
255 'patchSet': self.patchsets[-1],
256 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700257 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700258 return json.loads(json.dumps(event))
259
James E. Blair8c803f82012-07-31 16:25:42 -0700260 def getSubmitRecords(self):
261 status = {}
262 for cat in self.categories.keys():
263 status[cat] = 0
264
265 for a in self.patchsets[-1]['approvals']:
266 cur = status[a['type']]
267 cat_min, cat_max = self.categories[a['type']][1:]
268 new = int(a['value'])
269 if new == cat_min:
270 cur = new
271 elif abs(new) > abs(cur):
272 cur = new
273 status[a['type']] = cur
274
275 labels = []
276 ok = True
277 for typ, cat in self.categories.items():
278 cur = status[typ]
279 cat_min, cat_max = cat[1:]
280 if cur == cat_min:
281 value = 'REJECT'
282 ok = False
283 elif cur == cat_max:
284 value = 'OK'
285 else:
286 value = 'NEED'
287 ok = False
288 labels.append({'label': cat[0], 'status': value})
289 if ok:
290 return [{'status': 'OK'}]
291 return [{'status': 'NOT_READY',
292 'labels': labels}]
293
294 def setDependsOn(self, other, patchset):
295 self.depends_on_change = other
296 d = {'id': other.data['id'],
297 'number': other.data['number'],
298 'ref': other.patchsets[patchset - 1]['ref']
299 }
300 self.data['dependsOn'] = [d]
301
302 other.needed_by_changes.append(self)
303 needed = other.data.get('neededBy', [])
304 d = {'id': self.data['id'],
305 'number': self.data['number'],
306 'ref': self.patchsets[patchset - 1]['ref'],
307 'revision': self.patchsets[patchset - 1]['revision']
308 }
309 needed.append(d)
310 other.data['neededBy'] = needed
311
James E. Blairb0fcae42012-07-17 11:12:10 -0700312 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700313 self.queried += 1
314 d = self.data.get('dependsOn')
315 if d:
316 d = d[0]
317 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
318 d['isCurrentPatchSet'] = True
319 else:
320 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700321 return json.loads(json.dumps(self.data))
322
323 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800324 if (self.depends_on_change and
325 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700326 return
James E. Blair127bc182012-08-28 15:55:15 -0700327 if self.fail_merge:
328 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700329 self.data['status'] = 'MERGED'
330 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700331
James E. Blair1dbd5082012-08-23 15:12:15 -0700332 path = os.path.join(UPSTREAM_ROOT, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700333 repo = git.Repo(path)
334 repo.heads[self.branch].commit = \
335 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700336
James E. Blaird466dc42012-07-31 10:42:56 -0700337 def setReported(self):
338 self.reported += 1
339
James E. Blairb0fcae42012-07-17 11:12:10 -0700340
341class FakeGerrit(object):
342 def __init__(self, *args, **kw):
343 self.event_queue = Queue.Queue()
344 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
345 self.change_number = 0
346 self.changes = {}
347
348 def addFakeChange(self, project, branch, subject):
349 self.change_number += 1
James E. Blair8cc15a82012-08-01 11:17:57 -0700350 c = FakeChange(self, self.change_number, project, branch, subject)
James E. Blairb0fcae42012-07-17 11:12:10 -0700351 self.changes[self.change_number] = c
352 return c
353
354 def addEvent(self, data):
355 return self.event_queue.put(data)
356
357 def getEvent(self):
358 return self.event_queue.get()
359
360 def eventDone(self):
361 self.event_queue.task_done()
362
363 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700364 number, ps = changeid.split(',')
365 change = self.changes[int(number)]
James E. Blairb0fcae42012-07-17 11:12:10 -0700366 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700367 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700368 if message:
369 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700370
371 def query(self, number):
372 change = self.changes[int(number)]
373 return change.query()
374
375 def startWatching(self, *args, **kw):
376 pass
377
378
379class FakeJenkinsEvent(object):
380 def __init__(self, name, number, parameters, phase, status=None):
Zhongyue Luo5d556072012-09-21 02:00:47 +0900381 data = {
382 'build': {
383 'full_url': 'https://server/job/%s/%s/' % (name, number),
384 'number': number,
385 'parameters': parameters,
386 'phase': phase,
387 'url': 'job/%s/%s/' % (name, number),
388 },
389 'name': name,
390 'url': 'job/%s/' % name,
391 }
James E. Blairb0fcae42012-07-17 11:12:10 -0700392 if status:
393 data['build']['status'] = status
394 self.body = json.dumps(data)
395
396
397class FakeJenkinsJob(threading.Thread):
398 log = logging.getLogger("zuul.test")
399
400 def __init__(self, jenkins, callback, name, number, parameters):
401 threading.Thread.__init__(self)
402 self.jenkins = jenkins
403 self.callback = callback
404 self.name = name
405 self.number = number
406 self.parameters = parameters
407 self.wait_condition = threading.Condition()
408 self.waiting = False
James E. Blaird466dc42012-07-31 10:42:56 -0700409 self.aborted = False
410 self.canceled = False
411 self.created = time.time()
James E. Blairb0fcae42012-07-17 11:12:10 -0700412
413 def release(self):
414 self.wait_condition.acquire()
415 self.wait_condition.notify()
416 self.waiting = False
417 self.log.debug("Job %s released" % (self.parameters['UUID']))
418 self.wait_condition.release()
419
420 def isWaiting(self):
421 self.wait_condition.acquire()
422 if self.waiting:
423 ret = True
424 else:
425 ret = False
426 self.wait_condition.release()
427 return ret
428
429 def _wait(self):
430 self.wait_condition.acquire()
431 self.waiting = True
432 self.log.debug("Job %s waiting" % (self.parameters['UUID']))
433 self.wait_condition.wait()
434 self.wait_condition.release()
435
436 def run(self):
437 self.jenkins.fakeEnqueue(self)
438 if self.jenkins.hold_jobs_in_queue:
439 self._wait()
440 self.jenkins.fakeDequeue(self)
James E. Blaird466dc42012-07-31 10:42:56 -0700441 if self.canceled:
442 self.jenkins.all_jobs.remove(self)
443 return
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800444 self.callback.jenkins_endpoint(FakeJenkinsEvent(self.name,
445 self.number,
446 self.parameters,
447 'STARTED'))
James E. Blairb0fcae42012-07-17 11:12:10 -0700448 if self.jenkins.hold_jobs_in_build:
449 self._wait()
450 self.log.debug("Job %s continuing" % (self.parameters['UUID']))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700451
452 result = 'SUCCESS'
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800453 if (('ZUUL_REF' in self.parameters) and
454 self.jenkins.fakeShouldFailTest(self.name,
455 self.parameters['ZUUL_REF'])):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700456 result = 'FAILURE'
James E. Blaird466dc42012-07-31 10:42:56 -0700457 if self.aborted:
458 result = 'ABORTED'
James E. Blairb02a3bb2012-07-30 17:49:55 -0700459
James E. Blair0018a6c2013-02-27 14:11:45 -0800460 changes = None
461 if 'ZUUL_CHANGE_IDS' in self.parameters:
462 changes = self.parameters['ZUUL_CHANGE_IDS']
463
James E. Blairb0fcae42012-07-17 11:12:10 -0700464 self.jenkins.fakeAddHistory(name=self.name, number=self.number,
James E. Blair0018a6c2013-02-27 14:11:45 -0800465 result=result, changes=changes)
James E. Blairff791972013-01-09 11:45:43 -0800466 self.jenkins.lock.acquire()
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800467 self.callback.jenkins_endpoint(FakeJenkinsEvent(self.name,
468 self.number,
469 self.parameters,
470 'COMPLETED',
471 result))
472 self.callback.jenkins_endpoint(FakeJenkinsEvent(self.name,
473 self.number,
474 self.parameters,
475 'FINISHED',
476 result))
James E. Blairb0fcae42012-07-17 11:12:10 -0700477 self.jenkins.all_jobs.remove(self)
James E. Blairff791972013-01-09 11:45:43 -0800478 self.jenkins.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700479
480
481class FakeJenkins(object):
482 log = logging.getLogger("zuul.test")
483
484 def __init__(self, *args, **kw):
485 self.queue = []
486 self.all_jobs = []
487 self.job_counter = {}
James E. Blaird466dc42012-07-31 10:42:56 -0700488 self.queue_counter = 0
James E. Blairb0fcae42012-07-17 11:12:10 -0700489 self.job_history = []
490 self.hold_jobs_in_queue = False
491 self.hold_jobs_in_build = False
James E. Blairb02a3bb2012-07-30 17:49:55 -0700492 self.fail_tests = {}
James E. Blair7ee88a22012-09-12 18:59:31 +0200493 self.nonexistent_jobs = []
James E. Blairff791972013-01-09 11:45:43 -0800494 self.lock = threading.Lock()
James E. Blairb0fcae42012-07-17 11:12:10 -0700495
496 def fakeEnqueue(self, job):
497 self.queue.append(job)
498
499 def fakeDequeue(self, job):
500 self.queue.remove(job)
501
James E. Blair0018a6c2013-02-27 14:11:45 -0800502 class FakeJobHistory(object):
503 def __init__(self, **kw):
504 self.__dict__.update(kw)
505
506 def __repr__(self):
507 return ("<Completed job, result: %s name: %s #%s changes: %s>" %
508 (self.result, self.name, self.number, self.changes))
509
James E. Blairb0fcae42012-07-17 11:12:10 -0700510 def fakeAddHistory(self, **kw):
James E. Blair0018a6c2013-02-27 14:11:45 -0800511 self.job_history.append(self.FakeJobHistory(**kw))
James E. Blairb0fcae42012-07-17 11:12:10 -0700512
513 def fakeRelease(self, regex=None):
514 all_jobs = self.all_jobs[:]
515 self.log.debug("releasing jobs %s (%s)" % (regex, len(self.all_jobs)))
516 for job in all_jobs:
517 if not regex or re.match(regex, job.name):
518 self.log.debug("releasing job %s" % (job.parameters['UUID']))
519 job.release()
520 else:
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800521 self.log.debug("not releasing job %s" %
522 (job.parameters['UUID']))
James E. Blairb0fcae42012-07-17 11:12:10 -0700523 self.log.debug("done releasing jobs %s (%s)" % (regex,
524 len(self.all_jobs)))
525
526 def fakeAllWaiting(self, regex=None):
James E. Blair4aa1ad62012-10-05 12:39:26 -0700527 all_jobs = self.all_jobs[:] + self.queue[:]
James E. Blairb0fcae42012-07-17 11:12:10 -0700528 for job in all_jobs:
529 self.log.debug("job %s %s" % (job.parameters['UUID'],
530 job.isWaiting()))
531 if not job.isWaiting():
532 return False
533 return True
534
James E. Blairb02a3bb2012-07-30 17:49:55 -0700535 def fakeAddFailTest(self, name, change):
536 l = self.fail_tests.get(name, [])
537 l.append(change)
538 self.fail_tests[name] = l
539
James E. Blair4886cc12012-07-18 15:39:41 -0700540 def fakeShouldFailTest(self, name, ref):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700541 l = self.fail_tests.get(name, [])
542 for change in l:
James E. Blair4886cc12012-07-18 15:39:41 -0700543 if ref_has_change(ref, change):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700544 return True
545 return False
546
James E. Blairb0fcae42012-07-17 11:12:10 -0700547 def build_job(self, name, parameters):
James E. Blair7ee88a22012-09-12 18:59:31 +0200548 if name in self.nonexistent_jobs:
549 raise Exception("Job does not exist")
James E. Blairb0fcae42012-07-17 11:12:10 -0700550 count = self.job_counter.get(name, 0)
551 count += 1
552 self.job_counter[name] = count
James E. Blaird466dc42012-07-31 10:42:56 -0700553
554 queue_count = self.queue_counter
555 self.queue_counter += 1
James E. Blairb0fcae42012-07-17 11:12:10 -0700556 job = FakeJenkinsJob(self, self.callback, name, count, parameters)
James E. Blaird466dc42012-07-31 10:42:56 -0700557 job.queue_id = queue_count
558
James E. Blairb0fcae42012-07-17 11:12:10 -0700559 self.all_jobs.append(job)
560 job.start()
561
James E. Blaird466dc42012-07-31 10:42:56 -0700562 def stop_build(self, name, number):
563 for job in self.all_jobs:
564 if job.name == name and job.number == number:
565 job.aborted = True
566 job.release()
567 return
568
569 def cancel_queue(self, id):
570 for job in self.queue:
571 if job.queue_id == id:
572 job.canceled = True
573 job.release()
574 return
575
576 def get_queue_info(self):
577 items = []
James E. Blair1490eba2013-03-06 19:14:00 -0800578 for job in self.queue[:]:
579 self.log.debug("Queue info: %s %s" % (job.name,
580 job.parameters['UUID']))
James E. Blaird466dc42012-07-31 10:42:56 -0700581 paramstr = ''
582 paramlst = []
583 d = {'actions': [{'parameters': paramlst},
584 {'causes': [{'shortDescription':
585 'Started by user Jenkins',
586 'userId': 'jenkins',
587 'userName': 'Jenkins'}]}],
588 'blocked': False,
589 'buildable': True,
590 'buildableStartMilliseconds': (job.created * 1000) + 5,
591 'id': job.queue_id,
592 'inQueueSince': (job.created * 1000),
593 'params': paramstr,
594 'stuck': False,
595 'task': {'color': 'blue',
596 'name': job.name,
597 'url': 'https://server/job/%s/' % job.name},
598 'why': 'Waiting for next available executor'}
599 for k, v in job.parameters.items():
600 paramstr += "\n(StringParameterValue) %s='%s'" % (k, v)
601 pd = {'name': k, 'value': v}
602 paramlst.append(pd)
603 items.append(d)
604 return items
605
James E. Blairb0fcae42012-07-17 11:12:10 -0700606 def set_build_description(self, *args, **kw):
607 pass
608
609
610class FakeJenkinsCallback(zuul.launcher.jenkins.JenkinsCallback):
611 def start(self):
612 pass
613
614
James E. Blair8cc15a82012-08-01 11:17:57 -0700615class FakeURLOpener(object):
616 def __init__(self, fake_gerrit, url):
617 self.fake_gerrit = fake_gerrit
618 self.url = url
619
620 def read(self):
621 res = urlparse.urlparse(self.url)
622 path = res.path
623 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200624 ret = '001e# service=git-upload-pack\n'
625 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
626 'multi_ack thin-pack side-band side-band-64k ofs-delta '
627 'shallow no-progress include-tag multi_ack_detailed no-done\n')
James E. Blair1dbd5082012-08-23 15:12:15 -0700628 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700629 repo = git.Repo(path)
630 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200631 r = ref.object.hexsha + ' ' + ref.path + '\n'
632 ret += '%04x%s' % (len(r) + 4, r)
633 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700634 return ret
635
636
James E. Blair4886cc12012-07-18 15:39:41 -0700637class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
638 def getGitUrl(self, project):
James E. Blair1dbd5082012-08-23 15:12:15 -0700639 return os.path.join(UPSTREAM_ROOT, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700640
641
James E. Blairb0fcae42012-07-17 11:12:10 -0700642class testScheduler(unittest.TestCase):
643 log = logging.getLogger("zuul.test")
644
645 def setUp(self):
James E. Blair1dbd5082012-08-23 15:12:15 -0700646 if os.path.exists(TEST_ROOT):
647 shutil.rmtree(TEST_ROOT)
648 os.makedirs(TEST_ROOT)
649 os.makedirs(UPSTREAM_ROOT)
650 os.makedirs(GIT_ROOT)
James E. Blair4886cc12012-07-18 15:39:41 -0700651
652 # For each project in config:
653 init_repo("org/project")
654 init_repo("org/project1")
655 init_repo("org/project2")
James E. Blair127bc182012-08-28 15:55:15 -0700656 init_repo("org/project3")
James E. Blair7f71c802012-08-22 13:04:32 -0700657 init_repo("org/one-job-project")
James E. Blair4ec821f2012-08-23 15:28:28 -0700658 init_repo("org/nonvoting-project")
James E. Blairb0fcae42012-07-17 11:12:10 -0700659 self.config = CONFIG
660 self.sched = zuul.scheduler.Scheduler()
661
662 def jenkinsFactory(*args, **kw):
663 self.fake_jenkins = FakeJenkins()
664 return self.fake_jenkins
665
666 def jenkinsCallbackFactory(*args, **kw):
667 self.fake_jenkins_callback = FakeJenkinsCallback(*args, **kw)
668 return self.fake_jenkins_callback
669
James E. Blair8cc15a82012-08-01 11:17:57 -0700670 def URLOpenerFactory(*args, **kw):
671 args = [self.fake_gerrit] + list(args)
672 return FakeURLOpener(*args, **kw)
673
James E. Blairb0fcae42012-07-17 11:12:10 -0700674 zuul.launcher.jenkins.ExtendedJenkins = jenkinsFactory
675 zuul.launcher.jenkins.JenkinsCallback = jenkinsCallbackFactory
James E. Blair8cc15a82012-08-01 11:17:57 -0700676 urllib2.urlopen = URLOpenerFactory
James E. Blairb0fcae42012-07-17 11:12:10 -0700677 self.jenkins = zuul.launcher.jenkins.Jenkins(self.config, self.sched)
678 self.fake_jenkins.callback = self.fake_jenkins_callback
679
680 zuul.lib.gerrit.Gerrit = FakeGerrit
681
James E. Blair4886cc12012-07-18 15:39:41 -0700682 self.gerrit = FakeGerritTrigger(self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700683 self.gerrit.replication_timeout = 1.5
684 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700685 self.fake_gerrit = self.gerrit.gerrit
686
687 self.sched.setLauncher(self.jenkins)
688 self.sched.setTrigger(self.gerrit)
689
690 self.sched.start()
691 self.sched.reconfigure(self.config)
692 self.sched.resume()
693
694 def tearDown(self):
695 self.jenkins.stop()
696 self.gerrit.stop()
697 self.sched.stop()
698 self.sched.join()
James E. Blair1dbd5082012-08-23 15:12:15 -0700699 #shutil.rmtree(TEST_ROOT)
James E. Blairb0fcae42012-07-17 11:12:10 -0700700
701 def waitUntilSettled(self):
702 self.log.debug("Waiting until settled...")
703 start = time.time()
704 while True:
705 if time.time() - start > 10:
706 print 'queue status:',
707 print self.sched.trigger_event_queue.empty(),
708 print self.sched.result_event_queue.empty(),
709 print self.fake_gerrit.event_queue.empty(),
710 raise Exception("Timeout waiting for Zuul to settle")
James E. Blairff791972013-01-09 11:45:43 -0800711 # Make sure our fake jenkins doesn't end any jobs
712 # (and therefore, emit events) while we're checking
713 self.fake_jenkins.lock.acquire()
714 # Join ensures that the queue is empty _and_ events have been
715 # processed
James E. Blairb0fcae42012-07-17 11:12:10 -0700716 self.fake_gerrit.event_queue.join()
James E. Blairff791972013-01-09 11:45:43 -0800717 self.sched.trigger_event_queue.join()
718 self.sched.result_event_queue.join()
James E. Blairb0fcae42012-07-17 11:12:10 -0700719 if (self.sched.trigger_event_queue.empty() and
720 self.sched.result_event_queue.empty() and
721 self.fake_gerrit.event_queue.empty() and
722 self.fake_jenkins.fakeAllWaiting()):
James E. Blairff791972013-01-09 11:45:43 -0800723 self.fake_jenkins.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700724 self.log.debug("...settled.")
725 return
James E. Blairff791972013-01-09 11:45:43 -0800726 self.fake_jenkins.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700727 self.sched.wake_event.wait(0.1)
728
James E. Blaird466dc42012-07-31 10:42:56 -0700729 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -0800730 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -0700731 return len(jobs)
732
James E. Blaire0487072012-08-29 17:38:31 -0700733 def assertEmptyQueues(self):
734 # Make sure there are no orphaned jobs
735 for pipeline in self.sched.pipelines.values():
736 for queue in pipeline.queues:
737 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -0800738 print 'pipeline %s queue %s contents %s' % (
739 pipeline.name, queue.name, queue.queue)
James E. Blaire0487072012-08-29 17:38:31 -0700740 assert len(queue.queue) == 0
741 if len(queue.severed_heads) != 0:
742 print 'heads', queue.severed_heads
743 assert len(queue.severed_heads) == 0
744
James E. Blairb0fcae42012-07-17 11:12:10 -0700745 def test_jobs_launched(self):
746 "Test that jobs are launched and a change is merged"
747 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -0700748 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700749 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
750 self.waitUntilSettled()
751 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -0800752 job_names = [x.name for x in jobs]
James E. Blairb0fcae42012-07-17 11:12:10 -0700753 assert 'project-merge' in job_names
754 assert 'project-test1' in job_names
755 assert 'project-test2' in job_names
James E. Blair0018a6c2013-02-27 14:11:45 -0800756 assert jobs[0].result == 'SUCCESS'
757 assert jobs[1].result == 'SUCCESS'
758 assert jobs[2].result == 'SUCCESS'
James E. Blairb0fcae42012-07-17 11:12:10 -0700759 assert A.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700760 assert A.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700761 self.assertEmptyQueues()
James E. Blairb0fcae42012-07-17 11:12:10 -0700762
763 def test_parallel_changes(self):
764 "Test that changes are tested in parallel and merged in series"
765 self.fake_jenkins.hold_jobs_in_build = True
766 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
767 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
768 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700769 A.addApproval('CRVW', 2)
770 B.addApproval('CRVW', 2)
771 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700772
773 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
774 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
775 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
776
777 self.waitUntilSettled()
778 jobs = self.fake_jenkins.all_jobs
779 assert len(jobs) == 1
780 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700781 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700782
783 self.fake_jenkins.fakeRelease('.*-merge')
784 self.waitUntilSettled()
785 assert len(jobs) == 3
786 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700787 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700788 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700789 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700790 assert jobs[2].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700791 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700792
793 self.fake_jenkins.fakeRelease('.*-merge')
794 self.waitUntilSettled()
795 assert len(jobs) == 5
796 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700797 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700798 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700799 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700800
801 assert jobs[2].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700802 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700803 assert jobs[3].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700804 assert job_has_changes(jobs[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700805
806 assert jobs[4].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700807 assert job_has_changes(jobs[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700808
809 self.fake_jenkins.fakeRelease('.*-merge')
810 self.waitUntilSettled()
811 assert len(jobs) == 6
812 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700813 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700814 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700815 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700816
817 assert jobs[2].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700818 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700819 assert jobs[3].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700820 assert job_has_changes(jobs[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700821
822 assert jobs[4].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700823 assert job_has_changes(jobs[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700824 assert jobs[5].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700825 assert job_has_changes(jobs[5], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700826
827 self.fake_jenkins.hold_jobs_in_build = False
828 self.fake_jenkins.fakeRelease()
829 self.waitUntilSettled()
830 assert len(jobs) == 0
831
832 jobs = self.fake_jenkins.job_history
833 assert len(jobs) == 9
834 assert A.data['status'] == 'MERGED'
835 assert B.data['status'] == 'MERGED'
836 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700837 assert A.reported == 2
838 assert B.reported == 2
839 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700840 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700841
842 def test_failed_changes(self):
843 "Test that a change behind a failed change is retested"
844 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
845 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -0700846 A.addApproval('CRVW', 2)
847 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700848
849 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
850 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
851
James E. Blair4886cc12012-07-18 15:39:41 -0700852 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700853
854 self.waitUntilSettled()
855 jobs = self.fake_jenkins.job_history
856 assert len(jobs) > 6
857 assert A.data['status'] == 'NEW'
858 assert B.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700859 assert A.reported == 2
860 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700861 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700862
863 def test_independent_queues(self):
864 "Test that changes end up in the right queues"
865 self.fake_jenkins.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900866 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700867 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
868 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700869 A.addApproval('CRVW', 2)
870 B.addApproval('CRVW', 2)
871 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700872
873 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
874 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
875 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
876
877 jobs = self.fake_jenkins.all_jobs
878 self.waitUntilSettled()
879
880 # There should be one merge job at the head of each queue running
881 assert len(jobs) == 2
882 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700883 assert job_has_changes(jobs[0], A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700884 assert jobs[1].name == 'project1-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700885 assert job_has_changes(jobs[1], B)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700886
887 # Release the current merge jobs
888 self.fake_jenkins.fakeRelease('.*-merge')
889 self.waitUntilSettled()
890 # Release the merge job for project2 which is behind project1
891 self.fake_jenkins.fakeRelease('.*-merge')
892 self.waitUntilSettled()
893
894 # All the test jobs should be running:
895 # project1 (3) + project2 (3) + project (2) = 8
896 assert len(jobs) == 8
897
898 self.fake_jenkins.fakeRelease()
899 self.waitUntilSettled()
900 assert len(jobs) == 0
901
902 jobs = self.fake_jenkins.job_history
903 assert len(jobs) == 11
904 assert A.data['status'] == 'MERGED'
905 assert B.data['status'] == 'MERGED'
906 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700907 assert A.reported == 2
908 assert B.reported == 2
909 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700910 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -0700911
912 def test_failed_change_at_head(self):
913 "Test that if a change at the head fails, jobs behind it are canceled"
914 self.fake_jenkins.hold_jobs_in_build = True
915
916 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
917 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
918 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700919 A.addApproval('CRVW', 2)
920 B.addApproval('CRVW', 2)
921 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700922
James E. Blair4886cc12012-07-18 15:39:41 -0700923 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700924
925 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
926 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
927 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
928
929 self.waitUntilSettled()
930 jobs = self.fake_jenkins.all_jobs
931 finished_jobs = self.fake_jenkins.job_history
932
933 assert len(jobs) == 1
934 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700935 assert job_has_changes(jobs[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -0700936
937 self.fake_jenkins.fakeRelease('.*-merge')
938 self.waitUntilSettled()
939 self.fake_jenkins.fakeRelease('.*-merge')
940 self.waitUntilSettled()
941 self.fake_jenkins.fakeRelease('.*-merge')
942 self.waitUntilSettled()
943
944 assert len(jobs) == 6
945 assert jobs[0].name == 'project-test1'
946 assert jobs[1].name == 'project-test2'
947 assert jobs[2].name == 'project-test1'
948 assert jobs[3].name == 'project-test2'
949 assert jobs[4].name == 'project-test1'
950 assert jobs[5].name == 'project-test2'
951
952 jobs[0].release()
953 self.waitUntilSettled()
954
James E. Blairec590122012-08-22 15:19:31 -0700955 assert len(jobs) == 2 # project-test2, project-merge for B
James E. Blaird466dc42012-07-31 10:42:56 -0700956 assert self.countJobResults(finished_jobs, 'ABORTED') == 4
957
958 self.fake_jenkins.hold_jobs_in_build = False
959 self.fake_jenkins.fakeRelease()
960 self.waitUntilSettled()
961
James E. Blair0018a6c2013-02-27 14:11:45 -0800962 for x in jobs:
963 print x
964 for x in finished_jobs:
965 print x
966
James E. Blaird466dc42012-07-31 10:42:56 -0700967 assert len(jobs) == 0
968 assert len(finished_jobs) == 15
969 assert A.data['status'] == 'NEW'
970 assert B.data['status'] == 'MERGED'
971 assert C.data['status'] == 'MERGED'
972 assert A.reported == 2
973 assert B.reported == 2
974 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700975 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -0700976
977 def test_failed_change_at_head_with_queue(self):
978 "Test that if a change at the head fails, queued jobs are canceled"
979 self.fake_jenkins.hold_jobs_in_queue = True
980
981 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
982 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
983 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700984 A.addApproval('CRVW', 2)
985 B.addApproval('CRVW', 2)
986 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700987
James E. Blair4886cc12012-07-18 15:39:41 -0700988 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700989
990 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
991 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
992 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
993
994 self.waitUntilSettled()
995 jobs = self.fake_jenkins.all_jobs
996 finished_jobs = self.fake_jenkins.job_history
997 queue = self.fake_jenkins.queue
998
999 assert len(jobs) == 1
1000 assert len(queue) == 1
1001 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -07001002 assert job_has_changes(jobs[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -07001003
1004 self.fake_jenkins.fakeRelease('.*-merge')
1005 self.waitUntilSettled()
1006 self.fake_jenkins.fakeRelease('.*-merge')
1007 self.waitUntilSettled()
1008 self.fake_jenkins.fakeRelease('.*-merge')
1009 self.waitUntilSettled()
1010
1011 assert len(jobs) == 6
1012 assert len(queue) == 6
1013 assert jobs[0].name == 'project-test1'
1014 assert jobs[1].name == 'project-test2'
1015 assert jobs[2].name == 'project-test1'
1016 assert jobs[3].name == 'project-test2'
1017 assert jobs[4].name == 'project-test1'
1018 assert jobs[5].name == 'project-test2'
1019
1020 jobs[0].release()
1021 self.waitUntilSettled()
1022
James E. Blairec590122012-08-22 15:19:31 -07001023 assert len(jobs) == 2 # project-test2, project-merge for B
1024 assert len(queue) == 2
James E. Blaird466dc42012-07-31 10:42:56 -07001025 assert self.countJobResults(finished_jobs, 'ABORTED') == 0
1026
1027 self.fake_jenkins.hold_jobs_in_queue = False
1028 self.fake_jenkins.fakeRelease()
1029 self.waitUntilSettled()
1030
1031 assert len(jobs) == 0
1032 assert len(finished_jobs) == 11
1033 assert A.data['status'] == 'NEW'
1034 assert B.data['status'] == 'MERGED'
1035 assert C.data['status'] == 'MERGED'
1036 assert A.reported == 2
1037 assert B.reported == 2
1038 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001039 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001040
1041 def test_patch_order(self):
1042 "Test that dependent patches are tested in the right order"
1043 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1044 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1045 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1046 A.addApproval('CRVW', 2)
1047 B.addApproval('CRVW', 2)
1048 C.addApproval('CRVW', 2)
1049
1050 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1051 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1052 M2.setMerged()
1053 M1.setMerged()
1054
1055 # C -> B -> A -> M1 -> M2
1056 # M2 is here to make sure it is never queried. If it is, it
1057 # means zuul is walking down the entire history of merged
1058 # changes.
1059
1060 C.setDependsOn(B, 1)
1061 B.setDependsOn(A, 1)
1062 A.setDependsOn(M1, 1)
1063 M1.setDependsOn(M2, 1)
1064
1065 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1066
1067 self.waitUntilSettled()
1068
1069 assert A.data['status'] == 'NEW'
1070 assert B.data['status'] == 'NEW'
1071 assert C.data['status'] == 'NEW'
1072
1073 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1074 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1075
1076 self.waitUntilSettled()
1077 assert M2.queried == 0
1078 assert A.data['status'] == 'MERGED'
1079 assert B.data['status'] == 'MERGED'
1080 assert C.data['status'] == 'MERGED'
1081 assert A.reported == 2
1082 assert B.reported == 2
1083 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001084 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001085
1086 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001087 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001088 # TODO: move to test_gerrit (this is a unit test!)
1089 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4aea70c2012-07-26 14:23:24 -07001090 a = self.sched.trigger.getChange(1, 2)
1091 mgr = self.sched.pipelines['gate'].manager
1092 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001093
1094 A.addApproval('CRVW', 2)
James E. Blair4aea70c2012-07-26 14:23:24 -07001095 a = self.sched.trigger.getChange(1, 2)
1096 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001097
1098 A.addApproval('APRV', 1)
James E. Blair4aea70c2012-07-26 14:23:24 -07001099 a = self.sched.trigger.getChange(1, 2)
1100 assert self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blaire0487072012-08-29 17:38:31 -07001101 self.assertEmptyQueues()
James E. Blair4886cc12012-07-18 15:39:41 -07001102
1103 def test_build_configuration(self):
1104 "Test that zuul merges the right commits for testing"
1105 self.fake_jenkins.hold_jobs_in_queue = True
1106 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1107 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1108 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1109 A.addApproval('CRVW', 2)
1110 B.addApproval('CRVW', 2)
1111 C.addApproval('CRVW', 2)
1112 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1113 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1114 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1115 self.waitUntilSettled()
1116
1117 jobs = self.fake_jenkins.all_jobs
1118
1119 self.fake_jenkins.fakeRelease('.*-merge')
1120 self.waitUntilSettled()
1121 self.fake_jenkins.fakeRelease('.*-merge')
1122 self.waitUntilSettled()
1123 self.fake_jenkins.fakeRelease('.*-merge')
1124 self.waitUntilSettled()
James E. Blair1dbd5082012-08-23 15:12:15 -07001125
James E. Blair4886cc12012-07-18 15:39:41 -07001126 ref = jobs[-1].parameters['ZUUL_REF']
1127 self.fake_jenkins.hold_jobs_in_queue = False
1128 self.fake_jenkins.fakeRelease()
James E. Blair973721f2012-08-15 10:19:43 -07001129 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001130
James E. Blair1dbd5082012-08-23 15:12:15 -07001131 path = os.path.join(GIT_ROOT, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001132 repo = git.Repo(path)
1133 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1134 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001135 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
1136 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001137 self.assertEmptyQueues()
James E. Blair973721f2012-08-15 10:19:43 -07001138
1139 def test_build_configuration_conflict(self):
1140 "Test that merge conflicts are handled"
1141 self.fake_jenkins.hold_jobs_in_queue = True
1142 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1143 A.addPatchset(['conflict'])
1144 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1145 B.addPatchset(['conflict'])
1146 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1147 A.addApproval('CRVW', 2)
1148 B.addApproval('CRVW', 2)
1149 C.addApproval('CRVW', 2)
1150 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1151 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1152 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1153 self.waitUntilSettled()
1154
1155 jobs = self.fake_jenkins.all_jobs
1156
1157 self.fake_jenkins.fakeRelease('.*-merge')
1158 self.waitUntilSettled()
1159 self.fake_jenkins.fakeRelease('.*-merge')
1160 self.waitUntilSettled()
1161 self.fake_jenkins.fakeRelease('.*-merge')
1162 self.waitUntilSettled()
1163 ref = jobs[-1].parameters['ZUUL_REF']
1164 self.fake_jenkins.hold_jobs_in_queue = False
1165 self.fake_jenkins.fakeRelease()
1166 self.waitUntilSettled()
1167
1168 assert A.data['status'] == 'MERGED'
1169 assert B.data['status'] == 'NEW'
1170 assert C.data['status'] == 'MERGED'
1171 assert A.reported == 2
1172 assert B.reported == 2
1173 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001174 self.assertEmptyQueues()
James E. Blairdaabed22012-08-15 15:38:57 -07001175
1176 def test_post(self):
1177 "Test that post jobs run"
Zhongyue Luo5d556072012-09-21 02:00:47 +09001178 e = {
1179 "type": "ref-updated",
1180 "submitter": {
1181 "name": "User Name",
1182 },
1183 "refUpdate": {
1184 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1185 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1186 "refName": "master",
1187 "project": "org/project",
1188 }
1189 }
James E. Blairdaabed22012-08-15 15:38:57 -07001190 self.fake_gerrit.addEvent(e)
1191 self.waitUntilSettled()
1192
1193 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001194 job_names = [x.name for x in jobs]
James E. Blairdaabed22012-08-15 15:38:57 -07001195 assert len(jobs) == 1
1196 assert 'project-post' in job_names
James E. Blaire0487072012-08-29 17:38:31 -07001197 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001198
1199 def test_build_configuration_branch(self):
1200 "Test that the right commits are on alternate branches"
1201 self.fake_jenkins.hold_jobs_in_queue = True
1202 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1203 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1204 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1205 A.addApproval('CRVW', 2)
1206 B.addApproval('CRVW', 2)
1207 C.addApproval('CRVW', 2)
1208 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1209 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1210 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1211 self.waitUntilSettled()
1212
1213 jobs = self.fake_jenkins.all_jobs
1214
1215 self.fake_jenkins.fakeRelease('.*-merge')
1216 self.waitUntilSettled()
1217 self.fake_jenkins.fakeRelease('.*-merge')
1218 self.waitUntilSettled()
1219 self.fake_jenkins.fakeRelease('.*-merge')
1220 self.waitUntilSettled()
1221 ref = jobs[-1].parameters['ZUUL_REF']
1222 self.fake_jenkins.hold_jobs_in_queue = False
1223 self.fake_jenkins.fakeRelease()
1224 self.waitUntilSettled()
1225
James E. Blair1dbd5082012-08-23 15:12:15 -07001226 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001227 repo = git.Repo(path)
1228 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1229 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001230 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
1231 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001232 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001233
1234 def test_build_configuration_branch_interaction(self):
1235 "Test that switching between branches works"
1236 self.test_build_configuration()
1237 self.test_build_configuration_branch()
1238 # C has been merged, undo that
James E. Blair1dbd5082012-08-23 15:12:15 -07001239 path = os.path.join(UPSTREAM_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001240 repo = git.Repo(path)
1241 repo.heads.master.commit = repo.commit('init')
1242 self.test_build_configuration()
James E. Blaire0487072012-08-29 17:38:31 -07001243 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001244
1245 def test_build_configuration_multi_branch(self):
1246 "Test that dependent changes on multiple branches are merged"
1247 self.fake_jenkins.hold_jobs_in_queue = True
1248 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1249 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1250 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1251 A.addApproval('CRVW', 2)
1252 B.addApproval('CRVW', 2)
1253 C.addApproval('CRVW', 2)
1254 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1255 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1256 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1257 self.waitUntilSettled()
1258
1259 jobs = self.fake_jenkins.all_jobs
1260
1261 self.fake_jenkins.fakeRelease('.*-merge')
1262 self.waitUntilSettled()
1263 ref_mp = jobs[-1].parameters['ZUUL_REF']
1264 self.fake_jenkins.fakeRelease('.*-merge')
1265 self.waitUntilSettled()
1266 self.fake_jenkins.fakeRelease('.*-merge')
1267 self.waitUntilSettled()
1268 ref_master = jobs[-1].parameters['ZUUL_REF']
1269 self.fake_jenkins.hold_jobs_in_queue = False
1270 self.fake_jenkins.fakeRelease()
1271 self.waitUntilSettled()
1272
James E. Blair1dbd5082012-08-23 15:12:15 -07001273 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001274 repo = git.Repo(path)
1275
1276 repo_messages = [c.message.strip()
1277 for c in repo.iter_commits(ref_master)]
1278 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001279 correct_messages = ['initial commit', 'A-1', 'C-1']
1280 assert repo_messages == correct_messages
1281
1282 repo_messages = [c.message.strip()
1283 for c in repo.iter_commits(ref_mp)]
1284 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001285 correct_messages = ['initial commit', 'mp commit', 'B-1']
1286 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001287 self.assertEmptyQueues()
James E. Blair7f71c802012-08-22 13:04:32 -07001288
1289 def test_one_job_project(self):
1290 "Test that queueing works with one job"
1291 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1292 'master', 'A')
1293 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1294 'master', 'B')
1295 A.addApproval('CRVW', 2)
1296 B.addApproval('CRVW', 2)
1297 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1298 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1299 self.waitUntilSettled()
1300
1301 jobs = self.fake_jenkins.all_jobs
1302 finished_jobs = self.fake_jenkins.job_history
James E. Blair7f71c802012-08-22 13:04:32 -07001303
1304 assert A.data['status'] == 'MERGED'
1305 assert A.reported == 2
1306 assert B.data['status'] == 'MERGED'
1307 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001308 self.assertEmptyQueues()
James E. Blaircaec0c52012-08-22 14:52:22 -07001309
1310 def test_dependent_changes_dequeue(self):
1311 "Test that dependent patches are not needlessly tested"
1312 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1313 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1314 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1315 A.addApproval('CRVW', 2)
1316 B.addApproval('CRVW', 2)
1317 C.addApproval('CRVW', 2)
1318
1319 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1320 M1.setMerged()
1321
1322 # C -> B -> A -> M1
1323
1324 C.setDependsOn(B, 1)
1325 B.setDependsOn(A, 1)
1326 A.setDependsOn(M1, 1)
1327
1328 self.fake_jenkins.fakeAddFailTest('project-merge', A)
1329
1330 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1331 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1332 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1333
1334 self.waitUntilSettled()
1335
1336 jobs = self.fake_jenkins.all_jobs
1337 finished_jobs = self.fake_jenkins.job_history
1338
James E. Blair127bc182012-08-28 15:55:15 -07001339 for x in jobs:
1340 print x
1341 for x in finished_jobs:
1342 print x
1343
James E. Blairec590122012-08-22 15:19:31 -07001344 assert A.data['status'] == 'NEW'
1345 assert A.reported == 2
1346 assert B.data['status'] == 'NEW'
1347 assert B.reported == 2
1348 assert C.data['status'] == 'NEW'
1349 assert C.reported == 2
1350 assert len(finished_jobs) == 1
James E. Blaire0487072012-08-29 17:38:31 -07001351 self.assertEmptyQueues()
James E. Blairec590122012-08-22 15:19:31 -07001352
1353 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001354 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001355 # If it's dequeued more than once, we should see extra
1356 # aborted jobs.
1357 self.fake_jenkins.hold_jobs_in_build = True
1358
1359 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1360 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1361 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1362 A.addApproval('CRVW', 2)
1363 B.addApproval('CRVW', 2)
1364 C.addApproval('CRVW', 2)
1365
1366 self.fake_jenkins.fakeAddFailTest('project1-test1', A)
1367 self.fake_jenkins.fakeAddFailTest('project1-test2', A)
1368 self.fake_jenkins.fakeAddFailTest('project1-project2-integration', A)
1369
1370 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1371 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1372 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1373
1374 self.waitUntilSettled()
1375 jobs = self.fake_jenkins.all_jobs
1376 finished_jobs = self.fake_jenkins.job_history
1377
1378 assert len(jobs) == 1
1379 assert jobs[0].name == 'project1-merge'
1380 assert job_has_changes(jobs[0], A)
1381
1382 self.fake_jenkins.fakeRelease('.*-merge')
1383 self.waitUntilSettled()
1384 self.fake_jenkins.fakeRelease('.*-merge')
1385 self.waitUntilSettled()
1386 self.fake_jenkins.fakeRelease('.*-merge')
1387 self.waitUntilSettled()
1388
1389 assert len(jobs) == 9
1390 assert jobs[0].name == 'project1-test1'
1391 assert jobs[1].name == 'project1-test2'
1392 assert jobs[2].name == 'project1-project2-integration'
1393 assert jobs[3].name == 'project1-test1'
1394 assert jobs[4].name == 'project1-test2'
1395 assert jobs[5].name == 'project1-project2-integration'
1396 assert jobs[6].name == 'project1-test1'
1397 assert jobs[7].name == 'project1-test2'
1398 assert jobs[8].name == 'project1-project2-integration'
1399
1400 jobs[0].release()
1401 self.waitUntilSettled()
1402
1403 assert len(jobs) == 3 # test2, integration, merge for B
1404 assert self.countJobResults(finished_jobs, 'ABORTED') == 6
1405
1406 self.fake_jenkins.hold_jobs_in_build = False
1407 self.fake_jenkins.fakeRelease()
1408 self.waitUntilSettled()
1409
1410 assert len(jobs) == 0
1411 assert len(finished_jobs) == 20
James E. Blaircaec0c52012-08-22 14:52:22 -07001412
1413 assert A.data['status'] == 'NEW'
James E. Blairec590122012-08-22 15:19:31 -07001414 assert B.data['status'] == 'MERGED'
1415 assert C.data['status'] == 'MERGED'
1416 assert A.reported == 2
1417 assert B.reported == 2
1418 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001419 self.assertEmptyQueues()
James E. Blair4ec821f2012-08-23 15:28:28 -07001420
1421 def test_nonvoting_job(self):
1422 "Test that non-voting jobs don't vote."
1423 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1424 'master', 'A')
1425 A.addApproval('CRVW', 2)
1426 self.fake_jenkins.fakeAddFailTest('nonvoting-project-test2', A)
1427 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1428
1429 self.waitUntilSettled()
1430 jobs = self.fake_jenkins.all_jobs
1431 finished_jobs = self.fake_jenkins.job_history
1432
1433 assert A.data['status'] == 'MERGED'
1434 assert A.reported == 2
James E. Blair0018a6c2013-02-27 14:11:45 -08001435 assert finished_jobs[0].result == 'SUCCESS'
1436 assert finished_jobs[1].result == 'SUCCESS'
1437 assert finished_jobs[2].result == 'FAILURE'
James E. Blaire0487072012-08-29 17:38:31 -07001438 self.assertEmptyQueues()
1439
1440 def test_check_queue_success(self):
1441 "Test successful check queue jobs."
1442 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1443 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1444
1445 self.waitUntilSettled()
1446 jobs = self.fake_jenkins.all_jobs
1447 finished_jobs = self.fake_jenkins.job_history
1448
1449 assert A.data['status'] == 'NEW'
1450 assert A.reported == 1
James E. Blair0018a6c2013-02-27 14:11:45 -08001451 assert finished_jobs[0].result == 'SUCCESS'
1452 assert finished_jobs[1].result == 'SUCCESS'
1453 assert finished_jobs[2].result == 'SUCCESS'
James E. Blaire0487072012-08-29 17:38:31 -07001454 self.assertEmptyQueues()
1455
1456 def test_check_queue_failure(self):
1457 "Test failed check queue jobs."
1458 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1459 self.fake_jenkins.fakeAddFailTest('project-test2', A)
1460 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1461
1462 self.waitUntilSettled()
1463 jobs = self.fake_jenkins.all_jobs
1464 finished_jobs = self.fake_jenkins.job_history
1465
1466 assert A.data['status'] == 'NEW'
1467 assert A.reported == 1
James E. Blair0018a6c2013-02-27 14:11:45 -08001468 assert finished_jobs[0].result == 'SUCCESS'
1469 assert finished_jobs[1].result == 'SUCCESS'
1470 assert finished_jobs[2].result == 'FAILURE'
James E. Blaire0487072012-08-29 17:38:31 -07001471 self.assertEmptyQueues()
James E. Blair127bc182012-08-28 15:55:15 -07001472
1473 def test_dependent_behind_dequeue(self):
1474 "test that dependent changes behind dequeued changes work"
1475 # This complicated test is a reproduction of a real life bug
1476 self.sched.reconfigure(self.config)
1477 self.fake_jenkins.hold_jobs_in_build = True
1478
1479 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1480 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1481 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1482 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1483 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1484 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1485 D.setDependsOn(C, 1)
1486 E.setDependsOn(D, 1)
1487 A.addApproval('CRVW', 2)
1488 B.addApproval('CRVW', 2)
1489 C.addApproval('CRVW', 2)
1490 D.addApproval('CRVW', 2)
1491 E.addApproval('CRVW', 2)
1492 F.addApproval('CRVW', 2)
1493
1494 A.fail_merge = True
1495 jobs = self.fake_jenkins.all_jobs
1496 finished_jobs = self.fake_jenkins.job_history
1497
1498 # Change object re-use in the gerrit trigger is hidden if
1499 # changes are added in quick succession; waiting makes it more
1500 # like real life.
1501 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1502 self.waitUntilSettled()
1503 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1504 self.waitUntilSettled()
1505
1506 self.fake_jenkins.fakeRelease('.*-merge')
1507 self.waitUntilSettled()
1508 self.fake_jenkins.fakeRelease('.*-merge')
1509 self.waitUntilSettled()
1510
1511 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1512 self.waitUntilSettled()
1513 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1514 self.waitUntilSettled()
1515 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1516 self.waitUntilSettled()
1517 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1518 self.waitUntilSettled()
1519
1520 self.fake_jenkins.fakeRelease('.*-merge')
1521 self.waitUntilSettled()
1522 self.fake_jenkins.fakeRelease('.*-merge')
1523 self.waitUntilSettled()
1524 self.fake_jenkins.fakeRelease('.*-merge')
1525 self.waitUntilSettled()
1526 self.fake_jenkins.fakeRelease('.*-merge')
1527 self.waitUntilSettled()
1528
James E. Blair4aa1ad62012-10-05 12:39:26 -07001529 for x in jobs:
1530 print x
James E. Blair127bc182012-08-28 15:55:15 -07001531 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001532
1533 # Grab pointers to the jobs we want to release before
1534 # releasing any, because list indexes may change as
1535 # the jobs complete.
1536 a, b, c = jobs[:3]
1537 a.release()
1538 b.release()
1539 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001540 self.waitUntilSettled()
1541
1542 self.fake_jenkins.hold_jobs_in_build = False
1543 self.fake_jenkins.fakeRelease()
1544 self.waitUntilSettled()
1545
1546 for x in jobs:
1547 print x
1548 for x in finished_jobs:
1549 print x
1550 print self.sched.formatStatusHTML()
1551
1552 assert A.data['status'] == 'NEW'
1553 assert B.data['status'] == 'MERGED'
1554 assert C.data['status'] == 'MERGED'
1555 assert D.data['status'] == 'MERGED'
1556 assert E.data['status'] == 'MERGED'
1557 assert F.data['status'] == 'MERGED'
1558
1559 assert A.reported == 2
1560 assert B.reported == 2
1561 assert C.reported == 2
1562 assert D.reported == 2
1563 assert E.reported == 2
1564 assert F.reported == 2
1565
James E. Blair127bc182012-08-28 15:55:15 -07001566 assert self.countJobResults(finished_jobs, 'ABORTED') == 15
1567 assert len(finished_jobs) == 44
James E. Blaire0487072012-08-29 17:38:31 -07001568 self.assertEmptyQueues()
James E. Blair05fed602012-09-07 12:45:24 -07001569
1570 def test_merger_repack(self):
1571 "Test that the merger works after a repack"
1572 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1573 A.addApproval('CRVW', 2)
1574 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1575 self.waitUntilSettled()
1576 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001577 job_names = [x.name for x in jobs]
James E. Blair05fed602012-09-07 12:45:24 -07001578 assert 'project-merge' in job_names
1579 assert 'project-test1' in job_names
1580 assert 'project-test2' in job_names
James E. Blair0018a6c2013-02-27 14:11:45 -08001581 assert jobs[0].result == 'SUCCESS'
1582 assert jobs[1].result == 'SUCCESS'
1583 assert jobs[2].result == 'SUCCESS'
James E. Blair05fed602012-09-07 12:45:24 -07001584 assert A.data['status'] == 'MERGED'
1585 assert A.reported == 2
1586 self.assertEmptyQueues()
1587
1588 path = os.path.join(GIT_ROOT, "org/project")
1589 os.system('git --git-dir=%s/.git repack -afd' % path)
1590
1591 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1592 A.addApproval('CRVW', 2)
1593 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1594 self.waitUntilSettled()
1595 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001596 job_names = [x.name for x in jobs]
James E. Blair05fed602012-09-07 12:45:24 -07001597 assert 'project-merge' in job_names
1598 assert 'project-test1' in job_names
1599 assert 'project-test2' in job_names
James E. Blair0018a6c2013-02-27 14:11:45 -08001600 assert jobs[0].result == 'SUCCESS'
1601 assert jobs[1].result == 'SUCCESS'
1602 assert jobs[2].result == 'SUCCESS'
James E. Blair05fed602012-09-07 12:45:24 -07001603 assert A.data['status'] == 'MERGED'
1604 assert A.reported == 2
1605 self.assertEmptyQueues()
James E. Blair7ee88a22012-09-12 18:59:31 +02001606
James E. Blair4886f282012-11-15 09:27:33 -08001607 def test_merger_repack_large_change(self):
1608 "Test that the merger works with large changes after a repack"
1609 # https://bugs.launchpad.net/zuul/+bug/1078946
1610 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1611 A.addPatchset(large=True)
1612 path = os.path.join(UPSTREAM_ROOT, "org/project1")
1613 os.system('git --git-dir=%s/.git repack -afd' % path)
1614 path = os.path.join(GIT_ROOT, "org/project1")
1615 os.system('git --git-dir=%s/.git repack -afd' % path)
1616
1617 A.addApproval('CRVW', 2)
1618 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1619 self.waitUntilSettled()
1620 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001621 job_names = [x.name for x in jobs]
James E. Blair4886f282012-11-15 09:27:33 -08001622 assert 'project1-merge' in job_names
1623 assert 'project1-test1' in job_names
1624 assert 'project1-test2' in job_names
James E. Blair0018a6c2013-02-27 14:11:45 -08001625 assert jobs[0].result == 'SUCCESS'
1626 assert jobs[1].result == 'SUCCESS'
1627 assert jobs[2].result == 'SUCCESS'
James E. Blair4886f282012-11-15 09:27:33 -08001628 assert A.data['status'] == 'MERGED'
1629 assert A.reported == 2
1630 self.assertEmptyQueues()
1631
James E. Blair7ee88a22012-09-12 18:59:31 +02001632 def test_nonexistent_job(self):
1633 "Test launching a job that doesn't exist"
1634 self.fake_jenkins.nonexistent_jobs.append('project-merge')
1635 self.jenkins.launch_retry_timeout = 0.1
1636
1637 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1638 A.addApproval('CRVW', 2)
1639 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1640 # There may be a thread about to report a lost change
1641 while A.reported < 2:
1642 self.waitUntilSettled()
1643 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001644 job_names = [x.name for x in jobs]
James E. Blair7ee88a22012-09-12 18:59:31 +02001645 assert not job_names
1646 assert A.data['status'] == 'NEW'
1647 assert A.reported == 2
1648 self.assertEmptyQueues()
1649
1650 # Make sure things still work:
1651 self.fake_jenkins.nonexistent_jobs = []
1652 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1653 A.addApproval('CRVW', 2)
1654 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1655 self.waitUntilSettled()
1656 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001657 job_names = [x.name for x in jobs]
James E. Blair7ee88a22012-09-12 18:59:31 +02001658 assert 'project-merge' in job_names
1659 assert 'project-test1' in job_names
1660 assert 'project-test2' in job_names
James E. Blair0018a6c2013-02-27 14:11:45 -08001661 assert jobs[0].result == 'SUCCESS'
1662 assert jobs[1].result == 'SUCCESS'
1663 assert jobs[2].result == 'SUCCESS'
James E. Blair7ee88a22012-09-12 18:59:31 +02001664 assert A.data['status'] == 'MERGED'
1665 assert A.reported == 2
1666 self.assertEmptyQueues()
James E. Blairf62d4282012-12-31 17:01:50 -08001667
1668 def test_single_nonexistent_post_job(self):
1669 "Test launching a single post job that doesn't exist"
1670 self.fake_jenkins.nonexistent_jobs.append('project-post')
1671 self.jenkins.launch_retry_timeout = 0.1
1672
1673 e = {
1674 "type": "ref-updated",
1675 "submitter": {
1676 "name": "User Name",
1677 },
1678 "refUpdate": {
1679 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1680 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1681 "refName": "master",
1682 "project": "org/project",
1683 }
1684 }
1685 self.fake_gerrit.addEvent(e)
1686 self.waitUntilSettled()
1687
1688 jobs = self.fake_jenkins.job_history
1689 assert len(jobs) == 0
1690 self.assertEmptyQueues()
James E. Blair2fa50962013-01-30 21:50:41 -08001691
1692 def test_new_patchset_dequeues_old(self):
1693 "Test that a new patchset causes the old to be dequeued"
1694 # D -> C (depends on B) -> B (depends on A) -> A -> M
1695 self.fake_jenkins.hold_jobs_in_build = True
1696
1697 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1698 M.setMerged()
1699
1700 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1701 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1702 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1703 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1704 A.addApproval('CRVW', 2)
1705 B.addApproval('CRVW', 2)
1706 C.addApproval('CRVW', 2)
1707 D.addApproval('CRVW', 2)
1708
1709 C.setDependsOn(B, 1)
1710 B.setDependsOn(A, 1)
1711 A.setDependsOn(M, 1)
1712
1713 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1714 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1715 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1716 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1717 self.waitUntilSettled()
1718
1719 B.addPatchset()
1720 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1721 self.waitUntilSettled()
1722
1723 self.fake_jenkins.hold_jobs_in_build = False
1724 self.fake_jenkins.fakeRelease()
1725 self.waitUntilSettled()
1726
1727 jobs = self.fake_jenkins.all_jobs
1728 finished_jobs = self.fake_jenkins.job_history
1729
1730 for x in jobs:
1731 print x
1732 for x in finished_jobs:
1733 print x
1734
1735 assert A.data['status'] == 'MERGED'
1736 assert A.reported == 2
1737 assert B.data['status'] == 'NEW'
1738 assert B.reported == 2
1739 assert C.data['status'] == 'NEW'
1740 assert C.reported == 2
1741 assert D.data['status'] == 'MERGED'
1742 assert D.reported == 2
1743 assert len(finished_jobs) == 9 # 3 each for A, B, D.
1744 self.assertEmptyQueues()
1745
1746 def test_new_patchset_dequeues_old_on_head(self):
1747 "Test that a new patchset causes the old to be dequeued (at head)"
1748 # D -> C (depends on B) -> B (depends on A) -> A -> M
1749 self.fake_jenkins.hold_jobs_in_build = True
1750
1751 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1752 M.setMerged()
1753 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1754 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1755 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1756 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1757 A.addApproval('CRVW', 2)
1758 B.addApproval('CRVW', 2)
1759 C.addApproval('CRVW', 2)
1760 D.addApproval('CRVW', 2)
1761
1762 C.setDependsOn(B, 1)
1763 B.setDependsOn(A, 1)
1764 A.setDependsOn(M, 1)
1765
1766 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1767 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1768 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1769 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1770 self.waitUntilSettled()
1771
1772 A.addPatchset()
1773 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1774 self.waitUntilSettled()
1775
1776 self.fake_jenkins.hold_jobs_in_build = False
1777 self.fake_jenkins.fakeRelease()
1778 self.waitUntilSettled()
1779
1780 jobs = self.fake_jenkins.all_jobs
1781 finished_jobs = self.fake_jenkins.job_history
1782
1783 for x in jobs:
1784 print x
1785 for x in finished_jobs:
1786 print x
1787
1788 assert A.data['status'] == 'NEW'
1789 assert A.reported == 2
1790 assert B.data['status'] == 'NEW'
1791 assert B.reported == 2
1792 assert C.data['status'] == 'NEW'
1793 assert C.reported == 2
1794 assert D.data['status'] == 'MERGED'
1795 assert D.reported == 2
1796 assert len(finished_jobs) == 7
1797 self.assertEmptyQueues()
1798
1799 def test_new_patchset_dequeues_old_without_dependents(self):
1800 "Test that a new patchset causes only the old to be dequeued"
1801 self.fake_jenkins.hold_jobs_in_build = True
1802
1803 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1804 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1805 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1806 A.addApproval('CRVW', 2)
1807 B.addApproval('CRVW', 2)
1808 C.addApproval('CRVW', 2)
1809
1810 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1811 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1812 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1813 self.waitUntilSettled()
1814
1815 B.addPatchset()
1816 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1817 self.waitUntilSettled()
1818
1819 self.fake_jenkins.hold_jobs_in_build = False
1820 self.fake_jenkins.fakeRelease()
1821 self.waitUntilSettled()
1822
1823 jobs = self.fake_jenkins.all_jobs
1824 finished_jobs = self.fake_jenkins.job_history
1825
1826 for x in jobs:
1827 print x
1828 for x in finished_jobs:
1829 print x
1830
1831 assert A.data['status'] == 'MERGED'
1832 assert A.reported == 2
1833 assert B.data['status'] == 'NEW'
1834 assert B.reported == 2
1835 assert C.data['status'] == 'MERGED'
1836 assert C.reported == 2
1837 assert len(finished_jobs) == 9
1838 self.assertEmptyQueues()
1839
1840 def test_new_patchset_dequeues_old_independent_queue(self):
1841 "Test that a new patchset causes the old to be dequeued (independent)"
1842 self.fake_jenkins.hold_jobs_in_build = True
1843
1844 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1845 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1846 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1847 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1848 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1849 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1850 self.waitUntilSettled()
1851
1852 B.addPatchset()
1853 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1854 self.waitUntilSettled()
1855
1856 self.fake_jenkins.hold_jobs_in_build = False
1857 self.fake_jenkins.fakeRelease()
1858 self.waitUntilSettled()
1859
1860 jobs = self.fake_jenkins.all_jobs
1861 finished_jobs = self.fake_jenkins.job_history
1862
1863 for x in jobs:
1864 print x
1865 for x in finished_jobs:
1866 print x
1867
1868 assert A.data['status'] == 'NEW'
1869 assert A.reported == 1
1870 assert B.data['status'] == 'NEW'
1871 assert B.reported == 1
1872 assert C.data['status'] == 'NEW'
1873 assert C.reported == 1
1874 assert len(finished_jobs) == 10
1875 assert self.countJobResults(finished_jobs, 'ABORTED') == 1
1876 self.assertEmptyQueues()
James E. Blair7d0dedc2013-02-21 17:26:09 -08001877
1878 def test_zuul_refs(self):
1879 "Test that zuul refs exist and have the right changes"
1880 self.fake_jenkins.hold_jobs_in_build = True
1881
1882 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1883 M1.setMerged()
1884 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1885 M2.setMerged()
1886
1887 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1888 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1889 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1890 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1891 A.addApproval('CRVW', 2)
1892 B.addApproval('CRVW', 2)
1893 C.addApproval('CRVW', 2)
1894 D.addApproval('CRVW', 2)
1895 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1896 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1897 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1898 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1899
1900 self.waitUntilSettled()
1901 self.fake_jenkins.fakeRelease('.*-merge')
1902 self.waitUntilSettled()
1903 self.fake_jenkins.fakeRelease('.*-merge')
1904 self.waitUntilSettled()
1905 self.fake_jenkins.fakeRelease('.*-merge')
1906 self.waitUntilSettled()
1907 self.fake_jenkins.fakeRelease('.*-merge')
1908 self.waitUntilSettled()
1909
1910 jobs = self.fake_jenkins.all_jobs
1911 finished_jobs = self.fake_jenkins.job_history
1912
1913 a_zref = b_zref = c_zref = d_zref = None
1914 for x in jobs:
1915 if x.parameters['ZUUL_CHANGE'] == '3':
1916 a_zref = x.parameters['ZUUL_REF']
1917 if x.parameters['ZUUL_CHANGE'] == '4':
1918 b_zref = x.parameters['ZUUL_REF']
1919 if x.parameters['ZUUL_CHANGE'] == '5':
1920 c_zref = x.parameters['ZUUL_REF']
1921 if x.parameters['ZUUL_CHANGE'] == '6':
1922 d_zref = x.parameters['ZUUL_REF']
1923
1924 # There are... four... refs.
1925 assert a_zref is not None
1926 assert b_zref is not None
1927 assert c_zref is not None
1928 assert d_zref is not None
1929
1930 # And they should all be different
1931 refs = set([a_zref, b_zref, c_zref, d_zref])
1932 assert len(refs) == 4
1933
1934 # a ref should have a, not b, and should not be in project2
1935 assert ref_has_change(a_zref, A)
1936 assert not ref_has_change(a_zref, B)
1937 assert not ref_has_change(a_zref, M2)
1938
1939 # b ref should have a and b, and should not be in project2
1940 assert ref_has_change(b_zref, A)
1941 assert ref_has_change(b_zref, B)
1942 assert not ref_has_change(b_zref, M2)
1943
1944 # c ref should have a and b in 1, c in 2
1945 assert ref_has_change(c_zref, A)
1946 assert ref_has_change(c_zref, B)
1947 assert ref_has_change(c_zref, C)
1948 assert not ref_has_change(c_zref, D)
1949
1950 # d ref should have a and b in 1, c and d in 2
1951 assert ref_has_change(d_zref, A)
1952 assert ref_has_change(d_zref, B)
1953 assert ref_has_change(d_zref, C)
1954 assert ref_has_change(d_zref, D)
1955
1956 self.fake_jenkins.hold_jobs_in_build = False
1957 self.fake_jenkins.fakeRelease()
1958 self.waitUntilSettled()
1959
1960 assert A.data['status'] == 'MERGED'
1961 assert A.reported == 2
1962 assert B.data['status'] == 'MERGED'
1963 assert B.reported == 2
1964 assert C.data['status'] == 'MERGED'
1965 assert C.reported == 2
1966 assert D.data['status'] == 'MERGED'
1967 assert D.reported == 2
1968 self.assertEmptyQueues()