blob: 20ad4ec3b083232e9b3c919de2746f0380b70805 [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)
James E. Blairff791972013-01-09 11:45:43 -0800462 self.jenkins.lock.acquire()
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800463 self.callback.jenkins_endpoint(FakeJenkinsEvent(self.name,
464 self.number,
465 self.parameters,
466 'COMPLETED',
467 result))
468 self.callback.jenkins_endpoint(FakeJenkinsEvent(self.name,
469 self.number,
470 self.parameters,
471 'FINISHED',
472 result))
James E. Blairb0fcae42012-07-17 11:12:10 -0700473 self.jenkins.all_jobs.remove(self)
James E. Blairff791972013-01-09 11:45:43 -0800474 self.jenkins.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700475
476
477class FakeJenkins(object):
478 log = logging.getLogger("zuul.test")
479
480 def __init__(self, *args, **kw):
481 self.queue = []
482 self.all_jobs = []
483 self.job_counter = {}
James E. Blaird466dc42012-07-31 10:42:56 -0700484 self.queue_counter = 0
James E. Blairb0fcae42012-07-17 11:12:10 -0700485 self.job_history = []
486 self.hold_jobs_in_queue = False
487 self.hold_jobs_in_build = False
James E. Blairb02a3bb2012-07-30 17:49:55 -0700488 self.fail_tests = {}
James E. Blair7ee88a22012-09-12 18:59:31 +0200489 self.nonexistent_jobs = []
James E. Blairff791972013-01-09 11:45:43 -0800490 self.lock = threading.Lock()
James E. Blairb0fcae42012-07-17 11:12:10 -0700491
492 def fakeEnqueue(self, job):
493 self.queue.append(job)
494
495 def fakeDequeue(self, job):
496 self.queue.remove(job)
497
498 def fakeAddHistory(self, **kw):
499 self.job_history.append(kw)
500
501 def fakeRelease(self, regex=None):
502 all_jobs = self.all_jobs[:]
503 self.log.debug("releasing jobs %s (%s)" % (regex, len(self.all_jobs)))
504 for job in all_jobs:
505 if not regex or re.match(regex, job.name):
506 self.log.debug("releasing job %s" % (job.parameters['UUID']))
507 job.release()
508 else:
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800509 self.log.debug("not releasing job %s" %
510 (job.parameters['UUID']))
James E. Blairb0fcae42012-07-17 11:12:10 -0700511 self.log.debug("done releasing jobs %s (%s)" % (regex,
512 len(self.all_jobs)))
513
514 def fakeAllWaiting(self, regex=None):
James E. Blair4aa1ad62012-10-05 12:39:26 -0700515 all_jobs = self.all_jobs[:] + self.queue[:]
James E. Blairb0fcae42012-07-17 11:12:10 -0700516 for job in all_jobs:
517 self.log.debug("job %s %s" % (job.parameters['UUID'],
518 job.isWaiting()))
519 if not job.isWaiting():
520 return False
521 return True
522
James E. Blairb02a3bb2012-07-30 17:49:55 -0700523 def fakeAddFailTest(self, name, change):
524 l = self.fail_tests.get(name, [])
525 l.append(change)
526 self.fail_tests[name] = l
527
James E. Blair4886cc12012-07-18 15:39:41 -0700528 def fakeShouldFailTest(self, name, ref):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700529 l = self.fail_tests.get(name, [])
530 for change in l:
James E. Blair4886cc12012-07-18 15:39:41 -0700531 if ref_has_change(ref, change):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700532 return True
533 return False
534
James E. Blairb0fcae42012-07-17 11:12:10 -0700535 def build_job(self, name, parameters):
James E. Blair7ee88a22012-09-12 18:59:31 +0200536 if name in self.nonexistent_jobs:
537 raise Exception("Job does not exist")
James E. Blairb0fcae42012-07-17 11:12:10 -0700538 count = self.job_counter.get(name, 0)
539 count += 1
540 self.job_counter[name] = count
James E. Blaird466dc42012-07-31 10:42:56 -0700541
542 queue_count = self.queue_counter
543 self.queue_counter += 1
James E. Blairb0fcae42012-07-17 11:12:10 -0700544 job = FakeJenkinsJob(self, self.callback, name, count, parameters)
James E. Blaird466dc42012-07-31 10:42:56 -0700545 job.queue_id = queue_count
546
James E. Blairb0fcae42012-07-17 11:12:10 -0700547 self.all_jobs.append(job)
548 job.start()
549
James E. Blaird466dc42012-07-31 10:42:56 -0700550 def stop_build(self, name, number):
551 for job in self.all_jobs:
552 if job.name == name and job.number == number:
553 job.aborted = True
554 job.release()
555 return
556
557 def cancel_queue(self, id):
558 for job in self.queue:
559 if job.queue_id == id:
560 job.canceled = True
561 job.release()
562 return
563
564 def get_queue_info(self):
565 items = []
566 for job in self.queue:
567 paramstr = ''
568 paramlst = []
569 d = {'actions': [{'parameters': paramlst},
570 {'causes': [{'shortDescription':
571 'Started by user Jenkins',
572 'userId': 'jenkins',
573 'userName': 'Jenkins'}]}],
574 'blocked': False,
575 'buildable': True,
576 'buildableStartMilliseconds': (job.created * 1000) + 5,
577 'id': job.queue_id,
578 'inQueueSince': (job.created * 1000),
579 'params': paramstr,
580 'stuck': False,
581 'task': {'color': 'blue',
582 'name': job.name,
583 'url': 'https://server/job/%s/' % job.name},
584 'why': 'Waiting for next available executor'}
585 for k, v in job.parameters.items():
586 paramstr += "\n(StringParameterValue) %s='%s'" % (k, v)
587 pd = {'name': k, 'value': v}
588 paramlst.append(pd)
589 items.append(d)
590 return items
591
James E. Blairb0fcae42012-07-17 11:12:10 -0700592 def set_build_description(self, *args, **kw):
593 pass
594
595
596class FakeJenkinsCallback(zuul.launcher.jenkins.JenkinsCallback):
597 def start(self):
598 pass
599
600
James E. Blair8cc15a82012-08-01 11:17:57 -0700601class FakeURLOpener(object):
602 def __init__(self, fake_gerrit, url):
603 self.fake_gerrit = fake_gerrit
604 self.url = url
605
606 def read(self):
607 res = urlparse.urlparse(self.url)
608 path = res.path
609 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200610 ret = '001e# service=git-upload-pack\n'
611 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
612 'multi_ack thin-pack side-band side-band-64k ofs-delta '
613 'shallow no-progress include-tag multi_ack_detailed no-done\n')
James E. Blair1dbd5082012-08-23 15:12:15 -0700614 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700615 repo = git.Repo(path)
616 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200617 r = ref.object.hexsha + ' ' + ref.path + '\n'
618 ret += '%04x%s' % (len(r) + 4, r)
619 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700620 return ret
621
622
James E. Blair4886cc12012-07-18 15:39:41 -0700623class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
624 def getGitUrl(self, project):
James E. Blair1dbd5082012-08-23 15:12:15 -0700625 return os.path.join(UPSTREAM_ROOT, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700626
627
James E. Blairb0fcae42012-07-17 11:12:10 -0700628class testScheduler(unittest.TestCase):
629 log = logging.getLogger("zuul.test")
630
631 def setUp(self):
James E. Blair1dbd5082012-08-23 15:12:15 -0700632 if os.path.exists(TEST_ROOT):
633 shutil.rmtree(TEST_ROOT)
634 os.makedirs(TEST_ROOT)
635 os.makedirs(UPSTREAM_ROOT)
636 os.makedirs(GIT_ROOT)
James E. Blair4886cc12012-07-18 15:39:41 -0700637
638 # For each project in config:
639 init_repo("org/project")
640 init_repo("org/project1")
641 init_repo("org/project2")
James E. Blair127bc182012-08-28 15:55:15 -0700642 init_repo("org/project3")
James E. Blair7f71c802012-08-22 13:04:32 -0700643 init_repo("org/one-job-project")
James E. Blair4ec821f2012-08-23 15:28:28 -0700644 init_repo("org/nonvoting-project")
James E. Blairb0fcae42012-07-17 11:12:10 -0700645 self.config = CONFIG
646 self.sched = zuul.scheduler.Scheduler()
647
648 def jenkinsFactory(*args, **kw):
649 self.fake_jenkins = FakeJenkins()
650 return self.fake_jenkins
651
652 def jenkinsCallbackFactory(*args, **kw):
653 self.fake_jenkins_callback = FakeJenkinsCallback(*args, **kw)
654 return self.fake_jenkins_callback
655
James E. Blair8cc15a82012-08-01 11:17:57 -0700656 def URLOpenerFactory(*args, **kw):
657 args = [self.fake_gerrit] + list(args)
658 return FakeURLOpener(*args, **kw)
659
James E. Blairb0fcae42012-07-17 11:12:10 -0700660 zuul.launcher.jenkins.ExtendedJenkins = jenkinsFactory
661 zuul.launcher.jenkins.JenkinsCallback = jenkinsCallbackFactory
James E. Blair8cc15a82012-08-01 11:17:57 -0700662 urllib2.urlopen = URLOpenerFactory
James E. Blairb0fcae42012-07-17 11:12:10 -0700663 self.jenkins = zuul.launcher.jenkins.Jenkins(self.config, self.sched)
664 self.fake_jenkins.callback = self.fake_jenkins_callback
665
666 zuul.lib.gerrit.Gerrit = FakeGerrit
667
James E. Blair4886cc12012-07-18 15:39:41 -0700668 self.gerrit = FakeGerritTrigger(self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700669 self.gerrit.replication_timeout = 1.5
670 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700671 self.fake_gerrit = self.gerrit.gerrit
672
673 self.sched.setLauncher(self.jenkins)
674 self.sched.setTrigger(self.gerrit)
675
676 self.sched.start()
677 self.sched.reconfigure(self.config)
678 self.sched.resume()
679
680 def tearDown(self):
681 self.jenkins.stop()
682 self.gerrit.stop()
683 self.sched.stop()
684 self.sched.join()
James E. Blair1dbd5082012-08-23 15:12:15 -0700685 #shutil.rmtree(TEST_ROOT)
James E. Blairb0fcae42012-07-17 11:12:10 -0700686
687 def waitUntilSettled(self):
688 self.log.debug("Waiting until settled...")
689 start = time.time()
690 while True:
691 if time.time() - start > 10:
692 print 'queue status:',
693 print self.sched.trigger_event_queue.empty(),
694 print self.sched.result_event_queue.empty(),
695 print self.fake_gerrit.event_queue.empty(),
696 raise Exception("Timeout waiting for Zuul to settle")
James E. Blairff791972013-01-09 11:45:43 -0800697 # Make sure our fake jenkins doesn't end any jobs
698 # (and therefore, emit events) while we're checking
699 self.fake_jenkins.lock.acquire()
700 # Join ensures that the queue is empty _and_ events have been
701 # processed
James E. Blairb0fcae42012-07-17 11:12:10 -0700702 self.fake_gerrit.event_queue.join()
James E. Blairff791972013-01-09 11:45:43 -0800703 self.sched.trigger_event_queue.join()
704 self.sched.result_event_queue.join()
James E. Blairb0fcae42012-07-17 11:12:10 -0700705 if (self.sched.trigger_event_queue.empty() and
706 self.sched.result_event_queue.empty() and
707 self.fake_gerrit.event_queue.empty() and
708 self.fake_jenkins.fakeAllWaiting()):
James E. Blairff791972013-01-09 11:45:43 -0800709 self.fake_jenkins.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700710 self.log.debug("...settled.")
711 return
James E. Blairff791972013-01-09 11:45:43 -0800712 self.fake_jenkins.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700713 self.sched.wake_event.wait(0.1)
714
James E. Blaird466dc42012-07-31 10:42:56 -0700715 def countJobResults(self, jobs, result):
716 jobs = filter(lambda x: x['result'] == result, jobs)
717 return len(jobs)
718
James E. Blaire0487072012-08-29 17:38:31 -0700719 def assertEmptyQueues(self):
720 # Make sure there are no orphaned jobs
721 for pipeline in self.sched.pipelines.values():
722 for queue in pipeline.queues:
723 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -0800724 print 'pipeline %s queue %s contents %s' % (
725 pipeline.name, queue.name, queue.queue)
James E. Blaire0487072012-08-29 17:38:31 -0700726 assert len(queue.queue) == 0
727 if len(queue.severed_heads) != 0:
728 print 'heads', queue.severed_heads
729 assert len(queue.severed_heads) == 0
730
James E. Blairb0fcae42012-07-17 11:12:10 -0700731 def test_jobs_launched(self):
732 "Test that jobs are launched and a change is merged"
733 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -0700734 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700735 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
736 self.waitUntilSettled()
737 jobs = self.fake_jenkins.job_history
738 job_names = [x['name'] for x in jobs]
739 assert 'project-merge' in job_names
740 assert 'project-test1' in job_names
741 assert 'project-test2' in job_names
742 assert jobs[0]['result'] == 'SUCCESS'
743 assert jobs[1]['result'] == 'SUCCESS'
744 assert jobs[2]['result'] == 'SUCCESS'
745 assert A.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700746 assert A.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700747 self.assertEmptyQueues()
James E. Blairb0fcae42012-07-17 11:12:10 -0700748
749 def test_parallel_changes(self):
750 "Test that changes are tested in parallel and merged in series"
751 self.fake_jenkins.hold_jobs_in_build = True
752 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
753 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
754 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700755 A.addApproval('CRVW', 2)
756 B.addApproval('CRVW', 2)
757 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700758
759 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
760 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
761 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
762
763 self.waitUntilSettled()
764 jobs = self.fake_jenkins.all_jobs
765 assert len(jobs) == 1
766 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700767 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700768
769 self.fake_jenkins.fakeRelease('.*-merge')
770 self.waitUntilSettled()
771 assert len(jobs) == 3
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 assert jobs[2].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700777 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700778
779 self.fake_jenkins.fakeRelease('.*-merge')
780 self.waitUntilSettled()
781 assert len(jobs) == 5
782 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700783 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700784 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700785 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700786
787 assert jobs[2].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700788 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700789 assert jobs[3].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700790 assert job_has_changes(jobs[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700791
792 assert jobs[4].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700793 assert job_has_changes(jobs[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700794
795 self.fake_jenkins.fakeRelease('.*-merge')
796 self.waitUntilSettled()
797 assert len(jobs) == 6
798 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700799 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700800 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700801 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700802
803 assert jobs[2].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700804 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700805 assert jobs[3].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700806 assert job_has_changes(jobs[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700807
808 assert jobs[4].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700809 assert job_has_changes(jobs[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700810 assert jobs[5].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700811 assert job_has_changes(jobs[5], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700812
813 self.fake_jenkins.hold_jobs_in_build = False
814 self.fake_jenkins.fakeRelease()
815 self.waitUntilSettled()
816 assert len(jobs) == 0
817
818 jobs = self.fake_jenkins.job_history
819 assert len(jobs) == 9
820 assert A.data['status'] == 'MERGED'
821 assert B.data['status'] == 'MERGED'
822 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700823 assert A.reported == 2
824 assert B.reported == 2
825 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700826 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700827
828 def test_failed_changes(self):
829 "Test that a change behind a failed change is retested"
830 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
831 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -0700832 A.addApproval('CRVW', 2)
833 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700834
835 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
836 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
837
James E. Blair4886cc12012-07-18 15:39:41 -0700838 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700839
840 self.waitUntilSettled()
841 jobs = self.fake_jenkins.job_history
842 assert len(jobs) > 6
843 assert A.data['status'] == 'NEW'
844 assert B.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700845 assert A.reported == 2
846 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700847 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700848
849 def test_independent_queues(self):
850 "Test that changes end up in the right queues"
851 self.fake_jenkins.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900852 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700853 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
854 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700855 A.addApproval('CRVW', 2)
856 B.addApproval('CRVW', 2)
857 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700858
859 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
860 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
861 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
862
863 jobs = self.fake_jenkins.all_jobs
864 self.waitUntilSettled()
865
866 # There should be one merge job at the head of each queue running
867 assert len(jobs) == 2
868 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700869 assert job_has_changes(jobs[0], A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700870 assert jobs[1].name == 'project1-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700871 assert job_has_changes(jobs[1], B)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700872
873 # Release the current merge jobs
874 self.fake_jenkins.fakeRelease('.*-merge')
875 self.waitUntilSettled()
876 # Release the merge job for project2 which is behind project1
877 self.fake_jenkins.fakeRelease('.*-merge')
878 self.waitUntilSettled()
879
880 # All the test jobs should be running:
881 # project1 (3) + project2 (3) + project (2) = 8
882 assert len(jobs) == 8
883
884 self.fake_jenkins.fakeRelease()
885 self.waitUntilSettled()
886 assert len(jobs) == 0
887
888 jobs = self.fake_jenkins.job_history
889 assert len(jobs) == 11
890 assert A.data['status'] == 'MERGED'
891 assert B.data['status'] == 'MERGED'
892 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700893 assert A.reported == 2
894 assert B.reported == 2
895 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700896 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -0700897
898 def test_failed_change_at_head(self):
899 "Test that if a change at the head fails, jobs behind it are canceled"
900 self.fake_jenkins.hold_jobs_in_build = True
901
902 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
903 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
904 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700905 A.addApproval('CRVW', 2)
906 B.addApproval('CRVW', 2)
907 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700908
James E. Blair4886cc12012-07-18 15:39:41 -0700909 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700910
911 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
912 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
913 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
914
915 self.waitUntilSettled()
916 jobs = self.fake_jenkins.all_jobs
917 finished_jobs = self.fake_jenkins.job_history
918
919 assert len(jobs) == 1
920 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700921 assert job_has_changes(jobs[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -0700922
923 self.fake_jenkins.fakeRelease('.*-merge')
924 self.waitUntilSettled()
925 self.fake_jenkins.fakeRelease('.*-merge')
926 self.waitUntilSettled()
927 self.fake_jenkins.fakeRelease('.*-merge')
928 self.waitUntilSettled()
929
930 assert len(jobs) == 6
931 assert jobs[0].name == 'project-test1'
932 assert jobs[1].name == 'project-test2'
933 assert jobs[2].name == 'project-test1'
934 assert jobs[3].name == 'project-test2'
935 assert jobs[4].name == 'project-test1'
936 assert jobs[5].name == 'project-test2'
937
938 jobs[0].release()
939 self.waitUntilSettled()
940
James E. Blairec590122012-08-22 15:19:31 -0700941 assert len(jobs) == 2 # project-test2, project-merge for B
James E. Blaird466dc42012-07-31 10:42:56 -0700942 assert self.countJobResults(finished_jobs, 'ABORTED') == 4
943
944 self.fake_jenkins.hold_jobs_in_build = False
945 self.fake_jenkins.fakeRelease()
946 self.waitUntilSettled()
947
948 assert len(jobs) == 0
949 assert len(finished_jobs) == 15
950 assert A.data['status'] == 'NEW'
951 assert B.data['status'] == 'MERGED'
952 assert C.data['status'] == 'MERGED'
953 assert A.reported == 2
954 assert B.reported == 2
955 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700956 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -0700957
958 def test_failed_change_at_head_with_queue(self):
959 "Test that if a change at the head fails, queued jobs are canceled"
960 self.fake_jenkins.hold_jobs_in_queue = True
961
962 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
963 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
964 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700965 A.addApproval('CRVW', 2)
966 B.addApproval('CRVW', 2)
967 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700968
James E. Blair4886cc12012-07-18 15:39:41 -0700969 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700970
971 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
972 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
973 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
974
975 self.waitUntilSettled()
976 jobs = self.fake_jenkins.all_jobs
977 finished_jobs = self.fake_jenkins.job_history
978 queue = self.fake_jenkins.queue
979
980 assert len(jobs) == 1
981 assert len(queue) == 1
982 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700983 assert job_has_changes(jobs[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -0700984
985 self.fake_jenkins.fakeRelease('.*-merge')
986 self.waitUntilSettled()
987 self.fake_jenkins.fakeRelease('.*-merge')
988 self.waitUntilSettled()
989 self.fake_jenkins.fakeRelease('.*-merge')
990 self.waitUntilSettled()
991
992 assert len(jobs) == 6
993 assert len(queue) == 6
994 assert jobs[0].name == 'project-test1'
995 assert jobs[1].name == 'project-test2'
996 assert jobs[2].name == 'project-test1'
997 assert jobs[3].name == 'project-test2'
998 assert jobs[4].name == 'project-test1'
999 assert jobs[5].name == 'project-test2'
1000
1001 jobs[0].release()
1002 self.waitUntilSettled()
1003
James E. Blairec590122012-08-22 15:19:31 -07001004 assert len(jobs) == 2 # project-test2, project-merge for B
1005 assert len(queue) == 2
James E. Blaird466dc42012-07-31 10:42:56 -07001006 assert self.countJobResults(finished_jobs, 'ABORTED') == 0
1007
1008 self.fake_jenkins.hold_jobs_in_queue = False
1009 self.fake_jenkins.fakeRelease()
1010 self.waitUntilSettled()
1011
1012 assert len(jobs) == 0
1013 assert len(finished_jobs) == 11
1014 assert A.data['status'] == 'NEW'
1015 assert B.data['status'] == 'MERGED'
1016 assert C.data['status'] == 'MERGED'
1017 assert A.reported == 2
1018 assert B.reported == 2
1019 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001020 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001021
1022 def test_patch_order(self):
1023 "Test that dependent patches are tested in the right order"
1024 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1025 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1026 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1027 A.addApproval('CRVW', 2)
1028 B.addApproval('CRVW', 2)
1029 C.addApproval('CRVW', 2)
1030
1031 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1032 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1033 M2.setMerged()
1034 M1.setMerged()
1035
1036 # C -> B -> A -> M1 -> M2
1037 # M2 is here to make sure it is never queried. If it is, it
1038 # means zuul is walking down the entire history of merged
1039 # changes.
1040
1041 C.setDependsOn(B, 1)
1042 B.setDependsOn(A, 1)
1043 A.setDependsOn(M1, 1)
1044 M1.setDependsOn(M2, 1)
1045
1046 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1047
1048 self.waitUntilSettled()
1049
1050 assert A.data['status'] == 'NEW'
1051 assert B.data['status'] == 'NEW'
1052 assert C.data['status'] == 'NEW'
1053
1054 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1055 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1056
1057 self.waitUntilSettled()
1058 assert M2.queried == 0
1059 assert A.data['status'] == 'MERGED'
1060 assert B.data['status'] == 'MERGED'
1061 assert C.data['status'] == 'MERGED'
1062 assert A.reported == 2
1063 assert B.reported == 2
1064 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001065 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001066
1067 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001068 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001069 # TODO: move to test_gerrit (this is a unit test!)
1070 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4aea70c2012-07-26 14:23:24 -07001071 a = self.sched.trigger.getChange(1, 2)
1072 mgr = self.sched.pipelines['gate'].manager
1073 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001074
1075 A.addApproval('CRVW', 2)
James E. Blair4aea70c2012-07-26 14:23:24 -07001076 a = self.sched.trigger.getChange(1, 2)
1077 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001078
1079 A.addApproval('APRV', 1)
James E. Blair4aea70c2012-07-26 14:23:24 -07001080 a = self.sched.trigger.getChange(1, 2)
1081 assert self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blaire0487072012-08-29 17:38:31 -07001082 self.assertEmptyQueues()
James E. Blair4886cc12012-07-18 15:39:41 -07001083
1084 def test_build_configuration(self):
1085 "Test that zuul merges the right commits for testing"
1086 self.fake_jenkins.hold_jobs_in_queue = True
1087 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1088 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1089 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1090 A.addApproval('CRVW', 2)
1091 B.addApproval('CRVW', 2)
1092 C.addApproval('CRVW', 2)
1093 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1094 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1095 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1096 self.waitUntilSettled()
1097
1098 jobs = self.fake_jenkins.all_jobs
1099
1100 self.fake_jenkins.fakeRelease('.*-merge')
1101 self.waitUntilSettled()
1102 self.fake_jenkins.fakeRelease('.*-merge')
1103 self.waitUntilSettled()
1104 self.fake_jenkins.fakeRelease('.*-merge')
1105 self.waitUntilSettled()
James E. Blair1dbd5082012-08-23 15:12:15 -07001106
James E. Blair4886cc12012-07-18 15:39:41 -07001107 ref = jobs[-1].parameters['ZUUL_REF']
1108 self.fake_jenkins.hold_jobs_in_queue = False
1109 self.fake_jenkins.fakeRelease()
James E. Blair973721f2012-08-15 10:19:43 -07001110 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001111
James E. Blair1dbd5082012-08-23 15:12:15 -07001112 path = os.path.join(GIT_ROOT, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001113 repo = git.Repo(path)
1114 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1115 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001116 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
1117 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001118 self.assertEmptyQueues()
James E. Blair973721f2012-08-15 10:19:43 -07001119
1120 def test_build_configuration_conflict(self):
1121 "Test that merge conflicts are handled"
1122 self.fake_jenkins.hold_jobs_in_queue = True
1123 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1124 A.addPatchset(['conflict'])
1125 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1126 B.addPatchset(['conflict'])
1127 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1128 A.addApproval('CRVW', 2)
1129 B.addApproval('CRVW', 2)
1130 C.addApproval('CRVW', 2)
1131 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1132 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1133 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1134 self.waitUntilSettled()
1135
1136 jobs = self.fake_jenkins.all_jobs
1137
1138 self.fake_jenkins.fakeRelease('.*-merge')
1139 self.waitUntilSettled()
1140 self.fake_jenkins.fakeRelease('.*-merge')
1141 self.waitUntilSettled()
1142 self.fake_jenkins.fakeRelease('.*-merge')
1143 self.waitUntilSettled()
1144 ref = jobs[-1].parameters['ZUUL_REF']
1145 self.fake_jenkins.hold_jobs_in_queue = False
1146 self.fake_jenkins.fakeRelease()
1147 self.waitUntilSettled()
1148
1149 assert A.data['status'] == 'MERGED'
1150 assert B.data['status'] == 'NEW'
1151 assert C.data['status'] == 'MERGED'
1152 assert A.reported == 2
1153 assert B.reported == 2
1154 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001155 self.assertEmptyQueues()
James E. Blairdaabed22012-08-15 15:38:57 -07001156
1157 def test_post(self):
1158 "Test that post jobs run"
Zhongyue Luo5d556072012-09-21 02:00:47 +09001159 e = {
1160 "type": "ref-updated",
1161 "submitter": {
1162 "name": "User Name",
1163 },
1164 "refUpdate": {
1165 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1166 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1167 "refName": "master",
1168 "project": "org/project",
1169 }
1170 }
James E. Blairdaabed22012-08-15 15:38:57 -07001171 self.fake_gerrit.addEvent(e)
1172 self.waitUntilSettled()
1173
1174 jobs = self.fake_jenkins.job_history
1175 job_names = [x['name'] for x in jobs]
1176 assert len(jobs) == 1
1177 assert 'project-post' in job_names
James E. Blaire0487072012-08-29 17:38:31 -07001178 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001179
1180 def test_build_configuration_branch(self):
1181 "Test that the right commits are on alternate branches"
1182 self.fake_jenkins.hold_jobs_in_queue = True
1183 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1184 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1185 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1186 A.addApproval('CRVW', 2)
1187 B.addApproval('CRVW', 2)
1188 C.addApproval('CRVW', 2)
1189 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1190 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1191 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1192 self.waitUntilSettled()
1193
1194 jobs = self.fake_jenkins.all_jobs
1195
1196 self.fake_jenkins.fakeRelease('.*-merge')
1197 self.waitUntilSettled()
1198 self.fake_jenkins.fakeRelease('.*-merge')
1199 self.waitUntilSettled()
1200 self.fake_jenkins.fakeRelease('.*-merge')
1201 self.waitUntilSettled()
1202 ref = jobs[-1].parameters['ZUUL_REF']
1203 self.fake_jenkins.hold_jobs_in_queue = False
1204 self.fake_jenkins.fakeRelease()
1205 self.waitUntilSettled()
1206
James E. Blair1dbd5082012-08-23 15:12:15 -07001207 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001208 repo = git.Repo(path)
1209 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1210 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001211 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
1212 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001213 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001214
1215 def test_build_configuration_branch_interaction(self):
1216 "Test that switching between branches works"
1217 self.test_build_configuration()
1218 self.test_build_configuration_branch()
1219 # C has been merged, undo that
James E. Blair1dbd5082012-08-23 15:12:15 -07001220 path = os.path.join(UPSTREAM_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001221 repo = git.Repo(path)
1222 repo.heads.master.commit = repo.commit('init')
1223 self.test_build_configuration()
James E. Blaire0487072012-08-29 17:38:31 -07001224 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001225
1226 def test_build_configuration_multi_branch(self):
1227 "Test that dependent changes on multiple branches are merged"
1228 self.fake_jenkins.hold_jobs_in_queue = True
1229 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1230 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1231 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1232 A.addApproval('CRVW', 2)
1233 B.addApproval('CRVW', 2)
1234 C.addApproval('CRVW', 2)
1235 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1236 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1237 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1238 self.waitUntilSettled()
1239
1240 jobs = self.fake_jenkins.all_jobs
1241
1242 self.fake_jenkins.fakeRelease('.*-merge')
1243 self.waitUntilSettled()
1244 ref_mp = jobs[-1].parameters['ZUUL_REF']
1245 self.fake_jenkins.fakeRelease('.*-merge')
1246 self.waitUntilSettled()
1247 self.fake_jenkins.fakeRelease('.*-merge')
1248 self.waitUntilSettled()
1249 ref_master = jobs[-1].parameters['ZUUL_REF']
1250 self.fake_jenkins.hold_jobs_in_queue = False
1251 self.fake_jenkins.fakeRelease()
1252 self.waitUntilSettled()
1253
James E. Blair1dbd5082012-08-23 15:12:15 -07001254 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001255 repo = git.Repo(path)
1256
1257 repo_messages = [c.message.strip()
1258 for c in repo.iter_commits(ref_master)]
1259 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001260 correct_messages = ['initial commit', 'A-1', 'C-1']
1261 assert repo_messages == correct_messages
1262
1263 repo_messages = [c.message.strip()
1264 for c in repo.iter_commits(ref_mp)]
1265 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001266 correct_messages = ['initial commit', 'mp commit', 'B-1']
1267 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001268 self.assertEmptyQueues()
James E. Blair7f71c802012-08-22 13:04:32 -07001269
1270 def test_one_job_project(self):
1271 "Test that queueing works with one job"
1272 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1273 'master', 'A')
1274 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1275 'master', 'B')
1276 A.addApproval('CRVW', 2)
1277 B.addApproval('CRVW', 2)
1278 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1279 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1280 self.waitUntilSettled()
1281
1282 jobs = self.fake_jenkins.all_jobs
1283 finished_jobs = self.fake_jenkins.job_history
James E. Blair7f71c802012-08-22 13:04:32 -07001284
1285 assert A.data['status'] == 'MERGED'
1286 assert A.reported == 2
1287 assert B.data['status'] == 'MERGED'
1288 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001289 self.assertEmptyQueues()
James E. Blaircaec0c52012-08-22 14:52:22 -07001290
1291 def test_dependent_changes_dequeue(self):
1292 "Test that dependent patches are not needlessly tested"
1293 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1294 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1295 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1296 A.addApproval('CRVW', 2)
1297 B.addApproval('CRVW', 2)
1298 C.addApproval('CRVW', 2)
1299
1300 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1301 M1.setMerged()
1302
1303 # C -> B -> A -> M1
1304
1305 C.setDependsOn(B, 1)
1306 B.setDependsOn(A, 1)
1307 A.setDependsOn(M1, 1)
1308
1309 self.fake_jenkins.fakeAddFailTest('project-merge', A)
1310
1311 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1312 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1313 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1314
1315 self.waitUntilSettled()
1316
1317 jobs = self.fake_jenkins.all_jobs
1318 finished_jobs = self.fake_jenkins.job_history
1319
James E. Blair127bc182012-08-28 15:55:15 -07001320 for x in jobs:
1321 print x
1322 for x in finished_jobs:
1323 print x
1324
James E. Blairec590122012-08-22 15:19:31 -07001325 assert A.data['status'] == 'NEW'
1326 assert A.reported == 2
1327 assert B.data['status'] == 'NEW'
1328 assert B.reported == 2
1329 assert C.data['status'] == 'NEW'
1330 assert C.reported == 2
1331 assert len(finished_jobs) == 1
James E. Blaire0487072012-08-29 17:38:31 -07001332 self.assertEmptyQueues()
James E. Blairec590122012-08-22 15:19:31 -07001333
1334 def test_head_is_dequeued_once(self):
1335 "Test that if a change at the head fails it is dequeud only once"
1336 # If it's dequeued more than once, we should see extra
1337 # aborted jobs.
1338 self.fake_jenkins.hold_jobs_in_build = True
1339
1340 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1341 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1342 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1343 A.addApproval('CRVW', 2)
1344 B.addApproval('CRVW', 2)
1345 C.addApproval('CRVW', 2)
1346
1347 self.fake_jenkins.fakeAddFailTest('project1-test1', A)
1348 self.fake_jenkins.fakeAddFailTest('project1-test2', A)
1349 self.fake_jenkins.fakeAddFailTest('project1-project2-integration', A)
1350
1351 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1352 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1353 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1354
1355 self.waitUntilSettled()
1356 jobs = self.fake_jenkins.all_jobs
1357 finished_jobs = self.fake_jenkins.job_history
1358
1359 assert len(jobs) == 1
1360 assert jobs[0].name == 'project1-merge'
1361 assert job_has_changes(jobs[0], A)
1362
1363 self.fake_jenkins.fakeRelease('.*-merge')
1364 self.waitUntilSettled()
1365 self.fake_jenkins.fakeRelease('.*-merge')
1366 self.waitUntilSettled()
1367 self.fake_jenkins.fakeRelease('.*-merge')
1368 self.waitUntilSettled()
1369
1370 assert len(jobs) == 9
1371 assert jobs[0].name == 'project1-test1'
1372 assert jobs[1].name == 'project1-test2'
1373 assert jobs[2].name == 'project1-project2-integration'
1374 assert jobs[3].name == 'project1-test1'
1375 assert jobs[4].name == 'project1-test2'
1376 assert jobs[5].name == 'project1-project2-integration'
1377 assert jobs[6].name == 'project1-test1'
1378 assert jobs[7].name == 'project1-test2'
1379 assert jobs[8].name == 'project1-project2-integration'
1380
1381 jobs[0].release()
1382 self.waitUntilSettled()
1383
1384 assert len(jobs) == 3 # test2, integration, merge for B
1385 assert self.countJobResults(finished_jobs, 'ABORTED') == 6
1386
1387 self.fake_jenkins.hold_jobs_in_build = False
1388 self.fake_jenkins.fakeRelease()
1389 self.waitUntilSettled()
1390
1391 assert len(jobs) == 0
1392 assert len(finished_jobs) == 20
James E. Blaircaec0c52012-08-22 14:52:22 -07001393
1394 assert A.data['status'] == 'NEW'
James E. Blairec590122012-08-22 15:19:31 -07001395 assert B.data['status'] == 'MERGED'
1396 assert C.data['status'] == 'MERGED'
1397 assert A.reported == 2
1398 assert B.reported == 2
1399 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001400 self.assertEmptyQueues()
James E. Blair4ec821f2012-08-23 15:28:28 -07001401
1402 def test_nonvoting_job(self):
1403 "Test that non-voting jobs don't vote."
1404 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1405 'master', 'A')
1406 A.addApproval('CRVW', 2)
1407 self.fake_jenkins.fakeAddFailTest('nonvoting-project-test2', A)
1408 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1409
1410 self.waitUntilSettled()
1411 jobs = self.fake_jenkins.all_jobs
1412 finished_jobs = self.fake_jenkins.job_history
1413
1414 assert A.data['status'] == 'MERGED'
1415 assert A.reported == 2
1416 assert finished_jobs[0]['result'] == 'SUCCESS'
1417 assert finished_jobs[1]['result'] == 'SUCCESS'
1418 assert finished_jobs[2]['result'] == 'FAILURE'
James E. Blaire0487072012-08-29 17:38:31 -07001419 self.assertEmptyQueues()
1420
1421 def test_check_queue_success(self):
1422 "Test successful check queue jobs."
1423 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1424 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1425
1426 self.waitUntilSettled()
1427 jobs = self.fake_jenkins.all_jobs
1428 finished_jobs = self.fake_jenkins.job_history
1429
1430 assert A.data['status'] == 'NEW'
1431 assert A.reported == 1
1432 assert finished_jobs[0]['result'] == 'SUCCESS'
1433 assert finished_jobs[1]['result'] == 'SUCCESS'
1434 assert finished_jobs[2]['result'] == 'SUCCESS'
1435 self.assertEmptyQueues()
1436
1437 def test_check_queue_failure(self):
1438 "Test failed check queue jobs."
1439 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1440 self.fake_jenkins.fakeAddFailTest('project-test2', A)
1441 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1442
1443 self.waitUntilSettled()
1444 jobs = self.fake_jenkins.all_jobs
1445 finished_jobs = self.fake_jenkins.job_history
1446
1447 assert A.data['status'] == 'NEW'
1448 assert A.reported == 1
1449 assert finished_jobs[0]['result'] == 'SUCCESS'
1450 assert finished_jobs[1]['result'] == 'SUCCESS'
1451 assert finished_jobs[2]['result'] == 'FAILURE'
1452 self.assertEmptyQueues()
James E. Blair127bc182012-08-28 15:55:15 -07001453
1454 def test_dependent_behind_dequeue(self):
1455 "test that dependent changes behind dequeued changes work"
1456 # This complicated test is a reproduction of a real life bug
1457 self.sched.reconfigure(self.config)
1458 self.fake_jenkins.hold_jobs_in_build = True
1459
1460 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1461 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1462 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1463 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1464 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1465 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1466 D.setDependsOn(C, 1)
1467 E.setDependsOn(D, 1)
1468 A.addApproval('CRVW', 2)
1469 B.addApproval('CRVW', 2)
1470 C.addApproval('CRVW', 2)
1471 D.addApproval('CRVW', 2)
1472 E.addApproval('CRVW', 2)
1473 F.addApproval('CRVW', 2)
1474
1475 A.fail_merge = True
1476 jobs = self.fake_jenkins.all_jobs
1477 finished_jobs = self.fake_jenkins.job_history
1478
1479 # Change object re-use in the gerrit trigger is hidden if
1480 # changes are added in quick succession; waiting makes it more
1481 # like real life.
1482 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1483 self.waitUntilSettled()
1484 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1485 self.waitUntilSettled()
1486
1487 self.fake_jenkins.fakeRelease('.*-merge')
1488 self.waitUntilSettled()
1489 self.fake_jenkins.fakeRelease('.*-merge')
1490 self.waitUntilSettled()
1491
1492 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1493 self.waitUntilSettled()
1494 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1495 self.waitUntilSettled()
1496 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1497 self.waitUntilSettled()
1498 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1499 self.waitUntilSettled()
1500
1501 self.fake_jenkins.fakeRelease('.*-merge')
1502 self.waitUntilSettled()
1503 self.fake_jenkins.fakeRelease('.*-merge')
1504 self.waitUntilSettled()
1505 self.fake_jenkins.fakeRelease('.*-merge')
1506 self.waitUntilSettled()
1507 self.fake_jenkins.fakeRelease('.*-merge')
1508 self.waitUntilSettled()
1509
James E. Blair4aa1ad62012-10-05 12:39:26 -07001510 for x in jobs:
1511 print x
James E. Blair127bc182012-08-28 15:55:15 -07001512 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001513
1514 # Grab pointers to the jobs we want to release before
1515 # releasing any, because list indexes may change as
1516 # the jobs complete.
1517 a, b, c = jobs[:3]
1518 a.release()
1519 b.release()
1520 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001521 self.waitUntilSettled()
1522
1523 self.fake_jenkins.hold_jobs_in_build = False
1524 self.fake_jenkins.fakeRelease()
1525 self.waitUntilSettled()
1526
1527 for x in jobs:
1528 print x
1529 for x in finished_jobs:
1530 print x
1531 print self.sched.formatStatusHTML()
1532
1533 assert A.data['status'] == 'NEW'
1534 assert B.data['status'] == 'MERGED'
1535 assert C.data['status'] == 'MERGED'
1536 assert D.data['status'] == 'MERGED'
1537 assert E.data['status'] == 'MERGED'
1538 assert F.data['status'] == 'MERGED'
1539
1540 assert A.reported == 2
1541 assert B.reported == 2
1542 assert C.reported == 2
1543 assert D.reported == 2
1544 assert E.reported == 2
1545 assert F.reported == 2
1546
James E. Blair127bc182012-08-28 15:55:15 -07001547 assert self.countJobResults(finished_jobs, 'ABORTED') == 15
1548 assert len(finished_jobs) == 44
James E. Blaire0487072012-08-29 17:38:31 -07001549 self.assertEmptyQueues()
James E. Blair05fed602012-09-07 12:45:24 -07001550
1551 def test_merger_repack(self):
1552 "Test that the merger works after a repack"
1553 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1554 A.addApproval('CRVW', 2)
1555 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1556 self.waitUntilSettled()
1557 jobs = self.fake_jenkins.job_history
1558 job_names = [x['name'] for x in jobs]
1559 assert 'project-merge' in job_names
1560 assert 'project-test1' in job_names
1561 assert 'project-test2' in job_names
1562 assert jobs[0]['result'] == 'SUCCESS'
1563 assert jobs[1]['result'] == 'SUCCESS'
1564 assert jobs[2]['result'] == 'SUCCESS'
1565 assert A.data['status'] == 'MERGED'
1566 assert A.reported == 2
1567 self.assertEmptyQueues()
1568
1569 path = os.path.join(GIT_ROOT, "org/project")
1570 os.system('git --git-dir=%s/.git repack -afd' % path)
1571
1572 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1573 A.addApproval('CRVW', 2)
1574 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1575 self.waitUntilSettled()
1576 jobs = self.fake_jenkins.job_history
1577 job_names = [x['name'] for x in jobs]
1578 assert 'project-merge' in job_names
1579 assert 'project-test1' in job_names
1580 assert 'project-test2' in job_names
1581 assert jobs[0]['result'] == 'SUCCESS'
1582 assert jobs[1]['result'] == 'SUCCESS'
1583 assert jobs[2]['result'] == 'SUCCESS'
1584 assert A.data['status'] == 'MERGED'
1585 assert A.reported == 2
1586 self.assertEmptyQueues()
James E. Blair7ee88a22012-09-12 18:59:31 +02001587
James E. Blair4886f282012-11-15 09:27:33 -08001588 def test_merger_repack_large_change(self):
1589 "Test that the merger works with large changes after a repack"
1590 # https://bugs.launchpad.net/zuul/+bug/1078946
1591 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1592 A.addPatchset(large=True)
1593 path = os.path.join(UPSTREAM_ROOT, "org/project1")
1594 os.system('git --git-dir=%s/.git repack -afd' % path)
1595 path = os.path.join(GIT_ROOT, "org/project1")
1596 os.system('git --git-dir=%s/.git repack -afd' % path)
1597
1598 A.addApproval('CRVW', 2)
1599 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1600 self.waitUntilSettled()
1601 jobs = self.fake_jenkins.job_history
1602 job_names = [x['name'] for x in jobs]
1603 assert 'project1-merge' in job_names
1604 assert 'project1-test1' in job_names
1605 assert 'project1-test2' in job_names
1606 assert jobs[0]['result'] == 'SUCCESS'
1607 assert jobs[1]['result'] == 'SUCCESS'
1608 assert jobs[2]['result'] == 'SUCCESS'
1609 assert A.data['status'] == 'MERGED'
1610 assert A.reported == 2
1611 self.assertEmptyQueues()
1612
James E. Blair7ee88a22012-09-12 18:59:31 +02001613 def test_nonexistent_job(self):
1614 "Test launching a job that doesn't exist"
1615 self.fake_jenkins.nonexistent_jobs.append('project-merge')
1616 self.jenkins.launch_retry_timeout = 0.1
1617
1618 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1619 A.addApproval('CRVW', 2)
1620 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1621 # There may be a thread about to report a lost change
1622 while A.reported < 2:
1623 self.waitUntilSettled()
1624 jobs = self.fake_jenkins.job_history
1625 job_names = [x['name'] for x in jobs]
1626 assert not job_names
1627 assert A.data['status'] == 'NEW'
1628 assert A.reported == 2
1629 self.assertEmptyQueues()
1630
1631 # Make sure things still work:
1632 self.fake_jenkins.nonexistent_jobs = []
1633 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1634 A.addApproval('CRVW', 2)
1635 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1636 self.waitUntilSettled()
1637 jobs = self.fake_jenkins.job_history
1638 job_names = [x['name'] for x in jobs]
1639 assert 'project-merge' in job_names
1640 assert 'project-test1' in job_names
1641 assert 'project-test2' in job_names
1642 assert jobs[0]['result'] == 'SUCCESS'
1643 assert jobs[1]['result'] == 'SUCCESS'
1644 assert jobs[2]['result'] == 'SUCCESS'
1645 assert A.data['status'] == 'MERGED'
1646 assert A.reported == 2
1647 self.assertEmptyQueues()
James E. Blairf62d4282012-12-31 17:01:50 -08001648
1649 def test_single_nonexistent_post_job(self):
1650 "Test launching a single post job that doesn't exist"
1651 self.fake_jenkins.nonexistent_jobs.append('project-post')
1652 self.jenkins.launch_retry_timeout = 0.1
1653
1654 e = {
1655 "type": "ref-updated",
1656 "submitter": {
1657 "name": "User Name",
1658 },
1659 "refUpdate": {
1660 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1661 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1662 "refName": "master",
1663 "project": "org/project",
1664 }
1665 }
1666 self.fake_gerrit.addEvent(e)
1667 self.waitUntilSettled()
1668
1669 jobs = self.fake_jenkins.job_history
1670 assert len(jobs) == 0
1671 self.assertEmptyQueues()