blob: fd103c8db7f9f8814026ac800eedd08088100167 [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):
Zhongyue Luoafd39bb2012-09-21 02:00:47 +0900365 data = {
366 'build': {
367 'full_url': 'https://server/job/%s/%s/' % (name, number),
368 'number': number,
369 'parameters': parameters,
370 'phase': phase,
371 'url': 'job/%s/%s/' % (name, number),
372 },
373 'name': name,
374 'url': 'job/%s/' % name,
375 }
James E. Blairb0fcae42012-07-17 11:12:10 -0700376 if status:
377 data['build']['status'] = status
378 self.body = json.dumps(data)
379
380
381class FakeJenkinsJob(threading.Thread):
382 log = logging.getLogger("zuul.test")
383
384 def __init__(self, jenkins, callback, name, number, parameters):
385 threading.Thread.__init__(self)
386 self.jenkins = jenkins
387 self.callback = callback
388 self.name = name
389 self.number = number
390 self.parameters = parameters
391 self.wait_condition = threading.Condition()
392 self.waiting = False
James E. Blaird466dc42012-07-31 10:42:56 -0700393 self.aborted = False
394 self.canceled = False
395 self.created = time.time()
James E. Blairb0fcae42012-07-17 11:12:10 -0700396
397 def release(self):
398 self.wait_condition.acquire()
399 self.wait_condition.notify()
400 self.waiting = False
401 self.log.debug("Job %s released" % (self.parameters['UUID']))
402 self.wait_condition.release()
403
404 def isWaiting(self):
405 self.wait_condition.acquire()
406 if self.waiting:
407 ret = True
408 else:
409 ret = False
410 self.wait_condition.release()
411 return ret
412
413 def _wait(self):
414 self.wait_condition.acquire()
415 self.waiting = True
416 self.log.debug("Job %s waiting" % (self.parameters['UUID']))
417 self.wait_condition.wait()
418 self.wait_condition.release()
419
420 def run(self):
421 self.jenkins.fakeEnqueue(self)
422 if self.jenkins.hold_jobs_in_queue:
423 self._wait()
424 self.jenkins.fakeDequeue(self)
James E. Blaird466dc42012-07-31 10:42:56 -0700425 if self.canceled:
426 self.jenkins.all_jobs.remove(self)
427 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700428 self.callback.jenkins_endpoint(FakeJenkinsEvent(
429 self.name, self.number, self.parameters,
430 'STARTED'))
431 if self.jenkins.hold_jobs_in_build:
432 self._wait()
433 self.log.debug("Job %s continuing" % (self.parameters['UUID']))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700434
435 result = 'SUCCESS'
James E. Blairdaabed22012-08-15 15:38:57 -0700436 if ('ZUUL_REF' in self.parameters) and self.jenkins.fakeShouldFailTest(
James E. Blairb02a3bb2012-07-30 17:49:55 -0700437 self.name,
James E. Blair4886cc12012-07-18 15:39:41 -0700438 self.parameters['ZUUL_REF']):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700439 result = 'FAILURE'
James E. Blaird466dc42012-07-31 10:42:56 -0700440 if self.aborted:
441 result = 'ABORTED'
James E. Blairb02a3bb2012-07-30 17:49:55 -0700442
James E. Blairb0fcae42012-07-17 11:12:10 -0700443 self.jenkins.fakeAddHistory(name=self.name, number=self.number,
James E. Blairb02a3bb2012-07-30 17:49:55 -0700444 result=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 'COMPLETED', result))
James E. Blairb0fcae42012-07-17 11:12:10 -0700448 self.callback.jenkins_endpoint(FakeJenkinsEvent(
449 self.name, self.number, self.parameters,
James E. Blairb02a3bb2012-07-30 17:49:55 -0700450 'FINISHED', result))
James E. Blairb0fcae42012-07-17 11:12:10 -0700451 self.jenkins.all_jobs.remove(self)
452
453
454class FakeJenkins(object):
455 log = logging.getLogger("zuul.test")
456
457 def __init__(self, *args, **kw):
458 self.queue = []
459 self.all_jobs = []
460 self.job_counter = {}
James E. Blaird466dc42012-07-31 10:42:56 -0700461 self.queue_counter = 0
James E. Blairb0fcae42012-07-17 11:12:10 -0700462 self.job_history = []
463 self.hold_jobs_in_queue = False
464 self.hold_jobs_in_build = False
James E. Blairb02a3bb2012-07-30 17:49:55 -0700465 self.fail_tests = {}
James E. Blair7ee88a22012-09-12 18:59:31 +0200466 self.nonexistent_jobs = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700467
468 def fakeEnqueue(self, job):
469 self.queue.append(job)
470
471 def fakeDequeue(self, job):
472 self.queue.remove(job)
473
474 def fakeAddHistory(self, **kw):
475 self.job_history.append(kw)
476
477 def fakeRelease(self, regex=None):
478 all_jobs = self.all_jobs[:]
479 self.log.debug("releasing jobs %s (%s)" % (regex, len(self.all_jobs)))
480 for job in all_jobs:
481 if not regex or re.match(regex, job.name):
482 self.log.debug("releasing job %s" % (job.parameters['UUID']))
483 job.release()
484 else:
485 self.log.debug("not releasing job %s" % (
486 job.parameters['UUID']))
487 self.log.debug("done releasing jobs %s (%s)" % (regex,
488 len(self.all_jobs)))
489
490 def fakeAllWaiting(self, regex=None):
491 all_jobs = self.all_jobs[:]
492 for job in all_jobs:
493 self.log.debug("job %s %s" % (job.parameters['UUID'],
494 job.isWaiting()))
495 if not job.isWaiting():
496 return False
497 return True
498
James E. Blairb02a3bb2012-07-30 17:49:55 -0700499 def fakeAddFailTest(self, name, change):
500 l = self.fail_tests.get(name, [])
501 l.append(change)
502 self.fail_tests[name] = l
503
James E. Blair4886cc12012-07-18 15:39:41 -0700504 def fakeShouldFailTest(self, name, ref):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700505 l = self.fail_tests.get(name, [])
506 for change in l:
James E. Blair4886cc12012-07-18 15:39:41 -0700507 if ref_has_change(ref, change):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700508 return True
509 return False
510
James E. Blairb0fcae42012-07-17 11:12:10 -0700511 def build_job(self, name, parameters):
James E. Blair7ee88a22012-09-12 18:59:31 +0200512 if name in self.nonexistent_jobs:
513 raise Exception("Job does not exist")
James E. Blairb0fcae42012-07-17 11:12:10 -0700514 count = self.job_counter.get(name, 0)
515 count += 1
516 self.job_counter[name] = count
James E. Blaird466dc42012-07-31 10:42:56 -0700517
518 queue_count = self.queue_counter
519 self.queue_counter += 1
James E. Blairb0fcae42012-07-17 11:12:10 -0700520 job = FakeJenkinsJob(self, self.callback, name, count, parameters)
James E. Blaird466dc42012-07-31 10:42:56 -0700521 job.queue_id = queue_count
522
James E. Blairb0fcae42012-07-17 11:12:10 -0700523 self.all_jobs.append(job)
524 job.start()
525
James E. Blaird466dc42012-07-31 10:42:56 -0700526 def stop_build(self, name, number):
527 for job in self.all_jobs:
528 if job.name == name and job.number == number:
529 job.aborted = True
530 job.release()
531 return
532
533 def cancel_queue(self, id):
534 for job in self.queue:
535 if job.queue_id == id:
536 job.canceled = True
537 job.release()
538 return
539
540 def get_queue_info(self):
541 items = []
542 for job in self.queue:
543 paramstr = ''
544 paramlst = []
545 d = {'actions': [{'parameters': paramlst},
546 {'causes': [{'shortDescription':
547 'Started by user Jenkins',
548 'userId': 'jenkins',
549 'userName': 'Jenkins'}]}],
550 'blocked': False,
551 'buildable': True,
552 'buildableStartMilliseconds': (job.created * 1000) + 5,
553 'id': job.queue_id,
554 'inQueueSince': (job.created * 1000),
555 'params': paramstr,
556 'stuck': False,
557 'task': {'color': 'blue',
558 'name': job.name,
559 'url': 'https://server/job/%s/' % job.name},
560 'why': 'Waiting for next available executor'}
561 for k, v in job.parameters.items():
562 paramstr += "\n(StringParameterValue) %s='%s'" % (k, v)
563 pd = {'name': k, 'value': v}
564 paramlst.append(pd)
565 items.append(d)
566 return items
567
James E. Blairb0fcae42012-07-17 11:12:10 -0700568 def set_build_description(self, *args, **kw):
569 pass
570
571
572class FakeJenkinsCallback(zuul.launcher.jenkins.JenkinsCallback):
573 def start(self):
574 pass
575
576
James E. Blair8cc15a82012-08-01 11:17:57 -0700577class FakeURLOpener(object):
578 def __init__(self, fake_gerrit, url):
579 self.fake_gerrit = fake_gerrit
580 self.url = url
581
582 def read(self):
583 res = urlparse.urlparse(self.url)
584 path = res.path
585 project = '/'.join(path.split('/')[2:-2])
James E. Blair8cc15a82012-08-01 11:17:57 -0700586 ret = ''
James E. Blair1dbd5082012-08-23 15:12:15 -0700587 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700588 repo = git.Repo(path)
589 for ref in repo.refs:
590 ret += ref.object.hexsha + '\t' + ref.path + '\n'
James E. Blair8cc15a82012-08-01 11:17:57 -0700591 return ret
592
593
James E. Blair4886cc12012-07-18 15:39:41 -0700594class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
595 def getGitUrl(self, project):
James E. Blair1dbd5082012-08-23 15:12:15 -0700596 return os.path.join(UPSTREAM_ROOT, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700597
598
James E. Blairb0fcae42012-07-17 11:12:10 -0700599class testScheduler(unittest.TestCase):
600 log = logging.getLogger("zuul.test")
601
602 def setUp(self):
James E. Blair1dbd5082012-08-23 15:12:15 -0700603 if os.path.exists(TEST_ROOT):
604 shutil.rmtree(TEST_ROOT)
605 os.makedirs(TEST_ROOT)
606 os.makedirs(UPSTREAM_ROOT)
607 os.makedirs(GIT_ROOT)
James E. Blair4886cc12012-07-18 15:39:41 -0700608
609 # For each project in config:
610 init_repo("org/project")
611 init_repo("org/project1")
612 init_repo("org/project2")
James E. Blair127bc182012-08-28 15:55:15 -0700613 init_repo("org/project3")
James E. Blair7f71c802012-08-22 13:04:32 -0700614 init_repo("org/one-job-project")
James E. Blair4ec821f2012-08-23 15:28:28 -0700615 init_repo("org/nonvoting-project")
James E. Blairb0fcae42012-07-17 11:12:10 -0700616 self.config = CONFIG
617 self.sched = zuul.scheduler.Scheduler()
618
619 def jenkinsFactory(*args, **kw):
620 self.fake_jenkins = FakeJenkins()
621 return self.fake_jenkins
622
623 def jenkinsCallbackFactory(*args, **kw):
624 self.fake_jenkins_callback = FakeJenkinsCallback(*args, **kw)
625 return self.fake_jenkins_callback
626
James E. Blair8cc15a82012-08-01 11:17:57 -0700627 def URLOpenerFactory(*args, **kw):
628 args = [self.fake_gerrit] + list(args)
629 return FakeURLOpener(*args, **kw)
630
James E. Blairb0fcae42012-07-17 11:12:10 -0700631 zuul.launcher.jenkins.ExtendedJenkins = jenkinsFactory
632 zuul.launcher.jenkins.JenkinsCallback = jenkinsCallbackFactory
James E. Blair8cc15a82012-08-01 11:17:57 -0700633 urllib2.urlopen = URLOpenerFactory
James E. Blairb0fcae42012-07-17 11:12:10 -0700634 self.jenkins = zuul.launcher.jenkins.Jenkins(self.config, self.sched)
635 self.fake_jenkins.callback = self.fake_jenkins_callback
636
637 zuul.lib.gerrit.Gerrit = FakeGerrit
638
James E. Blair4886cc12012-07-18 15:39:41 -0700639 self.gerrit = FakeGerritTrigger(self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700640 self.gerrit.replication_timeout = 1.5
641 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700642 self.fake_gerrit = self.gerrit.gerrit
643
644 self.sched.setLauncher(self.jenkins)
645 self.sched.setTrigger(self.gerrit)
646
647 self.sched.start()
648 self.sched.reconfigure(self.config)
649 self.sched.resume()
650
651 def tearDown(self):
652 self.jenkins.stop()
653 self.gerrit.stop()
654 self.sched.stop()
655 self.sched.join()
James E. Blair1dbd5082012-08-23 15:12:15 -0700656 #shutil.rmtree(TEST_ROOT)
James E. Blairb0fcae42012-07-17 11:12:10 -0700657
658 def waitUntilSettled(self):
659 self.log.debug("Waiting until settled...")
660 start = time.time()
661 while True:
662 if time.time() - start > 10:
663 print 'queue status:',
664 print self.sched.trigger_event_queue.empty(),
665 print self.sched.result_event_queue.empty(),
666 print self.fake_gerrit.event_queue.empty(),
667 raise Exception("Timeout waiting for Zuul to settle")
668 self.fake_gerrit.event_queue.join()
669 self.sched.queue_lock.acquire()
670 if (self.sched.trigger_event_queue.empty() and
671 self.sched.result_event_queue.empty() and
672 self.fake_gerrit.event_queue.empty() and
673 self.fake_jenkins.fakeAllWaiting()):
674 self.sched.queue_lock.release()
675 self.log.debug("...settled.")
676 return
677 self.sched.queue_lock.release()
678 self.sched.wake_event.wait(0.1)
679
James E. Blaird466dc42012-07-31 10:42:56 -0700680 def countJobResults(self, jobs, result):
681 jobs = filter(lambda x: x['result'] == result, jobs)
682 return len(jobs)
683
James E. Blaire0487072012-08-29 17:38:31 -0700684 def assertEmptyQueues(self):
685 # Make sure there are no orphaned jobs
686 for pipeline in self.sched.pipelines.values():
687 for queue in pipeline.queues:
688 if len(queue.queue) != 0:
689 print 'queue', queue.queue
690 assert len(queue.queue) == 0
691 if len(queue.severed_heads) != 0:
692 print 'heads', queue.severed_heads
693 assert len(queue.severed_heads) == 0
694
James E. Blairb0fcae42012-07-17 11:12:10 -0700695 def test_jobs_launched(self):
696 "Test that jobs are launched and a change is merged"
697 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -0700698 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700699 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
700 self.waitUntilSettled()
701 jobs = self.fake_jenkins.job_history
702 job_names = [x['name'] for x in jobs]
703 assert 'project-merge' in job_names
704 assert 'project-test1' in job_names
705 assert 'project-test2' in job_names
706 assert jobs[0]['result'] == 'SUCCESS'
707 assert jobs[1]['result'] == 'SUCCESS'
708 assert jobs[2]['result'] == 'SUCCESS'
709 assert A.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700710 assert A.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700711 self.assertEmptyQueues()
James E. Blairb0fcae42012-07-17 11:12:10 -0700712
713 def test_parallel_changes(self):
714 "Test that changes are tested in parallel and merged in series"
715 self.fake_jenkins.hold_jobs_in_build = True
716 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
717 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
718 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700719 A.addApproval('CRVW', 2)
720 B.addApproval('CRVW', 2)
721 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700722
723 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
724 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
725 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
726
727 self.waitUntilSettled()
728 jobs = self.fake_jenkins.all_jobs
729 assert len(jobs) == 1
730 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700731 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700732
733 self.fake_jenkins.fakeRelease('.*-merge')
734 self.waitUntilSettled()
735 assert len(jobs) == 3
736 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700737 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700738 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700739 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700740 assert jobs[2].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700741 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700742
743 self.fake_jenkins.fakeRelease('.*-merge')
744 self.waitUntilSettled()
745 assert len(jobs) == 5
746 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700747 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700748 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700749 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700750
751 assert jobs[2].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700752 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700753 assert jobs[3].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700754 assert job_has_changes(jobs[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700755
756 assert jobs[4].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700757 assert job_has_changes(jobs[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700758
759 self.fake_jenkins.fakeRelease('.*-merge')
760 self.waitUntilSettled()
761 assert len(jobs) == 6
762 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700763 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700764 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700765 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700766
767 assert jobs[2].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700768 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700769 assert jobs[3].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700770 assert job_has_changes(jobs[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700771
772 assert jobs[4].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700773 assert job_has_changes(jobs[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700774 assert jobs[5].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700775 assert job_has_changes(jobs[5], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700776
777 self.fake_jenkins.hold_jobs_in_build = False
778 self.fake_jenkins.fakeRelease()
779 self.waitUntilSettled()
780 assert len(jobs) == 0
781
782 jobs = self.fake_jenkins.job_history
783 assert len(jobs) == 9
784 assert A.data['status'] == 'MERGED'
785 assert B.data['status'] == 'MERGED'
786 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700787 assert A.reported == 2
788 assert B.reported == 2
789 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700790 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700791
792 def test_failed_changes(self):
793 "Test that a change behind a failed change is retested"
794 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
795 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -0700796 A.addApproval('CRVW', 2)
797 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700798
799 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
800 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
801
James E. Blair4886cc12012-07-18 15:39:41 -0700802 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700803
804 self.waitUntilSettled()
805 jobs = self.fake_jenkins.job_history
806 assert len(jobs) > 6
807 assert A.data['status'] == 'NEW'
808 assert B.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700809 assert A.reported == 2
810 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700811 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700812
813 def test_independent_queues(self):
814 "Test that changes end up in the right queues"
815 self.fake_jenkins.hold_jobs_in_build = True
Zhongyue Luoafd39bb2012-09-21 02:00:47 +0900816 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700817 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
818 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700819 A.addApproval('CRVW', 2)
820 B.addApproval('CRVW', 2)
821 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700822
823 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
824 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
825 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
826
827 jobs = self.fake_jenkins.all_jobs
828 self.waitUntilSettled()
829
830 # There should be one merge job at the head of each queue running
831 assert len(jobs) == 2
832 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700833 assert job_has_changes(jobs[0], A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700834 assert jobs[1].name == 'project1-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700835 assert job_has_changes(jobs[1], B)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700836
837 # Release the current merge jobs
838 self.fake_jenkins.fakeRelease('.*-merge')
839 self.waitUntilSettled()
840 # Release the merge job for project2 which is behind project1
841 self.fake_jenkins.fakeRelease('.*-merge')
842 self.waitUntilSettled()
843
844 # All the test jobs should be running:
845 # project1 (3) + project2 (3) + project (2) = 8
846 assert len(jobs) == 8
847
848 self.fake_jenkins.fakeRelease()
849 self.waitUntilSettled()
850 assert len(jobs) == 0
851
852 jobs = self.fake_jenkins.job_history
853 assert len(jobs) == 11
854 assert A.data['status'] == 'MERGED'
855 assert B.data['status'] == 'MERGED'
856 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700857 assert A.reported == 2
858 assert B.reported == 2
859 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700860 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -0700861
862 def test_failed_change_at_head(self):
863 "Test that if a change at the head fails, jobs behind it are canceled"
864 self.fake_jenkins.hold_jobs_in_build = True
865
866 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
867 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
868 C = self.fake_gerrit.addFakeChange('org/project', '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. Blaird466dc42012-07-31 10:42:56 -0700872
James E. Blair4886cc12012-07-18 15:39:41 -0700873 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700874
875 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
876 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
877 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
878
879 self.waitUntilSettled()
880 jobs = self.fake_jenkins.all_jobs
881 finished_jobs = self.fake_jenkins.job_history
882
883 assert len(jobs) == 1
884 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700885 assert job_has_changes(jobs[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -0700886
887 self.fake_jenkins.fakeRelease('.*-merge')
888 self.waitUntilSettled()
889 self.fake_jenkins.fakeRelease('.*-merge')
890 self.waitUntilSettled()
891 self.fake_jenkins.fakeRelease('.*-merge')
892 self.waitUntilSettled()
893
894 assert len(jobs) == 6
895 assert jobs[0].name == 'project-test1'
896 assert jobs[1].name == 'project-test2'
897 assert jobs[2].name == 'project-test1'
898 assert jobs[3].name == 'project-test2'
899 assert jobs[4].name == 'project-test1'
900 assert jobs[5].name == 'project-test2'
901
902 jobs[0].release()
903 self.waitUntilSettled()
904
James E. Blairec590122012-08-22 15:19:31 -0700905 assert len(jobs) == 2 # project-test2, project-merge for B
James E. Blaird466dc42012-07-31 10:42:56 -0700906 assert self.countJobResults(finished_jobs, 'ABORTED') == 4
907
908 self.fake_jenkins.hold_jobs_in_build = False
909 self.fake_jenkins.fakeRelease()
910 self.waitUntilSettled()
911
912 assert len(jobs) == 0
913 assert len(finished_jobs) == 15
914 assert A.data['status'] == 'NEW'
915 assert B.data['status'] == 'MERGED'
916 assert C.data['status'] == 'MERGED'
917 assert A.reported == 2
918 assert B.reported == 2
919 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700920 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -0700921
922 def test_failed_change_at_head_with_queue(self):
923 "Test that if a change at the head fails, queued jobs are canceled"
924 self.fake_jenkins.hold_jobs_in_queue = True
925
926 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
927 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
928 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700929 A.addApproval('CRVW', 2)
930 B.addApproval('CRVW', 2)
931 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700932
James E. Blair4886cc12012-07-18 15:39:41 -0700933 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700934
935 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
936 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
937 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
938
939 self.waitUntilSettled()
940 jobs = self.fake_jenkins.all_jobs
941 finished_jobs = self.fake_jenkins.job_history
942 queue = self.fake_jenkins.queue
943
944 assert len(jobs) == 1
945 assert len(queue) == 1
946 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700947 assert job_has_changes(jobs[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -0700948
949 self.fake_jenkins.fakeRelease('.*-merge')
950 self.waitUntilSettled()
951 self.fake_jenkins.fakeRelease('.*-merge')
952 self.waitUntilSettled()
953 self.fake_jenkins.fakeRelease('.*-merge')
954 self.waitUntilSettled()
955
956 assert len(jobs) == 6
957 assert len(queue) == 6
958 assert jobs[0].name == 'project-test1'
959 assert jobs[1].name == 'project-test2'
960 assert jobs[2].name == 'project-test1'
961 assert jobs[3].name == 'project-test2'
962 assert jobs[4].name == 'project-test1'
963 assert jobs[5].name == 'project-test2'
964
965 jobs[0].release()
966 self.waitUntilSettled()
967
James E. Blairec590122012-08-22 15:19:31 -0700968 assert len(jobs) == 2 # project-test2, project-merge for B
969 assert len(queue) == 2
James E. Blaird466dc42012-07-31 10:42:56 -0700970 assert self.countJobResults(finished_jobs, 'ABORTED') == 0
971
972 self.fake_jenkins.hold_jobs_in_queue = False
973 self.fake_jenkins.fakeRelease()
974 self.waitUntilSettled()
975
976 assert len(jobs) == 0
977 assert len(finished_jobs) == 11
978 assert A.data['status'] == 'NEW'
979 assert B.data['status'] == 'MERGED'
980 assert C.data['status'] == 'MERGED'
981 assert A.reported == 2
982 assert B.reported == 2
983 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700984 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -0700985
986 def test_patch_order(self):
987 "Test that dependent patches are tested in the right order"
988 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
989 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
990 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
991 A.addApproval('CRVW', 2)
992 B.addApproval('CRVW', 2)
993 C.addApproval('CRVW', 2)
994
995 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
996 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
997 M2.setMerged()
998 M1.setMerged()
999
1000 # C -> B -> A -> M1 -> M2
1001 # M2 is here to make sure it is never queried. If it is, it
1002 # means zuul is walking down the entire history of merged
1003 # changes.
1004
1005 C.setDependsOn(B, 1)
1006 B.setDependsOn(A, 1)
1007 A.setDependsOn(M1, 1)
1008 M1.setDependsOn(M2, 1)
1009
1010 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1011
1012 self.waitUntilSettled()
1013
1014 assert A.data['status'] == 'NEW'
1015 assert B.data['status'] == 'NEW'
1016 assert C.data['status'] == 'NEW'
1017
1018 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1019 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1020
1021 self.waitUntilSettled()
1022 assert M2.queried == 0
1023 assert A.data['status'] == 'MERGED'
1024 assert B.data['status'] == 'MERGED'
1025 assert C.data['status'] == 'MERGED'
1026 assert A.reported == 2
1027 assert B.reported == 2
1028 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001029 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001030
1031 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001032 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001033 # TODO: move to test_gerrit (this is a unit test!)
1034 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4aea70c2012-07-26 14:23:24 -07001035 a = self.sched.trigger.getChange(1, 2)
1036 mgr = self.sched.pipelines['gate'].manager
1037 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001038
1039 A.addApproval('CRVW', 2)
James E. Blair4aea70c2012-07-26 14:23:24 -07001040 a = self.sched.trigger.getChange(1, 2)
1041 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001042
1043 A.addApproval('APRV', 1)
James E. Blair4aea70c2012-07-26 14:23:24 -07001044 a = self.sched.trigger.getChange(1, 2)
1045 assert self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blaire0487072012-08-29 17:38:31 -07001046 self.assertEmptyQueues()
James E. Blair4886cc12012-07-18 15:39:41 -07001047
1048 def test_build_configuration(self):
1049 "Test that zuul merges the right commits for testing"
1050 self.fake_jenkins.hold_jobs_in_queue = True
1051 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1052 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1053 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1054 A.addApproval('CRVW', 2)
1055 B.addApproval('CRVW', 2)
1056 C.addApproval('CRVW', 2)
1057 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1058 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1059 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1060 self.waitUntilSettled()
1061
1062 jobs = self.fake_jenkins.all_jobs
1063
1064 self.fake_jenkins.fakeRelease('.*-merge')
1065 self.waitUntilSettled()
1066 self.fake_jenkins.fakeRelease('.*-merge')
1067 self.waitUntilSettled()
1068 self.fake_jenkins.fakeRelease('.*-merge')
1069 self.waitUntilSettled()
James E. Blair1dbd5082012-08-23 15:12:15 -07001070
James E. Blair4886cc12012-07-18 15:39:41 -07001071 ref = jobs[-1].parameters['ZUUL_REF']
1072 self.fake_jenkins.hold_jobs_in_queue = False
1073 self.fake_jenkins.fakeRelease()
James E. Blair973721f2012-08-15 10:19:43 -07001074 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001075
James E. Blair1dbd5082012-08-23 15:12:15 -07001076 path = os.path.join(GIT_ROOT, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001077 repo = git.Repo(path)
1078 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1079 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001080 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
1081 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001082 self.assertEmptyQueues()
James E. Blair973721f2012-08-15 10:19:43 -07001083
1084 def test_build_configuration_conflict(self):
1085 "Test that merge conflicts are handled"
1086 self.fake_jenkins.hold_jobs_in_queue = True
1087 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1088 A.addPatchset(['conflict'])
1089 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1090 B.addPatchset(['conflict'])
1091 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1092 A.addApproval('CRVW', 2)
1093 B.addApproval('CRVW', 2)
1094 C.addApproval('CRVW', 2)
1095 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1096 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1097 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1098 self.waitUntilSettled()
1099
1100 jobs = self.fake_jenkins.all_jobs
1101
1102 self.fake_jenkins.fakeRelease('.*-merge')
1103 self.waitUntilSettled()
1104 self.fake_jenkins.fakeRelease('.*-merge')
1105 self.waitUntilSettled()
1106 self.fake_jenkins.fakeRelease('.*-merge')
1107 self.waitUntilSettled()
1108 ref = jobs[-1].parameters['ZUUL_REF']
1109 self.fake_jenkins.hold_jobs_in_queue = False
1110 self.fake_jenkins.fakeRelease()
1111 self.waitUntilSettled()
1112
1113 assert A.data['status'] == 'MERGED'
1114 assert B.data['status'] == 'NEW'
1115 assert C.data['status'] == 'MERGED'
1116 assert A.reported == 2
1117 assert B.reported == 2
1118 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001119 self.assertEmptyQueues()
James E. Blairdaabed22012-08-15 15:38:57 -07001120
1121 def test_post(self):
1122 "Test that post jobs run"
Zhongyue Luoafd39bb2012-09-21 02:00:47 +09001123 e = {
1124 "type": "ref-updated",
1125 "submitter": {
1126 "name": "User Name",
1127 },
1128 "refUpdate": {
1129 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1130 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1131 "refName": "master",
1132 "project": "org/project",
1133 }
1134 }
James E. Blairdaabed22012-08-15 15:38:57 -07001135 self.fake_gerrit.addEvent(e)
1136 self.waitUntilSettled()
1137
1138 jobs = self.fake_jenkins.job_history
1139 job_names = [x['name'] for x in jobs]
1140 assert len(jobs) == 1
1141 assert 'project-post' in job_names
James E. Blaire0487072012-08-29 17:38:31 -07001142 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001143
1144 def test_build_configuration_branch(self):
1145 "Test that the right commits are on alternate branches"
1146 self.fake_jenkins.hold_jobs_in_queue = True
1147 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1148 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1149 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1150 A.addApproval('CRVW', 2)
1151 B.addApproval('CRVW', 2)
1152 C.addApproval('CRVW', 2)
1153 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1154 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1155 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1156 self.waitUntilSettled()
1157
1158 jobs = self.fake_jenkins.all_jobs
1159
1160 self.fake_jenkins.fakeRelease('.*-merge')
1161 self.waitUntilSettled()
1162 self.fake_jenkins.fakeRelease('.*-merge')
1163 self.waitUntilSettled()
1164 self.fake_jenkins.fakeRelease('.*-merge')
1165 self.waitUntilSettled()
1166 ref = jobs[-1].parameters['ZUUL_REF']
1167 self.fake_jenkins.hold_jobs_in_queue = False
1168 self.fake_jenkins.fakeRelease()
1169 self.waitUntilSettled()
1170
James E. Blair1dbd5082012-08-23 15:12:15 -07001171 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001172 repo = git.Repo(path)
1173 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1174 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001175 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
1176 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001177 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001178
1179 def test_build_configuration_branch_interaction(self):
1180 "Test that switching between branches works"
1181 self.test_build_configuration()
1182 self.test_build_configuration_branch()
1183 # C has been merged, undo that
James E. Blair1dbd5082012-08-23 15:12:15 -07001184 path = os.path.join(UPSTREAM_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001185 repo = git.Repo(path)
1186 repo.heads.master.commit = repo.commit('init')
1187 self.test_build_configuration()
James E. Blaire0487072012-08-29 17:38:31 -07001188 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001189
1190 def test_build_configuration_multi_branch(self):
1191 "Test that dependent changes on multiple branches are merged"
1192 self.fake_jenkins.hold_jobs_in_queue = True
1193 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1194 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1195 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1196 A.addApproval('CRVW', 2)
1197 B.addApproval('CRVW', 2)
1198 C.addApproval('CRVW', 2)
1199 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1200 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1201 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1202 self.waitUntilSettled()
1203
1204 jobs = self.fake_jenkins.all_jobs
1205
1206 self.fake_jenkins.fakeRelease('.*-merge')
1207 self.waitUntilSettled()
1208 ref_mp = jobs[-1].parameters['ZUUL_REF']
1209 self.fake_jenkins.fakeRelease('.*-merge')
1210 self.waitUntilSettled()
1211 self.fake_jenkins.fakeRelease('.*-merge')
1212 self.waitUntilSettled()
1213 ref_master = jobs[-1].parameters['ZUUL_REF']
1214 self.fake_jenkins.hold_jobs_in_queue = False
1215 self.fake_jenkins.fakeRelease()
1216 self.waitUntilSettled()
1217
James E. Blair1dbd5082012-08-23 15:12:15 -07001218 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001219 repo = git.Repo(path)
1220
1221 repo_messages = [c.message.strip()
1222 for c in repo.iter_commits(ref_master)]
1223 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001224 correct_messages = ['initial commit', 'A-1', 'C-1']
1225 assert repo_messages == correct_messages
1226
1227 repo_messages = [c.message.strip()
1228 for c in repo.iter_commits(ref_mp)]
1229 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001230 correct_messages = ['initial commit', 'mp commit', 'B-1']
1231 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001232 self.assertEmptyQueues()
James E. Blair7f71c802012-08-22 13:04:32 -07001233
1234 def test_one_job_project(self):
1235 "Test that queueing works with one job"
1236 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1237 'master', 'A')
1238 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1239 'master', 'B')
1240 A.addApproval('CRVW', 2)
1241 B.addApproval('CRVW', 2)
1242 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1243 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1244 self.waitUntilSettled()
1245
1246 jobs = self.fake_jenkins.all_jobs
1247 finished_jobs = self.fake_jenkins.job_history
James E. Blair7f71c802012-08-22 13:04:32 -07001248
1249 assert A.data['status'] == 'MERGED'
1250 assert A.reported == 2
1251 assert B.data['status'] == 'MERGED'
1252 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001253 self.assertEmptyQueues()
James E. Blaircaec0c52012-08-22 14:52:22 -07001254
1255 def test_dependent_changes_dequeue(self):
1256 "Test that dependent patches are not needlessly tested"
1257 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1258 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1259 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1260 A.addApproval('CRVW', 2)
1261 B.addApproval('CRVW', 2)
1262 C.addApproval('CRVW', 2)
1263
1264 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1265 M1.setMerged()
1266
1267 # C -> B -> A -> M1
1268
1269 C.setDependsOn(B, 1)
1270 B.setDependsOn(A, 1)
1271 A.setDependsOn(M1, 1)
1272
1273 self.fake_jenkins.fakeAddFailTest('project-merge', A)
1274
1275 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1276 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1277 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1278
1279 self.waitUntilSettled()
1280
1281 jobs = self.fake_jenkins.all_jobs
1282 finished_jobs = self.fake_jenkins.job_history
1283
James E. Blair127bc182012-08-28 15:55:15 -07001284 for x in jobs:
1285 print x
1286 for x in finished_jobs:
1287 print x
1288
James E. Blairec590122012-08-22 15:19:31 -07001289 assert A.data['status'] == 'NEW'
1290 assert A.reported == 2
1291 assert B.data['status'] == 'NEW'
1292 assert B.reported == 2
1293 assert C.data['status'] == 'NEW'
1294 assert C.reported == 2
1295 assert len(finished_jobs) == 1
James E. Blaire0487072012-08-29 17:38:31 -07001296 self.assertEmptyQueues()
James E. Blairec590122012-08-22 15:19:31 -07001297
1298 def test_head_is_dequeued_once(self):
1299 "Test that if a change at the head fails it is dequeud only once"
1300 # If it's dequeued more than once, we should see extra
1301 # aborted jobs.
1302 self.fake_jenkins.hold_jobs_in_build = True
1303
1304 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1305 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1306 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1307 A.addApproval('CRVW', 2)
1308 B.addApproval('CRVW', 2)
1309 C.addApproval('CRVW', 2)
1310
1311 self.fake_jenkins.fakeAddFailTest('project1-test1', A)
1312 self.fake_jenkins.fakeAddFailTest('project1-test2', A)
1313 self.fake_jenkins.fakeAddFailTest('project1-project2-integration', A)
1314
1315 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1316 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1317 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1318
1319 self.waitUntilSettled()
1320 jobs = self.fake_jenkins.all_jobs
1321 finished_jobs = self.fake_jenkins.job_history
1322
1323 assert len(jobs) == 1
1324 assert jobs[0].name == 'project1-merge'
1325 assert job_has_changes(jobs[0], A)
1326
1327 self.fake_jenkins.fakeRelease('.*-merge')
1328 self.waitUntilSettled()
1329 self.fake_jenkins.fakeRelease('.*-merge')
1330 self.waitUntilSettled()
1331 self.fake_jenkins.fakeRelease('.*-merge')
1332 self.waitUntilSettled()
1333
1334 assert len(jobs) == 9
1335 assert jobs[0].name == 'project1-test1'
1336 assert jobs[1].name == 'project1-test2'
1337 assert jobs[2].name == 'project1-project2-integration'
1338 assert jobs[3].name == 'project1-test1'
1339 assert jobs[4].name == 'project1-test2'
1340 assert jobs[5].name == 'project1-project2-integration'
1341 assert jobs[6].name == 'project1-test1'
1342 assert jobs[7].name == 'project1-test2'
1343 assert jobs[8].name == 'project1-project2-integration'
1344
1345 jobs[0].release()
1346 self.waitUntilSettled()
1347
1348 assert len(jobs) == 3 # test2, integration, merge for B
1349 assert self.countJobResults(finished_jobs, 'ABORTED') == 6
1350
1351 self.fake_jenkins.hold_jobs_in_build = False
1352 self.fake_jenkins.fakeRelease()
1353 self.waitUntilSettled()
1354
1355 assert len(jobs) == 0
1356 assert len(finished_jobs) == 20
James E. Blaircaec0c52012-08-22 14:52:22 -07001357
1358 assert A.data['status'] == 'NEW'
James E. Blairec590122012-08-22 15:19:31 -07001359 assert B.data['status'] == 'MERGED'
1360 assert C.data['status'] == 'MERGED'
1361 assert A.reported == 2
1362 assert B.reported == 2
1363 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001364 self.assertEmptyQueues()
James E. Blair4ec821f2012-08-23 15:28:28 -07001365
1366 def test_nonvoting_job(self):
1367 "Test that non-voting jobs don't vote."
1368 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1369 'master', 'A')
1370 A.addApproval('CRVW', 2)
1371 self.fake_jenkins.fakeAddFailTest('nonvoting-project-test2', A)
1372 self.fake_gerrit.addEvent(A.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 A.data['status'] == 'MERGED'
1379 assert A.reported == 2
1380 assert finished_jobs[0]['result'] == 'SUCCESS'
1381 assert finished_jobs[1]['result'] == 'SUCCESS'
1382 assert finished_jobs[2]['result'] == 'FAILURE'
James E. Blaire0487072012-08-29 17:38:31 -07001383 self.assertEmptyQueues()
1384
1385 def test_check_queue_success(self):
1386 "Test successful check queue jobs."
1387 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1388 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1389
1390 self.waitUntilSettled()
1391 jobs = self.fake_jenkins.all_jobs
1392 finished_jobs = self.fake_jenkins.job_history
1393
1394 assert A.data['status'] == 'NEW'
1395 assert A.reported == 1
1396 assert finished_jobs[0]['result'] == 'SUCCESS'
1397 assert finished_jobs[1]['result'] == 'SUCCESS'
1398 assert finished_jobs[2]['result'] == 'SUCCESS'
1399 self.assertEmptyQueues()
1400
1401 def test_check_queue_failure(self):
1402 "Test failed check queue jobs."
1403 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1404 self.fake_jenkins.fakeAddFailTest('project-test2', A)
1405 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1406
1407 self.waitUntilSettled()
1408 jobs = self.fake_jenkins.all_jobs
1409 finished_jobs = self.fake_jenkins.job_history
1410
1411 assert A.data['status'] == 'NEW'
1412 assert A.reported == 1
1413 assert finished_jobs[0]['result'] == 'SUCCESS'
1414 assert finished_jobs[1]['result'] == 'SUCCESS'
1415 assert finished_jobs[2]['result'] == 'FAILURE'
1416 self.assertEmptyQueues()
James E. Blair127bc182012-08-28 15:55:15 -07001417
1418 def test_dependent_behind_dequeue(self):
1419 "test that dependent changes behind dequeued changes work"
1420 # This complicated test is a reproduction of a real life bug
1421 self.sched.reconfigure(self.config)
1422 self.fake_jenkins.hold_jobs_in_build = True
1423
1424 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1425 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1426 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1427 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1428 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1429 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1430 D.setDependsOn(C, 1)
1431 E.setDependsOn(D, 1)
1432 A.addApproval('CRVW', 2)
1433 B.addApproval('CRVW', 2)
1434 C.addApproval('CRVW', 2)
1435 D.addApproval('CRVW', 2)
1436 E.addApproval('CRVW', 2)
1437 F.addApproval('CRVW', 2)
1438
1439 A.fail_merge = True
1440 jobs = self.fake_jenkins.all_jobs
1441 finished_jobs = self.fake_jenkins.job_history
1442
1443 # Change object re-use in the gerrit trigger is hidden if
1444 # changes are added in quick succession; waiting makes it more
1445 # like real life.
1446 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1447 self.waitUntilSettled()
1448 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1449 self.waitUntilSettled()
1450
1451 self.fake_jenkins.fakeRelease('.*-merge')
1452 self.waitUntilSettled()
1453 self.fake_jenkins.fakeRelease('.*-merge')
1454 self.waitUntilSettled()
1455
1456 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1457 self.waitUntilSettled()
1458 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1459 self.waitUntilSettled()
1460 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1461 self.waitUntilSettled()
1462 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1463 self.waitUntilSettled()
1464
1465 self.fake_jenkins.fakeRelease('.*-merge')
1466 self.waitUntilSettled()
1467 self.fake_jenkins.fakeRelease('.*-merge')
1468 self.waitUntilSettled()
1469 self.fake_jenkins.fakeRelease('.*-merge')
1470 self.waitUntilSettled()
1471 self.fake_jenkins.fakeRelease('.*-merge')
1472 self.waitUntilSettled()
1473
1474 # all jobs running
1475 jobs[0].release()
1476 jobs[1].release()
1477 jobs[2].release()
1478 self.waitUntilSettled()
1479
1480 self.fake_jenkins.hold_jobs_in_build = False
1481 self.fake_jenkins.fakeRelease()
1482 self.waitUntilSettled()
1483
1484 for x in jobs:
1485 print x
1486 for x in finished_jobs:
1487 print x
1488 print self.sched.formatStatusHTML()
1489
1490 assert A.data['status'] == 'NEW'
1491 assert B.data['status'] == 'MERGED'
1492 assert C.data['status'] == 'MERGED'
1493 assert D.data['status'] == 'MERGED'
1494 assert E.data['status'] == 'MERGED'
1495 assert F.data['status'] == 'MERGED'
1496
1497 assert A.reported == 2
1498 assert B.reported == 2
1499 assert C.reported == 2
1500 assert D.reported == 2
1501 assert E.reported == 2
1502 assert F.reported == 2
1503
James E. Blair127bc182012-08-28 15:55:15 -07001504 assert self.countJobResults(finished_jobs, 'ABORTED') == 15
1505 assert len(finished_jobs) == 44
James E. Blaire0487072012-08-29 17:38:31 -07001506 self.assertEmptyQueues()
James E. Blair05fed602012-09-07 12:45:24 -07001507
1508 def test_merger_repack(self):
1509 "Test that the merger works after a repack"
1510 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1511 A.addApproval('CRVW', 2)
1512 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1513 self.waitUntilSettled()
1514 jobs = self.fake_jenkins.job_history
1515 job_names = [x['name'] for x in jobs]
1516 assert 'project-merge' in job_names
1517 assert 'project-test1' in job_names
1518 assert 'project-test2' in job_names
1519 assert jobs[0]['result'] == 'SUCCESS'
1520 assert jobs[1]['result'] == 'SUCCESS'
1521 assert jobs[2]['result'] == 'SUCCESS'
1522 assert A.data['status'] == 'MERGED'
1523 assert A.reported == 2
1524 self.assertEmptyQueues()
1525
1526 path = os.path.join(GIT_ROOT, "org/project")
1527 os.system('git --git-dir=%s/.git repack -afd' % path)
1528
1529 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1530 A.addApproval('CRVW', 2)
1531 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1532 self.waitUntilSettled()
1533 jobs = self.fake_jenkins.job_history
1534 job_names = [x['name'] for x in jobs]
1535 assert 'project-merge' in job_names
1536 assert 'project-test1' in job_names
1537 assert 'project-test2' in job_names
1538 assert jobs[0]['result'] == 'SUCCESS'
1539 assert jobs[1]['result'] == 'SUCCESS'
1540 assert jobs[2]['result'] == 'SUCCESS'
1541 assert A.data['status'] == 'MERGED'
1542 assert A.reported == 2
1543 self.assertEmptyQueues()
James E. Blair7ee88a22012-09-12 18:59:31 +02001544
1545 def test_nonexistent_job(self):
1546 "Test launching a job that doesn't exist"
1547 self.fake_jenkins.nonexistent_jobs.append('project-merge')
1548 self.jenkins.launch_retry_timeout = 0.1
1549
1550 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1551 A.addApproval('CRVW', 2)
1552 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1553 # There may be a thread about to report a lost change
1554 while A.reported < 2:
1555 self.waitUntilSettled()
1556 jobs = self.fake_jenkins.job_history
1557 job_names = [x['name'] for x in jobs]
1558 assert not job_names
1559 assert A.data['status'] == 'NEW'
1560 assert A.reported == 2
1561 self.assertEmptyQueues()
1562
1563 # Make sure things still work:
1564 self.fake_jenkins.nonexistent_jobs = []
1565 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1566 A.addApproval('CRVW', 2)
1567 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1568 self.waitUntilSettled()
1569 jobs = self.fake_jenkins.job_history
1570 job_names = [x['name'] for x in jobs]
1571 assert 'project-merge' in job_names
1572 assert 'project-test1' in job_names
1573 assert 'project-test2' in job_names
1574 assert jobs[0]['result'] == 'SUCCESS'
1575 assert jobs[1]['result'] == 'SUCCESS'
1576 assert jobs[2]['result'] == 'SUCCESS'
1577 assert A.data['status'] == 'MERGED'
1578 assert A.reported == 2
1579 self.assertEmptyQueues()