blob: cbc006ea38087767a32e2713edd1ba657ffcdc01 [file] [log] [blame]
James E. Blairb0fcae42012-07-17 11:12:10 -07001#!/usr/bin/env python
2
3# Copyright 2012 Hewlett-Packard Development Company, L.P.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
17import unittest
18import ConfigParser
19import os
20import Queue
James E. Blair8cc15a82012-08-01 11:17:57 -070021import hashlib
James E. Blairb0fcae42012-07-17 11:12:10 -070022import logging
James E. Blair8cc15a82012-08-01 11:17:57 -070023import random
James E. Blairb0fcae42012-07-17 11:12:10 -070024import json
25import threading
26import time
27import pprint
28import re
James E. Blair8cc15a82012-08-01 11:17:57 -070029import urllib2
30import urlparse
James E. Blair4886cc12012-07-18 15:39:41 -070031import shutil
James E. Blair4886f282012-11-15 09:27:33 -080032import string
James E. Blair4886cc12012-07-18 15:39:41 -070033import git
James E. Blairb0fcae42012-07-17 11:12:10 -070034
35import zuul
36import zuul.scheduler
37import zuul.launcher.jenkins
38import zuul.trigger.gerrit
39
40FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
41 'fixtures')
42CONFIG = ConfigParser.ConfigParser()
43CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
44
45CONFIG.set('zuul', 'layout_config',
46 os.path.join(FIXTURE_DIR, "layout.yaml"))
47
James E. Blair1dbd5082012-08-23 15:12:15 -070048TMP_ROOT = os.environ.get("ZUUL_TEST_ROOT", "/tmp")
49TEST_ROOT = os.path.join(TMP_ROOT, "zuul-test")
50UPSTREAM_ROOT = os.path.join(TEST_ROOT, "upstream")
51GIT_ROOT = os.path.join(TEST_ROOT, "git")
52
53CONFIG.set('zuul', 'git_dir', GIT_ROOT)
54
James E. Blairb0fcae42012-07-17 11:12:10 -070055logging.basicConfig(level=logging.DEBUG)
56
57
James E. Blair8cc15a82012-08-01 11:17:57 -070058def random_sha1():
59 return hashlib.sha1(str(random.random())).hexdigest()
60
61
James E. Blair4886cc12012-07-18 15:39:41 -070062class ChangeReference(git.Reference):
63 _common_path_default = "refs/changes"
64 _points_to_commits_only = True
65
66
67def init_repo(project):
68 parts = project.split('/')
James E. Blair1dbd5082012-08-23 15:12:15 -070069 path = os.path.join(UPSTREAM_ROOT, *parts[:-1])
James E. Blair4886cc12012-07-18 15:39:41 -070070 if not os.path.exists(path):
71 os.makedirs(path)
James E. Blair1dbd5082012-08-23 15:12:15 -070072 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -070073 repo = git.Repo.init(path)
74
75 fn = os.path.join(path, 'README')
76 f = open(fn, 'w')
77 f.write("test\n")
78 f.close()
79 repo.index.add([fn])
80 repo.index.commit('initial commit')
James E. Blairc6294a52012-08-17 10:19:48 -070081 master = repo.create_head('master')
James E. Blair4886cc12012-07-18 15:39:41 -070082 repo.create_tag('init')
83
James E. Blairc6294a52012-08-17 10:19:48 -070084 mp = repo.create_head('mp')
85 repo.head.reference = mp
86 f = open(fn, 'a')
87 f.write("test mp\n")
88 f.close()
89 repo.index.add([fn])
90 repo.index.commit('mp commit')
91
92 repo.head.reference = master
93 repo.head.reset(index=True, working_tree=True)
94 repo.git.clean('-x', '-f', '-d')
95
James E. Blair4886cc12012-07-18 15:39:41 -070096
James E. Blair4886f282012-11-15 09:27:33 -080097def add_fake_change_to_repo(project, branch, change_num, patchset, msg, fn,
98 large):
James E. Blair1dbd5082012-08-23 15:12:15 -070099 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -0700100 repo = git.Repo(path)
101 ref = ChangeReference.create(repo, '1/%s/%s' % (change_num,
102 patchset),
103 'refs/tags/init')
104 repo.head.reference = ref
105 repo.head.reset(index=True, working_tree=True)
106 repo.git.clean('-x', '-f', '-d')
107
James E. Blair1dbd5082012-08-23 15:12:15 -0700108 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair4886f282012-11-15 09:27:33 -0800109 if not large:
110 fn = os.path.join(path, fn)
111 f = open(fn, 'w')
112 f.write("test %s %s %s\n" % (branch, change_num, patchset))
113 f.close()
114 repo.index.add([fn])
115 else:
116 for fni in range(100):
117 fn = os.path.join(path, str(fni))
118 f = open(fn, 'w')
119 for ci in range(4096):
120 f.write(random.choice(string.printable))
121 f.close()
122 repo.index.add([fn])
123
James E. Blairdaabed22012-08-15 15:38:57 -0700124 return repo.index.commit(msg)
James E. Blair4886cc12012-07-18 15:39:41 -0700125
126
127def ref_has_change(ref, change):
James E. Blair1dbd5082012-08-23 15:12:15 -0700128 path = os.path.join(GIT_ROOT, change.project)
James E. Blair4886cc12012-07-18 15:39:41 -0700129 repo = git.Repo(path)
130 for commit in repo.iter_commits(ref):
131 if commit.message.strip() == ('%s-1' % change.subject):
132 return True
133 return False
134
135
136def job_has_changes(*args):
137 job = args[0]
138 commits = args[1:]
139 project = job.parameters['ZUUL_PROJECT']
James E. Blair1dbd5082012-08-23 15:12:15 -0700140 path = os.path.join(GIT_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -0700141 repo = git.Repo(path)
142 ref = job.parameters['ZUUL_REF']
James E. Blair81515ad2012-10-01 18:29:08 -0700143 sha = job.parameters['ZUUL_COMMIT']
James E. Blair4886cc12012-07-18 15:39:41 -0700144 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
James E. Blair81515ad2012-10-01 18:29:08 -0700145 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
James E. Blair4886cc12012-07-18 15:39:41 -0700146 commit_messages = ['%s-1' % commit.subject for commit in commits]
James E. Blair4886cc12012-07-18 15:39:41 -0700147 for msg in commit_messages:
148 if msg not in repo_messages:
149 return False
James E. Blair81515ad2012-10-01 18:29:08 -0700150 if repo_shas[0] != sha:
151 return False
James E. Blair4886cc12012-07-18 15:39:41 -0700152 return True
153
154
James E. Blairb0fcae42012-07-17 11:12:10 -0700155class FakeChange(object):
James E. Blair8c803f82012-07-31 16:25:42 -0700156 categories = {'APRV': ('Approved', -1, 1),
157 'CRVW': ('Code-Review', -2, 2),
158 'VRFY': ('Verified', -2, 2)}
James E. Blairb0fcae42012-07-17 11:12:10 -0700159
James E. Blair8cc15a82012-08-01 11:17:57 -0700160 def __init__(self, gerrit, number, project, branch, subject, status='NEW'):
161 self.gerrit = gerrit
James E. Blaird466dc42012-07-31 10:42:56 -0700162 self.reported = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700163 self.queried = 0
James E. Blairb0fcae42012-07-17 11:12:10 -0700164 self.patchsets = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700165 self.number = number
166 self.project = project
167 self.branch = branch
168 self.subject = subject
169 self.latest_patchset = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700170 self.depends_on_change = None
171 self.needed_by_changes = []
James E. Blair127bc182012-08-28 15:55:15 -0700172 self.fail_merge = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700173 self.data = {
174 'branch': branch,
175 'comments': [],
176 'commitMessage': subject,
177 'createdOn': time.time(),
James E. Blair8cc15a82012-08-01 11:17:57 -0700178 'id': 'I' + random_sha1(),
James E. Blairb0fcae42012-07-17 11:12:10 -0700179 'lastUpdated': time.time(),
180 'number': str(number),
181 'open': True,
182 'owner': {'email': 'user@example.com',
183 'name': 'User Name',
184 'username': 'username'},
185 'patchSets': self.patchsets,
186 'project': project,
187 'status': status,
188 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700189 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700190 'url': 'https://hostname/%s' % number}
191
192 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700193 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700194
James E. Blair4886f282012-11-15 09:27:33 -0800195 def addPatchset(self, files=None, large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700196 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700197 if files:
198 fn = files[0]
199 else:
200 fn = '%s-%s' % (self.branch, self.number)
201 msg = self.subject + '-' + str(self.latest_patchset)
202 c = add_fake_change_to_repo(self.project, self.branch,
203 self.number, self.latest_patchset,
James E. Blair4886f282012-11-15 09:27:33 -0800204 msg, fn, large)
James E. Blairb0fcae42012-07-17 11:12:10 -0700205 d = {'approvals': [],
206 'createdOn': time.time(),
207 'files': [{'file': '/COMMIT_MSG',
208 'type': 'ADDED'},
209 {'file': 'README',
210 'type': 'MODIFIED'}],
James E. Blair8c803f82012-07-31 16:25:42 -0700211 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700212 'ref': 'refs/changes/1/%s/%s' % (self.number,
213 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700214 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700215 'uploader': {'email': 'user@example.com',
216 'name': 'User name',
217 'username': 'user'}}
218 self.data['currentPatchSet'] = d
219 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700220 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700221
James E. Blaire0487072012-08-29 17:38:31 -0700222 def getPatchsetCreatedEvent(self, patchset):
223 event = {"type": "patchset-created",
224 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800225 "branch": self.branch,
226 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
227 "number": str(self.number),
228 "subject": self.subject,
229 "owner": {"name": "User Name"},
230 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700231 "patchSet": self.patchsets[patchset - 1],
232 "uploader": {"name": "User Name"}}
233 return event
234
James E. Blairb0fcae42012-07-17 11:12:10 -0700235 def addApproval(self, category, value):
James E. Blair8c803f82012-07-31 16:25:42 -0700236 approval = {'description': self.categories[category][0],
237 'type': category,
238 'value': str(value)}
239 self.patchsets[-1]['approvals'].append(approval)
240 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700241 'author': {'email': 'user@example.com',
242 'name': 'User Name',
243 'username': 'username'},
244 'change': {'branch': self.branch,
245 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
246 'number': str(self.number),
247 'owner': {'email': 'user@example.com',
248 'name': 'User Name',
249 'username': 'username'},
250 'project': self.project,
251 'subject': self.subject,
252 'topic': 'master',
253 'url': 'https://hostname/459'},
254 'comment': '',
255 'patchSet': self.patchsets[-1],
256 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700257 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700258 return json.loads(json.dumps(event))
259
James E. Blair8c803f82012-07-31 16:25:42 -0700260 def getSubmitRecords(self):
261 status = {}
262 for cat in self.categories.keys():
263 status[cat] = 0
264
265 for a in self.patchsets[-1]['approvals']:
266 cur = status[a['type']]
267 cat_min, cat_max = self.categories[a['type']][1:]
268 new = int(a['value'])
269 if new == cat_min:
270 cur = new
271 elif abs(new) > abs(cur):
272 cur = new
273 status[a['type']] = cur
274
275 labels = []
276 ok = True
277 for typ, cat in self.categories.items():
278 cur = status[typ]
279 cat_min, cat_max = cat[1:]
280 if cur == cat_min:
281 value = 'REJECT'
282 ok = False
283 elif cur == cat_max:
284 value = 'OK'
285 else:
286 value = 'NEED'
287 ok = False
288 labels.append({'label': cat[0], 'status': value})
289 if ok:
290 return [{'status': 'OK'}]
291 return [{'status': 'NOT_READY',
292 'labels': labels}]
293
294 def setDependsOn(self, other, patchset):
295 self.depends_on_change = other
296 d = {'id': other.data['id'],
297 'number': other.data['number'],
298 'ref': other.patchsets[patchset - 1]['ref']
299 }
300 self.data['dependsOn'] = [d]
301
302 other.needed_by_changes.append(self)
303 needed = other.data.get('neededBy', [])
304 d = {'id': self.data['id'],
305 'number': self.data['number'],
306 'ref': self.patchsets[patchset - 1]['ref'],
307 'revision': self.patchsets[patchset - 1]['revision']
308 }
309 needed.append(d)
310 other.data['neededBy'] = needed
311
James E. Blairb0fcae42012-07-17 11:12:10 -0700312 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700313 self.queried += 1
314 d = self.data.get('dependsOn')
315 if d:
316 d = d[0]
317 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
318 d['isCurrentPatchSet'] = True
319 else:
320 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700321 return json.loads(json.dumps(self.data))
322
323 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800324 if (self.depends_on_change and
325 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700326 return
James E. Blair127bc182012-08-28 15:55:15 -0700327 if self.fail_merge:
328 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700329 self.data['status'] = 'MERGED'
330 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700331
James E. Blair1dbd5082012-08-23 15:12:15 -0700332 path = os.path.join(UPSTREAM_ROOT, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700333 repo = git.Repo(path)
334 repo.heads[self.branch].commit = \
335 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700336
James E. Blaird466dc42012-07-31 10:42:56 -0700337 def setReported(self):
338 self.reported += 1
339
James E. Blairb0fcae42012-07-17 11:12:10 -0700340
341class FakeGerrit(object):
342 def __init__(self, *args, **kw):
343 self.event_queue = Queue.Queue()
344 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
345 self.change_number = 0
346 self.changes = {}
347
348 def addFakeChange(self, project, branch, subject):
349 self.change_number += 1
James E. Blair8cc15a82012-08-01 11:17:57 -0700350 c = FakeChange(self, self.change_number, project, branch, subject)
James E. Blairb0fcae42012-07-17 11:12:10 -0700351 self.changes[self.change_number] = c
352 return c
353
354 def addEvent(self, data):
355 return self.event_queue.put(data)
356
357 def getEvent(self):
358 return self.event_queue.get()
359
360 def eventDone(self):
361 self.event_queue.task_done()
362
363 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700364 number, ps = changeid.split(',')
365 change = self.changes[int(number)]
James E. Blairb0fcae42012-07-17 11:12:10 -0700366 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700367 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700368 if message:
369 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700370
371 def query(self, number):
372 change = self.changes[int(number)]
373 return change.query()
374
375 def startWatching(self, *args, **kw):
376 pass
377
378
379class FakeJenkinsEvent(object):
380 def __init__(self, name, number, parameters, phase, status=None):
Zhongyue Luo5d556072012-09-21 02:00:47 +0900381 data = {
382 'build': {
383 'full_url': 'https://server/job/%s/%s/' % (name, number),
384 'number': number,
385 'parameters': parameters,
386 'phase': phase,
387 'url': 'job/%s/%s/' % (name, number),
388 },
389 'name': name,
390 'url': 'job/%s/' % name,
391 }
James E. Blairb0fcae42012-07-17 11:12:10 -0700392 if status:
393 data['build']['status'] = status
394 self.body = json.dumps(data)
395
396
397class FakeJenkinsJob(threading.Thread):
398 log = logging.getLogger("zuul.test")
399
400 def __init__(self, jenkins, callback, name, number, parameters):
401 threading.Thread.__init__(self)
402 self.jenkins = jenkins
403 self.callback = callback
404 self.name = name
405 self.number = number
406 self.parameters = parameters
407 self.wait_condition = threading.Condition()
408 self.waiting = False
James E. Blaird466dc42012-07-31 10:42:56 -0700409 self.aborted = False
410 self.canceled = False
411 self.created = time.time()
James E. Blairb0fcae42012-07-17 11:12:10 -0700412
413 def release(self):
414 self.wait_condition.acquire()
415 self.wait_condition.notify()
416 self.waiting = False
417 self.log.debug("Job %s released" % (self.parameters['UUID']))
418 self.wait_condition.release()
419
420 def isWaiting(self):
421 self.wait_condition.acquire()
422 if self.waiting:
423 ret = True
424 else:
425 ret = False
426 self.wait_condition.release()
427 return ret
428
429 def _wait(self):
430 self.wait_condition.acquire()
431 self.waiting = True
432 self.log.debug("Job %s waiting" % (self.parameters['UUID']))
433 self.wait_condition.wait()
434 self.wait_condition.release()
435
436 def run(self):
437 self.jenkins.fakeEnqueue(self)
438 if self.jenkins.hold_jobs_in_queue:
439 self._wait()
440 self.jenkins.fakeDequeue(self)
James E. Blaird466dc42012-07-31 10:42:56 -0700441 if self.canceled:
442 self.jenkins.all_jobs.remove(self)
443 return
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800444 self.callback.jenkins_endpoint(FakeJenkinsEvent(self.name,
445 self.number,
446 self.parameters,
447 'STARTED'))
James E. Blairb0fcae42012-07-17 11:12:10 -0700448 if self.jenkins.hold_jobs_in_build:
449 self._wait()
450 self.log.debug("Job %s continuing" % (self.parameters['UUID']))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700451
452 result = 'SUCCESS'
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800453 if (('ZUUL_REF' in self.parameters) and
454 self.jenkins.fakeShouldFailTest(self.name,
455 self.parameters['ZUUL_REF'])):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700456 result = 'FAILURE'
James E. Blaird466dc42012-07-31 10:42:56 -0700457 if self.aborted:
458 result = 'ABORTED'
James E. Blairb02a3bb2012-07-30 17:49:55 -0700459
James E. Blairb0fcae42012-07-17 11:12:10 -0700460 self.jenkins.fakeAddHistory(name=self.name, number=self.number,
James E. Blairb02a3bb2012-07-30 17:49:55 -0700461 result=result)
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800462 self.callback.jenkins_endpoint(FakeJenkinsEvent(self.name,
463 self.number,
464 self.parameters,
465 'COMPLETED',
466 result))
467 self.callback.jenkins_endpoint(FakeJenkinsEvent(self.name,
468 self.number,
469 self.parameters,
470 'FINISHED',
471 result))
James E. Blairb0fcae42012-07-17 11:12:10 -0700472 self.jenkins.all_jobs.remove(self)
473
474
475class FakeJenkins(object):
476 log = logging.getLogger("zuul.test")
477
478 def __init__(self, *args, **kw):
479 self.queue = []
480 self.all_jobs = []
481 self.job_counter = {}
James E. Blaird466dc42012-07-31 10:42:56 -0700482 self.queue_counter = 0
James E. Blairb0fcae42012-07-17 11:12:10 -0700483 self.job_history = []
484 self.hold_jobs_in_queue = False
485 self.hold_jobs_in_build = False
James E. Blairb02a3bb2012-07-30 17:49:55 -0700486 self.fail_tests = {}
James E. Blair7ee88a22012-09-12 18:59:31 +0200487 self.nonexistent_jobs = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700488
489 def fakeEnqueue(self, job):
490 self.queue.append(job)
491
492 def fakeDequeue(self, job):
493 self.queue.remove(job)
494
495 def fakeAddHistory(self, **kw):
496 self.job_history.append(kw)
497
498 def fakeRelease(self, regex=None):
499 all_jobs = self.all_jobs[:]
500 self.log.debug("releasing jobs %s (%s)" % (regex, len(self.all_jobs)))
501 for job in all_jobs:
502 if not regex or re.match(regex, job.name):
503 self.log.debug("releasing job %s" % (job.parameters['UUID']))
504 job.release()
505 else:
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800506 self.log.debug("not releasing job %s" %
507 (job.parameters['UUID']))
James E. Blairb0fcae42012-07-17 11:12:10 -0700508 self.log.debug("done releasing jobs %s (%s)" % (regex,
509 len(self.all_jobs)))
510
511 def fakeAllWaiting(self, regex=None):
James E. Blair4aa1ad62012-10-05 12:39:26 -0700512 all_jobs = self.all_jobs[:] + self.queue[:]
James E. Blairb0fcae42012-07-17 11:12:10 -0700513 for job in all_jobs:
514 self.log.debug("job %s %s" % (job.parameters['UUID'],
515 job.isWaiting()))
516 if not job.isWaiting():
517 return False
518 return True
519
James E. Blairb02a3bb2012-07-30 17:49:55 -0700520 def fakeAddFailTest(self, name, change):
521 l = self.fail_tests.get(name, [])
522 l.append(change)
523 self.fail_tests[name] = l
524
James E. Blair4886cc12012-07-18 15:39:41 -0700525 def fakeShouldFailTest(self, name, ref):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700526 l = self.fail_tests.get(name, [])
527 for change in l:
James E. Blair4886cc12012-07-18 15:39:41 -0700528 if ref_has_change(ref, change):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700529 return True
530 return False
531
James E. Blairb0fcae42012-07-17 11:12:10 -0700532 def build_job(self, name, parameters):
James E. Blair7ee88a22012-09-12 18:59:31 +0200533 if name in self.nonexistent_jobs:
534 raise Exception("Job does not exist")
James E. Blairb0fcae42012-07-17 11:12:10 -0700535 count = self.job_counter.get(name, 0)
536 count += 1
537 self.job_counter[name] = count
James E. Blaird466dc42012-07-31 10:42:56 -0700538
539 queue_count = self.queue_counter
540 self.queue_counter += 1
James E. Blairb0fcae42012-07-17 11:12:10 -0700541 job = FakeJenkinsJob(self, self.callback, name, count, parameters)
James E. Blaird466dc42012-07-31 10:42:56 -0700542 job.queue_id = queue_count
543
James E. Blairb0fcae42012-07-17 11:12:10 -0700544 self.all_jobs.append(job)
545 job.start()
546
James E. Blaird466dc42012-07-31 10:42:56 -0700547 def stop_build(self, name, number):
548 for job in self.all_jobs:
549 if job.name == name and job.number == number:
550 job.aborted = True
551 job.release()
552 return
553
554 def cancel_queue(self, id):
555 for job in self.queue:
556 if job.queue_id == id:
557 job.canceled = True
558 job.release()
559 return
560
561 def get_queue_info(self):
562 items = []
563 for job in self.queue:
564 paramstr = ''
565 paramlst = []
566 d = {'actions': [{'parameters': paramlst},
567 {'causes': [{'shortDescription':
568 'Started by user Jenkins',
569 'userId': 'jenkins',
570 'userName': 'Jenkins'}]}],
571 'blocked': False,
572 'buildable': True,
573 'buildableStartMilliseconds': (job.created * 1000) + 5,
574 'id': job.queue_id,
575 'inQueueSince': (job.created * 1000),
576 'params': paramstr,
577 'stuck': False,
578 'task': {'color': 'blue',
579 'name': job.name,
580 'url': 'https://server/job/%s/' % job.name},
581 'why': 'Waiting for next available executor'}
582 for k, v in job.parameters.items():
583 paramstr += "\n(StringParameterValue) %s='%s'" % (k, v)
584 pd = {'name': k, 'value': v}
585 paramlst.append(pd)
586 items.append(d)
587 return items
588
James E. Blairb0fcae42012-07-17 11:12:10 -0700589 def set_build_description(self, *args, **kw):
590 pass
591
592
593class FakeJenkinsCallback(zuul.launcher.jenkins.JenkinsCallback):
594 def start(self):
595 pass
596
597
James E. Blair8cc15a82012-08-01 11:17:57 -0700598class FakeURLOpener(object):
599 def __init__(self, fake_gerrit, url):
600 self.fake_gerrit = fake_gerrit
601 self.url = url
602
603 def read(self):
604 res = urlparse.urlparse(self.url)
605 path = res.path
606 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200607 ret = '001e# service=git-upload-pack\n'
608 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
609 'multi_ack thin-pack side-band side-band-64k ofs-delta '
610 'shallow no-progress include-tag multi_ack_detailed no-done\n')
James E. Blair1dbd5082012-08-23 15:12:15 -0700611 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700612 repo = git.Repo(path)
613 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200614 r = ref.object.hexsha + ' ' + ref.path + '\n'
615 ret += '%04x%s' % (len(r) + 4, r)
616 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700617 return ret
618
619
James E. Blair4886cc12012-07-18 15:39:41 -0700620class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
621 def getGitUrl(self, project):
James E. Blair1dbd5082012-08-23 15:12:15 -0700622 return os.path.join(UPSTREAM_ROOT, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700623
624
James E. Blairb0fcae42012-07-17 11:12:10 -0700625class testScheduler(unittest.TestCase):
626 log = logging.getLogger("zuul.test")
627
628 def setUp(self):
James E. Blair1dbd5082012-08-23 15:12:15 -0700629 if os.path.exists(TEST_ROOT):
630 shutil.rmtree(TEST_ROOT)
631 os.makedirs(TEST_ROOT)
632 os.makedirs(UPSTREAM_ROOT)
633 os.makedirs(GIT_ROOT)
James E. Blair4886cc12012-07-18 15:39:41 -0700634
635 # For each project in config:
636 init_repo("org/project")
637 init_repo("org/project1")
638 init_repo("org/project2")
James E. Blair127bc182012-08-28 15:55:15 -0700639 init_repo("org/project3")
James E. Blair7f71c802012-08-22 13:04:32 -0700640 init_repo("org/one-job-project")
James E. Blair4ec821f2012-08-23 15:28:28 -0700641 init_repo("org/nonvoting-project")
James E. Blairb0fcae42012-07-17 11:12:10 -0700642 self.config = CONFIG
643 self.sched = zuul.scheduler.Scheduler()
644
645 def jenkinsFactory(*args, **kw):
646 self.fake_jenkins = FakeJenkins()
647 return self.fake_jenkins
648
649 def jenkinsCallbackFactory(*args, **kw):
650 self.fake_jenkins_callback = FakeJenkinsCallback(*args, **kw)
651 return self.fake_jenkins_callback
652
James E. Blair8cc15a82012-08-01 11:17:57 -0700653 def URLOpenerFactory(*args, **kw):
654 args = [self.fake_gerrit] + list(args)
655 return FakeURLOpener(*args, **kw)
656
James E. Blairb0fcae42012-07-17 11:12:10 -0700657 zuul.launcher.jenkins.ExtendedJenkins = jenkinsFactory
658 zuul.launcher.jenkins.JenkinsCallback = jenkinsCallbackFactory
James E. Blair8cc15a82012-08-01 11:17:57 -0700659 urllib2.urlopen = URLOpenerFactory
James E. Blairb0fcae42012-07-17 11:12:10 -0700660 self.jenkins = zuul.launcher.jenkins.Jenkins(self.config, self.sched)
661 self.fake_jenkins.callback = self.fake_jenkins_callback
662
663 zuul.lib.gerrit.Gerrit = FakeGerrit
664
James E. Blair4886cc12012-07-18 15:39:41 -0700665 self.gerrit = FakeGerritTrigger(self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700666 self.gerrit.replication_timeout = 1.5
667 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700668 self.fake_gerrit = self.gerrit.gerrit
669
670 self.sched.setLauncher(self.jenkins)
671 self.sched.setTrigger(self.gerrit)
672
673 self.sched.start()
674 self.sched.reconfigure(self.config)
675 self.sched.resume()
676
677 def tearDown(self):
678 self.jenkins.stop()
679 self.gerrit.stop()
680 self.sched.stop()
681 self.sched.join()
James E. Blair1dbd5082012-08-23 15:12:15 -0700682 #shutil.rmtree(TEST_ROOT)
James E. Blairb0fcae42012-07-17 11:12:10 -0700683
684 def waitUntilSettled(self):
685 self.log.debug("Waiting until settled...")
686 start = time.time()
687 while True:
688 if time.time() - start > 10:
689 print 'queue status:',
690 print self.sched.trigger_event_queue.empty(),
691 print self.sched.result_event_queue.empty(),
692 print self.fake_gerrit.event_queue.empty(),
693 raise Exception("Timeout waiting for Zuul to settle")
694 self.fake_gerrit.event_queue.join()
695 self.sched.queue_lock.acquire()
696 if (self.sched.trigger_event_queue.empty() and
697 self.sched.result_event_queue.empty() and
698 self.fake_gerrit.event_queue.empty() and
699 self.fake_jenkins.fakeAllWaiting()):
700 self.sched.queue_lock.release()
701 self.log.debug("...settled.")
702 return
703 self.sched.queue_lock.release()
704 self.sched.wake_event.wait(0.1)
705
James E. Blaird466dc42012-07-31 10:42:56 -0700706 def countJobResults(self, jobs, result):
707 jobs = filter(lambda x: x['result'] == result, jobs)
708 return len(jobs)
709
James E. Blaire0487072012-08-29 17:38:31 -0700710 def assertEmptyQueues(self):
711 # Make sure there are no orphaned jobs
712 for pipeline in self.sched.pipelines.values():
713 for queue in pipeline.queues:
714 if len(queue.queue) != 0:
715 print 'queue', queue.queue
716 assert len(queue.queue) == 0
717 if len(queue.severed_heads) != 0:
718 print 'heads', queue.severed_heads
719 assert len(queue.severed_heads) == 0
720
James E. Blairb0fcae42012-07-17 11:12:10 -0700721 def test_jobs_launched(self):
722 "Test that jobs are launched and a change is merged"
723 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -0700724 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700725 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
726 self.waitUntilSettled()
727 jobs = self.fake_jenkins.job_history
728 job_names = [x['name'] for x in jobs]
729 assert 'project-merge' in job_names
730 assert 'project-test1' in job_names
731 assert 'project-test2' in job_names
732 assert jobs[0]['result'] == 'SUCCESS'
733 assert jobs[1]['result'] == 'SUCCESS'
734 assert jobs[2]['result'] == 'SUCCESS'
735 assert A.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700736 assert A.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700737 self.assertEmptyQueues()
James E. Blairb0fcae42012-07-17 11:12:10 -0700738
739 def test_parallel_changes(self):
740 "Test that changes are tested in parallel and merged in series"
741 self.fake_jenkins.hold_jobs_in_build = True
742 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
743 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
744 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700745 A.addApproval('CRVW', 2)
746 B.addApproval('CRVW', 2)
747 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700748
749 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
750 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
751 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
752
753 self.waitUntilSettled()
754 jobs = self.fake_jenkins.all_jobs
755 assert len(jobs) == 1
756 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700757 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700758
759 self.fake_jenkins.fakeRelease('.*-merge')
760 self.waitUntilSettled()
761 assert len(jobs) == 3
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 assert jobs[2].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700767 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700768
769 self.fake_jenkins.fakeRelease('.*-merge')
770 self.waitUntilSettled()
771 assert len(jobs) == 5
772 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700773 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700774 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700775 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700776
777 assert jobs[2].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700778 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700779 assert jobs[3].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700780 assert job_has_changes(jobs[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700781
782 assert jobs[4].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700783 assert job_has_changes(jobs[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700784
785 self.fake_jenkins.fakeRelease('.*-merge')
786 self.waitUntilSettled()
787 assert len(jobs) == 6
788 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700789 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700790 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700791 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700792
793 assert jobs[2].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700794 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700795 assert jobs[3].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700796 assert job_has_changes(jobs[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700797
798 assert jobs[4].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700799 assert job_has_changes(jobs[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700800 assert jobs[5].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700801 assert job_has_changes(jobs[5], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700802
803 self.fake_jenkins.hold_jobs_in_build = False
804 self.fake_jenkins.fakeRelease()
805 self.waitUntilSettled()
806 assert len(jobs) == 0
807
808 jobs = self.fake_jenkins.job_history
809 assert len(jobs) == 9
810 assert A.data['status'] == 'MERGED'
811 assert B.data['status'] == 'MERGED'
812 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700813 assert A.reported == 2
814 assert B.reported == 2
815 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700816 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700817
818 def test_failed_changes(self):
819 "Test that a change behind a failed change is retested"
820 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
821 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -0700822 A.addApproval('CRVW', 2)
823 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700824
825 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
826 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
827
James E. Blair4886cc12012-07-18 15:39:41 -0700828 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700829
830 self.waitUntilSettled()
831 jobs = self.fake_jenkins.job_history
832 assert len(jobs) > 6
833 assert A.data['status'] == 'NEW'
834 assert B.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700835 assert A.reported == 2
836 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700837 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700838
839 def test_independent_queues(self):
840 "Test that changes end up in the right queues"
841 self.fake_jenkins.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900842 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700843 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
844 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700845 A.addApproval('CRVW', 2)
846 B.addApproval('CRVW', 2)
847 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700848
849 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
850 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
851 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
852
853 jobs = self.fake_jenkins.all_jobs
854 self.waitUntilSettled()
855
856 # There should be one merge job at the head of each queue running
857 assert len(jobs) == 2
858 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700859 assert job_has_changes(jobs[0], A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700860 assert jobs[1].name == 'project1-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700861 assert job_has_changes(jobs[1], B)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700862
863 # Release the current merge jobs
864 self.fake_jenkins.fakeRelease('.*-merge')
865 self.waitUntilSettled()
866 # Release the merge job for project2 which is behind project1
867 self.fake_jenkins.fakeRelease('.*-merge')
868 self.waitUntilSettled()
869
870 # All the test jobs should be running:
871 # project1 (3) + project2 (3) + project (2) = 8
872 assert len(jobs) == 8
873
874 self.fake_jenkins.fakeRelease()
875 self.waitUntilSettled()
876 assert len(jobs) == 0
877
878 jobs = self.fake_jenkins.job_history
879 assert len(jobs) == 11
880 assert A.data['status'] == 'MERGED'
881 assert B.data['status'] == 'MERGED'
882 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700883 assert A.reported == 2
884 assert B.reported == 2
885 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700886 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -0700887
888 def test_failed_change_at_head(self):
889 "Test that if a change at the head fails, jobs behind it are canceled"
890 self.fake_jenkins.hold_jobs_in_build = True
891
892 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
893 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
894 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700895 A.addApproval('CRVW', 2)
896 B.addApproval('CRVW', 2)
897 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700898
James E. Blair4886cc12012-07-18 15:39:41 -0700899 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700900
901 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
902 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
903 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
904
905 self.waitUntilSettled()
906 jobs = self.fake_jenkins.all_jobs
907 finished_jobs = self.fake_jenkins.job_history
908
909 assert len(jobs) == 1
910 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700911 assert job_has_changes(jobs[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -0700912
913 self.fake_jenkins.fakeRelease('.*-merge')
914 self.waitUntilSettled()
915 self.fake_jenkins.fakeRelease('.*-merge')
916 self.waitUntilSettled()
917 self.fake_jenkins.fakeRelease('.*-merge')
918 self.waitUntilSettled()
919
920 assert len(jobs) == 6
921 assert jobs[0].name == 'project-test1'
922 assert jobs[1].name == 'project-test2'
923 assert jobs[2].name == 'project-test1'
924 assert jobs[3].name == 'project-test2'
925 assert jobs[4].name == 'project-test1'
926 assert jobs[5].name == 'project-test2'
927
928 jobs[0].release()
929 self.waitUntilSettled()
930
James E. Blairec590122012-08-22 15:19:31 -0700931 assert len(jobs) == 2 # project-test2, project-merge for B
James E. Blaird466dc42012-07-31 10:42:56 -0700932 assert self.countJobResults(finished_jobs, 'ABORTED') == 4
933
934 self.fake_jenkins.hold_jobs_in_build = False
935 self.fake_jenkins.fakeRelease()
936 self.waitUntilSettled()
937
938 assert len(jobs) == 0
939 assert len(finished_jobs) == 15
940 assert A.data['status'] == 'NEW'
941 assert B.data['status'] == 'MERGED'
942 assert C.data['status'] == 'MERGED'
943 assert A.reported == 2
944 assert B.reported == 2
945 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700946 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -0700947
948 def test_failed_change_at_head_with_queue(self):
949 "Test that if a change at the head fails, queued jobs are canceled"
950 self.fake_jenkins.hold_jobs_in_queue = True
951
952 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
953 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
954 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700955 A.addApproval('CRVW', 2)
956 B.addApproval('CRVW', 2)
957 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700958
James E. Blair4886cc12012-07-18 15:39:41 -0700959 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700960
961 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
962 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
963 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
964
965 self.waitUntilSettled()
966 jobs = self.fake_jenkins.all_jobs
967 finished_jobs = self.fake_jenkins.job_history
968 queue = self.fake_jenkins.queue
969
970 assert len(jobs) == 1
971 assert len(queue) == 1
972 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700973 assert job_has_changes(jobs[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -0700974
975 self.fake_jenkins.fakeRelease('.*-merge')
976 self.waitUntilSettled()
977 self.fake_jenkins.fakeRelease('.*-merge')
978 self.waitUntilSettled()
979 self.fake_jenkins.fakeRelease('.*-merge')
980 self.waitUntilSettled()
981
982 assert len(jobs) == 6
983 assert len(queue) == 6
984 assert jobs[0].name == 'project-test1'
985 assert jobs[1].name == 'project-test2'
986 assert jobs[2].name == 'project-test1'
987 assert jobs[3].name == 'project-test2'
988 assert jobs[4].name == 'project-test1'
989 assert jobs[5].name == 'project-test2'
990
991 jobs[0].release()
992 self.waitUntilSettled()
993
James E. Blairec590122012-08-22 15:19:31 -0700994 assert len(jobs) == 2 # project-test2, project-merge for B
995 assert len(queue) == 2
James E. Blaird466dc42012-07-31 10:42:56 -0700996 assert self.countJobResults(finished_jobs, 'ABORTED') == 0
997
998 self.fake_jenkins.hold_jobs_in_queue = False
999 self.fake_jenkins.fakeRelease()
1000 self.waitUntilSettled()
1001
1002 assert len(jobs) == 0
1003 assert len(finished_jobs) == 11
1004 assert A.data['status'] == 'NEW'
1005 assert B.data['status'] == 'MERGED'
1006 assert C.data['status'] == 'MERGED'
1007 assert A.reported == 2
1008 assert B.reported == 2
1009 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001010 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001011
1012 def test_patch_order(self):
1013 "Test that dependent patches are tested in the right order"
1014 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1015 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1016 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1017 A.addApproval('CRVW', 2)
1018 B.addApproval('CRVW', 2)
1019 C.addApproval('CRVW', 2)
1020
1021 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1022 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1023 M2.setMerged()
1024 M1.setMerged()
1025
1026 # C -> B -> A -> M1 -> M2
1027 # M2 is here to make sure it is never queried. If it is, it
1028 # means zuul is walking down the entire history of merged
1029 # changes.
1030
1031 C.setDependsOn(B, 1)
1032 B.setDependsOn(A, 1)
1033 A.setDependsOn(M1, 1)
1034 M1.setDependsOn(M2, 1)
1035
1036 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1037
1038 self.waitUntilSettled()
1039
1040 assert A.data['status'] == 'NEW'
1041 assert B.data['status'] == 'NEW'
1042 assert C.data['status'] == 'NEW'
1043
1044 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1045 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1046
1047 self.waitUntilSettled()
1048 assert M2.queried == 0
1049 assert A.data['status'] == 'MERGED'
1050 assert B.data['status'] == 'MERGED'
1051 assert C.data['status'] == 'MERGED'
1052 assert A.reported == 2
1053 assert B.reported == 2
1054 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001055 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001056
1057 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001058 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001059 # TODO: move to test_gerrit (this is a unit test!)
1060 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4aea70c2012-07-26 14:23:24 -07001061 a = self.sched.trigger.getChange(1, 2)
1062 mgr = self.sched.pipelines['gate'].manager
1063 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001064
1065 A.addApproval('CRVW', 2)
James E. Blair4aea70c2012-07-26 14:23:24 -07001066 a = self.sched.trigger.getChange(1, 2)
1067 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001068
1069 A.addApproval('APRV', 1)
James E. Blair4aea70c2012-07-26 14:23:24 -07001070 a = self.sched.trigger.getChange(1, 2)
1071 assert self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blaire0487072012-08-29 17:38:31 -07001072 self.assertEmptyQueues()
James E. Blair4886cc12012-07-18 15:39:41 -07001073
1074 def test_build_configuration(self):
1075 "Test that zuul merges the right commits for testing"
1076 self.fake_jenkins.hold_jobs_in_queue = True
1077 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1078 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1079 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1080 A.addApproval('CRVW', 2)
1081 B.addApproval('CRVW', 2)
1082 C.addApproval('CRVW', 2)
1083 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1084 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1085 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1086 self.waitUntilSettled()
1087
1088 jobs = self.fake_jenkins.all_jobs
1089
1090 self.fake_jenkins.fakeRelease('.*-merge')
1091 self.waitUntilSettled()
1092 self.fake_jenkins.fakeRelease('.*-merge')
1093 self.waitUntilSettled()
1094 self.fake_jenkins.fakeRelease('.*-merge')
1095 self.waitUntilSettled()
James E. Blair1dbd5082012-08-23 15:12:15 -07001096
James E. Blair4886cc12012-07-18 15:39:41 -07001097 ref = jobs[-1].parameters['ZUUL_REF']
1098 self.fake_jenkins.hold_jobs_in_queue = False
1099 self.fake_jenkins.fakeRelease()
James E. Blair973721f2012-08-15 10:19:43 -07001100 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001101
James E. Blair1dbd5082012-08-23 15:12:15 -07001102 path = os.path.join(GIT_ROOT, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001103 repo = git.Repo(path)
1104 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1105 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001106 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
1107 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001108 self.assertEmptyQueues()
James E. Blair973721f2012-08-15 10:19:43 -07001109
1110 def test_build_configuration_conflict(self):
1111 "Test that merge conflicts are handled"
1112 self.fake_jenkins.hold_jobs_in_queue = True
1113 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1114 A.addPatchset(['conflict'])
1115 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1116 B.addPatchset(['conflict'])
1117 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1118 A.addApproval('CRVW', 2)
1119 B.addApproval('CRVW', 2)
1120 C.addApproval('CRVW', 2)
1121 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1122 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1123 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1124 self.waitUntilSettled()
1125
1126 jobs = self.fake_jenkins.all_jobs
1127
1128 self.fake_jenkins.fakeRelease('.*-merge')
1129 self.waitUntilSettled()
1130 self.fake_jenkins.fakeRelease('.*-merge')
1131 self.waitUntilSettled()
1132 self.fake_jenkins.fakeRelease('.*-merge')
1133 self.waitUntilSettled()
1134 ref = jobs[-1].parameters['ZUUL_REF']
1135 self.fake_jenkins.hold_jobs_in_queue = False
1136 self.fake_jenkins.fakeRelease()
1137 self.waitUntilSettled()
1138
1139 assert A.data['status'] == 'MERGED'
1140 assert B.data['status'] == 'NEW'
1141 assert C.data['status'] == 'MERGED'
1142 assert A.reported == 2
1143 assert B.reported == 2
1144 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001145 self.assertEmptyQueues()
James E. Blairdaabed22012-08-15 15:38:57 -07001146
1147 def test_post(self):
1148 "Test that post jobs run"
Zhongyue Luo5d556072012-09-21 02:00:47 +09001149 e = {
1150 "type": "ref-updated",
1151 "submitter": {
1152 "name": "User Name",
1153 },
1154 "refUpdate": {
1155 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1156 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1157 "refName": "master",
1158 "project": "org/project",
1159 }
1160 }
James E. Blairdaabed22012-08-15 15:38:57 -07001161 self.fake_gerrit.addEvent(e)
1162 self.waitUntilSettled()
1163
1164 jobs = self.fake_jenkins.job_history
1165 job_names = [x['name'] for x in jobs]
1166 assert len(jobs) == 1
1167 assert 'project-post' in job_names
James E. Blaire0487072012-08-29 17:38:31 -07001168 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001169
1170 def test_build_configuration_branch(self):
1171 "Test that the right commits are on alternate branches"
1172 self.fake_jenkins.hold_jobs_in_queue = True
1173 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1174 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1175 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1176 A.addApproval('CRVW', 2)
1177 B.addApproval('CRVW', 2)
1178 C.addApproval('CRVW', 2)
1179 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1180 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1181 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1182 self.waitUntilSettled()
1183
1184 jobs = self.fake_jenkins.all_jobs
1185
1186 self.fake_jenkins.fakeRelease('.*-merge')
1187 self.waitUntilSettled()
1188 self.fake_jenkins.fakeRelease('.*-merge')
1189 self.waitUntilSettled()
1190 self.fake_jenkins.fakeRelease('.*-merge')
1191 self.waitUntilSettled()
1192 ref = jobs[-1].parameters['ZUUL_REF']
1193 self.fake_jenkins.hold_jobs_in_queue = False
1194 self.fake_jenkins.fakeRelease()
1195 self.waitUntilSettled()
1196
James E. Blair1dbd5082012-08-23 15:12:15 -07001197 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001198 repo = git.Repo(path)
1199 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1200 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001201 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
1202 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001203 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001204
1205 def test_build_configuration_branch_interaction(self):
1206 "Test that switching between branches works"
1207 self.test_build_configuration()
1208 self.test_build_configuration_branch()
1209 # C has been merged, undo that
James E. Blair1dbd5082012-08-23 15:12:15 -07001210 path = os.path.join(UPSTREAM_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001211 repo = git.Repo(path)
1212 repo.heads.master.commit = repo.commit('init')
1213 self.test_build_configuration()
James E. Blaire0487072012-08-29 17:38:31 -07001214 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001215
1216 def test_build_configuration_multi_branch(self):
1217 "Test that dependent changes on multiple branches are merged"
1218 self.fake_jenkins.hold_jobs_in_queue = True
1219 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1220 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1221 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1222 A.addApproval('CRVW', 2)
1223 B.addApproval('CRVW', 2)
1224 C.addApproval('CRVW', 2)
1225 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1226 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1227 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1228 self.waitUntilSettled()
1229
1230 jobs = self.fake_jenkins.all_jobs
1231
1232 self.fake_jenkins.fakeRelease('.*-merge')
1233 self.waitUntilSettled()
1234 ref_mp = jobs[-1].parameters['ZUUL_REF']
1235 self.fake_jenkins.fakeRelease('.*-merge')
1236 self.waitUntilSettled()
1237 self.fake_jenkins.fakeRelease('.*-merge')
1238 self.waitUntilSettled()
1239 ref_master = jobs[-1].parameters['ZUUL_REF']
1240 self.fake_jenkins.hold_jobs_in_queue = False
1241 self.fake_jenkins.fakeRelease()
1242 self.waitUntilSettled()
1243
James E. Blair1dbd5082012-08-23 15:12:15 -07001244 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001245 repo = git.Repo(path)
1246
1247 repo_messages = [c.message.strip()
1248 for c in repo.iter_commits(ref_master)]
1249 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001250 correct_messages = ['initial commit', 'A-1', 'C-1']
1251 assert repo_messages == correct_messages
1252
1253 repo_messages = [c.message.strip()
1254 for c in repo.iter_commits(ref_mp)]
1255 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001256 correct_messages = ['initial commit', 'mp commit', 'B-1']
1257 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001258 self.assertEmptyQueues()
James E. Blair7f71c802012-08-22 13:04:32 -07001259
1260 def test_one_job_project(self):
1261 "Test that queueing works with one job"
1262 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1263 'master', 'A')
1264 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1265 'master', 'B')
1266 A.addApproval('CRVW', 2)
1267 B.addApproval('CRVW', 2)
1268 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1269 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1270 self.waitUntilSettled()
1271
1272 jobs = self.fake_jenkins.all_jobs
1273 finished_jobs = self.fake_jenkins.job_history
James E. Blair7f71c802012-08-22 13:04:32 -07001274
1275 assert A.data['status'] == 'MERGED'
1276 assert A.reported == 2
1277 assert B.data['status'] == 'MERGED'
1278 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001279 self.assertEmptyQueues()
James E. Blaircaec0c52012-08-22 14:52:22 -07001280
1281 def test_dependent_changes_dequeue(self):
1282 "Test that dependent patches are not needlessly tested"
1283 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1284 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1285 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1286 A.addApproval('CRVW', 2)
1287 B.addApproval('CRVW', 2)
1288 C.addApproval('CRVW', 2)
1289
1290 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1291 M1.setMerged()
1292
1293 # C -> B -> A -> M1
1294
1295 C.setDependsOn(B, 1)
1296 B.setDependsOn(A, 1)
1297 A.setDependsOn(M1, 1)
1298
1299 self.fake_jenkins.fakeAddFailTest('project-merge', A)
1300
1301 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1302 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1303 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1304
1305 self.waitUntilSettled()
1306
1307 jobs = self.fake_jenkins.all_jobs
1308 finished_jobs = self.fake_jenkins.job_history
1309
James E. Blair127bc182012-08-28 15:55:15 -07001310 for x in jobs:
1311 print x
1312 for x in finished_jobs:
1313 print x
1314
James E. Blairec590122012-08-22 15:19:31 -07001315 assert A.data['status'] == 'NEW'
1316 assert A.reported == 2
1317 assert B.data['status'] == 'NEW'
1318 assert B.reported == 2
1319 assert C.data['status'] == 'NEW'
1320 assert C.reported == 2
1321 assert len(finished_jobs) == 1
James E. Blaire0487072012-08-29 17:38:31 -07001322 self.assertEmptyQueues()
James E. Blairec590122012-08-22 15:19:31 -07001323
1324 def test_head_is_dequeued_once(self):
1325 "Test that if a change at the head fails it is dequeud only once"
1326 # If it's dequeued more than once, we should see extra
1327 # aborted jobs.
1328 self.fake_jenkins.hold_jobs_in_build = True
1329
1330 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1331 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1332 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1333 A.addApproval('CRVW', 2)
1334 B.addApproval('CRVW', 2)
1335 C.addApproval('CRVW', 2)
1336
1337 self.fake_jenkins.fakeAddFailTest('project1-test1', A)
1338 self.fake_jenkins.fakeAddFailTest('project1-test2', A)
1339 self.fake_jenkins.fakeAddFailTest('project1-project2-integration', A)
1340
1341 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1342 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1343 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1344
1345 self.waitUntilSettled()
1346 jobs = self.fake_jenkins.all_jobs
1347 finished_jobs = self.fake_jenkins.job_history
1348
1349 assert len(jobs) == 1
1350 assert jobs[0].name == 'project1-merge'
1351 assert job_has_changes(jobs[0], A)
1352
1353 self.fake_jenkins.fakeRelease('.*-merge')
1354 self.waitUntilSettled()
1355 self.fake_jenkins.fakeRelease('.*-merge')
1356 self.waitUntilSettled()
1357 self.fake_jenkins.fakeRelease('.*-merge')
1358 self.waitUntilSettled()
1359
1360 assert len(jobs) == 9
1361 assert jobs[0].name == 'project1-test1'
1362 assert jobs[1].name == 'project1-test2'
1363 assert jobs[2].name == 'project1-project2-integration'
1364 assert jobs[3].name == 'project1-test1'
1365 assert jobs[4].name == 'project1-test2'
1366 assert jobs[5].name == 'project1-project2-integration'
1367 assert jobs[6].name == 'project1-test1'
1368 assert jobs[7].name == 'project1-test2'
1369 assert jobs[8].name == 'project1-project2-integration'
1370
1371 jobs[0].release()
1372 self.waitUntilSettled()
1373
1374 assert len(jobs) == 3 # test2, integration, merge for B
1375 assert self.countJobResults(finished_jobs, 'ABORTED') == 6
1376
1377 self.fake_jenkins.hold_jobs_in_build = False
1378 self.fake_jenkins.fakeRelease()
1379 self.waitUntilSettled()
1380
1381 assert len(jobs) == 0
1382 assert len(finished_jobs) == 20
James E. Blaircaec0c52012-08-22 14:52:22 -07001383
1384 assert A.data['status'] == 'NEW'
James E. Blairec590122012-08-22 15:19:31 -07001385 assert B.data['status'] == 'MERGED'
1386 assert C.data['status'] == 'MERGED'
1387 assert A.reported == 2
1388 assert B.reported == 2
1389 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001390 self.assertEmptyQueues()
James E. Blair4ec821f2012-08-23 15:28:28 -07001391
1392 def test_nonvoting_job(self):
1393 "Test that non-voting jobs don't vote."
1394 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1395 'master', 'A')
1396 A.addApproval('CRVW', 2)
1397 self.fake_jenkins.fakeAddFailTest('nonvoting-project-test2', A)
1398 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1399
1400 self.waitUntilSettled()
1401 jobs = self.fake_jenkins.all_jobs
1402 finished_jobs = self.fake_jenkins.job_history
1403
1404 assert A.data['status'] == 'MERGED'
1405 assert A.reported == 2
1406 assert finished_jobs[0]['result'] == 'SUCCESS'
1407 assert finished_jobs[1]['result'] == 'SUCCESS'
1408 assert finished_jobs[2]['result'] == 'FAILURE'
James E. Blaire0487072012-08-29 17:38:31 -07001409 self.assertEmptyQueues()
1410
1411 def test_check_queue_success(self):
1412 "Test successful check queue jobs."
1413 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1414 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1415
1416 self.waitUntilSettled()
1417 jobs = self.fake_jenkins.all_jobs
1418 finished_jobs = self.fake_jenkins.job_history
1419
1420 assert A.data['status'] == 'NEW'
1421 assert A.reported == 1
1422 assert finished_jobs[0]['result'] == 'SUCCESS'
1423 assert finished_jobs[1]['result'] == 'SUCCESS'
1424 assert finished_jobs[2]['result'] == 'SUCCESS'
1425 self.assertEmptyQueues()
1426
1427 def test_check_queue_failure(self):
1428 "Test failed check queue jobs."
1429 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1430 self.fake_jenkins.fakeAddFailTest('project-test2', A)
1431 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1432
1433 self.waitUntilSettled()
1434 jobs = self.fake_jenkins.all_jobs
1435 finished_jobs = self.fake_jenkins.job_history
1436
1437 assert A.data['status'] == 'NEW'
1438 assert A.reported == 1
1439 assert finished_jobs[0]['result'] == 'SUCCESS'
1440 assert finished_jobs[1]['result'] == 'SUCCESS'
1441 assert finished_jobs[2]['result'] == 'FAILURE'
1442 self.assertEmptyQueues()
James E. Blair127bc182012-08-28 15:55:15 -07001443
1444 def test_dependent_behind_dequeue(self):
1445 "test that dependent changes behind dequeued changes work"
1446 # This complicated test is a reproduction of a real life bug
1447 self.sched.reconfigure(self.config)
1448 self.fake_jenkins.hold_jobs_in_build = True
1449
1450 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1451 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1452 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1453 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1454 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1455 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1456 D.setDependsOn(C, 1)
1457 E.setDependsOn(D, 1)
1458 A.addApproval('CRVW', 2)
1459 B.addApproval('CRVW', 2)
1460 C.addApproval('CRVW', 2)
1461 D.addApproval('CRVW', 2)
1462 E.addApproval('CRVW', 2)
1463 F.addApproval('CRVW', 2)
1464
1465 A.fail_merge = True
1466 jobs = self.fake_jenkins.all_jobs
1467 finished_jobs = self.fake_jenkins.job_history
1468
1469 # Change object re-use in the gerrit trigger is hidden if
1470 # changes are added in quick succession; waiting makes it more
1471 # like real life.
1472 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1473 self.waitUntilSettled()
1474 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1475 self.waitUntilSettled()
1476
1477 self.fake_jenkins.fakeRelease('.*-merge')
1478 self.waitUntilSettled()
1479 self.fake_jenkins.fakeRelease('.*-merge')
1480 self.waitUntilSettled()
1481
1482 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1483 self.waitUntilSettled()
1484 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1485 self.waitUntilSettled()
1486 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1487 self.waitUntilSettled()
1488 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1489 self.waitUntilSettled()
1490
1491 self.fake_jenkins.fakeRelease('.*-merge')
1492 self.waitUntilSettled()
1493 self.fake_jenkins.fakeRelease('.*-merge')
1494 self.waitUntilSettled()
1495 self.fake_jenkins.fakeRelease('.*-merge')
1496 self.waitUntilSettled()
1497 self.fake_jenkins.fakeRelease('.*-merge')
1498 self.waitUntilSettled()
1499
James E. Blair4aa1ad62012-10-05 12:39:26 -07001500 for x in jobs:
1501 print x
James E. Blair127bc182012-08-28 15:55:15 -07001502 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001503
1504 # Grab pointers to the jobs we want to release before
1505 # releasing any, because list indexes may change as
1506 # the jobs complete.
1507 a, b, c = jobs[:3]
1508 a.release()
1509 b.release()
1510 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001511 self.waitUntilSettled()
1512
1513 self.fake_jenkins.hold_jobs_in_build = False
1514 self.fake_jenkins.fakeRelease()
1515 self.waitUntilSettled()
1516
1517 for x in jobs:
1518 print x
1519 for x in finished_jobs:
1520 print x
1521 print self.sched.formatStatusHTML()
1522
1523 assert A.data['status'] == 'NEW'
1524 assert B.data['status'] == 'MERGED'
1525 assert C.data['status'] == 'MERGED'
1526 assert D.data['status'] == 'MERGED'
1527 assert E.data['status'] == 'MERGED'
1528 assert F.data['status'] == 'MERGED'
1529
1530 assert A.reported == 2
1531 assert B.reported == 2
1532 assert C.reported == 2
1533 assert D.reported == 2
1534 assert E.reported == 2
1535 assert F.reported == 2
1536
James E. Blair127bc182012-08-28 15:55:15 -07001537 assert self.countJobResults(finished_jobs, 'ABORTED') == 15
1538 assert len(finished_jobs) == 44
James E. Blaire0487072012-08-29 17:38:31 -07001539 self.assertEmptyQueues()
James E. Blair05fed602012-09-07 12:45:24 -07001540
1541 def test_merger_repack(self):
1542 "Test that the merger works after a repack"
1543 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1544 A.addApproval('CRVW', 2)
1545 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1546 self.waitUntilSettled()
1547 jobs = self.fake_jenkins.job_history
1548 job_names = [x['name'] for x in jobs]
1549 assert 'project-merge' in job_names
1550 assert 'project-test1' in job_names
1551 assert 'project-test2' in job_names
1552 assert jobs[0]['result'] == 'SUCCESS'
1553 assert jobs[1]['result'] == 'SUCCESS'
1554 assert jobs[2]['result'] == 'SUCCESS'
1555 assert A.data['status'] == 'MERGED'
1556 assert A.reported == 2
1557 self.assertEmptyQueues()
1558
1559 path = os.path.join(GIT_ROOT, "org/project")
1560 os.system('git --git-dir=%s/.git repack -afd' % path)
1561
1562 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1563 A.addApproval('CRVW', 2)
1564 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1565 self.waitUntilSettled()
1566 jobs = self.fake_jenkins.job_history
1567 job_names = [x['name'] for x in jobs]
1568 assert 'project-merge' in job_names
1569 assert 'project-test1' in job_names
1570 assert 'project-test2' in job_names
1571 assert jobs[0]['result'] == 'SUCCESS'
1572 assert jobs[1]['result'] == 'SUCCESS'
1573 assert jobs[2]['result'] == 'SUCCESS'
1574 assert A.data['status'] == 'MERGED'
1575 assert A.reported == 2
1576 self.assertEmptyQueues()
James E. Blair7ee88a22012-09-12 18:59:31 +02001577
James E. Blair4886f282012-11-15 09:27:33 -08001578 def test_merger_repack_large_change(self):
1579 "Test that the merger works with large changes after a repack"
1580 # https://bugs.launchpad.net/zuul/+bug/1078946
1581 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1582 A.addPatchset(large=True)
1583 path = os.path.join(UPSTREAM_ROOT, "org/project1")
1584 os.system('git --git-dir=%s/.git repack -afd' % path)
1585 path = os.path.join(GIT_ROOT, "org/project1")
1586 os.system('git --git-dir=%s/.git repack -afd' % path)
1587
1588 A.addApproval('CRVW', 2)
1589 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1590 self.waitUntilSettled()
1591 jobs = self.fake_jenkins.job_history
1592 job_names = [x['name'] for x in jobs]
1593 assert 'project1-merge' in job_names
1594 assert 'project1-test1' in job_names
1595 assert 'project1-test2' in job_names
1596 assert jobs[0]['result'] == 'SUCCESS'
1597 assert jobs[1]['result'] == 'SUCCESS'
1598 assert jobs[2]['result'] == 'SUCCESS'
1599 assert A.data['status'] == 'MERGED'
1600 assert A.reported == 2
1601 self.assertEmptyQueues()
1602
James E. Blair7ee88a22012-09-12 18:59:31 +02001603 def test_nonexistent_job(self):
1604 "Test launching a job that doesn't exist"
1605 self.fake_jenkins.nonexistent_jobs.append('project-merge')
1606 self.jenkins.launch_retry_timeout = 0.1
1607
1608 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1609 A.addApproval('CRVW', 2)
1610 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1611 # There may be a thread about to report a lost change
1612 while A.reported < 2:
1613 self.waitUntilSettled()
1614 jobs = self.fake_jenkins.job_history
1615 job_names = [x['name'] for x in jobs]
1616 assert not job_names
1617 assert A.data['status'] == 'NEW'
1618 assert A.reported == 2
1619 self.assertEmptyQueues()
1620
1621 # Make sure things still work:
1622 self.fake_jenkins.nonexistent_jobs = []
1623 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1624 A.addApproval('CRVW', 2)
1625 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1626 self.waitUntilSettled()
1627 jobs = self.fake_jenkins.job_history
1628 job_names = [x['name'] for x in jobs]
1629 assert 'project-merge' in job_names
1630 assert 'project-test1' in job_names
1631 assert 'project-test2' in job_names
1632 assert jobs[0]['result'] == 'SUCCESS'
1633 assert jobs[1]['result'] == 'SUCCESS'
1634 assert jobs[2]['result'] == 'SUCCESS'
1635 assert A.data['status'] == 'MERGED'
1636 assert A.reported == 2
1637 self.assertEmptyQueues()