blob: c199345d8fc4190e780a06bd00c14295bc13fc57 [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
James E. Blairf62d4282012-12-31 17:01:50 -0800699 len(self.jenkins.lost_threads) == 0 and
James E. Blairb0fcae42012-07-17 11:12:10 -0700700 self.fake_jenkins.fakeAllWaiting()):
701 self.sched.queue_lock.release()
702 self.log.debug("...settled.")
703 return
704 self.sched.queue_lock.release()
705 self.sched.wake_event.wait(0.1)
706
James E. Blaird466dc42012-07-31 10:42:56 -0700707 def countJobResults(self, jobs, result):
708 jobs = filter(lambda x: x['result'] == result, jobs)
709 return len(jobs)
710
James E. Blaire0487072012-08-29 17:38:31 -0700711 def assertEmptyQueues(self):
712 # Make sure there are no orphaned jobs
713 for pipeline in self.sched.pipelines.values():
714 for queue in pipeline.queues:
715 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -0800716 print 'pipeline %s queue %s contents %s' % (
717 pipeline.name, queue.name, queue.queue)
James E. Blaire0487072012-08-29 17:38:31 -0700718 assert len(queue.queue) == 0
719 if len(queue.severed_heads) != 0:
720 print 'heads', queue.severed_heads
721 assert len(queue.severed_heads) == 0
722
James E. Blairb0fcae42012-07-17 11:12:10 -0700723 def test_jobs_launched(self):
724 "Test that jobs are launched and a change is merged"
725 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -0700726 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700727 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
728 self.waitUntilSettled()
729 jobs = self.fake_jenkins.job_history
730 job_names = [x['name'] for x in jobs]
731 assert 'project-merge' in job_names
732 assert 'project-test1' in job_names
733 assert 'project-test2' in job_names
734 assert jobs[0]['result'] == 'SUCCESS'
735 assert jobs[1]['result'] == 'SUCCESS'
736 assert jobs[2]['result'] == 'SUCCESS'
737 assert A.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700738 assert A.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700739 self.assertEmptyQueues()
James E. Blairb0fcae42012-07-17 11:12:10 -0700740
741 def test_parallel_changes(self):
742 "Test that changes are tested in parallel and merged in series"
743 self.fake_jenkins.hold_jobs_in_build = True
744 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
745 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
746 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700747 A.addApproval('CRVW', 2)
748 B.addApproval('CRVW', 2)
749 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700750
751 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
752 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
753 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
754
755 self.waitUntilSettled()
756 jobs = self.fake_jenkins.all_jobs
757 assert len(jobs) == 1
758 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700759 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700760
761 self.fake_jenkins.fakeRelease('.*-merge')
762 self.waitUntilSettled()
763 assert len(jobs) == 3
764 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700765 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700766 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700767 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700768 assert jobs[2].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700769 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700770
771 self.fake_jenkins.fakeRelease('.*-merge')
772 self.waitUntilSettled()
773 assert len(jobs) == 5
774 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700775 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700776 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700777 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700778
779 assert jobs[2].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700780 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700781 assert jobs[3].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700782 assert job_has_changes(jobs[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700783
784 assert jobs[4].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700785 assert job_has_changes(jobs[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700786
787 self.fake_jenkins.fakeRelease('.*-merge')
788 self.waitUntilSettled()
789 assert len(jobs) == 6
790 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700791 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700792 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700793 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700794
795 assert jobs[2].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700796 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700797 assert jobs[3].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700798 assert job_has_changes(jobs[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700799
800 assert jobs[4].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700801 assert job_has_changes(jobs[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700802 assert jobs[5].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700803 assert job_has_changes(jobs[5], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700804
805 self.fake_jenkins.hold_jobs_in_build = False
806 self.fake_jenkins.fakeRelease()
807 self.waitUntilSettled()
808 assert len(jobs) == 0
809
810 jobs = self.fake_jenkins.job_history
811 assert len(jobs) == 9
812 assert A.data['status'] == 'MERGED'
813 assert B.data['status'] == 'MERGED'
814 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700815 assert A.reported == 2
816 assert B.reported == 2
817 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700818 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700819
820 def test_failed_changes(self):
821 "Test that a change behind a failed change is retested"
822 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
823 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -0700824 A.addApproval('CRVW', 2)
825 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700826
827 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
828 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
829
James E. Blair4886cc12012-07-18 15:39:41 -0700830 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700831
832 self.waitUntilSettled()
833 jobs = self.fake_jenkins.job_history
834 assert len(jobs) > 6
835 assert A.data['status'] == 'NEW'
836 assert B.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700837 assert A.reported == 2
838 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700839 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700840
841 def test_independent_queues(self):
842 "Test that changes end up in the right queues"
843 self.fake_jenkins.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900844 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700845 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
846 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700847 A.addApproval('CRVW', 2)
848 B.addApproval('CRVW', 2)
849 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700850
851 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
852 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
853 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
854
855 jobs = self.fake_jenkins.all_jobs
856 self.waitUntilSettled()
857
858 # There should be one merge job at the head of each queue running
859 assert len(jobs) == 2
860 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700861 assert job_has_changes(jobs[0], A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700862 assert jobs[1].name == 'project1-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700863 assert job_has_changes(jobs[1], B)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700864
865 # Release the current merge jobs
866 self.fake_jenkins.fakeRelease('.*-merge')
867 self.waitUntilSettled()
868 # Release the merge job for project2 which is behind project1
869 self.fake_jenkins.fakeRelease('.*-merge')
870 self.waitUntilSettled()
871
872 # All the test jobs should be running:
873 # project1 (3) + project2 (3) + project (2) = 8
874 assert len(jobs) == 8
875
876 self.fake_jenkins.fakeRelease()
877 self.waitUntilSettled()
878 assert len(jobs) == 0
879
880 jobs = self.fake_jenkins.job_history
881 assert len(jobs) == 11
882 assert A.data['status'] == 'MERGED'
883 assert B.data['status'] == 'MERGED'
884 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700885 assert A.reported == 2
886 assert B.reported == 2
887 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700888 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -0700889
890 def test_failed_change_at_head(self):
891 "Test that if a change at the head fails, jobs behind it are canceled"
892 self.fake_jenkins.hold_jobs_in_build = True
893
894 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
895 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
896 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700897 A.addApproval('CRVW', 2)
898 B.addApproval('CRVW', 2)
899 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700900
James E. Blair4886cc12012-07-18 15:39:41 -0700901 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700902
903 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
904 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
905 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
906
907 self.waitUntilSettled()
908 jobs = self.fake_jenkins.all_jobs
909 finished_jobs = self.fake_jenkins.job_history
910
911 assert len(jobs) == 1
912 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700913 assert job_has_changes(jobs[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -0700914
915 self.fake_jenkins.fakeRelease('.*-merge')
916 self.waitUntilSettled()
917 self.fake_jenkins.fakeRelease('.*-merge')
918 self.waitUntilSettled()
919 self.fake_jenkins.fakeRelease('.*-merge')
920 self.waitUntilSettled()
921
922 assert len(jobs) == 6
923 assert jobs[0].name == 'project-test1'
924 assert jobs[1].name == 'project-test2'
925 assert jobs[2].name == 'project-test1'
926 assert jobs[3].name == 'project-test2'
927 assert jobs[4].name == 'project-test1'
928 assert jobs[5].name == 'project-test2'
929
930 jobs[0].release()
931 self.waitUntilSettled()
932
James E. Blairec590122012-08-22 15:19:31 -0700933 assert len(jobs) == 2 # project-test2, project-merge for B
James E. Blaird466dc42012-07-31 10:42:56 -0700934 assert self.countJobResults(finished_jobs, 'ABORTED') == 4
935
936 self.fake_jenkins.hold_jobs_in_build = False
937 self.fake_jenkins.fakeRelease()
938 self.waitUntilSettled()
939
940 assert len(jobs) == 0
941 assert len(finished_jobs) == 15
942 assert A.data['status'] == 'NEW'
943 assert B.data['status'] == 'MERGED'
944 assert C.data['status'] == 'MERGED'
945 assert A.reported == 2
946 assert B.reported == 2
947 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700948 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -0700949
950 def test_failed_change_at_head_with_queue(self):
951 "Test that if a change at the head fails, queued jobs are canceled"
952 self.fake_jenkins.hold_jobs_in_queue = True
953
954 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
955 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
956 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700957 A.addApproval('CRVW', 2)
958 B.addApproval('CRVW', 2)
959 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700960
James E. Blair4886cc12012-07-18 15:39:41 -0700961 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700962
963 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
964 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
965 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
966
967 self.waitUntilSettled()
968 jobs = self.fake_jenkins.all_jobs
969 finished_jobs = self.fake_jenkins.job_history
970 queue = self.fake_jenkins.queue
971
972 assert len(jobs) == 1
973 assert len(queue) == 1
974 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700975 assert job_has_changes(jobs[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -0700976
977 self.fake_jenkins.fakeRelease('.*-merge')
978 self.waitUntilSettled()
979 self.fake_jenkins.fakeRelease('.*-merge')
980 self.waitUntilSettled()
981 self.fake_jenkins.fakeRelease('.*-merge')
982 self.waitUntilSettled()
983
984 assert len(jobs) == 6
985 assert len(queue) == 6
986 assert jobs[0].name == 'project-test1'
987 assert jobs[1].name == 'project-test2'
988 assert jobs[2].name == 'project-test1'
989 assert jobs[3].name == 'project-test2'
990 assert jobs[4].name == 'project-test1'
991 assert jobs[5].name == 'project-test2'
992
993 jobs[0].release()
994 self.waitUntilSettled()
995
James E. Blairec590122012-08-22 15:19:31 -0700996 assert len(jobs) == 2 # project-test2, project-merge for B
997 assert len(queue) == 2
James E. Blaird466dc42012-07-31 10:42:56 -0700998 assert self.countJobResults(finished_jobs, 'ABORTED') == 0
999
1000 self.fake_jenkins.hold_jobs_in_queue = False
1001 self.fake_jenkins.fakeRelease()
1002 self.waitUntilSettled()
1003
1004 assert len(jobs) == 0
1005 assert len(finished_jobs) == 11
1006 assert A.data['status'] == 'NEW'
1007 assert B.data['status'] == 'MERGED'
1008 assert C.data['status'] == 'MERGED'
1009 assert A.reported == 2
1010 assert B.reported == 2
1011 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001012 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001013
1014 def test_patch_order(self):
1015 "Test that dependent patches are tested in the right order"
1016 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1017 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1018 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1019 A.addApproval('CRVW', 2)
1020 B.addApproval('CRVW', 2)
1021 C.addApproval('CRVW', 2)
1022
1023 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1024 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1025 M2.setMerged()
1026 M1.setMerged()
1027
1028 # C -> B -> A -> M1 -> M2
1029 # M2 is here to make sure it is never queried. If it is, it
1030 # means zuul is walking down the entire history of merged
1031 # changes.
1032
1033 C.setDependsOn(B, 1)
1034 B.setDependsOn(A, 1)
1035 A.setDependsOn(M1, 1)
1036 M1.setDependsOn(M2, 1)
1037
1038 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1039
1040 self.waitUntilSettled()
1041
1042 assert A.data['status'] == 'NEW'
1043 assert B.data['status'] == 'NEW'
1044 assert C.data['status'] == 'NEW'
1045
1046 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1047 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1048
1049 self.waitUntilSettled()
1050 assert M2.queried == 0
1051 assert A.data['status'] == 'MERGED'
1052 assert B.data['status'] == 'MERGED'
1053 assert C.data['status'] == 'MERGED'
1054 assert A.reported == 2
1055 assert B.reported == 2
1056 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001057 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001058
1059 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001060 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001061 # TODO: move to test_gerrit (this is a unit test!)
1062 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4aea70c2012-07-26 14:23:24 -07001063 a = self.sched.trigger.getChange(1, 2)
1064 mgr = self.sched.pipelines['gate'].manager
1065 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001066
1067 A.addApproval('CRVW', 2)
James E. Blair4aea70c2012-07-26 14:23:24 -07001068 a = self.sched.trigger.getChange(1, 2)
1069 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001070
1071 A.addApproval('APRV', 1)
James E. Blair4aea70c2012-07-26 14:23:24 -07001072 a = self.sched.trigger.getChange(1, 2)
1073 assert self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blaire0487072012-08-29 17:38:31 -07001074 self.assertEmptyQueues()
James E. Blair4886cc12012-07-18 15:39:41 -07001075
1076 def test_build_configuration(self):
1077 "Test that zuul merges the right commits for testing"
1078 self.fake_jenkins.hold_jobs_in_queue = True
1079 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1080 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1081 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1082 A.addApproval('CRVW', 2)
1083 B.addApproval('CRVW', 2)
1084 C.addApproval('CRVW', 2)
1085 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1086 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1087 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1088 self.waitUntilSettled()
1089
1090 jobs = self.fake_jenkins.all_jobs
1091
1092 self.fake_jenkins.fakeRelease('.*-merge')
1093 self.waitUntilSettled()
1094 self.fake_jenkins.fakeRelease('.*-merge')
1095 self.waitUntilSettled()
1096 self.fake_jenkins.fakeRelease('.*-merge')
1097 self.waitUntilSettled()
James E. Blair1dbd5082012-08-23 15:12:15 -07001098
James E. Blair4886cc12012-07-18 15:39:41 -07001099 ref = jobs[-1].parameters['ZUUL_REF']
1100 self.fake_jenkins.hold_jobs_in_queue = False
1101 self.fake_jenkins.fakeRelease()
James E. Blair973721f2012-08-15 10:19:43 -07001102 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001103
James E. Blair1dbd5082012-08-23 15:12:15 -07001104 path = os.path.join(GIT_ROOT, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001105 repo = git.Repo(path)
1106 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1107 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001108 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
1109 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001110 self.assertEmptyQueues()
James E. Blair973721f2012-08-15 10:19:43 -07001111
1112 def test_build_configuration_conflict(self):
1113 "Test that merge conflicts are handled"
1114 self.fake_jenkins.hold_jobs_in_queue = True
1115 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1116 A.addPatchset(['conflict'])
1117 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1118 B.addPatchset(['conflict'])
1119 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1120 A.addApproval('CRVW', 2)
1121 B.addApproval('CRVW', 2)
1122 C.addApproval('CRVW', 2)
1123 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1124 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1125 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1126 self.waitUntilSettled()
1127
1128 jobs = self.fake_jenkins.all_jobs
1129
1130 self.fake_jenkins.fakeRelease('.*-merge')
1131 self.waitUntilSettled()
1132 self.fake_jenkins.fakeRelease('.*-merge')
1133 self.waitUntilSettled()
1134 self.fake_jenkins.fakeRelease('.*-merge')
1135 self.waitUntilSettled()
1136 ref = jobs[-1].parameters['ZUUL_REF']
1137 self.fake_jenkins.hold_jobs_in_queue = False
1138 self.fake_jenkins.fakeRelease()
1139 self.waitUntilSettled()
1140
1141 assert A.data['status'] == 'MERGED'
1142 assert B.data['status'] == 'NEW'
1143 assert C.data['status'] == 'MERGED'
1144 assert A.reported == 2
1145 assert B.reported == 2
1146 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001147 self.assertEmptyQueues()
James E. Blairdaabed22012-08-15 15:38:57 -07001148
1149 def test_post(self):
1150 "Test that post jobs run"
Zhongyue Luo5d556072012-09-21 02:00:47 +09001151 e = {
1152 "type": "ref-updated",
1153 "submitter": {
1154 "name": "User Name",
1155 },
1156 "refUpdate": {
1157 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1158 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1159 "refName": "master",
1160 "project": "org/project",
1161 }
1162 }
James E. Blairdaabed22012-08-15 15:38:57 -07001163 self.fake_gerrit.addEvent(e)
1164 self.waitUntilSettled()
1165
1166 jobs = self.fake_jenkins.job_history
1167 job_names = [x['name'] for x in jobs]
1168 assert len(jobs) == 1
1169 assert 'project-post' in job_names
James E. Blaire0487072012-08-29 17:38:31 -07001170 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001171
1172 def test_build_configuration_branch(self):
1173 "Test that the right commits are on alternate branches"
1174 self.fake_jenkins.hold_jobs_in_queue = True
1175 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1176 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1177 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1178 A.addApproval('CRVW', 2)
1179 B.addApproval('CRVW', 2)
1180 C.addApproval('CRVW', 2)
1181 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1182 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1183 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1184 self.waitUntilSettled()
1185
1186 jobs = self.fake_jenkins.all_jobs
1187
1188 self.fake_jenkins.fakeRelease('.*-merge')
1189 self.waitUntilSettled()
1190 self.fake_jenkins.fakeRelease('.*-merge')
1191 self.waitUntilSettled()
1192 self.fake_jenkins.fakeRelease('.*-merge')
1193 self.waitUntilSettled()
1194 ref = jobs[-1].parameters['ZUUL_REF']
1195 self.fake_jenkins.hold_jobs_in_queue = False
1196 self.fake_jenkins.fakeRelease()
1197 self.waitUntilSettled()
1198
James E. Blair1dbd5082012-08-23 15:12:15 -07001199 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001200 repo = git.Repo(path)
1201 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1202 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001203 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
1204 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001205 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001206
1207 def test_build_configuration_branch_interaction(self):
1208 "Test that switching between branches works"
1209 self.test_build_configuration()
1210 self.test_build_configuration_branch()
1211 # C has been merged, undo that
James E. Blair1dbd5082012-08-23 15:12:15 -07001212 path = os.path.join(UPSTREAM_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001213 repo = git.Repo(path)
1214 repo.heads.master.commit = repo.commit('init')
1215 self.test_build_configuration()
James E. Blaire0487072012-08-29 17:38:31 -07001216 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001217
1218 def test_build_configuration_multi_branch(self):
1219 "Test that dependent changes on multiple branches are merged"
1220 self.fake_jenkins.hold_jobs_in_queue = True
1221 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1222 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1223 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1224 A.addApproval('CRVW', 2)
1225 B.addApproval('CRVW', 2)
1226 C.addApproval('CRVW', 2)
1227 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1228 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1229 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1230 self.waitUntilSettled()
1231
1232 jobs = self.fake_jenkins.all_jobs
1233
1234 self.fake_jenkins.fakeRelease('.*-merge')
1235 self.waitUntilSettled()
1236 ref_mp = jobs[-1].parameters['ZUUL_REF']
1237 self.fake_jenkins.fakeRelease('.*-merge')
1238 self.waitUntilSettled()
1239 self.fake_jenkins.fakeRelease('.*-merge')
1240 self.waitUntilSettled()
1241 ref_master = jobs[-1].parameters['ZUUL_REF']
1242 self.fake_jenkins.hold_jobs_in_queue = False
1243 self.fake_jenkins.fakeRelease()
1244 self.waitUntilSettled()
1245
James E. Blair1dbd5082012-08-23 15:12:15 -07001246 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001247 repo = git.Repo(path)
1248
1249 repo_messages = [c.message.strip()
1250 for c in repo.iter_commits(ref_master)]
1251 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001252 correct_messages = ['initial commit', 'A-1', 'C-1']
1253 assert repo_messages == correct_messages
1254
1255 repo_messages = [c.message.strip()
1256 for c in repo.iter_commits(ref_mp)]
1257 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001258 correct_messages = ['initial commit', 'mp commit', 'B-1']
1259 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001260 self.assertEmptyQueues()
James E. Blair7f71c802012-08-22 13:04:32 -07001261
1262 def test_one_job_project(self):
1263 "Test that queueing works with one job"
1264 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1265 'master', 'A')
1266 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1267 'master', 'B')
1268 A.addApproval('CRVW', 2)
1269 B.addApproval('CRVW', 2)
1270 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1271 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1272 self.waitUntilSettled()
1273
1274 jobs = self.fake_jenkins.all_jobs
1275 finished_jobs = self.fake_jenkins.job_history
James E. Blair7f71c802012-08-22 13:04:32 -07001276
1277 assert A.data['status'] == 'MERGED'
1278 assert A.reported == 2
1279 assert B.data['status'] == 'MERGED'
1280 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001281 self.assertEmptyQueues()
James E. Blaircaec0c52012-08-22 14:52:22 -07001282
1283 def test_dependent_changes_dequeue(self):
1284 "Test that dependent patches are not needlessly tested"
1285 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1286 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1287 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1288 A.addApproval('CRVW', 2)
1289 B.addApproval('CRVW', 2)
1290 C.addApproval('CRVW', 2)
1291
1292 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1293 M1.setMerged()
1294
1295 # C -> B -> A -> M1
1296
1297 C.setDependsOn(B, 1)
1298 B.setDependsOn(A, 1)
1299 A.setDependsOn(M1, 1)
1300
1301 self.fake_jenkins.fakeAddFailTest('project-merge', A)
1302
1303 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1304 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1305 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1306
1307 self.waitUntilSettled()
1308
1309 jobs = self.fake_jenkins.all_jobs
1310 finished_jobs = self.fake_jenkins.job_history
1311
James E. Blair127bc182012-08-28 15:55:15 -07001312 for x in jobs:
1313 print x
1314 for x in finished_jobs:
1315 print x
1316
James E. Blairec590122012-08-22 15:19:31 -07001317 assert A.data['status'] == 'NEW'
1318 assert A.reported == 2
1319 assert B.data['status'] == 'NEW'
1320 assert B.reported == 2
1321 assert C.data['status'] == 'NEW'
1322 assert C.reported == 2
1323 assert len(finished_jobs) == 1
James E. Blaire0487072012-08-29 17:38:31 -07001324 self.assertEmptyQueues()
James E. Blairec590122012-08-22 15:19:31 -07001325
1326 def test_head_is_dequeued_once(self):
1327 "Test that if a change at the head fails it is dequeud only once"
1328 # If it's dequeued more than once, we should see extra
1329 # aborted jobs.
1330 self.fake_jenkins.hold_jobs_in_build = True
1331
1332 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1333 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1334 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1335 A.addApproval('CRVW', 2)
1336 B.addApproval('CRVW', 2)
1337 C.addApproval('CRVW', 2)
1338
1339 self.fake_jenkins.fakeAddFailTest('project1-test1', A)
1340 self.fake_jenkins.fakeAddFailTest('project1-test2', A)
1341 self.fake_jenkins.fakeAddFailTest('project1-project2-integration', A)
1342
1343 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1344 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1345 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1346
1347 self.waitUntilSettled()
1348 jobs = self.fake_jenkins.all_jobs
1349 finished_jobs = self.fake_jenkins.job_history
1350
1351 assert len(jobs) == 1
1352 assert jobs[0].name == 'project1-merge'
1353 assert job_has_changes(jobs[0], A)
1354
1355 self.fake_jenkins.fakeRelease('.*-merge')
1356 self.waitUntilSettled()
1357 self.fake_jenkins.fakeRelease('.*-merge')
1358 self.waitUntilSettled()
1359 self.fake_jenkins.fakeRelease('.*-merge')
1360 self.waitUntilSettled()
1361
1362 assert len(jobs) == 9
1363 assert jobs[0].name == 'project1-test1'
1364 assert jobs[1].name == 'project1-test2'
1365 assert jobs[2].name == 'project1-project2-integration'
1366 assert jobs[3].name == 'project1-test1'
1367 assert jobs[4].name == 'project1-test2'
1368 assert jobs[5].name == 'project1-project2-integration'
1369 assert jobs[6].name == 'project1-test1'
1370 assert jobs[7].name == 'project1-test2'
1371 assert jobs[8].name == 'project1-project2-integration'
1372
1373 jobs[0].release()
1374 self.waitUntilSettled()
1375
1376 assert len(jobs) == 3 # test2, integration, merge for B
1377 assert self.countJobResults(finished_jobs, 'ABORTED') == 6
1378
1379 self.fake_jenkins.hold_jobs_in_build = False
1380 self.fake_jenkins.fakeRelease()
1381 self.waitUntilSettled()
1382
1383 assert len(jobs) == 0
1384 assert len(finished_jobs) == 20
James E. Blaircaec0c52012-08-22 14:52:22 -07001385
1386 assert A.data['status'] == 'NEW'
James E. Blairec590122012-08-22 15:19:31 -07001387 assert B.data['status'] == 'MERGED'
1388 assert C.data['status'] == 'MERGED'
1389 assert A.reported == 2
1390 assert B.reported == 2
1391 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001392 self.assertEmptyQueues()
James E. Blair4ec821f2012-08-23 15:28:28 -07001393
1394 def test_nonvoting_job(self):
1395 "Test that non-voting jobs don't vote."
1396 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1397 'master', 'A')
1398 A.addApproval('CRVW', 2)
1399 self.fake_jenkins.fakeAddFailTest('nonvoting-project-test2', A)
1400 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1401
1402 self.waitUntilSettled()
1403 jobs = self.fake_jenkins.all_jobs
1404 finished_jobs = self.fake_jenkins.job_history
1405
1406 assert A.data['status'] == 'MERGED'
1407 assert A.reported == 2
1408 assert finished_jobs[0]['result'] == 'SUCCESS'
1409 assert finished_jobs[1]['result'] == 'SUCCESS'
1410 assert finished_jobs[2]['result'] == 'FAILURE'
James E. Blaire0487072012-08-29 17:38:31 -07001411 self.assertEmptyQueues()
1412
1413 def test_check_queue_success(self):
1414 "Test successful check queue jobs."
1415 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1416 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1417
1418 self.waitUntilSettled()
1419 jobs = self.fake_jenkins.all_jobs
1420 finished_jobs = self.fake_jenkins.job_history
1421
1422 assert A.data['status'] == 'NEW'
1423 assert A.reported == 1
1424 assert finished_jobs[0]['result'] == 'SUCCESS'
1425 assert finished_jobs[1]['result'] == 'SUCCESS'
1426 assert finished_jobs[2]['result'] == 'SUCCESS'
1427 self.assertEmptyQueues()
1428
1429 def test_check_queue_failure(self):
1430 "Test failed check queue jobs."
1431 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1432 self.fake_jenkins.fakeAddFailTest('project-test2', A)
1433 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1434
1435 self.waitUntilSettled()
1436 jobs = self.fake_jenkins.all_jobs
1437 finished_jobs = self.fake_jenkins.job_history
1438
1439 assert A.data['status'] == 'NEW'
1440 assert A.reported == 1
1441 assert finished_jobs[0]['result'] == 'SUCCESS'
1442 assert finished_jobs[1]['result'] == 'SUCCESS'
1443 assert finished_jobs[2]['result'] == 'FAILURE'
1444 self.assertEmptyQueues()
James E. Blair127bc182012-08-28 15:55:15 -07001445
1446 def test_dependent_behind_dequeue(self):
1447 "test that dependent changes behind dequeued changes work"
1448 # This complicated test is a reproduction of a real life bug
1449 self.sched.reconfigure(self.config)
1450 self.fake_jenkins.hold_jobs_in_build = True
1451
1452 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1453 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1454 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1455 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1456 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1457 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1458 D.setDependsOn(C, 1)
1459 E.setDependsOn(D, 1)
1460 A.addApproval('CRVW', 2)
1461 B.addApproval('CRVW', 2)
1462 C.addApproval('CRVW', 2)
1463 D.addApproval('CRVW', 2)
1464 E.addApproval('CRVW', 2)
1465 F.addApproval('CRVW', 2)
1466
1467 A.fail_merge = True
1468 jobs = self.fake_jenkins.all_jobs
1469 finished_jobs = self.fake_jenkins.job_history
1470
1471 # Change object re-use in the gerrit trigger is hidden if
1472 # changes are added in quick succession; waiting makes it more
1473 # like real life.
1474 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1475 self.waitUntilSettled()
1476 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1477 self.waitUntilSettled()
1478
1479 self.fake_jenkins.fakeRelease('.*-merge')
1480 self.waitUntilSettled()
1481 self.fake_jenkins.fakeRelease('.*-merge')
1482 self.waitUntilSettled()
1483
1484 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1485 self.waitUntilSettled()
1486 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1487 self.waitUntilSettled()
1488 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1489 self.waitUntilSettled()
1490 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1491 self.waitUntilSettled()
1492
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 self.fake_jenkins.fakeRelease('.*-merge')
1500 self.waitUntilSettled()
1501
James E. Blair4aa1ad62012-10-05 12:39:26 -07001502 for x in jobs:
1503 print x
James E. Blair127bc182012-08-28 15:55:15 -07001504 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001505
1506 # Grab pointers to the jobs we want to release before
1507 # releasing any, because list indexes may change as
1508 # the jobs complete.
1509 a, b, c = jobs[:3]
1510 a.release()
1511 b.release()
1512 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001513 self.waitUntilSettled()
1514
1515 self.fake_jenkins.hold_jobs_in_build = False
1516 self.fake_jenkins.fakeRelease()
1517 self.waitUntilSettled()
1518
1519 for x in jobs:
1520 print x
1521 for x in finished_jobs:
1522 print x
1523 print self.sched.formatStatusHTML()
1524
1525 assert A.data['status'] == 'NEW'
1526 assert B.data['status'] == 'MERGED'
1527 assert C.data['status'] == 'MERGED'
1528 assert D.data['status'] == 'MERGED'
1529 assert E.data['status'] == 'MERGED'
1530 assert F.data['status'] == 'MERGED'
1531
1532 assert A.reported == 2
1533 assert B.reported == 2
1534 assert C.reported == 2
1535 assert D.reported == 2
1536 assert E.reported == 2
1537 assert F.reported == 2
1538
James E. Blair127bc182012-08-28 15:55:15 -07001539 assert self.countJobResults(finished_jobs, 'ABORTED') == 15
1540 assert len(finished_jobs) == 44
James E. Blaire0487072012-08-29 17:38:31 -07001541 self.assertEmptyQueues()
James E. Blair05fed602012-09-07 12:45:24 -07001542
1543 def test_merger_repack(self):
1544 "Test that the merger works after a repack"
1545 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1546 A.addApproval('CRVW', 2)
1547 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1548 self.waitUntilSettled()
1549 jobs = self.fake_jenkins.job_history
1550 job_names = [x['name'] for x in jobs]
1551 assert 'project-merge' in job_names
1552 assert 'project-test1' in job_names
1553 assert 'project-test2' in job_names
1554 assert jobs[0]['result'] == 'SUCCESS'
1555 assert jobs[1]['result'] == 'SUCCESS'
1556 assert jobs[2]['result'] == 'SUCCESS'
1557 assert A.data['status'] == 'MERGED'
1558 assert A.reported == 2
1559 self.assertEmptyQueues()
1560
1561 path = os.path.join(GIT_ROOT, "org/project")
1562 os.system('git --git-dir=%s/.git repack -afd' % path)
1563
1564 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1565 A.addApproval('CRVW', 2)
1566 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1567 self.waitUntilSettled()
1568 jobs = self.fake_jenkins.job_history
1569 job_names = [x['name'] for x in jobs]
1570 assert 'project-merge' in job_names
1571 assert 'project-test1' in job_names
1572 assert 'project-test2' in job_names
1573 assert jobs[0]['result'] == 'SUCCESS'
1574 assert jobs[1]['result'] == 'SUCCESS'
1575 assert jobs[2]['result'] == 'SUCCESS'
1576 assert A.data['status'] == 'MERGED'
1577 assert A.reported == 2
1578 self.assertEmptyQueues()
James E. Blair7ee88a22012-09-12 18:59:31 +02001579
James E. Blair4886f282012-11-15 09:27:33 -08001580 def test_merger_repack_large_change(self):
1581 "Test that the merger works with large changes after a repack"
1582 # https://bugs.launchpad.net/zuul/+bug/1078946
1583 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1584 A.addPatchset(large=True)
1585 path = os.path.join(UPSTREAM_ROOT, "org/project1")
1586 os.system('git --git-dir=%s/.git repack -afd' % path)
1587 path = os.path.join(GIT_ROOT, "org/project1")
1588 os.system('git --git-dir=%s/.git repack -afd' % path)
1589
1590 A.addApproval('CRVW', 2)
1591 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1592 self.waitUntilSettled()
1593 jobs = self.fake_jenkins.job_history
1594 job_names = [x['name'] for x in jobs]
1595 assert 'project1-merge' in job_names
1596 assert 'project1-test1' in job_names
1597 assert 'project1-test2' in job_names
1598 assert jobs[0]['result'] == 'SUCCESS'
1599 assert jobs[1]['result'] == 'SUCCESS'
1600 assert jobs[2]['result'] == 'SUCCESS'
1601 assert A.data['status'] == 'MERGED'
1602 assert A.reported == 2
1603 self.assertEmptyQueues()
1604
James E. Blair7ee88a22012-09-12 18:59:31 +02001605 def test_nonexistent_job(self):
1606 "Test launching a job that doesn't exist"
1607 self.fake_jenkins.nonexistent_jobs.append('project-merge')
1608 self.jenkins.launch_retry_timeout = 0.1
1609
1610 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1611 A.addApproval('CRVW', 2)
1612 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1613 # There may be a thread about to report a lost change
1614 while A.reported < 2:
1615 self.waitUntilSettled()
1616 jobs = self.fake_jenkins.job_history
1617 job_names = [x['name'] for x in jobs]
1618 assert not job_names
1619 assert A.data['status'] == 'NEW'
1620 assert A.reported == 2
1621 self.assertEmptyQueues()
1622
1623 # Make sure things still work:
1624 self.fake_jenkins.nonexistent_jobs = []
1625 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1626 A.addApproval('CRVW', 2)
1627 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1628 self.waitUntilSettled()
1629 jobs = self.fake_jenkins.job_history
1630 job_names = [x['name'] for x in jobs]
1631 assert 'project-merge' in job_names
1632 assert 'project-test1' in job_names
1633 assert 'project-test2' in job_names
1634 assert jobs[0]['result'] == 'SUCCESS'
1635 assert jobs[1]['result'] == 'SUCCESS'
1636 assert jobs[2]['result'] == 'SUCCESS'
1637 assert A.data['status'] == 'MERGED'
1638 assert A.reported == 2
1639 self.assertEmptyQueues()
James E. Blairf62d4282012-12-31 17:01:50 -08001640
1641 def test_single_nonexistent_post_job(self):
1642 "Test launching a single post job that doesn't exist"
1643 self.fake_jenkins.nonexistent_jobs.append('project-post')
1644 self.jenkins.launch_retry_timeout = 0.1
1645
1646 e = {
1647 "type": "ref-updated",
1648 "submitter": {
1649 "name": "User Name",
1650 },
1651 "refUpdate": {
1652 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1653 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1654 "refName": "master",
1655 "project": "org/project",
1656 }
1657 }
1658 self.fake_gerrit.addEvent(e)
1659 self.waitUntilSettled()
1660
1661 jobs = self.fake_jenkins.job_history
1662 assert len(jobs) == 0
1663 self.assertEmptyQueues()