blob: 26c2710ed8622957a7f52f588165f876d1cac3df [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. Blair412e5582013-04-22 15:50:12 -070031import select
32import statsd
James E. Blair4886cc12012-07-18 15:39:41 -070033import shutil
James E. Blair412e5582013-04-22 15:50:12 -070034import socket
James E. Blair4886f282012-11-15 09:27:33 -080035import string
James E. Blair4886cc12012-07-18 15:39:41 -070036import git
James E. Blairb0fcae42012-07-17 11:12:10 -070037
James E. Blairb0fcae42012-07-17 11:12:10 -070038import zuul.scheduler
39import zuul.launcher.jenkins
40import zuul.trigger.gerrit
41
42FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
43 'fixtures')
44CONFIG = ConfigParser.ConfigParser()
45CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
46
47CONFIG.set('zuul', 'layout_config',
48 os.path.join(FIXTURE_DIR, "layout.yaml"))
49
James E. Blair1dbd5082012-08-23 15:12:15 -070050TMP_ROOT = os.environ.get("ZUUL_TEST_ROOT", "/tmp")
51TEST_ROOT = os.path.join(TMP_ROOT, "zuul-test")
52UPSTREAM_ROOT = os.path.join(TEST_ROOT, "upstream")
53GIT_ROOT = os.path.join(TEST_ROOT, "git")
54
55CONFIG.set('zuul', 'git_dir', GIT_ROOT)
56
James E. Blairb0fcae42012-07-17 11:12:10 -070057logging.basicConfig(level=logging.DEBUG)
58
59
James E. Blair8cc15a82012-08-01 11:17:57 -070060def random_sha1():
61 return hashlib.sha1(str(random.random())).hexdigest()
62
63
James E. Blair4886cc12012-07-18 15:39:41 -070064class ChangeReference(git.Reference):
65 _common_path_default = "refs/changes"
66 _points_to_commits_only = True
67
68
69def init_repo(project):
70 parts = project.split('/')
James E. Blair1dbd5082012-08-23 15:12:15 -070071 path = os.path.join(UPSTREAM_ROOT, *parts[:-1])
James E. Blair4886cc12012-07-18 15:39:41 -070072 if not os.path.exists(path):
73 os.makedirs(path)
James E. Blair1dbd5082012-08-23 15:12:15 -070074 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -070075 repo = git.Repo.init(path)
76
77 fn = os.path.join(path, 'README')
78 f = open(fn, 'w')
79 f.write("test\n")
80 f.close()
81 repo.index.add([fn])
82 repo.index.commit('initial commit')
James E. Blairc6294a52012-08-17 10:19:48 -070083 master = repo.create_head('master')
James E. Blair4886cc12012-07-18 15:39:41 -070084 repo.create_tag('init')
85
James E. Blairc6294a52012-08-17 10:19:48 -070086 mp = repo.create_head('mp')
87 repo.head.reference = mp
88 f = open(fn, 'a')
89 f.write("test mp\n")
90 f.close()
91 repo.index.add([fn])
92 repo.index.commit('mp commit')
93
94 repo.head.reference = master
95 repo.head.reset(index=True, working_tree=True)
96 repo.git.clean('-x', '-f', '-d')
97
James E. Blair4886cc12012-07-18 15:39:41 -070098
James E. Blair4886f282012-11-15 09:27:33 -080099def add_fake_change_to_repo(project, branch, change_num, patchset, msg, fn,
100 large):
James E. Blair1dbd5082012-08-23 15:12:15 -0700101 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -0700102 repo = git.Repo(path)
103 ref = ChangeReference.create(repo, '1/%s/%s' % (change_num,
104 patchset),
105 'refs/tags/init')
106 repo.head.reference = ref
107 repo.head.reset(index=True, working_tree=True)
108 repo.git.clean('-x', '-f', '-d')
109
James E. Blair1dbd5082012-08-23 15:12:15 -0700110 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair4886f282012-11-15 09:27:33 -0800111 if not large:
112 fn = os.path.join(path, fn)
113 f = open(fn, 'w')
114 f.write("test %s %s %s\n" % (branch, change_num, patchset))
115 f.close()
116 repo.index.add([fn])
117 else:
118 for fni in range(100):
119 fn = os.path.join(path, str(fni))
120 f = open(fn, 'w')
121 for ci in range(4096):
122 f.write(random.choice(string.printable))
123 f.close()
124 repo.index.add([fn])
125
James E. Blairdaabed22012-08-15 15:38:57 -0700126 return repo.index.commit(msg)
James E. Blair4886cc12012-07-18 15:39:41 -0700127
128
129def ref_has_change(ref, change):
James E. Blair1dbd5082012-08-23 15:12:15 -0700130 path = os.path.join(GIT_ROOT, change.project)
James E. Blair4886cc12012-07-18 15:39:41 -0700131 repo = git.Repo(path)
132 for commit in repo.iter_commits(ref):
133 if commit.message.strip() == ('%s-1' % change.subject):
134 return True
135 return False
136
137
138def job_has_changes(*args):
139 job = args[0]
140 commits = args[1:]
141 project = job.parameters['ZUUL_PROJECT']
James E. Blair1dbd5082012-08-23 15:12:15 -0700142 path = os.path.join(GIT_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -0700143 repo = git.Repo(path)
144 ref = job.parameters['ZUUL_REF']
James E. Blair81515ad2012-10-01 18:29:08 -0700145 sha = job.parameters['ZUUL_COMMIT']
James E. Blair4886cc12012-07-18 15:39:41 -0700146 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
James E. Blair81515ad2012-10-01 18:29:08 -0700147 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
James E. Blair4886cc12012-07-18 15:39:41 -0700148 commit_messages = ['%s-1' % commit.subject for commit in commits]
James E. Blair4886cc12012-07-18 15:39:41 -0700149 for msg in commit_messages:
150 if msg not in repo_messages:
151 return False
James E. Blair81515ad2012-10-01 18:29:08 -0700152 if repo_shas[0] != sha:
153 return False
James E. Blair4886cc12012-07-18 15:39:41 -0700154 return True
155
156
James E. Blairb0fcae42012-07-17 11:12:10 -0700157class FakeChange(object):
James E. Blair8c803f82012-07-31 16:25:42 -0700158 categories = {'APRV': ('Approved', -1, 1),
159 'CRVW': ('Code-Review', -2, 2),
160 'VRFY': ('Verified', -2, 2)}
James E. Blairb0fcae42012-07-17 11:12:10 -0700161
James E. Blair8cc15a82012-08-01 11:17:57 -0700162 def __init__(self, gerrit, number, project, branch, subject, status='NEW'):
163 self.gerrit = gerrit
James E. Blaird466dc42012-07-31 10:42:56 -0700164 self.reported = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700165 self.queried = 0
James E. Blairb0fcae42012-07-17 11:12:10 -0700166 self.patchsets = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700167 self.number = number
168 self.project = project
169 self.branch = branch
170 self.subject = subject
171 self.latest_patchset = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700172 self.depends_on_change = None
173 self.needed_by_changes = []
James E. Blair127bc182012-08-28 15:55:15 -0700174 self.fail_merge = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700175 self.data = {
176 'branch': branch,
177 'comments': [],
178 'commitMessage': subject,
179 'createdOn': time.time(),
James E. Blair8cc15a82012-08-01 11:17:57 -0700180 'id': 'I' + random_sha1(),
James E. Blairb0fcae42012-07-17 11:12:10 -0700181 'lastUpdated': time.time(),
182 'number': str(number),
183 'open': True,
184 'owner': {'email': 'user@example.com',
185 'name': 'User Name',
186 'username': 'username'},
187 'patchSets': self.patchsets,
188 'project': project,
189 'status': status,
190 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700191 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700192 'url': 'https://hostname/%s' % number}
193
194 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700195 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700196
James E. Blair70c71582013-03-06 08:50:50 -0800197 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700198 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700199 if files:
200 fn = files[0]
201 else:
202 fn = '%s-%s' % (self.branch, self.number)
203 msg = self.subject + '-' + str(self.latest_patchset)
204 c = add_fake_change_to_repo(self.project, self.branch,
205 self.number, self.latest_patchset,
James E. Blair4886f282012-11-15 09:27:33 -0800206 msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800207 ps_files = [{'file': '/COMMIT_MSG',
208 'type': 'ADDED'},
209 {'file': 'README',
210 'type': 'MODIFIED'}]
211 for f in files:
212 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700213 d = {'approvals': [],
214 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800215 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700216 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700217 'ref': 'refs/changes/1/%s/%s' % (self.number,
218 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700219 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700220 'uploader': {'email': 'user@example.com',
221 'name': 'User name',
222 'username': 'user'}}
223 self.data['currentPatchSet'] = d
224 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700225 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700226
James E. Blaire0487072012-08-29 17:38:31 -0700227 def getPatchsetCreatedEvent(self, patchset):
228 event = {"type": "patchset-created",
229 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800230 "branch": self.branch,
231 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
232 "number": str(self.number),
233 "subject": self.subject,
234 "owner": {"name": "User Name"},
235 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700236 "patchSet": self.patchsets[patchset - 1],
237 "uploader": {"name": "User Name"}}
238 return event
239
James E. Blairb0fcae42012-07-17 11:12:10 -0700240 def addApproval(self, category, value):
James E. Blair8c803f82012-07-31 16:25:42 -0700241 approval = {'description': self.categories[category][0],
242 'type': category,
243 'value': str(value)}
244 self.patchsets[-1]['approvals'].append(approval)
245 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700246 'author': {'email': 'user@example.com',
247 'name': 'User Name',
248 'username': 'username'},
249 'change': {'branch': self.branch,
250 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
251 'number': str(self.number),
252 'owner': {'email': 'user@example.com',
253 'name': 'User Name',
254 'username': 'username'},
255 'project': self.project,
256 'subject': self.subject,
257 'topic': 'master',
258 'url': 'https://hostname/459'},
259 'comment': '',
260 'patchSet': self.patchsets[-1],
261 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700262 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700263 return json.loads(json.dumps(event))
264
James E. Blair8c803f82012-07-31 16:25:42 -0700265 def getSubmitRecords(self):
266 status = {}
267 for cat in self.categories.keys():
268 status[cat] = 0
269
270 for a in self.patchsets[-1]['approvals']:
271 cur = status[a['type']]
272 cat_min, cat_max = self.categories[a['type']][1:]
273 new = int(a['value'])
274 if new == cat_min:
275 cur = new
276 elif abs(new) > abs(cur):
277 cur = new
278 status[a['type']] = cur
279
280 labels = []
281 ok = True
282 for typ, cat in self.categories.items():
283 cur = status[typ]
284 cat_min, cat_max = cat[1:]
285 if cur == cat_min:
286 value = 'REJECT'
287 ok = False
288 elif cur == cat_max:
289 value = 'OK'
290 else:
291 value = 'NEED'
292 ok = False
293 labels.append({'label': cat[0], 'status': value})
294 if ok:
295 return [{'status': 'OK'}]
296 return [{'status': 'NOT_READY',
297 'labels': labels}]
298
299 def setDependsOn(self, other, patchset):
300 self.depends_on_change = other
301 d = {'id': other.data['id'],
302 'number': other.data['number'],
303 'ref': other.patchsets[patchset - 1]['ref']
304 }
305 self.data['dependsOn'] = [d]
306
307 other.needed_by_changes.append(self)
308 needed = other.data.get('neededBy', [])
309 d = {'id': self.data['id'],
310 'number': self.data['number'],
311 'ref': self.patchsets[patchset - 1]['ref'],
312 'revision': self.patchsets[patchset - 1]['revision']
313 }
314 needed.append(d)
315 other.data['neededBy'] = needed
316
James E. Blairb0fcae42012-07-17 11:12:10 -0700317 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700318 self.queried += 1
319 d = self.data.get('dependsOn')
320 if d:
321 d = d[0]
322 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
323 d['isCurrentPatchSet'] = True
324 else:
325 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700326 return json.loads(json.dumps(self.data))
327
328 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800329 if (self.depends_on_change and
330 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700331 return
James E. Blair127bc182012-08-28 15:55:15 -0700332 if self.fail_merge:
333 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700334 self.data['status'] = 'MERGED'
335 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700336
James E. Blair1dbd5082012-08-23 15:12:15 -0700337 path = os.path.join(UPSTREAM_ROOT, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700338 repo = git.Repo(path)
339 repo.heads[self.branch].commit = \
340 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700341
James E. Blaird466dc42012-07-31 10:42:56 -0700342 def setReported(self):
343 self.reported += 1
344
James E. Blairb0fcae42012-07-17 11:12:10 -0700345
346class FakeGerrit(object):
347 def __init__(self, *args, **kw):
348 self.event_queue = Queue.Queue()
349 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
350 self.change_number = 0
351 self.changes = {}
352
353 def addFakeChange(self, project, branch, subject):
354 self.change_number += 1
James E. Blair8cc15a82012-08-01 11:17:57 -0700355 c = FakeChange(self, self.change_number, project, branch, subject)
James E. Blairb0fcae42012-07-17 11:12:10 -0700356 self.changes[self.change_number] = c
357 return c
358
359 def addEvent(self, data):
360 return self.event_queue.put(data)
361
362 def getEvent(self):
363 return self.event_queue.get()
364
365 def eventDone(self):
366 self.event_queue.task_done()
367
368 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700369 number, ps = changeid.split(',')
370 change = self.changes[int(number)]
James E. Blairb0fcae42012-07-17 11:12:10 -0700371 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700372 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700373 if message:
374 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700375
376 def query(self, number):
377 change = self.changes[int(number)]
378 return change.query()
379
380 def startWatching(self, *args, **kw):
381 pass
382
383
384class FakeJenkinsEvent(object):
385 def __init__(self, name, number, parameters, phase, status=None):
Zhongyue Luo5d556072012-09-21 02:00:47 +0900386 data = {
387 'build': {
388 'full_url': 'https://server/job/%s/%s/' % (name, number),
389 'number': number,
390 'parameters': parameters,
391 'phase': phase,
392 'url': 'job/%s/%s/' % (name, number),
393 },
394 'name': name,
395 'url': 'job/%s/' % name,
396 }
James E. Blairb0fcae42012-07-17 11:12:10 -0700397 if status:
398 data['build']['status'] = status
399 self.body = json.dumps(data)
400
401
402class FakeJenkinsJob(threading.Thread):
403 log = logging.getLogger("zuul.test")
404
405 def __init__(self, jenkins, callback, name, number, parameters):
406 threading.Thread.__init__(self)
407 self.jenkins = jenkins
408 self.callback = callback
409 self.name = name
410 self.number = number
411 self.parameters = parameters
412 self.wait_condition = threading.Condition()
413 self.waiting = False
James E. Blaird466dc42012-07-31 10:42:56 -0700414 self.aborted = False
415 self.canceled = False
416 self.created = time.time()
James E. Blairb0fcae42012-07-17 11:12:10 -0700417
418 def release(self):
419 self.wait_condition.acquire()
420 self.wait_condition.notify()
421 self.waiting = False
422 self.log.debug("Job %s released" % (self.parameters['UUID']))
423 self.wait_condition.release()
424
425 def isWaiting(self):
426 self.wait_condition.acquire()
427 if self.waiting:
428 ret = True
429 else:
430 ret = False
431 self.wait_condition.release()
432 return ret
433
434 def _wait(self):
435 self.wait_condition.acquire()
436 self.waiting = True
437 self.log.debug("Job %s waiting" % (self.parameters['UUID']))
438 self.wait_condition.wait()
439 self.wait_condition.release()
440
441 def run(self):
442 self.jenkins.fakeEnqueue(self)
443 if self.jenkins.hold_jobs_in_queue:
444 self._wait()
445 self.jenkins.fakeDequeue(self)
James E. Blaird466dc42012-07-31 10:42:56 -0700446 if self.canceled:
447 self.jenkins.all_jobs.remove(self)
448 return
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800449 self.callback.jenkins_endpoint(FakeJenkinsEvent(self.name,
450 self.number,
451 self.parameters,
452 'STARTED'))
James E. Blairb0fcae42012-07-17 11:12:10 -0700453 if self.jenkins.hold_jobs_in_build:
454 self._wait()
455 self.log.debug("Job %s continuing" % (self.parameters['UUID']))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700456
457 result = 'SUCCESS'
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800458 if (('ZUUL_REF' in self.parameters) and
459 self.jenkins.fakeShouldFailTest(self.name,
460 self.parameters['ZUUL_REF'])):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700461 result = 'FAILURE'
James E. Blaird466dc42012-07-31 10:42:56 -0700462 if self.aborted:
463 result = 'ABORTED'
James E. Blairb02a3bb2012-07-30 17:49:55 -0700464
James E. Blair0018a6c2013-02-27 14:11:45 -0800465 changes = None
466 if 'ZUUL_CHANGE_IDS' in self.parameters:
467 changes = self.parameters['ZUUL_CHANGE_IDS']
468
James E. Blairb0fcae42012-07-17 11:12:10 -0700469 self.jenkins.fakeAddHistory(name=self.name, number=self.number,
James E. Blair0018a6c2013-02-27 14:11:45 -0800470 result=result, changes=changes)
James E. Blairff791972013-01-09 11:45:43 -0800471 self.jenkins.lock.acquire()
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800472 self.callback.jenkins_endpoint(FakeJenkinsEvent(self.name,
473 self.number,
474 self.parameters,
475 'COMPLETED',
476 result))
477 self.callback.jenkins_endpoint(FakeJenkinsEvent(self.name,
478 self.number,
479 self.parameters,
480 'FINISHED',
481 result))
James E. Blairb0fcae42012-07-17 11:12:10 -0700482 self.jenkins.all_jobs.remove(self)
James E. Blairff791972013-01-09 11:45:43 -0800483 self.jenkins.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700484
485
486class FakeJenkins(object):
487 log = logging.getLogger("zuul.test")
488
489 def __init__(self, *args, **kw):
490 self.queue = []
491 self.all_jobs = []
492 self.job_counter = {}
James E. Blaird466dc42012-07-31 10:42:56 -0700493 self.queue_counter = 0
James E. Blairb0fcae42012-07-17 11:12:10 -0700494 self.job_history = []
495 self.hold_jobs_in_queue = False
496 self.hold_jobs_in_build = False
James E. Blairb02a3bb2012-07-30 17:49:55 -0700497 self.fail_tests = {}
James E. Blair7ee88a22012-09-12 18:59:31 +0200498 self.nonexistent_jobs = []
James E. Blairff791972013-01-09 11:45:43 -0800499 self.lock = threading.Lock()
James E. Blairb0fcae42012-07-17 11:12:10 -0700500
501 def fakeEnqueue(self, job):
502 self.queue.append(job)
503
504 def fakeDequeue(self, job):
505 self.queue.remove(job)
506
James E. Blair0018a6c2013-02-27 14:11:45 -0800507 class FakeJobHistory(object):
508 def __init__(self, **kw):
509 self.__dict__.update(kw)
510
511 def __repr__(self):
512 return ("<Completed job, result: %s name: %s #%s changes: %s>" %
513 (self.result, self.name, self.number, self.changes))
514
James E. Blairb0fcae42012-07-17 11:12:10 -0700515 def fakeAddHistory(self, **kw):
James E. Blair0018a6c2013-02-27 14:11:45 -0800516 self.job_history.append(self.FakeJobHistory(**kw))
James E. Blairb0fcae42012-07-17 11:12:10 -0700517
518 def fakeRelease(self, regex=None):
519 all_jobs = self.all_jobs[:]
520 self.log.debug("releasing jobs %s (%s)" % (regex, len(self.all_jobs)))
521 for job in all_jobs:
522 if not regex or re.match(regex, job.name):
523 self.log.debug("releasing job %s" % (job.parameters['UUID']))
524 job.release()
525 else:
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800526 self.log.debug("not releasing job %s" %
527 (job.parameters['UUID']))
James E. Blairb0fcae42012-07-17 11:12:10 -0700528 self.log.debug("done releasing jobs %s (%s)" % (regex,
529 len(self.all_jobs)))
530
531 def fakeAllWaiting(self, regex=None):
James E. Blair4aa1ad62012-10-05 12:39:26 -0700532 all_jobs = self.all_jobs[:] + self.queue[:]
James E. Blairb0fcae42012-07-17 11:12:10 -0700533 for job in all_jobs:
534 self.log.debug("job %s %s" % (job.parameters['UUID'],
535 job.isWaiting()))
536 if not job.isWaiting():
537 return False
538 return True
539
James E. Blairb02a3bb2012-07-30 17:49:55 -0700540 def fakeAddFailTest(self, name, change):
541 l = self.fail_tests.get(name, [])
542 l.append(change)
543 self.fail_tests[name] = l
544
James E. Blair4886cc12012-07-18 15:39:41 -0700545 def fakeShouldFailTest(self, name, ref):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700546 l = self.fail_tests.get(name, [])
547 for change in l:
James E. Blair4886cc12012-07-18 15:39:41 -0700548 if ref_has_change(ref, change):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700549 return True
550 return False
551
James E. Blairb0fcae42012-07-17 11:12:10 -0700552 def build_job(self, name, parameters):
James E. Blair7ee88a22012-09-12 18:59:31 +0200553 if name in self.nonexistent_jobs:
554 raise Exception("Job does not exist")
James E. Blairb0fcae42012-07-17 11:12:10 -0700555 count = self.job_counter.get(name, 0)
556 count += 1
557 self.job_counter[name] = count
James E. Blaird466dc42012-07-31 10:42:56 -0700558
559 queue_count = self.queue_counter
560 self.queue_counter += 1
James E. Blairb0fcae42012-07-17 11:12:10 -0700561 job = FakeJenkinsJob(self, self.callback, name, count, parameters)
James E. Blaird466dc42012-07-31 10:42:56 -0700562 job.queue_id = queue_count
563
James E. Blairb0fcae42012-07-17 11:12:10 -0700564 self.all_jobs.append(job)
565 job.start()
566
James E. Blaird466dc42012-07-31 10:42:56 -0700567 def stop_build(self, name, number):
568 for job in self.all_jobs:
569 if job.name == name and job.number == number:
570 job.aborted = True
571 job.release()
572 return
573
574 def cancel_queue(self, id):
575 for job in self.queue:
576 if job.queue_id == id:
577 job.canceled = True
578 job.release()
579 return
580
581 def get_queue_info(self):
582 items = []
James E. Blair1490eba2013-03-06 19:14:00 -0800583 for job in self.queue[:]:
584 self.log.debug("Queue info: %s %s" % (job.name,
585 job.parameters['UUID']))
James E. Blaird466dc42012-07-31 10:42:56 -0700586 paramstr = ''
587 paramlst = []
588 d = {'actions': [{'parameters': paramlst},
589 {'causes': [{'shortDescription':
590 'Started by user Jenkins',
591 'userId': 'jenkins',
592 'userName': 'Jenkins'}]}],
593 'blocked': False,
594 'buildable': True,
595 'buildableStartMilliseconds': (job.created * 1000) + 5,
596 'id': job.queue_id,
597 'inQueueSince': (job.created * 1000),
598 'params': paramstr,
599 'stuck': False,
600 'task': {'color': 'blue',
601 'name': job.name,
602 'url': 'https://server/job/%s/' % job.name},
603 'why': 'Waiting for next available executor'}
604 for k, v in job.parameters.items():
605 paramstr += "\n(StringParameterValue) %s='%s'" % (k, v)
606 pd = {'name': k, 'value': v}
607 paramlst.append(pd)
608 items.append(d)
609 return items
610
James E. Blairb0fcae42012-07-17 11:12:10 -0700611 def set_build_description(self, *args, **kw):
612 pass
613
614
615class FakeJenkinsCallback(zuul.launcher.jenkins.JenkinsCallback):
616 def start(self):
617 pass
618
619
James E. Blair8cc15a82012-08-01 11:17:57 -0700620class FakeURLOpener(object):
621 def __init__(self, fake_gerrit, url):
622 self.fake_gerrit = fake_gerrit
623 self.url = url
624
625 def read(self):
626 res = urlparse.urlparse(self.url)
627 path = res.path
628 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200629 ret = '001e# service=git-upload-pack\n'
630 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
631 'multi_ack thin-pack side-band side-band-64k ofs-delta '
632 'shallow no-progress include-tag multi_ack_detailed no-done\n')
James E. Blair1dbd5082012-08-23 15:12:15 -0700633 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700634 repo = git.Repo(path)
635 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200636 r = ref.object.hexsha + ' ' + ref.path + '\n'
637 ret += '%04x%s' % (len(r) + 4, r)
638 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700639 return ret
640
641
James E. Blair4886cc12012-07-18 15:39:41 -0700642class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
643 def getGitUrl(self, project):
James E. Blair1dbd5082012-08-23 15:12:15 -0700644 return os.path.join(UPSTREAM_ROOT, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700645
646
James E. Blair412e5582013-04-22 15:50:12 -0700647class FakeStatsd(threading.Thread):
648 def __init__(self):
649 threading.Thread.__init__(self)
650 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
651 self.sock.bind(('', 0))
652 self.port = self.sock.getsockname()[1]
653 self.wake_read, self.wake_write = os.pipe()
654 self.stats = []
655
656 def run(self):
657 while True:
658 poll = select.poll()
659 poll.register(self.sock, select.POLLIN)
660 poll.register(self.wake_read, select.POLLIN)
661 ret = poll.poll()
662 for (fd, event) in ret:
663 if fd == self.sock.fileno():
664 data = self.sock.recvfrom(1024)
665 if not data:
666 return
667 self.stats.append(data[0])
668 if fd == self.wake_read:
669 return
670
671 def stop(self):
672 os.write(self.wake_write, '1\n')
673
674
James E. Blairb0fcae42012-07-17 11:12:10 -0700675class testScheduler(unittest.TestCase):
676 log = logging.getLogger("zuul.test")
677
678 def setUp(self):
James E. Blair1dbd5082012-08-23 15:12:15 -0700679 if os.path.exists(TEST_ROOT):
680 shutil.rmtree(TEST_ROOT)
681 os.makedirs(TEST_ROOT)
682 os.makedirs(UPSTREAM_ROOT)
683 os.makedirs(GIT_ROOT)
James E. Blair4886cc12012-07-18 15:39:41 -0700684
685 # For each project in config:
686 init_repo("org/project")
687 init_repo("org/project1")
688 init_repo("org/project2")
James E. Blair127bc182012-08-28 15:55:15 -0700689 init_repo("org/project3")
James E. Blair7f71c802012-08-22 13:04:32 -0700690 init_repo("org/one-job-project")
James E. Blair4ec821f2012-08-23 15:28:28 -0700691 init_repo("org/nonvoting-project")
Antoine Musso80edd5a2013-02-13 15:37:53 +0100692 init_repo("org/templated-project")
James E. Blairb0fcae42012-07-17 11:12:10 -0700693 self.config = CONFIG
James E. Blair412e5582013-04-22 15:50:12 -0700694
695 self.statsd = FakeStatsd()
696 os.environ['STATSD_HOST'] = 'localhost'
697 os.environ['STATSD_PORT'] = str(self.statsd.port)
698 self.statsd.start()
699 # the statsd client object is configured in the statsd module import
700 reload(statsd)
701 reload(zuul.scheduler)
702
James E. Blairb0fcae42012-07-17 11:12:10 -0700703 self.sched = zuul.scheduler.Scheduler()
704
705 def jenkinsFactory(*args, **kw):
706 self.fake_jenkins = FakeJenkins()
707 return self.fake_jenkins
708
709 def jenkinsCallbackFactory(*args, **kw):
710 self.fake_jenkins_callback = FakeJenkinsCallback(*args, **kw)
711 return self.fake_jenkins_callback
712
James E. Blair8cc15a82012-08-01 11:17:57 -0700713 def URLOpenerFactory(*args, **kw):
714 args = [self.fake_gerrit] + list(args)
715 return FakeURLOpener(*args, **kw)
716
James E. Blairb0fcae42012-07-17 11:12:10 -0700717 zuul.launcher.jenkins.ExtendedJenkins = jenkinsFactory
718 zuul.launcher.jenkins.JenkinsCallback = jenkinsCallbackFactory
James E. Blair8cc15a82012-08-01 11:17:57 -0700719 urllib2.urlopen = URLOpenerFactory
James E. Blairb0fcae42012-07-17 11:12:10 -0700720 self.jenkins = zuul.launcher.jenkins.Jenkins(self.config, self.sched)
721 self.fake_jenkins.callback = self.fake_jenkins_callback
722
723 zuul.lib.gerrit.Gerrit = FakeGerrit
724
James E. Blair4886cc12012-07-18 15:39:41 -0700725 self.gerrit = FakeGerritTrigger(self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700726 self.gerrit.replication_timeout = 1.5
727 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700728 self.fake_gerrit = self.gerrit.gerrit
729
730 self.sched.setLauncher(self.jenkins)
731 self.sched.setTrigger(self.gerrit)
732
733 self.sched.start()
734 self.sched.reconfigure(self.config)
735 self.sched.resume()
736
737 def tearDown(self):
738 self.jenkins.stop()
739 self.gerrit.stop()
740 self.sched.stop()
741 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700742 self.statsd.stop()
743 self.statsd.join()
James E. Blair1dbd5082012-08-23 15:12:15 -0700744 #shutil.rmtree(TEST_ROOT)
James E. Blairb0fcae42012-07-17 11:12:10 -0700745
746 def waitUntilSettled(self):
747 self.log.debug("Waiting until settled...")
748 start = time.time()
749 while True:
750 if time.time() - start > 10:
751 print 'queue status:',
752 print self.sched.trigger_event_queue.empty(),
753 print self.sched.result_event_queue.empty(),
754 print self.fake_gerrit.event_queue.empty(),
755 raise Exception("Timeout waiting for Zuul to settle")
James E. Blairff791972013-01-09 11:45:43 -0800756 # Make sure our fake jenkins doesn't end any jobs
757 # (and therefore, emit events) while we're checking
758 self.fake_jenkins.lock.acquire()
759 # Join ensures that the queue is empty _and_ events have been
760 # processed
James E. Blairb0fcae42012-07-17 11:12:10 -0700761 self.fake_gerrit.event_queue.join()
James E. Blairff791972013-01-09 11:45:43 -0800762 self.sched.trigger_event_queue.join()
763 self.sched.result_event_queue.join()
James E. Blairb0fcae42012-07-17 11:12:10 -0700764 if (self.sched.trigger_event_queue.empty() and
765 self.sched.result_event_queue.empty() and
766 self.fake_gerrit.event_queue.empty() and
767 self.fake_jenkins.fakeAllWaiting()):
James E. Blairff791972013-01-09 11:45:43 -0800768 self.fake_jenkins.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700769 self.log.debug("...settled.")
770 return
James E. Blairff791972013-01-09 11:45:43 -0800771 self.fake_jenkins.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700772 self.sched.wake_event.wait(0.1)
773
James E. Blaird466dc42012-07-31 10:42:56 -0700774 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -0800775 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -0700776 return len(jobs)
777
James E. Blaire0487072012-08-29 17:38:31 -0700778 def assertEmptyQueues(self):
779 # Make sure there are no orphaned jobs
780 for pipeline in self.sched.pipelines.values():
781 for queue in pipeline.queues:
782 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -0800783 print 'pipeline %s queue %s contents %s' % (
784 pipeline.name, queue.name, queue.queue)
James E. Blaire0487072012-08-29 17:38:31 -0700785 assert len(queue.queue) == 0
786 if len(queue.severed_heads) != 0:
787 print 'heads', queue.severed_heads
788 assert len(queue.severed_heads) == 0
789
James E. Blair412e5582013-04-22 15:50:12 -0700790 def assertReportedStat(self, key, value=None):
791 start = time.time()
792 while time.time() < (start + 5):
793 for stat in self.statsd.stats:
794 k, v = stat.split(':')
795 if key == k:
796 if value is None:
797 return
798 if value == v:
799 return
800 time.sleep(0.1)
801
802 pprint.pprint(self.statsd.stats)
803 raise Exception("Key %s not found in reported stats" % key)
804
James E. Blairb0fcae42012-07-17 11:12:10 -0700805 def test_jobs_launched(self):
806 "Test that jobs are launched and a change is merged"
807 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -0700808 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700809 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
810 self.waitUntilSettled()
811 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -0800812 job_names = [x.name for x in jobs]
James E. Blairb0fcae42012-07-17 11:12:10 -0700813 assert 'project-merge' in job_names
814 assert 'project-test1' in job_names
815 assert 'project-test2' in job_names
James E. Blair0018a6c2013-02-27 14:11:45 -0800816 assert jobs[0].result == 'SUCCESS'
817 assert jobs[1].result == 'SUCCESS'
818 assert jobs[2].result == 'SUCCESS'
James E. Blairb0fcae42012-07-17 11:12:10 -0700819 assert A.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700820 assert A.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700821 self.assertEmptyQueues()
James E. Blairb0fcae42012-07-17 11:12:10 -0700822
James E. Blair412e5582013-04-22 15:50:12 -0700823 self.assertReportedStat('gerrit.event.comment-added', '1|c')
824 self.assertReportedStat('zuul.pipeline.gate.current_changes', '1|g')
825 self.assertReportedStat('zuul.job.project-merge')
826 self.assertReportedStat('zuul.pipeline.gate.resident_time')
827 self.assertReportedStat('zuul.pipeline.gate.total_changes', '1|c')
828 self.assertReportedStat(
829 'zuul.pipeline.gate.org.project.resident_time')
830 self.assertReportedStat(
831 'zuul.pipeline.gate.org.project.total_changes', '1|c')
832
James E. Blairb0fcae42012-07-17 11:12:10 -0700833 def test_parallel_changes(self):
834 "Test that changes are tested in parallel and merged in series"
835 self.fake_jenkins.hold_jobs_in_build = True
836 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
837 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
838 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700839 A.addApproval('CRVW', 2)
840 B.addApproval('CRVW', 2)
841 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700842
843 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
844 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
845 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
846
847 self.waitUntilSettled()
848 jobs = self.fake_jenkins.all_jobs
849 assert len(jobs) == 1
850 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700851 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700852
853 self.fake_jenkins.fakeRelease('.*-merge')
854 self.waitUntilSettled()
855 assert len(jobs) == 3
856 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700857 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700858 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700859 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700860 assert jobs[2].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700861 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700862
863 self.fake_jenkins.fakeRelease('.*-merge')
864 self.waitUntilSettled()
865 assert len(jobs) == 5
866 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700867 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700868 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700869 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700870
871 assert jobs[2].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700872 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700873 assert jobs[3].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700874 assert job_has_changes(jobs[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700875
876 assert jobs[4].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700877 assert job_has_changes(jobs[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700878
879 self.fake_jenkins.fakeRelease('.*-merge')
880 self.waitUntilSettled()
881 assert len(jobs) == 6
882 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700883 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700884 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700885 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700886
887 assert jobs[2].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700888 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700889 assert jobs[3].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700890 assert job_has_changes(jobs[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700891
892 assert jobs[4].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700893 assert job_has_changes(jobs[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700894 assert jobs[5].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700895 assert job_has_changes(jobs[5], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700896
897 self.fake_jenkins.hold_jobs_in_build = False
898 self.fake_jenkins.fakeRelease()
899 self.waitUntilSettled()
900 assert len(jobs) == 0
901
902 jobs = self.fake_jenkins.job_history
903 assert len(jobs) == 9
904 assert A.data['status'] == 'MERGED'
905 assert B.data['status'] == 'MERGED'
906 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700907 assert A.reported == 2
908 assert B.reported == 2
909 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700910 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700911
912 def test_failed_changes(self):
913 "Test that a change behind a failed change is retested"
914 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
915 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -0700916 A.addApproval('CRVW', 2)
917 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700918
919 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
920 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
921
James E. Blair4886cc12012-07-18 15:39:41 -0700922 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700923
924 self.waitUntilSettled()
925 jobs = self.fake_jenkins.job_history
926 assert len(jobs) > 6
927 assert A.data['status'] == 'NEW'
928 assert B.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700929 assert A.reported == 2
930 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700931 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700932
933 def test_independent_queues(self):
934 "Test that changes end up in the right queues"
935 self.fake_jenkins.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900936 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700937 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
938 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700939 A.addApproval('CRVW', 2)
940 B.addApproval('CRVW', 2)
941 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700942
943 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
944 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
945 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
946
947 jobs = self.fake_jenkins.all_jobs
948 self.waitUntilSettled()
949
950 # There should be one merge job at the head of each queue running
951 assert len(jobs) == 2
952 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700953 assert job_has_changes(jobs[0], A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700954 assert jobs[1].name == 'project1-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700955 assert job_has_changes(jobs[1], B)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700956
957 # Release the current merge jobs
958 self.fake_jenkins.fakeRelease('.*-merge')
959 self.waitUntilSettled()
960 # Release the merge job for project2 which is behind project1
961 self.fake_jenkins.fakeRelease('.*-merge')
962 self.waitUntilSettled()
963
964 # All the test jobs should be running:
965 # project1 (3) + project2 (3) + project (2) = 8
966 assert len(jobs) == 8
967
968 self.fake_jenkins.fakeRelease()
969 self.waitUntilSettled()
970 assert len(jobs) == 0
971
972 jobs = self.fake_jenkins.job_history
973 assert len(jobs) == 11
974 assert A.data['status'] == 'MERGED'
975 assert B.data['status'] == 'MERGED'
976 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700977 assert A.reported == 2
978 assert B.reported == 2
979 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700980 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -0700981
982 def test_failed_change_at_head(self):
983 "Test that if a change at the head fails, jobs behind it are canceled"
984 self.fake_jenkins.hold_jobs_in_build = True
985
986 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
987 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
988 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700989 A.addApproval('CRVW', 2)
990 B.addApproval('CRVW', 2)
991 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700992
James E. Blair4886cc12012-07-18 15:39:41 -0700993 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700994
995 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
996 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
997 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
998
999 self.waitUntilSettled()
1000 jobs = self.fake_jenkins.all_jobs
1001 finished_jobs = self.fake_jenkins.job_history
1002
1003 assert len(jobs) == 1
1004 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -07001005 assert job_has_changes(jobs[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -07001006
1007 self.fake_jenkins.fakeRelease('.*-merge')
1008 self.waitUntilSettled()
1009 self.fake_jenkins.fakeRelease('.*-merge')
1010 self.waitUntilSettled()
1011 self.fake_jenkins.fakeRelease('.*-merge')
1012 self.waitUntilSettled()
1013
1014 assert len(jobs) == 6
1015 assert jobs[0].name == 'project-test1'
1016 assert jobs[1].name == 'project-test2'
1017 assert jobs[2].name == 'project-test1'
1018 assert jobs[3].name == 'project-test2'
1019 assert jobs[4].name == 'project-test1'
1020 assert jobs[5].name == 'project-test2'
1021
1022 jobs[0].release()
1023 self.waitUntilSettled()
1024
James E. Blairec590122012-08-22 15:19:31 -07001025 assert len(jobs) == 2 # project-test2, project-merge for B
James E. Blaird466dc42012-07-31 10:42:56 -07001026 assert self.countJobResults(finished_jobs, 'ABORTED') == 4
1027
1028 self.fake_jenkins.hold_jobs_in_build = False
1029 self.fake_jenkins.fakeRelease()
1030 self.waitUntilSettled()
1031
James E. Blair0018a6c2013-02-27 14:11:45 -08001032 for x in jobs:
1033 print x
1034 for x in finished_jobs:
1035 print x
1036
James E. Blaird466dc42012-07-31 10:42:56 -07001037 assert len(jobs) == 0
1038 assert len(finished_jobs) == 15
1039 assert A.data['status'] == 'NEW'
1040 assert B.data['status'] == 'MERGED'
1041 assert C.data['status'] == 'MERGED'
1042 assert A.reported == 2
1043 assert B.reported == 2
1044 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001045 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -07001046
1047 def test_failed_change_at_head_with_queue(self):
1048 "Test that if a change at the head fails, queued jobs are canceled"
1049 self.fake_jenkins.hold_jobs_in_queue = True
1050
1051 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1052 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1053 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001054 A.addApproval('CRVW', 2)
1055 B.addApproval('CRVW', 2)
1056 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001057
James E. Blair4886cc12012-07-18 15:39:41 -07001058 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001059
1060 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1061 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1062 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1063
1064 self.waitUntilSettled()
1065 jobs = self.fake_jenkins.all_jobs
1066 finished_jobs = self.fake_jenkins.job_history
1067 queue = self.fake_jenkins.queue
1068
1069 assert len(jobs) == 1
1070 assert len(queue) == 1
1071 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -07001072 assert job_has_changes(jobs[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -07001073
1074 self.fake_jenkins.fakeRelease('.*-merge')
1075 self.waitUntilSettled()
1076 self.fake_jenkins.fakeRelease('.*-merge')
1077 self.waitUntilSettled()
1078 self.fake_jenkins.fakeRelease('.*-merge')
1079 self.waitUntilSettled()
1080
1081 assert len(jobs) == 6
1082 assert len(queue) == 6
1083 assert jobs[0].name == 'project-test1'
1084 assert jobs[1].name == 'project-test2'
1085 assert jobs[2].name == 'project-test1'
1086 assert jobs[3].name == 'project-test2'
1087 assert jobs[4].name == 'project-test1'
1088 assert jobs[5].name == 'project-test2'
1089
1090 jobs[0].release()
1091 self.waitUntilSettled()
1092
James E. Blairec590122012-08-22 15:19:31 -07001093 assert len(jobs) == 2 # project-test2, project-merge for B
1094 assert len(queue) == 2
James E. Blaird466dc42012-07-31 10:42:56 -07001095 assert self.countJobResults(finished_jobs, 'ABORTED') == 0
1096
1097 self.fake_jenkins.hold_jobs_in_queue = False
1098 self.fake_jenkins.fakeRelease()
1099 self.waitUntilSettled()
1100
1101 assert len(jobs) == 0
1102 assert len(finished_jobs) == 11
1103 assert A.data['status'] == 'NEW'
1104 assert B.data['status'] == 'MERGED'
1105 assert C.data['status'] == 'MERGED'
1106 assert A.reported == 2
1107 assert B.reported == 2
1108 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001109 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001110
1111 def test_patch_order(self):
1112 "Test that dependent patches are tested in the right order"
1113 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1114 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1115 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1116 A.addApproval('CRVW', 2)
1117 B.addApproval('CRVW', 2)
1118 C.addApproval('CRVW', 2)
1119
1120 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1121 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1122 M2.setMerged()
1123 M1.setMerged()
1124
1125 # C -> B -> A -> M1 -> M2
1126 # M2 is here to make sure it is never queried. If it is, it
1127 # means zuul is walking down the entire history of merged
1128 # changes.
1129
1130 C.setDependsOn(B, 1)
1131 B.setDependsOn(A, 1)
1132 A.setDependsOn(M1, 1)
1133 M1.setDependsOn(M2, 1)
1134
1135 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1136
1137 self.waitUntilSettled()
1138
1139 assert A.data['status'] == 'NEW'
1140 assert B.data['status'] == 'NEW'
1141 assert C.data['status'] == 'NEW'
1142
1143 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1144 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1145
1146 self.waitUntilSettled()
1147 assert M2.queried == 0
1148 assert A.data['status'] == 'MERGED'
1149 assert B.data['status'] == 'MERGED'
1150 assert C.data['status'] == 'MERGED'
1151 assert A.reported == 2
1152 assert B.reported == 2
1153 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001154 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001155
1156 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001157 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001158 # TODO: move to test_gerrit (this is a unit test!)
1159 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4aea70c2012-07-26 14:23:24 -07001160 a = self.sched.trigger.getChange(1, 2)
1161 mgr = self.sched.pipelines['gate'].manager
1162 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001163
1164 A.addApproval('CRVW', 2)
James E. Blair4aea70c2012-07-26 14:23:24 -07001165 a = self.sched.trigger.getChange(1, 2)
1166 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001167
1168 A.addApproval('APRV', 1)
James E. Blair4aea70c2012-07-26 14:23:24 -07001169 a = self.sched.trigger.getChange(1, 2)
1170 assert self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blaire0487072012-08-29 17:38:31 -07001171 self.assertEmptyQueues()
James E. Blair4886cc12012-07-18 15:39:41 -07001172
1173 def test_build_configuration(self):
1174 "Test that zuul merges the right commits for testing"
1175 self.fake_jenkins.hold_jobs_in_queue = True
1176 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1177 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1178 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1179 A.addApproval('CRVW', 2)
1180 B.addApproval('CRVW', 2)
1181 C.addApproval('CRVW', 2)
1182 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1183 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1184 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1185 self.waitUntilSettled()
1186
1187 jobs = self.fake_jenkins.all_jobs
1188
1189 self.fake_jenkins.fakeRelease('.*-merge')
1190 self.waitUntilSettled()
1191 self.fake_jenkins.fakeRelease('.*-merge')
1192 self.waitUntilSettled()
1193 self.fake_jenkins.fakeRelease('.*-merge')
1194 self.waitUntilSettled()
James E. Blair1dbd5082012-08-23 15:12:15 -07001195
James E. Blair4886cc12012-07-18 15:39:41 -07001196 ref = jobs[-1].parameters['ZUUL_REF']
1197 self.fake_jenkins.hold_jobs_in_queue = False
1198 self.fake_jenkins.fakeRelease()
James E. Blair973721f2012-08-15 10:19:43 -07001199 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001200
James E. Blair1dbd5082012-08-23 15:12:15 -07001201 path = os.path.join(GIT_ROOT, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001202 repo = git.Repo(path)
1203 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1204 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001205 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
1206 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001207 self.assertEmptyQueues()
James E. Blair973721f2012-08-15 10:19:43 -07001208
1209 def test_build_configuration_conflict(self):
1210 "Test that merge conflicts are handled"
1211 self.fake_jenkins.hold_jobs_in_queue = True
1212 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1213 A.addPatchset(['conflict'])
1214 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1215 B.addPatchset(['conflict'])
1216 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1217 A.addApproval('CRVW', 2)
1218 B.addApproval('CRVW', 2)
1219 C.addApproval('CRVW', 2)
1220 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1221 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1222 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1223 self.waitUntilSettled()
1224
1225 jobs = self.fake_jenkins.all_jobs
1226
1227 self.fake_jenkins.fakeRelease('.*-merge')
1228 self.waitUntilSettled()
1229 self.fake_jenkins.fakeRelease('.*-merge')
1230 self.waitUntilSettled()
1231 self.fake_jenkins.fakeRelease('.*-merge')
1232 self.waitUntilSettled()
1233 ref = jobs[-1].parameters['ZUUL_REF']
1234 self.fake_jenkins.hold_jobs_in_queue = False
1235 self.fake_jenkins.fakeRelease()
1236 self.waitUntilSettled()
1237
1238 assert A.data['status'] == 'MERGED'
1239 assert B.data['status'] == 'NEW'
1240 assert C.data['status'] == 'MERGED'
1241 assert A.reported == 2
1242 assert B.reported == 2
1243 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001244 self.assertEmptyQueues()
James E. Blairdaabed22012-08-15 15:38:57 -07001245
1246 def test_post(self):
1247 "Test that post jobs run"
Zhongyue Luo5d556072012-09-21 02:00:47 +09001248 e = {
1249 "type": "ref-updated",
1250 "submitter": {
1251 "name": "User Name",
1252 },
1253 "refUpdate": {
1254 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1255 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1256 "refName": "master",
1257 "project": "org/project",
1258 }
1259 }
James E. Blairdaabed22012-08-15 15:38:57 -07001260 self.fake_gerrit.addEvent(e)
1261 self.waitUntilSettled()
1262
1263 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001264 job_names = [x.name for x in jobs]
James E. Blairdaabed22012-08-15 15:38:57 -07001265 assert len(jobs) == 1
1266 assert 'project-post' in job_names
James E. Blaire0487072012-08-29 17:38:31 -07001267 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001268
1269 def test_build_configuration_branch(self):
1270 "Test that the right commits are on alternate branches"
1271 self.fake_jenkins.hold_jobs_in_queue = True
1272 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1273 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1274 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1275 A.addApproval('CRVW', 2)
1276 B.addApproval('CRVW', 2)
1277 C.addApproval('CRVW', 2)
1278 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1279 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1280 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1281 self.waitUntilSettled()
1282
1283 jobs = self.fake_jenkins.all_jobs
1284
1285 self.fake_jenkins.fakeRelease('.*-merge')
1286 self.waitUntilSettled()
1287 self.fake_jenkins.fakeRelease('.*-merge')
1288 self.waitUntilSettled()
1289 self.fake_jenkins.fakeRelease('.*-merge')
1290 self.waitUntilSettled()
1291 ref = jobs[-1].parameters['ZUUL_REF']
1292 self.fake_jenkins.hold_jobs_in_queue = False
1293 self.fake_jenkins.fakeRelease()
1294 self.waitUntilSettled()
1295
James E. Blair1dbd5082012-08-23 15:12:15 -07001296 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001297 repo = git.Repo(path)
1298 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1299 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001300 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
1301 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001302 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001303
1304 def test_build_configuration_branch_interaction(self):
1305 "Test that switching between branches works"
1306 self.test_build_configuration()
1307 self.test_build_configuration_branch()
1308 # C has been merged, undo that
James E. Blair1dbd5082012-08-23 15:12:15 -07001309 path = os.path.join(UPSTREAM_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001310 repo = git.Repo(path)
1311 repo.heads.master.commit = repo.commit('init')
1312 self.test_build_configuration()
James E. Blaire0487072012-08-29 17:38:31 -07001313 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001314
1315 def test_build_configuration_multi_branch(self):
1316 "Test that dependent changes on multiple branches are merged"
1317 self.fake_jenkins.hold_jobs_in_queue = True
1318 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1319 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1320 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1321 A.addApproval('CRVW', 2)
1322 B.addApproval('CRVW', 2)
1323 C.addApproval('CRVW', 2)
1324 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1325 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1326 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1327 self.waitUntilSettled()
1328
1329 jobs = self.fake_jenkins.all_jobs
1330
1331 self.fake_jenkins.fakeRelease('.*-merge')
1332 self.waitUntilSettled()
1333 ref_mp = jobs[-1].parameters['ZUUL_REF']
1334 self.fake_jenkins.fakeRelease('.*-merge')
1335 self.waitUntilSettled()
1336 self.fake_jenkins.fakeRelease('.*-merge')
1337 self.waitUntilSettled()
1338 ref_master = jobs[-1].parameters['ZUUL_REF']
1339 self.fake_jenkins.hold_jobs_in_queue = False
1340 self.fake_jenkins.fakeRelease()
1341 self.waitUntilSettled()
1342
James E. Blair1dbd5082012-08-23 15:12:15 -07001343 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001344 repo = git.Repo(path)
1345
1346 repo_messages = [c.message.strip()
1347 for c in repo.iter_commits(ref_master)]
1348 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001349 correct_messages = ['initial commit', 'A-1', 'C-1']
1350 assert repo_messages == correct_messages
1351
1352 repo_messages = [c.message.strip()
1353 for c in repo.iter_commits(ref_mp)]
1354 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001355 correct_messages = ['initial commit', 'mp commit', 'B-1']
1356 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001357 self.assertEmptyQueues()
James E. Blair7f71c802012-08-22 13:04:32 -07001358
1359 def test_one_job_project(self):
1360 "Test that queueing works with one job"
1361 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1362 'master', 'A')
1363 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1364 'master', 'B')
1365 A.addApproval('CRVW', 2)
1366 B.addApproval('CRVW', 2)
1367 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1368 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1369 self.waitUntilSettled()
1370
1371 jobs = self.fake_jenkins.all_jobs
1372 finished_jobs = self.fake_jenkins.job_history
James E. Blair7f71c802012-08-22 13:04:32 -07001373
1374 assert A.data['status'] == 'MERGED'
1375 assert A.reported == 2
1376 assert B.data['status'] == 'MERGED'
1377 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001378 self.assertEmptyQueues()
James E. Blaircaec0c52012-08-22 14:52:22 -07001379
Antoine Musso80edd5a2013-02-13 15:37:53 +01001380 def test_job_from_templates_launched(self):
1381 "Test whether a job generated via a template can be launched"
1382 A = self.fake_gerrit.addFakeChange(
1383 'org/templated-project', 'master', 'A')
1384 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1385 self.waitUntilSettled()
1386 jobs = self.fake_jenkins.job_history
1387 job_names = [x.name for x in jobs]
1388
1389 assert 'project-test1' in job_names
1390 assert 'project-test2' in job_names
1391 assert jobs[0].result == 'SUCCESS'
1392 assert jobs[1].result == 'SUCCESS'
1393
James E. Blaircaec0c52012-08-22 14:52:22 -07001394 def test_dependent_changes_dequeue(self):
1395 "Test that dependent patches are not needlessly tested"
1396 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1397 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1398 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1399 A.addApproval('CRVW', 2)
1400 B.addApproval('CRVW', 2)
1401 C.addApproval('CRVW', 2)
1402
1403 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1404 M1.setMerged()
1405
1406 # C -> B -> A -> M1
1407
1408 C.setDependsOn(B, 1)
1409 B.setDependsOn(A, 1)
1410 A.setDependsOn(M1, 1)
1411
1412 self.fake_jenkins.fakeAddFailTest('project-merge', A)
1413
1414 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1415 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1416 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1417
1418 self.waitUntilSettled()
1419
1420 jobs = self.fake_jenkins.all_jobs
1421 finished_jobs = self.fake_jenkins.job_history
1422
James E. Blair127bc182012-08-28 15:55:15 -07001423 for x in jobs:
1424 print x
1425 for x in finished_jobs:
1426 print x
1427
James E. Blairec590122012-08-22 15:19:31 -07001428 assert A.data['status'] == 'NEW'
1429 assert A.reported == 2
1430 assert B.data['status'] == 'NEW'
1431 assert B.reported == 2
1432 assert C.data['status'] == 'NEW'
1433 assert C.reported == 2
1434 assert len(finished_jobs) == 1
James E. Blaire0487072012-08-29 17:38:31 -07001435 self.assertEmptyQueues()
James E. Blairec590122012-08-22 15:19:31 -07001436
1437 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001438 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001439 # If it's dequeued more than once, we should see extra
1440 # aborted jobs.
1441 self.fake_jenkins.hold_jobs_in_build = True
1442
1443 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1444 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1445 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1446 A.addApproval('CRVW', 2)
1447 B.addApproval('CRVW', 2)
1448 C.addApproval('CRVW', 2)
1449
1450 self.fake_jenkins.fakeAddFailTest('project1-test1', A)
1451 self.fake_jenkins.fakeAddFailTest('project1-test2', A)
1452 self.fake_jenkins.fakeAddFailTest('project1-project2-integration', A)
1453
1454 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1455 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1456 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1457
1458 self.waitUntilSettled()
1459 jobs = self.fake_jenkins.all_jobs
1460 finished_jobs = self.fake_jenkins.job_history
1461
1462 assert len(jobs) == 1
1463 assert jobs[0].name == 'project1-merge'
1464 assert job_has_changes(jobs[0], A)
1465
1466 self.fake_jenkins.fakeRelease('.*-merge')
1467 self.waitUntilSettled()
1468 self.fake_jenkins.fakeRelease('.*-merge')
1469 self.waitUntilSettled()
1470 self.fake_jenkins.fakeRelease('.*-merge')
1471 self.waitUntilSettled()
1472
1473 assert len(jobs) == 9
1474 assert jobs[0].name == 'project1-test1'
1475 assert jobs[1].name == 'project1-test2'
1476 assert jobs[2].name == 'project1-project2-integration'
1477 assert jobs[3].name == 'project1-test1'
1478 assert jobs[4].name == 'project1-test2'
1479 assert jobs[5].name == 'project1-project2-integration'
1480 assert jobs[6].name == 'project1-test1'
1481 assert jobs[7].name == 'project1-test2'
1482 assert jobs[8].name == 'project1-project2-integration'
1483
1484 jobs[0].release()
1485 self.waitUntilSettled()
1486
1487 assert len(jobs) == 3 # test2, integration, merge for B
1488 assert self.countJobResults(finished_jobs, 'ABORTED') == 6
1489
1490 self.fake_jenkins.hold_jobs_in_build = False
1491 self.fake_jenkins.fakeRelease()
1492 self.waitUntilSettled()
1493
1494 assert len(jobs) == 0
1495 assert len(finished_jobs) == 20
James E. Blaircaec0c52012-08-22 14:52:22 -07001496
1497 assert A.data['status'] == 'NEW'
James E. Blairec590122012-08-22 15:19:31 -07001498 assert B.data['status'] == 'MERGED'
1499 assert C.data['status'] == 'MERGED'
1500 assert A.reported == 2
1501 assert B.reported == 2
1502 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001503 self.assertEmptyQueues()
James E. Blair4ec821f2012-08-23 15:28:28 -07001504
1505 def test_nonvoting_job(self):
1506 "Test that non-voting jobs don't vote."
1507 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1508 'master', 'A')
1509 A.addApproval('CRVW', 2)
1510 self.fake_jenkins.fakeAddFailTest('nonvoting-project-test2', A)
1511 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1512
1513 self.waitUntilSettled()
1514 jobs = self.fake_jenkins.all_jobs
1515 finished_jobs = self.fake_jenkins.job_history
1516
1517 assert A.data['status'] == 'MERGED'
1518 assert A.reported == 2
James E. Blair0018a6c2013-02-27 14:11:45 -08001519 assert finished_jobs[0].result == 'SUCCESS'
1520 assert finished_jobs[1].result == 'SUCCESS'
1521 assert finished_jobs[2].result == 'FAILURE'
James E. Blaire0487072012-08-29 17:38:31 -07001522 self.assertEmptyQueues()
1523
1524 def test_check_queue_success(self):
1525 "Test successful check queue jobs."
1526 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1527 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1528
1529 self.waitUntilSettled()
1530 jobs = self.fake_jenkins.all_jobs
1531 finished_jobs = self.fake_jenkins.job_history
1532
1533 assert A.data['status'] == 'NEW'
1534 assert A.reported == 1
James E. Blair0018a6c2013-02-27 14:11:45 -08001535 assert finished_jobs[0].result == 'SUCCESS'
1536 assert finished_jobs[1].result == 'SUCCESS'
1537 assert finished_jobs[2].result == 'SUCCESS'
James E. Blaire0487072012-08-29 17:38:31 -07001538 self.assertEmptyQueues()
1539
1540 def test_check_queue_failure(self):
1541 "Test failed check queue jobs."
1542 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1543 self.fake_jenkins.fakeAddFailTest('project-test2', A)
1544 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1545
1546 self.waitUntilSettled()
1547 jobs = self.fake_jenkins.all_jobs
1548 finished_jobs = self.fake_jenkins.job_history
1549
1550 assert A.data['status'] == 'NEW'
1551 assert A.reported == 1
James E. Blair0018a6c2013-02-27 14:11:45 -08001552 assert finished_jobs[0].result == 'SUCCESS'
1553 assert finished_jobs[1].result == 'SUCCESS'
1554 assert finished_jobs[2].result == 'FAILURE'
James E. Blaire0487072012-08-29 17:38:31 -07001555 self.assertEmptyQueues()
James E. Blair127bc182012-08-28 15:55:15 -07001556
1557 def test_dependent_behind_dequeue(self):
1558 "test that dependent changes behind dequeued changes work"
1559 # This complicated test is a reproduction of a real life bug
1560 self.sched.reconfigure(self.config)
1561 self.fake_jenkins.hold_jobs_in_build = True
1562
1563 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1564 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1565 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1566 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1567 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1568 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1569 D.setDependsOn(C, 1)
1570 E.setDependsOn(D, 1)
1571 A.addApproval('CRVW', 2)
1572 B.addApproval('CRVW', 2)
1573 C.addApproval('CRVW', 2)
1574 D.addApproval('CRVW', 2)
1575 E.addApproval('CRVW', 2)
1576 F.addApproval('CRVW', 2)
1577
1578 A.fail_merge = True
1579 jobs = self.fake_jenkins.all_jobs
1580 finished_jobs = self.fake_jenkins.job_history
1581
1582 # Change object re-use in the gerrit trigger is hidden if
1583 # changes are added in quick succession; waiting makes it more
1584 # like real life.
1585 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1586 self.waitUntilSettled()
1587 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1588 self.waitUntilSettled()
1589
1590 self.fake_jenkins.fakeRelease('.*-merge')
1591 self.waitUntilSettled()
1592 self.fake_jenkins.fakeRelease('.*-merge')
1593 self.waitUntilSettled()
1594
1595 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1596 self.waitUntilSettled()
1597 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1598 self.waitUntilSettled()
1599 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1600 self.waitUntilSettled()
1601 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1602 self.waitUntilSettled()
1603
1604 self.fake_jenkins.fakeRelease('.*-merge')
1605 self.waitUntilSettled()
1606 self.fake_jenkins.fakeRelease('.*-merge')
1607 self.waitUntilSettled()
1608 self.fake_jenkins.fakeRelease('.*-merge')
1609 self.waitUntilSettled()
1610 self.fake_jenkins.fakeRelease('.*-merge')
1611 self.waitUntilSettled()
1612
James E. Blair4aa1ad62012-10-05 12:39:26 -07001613 for x in jobs:
1614 print x
James E. Blair127bc182012-08-28 15:55:15 -07001615 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001616
1617 # Grab pointers to the jobs we want to release before
1618 # releasing any, because list indexes may change as
1619 # the jobs complete.
1620 a, b, c = jobs[:3]
1621 a.release()
1622 b.release()
1623 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001624 self.waitUntilSettled()
1625
1626 self.fake_jenkins.hold_jobs_in_build = False
1627 self.fake_jenkins.fakeRelease()
1628 self.waitUntilSettled()
1629
1630 for x in jobs:
1631 print x
1632 for x in finished_jobs:
1633 print x
1634 print self.sched.formatStatusHTML()
1635
1636 assert A.data['status'] == 'NEW'
1637 assert B.data['status'] == 'MERGED'
1638 assert C.data['status'] == 'MERGED'
1639 assert D.data['status'] == 'MERGED'
1640 assert E.data['status'] == 'MERGED'
1641 assert F.data['status'] == 'MERGED'
1642
1643 assert A.reported == 2
1644 assert B.reported == 2
1645 assert C.reported == 2
1646 assert D.reported == 2
1647 assert E.reported == 2
1648 assert F.reported == 2
1649
James E. Blair127bc182012-08-28 15:55:15 -07001650 assert self.countJobResults(finished_jobs, 'ABORTED') == 15
1651 assert len(finished_jobs) == 44
James E. Blaire0487072012-08-29 17:38:31 -07001652 self.assertEmptyQueues()
James E. Blair05fed602012-09-07 12:45:24 -07001653
1654 def test_merger_repack(self):
1655 "Test that the merger works after a repack"
1656 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1657 A.addApproval('CRVW', 2)
1658 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1659 self.waitUntilSettled()
1660 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001661 job_names = [x.name for x in jobs]
James E. Blair05fed602012-09-07 12:45:24 -07001662 assert 'project-merge' in job_names
1663 assert 'project-test1' in job_names
1664 assert 'project-test2' in job_names
James E. Blair0018a6c2013-02-27 14:11:45 -08001665 assert jobs[0].result == 'SUCCESS'
1666 assert jobs[1].result == 'SUCCESS'
1667 assert jobs[2].result == 'SUCCESS'
James E. Blair05fed602012-09-07 12:45:24 -07001668 assert A.data['status'] == 'MERGED'
1669 assert A.reported == 2
1670 self.assertEmptyQueues()
1671
1672 path = os.path.join(GIT_ROOT, "org/project")
1673 os.system('git --git-dir=%s/.git repack -afd' % path)
1674
1675 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1676 A.addApproval('CRVW', 2)
1677 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1678 self.waitUntilSettled()
1679 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001680 job_names = [x.name for x in jobs]
James E. Blair05fed602012-09-07 12:45:24 -07001681 assert 'project-merge' in job_names
1682 assert 'project-test1' in job_names
1683 assert 'project-test2' in job_names
James E. Blair0018a6c2013-02-27 14:11:45 -08001684 assert jobs[0].result == 'SUCCESS'
1685 assert jobs[1].result == 'SUCCESS'
1686 assert jobs[2].result == 'SUCCESS'
James E. Blair05fed602012-09-07 12:45:24 -07001687 assert A.data['status'] == 'MERGED'
1688 assert A.reported == 2
1689 self.assertEmptyQueues()
James E. Blair7ee88a22012-09-12 18:59:31 +02001690
James E. Blair4886f282012-11-15 09:27:33 -08001691 def test_merger_repack_large_change(self):
1692 "Test that the merger works with large changes after a repack"
1693 # https://bugs.launchpad.net/zuul/+bug/1078946
1694 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1695 A.addPatchset(large=True)
1696 path = os.path.join(UPSTREAM_ROOT, "org/project1")
1697 os.system('git --git-dir=%s/.git repack -afd' % path)
1698 path = os.path.join(GIT_ROOT, "org/project1")
1699 os.system('git --git-dir=%s/.git repack -afd' % path)
1700
1701 A.addApproval('CRVW', 2)
1702 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1703 self.waitUntilSettled()
1704 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001705 job_names = [x.name for x in jobs]
James E. Blair4886f282012-11-15 09:27:33 -08001706 assert 'project1-merge' in job_names
1707 assert 'project1-test1' in job_names
1708 assert 'project1-test2' in job_names
James E. Blair0018a6c2013-02-27 14:11:45 -08001709 assert jobs[0].result == 'SUCCESS'
1710 assert jobs[1].result == 'SUCCESS'
1711 assert jobs[2].result == 'SUCCESS'
James E. Blair4886f282012-11-15 09:27:33 -08001712 assert A.data['status'] == 'MERGED'
1713 assert A.reported == 2
1714 self.assertEmptyQueues()
1715
James E. Blair7ee88a22012-09-12 18:59:31 +02001716 def test_nonexistent_job(self):
1717 "Test launching a job that doesn't exist"
1718 self.fake_jenkins.nonexistent_jobs.append('project-merge')
1719 self.jenkins.launch_retry_timeout = 0.1
1720
1721 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1722 A.addApproval('CRVW', 2)
1723 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1724 # There may be a thread about to report a lost change
1725 while A.reported < 2:
1726 self.waitUntilSettled()
1727 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001728 job_names = [x.name for x in jobs]
James E. Blair7ee88a22012-09-12 18:59:31 +02001729 assert not job_names
1730 assert A.data['status'] == 'NEW'
1731 assert A.reported == 2
1732 self.assertEmptyQueues()
1733
1734 # Make sure things still work:
1735 self.fake_jenkins.nonexistent_jobs = []
1736 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1737 A.addApproval('CRVW', 2)
1738 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1739 self.waitUntilSettled()
1740 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001741 job_names = [x.name for x in jobs]
James E. Blair7ee88a22012-09-12 18:59:31 +02001742 assert 'project-merge' in job_names
1743 assert 'project-test1' in job_names
1744 assert 'project-test2' in job_names
James E. Blair0018a6c2013-02-27 14:11:45 -08001745 assert jobs[0].result == 'SUCCESS'
1746 assert jobs[1].result == 'SUCCESS'
1747 assert jobs[2].result == 'SUCCESS'
James E. Blair7ee88a22012-09-12 18:59:31 +02001748 assert A.data['status'] == 'MERGED'
1749 assert A.reported == 2
1750 self.assertEmptyQueues()
James E. Blairf62d4282012-12-31 17:01:50 -08001751
1752 def test_single_nonexistent_post_job(self):
1753 "Test launching a single post job that doesn't exist"
1754 self.fake_jenkins.nonexistent_jobs.append('project-post')
1755 self.jenkins.launch_retry_timeout = 0.1
1756
1757 e = {
1758 "type": "ref-updated",
1759 "submitter": {
1760 "name": "User Name",
1761 },
1762 "refUpdate": {
1763 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1764 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1765 "refName": "master",
1766 "project": "org/project",
1767 }
1768 }
1769 self.fake_gerrit.addEvent(e)
1770 self.waitUntilSettled()
1771
1772 jobs = self.fake_jenkins.job_history
1773 assert len(jobs) == 0
1774 self.assertEmptyQueues()
James E. Blair2fa50962013-01-30 21:50:41 -08001775
1776 def test_new_patchset_dequeues_old(self):
1777 "Test that a new patchset causes the old to be dequeued"
1778 # D -> C (depends on B) -> B (depends on A) -> A -> M
1779 self.fake_jenkins.hold_jobs_in_build = True
1780
1781 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1782 M.setMerged()
1783
1784 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1785 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1786 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1787 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1788 A.addApproval('CRVW', 2)
1789 B.addApproval('CRVW', 2)
1790 C.addApproval('CRVW', 2)
1791 D.addApproval('CRVW', 2)
1792
1793 C.setDependsOn(B, 1)
1794 B.setDependsOn(A, 1)
1795 A.setDependsOn(M, 1)
1796
1797 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1798 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1799 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1800 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1801 self.waitUntilSettled()
1802
1803 B.addPatchset()
1804 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1805 self.waitUntilSettled()
1806
1807 self.fake_jenkins.hold_jobs_in_build = False
1808 self.fake_jenkins.fakeRelease()
1809 self.waitUntilSettled()
1810
1811 jobs = self.fake_jenkins.all_jobs
1812 finished_jobs = self.fake_jenkins.job_history
1813
1814 for x in jobs:
1815 print x
1816 for x in finished_jobs:
1817 print x
1818
1819 assert A.data['status'] == 'MERGED'
1820 assert A.reported == 2
1821 assert B.data['status'] == 'NEW'
1822 assert B.reported == 2
1823 assert C.data['status'] == 'NEW'
1824 assert C.reported == 2
1825 assert D.data['status'] == 'MERGED'
1826 assert D.reported == 2
1827 assert len(finished_jobs) == 9 # 3 each for A, B, D.
1828 self.assertEmptyQueues()
1829
1830 def test_new_patchset_dequeues_old_on_head(self):
1831 "Test that a new patchset causes the old to be dequeued (at head)"
1832 # D -> C (depends on B) -> B (depends on A) -> A -> M
1833 self.fake_jenkins.hold_jobs_in_build = True
1834
1835 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1836 M.setMerged()
1837 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1838 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1839 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1840 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1841 A.addApproval('CRVW', 2)
1842 B.addApproval('CRVW', 2)
1843 C.addApproval('CRVW', 2)
1844 D.addApproval('CRVW', 2)
1845
1846 C.setDependsOn(B, 1)
1847 B.setDependsOn(A, 1)
1848 A.setDependsOn(M, 1)
1849
1850 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1851 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1852 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1853 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1854 self.waitUntilSettled()
1855
1856 A.addPatchset()
1857 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1858 self.waitUntilSettled()
1859
1860 self.fake_jenkins.hold_jobs_in_build = False
1861 self.fake_jenkins.fakeRelease()
1862 self.waitUntilSettled()
1863
1864 jobs = self.fake_jenkins.all_jobs
1865 finished_jobs = self.fake_jenkins.job_history
1866
1867 for x in jobs:
1868 print x
1869 for x in finished_jobs:
1870 print x
1871
1872 assert A.data['status'] == 'NEW'
1873 assert A.reported == 2
1874 assert B.data['status'] == 'NEW'
1875 assert B.reported == 2
1876 assert C.data['status'] == 'NEW'
1877 assert C.reported == 2
1878 assert D.data['status'] == 'MERGED'
1879 assert D.reported == 2
1880 assert len(finished_jobs) == 7
1881 self.assertEmptyQueues()
1882
1883 def test_new_patchset_dequeues_old_without_dependents(self):
1884 "Test that a new patchset causes only the old to be dequeued"
1885 self.fake_jenkins.hold_jobs_in_build = True
1886
1887 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1888 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1889 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1890 A.addApproval('CRVW', 2)
1891 B.addApproval('CRVW', 2)
1892 C.addApproval('CRVW', 2)
1893
1894 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1895 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1896 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1897 self.waitUntilSettled()
1898
1899 B.addPatchset()
1900 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1901 self.waitUntilSettled()
1902
1903 self.fake_jenkins.hold_jobs_in_build = False
1904 self.fake_jenkins.fakeRelease()
1905 self.waitUntilSettled()
1906
1907 jobs = self.fake_jenkins.all_jobs
1908 finished_jobs = self.fake_jenkins.job_history
1909
1910 for x in jobs:
1911 print x
1912 for x in finished_jobs:
1913 print x
1914
1915 assert A.data['status'] == 'MERGED'
1916 assert A.reported == 2
1917 assert B.data['status'] == 'NEW'
1918 assert B.reported == 2
1919 assert C.data['status'] == 'MERGED'
1920 assert C.reported == 2
1921 assert len(finished_jobs) == 9
1922 self.assertEmptyQueues()
1923
1924 def test_new_patchset_dequeues_old_independent_queue(self):
1925 "Test that a new patchset causes the old to be dequeued (independent)"
1926 self.fake_jenkins.hold_jobs_in_build = True
1927
1928 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1929 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1930 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1931 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1932 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1933 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1934 self.waitUntilSettled()
1935
1936 B.addPatchset()
1937 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1938 self.waitUntilSettled()
1939
1940 self.fake_jenkins.hold_jobs_in_build = False
1941 self.fake_jenkins.fakeRelease()
1942 self.waitUntilSettled()
1943
1944 jobs = self.fake_jenkins.all_jobs
1945 finished_jobs = self.fake_jenkins.job_history
1946
1947 for x in jobs:
1948 print x
1949 for x in finished_jobs:
1950 print x
1951
1952 assert A.data['status'] == 'NEW'
1953 assert A.reported == 1
1954 assert B.data['status'] == 'NEW'
1955 assert B.reported == 1
1956 assert C.data['status'] == 'NEW'
1957 assert C.reported == 1
1958 assert len(finished_jobs) == 10
1959 assert self.countJobResults(finished_jobs, 'ABORTED') == 1
1960 self.assertEmptyQueues()
James E. Blair7d0dedc2013-02-21 17:26:09 -08001961
1962 def test_zuul_refs(self):
1963 "Test that zuul refs exist and have the right changes"
1964 self.fake_jenkins.hold_jobs_in_build = True
1965
1966 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1967 M1.setMerged()
1968 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1969 M2.setMerged()
1970
1971 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1972 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1973 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1974 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1975 A.addApproval('CRVW', 2)
1976 B.addApproval('CRVW', 2)
1977 C.addApproval('CRVW', 2)
1978 D.addApproval('CRVW', 2)
1979 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1980 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1981 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1982 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1983
1984 self.waitUntilSettled()
1985 self.fake_jenkins.fakeRelease('.*-merge')
1986 self.waitUntilSettled()
1987 self.fake_jenkins.fakeRelease('.*-merge')
1988 self.waitUntilSettled()
1989 self.fake_jenkins.fakeRelease('.*-merge')
1990 self.waitUntilSettled()
1991 self.fake_jenkins.fakeRelease('.*-merge')
1992 self.waitUntilSettled()
1993
1994 jobs = self.fake_jenkins.all_jobs
1995 finished_jobs = self.fake_jenkins.job_history
1996
1997 a_zref = b_zref = c_zref = d_zref = None
1998 for x in jobs:
1999 if x.parameters['ZUUL_CHANGE'] == '3':
2000 a_zref = x.parameters['ZUUL_REF']
2001 if x.parameters['ZUUL_CHANGE'] == '4':
2002 b_zref = x.parameters['ZUUL_REF']
2003 if x.parameters['ZUUL_CHANGE'] == '5':
2004 c_zref = x.parameters['ZUUL_REF']
2005 if x.parameters['ZUUL_CHANGE'] == '6':
2006 d_zref = x.parameters['ZUUL_REF']
2007
2008 # There are... four... refs.
2009 assert a_zref is not None
2010 assert b_zref is not None
2011 assert c_zref is not None
2012 assert d_zref is not None
2013
2014 # And they should all be different
2015 refs = set([a_zref, b_zref, c_zref, d_zref])
2016 assert len(refs) == 4
2017
2018 # a ref should have a, not b, and should not be in project2
2019 assert ref_has_change(a_zref, A)
2020 assert not ref_has_change(a_zref, B)
2021 assert not ref_has_change(a_zref, M2)
2022
2023 # b ref should have a and b, and should not be in project2
2024 assert ref_has_change(b_zref, A)
2025 assert ref_has_change(b_zref, B)
2026 assert not ref_has_change(b_zref, M2)
2027
2028 # c ref should have a and b in 1, c in 2
2029 assert ref_has_change(c_zref, A)
2030 assert ref_has_change(c_zref, B)
2031 assert ref_has_change(c_zref, C)
2032 assert not ref_has_change(c_zref, D)
2033
2034 # d ref should have a and b in 1, c and d in 2
2035 assert ref_has_change(d_zref, A)
2036 assert ref_has_change(d_zref, B)
2037 assert ref_has_change(d_zref, C)
2038 assert ref_has_change(d_zref, D)
2039
2040 self.fake_jenkins.hold_jobs_in_build = False
2041 self.fake_jenkins.fakeRelease()
2042 self.waitUntilSettled()
2043
2044 assert A.data['status'] == 'MERGED'
2045 assert A.reported == 2
2046 assert B.data['status'] == 'MERGED'
2047 assert B.reported == 2
2048 assert C.data['status'] == 'MERGED'
2049 assert C.reported == 2
2050 assert D.data['status'] == 'MERGED'
2051 assert D.reported == 2
2052 self.assertEmptyQueues()
James E. Blair70c71582013-03-06 08:50:50 -08002053
James E. Blair412e5582013-04-22 15:50:12 -07002054 def test_statsd(self):
2055 "Test each of the statsd methods used in the scheduler"
2056 import extras
2057 statsd = extras.try_import('statsd.statsd')
2058 statsd.incr('test-incr')
2059 statsd.timing('test-timing', 3)
2060 statsd.gauge('test-guage', 12)
2061 self.assertReportedStat('test-incr', '1|c')
2062 self.assertReportedStat('test-timing', '3|ms')
2063 self.assertReportedStat('test-guage', '12|g')
2064
James E. Blair70c71582013-03-06 08:50:50 -08002065 def test_file_jobs(self):
2066 "Test that file jobs run only when appropriate"
2067 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2068 A.addPatchset(['pip-requires'])
2069 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2070 A.addApproval('CRVW', 2)
2071 B.addApproval('CRVW', 2)
2072 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2073 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2074 self.waitUntilSettled()
2075
2076 jobs = self.fake_jenkins.all_jobs
2077 finished_jobs = self.fake_jenkins.job_history
2078
2079 testfile_jobs = [x for x in finished_jobs
2080 if x.name == 'project-testfile']
2081
2082 assert len(testfile_jobs) == 1
2083 assert testfile_jobs[0].changes == '1,2'
2084 assert A.data['status'] == 'MERGED'
2085 assert A.reported == 2
2086 assert B.data['status'] == 'MERGED'
2087 assert B.reported == 2
2088 self.assertEmptyQueues()
James E. Blair3c5e5b52013-04-26 11:17:03 -07002089
2090 def test_test_config(self):
2091 "Test that we can test the config"
2092 sched = zuul.scheduler.Scheduler()
2093 sched.testConfig(CONFIG.get('zuul', 'layout_config'))