blob: cacf4a91e8857f05335e79ee9de20d43748085bb [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
32import git
James E. Blairb0fcae42012-07-17 11:12:10 -070033
34import zuul
35import zuul.scheduler
36import zuul.launcher.jenkins
37import zuul.trigger.gerrit
38
39FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
40 'fixtures')
41CONFIG = ConfigParser.ConfigParser()
42CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
43
44CONFIG.set('zuul', 'layout_config',
45 os.path.join(FIXTURE_DIR, "layout.yaml"))
46
James E. Blair1dbd5082012-08-23 15:12:15 -070047TMP_ROOT = os.environ.get("ZUUL_TEST_ROOT", "/tmp")
48TEST_ROOT = os.path.join(TMP_ROOT, "zuul-test")
49UPSTREAM_ROOT = os.path.join(TEST_ROOT, "upstream")
50GIT_ROOT = os.path.join(TEST_ROOT, "git")
51
52CONFIG.set('zuul', 'git_dir', GIT_ROOT)
53
James E. Blairb0fcae42012-07-17 11:12:10 -070054logging.basicConfig(level=logging.DEBUG)
55
56
James E. Blair8cc15a82012-08-01 11:17:57 -070057def random_sha1():
58 return hashlib.sha1(str(random.random())).hexdigest()
59
60
James E. Blair4886cc12012-07-18 15:39:41 -070061class ChangeReference(git.Reference):
62 _common_path_default = "refs/changes"
63 _points_to_commits_only = True
64
65
66def init_repo(project):
67 parts = project.split('/')
James E. Blair1dbd5082012-08-23 15:12:15 -070068 path = os.path.join(UPSTREAM_ROOT, *parts[:-1])
James E. Blair4886cc12012-07-18 15:39:41 -070069 if not os.path.exists(path):
70 os.makedirs(path)
James E. Blair1dbd5082012-08-23 15:12:15 -070071 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -070072 repo = git.Repo.init(path)
73
74 fn = os.path.join(path, 'README')
75 f = open(fn, 'w')
76 f.write("test\n")
77 f.close()
78 repo.index.add([fn])
79 repo.index.commit('initial commit')
James E. Blairc6294a52012-08-17 10:19:48 -070080 master = repo.create_head('master')
James E. Blair4886cc12012-07-18 15:39:41 -070081 repo.create_tag('init')
82
James E. Blairc6294a52012-08-17 10:19:48 -070083 mp = repo.create_head('mp')
84 repo.head.reference = mp
85 f = open(fn, 'a')
86 f.write("test mp\n")
87 f.close()
88 repo.index.add([fn])
89 repo.index.commit('mp commit')
90
91 repo.head.reference = master
92 repo.head.reset(index=True, working_tree=True)
93 repo.git.clean('-x', '-f', '-d')
94
James E. Blair4886cc12012-07-18 15:39:41 -070095
James E. Blair973721f2012-08-15 10:19:43 -070096def add_fake_change_to_repo(project, branch, change_num, patchset, msg, fn):
James E. Blair1dbd5082012-08-23 15:12:15 -070097 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -070098 repo = git.Repo(path)
99 ref = ChangeReference.create(repo, '1/%s/%s' % (change_num,
100 patchset),
101 'refs/tags/init')
102 repo.head.reference = ref
103 repo.head.reset(index=True, working_tree=True)
104 repo.git.clean('-x', '-f', '-d')
105
James E. Blair1dbd5082012-08-23 15:12:15 -0700106 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair973721f2012-08-15 10:19:43 -0700107 fn = os.path.join(path, fn)
James E. Blair4886cc12012-07-18 15:39:41 -0700108 f = open(fn, 'w')
James E. Blair973721f2012-08-15 10:19:43 -0700109 f.write("test %s %s %s\n" % (branch, change_num, patchset))
James E. Blair4886cc12012-07-18 15:39:41 -0700110 f.close()
111 repo.index.add([fn])
James E. Blairdaabed22012-08-15 15:38:57 -0700112 return repo.index.commit(msg)
James E. Blair4886cc12012-07-18 15:39:41 -0700113
114
115def ref_has_change(ref, change):
James E. Blair1dbd5082012-08-23 15:12:15 -0700116 path = os.path.join(GIT_ROOT, change.project)
James E. Blair4886cc12012-07-18 15:39:41 -0700117 repo = git.Repo(path)
118 for commit in repo.iter_commits(ref):
119 if commit.message.strip() == ('%s-1' % change.subject):
120 return True
121 return False
122
123
124def job_has_changes(*args):
125 job = args[0]
126 commits = args[1:]
127 project = job.parameters['ZUUL_PROJECT']
James E. Blair1dbd5082012-08-23 15:12:15 -0700128 path = os.path.join(GIT_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -0700129 repo = git.Repo(path)
130 ref = job.parameters['ZUUL_REF']
131 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
132 commit_messages = ['%s-1' % commit.subject for commit in commits]
James E. Blair4886cc12012-07-18 15:39:41 -0700133 for msg in commit_messages:
134 if msg not in repo_messages:
135 return False
136 return True
137
138
James E. Blairb0fcae42012-07-17 11:12:10 -0700139class FakeChange(object):
James E. Blair8c803f82012-07-31 16:25:42 -0700140 categories = {'APRV': ('Approved', -1, 1),
141 'CRVW': ('Code-Review', -2, 2),
142 'VRFY': ('Verified', -2, 2)}
James E. Blairb0fcae42012-07-17 11:12:10 -0700143
James E. Blair8cc15a82012-08-01 11:17:57 -0700144 def __init__(self, gerrit, number, project, branch, subject, status='NEW'):
145 self.gerrit = gerrit
James E. Blaird466dc42012-07-31 10:42:56 -0700146 self.reported = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700147 self.queried = 0
James E. Blairb0fcae42012-07-17 11:12:10 -0700148 self.patchsets = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700149 self.number = number
150 self.project = project
151 self.branch = branch
152 self.subject = subject
153 self.latest_patchset = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700154 self.depends_on_change = None
155 self.needed_by_changes = []
James E. Blair127bc182012-08-28 15:55:15 -0700156 self.fail_merge = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700157 self.data = {
158 'branch': branch,
159 'comments': [],
160 'commitMessage': subject,
161 'createdOn': time.time(),
James E. Blair8cc15a82012-08-01 11:17:57 -0700162 'id': 'I' + random_sha1(),
James E. Blairb0fcae42012-07-17 11:12:10 -0700163 'lastUpdated': time.time(),
164 'number': str(number),
165 'open': True,
166 'owner': {'email': 'user@example.com',
167 'name': 'User Name',
168 'username': 'username'},
169 'patchSets': self.patchsets,
170 'project': project,
171 'status': status,
172 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700173 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700174 'url': 'https://hostname/%s' % number}
175
176 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700177 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700178
179 def addPatchset(self, files=None):
180 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700181 if files:
182 fn = files[0]
183 else:
184 fn = '%s-%s' % (self.branch, self.number)
185 msg = self.subject + '-' + str(self.latest_patchset)
186 c = add_fake_change_to_repo(self.project, self.branch,
187 self.number, self.latest_patchset,
188 msg, fn)
James E. Blairb0fcae42012-07-17 11:12:10 -0700189 d = {'approvals': [],
190 'createdOn': time.time(),
191 'files': [{'file': '/COMMIT_MSG',
192 'type': 'ADDED'},
193 {'file': 'README',
194 'type': 'MODIFIED'}],
James E. Blair8c803f82012-07-31 16:25:42 -0700195 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700196 'ref': 'refs/changes/1/%s/%s' % (self.number,
197 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700198 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700199 'uploader': {'email': 'user@example.com',
200 'name': 'User name',
201 'username': 'user'}}
202 self.data['currentPatchSet'] = d
203 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700204 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700205
James E. Blaire0487072012-08-29 17:38:31 -0700206 def getPatchsetCreatedEvent(self, patchset):
207 event = {"type": "patchset-created",
208 "change": {"project": self.project,
209 "branch": self.branch,
210 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
211 "number": str(self.number),
212 "subject": self.subject,
213 "owner": {"name": "User Name"},
214 "url": "https://hostname/3"},
215 "patchSet": self.patchsets[patchset - 1],
216 "uploader": {"name": "User Name"}}
217 return event
218
James E. Blairb0fcae42012-07-17 11:12:10 -0700219 def addApproval(self, category, value):
James E. Blair8c803f82012-07-31 16:25:42 -0700220 approval = {'description': self.categories[category][0],
221 'type': category,
222 'value': str(value)}
223 self.patchsets[-1]['approvals'].append(approval)
224 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700225 'author': {'email': 'user@example.com',
226 'name': 'User Name',
227 'username': 'username'},
228 'change': {'branch': self.branch,
229 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
230 'number': str(self.number),
231 'owner': {'email': 'user@example.com',
232 'name': 'User Name',
233 'username': 'username'},
234 'project': self.project,
235 'subject': self.subject,
236 'topic': 'master',
237 'url': 'https://hostname/459'},
238 'comment': '',
239 'patchSet': self.patchsets[-1],
240 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700241 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700242 return json.loads(json.dumps(event))
243
James E. Blair8c803f82012-07-31 16:25:42 -0700244 def getSubmitRecords(self):
245 status = {}
246 for cat in self.categories.keys():
247 status[cat] = 0
248
249 for a in self.patchsets[-1]['approvals']:
250 cur = status[a['type']]
251 cat_min, cat_max = self.categories[a['type']][1:]
252 new = int(a['value'])
253 if new == cat_min:
254 cur = new
255 elif abs(new) > abs(cur):
256 cur = new
257 status[a['type']] = cur
258
259 labels = []
260 ok = True
261 for typ, cat in self.categories.items():
262 cur = status[typ]
263 cat_min, cat_max = cat[1:]
264 if cur == cat_min:
265 value = 'REJECT'
266 ok = False
267 elif cur == cat_max:
268 value = 'OK'
269 else:
270 value = 'NEED'
271 ok = False
272 labels.append({'label': cat[0], 'status': value})
273 if ok:
274 return [{'status': 'OK'}]
275 return [{'status': 'NOT_READY',
276 'labels': labels}]
277
278 def setDependsOn(self, other, patchset):
279 self.depends_on_change = other
280 d = {'id': other.data['id'],
281 'number': other.data['number'],
282 'ref': other.patchsets[patchset - 1]['ref']
283 }
284 self.data['dependsOn'] = [d]
285
286 other.needed_by_changes.append(self)
287 needed = other.data.get('neededBy', [])
288 d = {'id': self.data['id'],
289 'number': self.data['number'],
290 'ref': self.patchsets[patchset - 1]['ref'],
291 'revision': self.patchsets[patchset - 1]['revision']
292 }
293 needed.append(d)
294 other.data['neededBy'] = needed
295
James E. Blairb0fcae42012-07-17 11:12:10 -0700296 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700297 self.queried += 1
298 d = self.data.get('dependsOn')
299 if d:
300 d = d[0]
301 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
302 d['isCurrentPatchSet'] = True
303 else:
304 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700305 return json.loads(json.dumps(self.data))
306
307 def setMerged(self):
James E. Blaircaec0c52012-08-22 14:52:22 -0700308 if (self.depends_on_change
309 and self.depends_on_change.data['status'] != 'MERGED'):
310 return
James E. Blair127bc182012-08-28 15:55:15 -0700311 if self.fail_merge:
312 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700313 self.data['status'] = 'MERGED'
314 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700315
James E. Blair1dbd5082012-08-23 15:12:15 -0700316 path = os.path.join(UPSTREAM_ROOT, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700317 repo = git.Repo(path)
318 repo.heads[self.branch].commit = \
319 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700320
James E. Blaird466dc42012-07-31 10:42:56 -0700321 def setReported(self):
322 self.reported += 1
323
James E. Blairb0fcae42012-07-17 11:12:10 -0700324
325class FakeGerrit(object):
326 def __init__(self, *args, **kw):
327 self.event_queue = Queue.Queue()
328 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
329 self.change_number = 0
330 self.changes = {}
331
332 def addFakeChange(self, project, branch, subject):
333 self.change_number += 1
James E. Blair8cc15a82012-08-01 11:17:57 -0700334 c = FakeChange(self, self.change_number, project, branch, subject)
James E. Blairb0fcae42012-07-17 11:12:10 -0700335 self.changes[self.change_number] = c
336 return c
337
338 def addEvent(self, data):
339 return self.event_queue.put(data)
340
341 def getEvent(self):
342 return self.event_queue.get()
343
344 def eventDone(self):
345 self.event_queue.task_done()
346
347 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700348 number, ps = changeid.split(',')
349 change = self.changes[int(number)]
James E. Blairb0fcae42012-07-17 11:12:10 -0700350 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700351 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700352 if message:
353 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700354
355 def query(self, number):
356 change = self.changes[int(number)]
357 return change.query()
358
359 def startWatching(self, *args, **kw):
360 pass
361
362
363class FakeJenkinsEvent(object):
364 def __init__(self, name, number, parameters, phase, status=None):
Jeremy Stanleyb8ff8a72012-09-25 16:10:02 +0000365 data = {'build':
366 {'full_url': 'https://server/job/%s/%s/' % (name, number),
367 'number': number,
368 'parameters': parameters,
369 'phase': phase,
370 'url': 'job/%s/%s/' % (name, number)},
371 'name': name,
372 'url': 'job/%s/' % name}
James E. Blairb0fcae42012-07-17 11:12:10 -0700373 if status:
374 data['build']['status'] = status
375 self.body = json.dumps(data)
376
377
378class FakeJenkinsJob(threading.Thread):
379 log = logging.getLogger("zuul.test")
380
381 def __init__(self, jenkins, callback, name, number, parameters):
382 threading.Thread.__init__(self)
383 self.jenkins = jenkins
384 self.callback = callback
385 self.name = name
386 self.number = number
387 self.parameters = parameters
388 self.wait_condition = threading.Condition()
389 self.waiting = False
James E. Blaird466dc42012-07-31 10:42:56 -0700390 self.aborted = False
391 self.canceled = False
392 self.created = time.time()
James E. Blairb0fcae42012-07-17 11:12:10 -0700393
394 def release(self):
395 self.wait_condition.acquire()
396 self.wait_condition.notify()
397 self.waiting = False
398 self.log.debug("Job %s released" % (self.parameters['UUID']))
399 self.wait_condition.release()
400
401 def isWaiting(self):
402 self.wait_condition.acquire()
403 if self.waiting:
404 ret = True
405 else:
406 ret = False
407 self.wait_condition.release()
408 return ret
409
410 def _wait(self):
411 self.wait_condition.acquire()
412 self.waiting = True
413 self.log.debug("Job %s waiting" % (self.parameters['UUID']))
414 self.wait_condition.wait()
415 self.wait_condition.release()
416
417 def run(self):
418 self.jenkins.fakeEnqueue(self)
419 if self.jenkins.hold_jobs_in_queue:
420 self._wait()
421 self.jenkins.fakeDequeue(self)
James E. Blaird466dc42012-07-31 10:42:56 -0700422 if self.canceled:
423 self.jenkins.all_jobs.remove(self)
424 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700425 self.callback.jenkins_endpoint(FakeJenkinsEvent(
426 self.name, self.number, self.parameters,
427 'STARTED'))
428 if self.jenkins.hold_jobs_in_build:
429 self._wait()
430 self.log.debug("Job %s continuing" % (self.parameters['UUID']))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700431
432 result = 'SUCCESS'
James E. Blairdaabed22012-08-15 15:38:57 -0700433 if ('ZUUL_REF' in self.parameters) and self.jenkins.fakeShouldFailTest(
James E. Blairb02a3bb2012-07-30 17:49:55 -0700434 self.name,
James E. Blair4886cc12012-07-18 15:39:41 -0700435 self.parameters['ZUUL_REF']):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700436 result = 'FAILURE'
James E. Blaird466dc42012-07-31 10:42:56 -0700437 if self.aborted:
438 result = 'ABORTED'
James E. Blairb02a3bb2012-07-30 17:49:55 -0700439
James E. Blairb0fcae42012-07-17 11:12:10 -0700440 self.jenkins.fakeAddHistory(name=self.name, number=self.number,
James E. Blairb02a3bb2012-07-30 17:49:55 -0700441 result=result)
James E. Blairb0fcae42012-07-17 11:12:10 -0700442 self.callback.jenkins_endpoint(FakeJenkinsEvent(
443 self.name, self.number, self.parameters,
James E. Blairb02a3bb2012-07-30 17:49:55 -0700444 'COMPLETED', result))
James E. Blairb0fcae42012-07-17 11:12:10 -0700445 self.callback.jenkins_endpoint(FakeJenkinsEvent(
446 self.name, self.number, self.parameters,
James E. Blairb02a3bb2012-07-30 17:49:55 -0700447 'FINISHED', result))
James E. Blairb0fcae42012-07-17 11:12:10 -0700448 self.jenkins.all_jobs.remove(self)
449
450
451class FakeJenkins(object):
452 log = logging.getLogger("zuul.test")
453
454 def __init__(self, *args, **kw):
455 self.queue = []
456 self.all_jobs = []
457 self.job_counter = {}
James E. Blaird466dc42012-07-31 10:42:56 -0700458 self.queue_counter = 0
James E. Blairb0fcae42012-07-17 11:12:10 -0700459 self.job_history = []
460 self.hold_jobs_in_queue = False
461 self.hold_jobs_in_build = False
James E. Blairb02a3bb2012-07-30 17:49:55 -0700462 self.fail_tests = {}
James E. Blair7ee88a22012-09-12 18:59:31 +0200463 self.nonexistent_jobs = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700464
465 def fakeEnqueue(self, job):
466 self.queue.append(job)
467
468 def fakeDequeue(self, job):
469 self.queue.remove(job)
470
471 def fakeAddHistory(self, **kw):
472 self.job_history.append(kw)
473
474 def fakeRelease(self, regex=None):
475 all_jobs = self.all_jobs[:]
476 self.log.debug("releasing jobs %s (%s)" % (regex, len(self.all_jobs)))
477 for job in all_jobs:
478 if not regex or re.match(regex, job.name):
479 self.log.debug("releasing job %s" % (job.parameters['UUID']))
480 job.release()
481 else:
482 self.log.debug("not releasing job %s" % (
483 job.parameters['UUID']))
484 self.log.debug("done releasing jobs %s (%s)" % (regex,
485 len(self.all_jobs)))
486
487 def fakeAllWaiting(self, regex=None):
488 all_jobs = self.all_jobs[:]
489 for job in all_jobs:
490 self.log.debug("job %s %s" % (job.parameters['UUID'],
491 job.isWaiting()))
492 if not job.isWaiting():
493 return False
494 return True
495
James E. Blairb02a3bb2012-07-30 17:49:55 -0700496 def fakeAddFailTest(self, name, change):
497 l = self.fail_tests.get(name, [])
498 l.append(change)
499 self.fail_tests[name] = l
500
James E. Blair4886cc12012-07-18 15:39:41 -0700501 def fakeShouldFailTest(self, name, ref):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700502 l = self.fail_tests.get(name, [])
503 for change in l:
James E. Blair4886cc12012-07-18 15:39:41 -0700504 if ref_has_change(ref, change):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700505 return True
506 return False
507
James E. Blairb0fcae42012-07-17 11:12:10 -0700508 def build_job(self, name, parameters):
James E. Blair7ee88a22012-09-12 18:59:31 +0200509 if name in self.nonexistent_jobs:
510 raise Exception("Job does not exist")
James E. Blairb0fcae42012-07-17 11:12:10 -0700511 count = self.job_counter.get(name, 0)
512 count += 1
513 self.job_counter[name] = count
James E. Blaird466dc42012-07-31 10:42:56 -0700514
515 queue_count = self.queue_counter
516 self.queue_counter += 1
James E. Blairb0fcae42012-07-17 11:12:10 -0700517 job = FakeJenkinsJob(self, self.callback, name, count, parameters)
James E. Blaird466dc42012-07-31 10:42:56 -0700518 job.queue_id = queue_count
519
James E. Blairb0fcae42012-07-17 11:12:10 -0700520 self.all_jobs.append(job)
521 job.start()
522
James E. Blaird466dc42012-07-31 10:42:56 -0700523 def stop_build(self, name, number):
524 for job in self.all_jobs:
525 if job.name == name and job.number == number:
526 job.aborted = True
527 job.release()
528 return
529
530 def cancel_queue(self, id):
531 for job in self.queue:
532 if job.queue_id == id:
533 job.canceled = True
534 job.release()
535 return
536
537 def get_queue_info(self):
538 items = []
539 for job in self.queue:
540 paramstr = ''
541 paramlst = []
542 d = {'actions': [{'parameters': paramlst},
543 {'causes': [{'shortDescription':
544 'Started by user Jenkins',
545 'userId': 'jenkins',
546 'userName': 'Jenkins'}]}],
547 'blocked': False,
548 'buildable': True,
549 'buildableStartMilliseconds': (job.created * 1000) + 5,
550 'id': job.queue_id,
551 'inQueueSince': (job.created * 1000),
552 'params': paramstr,
553 'stuck': False,
554 'task': {'color': 'blue',
555 'name': job.name,
556 'url': 'https://server/job/%s/' % job.name},
557 'why': 'Waiting for next available executor'}
558 for k, v in job.parameters.items():
559 paramstr += "\n(StringParameterValue) %s='%s'" % (k, v)
560 pd = {'name': k, 'value': v}
561 paramlst.append(pd)
562 items.append(d)
563 return items
564
James E. Blairb0fcae42012-07-17 11:12:10 -0700565 def set_build_description(self, *args, **kw):
566 pass
567
568
569class FakeJenkinsCallback(zuul.launcher.jenkins.JenkinsCallback):
570 def start(self):
571 pass
572
573
James E. Blair8cc15a82012-08-01 11:17:57 -0700574class FakeURLOpener(object):
575 def __init__(self, fake_gerrit, url):
576 self.fake_gerrit = fake_gerrit
577 self.url = url
578
579 def read(self):
580 res = urlparse.urlparse(self.url)
581 path = res.path
582 project = '/'.join(path.split('/')[2:-2])
James E. Blair8cc15a82012-08-01 11:17:57 -0700583 ret = ''
James E. Blair1dbd5082012-08-23 15:12:15 -0700584 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700585 repo = git.Repo(path)
586 for ref in repo.refs:
587 ret += ref.object.hexsha + '\t' + ref.path + '\n'
James E. Blair8cc15a82012-08-01 11:17:57 -0700588 return ret
589
590
James E. Blair4886cc12012-07-18 15:39:41 -0700591class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
592 def getGitUrl(self, project):
James E. Blair1dbd5082012-08-23 15:12:15 -0700593 return os.path.join(UPSTREAM_ROOT, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700594
595
James E. Blairb0fcae42012-07-17 11:12:10 -0700596class testScheduler(unittest.TestCase):
597 log = logging.getLogger("zuul.test")
598
599 def setUp(self):
James E. Blair1dbd5082012-08-23 15:12:15 -0700600 if os.path.exists(TEST_ROOT):
601 shutil.rmtree(TEST_ROOT)
602 os.makedirs(TEST_ROOT)
603 os.makedirs(UPSTREAM_ROOT)
604 os.makedirs(GIT_ROOT)
James E. Blair4886cc12012-07-18 15:39:41 -0700605
606 # For each project in config:
607 init_repo("org/project")
608 init_repo("org/project1")
609 init_repo("org/project2")
James E. Blair127bc182012-08-28 15:55:15 -0700610 init_repo("org/project3")
James E. Blair7f71c802012-08-22 13:04:32 -0700611 init_repo("org/one-job-project")
James E. Blair4ec821f2012-08-23 15:28:28 -0700612 init_repo("org/nonvoting-project")
James E. Blairb0fcae42012-07-17 11:12:10 -0700613 self.config = CONFIG
614 self.sched = zuul.scheduler.Scheduler()
615
616 def jenkinsFactory(*args, **kw):
617 self.fake_jenkins = FakeJenkins()
618 return self.fake_jenkins
619
620 def jenkinsCallbackFactory(*args, **kw):
621 self.fake_jenkins_callback = FakeJenkinsCallback(*args, **kw)
622 return self.fake_jenkins_callback
623
James E. Blair8cc15a82012-08-01 11:17:57 -0700624 def URLOpenerFactory(*args, **kw):
625 args = [self.fake_gerrit] + list(args)
626 return FakeURLOpener(*args, **kw)
627
James E. Blairb0fcae42012-07-17 11:12:10 -0700628 zuul.launcher.jenkins.ExtendedJenkins = jenkinsFactory
629 zuul.launcher.jenkins.JenkinsCallback = jenkinsCallbackFactory
James E. Blair8cc15a82012-08-01 11:17:57 -0700630 urllib2.urlopen = URLOpenerFactory
James E. Blairb0fcae42012-07-17 11:12:10 -0700631 self.jenkins = zuul.launcher.jenkins.Jenkins(self.config, self.sched)
632 self.fake_jenkins.callback = self.fake_jenkins_callback
633
634 zuul.lib.gerrit.Gerrit = FakeGerrit
635
James E. Blair4886cc12012-07-18 15:39:41 -0700636 self.gerrit = FakeGerritTrigger(self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700637 self.gerrit.replication_timeout = 1.5
638 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700639 self.fake_gerrit = self.gerrit.gerrit
640
641 self.sched.setLauncher(self.jenkins)
642 self.sched.setTrigger(self.gerrit)
643
644 self.sched.start()
645 self.sched.reconfigure(self.config)
646 self.sched.resume()
647
648 def tearDown(self):
649 self.jenkins.stop()
650 self.gerrit.stop()
651 self.sched.stop()
652 self.sched.join()
James E. Blair1dbd5082012-08-23 15:12:15 -0700653 #shutil.rmtree(TEST_ROOT)
James E. Blairb0fcae42012-07-17 11:12:10 -0700654
655 def waitUntilSettled(self):
656 self.log.debug("Waiting until settled...")
657 start = time.time()
658 while True:
659 if time.time() - start > 10:
660 print 'queue status:',
661 print self.sched.trigger_event_queue.empty(),
662 print self.sched.result_event_queue.empty(),
663 print self.fake_gerrit.event_queue.empty(),
664 raise Exception("Timeout waiting for Zuul to settle")
665 self.fake_gerrit.event_queue.join()
666 self.sched.queue_lock.acquire()
667 if (self.sched.trigger_event_queue.empty() and
668 self.sched.result_event_queue.empty() and
669 self.fake_gerrit.event_queue.empty() and
670 self.fake_jenkins.fakeAllWaiting()):
671 self.sched.queue_lock.release()
672 self.log.debug("...settled.")
673 return
674 self.sched.queue_lock.release()
675 self.sched.wake_event.wait(0.1)
676
James E. Blaird466dc42012-07-31 10:42:56 -0700677 def countJobResults(self, jobs, result):
678 jobs = filter(lambda x: x['result'] == result, jobs)
679 return len(jobs)
680
James E. Blaire0487072012-08-29 17:38:31 -0700681 def assertEmptyQueues(self):
682 # Make sure there are no orphaned jobs
683 for pipeline in self.sched.pipelines.values():
684 for queue in pipeline.queues:
685 if len(queue.queue) != 0:
686 print 'queue', queue.queue
687 assert len(queue.queue) == 0
688 if len(queue.severed_heads) != 0:
689 print 'heads', queue.severed_heads
690 assert len(queue.severed_heads) == 0
691
James E. Blairb0fcae42012-07-17 11:12:10 -0700692 def test_jobs_launched(self):
693 "Test that jobs are launched and a change is merged"
694 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -0700695 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700696 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
697 self.waitUntilSettled()
698 jobs = self.fake_jenkins.job_history
699 job_names = [x['name'] for x in jobs]
700 assert 'project-merge' in job_names
701 assert 'project-test1' in job_names
702 assert 'project-test2' in job_names
703 assert jobs[0]['result'] == 'SUCCESS'
704 assert jobs[1]['result'] == 'SUCCESS'
705 assert jobs[2]['result'] == 'SUCCESS'
706 assert A.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700707 assert A.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700708 self.assertEmptyQueues()
James E. Blairb0fcae42012-07-17 11:12:10 -0700709
710 def test_parallel_changes(self):
711 "Test that changes are tested in parallel and merged in series"
712 self.fake_jenkins.hold_jobs_in_build = True
713 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
714 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
715 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700716 A.addApproval('CRVW', 2)
717 B.addApproval('CRVW', 2)
718 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700719
720 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
721 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
722 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
723
724 self.waitUntilSettled()
725 jobs = self.fake_jenkins.all_jobs
726 assert len(jobs) == 1
727 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700728 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700729
730 self.fake_jenkins.fakeRelease('.*-merge')
731 self.waitUntilSettled()
732 assert len(jobs) == 3
733 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700734 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700735 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700736 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700737 assert jobs[2].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700738 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700739
740 self.fake_jenkins.fakeRelease('.*-merge')
741 self.waitUntilSettled()
742 assert len(jobs) == 5
743 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700744 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700745 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700746 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700747
748 assert jobs[2].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700749 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700750 assert jobs[3].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700751 assert job_has_changes(jobs[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700752
753 assert jobs[4].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700754 assert job_has_changes(jobs[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700755
756 self.fake_jenkins.fakeRelease('.*-merge')
757 self.waitUntilSettled()
758 assert len(jobs) == 6
759 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700760 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700761 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700762 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700763
764 assert jobs[2].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700765 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700766 assert jobs[3].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700767 assert job_has_changes(jobs[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700768
769 assert jobs[4].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700770 assert job_has_changes(jobs[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700771 assert jobs[5].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700772 assert job_has_changes(jobs[5], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700773
774 self.fake_jenkins.hold_jobs_in_build = False
775 self.fake_jenkins.fakeRelease()
776 self.waitUntilSettled()
777 assert len(jobs) == 0
778
779 jobs = self.fake_jenkins.job_history
780 assert len(jobs) == 9
781 assert A.data['status'] == 'MERGED'
782 assert B.data['status'] == 'MERGED'
783 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700784 assert A.reported == 2
785 assert B.reported == 2
786 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700787 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700788
789 def test_failed_changes(self):
790 "Test that a change behind a failed change is retested"
791 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
792 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -0700793 A.addApproval('CRVW', 2)
794 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700795
796 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
797 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
798
James E. Blair4886cc12012-07-18 15:39:41 -0700799 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700800
801 self.waitUntilSettled()
802 jobs = self.fake_jenkins.job_history
803 assert len(jobs) > 6
804 assert A.data['status'] == 'NEW'
805 assert B.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700806 assert A.reported == 2
807 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700808 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700809
810 def test_independent_queues(self):
811 "Test that changes end up in the right queues"
812 self.fake_jenkins.hold_jobs_in_build = True
Jeremy Stanleyb8ff8a72012-09-25 16:10:02 +0000813 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700814 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
815 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700816 A.addApproval('CRVW', 2)
817 B.addApproval('CRVW', 2)
818 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700819
820 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
821 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
822 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
823
824 jobs = self.fake_jenkins.all_jobs
825 self.waitUntilSettled()
826
827 # There should be one merge job at the head of each queue running
828 assert len(jobs) == 2
829 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700830 assert job_has_changes(jobs[0], A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700831 assert jobs[1].name == 'project1-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700832 assert job_has_changes(jobs[1], B)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700833
834 # Release the current merge jobs
835 self.fake_jenkins.fakeRelease('.*-merge')
836 self.waitUntilSettled()
837 # Release the merge job for project2 which is behind project1
838 self.fake_jenkins.fakeRelease('.*-merge')
839 self.waitUntilSettled()
840
841 # All the test jobs should be running:
842 # project1 (3) + project2 (3) + project (2) = 8
843 assert len(jobs) == 8
844
845 self.fake_jenkins.fakeRelease()
846 self.waitUntilSettled()
847 assert len(jobs) == 0
848
849 jobs = self.fake_jenkins.job_history
850 assert len(jobs) == 11
851 assert A.data['status'] == 'MERGED'
852 assert B.data['status'] == 'MERGED'
853 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700854 assert A.reported == 2
855 assert B.reported == 2
856 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700857 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -0700858
859 def test_failed_change_at_head(self):
860 "Test that if a change at the head fails, jobs behind it are canceled"
861 self.fake_jenkins.hold_jobs_in_build = True
862
863 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
864 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
865 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700866 A.addApproval('CRVW', 2)
867 B.addApproval('CRVW', 2)
868 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700869
James E. Blair4886cc12012-07-18 15:39:41 -0700870 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700871
872 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
873 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
874 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
875
876 self.waitUntilSettled()
877 jobs = self.fake_jenkins.all_jobs
878 finished_jobs = self.fake_jenkins.job_history
879
880 assert len(jobs) == 1
881 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700882 assert job_has_changes(jobs[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -0700883
884 self.fake_jenkins.fakeRelease('.*-merge')
885 self.waitUntilSettled()
886 self.fake_jenkins.fakeRelease('.*-merge')
887 self.waitUntilSettled()
888 self.fake_jenkins.fakeRelease('.*-merge')
889 self.waitUntilSettled()
890
891 assert len(jobs) == 6
892 assert jobs[0].name == 'project-test1'
893 assert jobs[1].name == 'project-test2'
894 assert jobs[2].name == 'project-test1'
895 assert jobs[3].name == 'project-test2'
896 assert jobs[4].name == 'project-test1'
897 assert jobs[5].name == 'project-test2'
898
899 jobs[0].release()
900 self.waitUntilSettled()
901
James E. Blairec590122012-08-22 15:19:31 -0700902 assert len(jobs) == 2 # project-test2, project-merge for B
James E. Blaird466dc42012-07-31 10:42:56 -0700903 assert self.countJobResults(finished_jobs, 'ABORTED') == 4
904
905 self.fake_jenkins.hold_jobs_in_build = False
906 self.fake_jenkins.fakeRelease()
907 self.waitUntilSettled()
908
909 assert len(jobs) == 0
910 assert len(finished_jobs) == 15
911 assert A.data['status'] == 'NEW'
912 assert B.data['status'] == 'MERGED'
913 assert C.data['status'] == 'MERGED'
914 assert A.reported == 2
915 assert B.reported == 2
916 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700917 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -0700918
919 def test_failed_change_at_head_with_queue(self):
920 "Test that if a change at the head fails, queued jobs are canceled"
921 self.fake_jenkins.hold_jobs_in_queue = True
922
923 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
924 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
925 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700926 A.addApproval('CRVW', 2)
927 B.addApproval('CRVW', 2)
928 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700929
James E. Blair4886cc12012-07-18 15:39:41 -0700930 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700931
932 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
933 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
934 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
935
936 self.waitUntilSettled()
937 jobs = self.fake_jenkins.all_jobs
938 finished_jobs = self.fake_jenkins.job_history
939 queue = self.fake_jenkins.queue
940
941 assert len(jobs) == 1
942 assert len(queue) == 1
943 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700944 assert job_has_changes(jobs[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -0700945
946 self.fake_jenkins.fakeRelease('.*-merge')
947 self.waitUntilSettled()
948 self.fake_jenkins.fakeRelease('.*-merge')
949 self.waitUntilSettled()
950 self.fake_jenkins.fakeRelease('.*-merge')
951 self.waitUntilSettled()
952
953 assert len(jobs) == 6
954 assert len(queue) == 6
955 assert jobs[0].name == 'project-test1'
956 assert jobs[1].name == 'project-test2'
957 assert jobs[2].name == 'project-test1'
958 assert jobs[3].name == 'project-test2'
959 assert jobs[4].name == 'project-test1'
960 assert jobs[5].name == 'project-test2'
961
962 jobs[0].release()
963 self.waitUntilSettled()
964
James E. Blairec590122012-08-22 15:19:31 -0700965 assert len(jobs) == 2 # project-test2, project-merge for B
966 assert len(queue) == 2
James E. Blaird466dc42012-07-31 10:42:56 -0700967 assert self.countJobResults(finished_jobs, 'ABORTED') == 0
968
969 self.fake_jenkins.hold_jobs_in_queue = False
970 self.fake_jenkins.fakeRelease()
971 self.waitUntilSettled()
972
973 assert len(jobs) == 0
974 assert len(finished_jobs) == 11
975 assert A.data['status'] == 'NEW'
976 assert B.data['status'] == 'MERGED'
977 assert C.data['status'] == 'MERGED'
978 assert A.reported == 2
979 assert B.reported == 2
980 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700981 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -0700982
983 def test_patch_order(self):
984 "Test that dependent patches are tested in the right order"
985 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
986 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
987 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
988 A.addApproval('CRVW', 2)
989 B.addApproval('CRVW', 2)
990 C.addApproval('CRVW', 2)
991
992 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
993 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
994 M2.setMerged()
995 M1.setMerged()
996
997 # C -> B -> A -> M1 -> M2
998 # M2 is here to make sure it is never queried. If it is, it
999 # means zuul is walking down the entire history of merged
1000 # changes.
1001
1002 C.setDependsOn(B, 1)
1003 B.setDependsOn(A, 1)
1004 A.setDependsOn(M1, 1)
1005 M1.setDependsOn(M2, 1)
1006
1007 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1008
1009 self.waitUntilSettled()
1010
1011 assert A.data['status'] == 'NEW'
1012 assert B.data['status'] == 'NEW'
1013 assert C.data['status'] == 'NEW'
1014
1015 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1016 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1017
1018 self.waitUntilSettled()
1019 assert M2.queried == 0
1020 assert A.data['status'] == 'MERGED'
1021 assert B.data['status'] == 'MERGED'
1022 assert C.data['status'] == 'MERGED'
1023 assert A.reported == 2
1024 assert B.reported == 2
1025 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001026 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001027
1028 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001029 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001030 # TODO: move to test_gerrit (this is a unit test!)
1031 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4aea70c2012-07-26 14:23:24 -07001032 a = self.sched.trigger.getChange(1, 2)
1033 mgr = self.sched.pipelines['gate'].manager
1034 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001035
1036 A.addApproval('CRVW', 2)
James E. Blair4aea70c2012-07-26 14:23:24 -07001037 a = self.sched.trigger.getChange(1, 2)
1038 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001039
1040 A.addApproval('APRV', 1)
James E. Blair4aea70c2012-07-26 14:23:24 -07001041 a = self.sched.trigger.getChange(1, 2)
1042 assert self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blaire0487072012-08-29 17:38:31 -07001043 self.assertEmptyQueues()
James E. Blair4886cc12012-07-18 15:39:41 -07001044
1045 def test_build_configuration(self):
1046 "Test that zuul merges the right commits for testing"
1047 self.fake_jenkins.hold_jobs_in_queue = True
1048 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1049 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1050 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1051 A.addApproval('CRVW', 2)
1052 B.addApproval('CRVW', 2)
1053 C.addApproval('CRVW', 2)
1054 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1055 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1056 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1057 self.waitUntilSettled()
1058
1059 jobs = self.fake_jenkins.all_jobs
1060
1061 self.fake_jenkins.fakeRelease('.*-merge')
1062 self.waitUntilSettled()
1063 self.fake_jenkins.fakeRelease('.*-merge')
1064 self.waitUntilSettled()
1065 self.fake_jenkins.fakeRelease('.*-merge')
1066 self.waitUntilSettled()
James E. Blair1dbd5082012-08-23 15:12:15 -07001067
James E. Blair4886cc12012-07-18 15:39:41 -07001068 ref = jobs[-1].parameters['ZUUL_REF']
1069 self.fake_jenkins.hold_jobs_in_queue = False
1070 self.fake_jenkins.fakeRelease()
James E. Blair973721f2012-08-15 10:19:43 -07001071 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001072
James E. Blair1dbd5082012-08-23 15:12:15 -07001073 path = os.path.join(GIT_ROOT, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001074 repo = git.Repo(path)
1075 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1076 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001077 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
1078 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001079 self.assertEmptyQueues()
James E. Blair973721f2012-08-15 10:19:43 -07001080
1081 def test_build_configuration_conflict(self):
1082 "Test that merge conflicts are handled"
1083 self.fake_jenkins.hold_jobs_in_queue = True
1084 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1085 A.addPatchset(['conflict'])
1086 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1087 B.addPatchset(['conflict'])
1088 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1089 A.addApproval('CRVW', 2)
1090 B.addApproval('CRVW', 2)
1091 C.addApproval('CRVW', 2)
1092 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1093 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1094 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1095 self.waitUntilSettled()
1096
1097 jobs = self.fake_jenkins.all_jobs
1098
1099 self.fake_jenkins.fakeRelease('.*-merge')
1100 self.waitUntilSettled()
1101 self.fake_jenkins.fakeRelease('.*-merge')
1102 self.waitUntilSettled()
1103 self.fake_jenkins.fakeRelease('.*-merge')
1104 self.waitUntilSettled()
1105 ref = jobs[-1].parameters['ZUUL_REF']
1106 self.fake_jenkins.hold_jobs_in_queue = False
1107 self.fake_jenkins.fakeRelease()
1108 self.waitUntilSettled()
1109
1110 assert A.data['status'] == 'MERGED'
1111 assert B.data['status'] == 'NEW'
1112 assert C.data['status'] == 'MERGED'
1113 assert A.reported == 2
1114 assert B.reported == 2
1115 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001116 self.assertEmptyQueues()
James E. Blairdaabed22012-08-15 15:38:57 -07001117
1118 def test_post(self):
1119 "Test that post jobs run"
Jeremy Stanleyb8ff8a72012-09-25 16:10:02 +00001120 e = {"type": "ref-updated",
1121 "submitter": {"name": "User Name"},
1122 "refUpdate": {"oldRev":
1123 "90f173846e3af9154517b88543ffbd1691f31366",
1124 "newRev":
1125 "d479a0bfcb34da57a31adb2a595c0cf687812543",
1126 "refName": "master", "project": "org/project"}}
James E. Blairdaabed22012-08-15 15:38:57 -07001127 self.fake_gerrit.addEvent(e)
1128 self.waitUntilSettled()
1129
1130 jobs = self.fake_jenkins.job_history
1131 job_names = [x['name'] for x in jobs]
1132 assert len(jobs) == 1
1133 assert 'project-post' in job_names
James E. Blaire0487072012-08-29 17:38:31 -07001134 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001135
1136 def test_build_configuration_branch(self):
1137 "Test that the right commits are on alternate branches"
1138 self.fake_jenkins.hold_jobs_in_queue = True
1139 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1140 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1141 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1142 A.addApproval('CRVW', 2)
1143 B.addApproval('CRVW', 2)
1144 C.addApproval('CRVW', 2)
1145 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1146 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1147 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1148 self.waitUntilSettled()
1149
1150 jobs = self.fake_jenkins.all_jobs
1151
1152 self.fake_jenkins.fakeRelease('.*-merge')
1153 self.waitUntilSettled()
1154 self.fake_jenkins.fakeRelease('.*-merge')
1155 self.waitUntilSettled()
1156 self.fake_jenkins.fakeRelease('.*-merge')
1157 self.waitUntilSettled()
1158 ref = jobs[-1].parameters['ZUUL_REF']
1159 self.fake_jenkins.hold_jobs_in_queue = False
1160 self.fake_jenkins.fakeRelease()
1161 self.waitUntilSettled()
1162
James E. Blair1dbd5082012-08-23 15:12:15 -07001163 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001164 repo = git.Repo(path)
1165 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1166 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001167 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
1168 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001169 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001170
1171 def test_build_configuration_branch_interaction(self):
1172 "Test that switching between branches works"
1173 self.test_build_configuration()
1174 self.test_build_configuration_branch()
1175 # C has been merged, undo that
James E. Blair1dbd5082012-08-23 15:12:15 -07001176 path = os.path.join(UPSTREAM_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001177 repo = git.Repo(path)
1178 repo.heads.master.commit = repo.commit('init')
1179 self.test_build_configuration()
James E. Blaire0487072012-08-29 17:38:31 -07001180 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001181
1182 def test_build_configuration_multi_branch(self):
1183 "Test that dependent changes on multiple branches are merged"
1184 self.fake_jenkins.hold_jobs_in_queue = True
1185 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1186 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1187 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1188 A.addApproval('CRVW', 2)
1189 B.addApproval('CRVW', 2)
1190 C.addApproval('CRVW', 2)
1191 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1192 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1193 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1194 self.waitUntilSettled()
1195
1196 jobs = self.fake_jenkins.all_jobs
1197
1198 self.fake_jenkins.fakeRelease('.*-merge')
1199 self.waitUntilSettled()
1200 ref_mp = jobs[-1].parameters['ZUUL_REF']
1201 self.fake_jenkins.fakeRelease('.*-merge')
1202 self.waitUntilSettled()
1203 self.fake_jenkins.fakeRelease('.*-merge')
1204 self.waitUntilSettled()
1205 ref_master = jobs[-1].parameters['ZUUL_REF']
1206 self.fake_jenkins.hold_jobs_in_queue = False
1207 self.fake_jenkins.fakeRelease()
1208 self.waitUntilSettled()
1209
James E. Blair1dbd5082012-08-23 15:12:15 -07001210 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001211 repo = git.Repo(path)
1212
1213 repo_messages = [c.message.strip()
1214 for c in repo.iter_commits(ref_master)]
1215 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001216 correct_messages = ['initial commit', 'A-1', 'C-1']
1217 assert repo_messages == correct_messages
1218
1219 repo_messages = [c.message.strip()
1220 for c in repo.iter_commits(ref_mp)]
1221 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001222 correct_messages = ['initial commit', 'mp commit', 'B-1']
1223 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001224 self.assertEmptyQueues()
James E. Blair7f71c802012-08-22 13:04:32 -07001225
1226 def test_one_job_project(self):
1227 "Test that queueing works with one job"
1228 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1229 'master', 'A')
1230 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1231 'master', 'B')
1232 A.addApproval('CRVW', 2)
1233 B.addApproval('CRVW', 2)
1234 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1235 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1236 self.waitUntilSettled()
1237
1238 jobs = self.fake_jenkins.all_jobs
1239 finished_jobs = self.fake_jenkins.job_history
James E. Blair7f71c802012-08-22 13:04:32 -07001240
1241 assert A.data['status'] == 'MERGED'
1242 assert A.reported == 2
1243 assert B.data['status'] == 'MERGED'
1244 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001245 self.assertEmptyQueues()
James E. Blaircaec0c52012-08-22 14:52:22 -07001246
1247 def test_dependent_changes_dequeue(self):
1248 "Test that dependent patches are not needlessly tested"
1249 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1250 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1251 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1252 A.addApproval('CRVW', 2)
1253 B.addApproval('CRVW', 2)
1254 C.addApproval('CRVW', 2)
1255
1256 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1257 M1.setMerged()
1258
1259 # C -> B -> A -> M1
1260
1261 C.setDependsOn(B, 1)
1262 B.setDependsOn(A, 1)
1263 A.setDependsOn(M1, 1)
1264
1265 self.fake_jenkins.fakeAddFailTest('project-merge', A)
1266
1267 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1268 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1269 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1270
1271 self.waitUntilSettled()
1272
1273 jobs = self.fake_jenkins.all_jobs
1274 finished_jobs = self.fake_jenkins.job_history
1275
James E. Blair127bc182012-08-28 15:55:15 -07001276 for x in jobs:
1277 print x
1278 for x in finished_jobs:
1279 print x
1280
James E. Blairec590122012-08-22 15:19:31 -07001281 assert A.data['status'] == 'NEW'
1282 assert A.reported == 2
1283 assert B.data['status'] == 'NEW'
1284 assert B.reported == 2
1285 assert C.data['status'] == 'NEW'
1286 assert C.reported == 2
1287 assert len(finished_jobs) == 1
James E. Blaire0487072012-08-29 17:38:31 -07001288 self.assertEmptyQueues()
James E. Blairec590122012-08-22 15:19:31 -07001289
1290 def test_head_is_dequeued_once(self):
1291 "Test that if a change at the head fails it is dequeud only once"
1292 # If it's dequeued more than once, we should see extra
1293 # aborted jobs.
1294 self.fake_jenkins.hold_jobs_in_build = True
1295
1296 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1297 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1298 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1299 A.addApproval('CRVW', 2)
1300 B.addApproval('CRVW', 2)
1301 C.addApproval('CRVW', 2)
1302
1303 self.fake_jenkins.fakeAddFailTest('project1-test1', A)
1304 self.fake_jenkins.fakeAddFailTest('project1-test2', A)
1305 self.fake_jenkins.fakeAddFailTest('project1-project2-integration', A)
1306
1307 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1308 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1309 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1310
1311 self.waitUntilSettled()
1312 jobs = self.fake_jenkins.all_jobs
1313 finished_jobs = self.fake_jenkins.job_history
1314
1315 assert len(jobs) == 1
1316 assert jobs[0].name == 'project1-merge'
1317 assert job_has_changes(jobs[0], A)
1318
1319 self.fake_jenkins.fakeRelease('.*-merge')
1320 self.waitUntilSettled()
1321 self.fake_jenkins.fakeRelease('.*-merge')
1322 self.waitUntilSettled()
1323 self.fake_jenkins.fakeRelease('.*-merge')
1324 self.waitUntilSettled()
1325
1326 assert len(jobs) == 9
1327 assert jobs[0].name == 'project1-test1'
1328 assert jobs[1].name == 'project1-test2'
1329 assert jobs[2].name == 'project1-project2-integration'
1330 assert jobs[3].name == 'project1-test1'
1331 assert jobs[4].name == 'project1-test2'
1332 assert jobs[5].name == 'project1-project2-integration'
1333 assert jobs[6].name == 'project1-test1'
1334 assert jobs[7].name == 'project1-test2'
1335 assert jobs[8].name == 'project1-project2-integration'
1336
1337 jobs[0].release()
1338 self.waitUntilSettled()
1339
1340 assert len(jobs) == 3 # test2, integration, merge for B
1341 assert self.countJobResults(finished_jobs, 'ABORTED') == 6
1342
1343 self.fake_jenkins.hold_jobs_in_build = False
1344 self.fake_jenkins.fakeRelease()
1345 self.waitUntilSettled()
1346
1347 assert len(jobs) == 0
1348 assert len(finished_jobs) == 20
James E. Blaircaec0c52012-08-22 14:52:22 -07001349
1350 assert A.data['status'] == 'NEW'
James E. Blairec590122012-08-22 15:19:31 -07001351 assert B.data['status'] == 'MERGED'
1352 assert C.data['status'] == 'MERGED'
1353 assert A.reported == 2
1354 assert B.reported == 2
1355 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001356 self.assertEmptyQueues()
James E. Blair4ec821f2012-08-23 15:28:28 -07001357
1358 def test_nonvoting_job(self):
1359 "Test that non-voting jobs don't vote."
1360 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1361 'master', 'A')
1362 A.addApproval('CRVW', 2)
1363 self.fake_jenkins.fakeAddFailTest('nonvoting-project-test2', A)
1364 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1365
1366 self.waitUntilSettled()
1367 jobs = self.fake_jenkins.all_jobs
1368 finished_jobs = self.fake_jenkins.job_history
1369
1370 assert A.data['status'] == 'MERGED'
1371 assert A.reported == 2
1372 assert finished_jobs[0]['result'] == 'SUCCESS'
1373 assert finished_jobs[1]['result'] == 'SUCCESS'
1374 assert finished_jobs[2]['result'] == 'FAILURE'
James E. Blaire0487072012-08-29 17:38:31 -07001375 self.assertEmptyQueues()
1376
1377 def test_check_queue_success(self):
1378 "Test successful check queue jobs."
1379 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1380 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1381
1382 self.waitUntilSettled()
1383 jobs = self.fake_jenkins.all_jobs
1384 finished_jobs = self.fake_jenkins.job_history
1385
1386 assert A.data['status'] == 'NEW'
1387 assert A.reported == 1
1388 assert finished_jobs[0]['result'] == 'SUCCESS'
1389 assert finished_jobs[1]['result'] == 'SUCCESS'
1390 assert finished_jobs[2]['result'] == 'SUCCESS'
1391 self.assertEmptyQueues()
1392
1393 def test_check_queue_failure(self):
1394 "Test failed check queue jobs."
1395 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1396 self.fake_jenkins.fakeAddFailTest('project-test2', A)
1397 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1398
1399 self.waitUntilSettled()
1400 jobs = self.fake_jenkins.all_jobs
1401 finished_jobs = self.fake_jenkins.job_history
1402
1403 assert A.data['status'] == 'NEW'
1404 assert A.reported == 1
1405 assert finished_jobs[0]['result'] == 'SUCCESS'
1406 assert finished_jobs[1]['result'] == 'SUCCESS'
1407 assert finished_jobs[2]['result'] == 'FAILURE'
1408 self.assertEmptyQueues()
James E. Blair127bc182012-08-28 15:55:15 -07001409
1410 def test_dependent_behind_dequeue(self):
1411 "test that dependent changes behind dequeued changes work"
1412 # This complicated test is a reproduction of a real life bug
1413 self.sched.reconfigure(self.config)
1414 self.fake_jenkins.hold_jobs_in_build = True
1415
1416 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1417 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1418 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1419 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1420 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1421 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1422 D.setDependsOn(C, 1)
1423 E.setDependsOn(D, 1)
1424 A.addApproval('CRVW', 2)
1425 B.addApproval('CRVW', 2)
1426 C.addApproval('CRVW', 2)
1427 D.addApproval('CRVW', 2)
1428 E.addApproval('CRVW', 2)
1429 F.addApproval('CRVW', 2)
1430
1431 A.fail_merge = True
1432 jobs = self.fake_jenkins.all_jobs
1433 finished_jobs = self.fake_jenkins.job_history
1434
1435 # Change object re-use in the gerrit trigger is hidden if
1436 # changes are added in quick succession; waiting makes it more
1437 # like real life.
1438 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1439 self.waitUntilSettled()
1440 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1441 self.waitUntilSettled()
1442
1443 self.fake_jenkins.fakeRelease('.*-merge')
1444 self.waitUntilSettled()
1445 self.fake_jenkins.fakeRelease('.*-merge')
1446 self.waitUntilSettled()
1447
1448 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1449 self.waitUntilSettled()
1450 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1451 self.waitUntilSettled()
1452 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1453 self.waitUntilSettled()
1454 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1455 self.waitUntilSettled()
1456
1457 self.fake_jenkins.fakeRelease('.*-merge')
1458 self.waitUntilSettled()
1459 self.fake_jenkins.fakeRelease('.*-merge')
1460 self.waitUntilSettled()
1461 self.fake_jenkins.fakeRelease('.*-merge')
1462 self.waitUntilSettled()
1463 self.fake_jenkins.fakeRelease('.*-merge')
1464 self.waitUntilSettled()
1465
1466 # all jobs running
1467 jobs[0].release()
1468 jobs[1].release()
1469 jobs[2].release()
1470 self.waitUntilSettled()
1471
1472 self.fake_jenkins.hold_jobs_in_build = False
1473 self.fake_jenkins.fakeRelease()
1474 self.waitUntilSettled()
1475
1476 for x in jobs:
1477 print x
1478 for x in finished_jobs:
1479 print x
1480 print self.sched.formatStatusHTML()
1481
1482 assert A.data['status'] == 'NEW'
1483 assert B.data['status'] == 'MERGED'
1484 assert C.data['status'] == 'MERGED'
1485 assert D.data['status'] == 'MERGED'
1486 assert E.data['status'] == 'MERGED'
1487 assert F.data['status'] == 'MERGED'
1488
1489 assert A.reported == 2
1490 assert B.reported == 2
1491 assert C.reported == 2
1492 assert D.reported == 2
1493 assert E.reported == 2
1494 assert F.reported == 2
1495
James E. Blair127bc182012-08-28 15:55:15 -07001496 assert self.countJobResults(finished_jobs, 'ABORTED') == 15
1497 assert len(finished_jobs) == 44
James E. Blaire0487072012-08-29 17:38:31 -07001498 self.assertEmptyQueues()
James E. Blair05fed602012-09-07 12:45:24 -07001499
1500 def test_merger_repack(self):
1501 "Test that the merger works after a repack"
1502 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1503 A.addApproval('CRVW', 2)
1504 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1505 self.waitUntilSettled()
1506 jobs = self.fake_jenkins.job_history
1507 job_names = [x['name'] for x in jobs]
1508 assert 'project-merge' in job_names
1509 assert 'project-test1' in job_names
1510 assert 'project-test2' in job_names
1511 assert jobs[0]['result'] == 'SUCCESS'
1512 assert jobs[1]['result'] == 'SUCCESS'
1513 assert jobs[2]['result'] == 'SUCCESS'
1514 assert A.data['status'] == 'MERGED'
1515 assert A.reported == 2
1516 self.assertEmptyQueues()
1517
1518 path = os.path.join(GIT_ROOT, "org/project")
1519 os.system('git --git-dir=%s/.git repack -afd' % path)
1520
1521 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1522 A.addApproval('CRVW', 2)
1523 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1524 self.waitUntilSettled()
1525 jobs = self.fake_jenkins.job_history
1526 job_names = [x['name'] for x in jobs]
1527 assert 'project-merge' in job_names
1528 assert 'project-test1' in job_names
1529 assert 'project-test2' in job_names
1530 assert jobs[0]['result'] == 'SUCCESS'
1531 assert jobs[1]['result'] == 'SUCCESS'
1532 assert jobs[2]['result'] == 'SUCCESS'
1533 assert A.data['status'] == 'MERGED'
1534 assert A.reported == 2
1535 self.assertEmptyQueues()
James E. Blair7ee88a22012-09-12 18:59:31 +02001536
1537 def test_nonexistent_job(self):
1538 "Test launching a job that doesn't exist"
1539 self.fake_jenkins.nonexistent_jobs.append('project-merge')
1540 self.jenkins.launch_retry_timeout = 0.1
1541
1542 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1543 A.addApproval('CRVW', 2)
1544 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1545 # There may be a thread about to report a lost change
1546 while A.reported < 2:
1547 self.waitUntilSettled()
1548 jobs = self.fake_jenkins.job_history
1549 job_names = [x['name'] for x in jobs]
1550 assert not job_names
1551 assert A.data['status'] == 'NEW'
1552 assert A.reported == 2
1553 self.assertEmptyQueues()
1554
1555 # Make sure things still work:
1556 self.fake_jenkins.nonexistent_jobs = []
1557 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1558 A.addApproval('CRVW', 2)
1559 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1560 self.waitUntilSettled()
1561 jobs = self.fake_jenkins.job_history
1562 job_names = [x['name'] for x in jobs]
1563 assert 'project-merge' in job_names
1564 assert 'project-test1' in job_names
1565 assert 'project-test2' in job_names
1566 assert jobs[0]['result'] == 'SUCCESS'
1567 assert jobs[1]['result'] == 'SUCCESS'
1568 assert jobs[2]['result'] == 'SUCCESS'
1569 assert A.data['status'] == 'MERGED'
1570 assert A.reported == 2
1571 self.assertEmptyQueues()