blob: 92104dc031b2593343ff9219062bccdb32000cb6 [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
James E. Blairb0fcae42012-07-17 11:12:10 -070017import ConfigParser
Monty Taylorbc758832013-06-17 17:22:42 -040018from cStringIO import StringIO
James E. Blair8cc15a82012-08-01 11:17:57 -070019import hashlib
James E. Blairb0fcae42012-07-17 11:12:10 -070020import json
Monty Taylorbc758832013-06-17 17:22:42 -040021import logging
22import os
James E. Blairb0fcae42012-07-17 11:12:10 -070023import pprint
Monty Taylorbc758832013-06-17 17:22:42 -040024import Queue
25import random
James E. Blairb0fcae42012-07-17 11:12:10 -070026import re
James E. Blair412e5582013-04-22 15:50:12 -070027import select
James E. Blair4886cc12012-07-18 15:39:41 -070028import shutil
James E. Blair412e5582013-04-22 15:50:12 -070029import socket
James E. Blair4886f282012-11-15 09:27:33 -080030import string
Monty Taylorbc758832013-06-17 17:22:42 -040031import subprocess
32import tempfile
33import threading
34import time
35import urllib2
36import urlparse
37
James E. Blair4886cc12012-07-18 15:39:41 -070038import git
James E. Blair1f4c2bb2013-04-26 08:40:46 -070039import gear
Monty Taylorbc758832013-06-17 17:22:42 -040040import fixtures
41import statsd
42import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070043
James E. Blairb0fcae42012-07-17 11:12:10 -070044import zuul.scheduler
James E. Blair1f4c2bb2013-04-26 08:40:46 -070045import zuul.launcher.gearman
James E. Blairb0fcae42012-07-17 11:12:10 -070046import zuul.trigger.gerrit
47
48FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
49 'fixtures')
50CONFIG = ConfigParser.ConfigParser()
51CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
52
53CONFIG.set('zuul', 'layout_config',
54 os.path.join(FIXTURE_DIR, "layout.yaml"))
55
James E. Blair1f4c2bb2013-04-26 08:40:46 -070056logging.basicConfig(level=logging.DEBUG,
57 format='%(asctime)s %(name)-32s '
58 '%(levelname)-8s %(message)s')
James E. Blairb0fcae42012-07-17 11:12:10 -070059
60
Monty Taylorbc758832013-06-17 17:22:42 -040061def repack_repo(path):
62 output = subprocess.Popen(
63 ['git', '--git-dir=%s/.git' % path, 'repack', '-afd'],
64 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
65 out = output.communicate()
66 if output.returncode:
67 raise Exception("git repack returned %d" % output.returncode)
68 return out
69
70
James E. Blair8cc15a82012-08-01 11:17:57 -070071def random_sha1():
72 return hashlib.sha1(str(random.random())).hexdigest()
73
74
James E. Blair4886cc12012-07-18 15:39:41 -070075class ChangeReference(git.Reference):
76 _common_path_default = "refs/changes"
77 _points_to_commits_only = True
78
79
James E. Blairb0fcae42012-07-17 11:12:10 -070080class FakeChange(object):
James E. Blair8c803f82012-07-31 16:25:42 -070081 categories = {'APRV': ('Approved', -1, 1),
82 'CRVW': ('Code-Review', -2, 2),
83 'VRFY': ('Verified', -2, 2)}
James E. Blairb0fcae42012-07-17 11:12:10 -070084
Monty Taylorbc758832013-06-17 17:22:42 -040085 def __init__(self, gerrit, number, project, branch, subject,
86 status='NEW', upstream_root=None):
James E. Blair8cc15a82012-08-01 11:17:57 -070087 self.gerrit = gerrit
James E. Blaird466dc42012-07-31 10:42:56 -070088 self.reported = 0
James E. Blair8c803f82012-07-31 16:25:42 -070089 self.queried = 0
James E. Blairb0fcae42012-07-17 11:12:10 -070090 self.patchsets = []
James E. Blairb0fcae42012-07-17 11:12:10 -070091 self.number = number
92 self.project = project
93 self.branch = branch
94 self.subject = subject
95 self.latest_patchset = 0
James E. Blair8c803f82012-07-31 16:25:42 -070096 self.depends_on_change = None
97 self.needed_by_changes = []
James E. Blair127bc182012-08-28 15:55:15 -070098 self.fail_merge = False
James E. Blair42f74822013-05-14 15:18:03 -070099 self.messages = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700100 self.data = {
101 'branch': branch,
102 'comments': [],
103 'commitMessage': subject,
104 'createdOn': time.time(),
James E. Blair8cc15a82012-08-01 11:17:57 -0700105 'id': 'I' + random_sha1(),
James E. Blairb0fcae42012-07-17 11:12:10 -0700106 'lastUpdated': time.time(),
107 'number': str(number),
108 'open': True,
109 'owner': {'email': 'user@example.com',
110 'name': 'User Name',
111 'username': 'username'},
112 'patchSets': self.patchsets,
113 'project': project,
114 'status': status,
115 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700116 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700117 'url': 'https://hostname/%s' % number}
118
Monty Taylorbc758832013-06-17 17:22:42 -0400119 self.upstream_root = upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700120 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700121 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700122
Monty Taylorbc758832013-06-17 17:22:42 -0400123 def add_fake_change_to_repo(self, msg, fn, large):
124 path = os.path.join(self.upstream_root, self.project)
125 repo = git.Repo(path)
126 ref = ChangeReference.create(repo, '1/%s/%s' % (self.number,
127 self.latest_patchset),
128 'refs/tags/init')
129 repo.head.reference = ref
130 repo.head.reset(index=True, working_tree=True)
131 repo.git.clean('-x', '-f', '-d')
132
133 path = os.path.join(self.upstream_root, self.project)
134 if not large:
135 fn = os.path.join(path, fn)
136 f = open(fn, 'w')
137 f.write("test %s %s %s\n" %
138 (self.branch, self.number, self.latest_patchset))
139 f.close()
140 repo.index.add([fn])
141 else:
142 for fni in range(100):
143 fn = os.path.join(path, str(fni))
144 f = open(fn, 'w')
145 for ci in range(4096):
146 f.write(random.choice(string.printable))
147 f.close()
148 repo.index.add([fn])
149
150 return repo.index.commit(msg)
151
James E. Blair70c71582013-03-06 08:50:50 -0800152 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700153 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700154 if files:
155 fn = files[0]
156 else:
157 fn = '%s-%s' % (self.branch, self.number)
158 msg = self.subject + '-' + str(self.latest_patchset)
Monty Taylorbc758832013-06-17 17:22:42 -0400159 c = self.add_fake_change_to_repo(msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800160 ps_files = [{'file': '/COMMIT_MSG',
161 'type': 'ADDED'},
162 {'file': 'README',
163 'type': 'MODIFIED'}]
164 for f in files:
165 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700166 d = {'approvals': [],
167 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800168 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700169 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700170 'ref': 'refs/changes/1/%s/%s' % (self.number,
171 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700172 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700173 'uploader': {'email': 'user@example.com',
174 'name': 'User name',
175 'username': 'user'}}
176 self.data['currentPatchSet'] = d
177 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700178 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700179
James E. Blaire0487072012-08-29 17:38:31 -0700180 def getPatchsetCreatedEvent(self, patchset):
181 event = {"type": "patchset-created",
182 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800183 "branch": self.branch,
184 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
185 "number": str(self.number),
186 "subject": self.subject,
187 "owner": {"name": "User Name"},
188 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700189 "patchSet": self.patchsets[patchset - 1],
190 "uploader": {"name": "User Name"}}
191 return event
192
James E. Blair42f74822013-05-14 15:18:03 -0700193 def getChangeRestoredEvent(self):
194 event = {"type": "change-restored",
195 "change": {"project": self.project,
196 "branch": self.branch,
197 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
198 "number": str(self.number),
199 "subject": self.subject,
200 "owner": {"name": "User Name"},
201 "url": "https://hostname/3"},
202 "restorer": {"name": "User Name"},
203 "reason": ""}
204 return event
205
James E. Blairb0fcae42012-07-17 11:12:10 -0700206 def addApproval(self, category, value):
James E. Blair8c803f82012-07-31 16:25:42 -0700207 approval = {'description': self.categories[category][0],
208 'type': category,
209 'value': str(value)}
210 self.patchsets[-1]['approvals'].append(approval)
211 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700212 'author': {'email': 'user@example.com',
213 'name': 'User Name',
214 'username': 'username'},
215 'change': {'branch': self.branch,
216 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
217 'number': str(self.number),
218 'owner': {'email': 'user@example.com',
219 'name': 'User Name',
220 'username': 'username'},
221 'project': self.project,
222 'subject': self.subject,
223 'topic': 'master',
224 'url': 'https://hostname/459'},
225 'comment': '',
226 'patchSet': self.patchsets[-1],
227 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700228 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700229 return json.loads(json.dumps(event))
230
James E. Blair8c803f82012-07-31 16:25:42 -0700231 def getSubmitRecords(self):
232 status = {}
233 for cat in self.categories.keys():
234 status[cat] = 0
235
236 for a in self.patchsets[-1]['approvals']:
237 cur = status[a['type']]
238 cat_min, cat_max = self.categories[a['type']][1:]
239 new = int(a['value'])
240 if new == cat_min:
241 cur = new
242 elif abs(new) > abs(cur):
243 cur = new
244 status[a['type']] = cur
245
246 labels = []
247 ok = True
248 for typ, cat in self.categories.items():
249 cur = status[typ]
250 cat_min, cat_max = cat[1:]
251 if cur == cat_min:
252 value = 'REJECT'
253 ok = False
254 elif cur == cat_max:
255 value = 'OK'
256 else:
257 value = 'NEED'
258 ok = False
259 labels.append({'label': cat[0], 'status': value})
260 if ok:
261 return [{'status': 'OK'}]
262 return [{'status': 'NOT_READY',
263 'labels': labels}]
264
265 def setDependsOn(self, other, patchset):
266 self.depends_on_change = other
267 d = {'id': other.data['id'],
268 'number': other.data['number'],
269 'ref': other.patchsets[patchset - 1]['ref']
270 }
271 self.data['dependsOn'] = [d]
272
273 other.needed_by_changes.append(self)
274 needed = other.data.get('neededBy', [])
275 d = {'id': self.data['id'],
276 'number': self.data['number'],
277 'ref': self.patchsets[patchset - 1]['ref'],
278 'revision': self.patchsets[patchset - 1]['revision']
279 }
280 needed.append(d)
281 other.data['neededBy'] = needed
282
James E. Blairb0fcae42012-07-17 11:12:10 -0700283 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700284 self.queried += 1
285 d = self.data.get('dependsOn')
286 if d:
287 d = d[0]
288 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
289 d['isCurrentPatchSet'] = True
290 else:
291 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700292 return json.loads(json.dumps(self.data))
293
294 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800295 if (self.depends_on_change and
296 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700297 return
James E. Blair127bc182012-08-28 15:55:15 -0700298 if self.fail_merge:
299 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700300 self.data['status'] = 'MERGED'
301 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700302
Monty Taylorbc758832013-06-17 17:22:42 -0400303 path = os.path.join(self.upstream_root, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700304 repo = git.Repo(path)
305 repo.heads[self.branch].commit = \
306 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700307
James E. Blaird466dc42012-07-31 10:42:56 -0700308 def setReported(self):
309 self.reported += 1
310
James E. Blairb0fcae42012-07-17 11:12:10 -0700311
312class FakeGerrit(object):
313 def __init__(self, *args, **kw):
314 self.event_queue = Queue.Queue()
315 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
316 self.change_number = 0
317 self.changes = {}
318
319 def addFakeChange(self, project, branch, subject):
320 self.change_number += 1
Monty Taylorbc758832013-06-17 17:22:42 -0400321 c = FakeChange(self, self.change_number, project, branch, subject,
322 upstream_root=self.upstream_root)
James E. Blairb0fcae42012-07-17 11:12:10 -0700323 self.changes[self.change_number] = c
324 return c
325
326 def addEvent(self, data):
327 return self.event_queue.put(data)
328
329 def getEvent(self):
330 return self.event_queue.get()
331
332 def eventDone(self):
333 self.event_queue.task_done()
334
335 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700336 number, ps = changeid.split(',')
337 change = self.changes[int(number)]
James E. Blair42f74822013-05-14 15:18:03 -0700338 change.messages.append(message)
James E. Blairb0fcae42012-07-17 11:12:10 -0700339 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700340 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700341 if message:
342 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700343
344 def query(self, number):
345 change = self.changes[int(number)]
346 return change.query()
347
348 def startWatching(self, *args, **kw):
349 pass
350
351
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700352class BuildHistory(object):
353 def __init__(self, **kw):
354 self.__dict__.update(kw)
James E. Blairb0fcae42012-07-17 11:12:10 -0700355
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700356 def __repr__(self):
357 return ("<Completed build, result: %s name: %s #%s changes: %s>" %
358 (self.result, self.name, self.number, self.changes))
James E. Blairb0fcae42012-07-17 11:12:10 -0700359
360
James E. Blair8cc15a82012-08-01 11:17:57 -0700361class FakeURLOpener(object):
Monty Taylorbc758832013-06-17 17:22:42 -0400362 def __init__(self, upstream_root, fake_gerrit, url):
363 self.upstream_root = upstream_root
James E. Blair8cc15a82012-08-01 11:17:57 -0700364 self.fake_gerrit = fake_gerrit
365 self.url = url
366
367 def read(self):
368 res = urlparse.urlparse(self.url)
369 path = res.path
370 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200371 ret = '001e# service=git-upload-pack\n'
372 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
373 'multi_ack thin-pack side-band side-band-64k ofs-delta '
374 'shallow no-progress include-tag multi_ack_detailed no-done\n')
Monty Taylorbc758832013-06-17 17:22:42 -0400375 path = os.path.join(self.upstream_root, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700376 repo = git.Repo(path)
377 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200378 r = ref.object.hexsha + ' ' + ref.path + '\n'
379 ret += '%04x%s' % (len(r) + 4, r)
380 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700381 return ret
382
383
James E. Blair4886cc12012-07-18 15:39:41 -0700384class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
Monty Taylorbc758832013-06-17 17:22:42 -0400385 def __init__(self, upstream_root, *args):
386 super(FakeGerritTrigger, self).__init__(*args)
387 self.upstream_root = upstream_root
388
James E. Blair4886cc12012-07-18 15:39:41 -0700389 def getGitUrl(self, project):
Monty Taylorbc758832013-06-17 17:22:42 -0400390 return os.path.join(self.upstream_root, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700391
392
James E. Blair412e5582013-04-22 15:50:12 -0700393class FakeStatsd(threading.Thread):
394 def __init__(self):
395 threading.Thread.__init__(self)
396 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
397 self.sock.bind(('', 0))
398 self.port = self.sock.getsockname()[1]
399 self.wake_read, self.wake_write = os.pipe()
400 self.stats = []
401
402 def run(self):
403 while True:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700404 poll = select.poll()
405 poll.register(self.sock, select.POLLIN)
406 poll.register(self.wake_read, select.POLLIN)
407 ret = poll.poll()
408 for (fd, event) in ret:
409 if fd == self.sock.fileno():
James E. Blair412e5582013-04-22 15:50:12 -0700410 data = self.sock.recvfrom(1024)
411 if not data:
412 return
413 self.stats.append(data[0])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700414 if fd == self.wake_read:
James E. Blair412e5582013-04-22 15:50:12 -0700415 return
416
417 def stop(self):
418 os.write(self.wake_write, '1\n')
419
420
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700421class FakeBuild(threading.Thread):
422 log = logging.getLogger("zuul.test")
423
424 def __init__(self, worker, job, number, node):
425 threading.Thread.__init__(self)
426 self.worker = worker
427 self.job = job
428 self.name = job.name.split(':')[1]
429 self.number = number
430 self.node = node
431 self.parameters = json.loads(job.arguments)
432 self.unique = self.parameters['ZUUL_UUID']
433 self.wait_condition = threading.Condition()
434 self.waiting = False
435 self.aborted = False
436 self.created = time.time()
437 self.description = ''
438
439 def release(self):
440 self.wait_condition.acquire()
441 self.wait_condition.notify()
442 self.waiting = False
443 self.log.debug("Build %s released" % self.unique)
444 self.wait_condition.release()
445
446 def isWaiting(self):
447 self.wait_condition.acquire()
448 if self.waiting:
449 ret = True
450 else:
451 ret = False
452 self.wait_condition.release()
453 return ret
454
455 def _wait(self):
456 self.wait_condition.acquire()
457 self.waiting = True
458 self.log.debug("Build %s waiting" % self.unique)
459 self.wait_condition.wait()
460 self.wait_condition.release()
461
462 def run(self):
463 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700464 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
465 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700466 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700467 'manager': self.worker.worker_id,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700468 }
469
470 self.job.sendWorkData(json.dumps(data))
471 self.job.sendWorkStatus(0, 100)
472
473 if self.worker.hold_jobs_in_build:
474 self._wait()
475 self.log.debug("Build %s continuing" % self.unique)
476
477 self.worker.lock.acquire()
478
479 result = 'SUCCESS'
480 if (('ZUUL_REF' in self.parameters) and
481 self.worker.shouldFailTest(self.name,
482 self.parameters['ZUUL_REF'])):
483 result = 'FAILURE'
484 if self.aborted:
485 result = 'ABORTED'
486
487 data = {'result': result}
488 changes = None
489 if 'ZUUL_CHANGE_IDS' in self.parameters:
490 changes = self.parameters['ZUUL_CHANGE_IDS']
491
492 self.worker.build_history.append(
493 BuildHistory(name=self.name, number=self.number,
494 result=result, changes=changes, node=self.node,
495 uuid=self.unique, description=self.description)
496 )
497
498 self.job.sendWorkComplete(json.dumps(data))
499 del self.worker.gearman_jobs[self.job.unique]
500 self.worker.running_builds.remove(self)
501 self.worker.lock.release()
502
503
504class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400505 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700506 super(FakeWorker, self).__init__(worker_id)
507 self.gearman_jobs = {}
508 self.build_history = []
509 self.running_builds = []
510 self.build_counter = 0
511 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400512 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700513
514 self.hold_jobs_in_build = False
515 self.lock = threading.Lock()
516 self.__work_thread = threading.Thread(target=self.work)
517 self.__work_thread.start()
518
519 def handleJob(self, job):
520 parts = job.name.split(":")
521 cmd = parts[0]
522 name = parts[1]
523 if len(parts) > 2:
524 node = parts[2]
525 else:
526 node = None
527 if cmd == 'build':
528 self.handleBuild(job, name, node)
529 elif cmd == 'stop':
530 self.handleStop(job, name)
531 elif cmd == 'set_description':
532 self.handleSetDescription(job, name)
533
534 def handleBuild(self, job, name, node):
535 build = FakeBuild(self, job, self.build_counter, node)
536 job.build = build
537 self.gearman_jobs[job.unique] = job
538 self.build_counter += 1
539
540 self.running_builds.append(build)
541 build.start()
542
543 def handleStop(self, job, name):
544 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700545 parameters = json.loads(job.arguments)
546 name = parameters['name']
547 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700548 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700549 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700550 build.aborted = True
551 build.release()
552 job.sendWorkComplete()
553 return
554 job.sendWorkFail()
555
556 def handleSetDescription(self, job, name):
557 self.log.debug("handle set description")
558 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700559 name = parameters['name']
560 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700561 descr = parameters['html_description']
562 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700563 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700564 build.description = descr
565 job.sendWorkComplete()
566 return
567 for build in self.build_history:
James E. Blair3c483cf2013-06-04 16:30:43 -0700568 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700569 build.description = descr
570 job.sendWorkComplete()
571 return
572 job.sendWorkFail()
573
574 def work(self):
575 while self.running:
576 try:
577 job = self.getJob()
578 except gear.InterruptedError:
579 continue
580 try:
581 self.handleJob(job)
582 except:
583 self.log.exception("Worker exception:")
584
585 def addFailTest(self, name, change):
586 l = self.fail_tests.get(name, [])
587 l.append(change)
588 self.fail_tests[name] = l
589
590 def shouldFailTest(self, name, ref):
591 l = self.fail_tests.get(name, [])
592 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400593 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700594 return True
595 return False
596
597 def release(self, regex=None):
598 builds = self.running_builds[:]
599 self.log.debug("releasing build %s (%s)" % (regex,
600 len(self.running_builds)))
601 for build in builds:
602 if not regex or re.match(regex, build.name):
603 self.log.debug("releasing build %s" %
604 (build.parameters['ZUUL_UUID']))
605 build.release()
606 else:
607 self.log.debug("not releasing build %s" %
608 (build.parameters['ZUUL_UUID']))
609 self.log.debug("done releasing builds %s (%s)" %
610 (regex, len(self.running_builds)))
611
612
613class FakeGearmanServer(gear.Server):
614 def __init__(self):
615 self.hold_jobs_in_queue = False
616 super(FakeGearmanServer, self).__init__(0)
617
618 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700619 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
620 for job in queue:
621 if not hasattr(job, 'waiting'):
622 if job.name.startswith('build:'):
623 job.waiting = self.hold_jobs_in_queue
624 else:
625 job.waiting = False
626 if job.waiting:
627 continue
628 if job.name in connection.functions:
629 if not peek:
630 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400631 connection.related_jobs[job.handle] = job
632 job.worker_connection = connection
633 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700634 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700635 return None
636
637 def release(self, regex=None):
638 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700639 qlen = (len(self.high_queue) + len(self.normal_queue) +
640 len(self.low_queue))
641 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
642 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
643 queue = queue[:]
644 for job in queue:
645 cmd, name = job.name.split(':')
646 if cmd != 'build':
647 continue
648 if not regex or re.match(regex, name):
649 self.log.debug("releasing queued job %s" %
650 job.unique)
651 job.waiting = False
652 released = True
653 else:
654 self.log.debug("not releasing queued job %s" %
655 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700656 if released:
657 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700658 qlen = (len(self.high_queue) + len(self.normal_queue) +
659 len(self.low_queue))
660 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700661
662
Monty Taylorbc758832013-06-17 17:22:42 -0400663class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700664 log = logging.getLogger("zuul.test")
665
666 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400667 super(TestScheduler, self).setUp()
668 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
669 try:
670 test_timeout = int(test_timeout)
671 except ValueError:
672 # If timeout value is invalid do not set a timeout.
673 test_timeout = 0
674 if test_timeout > 0:
675 self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
676
677 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
678 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
679 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
680 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
681 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
682 os.environ.get('OS_STDERR_CAPTURE') == '1'):
683 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
684 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
685 self.useFixture(fixtures.NestedTempfile())
Monty Taylore6a6c402013-07-02 09:25:55 -0700686 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
687 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400688 self.useFixture(fixtures.FakeLogger(
689 level=logging.DEBUG,
690 format='%(asctime)s %(name)-32s '
691 '%(levelname)-8s %(message)s'))
692 tmp_root = tempfile.mkdtemp(dir=os.environ.get("ZUUL_TEST_ROOT",
693 '/tmp'))
Monty Taylorbc758832013-06-17 17:22:42 -0400694 self.test_root = os.path.join(tmp_root, "zuul-test")
695 self.upstream_root = os.path.join(self.test_root, "upstream")
696 self.git_root = os.path.join(self.test_root, "git")
697
698 CONFIG.set('zuul', 'git_dir', self.git_root)
699 if os.path.exists(self.test_root):
700 shutil.rmtree(self.test_root)
701 os.makedirs(self.test_root)
702 os.makedirs(self.upstream_root)
703 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700704
705 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400706 self.init_repo("org/project")
707 self.init_repo("org/project1")
708 self.init_repo("org/project2")
709 self.init_repo("org/project3")
710 self.init_repo("org/one-job-project")
711 self.init_repo("org/nonvoting-project")
712 self.init_repo("org/templated-project")
713 self.init_repo("org/node-project")
James E. Blair412e5582013-04-22 15:50:12 -0700714
715 self.statsd = FakeStatsd()
716 os.environ['STATSD_HOST'] = 'localhost'
717 os.environ['STATSD_PORT'] = str(self.statsd.port)
718 self.statsd.start()
719 # the statsd client object is configured in the statsd module import
720 reload(statsd)
721 reload(zuul.scheduler)
722
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700723 self.gearman_server = FakeGearmanServer()
724
725 self.config = ConfigParser.ConfigParser()
726 cfg = StringIO()
727 CONFIG.write(cfg)
728 cfg.seek(0)
729 self.config.readfp(cfg)
730 self.config.set('gearman', 'port', str(self.gearman_server.port))
731
Monty Taylorbc758832013-06-17 17:22:42 -0400732 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700733 self.worker.addServer('127.0.0.1', self.gearman_server.port)
734 self.gearman_server.worker = self.worker
735
James E. Blairb0fcae42012-07-17 11:12:10 -0700736 self.sched = zuul.scheduler.Scheduler()
737
James E. Blair8cc15a82012-08-01 11:17:57 -0700738 def URLOpenerFactory(*args, **kw):
739 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400740 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700741
James E. Blair8cc15a82012-08-01 11:17:57 -0700742 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700743 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700744
745 zuul.lib.gerrit.Gerrit = FakeGerrit
746
Monty Taylorbc758832013-06-17 17:22:42 -0400747 self.gerrit = FakeGerritTrigger(
748 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700749 self.gerrit.replication_timeout = 1.5
750 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700751 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400752 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700753
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700754 self.sched.setLauncher(self.launcher)
James E. Blairb0fcae42012-07-17 11:12:10 -0700755 self.sched.setTrigger(self.gerrit)
756
757 self.sched.start()
758 self.sched.reconfigure(self.config)
759 self.sched.resume()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700760 self.launcher.gearman.waitForServer()
761 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400762 self.builds = self.worker.running_builds
763 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700764
765 def tearDown(self):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700766 self.launcher.stop()
767 self.worker.shutdown()
768 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700769 self.gerrit.stop()
770 self.sched.stop()
771 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700772 self.statsd.stop()
773 self.statsd.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700774 threads = threading.enumerate()
775 if len(threads) > 1:
776 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400777 super(TestScheduler, self).tearDown()
778
779 def init_repo(self, project):
780 parts = project.split('/')
781 path = os.path.join(self.upstream_root, *parts[:-1])
782 if not os.path.exists(path):
783 os.makedirs(path)
784 path = os.path.join(self.upstream_root, project)
785 repo = git.Repo.init(path)
786
787 repo.config_writer().set_value('user', 'email', 'user@example.com')
788 repo.config_writer().set_value('user', 'name', 'User Name')
789 repo.config_writer().write()
790
791 fn = os.path.join(path, 'README')
792 f = open(fn, 'w')
793 f.write("test\n")
794 f.close()
795 repo.index.add([fn])
796 repo.index.commit('initial commit')
797 master = repo.create_head('master')
798 repo.create_tag('init')
799
800 mp = repo.create_head('mp')
801 repo.head.reference = mp
802 f = open(fn, 'a')
803 f.write("test mp\n")
804 f.close()
805 repo.index.add([fn])
806 repo.index.commit('mp commit')
807
808 repo.head.reference = master
809 repo.head.reset(index=True, working_tree=True)
810 repo.git.clean('-x', '-f', '-d')
811
812 def ref_has_change(self, ref, change):
813 path = os.path.join(self.git_root, change.project)
814 repo = git.Repo(path)
815 for commit in repo.iter_commits(ref):
816 if commit.message.strip() == ('%s-1' % change.subject):
817 return True
818 return False
819
820 def job_has_changes(self, *args):
821 job = args[0]
822 commits = args[1:]
823 if isinstance(job, FakeBuild):
824 parameters = job.parameters
825 else:
826 parameters = json.loads(job.arguments)
827 project = parameters['ZUUL_PROJECT']
828 path = os.path.join(self.git_root, project)
829 repo = git.Repo(path)
830 ref = parameters['ZUUL_REF']
831 sha = parameters['ZUUL_COMMIT']
832 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
833 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
834 commit_messages = ['%s-1' % commit.subject for commit in commits]
835 for msg in commit_messages:
836 if msg not in repo_messages:
837 return False
838 if repo_shas[0] != sha:
839 return False
840 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700841
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700842 def registerJobs(self):
843 count = 0
844 for job in self.sched.jobs.keys():
845 self.worker.registerFunction('build:' + job)
846 count += 1
847 self.worker.registerFunction('stop:' + self.worker.worker_id)
848 count += 1
849
850 while len(self.gearman_server.functions) < count:
851 time.sleep(0)
852
853 def release(self, job):
854 if isinstance(job, FakeBuild):
855 job.release()
856 else:
857 job.waiting = False
858 self.log.debug("Queued job %s released" % job.unique)
859 self.gearman_server.wakeConnections()
860
861 def getParameter(self, job, name):
862 if isinstance(job, FakeBuild):
863 return job.parameters[name]
864 else:
865 parameters = json.loads(job.arguments)
866 return parameters[name]
867
868 def resetGearmanServer(self):
869 self.worker.setFunctions([])
870 while True:
871 done = True
872 for connection in self.gearman_server.active_connections:
873 if connection.functions:
874 done = False
875 if done:
876 break
877 time.sleep(0)
878 self.gearman_server.functions = set()
879
880 def haveAllBuildsReported(self):
881 # See if Zuul is waiting on a meta job to complete
882 if self.launcher.meta_jobs:
883 return False
884 # Find out if every build that the worker has completed has been
885 # reported back to Zuul. If it hasn't then that means a Gearman
886 # event is still in transit and the system is not stable.
887 for build in self.worker.build_history:
888 zbuild = self.launcher.builds.get(build.uuid)
889 if not zbuild:
890 # It has already been reported
891 continue
892 # It hasn't been reported yet.
893 return False
894 # Make sure that none of the worker connections are in GRAB_WAIT
895 for connection in self.worker.active_connections:
896 if connection.state == 'GRAB_WAIT':
897 return False
898 return True
899
900 def areAllBuildsWaiting(self):
901 ret = True
902
903 builds = self.launcher.builds.values()
904 for build in builds:
905 client_job = None
906 for conn in self.launcher.gearman.active_connections:
907 for j in conn.related_jobs.values():
908 if j.unique == build.uuid:
909 client_job = j
910 break
911 if not client_job:
912 self.log.debug("%s is not known to the gearman client" %
913 build)
914 ret = False
915 continue
916 if not client_job.handle:
917 self.log.debug("%s has no handle" % client_job)
918 ret = False
919 continue
920 server_job = self.gearman_server.jobs.get(client_job.handle)
921 if not server_job:
922 self.log.debug("%s is not known to the gearman server" %
923 client_job)
924 ret = False
925 continue
926 if not hasattr(server_job, 'waiting'):
927 self.log.debug("%s is being enqueued" % server_job)
928 ret = False
929 continue
930 if server_job.waiting:
931 continue
932 worker_job = self.worker.gearman_jobs.get(server_job.unique)
933 if worker_job:
934 if worker_job.build.isWaiting():
935 continue
936 else:
937 self.log.debug("%s is running" % worker_job)
938 ret = False
939 else:
940 self.log.debug("%s is unassigned" % server_job)
941 ret = False
942 return ret
943
James E. Blairb0fcae42012-07-17 11:12:10 -0700944 def waitUntilSettled(self):
945 self.log.debug("Waiting until settled...")
946 start = time.time()
947 while True:
948 if time.time() - start > 10:
949 print 'queue status:',
950 print self.sched.trigger_event_queue.empty(),
951 print self.sched.result_event_queue.empty(),
952 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700953 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -0700954 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700955 # Make sure no new events show up while we're checking
956 self.worker.lock.acquire()
957 # have all build states propogated to zuul?
958 if self.haveAllBuildsReported():
959 # Join ensures that the queue is empty _and_ events have been
960 # processed
961 self.fake_gerrit.event_queue.join()
962 self.sched.trigger_event_queue.join()
963 self.sched.result_event_queue.join()
964 if (self.sched.trigger_event_queue.empty() and
965 self.sched.result_event_queue.empty() and
966 self.fake_gerrit.event_queue.empty() and
967 self.areAllBuildsWaiting()):
968 self.worker.lock.release()
969 self.log.debug("...settled.")
970 return
971 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700972 self.sched.wake_event.wait(0.1)
973
James E. Blaird466dc42012-07-31 10:42:56 -0700974 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -0800975 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -0700976 return len(jobs)
977
James E. Blair4ca985f2013-05-30 12:27:43 -0700978 def getJobFromHistory(self, name):
979 history = self.worker.build_history
980 for job in history:
981 if job.name == name:
982 return job
983 raise Exception("Unable to find job %s in history" % name)
984
James E. Blaire0487072012-08-29 17:38:31 -0700985 def assertEmptyQueues(self):
986 # Make sure there are no orphaned jobs
987 for pipeline in self.sched.pipelines.values():
988 for queue in pipeline.queues:
989 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -0800990 print 'pipeline %s queue %s contents %s' % (
991 pipeline.name, queue.name, queue.queue)
James E. Blaire0487072012-08-29 17:38:31 -0700992 assert len(queue.queue) == 0
993 if len(queue.severed_heads) != 0:
994 print 'heads', queue.severed_heads
995 assert len(queue.severed_heads) == 0
996
James E. Blair412e5582013-04-22 15:50:12 -0700997 def assertReportedStat(self, key, value=None):
998 start = time.time()
999 while time.time() < (start + 5):
1000 for stat in self.statsd.stats:
1001 k, v = stat.split(':')
1002 if key == k:
1003 if value is None:
1004 return
1005 if value == v:
1006 return
1007 time.sleep(0.1)
1008
1009 pprint.pprint(self.statsd.stats)
1010 raise Exception("Key %s not found in reported stats" % key)
1011
James E. Blairb0fcae42012-07-17 11:12:10 -07001012 def test_jobs_launched(self):
1013 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001014
James E. Blairb0fcae42012-07-17 11:12:10 -07001015 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001016 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001017 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1018 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001019 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1020 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1021 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blairb0fcae42012-07-17 11:12:10 -07001022 assert A.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -07001023 assert A.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001024 self.assertEmptyQueues()
James E. Blairb0fcae42012-07-17 11:12:10 -07001025
James E. Blair412e5582013-04-22 15:50:12 -07001026 self.assertReportedStat('gerrit.event.comment-added', '1|c')
1027 self.assertReportedStat('zuul.pipeline.gate.current_changes', '1|g')
1028 self.assertReportedStat('zuul.job.project-merge')
1029 self.assertReportedStat('zuul.pipeline.gate.resident_time')
1030 self.assertReportedStat('zuul.pipeline.gate.total_changes', '1|c')
1031 self.assertReportedStat(
1032 'zuul.pipeline.gate.org.project.resident_time')
1033 self.assertReportedStat(
1034 'zuul.pipeline.gate.org.project.total_changes', '1|c')
1035
James E. Blair42f74822013-05-14 15:18:03 -07001036 def test_duplicate_pipelines(self):
1037 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001038
James E. Blair42f74822013-05-14 15:18:03 -07001039 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1040 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1041 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001042
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001043 print self.builds
James E. Blair42f74822013-05-14 15:18:03 -07001044 print A.messages
1045
1046 self.assertEmptyQueues()
1047
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001048 assert len(self.history) == 2
1049 self.history[0].name == 'project-test1'
1050 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001051
1052 assert len(A.messages) == 2
1053 if 'dup1/project-test1' in A.messages[0]:
1054 assert 'dup1/project-test1' in A.messages[0]
1055 assert 'dup2/project-test1' not in A.messages[0]
1056 assert 'dup1/project-test1' not in A.messages[1]
1057 assert 'dup2/project-test1' in A.messages[1]
1058 else:
1059 assert 'dup1/project-test1' in A.messages[1]
1060 assert 'dup2/project-test1' not in A.messages[1]
1061 assert 'dup1/project-test1' not in A.messages[0]
1062 assert 'dup2/project-test1' in A.messages[0]
1063
James E. Blairb0fcae42012-07-17 11:12:10 -07001064 def test_parallel_changes(self):
1065 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001066
1067 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001068 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1069 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1070 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001071 A.addApproval('CRVW', 2)
1072 B.addApproval('CRVW', 2)
1073 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001074
1075 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1076 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1077 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1078
1079 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001080 assert len(self.builds) == 1
1081 assert self.builds[0].name == 'project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001082 assert self.job_has_changes(self.builds[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -07001083
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001084 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001085 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001086 assert len(self.builds) == 3
1087 assert self.builds[0].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001088 assert self.job_has_changes(self.builds[0], A)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001089 assert self.builds[1].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001090 assert self.job_has_changes(self.builds[1], A)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001091 assert self.builds[2].name == 'project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001092 assert self.job_has_changes(self.builds[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -07001093
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001094 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001095 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001096 assert len(self.builds) == 5
1097 assert self.builds[0].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001098 assert self.job_has_changes(self.builds[0], A)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001099 assert self.builds[1].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001100 assert self.job_has_changes(self.builds[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -07001101
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001102 assert self.builds[2].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001103 assert self.job_has_changes(self.builds[2], A, B)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001104 assert self.builds[3].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001105 assert self.job_has_changes(self.builds[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -07001106
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001107 assert self.builds[4].name == 'project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001108 assert self.job_has_changes(self.builds[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -07001109
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001110 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001111 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001112 assert len(self.builds) == 6
1113 assert self.builds[0].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001114 assert self.job_has_changes(self.builds[0], A)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001115 assert self.builds[1].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001116 assert self.job_has_changes(self.builds[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -07001117
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001118 assert self.builds[2].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001119 assert self.job_has_changes(self.builds[2], A, B)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001120 assert self.builds[3].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001121 assert self.job_has_changes(self.builds[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -07001122
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001123 assert self.builds[4].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001124 assert self.job_has_changes(self.builds[4], A, B, C)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001125 assert self.builds[5].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001126 assert self.job_has_changes(self.builds[5], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -07001127
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001128 self.worker.hold_jobs_in_build = False
1129 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001130 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001131 assert len(self.builds) == 0
James E. Blairb0fcae42012-07-17 11:12:10 -07001132
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001133 assert len(self.history) == 9
James E. Blairb0fcae42012-07-17 11:12:10 -07001134 assert A.data['status'] == 'MERGED'
1135 assert B.data['status'] == 'MERGED'
1136 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -07001137 assert A.reported == 2
1138 assert B.reported == 2
1139 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001140 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001141
1142 def test_failed_changes(self):
1143 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001144 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001145
James E. Blairb02a3bb2012-07-30 17:49:55 -07001146 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1147 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001148 A.addApproval('CRVW', 2)
1149 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001150
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001151 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001152
James E. Blaire2819012013-06-28 17:17:26 -04001153 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1154 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001155 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001156
1157 self.worker.release('.*-merge')
1158 self.waitUntilSettled()
1159
1160 self.worker.hold_jobs_in_build = False
1161 self.worker.release()
1162
1163 self.waitUntilSettled()
1164 # It's certain that the merge job for change 2 will run, but
1165 # the test1 and test2 jobs may or may not run.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001166 assert len(self.history) > 6
James E. Blairb02a3bb2012-07-30 17:49:55 -07001167 assert A.data['status'] == 'NEW'
1168 assert B.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -07001169 assert A.reported == 2
1170 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001171 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001172
1173 def test_independent_queues(self):
1174 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001175
1176 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001177 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001178 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1179 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001180 A.addApproval('CRVW', 2)
1181 B.addApproval('CRVW', 2)
1182 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001183
1184 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1185 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1186 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1187
James E. Blairb02a3bb2012-07-30 17:49:55 -07001188 self.waitUntilSettled()
1189
1190 # There should be one merge job at the head of each queue running
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001191 assert len(self.builds) == 2
1192 assert self.builds[0].name == 'project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001193 assert self.job_has_changes(self.builds[0], A)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001194 assert self.builds[1].name == 'project1-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001195 assert self.job_has_changes(self.builds[1], B)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001196
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001197 # Release the current merge builds
1198 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001199 self.waitUntilSettled()
1200 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001201 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001202 self.waitUntilSettled()
1203
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001204 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001205 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001206 assert len(self.builds) == 8
James E. Blairb02a3bb2012-07-30 17:49:55 -07001207
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001208 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001209 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001210 assert len(self.builds) == 0
James E. Blairb02a3bb2012-07-30 17:49:55 -07001211
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001212 assert len(self.history) == 11
James E. Blairb02a3bb2012-07-30 17:49:55 -07001213 assert A.data['status'] == 'MERGED'
1214 assert B.data['status'] == 'MERGED'
1215 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -07001216 assert A.reported == 2
1217 assert B.reported == 2
1218 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001219 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -07001220
1221 def test_failed_change_at_head(self):
1222 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001223
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001224 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001225 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1226 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1227 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001228 A.addApproval('CRVW', 2)
1229 B.addApproval('CRVW', 2)
1230 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001231
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001232 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001233
1234 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1235 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1236 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1237
1238 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001239
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001240 assert len(self.builds) == 1
1241 assert self.builds[0].name == 'project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001242 assert self.job_has_changes(self.builds[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -07001243
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001244 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001245 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001246 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001247 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001248 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001249 self.waitUntilSettled()
1250
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001251 assert len(self.builds) == 6
1252 assert self.builds[0].name == 'project-test1'
1253 assert self.builds[1].name == 'project-test2'
1254 assert self.builds[2].name == 'project-test1'
1255 assert self.builds[3].name == 'project-test2'
1256 assert self.builds[4].name == 'project-test1'
1257 assert self.builds[5].name == 'project-test2'
James E. Blaird466dc42012-07-31 10:42:56 -07001258
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001259 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001260 self.waitUntilSettled()
1261
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001262 assert len(self.builds) == 2 # project-test2, project-merge for B
1263 assert self.countJobResults(self.history, 'ABORTED') == 4
James E. Blaird466dc42012-07-31 10:42:56 -07001264
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001265 self.worker.hold_jobs_in_build = False
1266 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001267 self.waitUntilSettled()
1268
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001269 assert len(self.builds) == 0
1270 assert len(self.history) == 15
James E. Blaird466dc42012-07-31 10:42:56 -07001271 assert A.data['status'] == 'NEW'
1272 assert B.data['status'] == 'MERGED'
1273 assert C.data['status'] == 'MERGED'
1274 assert A.reported == 2
1275 assert B.reported == 2
1276 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001277 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -07001278
1279 def test_failed_change_at_head_with_queue(self):
1280 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001281
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001282 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001283 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1284 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1285 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001286 A.addApproval('CRVW', 2)
1287 B.addApproval('CRVW', 2)
1288 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001289
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001290 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001291
1292 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1293 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1294 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1295
1296 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001297 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001298 assert len(self.builds) == 0
James E. Blaird466dc42012-07-31 10:42:56 -07001299 assert len(queue) == 1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001300 assert queue[0].name == 'build:project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001301 assert self.job_has_changes(queue[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -07001302
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001303 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001304 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001305 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001306 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001307 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001308 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001309 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001310
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001311 assert len(self.builds) == 0
James E. Blaird466dc42012-07-31 10:42:56 -07001312 assert len(queue) == 6
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001313 assert queue[0].name == 'build:project-test1'
1314 assert queue[1].name == 'build:project-test2'
1315 assert queue[2].name == 'build:project-test1'
1316 assert queue[3].name == 'build:project-test2'
1317 assert queue[4].name == 'build:project-test1'
1318 assert queue[5].name == 'build:project-test2'
James E. Blaird466dc42012-07-31 10:42:56 -07001319
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001320 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001321 self.waitUntilSettled()
1322
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001323 assert len(self.builds) == 0
James E. Blair701c5b42013-06-06 09:34:59 -07001324 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001325 assert len(queue) == 2 # project-test2, project-merge for B
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001326 assert self.countJobResults(self.history, 'ABORTED') == 0
James E. Blaird466dc42012-07-31 10:42:56 -07001327
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001328 self.gearman_server.hold_jobs_in_queue = False
1329 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001330 self.waitUntilSettled()
1331
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001332 assert len(self.builds) == 0
1333 assert len(self.history) == 11
James E. Blaird466dc42012-07-31 10:42:56 -07001334 assert A.data['status'] == 'NEW'
1335 assert B.data['status'] == 'MERGED'
1336 assert C.data['status'] == 'MERGED'
1337 assert A.reported == 2
1338 assert B.reported == 2
1339 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001340 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001341
1342 def test_patch_order(self):
1343 "Test that dependent patches are tested in the right order"
1344 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1345 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1346 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1347 A.addApproval('CRVW', 2)
1348 B.addApproval('CRVW', 2)
1349 C.addApproval('CRVW', 2)
1350
1351 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1352 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1353 M2.setMerged()
1354 M1.setMerged()
1355
1356 # C -> B -> A -> M1 -> M2
1357 # M2 is here to make sure it is never queried. If it is, it
1358 # means zuul is walking down the entire history of merged
1359 # changes.
1360
1361 C.setDependsOn(B, 1)
1362 B.setDependsOn(A, 1)
1363 A.setDependsOn(M1, 1)
1364 M1.setDependsOn(M2, 1)
1365
1366 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1367
1368 self.waitUntilSettled()
1369
1370 assert A.data['status'] == 'NEW'
1371 assert B.data['status'] == 'NEW'
1372 assert C.data['status'] == 'NEW'
1373
1374 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1375 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1376
1377 self.waitUntilSettled()
1378 assert M2.queried == 0
1379 assert A.data['status'] == 'MERGED'
1380 assert B.data['status'] == 'MERGED'
1381 assert C.data['status'] == 'MERGED'
1382 assert A.reported == 2
1383 assert B.reported == 2
1384 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001385 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001386
1387 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001388 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001389 # TODO: move to test_gerrit (this is a unit test!)
1390 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4aea70c2012-07-26 14:23:24 -07001391 a = self.sched.trigger.getChange(1, 2)
1392 mgr = self.sched.pipelines['gate'].manager
1393 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001394
1395 A.addApproval('CRVW', 2)
James E. Blair4aea70c2012-07-26 14:23:24 -07001396 a = self.sched.trigger.getChange(1, 2)
1397 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001398
1399 A.addApproval('APRV', 1)
James E. Blair4aea70c2012-07-26 14:23:24 -07001400 a = self.sched.trigger.getChange(1, 2)
1401 assert self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blaire0487072012-08-29 17:38:31 -07001402 self.assertEmptyQueues()
James E. Blair4886cc12012-07-18 15:39:41 -07001403
1404 def test_build_configuration(self):
1405 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001406
1407 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001408 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1409 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1410 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1411 A.addApproval('CRVW', 2)
1412 B.addApproval('CRVW', 2)
1413 C.addApproval('CRVW', 2)
1414 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1415 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1416 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1417 self.waitUntilSettled()
1418
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001419 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001420 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001421 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001422 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001423 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001424 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001425 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001426 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1427 self.gearman_server.hold_jobs_in_queue = False
1428 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001429 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001430
Monty Taylorbc758832013-06-17 17:22:42 -04001431 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001432 repo = git.Repo(path)
1433 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1434 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001435 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
1436 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001437 self.assertEmptyQueues()
James E. Blair973721f2012-08-15 10:19:43 -07001438
1439 def test_build_configuration_conflict(self):
1440 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001441
1442 self.gearman_server.hold_jobs_in_queue = True
James E. Blair973721f2012-08-15 10:19:43 -07001443 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1444 A.addPatchset(['conflict'])
1445 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1446 B.addPatchset(['conflict'])
1447 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1448 A.addApproval('CRVW', 2)
1449 B.addApproval('CRVW', 2)
1450 C.addApproval('CRVW', 2)
1451 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1452 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1453 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1454 self.waitUntilSettled()
1455
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001456 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001457 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001458 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001459 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001460 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001461 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001462 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001463 self.getParameter(queue[-1], 'ZUUL_REF')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001464 self.gearman_server.hold_jobs_in_queue = False
1465 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001466 self.waitUntilSettled()
1467
1468 assert A.data['status'] == 'MERGED'
1469 assert B.data['status'] == 'NEW'
1470 assert C.data['status'] == 'MERGED'
1471 assert A.reported == 2
1472 assert B.reported == 2
1473 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001474 self.assertEmptyQueues()
James E. Blairdaabed22012-08-15 15:38:57 -07001475
1476 def test_post(self):
1477 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001478
Zhongyue Luo5d556072012-09-21 02:00:47 +09001479 e = {
1480 "type": "ref-updated",
1481 "submitter": {
1482 "name": "User Name",
1483 },
1484 "refUpdate": {
1485 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1486 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1487 "refName": "master",
1488 "project": "org/project",
1489 }
1490 }
James E. Blairdaabed22012-08-15 15:38:57 -07001491 self.fake_gerrit.addEvent(e)
1492 self.waitUntilSettled()
1493
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001494 job_names = [x.name for x in self.history]
1495 assert len(self.history) == 1
James E. Blairdaabed22012-08-15 15:38:57 -07001496 assert 'project-post' in job_names
James E. Blaire0487072012-08-29 17:38:31 -07001497 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001498
1499 def test_build_configuration_branch(self):
1500 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001501
1502 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001503 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1504 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1505 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1506 A.addApproval('CRVW', 2)
1507 B.addApproval('CRVW', 2)
1508 C.addApproval('CRVW', 2)
1509 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1510 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1511 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1512 self.waitUntilSettled()
1513
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001514 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001515 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001516 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001517 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001518 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001519 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001520 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001521 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1522 self.gearman_server.hold_jobs_in_queue = False
1523 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001524 self.waitUntilSettled()
1525
Monty Taylorbc758832013-06-17 17:22:42 -04001526 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001527 repo = git.Repo(path)
1528 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1529 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001530 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
1531 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001532 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001533
1534 def test_build_configuration_branch_interaction(self):
1535 "Test that switching between branches works"
1536 self.test_build_configuration()
1537 self.test_build_configuration_branch()
1538 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001539 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001540 repo = git.Repo(path)
1541 repo.heads.master.commit = repo.commit('init')
1542 self.test_build_configuration()
James E. Blaire0487072012-08-29 17:38:31 -07001543 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001544
1545 def test_build_configuration_multi_branch(self):
1546 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001547
1548 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001549 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1550 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1551 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1552 A.addApproval('CRVW', 2)
1553 B.addApproval('CRVW', 2)
1554 C.addApproval('CRVW', 2)
1555 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1556 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1557 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1558 self.waitUntilSettled()
1559
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001560 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001561 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001562 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001563 ref_mp = self.getParameter(queue[-1], 'ZUUL_REF')
1564 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001565 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001566 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001567 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001568 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001569 ref_master = self.getParameter(queue[-1], 'ZUUL_REF')
1570 self.gearman_server.hold_jobs_in_queue = False
1571 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001572 self.waitUntilSettled()
1573
Monty Taylorbc758832013-06-17 17:22:42 -04001574 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001575 repo = git.Repo(path)
1576
1577 repo_messages = [c.message.strip()
1578 for c in repo.iter_commits(ref_master)]
1579 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001580 correct_messages = ['initial commit', 'A-1', 'C-1']
1581 assert repo_messages == correct_messages
1582
1583 repo_messages = [c.message.strip()
1584 for c in repo.iter_commits(ref_mp)]
1585 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001586 correct_messages = ['initial commit', 'mp commit', 'B-1']
1587 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001588 self.assertEmptyQueues()
James E. Blair7f71c802012-08-22 13:04:32 -07001589
1590 def test_one_job_project(self):
1591 "Test that queueing works with one job"
1592 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1593 'master', 'A')
1594 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1595 'master', 'B')
1596 A.addApproval('CRVW', 2)
1597 B.addApproval('CRVW', 2)
1598 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1599 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1600 self.waitUntilSettled()
1601
James E. Blair7f71c802012-08-22 13:04:32 -07001602 assert A.data['status'] == 'MERGED'
1603 assert A.reported == 2
1604 assert B.data['status'] == 'MERGED'
1605 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001606 self.assertEmptyQueues()
James E. Blaircaec0c52012-08-22 14:52:22 -07001607
Antoine Musso80edd5a2013-02-13 15:37:53 +01001608 def test_job_from_templates_launched(self):
1609 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001610
Antoine Musso80edd5a2013-02-13 15:37:53 +01001611 A = self.fake_gerrit.addFakeChange(
1612 'org/templated-project', 'master', 'A')
1613 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1614 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001615
James E. Blair4ca985f2013-05-30 12:27:43 -07001616 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1617 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
Antoine Musso80edd5a2013-02-13 15:37:53 +01001618
James E. Blaircaec0c52012-08-22 14:52:22 -07001619 def test_dependent_changes_dequeue(self):
1620 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001621
James E. Blaircaec0c52012-08-22 14:52:22 -07001622 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1623 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1624 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1625 A.addApproval('CRVW', 2)
1626 B.addApproval('CRVW', 2)
1627 C.addApproval('CRVW', 2)
1628
1629 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1630 M1.setMerged()
1631
1632 # C -> B -> A -> M1
1633
1634 C.setDependsOn(B, 1)
1635 B.setDependsOn(A, 1)
1636 A.setDependsOn(M1, 1)
1637
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001638 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001639
1640 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1641 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1642 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1643
1644 self.waitUntilSettled()
1645
James E. Blairec590122012-08-22 15:19:31 -07001646 assert A.data['status'] == 'NEW'
1647 assert A.reported == 2
1648 assert B.data['status'] == 'NEW'
1649 assert B.reported == 2
1650 assert C.data['status'] == 'NEW'
1651 assert C.reported == 2
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001652 assert len(self.history) == 1
James E. Blaire0487072012-08-29 17:38:31 -07001653 self.assertEmptyQueues()
James E. Blairec590122012-08-22 15:19:31 -07001654
1655 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001656 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001657 # If it's dequeued more than once, we should see extra
1658 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001659
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001660 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001661 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1662 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1663 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1664 A.addApproval('CRVW', 2)
1665 B.addApproval('CRVW', 2)
1666 C.addApproval('CRVW', 2)
1667
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001668 self.worker.addFailTest('project1-test1', A)
1669 self.worker.addFailTest('project1-test2', A)
1670 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001671
1672 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1673 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1674 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1675
1676 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001677
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001678 assert len(self.builds) == 1
1679 assert self.builds[0].name == 'project1-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001680 assert self.job_has_changes(self.builds[0], A)
James E. Blairec590122012-08-22 15:19:31 -07001681
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001682 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001683 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001684 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001685 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001686 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001687 self.waitUntilSettled()
1688
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001689 assert len(self.builds) == 9
1690 assert self.builds[0].name == 'project1-test1'
1691 assert self.builds[1].name == 'project1-test2'
1692 assert self.builds[2].name == 'project1-project2-integration'
1693 assert self.builds[3].name == 'project1-test1'
1694 assert self.builds[4].name == 'project1-test2'
1695 assert self.builds[5].name == 'project1-project2-integration'
1696 assert self.builds[6].name == 'project1-test1'
1697 assert self.builds[7].name == 'project1-test2'
1698 assert self.builds[8].name == 'project1-project2-integration'
James E. Blairec590122012-08-22 15:19:31 -07001699
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001700 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001701 self.waitUntilSettled()
1702
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001703 assert len(self.builds) == 3 # test2, integration, merge for B
1704 assert self.countJobResults(self.history, 'ABORTED') == 6
James E. Blairec590122012-08-22 15:19:31 -07001705
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001706 self.worker.hold_jobs_in_build = False
1707 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001708 self.waitUntilSettled()
1709
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001710 assert len(self.builds) == 0
1711 assert len(self.history) == 20
James E. Blaircaec0c52012-08-22 14:52:22 -07001712
1713 assert A.data['status'] == 'NEW'
James E. Blairec590122012-08-22 15:19:31 -07001714 assert B.data['status'] == 'MERGED'
1715 assert C.data['status'] == 'MERGED'
1716 assert A.reported == 2
1717 assert B.reported == 2
1718 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001719 self.assertEmptyQueues()
James E. Blair4ec821f2012-08-23 15:28:28 -07001720
1721 def test_nonvoting_job(self):
1722 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001723
James E. Blair4ec821f2012-08-23 15:28:28 -07001724 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1725 'master', 'A')
1726 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001727 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001728 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1729
1730 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001731
1732 assert A.data['status'] == 'MERGED'
1733 assert A.reported == 2
James E. Blair4ca985f2013-05-30 12:27:43 -07001734 assert (self.getJobFromHistory('nonvoting-project-merge').result ==
1735 'SUCCESS')
1736 assert (self.getJobFromHistory('nonvoting-project-test1').result ==
1737 'SUCCESS')
1738 assert (self.getJobFromHistory('nonvoting-project-test2').result ==
1739 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001740 self.assertEmptyQueues()
1741
1742 def test_check_queue_success(self):
1743 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001744
James E. Blaire0487072012-08-29 17:38:31 -07001745 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1746 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1747
1748 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001749
1750 assert A.data['status'] == 'NEW'
1751 assert A.reported == 1
James E. Blair4ca985f2013-05-30 12:27:43 -07001752 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1753 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1754 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blaire0487072012-08-29 17:38:31 -07001755 self.assertEmptyQueues()
1756
1757 def test_check_queue_failure(self):
1758 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001759
James E. Blaire0487072012-08-29 17:38:31 -07001760 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001761 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001762 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1763
1764 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001765
1766 assert A.data['status'] == 'NEW'
1767 assert A.reported == 1
James E. Blair4ca985f2013-05-30 12:27:43 -07001768 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1769 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1770 assert self.getJobFromHistory('project-test2').result == 'FAILURE'
James E. Blaire0487072012-08-29 17:38:31 -07001771 self.assertEmptyQueues()
James E. Blair127bc182012-08-28 15:55:15 -07001772
1773 def test_dependent_behind_dequeue(self):
1774 "test that dependent changes behind dequeued changes work"
1775 # This complicated test is a reproduction of a real life bug
1776 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001777
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001778 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001779 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1780 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1781 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1782 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1783 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1784 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1785 D.setDependsOn(C, 1)
1786 E.setDependsOn(D, 1)
1787 A.addApproval('CRVW', 2)
1788 B.addApproval('CRVW', 2)
1789 C.addApproval('CRVW', 2)
1790 D.addApproval('CRVW', 2)
1791 E.addApproval('CRVW', 2)
1792 F.addApproval('CRVW', 2)
1793
1794 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001795
1796 # Change object re-use in the gerrit trigger is hidden if
1797 # changes are added in quick succession; waiting makes it more
1798 # like real life.
1799 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1800 self.waitUntilSettled()
1801 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1802 self.waitUntilSettled()
1803
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001804 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001805 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001806 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001807 self.waitUntilSettled()
1808
1809 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1810 self.waitUntilSettled()
1811 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1812 self.waitUntilSettled()
1813 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1814 self.waitUntilSettled()
1815 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1816 self.waitUntilSettled()
1817
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001818 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001819 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001820 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001821 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001822 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001823 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001824 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001825 self.waitUntilSettled()
1826
1827 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001828
1829 # Grab pointers to the jobs we want to release before
1830 # releasing any, because list indexes may change as
1831 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001832 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001833 a.release()
1834 b.release()
1835 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001836 self.waitUntilSettled()
1837
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001838 self.worker.hold_jobs_in_build = False
1839 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07001840 self.waitUntilSettled()
1841
James E. Blair127bc182012-08-28 15:55:15 -07001842 assert A.data['status'] == 'NEW'
1843 assert B.data['status'] == 'MERGED'
1844 assert C.data['status'] == 'MERGED'
1845 assert D.data['status'] == 'MERGED'
1846 assert E.data['status'] == 'MERGED'
1847 assert F.data['status'] == 'MERGED'
1848
1849 assert A.reported == 2
1850 assert B.reported == 2
1851 assert C.reported == 2
1852 assert D.reported == 2
1853 assert E.reported == 2
1854 assert F.reported == 2
1855
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001856 assert self.countJobResults(self.history, 'ABORTED') == 15
1857 assert len(self.history) == 44
James E. Blaire0487072012-08-29 17:38:31 -07001858 self.assertEmptyQueues()
James E. Blair05fed602012-09-07 12:45:24 -07001859
1860 def test_merger_repack(self):
1861 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001862
James E. Blair05fed602012-09-07 12:45:24 -07001863 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1864 A.addApproval('CRVW', 2)
1865 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1866 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001867 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1868 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1869 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blair05fed602012-09-07 12:45:24 -07001870 assert A.data['status'] == 'MERGED'
1871 assert A.reported == 2
1872 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07001873 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001874
Monty Taylorbc758832013-06-17 17:22:42 -04001875 path = os.path.join(self.git_root, "org/project")
1876 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001877
1878 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1879 A.addApproval('CRVW', 2)
1880 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1881 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001882 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1883 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1884 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blair05fed602012-09-07 12:45:24 -07001885 assert A.data['status'] == 'MERGED'
1886 assert A.reported == 2
1887 self.assertEmptyQueues()
James E. Blair7ee88a22012-09-12 18:59:31 +02001888
James E. Blair4886f282012-11-15 09:27:33 -08001889 def test_merger_repack_large_change(self):
1890 "Test that the merger works with large changes after a repack"
1891 # https://bugs.launchpad.net/zuul/+bug/1078946
1892 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1893 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001894 path = os.path.join(self.upstream_root, "org/project1")
1895 print repack_repo(path)
1896 path = os.path.join(self.git_root, "org/project1")
1897 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001898
1899 A.addApproval('CRVW', 2)
1900 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1901 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001902 assert self.getJobFromHistory('project1-merge').result == 'SUCCESS'
1903 assert self.getJobFromHistory('project1-test1').result == 'SUCCESS'
1904 assert self.getJobFromHistory('project1-test2').result == 'SUCCESS'
James E. Blair4886f282012-11-15 09:27:33 -08001905 assert A.data['status'] == 'MERGED'
1906 assert A.reported == 2
1907 self.assertEmptyQueues()
1908
James E. Blair7ee88a22012-09-12 18:59:31 +02001909 def test_nonexistent_job(self):
1910 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001911 # Set to the state immediately after a restart
1912 self.resetGearmanServer()
1913 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02001914
1915 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1916 A.addApproval('CRVW', 2)
1917 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1918 # There may be a thread about to report a lost change
1919 while A.reported < 2:
1920 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001921 job_names = [x.name for x in self.history]
James E. Blair7ee88a22012-09-12 18:59:31 +02001922 assert not job_names
1923 assert A.data['status'] == 'NEW'
1924 assert A.reported == 2
1925 self.assertEmptyQueues()
1926
1927 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001928 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02001929 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1930 A.addApproval('CRVW', 2)
1931 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1932 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001933 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1934 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1935 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blair7ee88a22012-09-12 18:59:31 +02001936 assert A.data['status'] == 'MERGED'
1937 assert A.reported == 2
1938 self.assertEmptyQueues()
James E. Blairf62d4282012-12-31 17:01:50 -08001939
1940 def test_single_nonexistent_post_job(self):
1941 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08001942 e = {
1943 "type": "ref-updated",
1944 "submitter": {
1945 "name": "User Name",
1946 },
1947 "refUpdate": {
1948 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1949 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1950 "refName": "master",
1951 "project": "org/project",
1952 }
1953 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001954 # Set to the state immediately after a restart
1955 self.resetGearmanServer()
1956 self.launcher.negative_function_cache_ttl = 0
1957
James E. Blairf62d4282012-12-31 17:01:50 -08001958 self.fake_gerrit.addEvent(e)
1959 self.waitUntilSettled()
1960
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001961 assert len(self.history) == 0
James E. Blairf62d4282012-12-31 17:01:50 -08001962 self.assertEmptyQueues()
James E. Blair2fa50962013-01-30 21:50:41 -08001963
1964 def test_new_patchset_dequeues_old(self):
1965 "Test that a new patchset causes the old to be dequeued"
1966 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001967 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001968 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1969 M.setMerged()
1970
1971 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1972 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1973 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1974 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1975 A.addApproval('CRVW', 2)
1976 B.addApproval('CRVW', 2)
1977 C.addApproval('CRVW', 2)
1978 D.addApproval('CRVW', 2)
1979
1980 C.setDependsOn(B, 1)
1981 B.setDependsOn(A, 1)
1982 A.setDependsOn(M, 1)
1983
1984 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1985 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1986 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1987 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1988 self.waitUntilSettled()
1989
1990 B.addPatchset()
1991 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1992 self.waitUntilSettled()
1993
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001994 self.worker.hold_jobs_in_build = False
1995 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001996 self.waitUntilSettled()
1997
James E. Blair2fa50962013-01-30 21:50:41 -08001998 assert A.data['status'] == 'MERGED'
1999 assert A.reported == 2
2000 assert B.data['status'] == 'NEW'
2001 assert B.reported == 2
2002 assert C.data['status'] == 'NEW'
2003 assert C.reported == 2
2004 assert D.data['status'] == 'MERGED'
2005 assert D.reported == 2
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002006 assert len(self.history) == 9 # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002007 self.assertEmptyQueues()
2008
2009 def test_new_patchset_dequeues_old_on_head(self):
2010 "Test that a new patchset causes the old to be dequeued (at head)"
2011 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002012 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002013 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2014 M.setMerged()
2015 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2016 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2017 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2018 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2019 A.addApproval('CRVW', 2)
2020 B.addApproval('CRVW', 2)
2021 C.addApproval('CRVW', 2)
2022 D.addApproval('CRVW', 2)
2023
2024 C.setDependsOn(B, 1)
2025 B.setDependsOn(A, 1)
2026 A.setDependsOn(M, 1)
2027
2028 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2029 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2030 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2031 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2032 self.waitUntilSettled()
2033
2034 A.addPatchset()
2035 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2036 self.waitUntilSettled()
2037
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002038 self.worker.hold_jobs_in_build = False
2039 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002040 self.waitUntilSettled()
2041
James E. Blair2fa50962013-01-30 21:50:41 -08002042 assert A.data['status'] == 'NEW'
2043 assert A.reported == 2
2044 assert B.data['status'] == 'NEW'
2045 assert B.reported == 2
2046 assert C.data['status'] == 'NEW'
2047 assert C.reported == 2
2048 assert D.data['status'] == 'MERGED'
2049 assert D.reported == 2
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002050 assert len(self.history) == 7
James E. Blair2fa50962013-01-30 21:50:41 -08002051 self.assertEmptyQueues()
2052
2053 def test_new_patchset_dequeues_old_without_dependents(self):
2054 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002055 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002056 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2057 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2058 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2059 A.addApproval('CRVW', 2)
2060 B.addApproval('CRVW', 2)
2061 C.addApproval('CRVW', 2)
2062
2063 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2064 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2065 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2066 self.waitUntilSettled()
2067
2068 B.addPatchset()
2069 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2070 self.waitUntilSettled()
2071
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002072 self.worker.hold_jobs_in_build = False
2073 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002074 self.waitUntilSettled()
2075
James E. Blair2fa50962013-01-30 21:50:41 -08002076 assert A.data['status'] == 'MERGED'
2077 assert A.reported == 2
2078 assert B.data['status'] == 'NEW'
2079 assert B.reported == 2
2080 assert C.data['status'] == 'MERGED'
2081 assert C.reported == 2
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002082 assert len(self.history) == 9
James E. Blair2fa50962013-01-30 21:50:41 -08002083 self.assertEmptyQueues()
2084
2085 def test_new_patchset_dequeues_old_independent_queue(self):
2086 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002087 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002088 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2089 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2090 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2091 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2092 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2093 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2094 self.waitUntilSettled()
2095
2096 B.addPatchset()
2097 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2098 self.waitUntilSettled()
2099
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002100 self.worker.hold_jobs_in_build = False
2101 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002102 self.waitUntilSettled()
2103
James E. Blair2fa50962013-01-30 21:50:41 -08002104 assert A.data['status'] == 'NEW'
2105 assert A.reported == 1
2106 assert B.data['status'] == 'NEW'
2107 assert B.reported == 1
2108 assert C.data['status'] == 'NEW'
2109 assert C.reported == 1
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002110 assert len(self.history) == 10
2111 assert self.countJobResults(self.history, 'ABORTED') == 1
James E. Blair2fa50962013-01-30 21:50:41 -08002112 self.assertEmptyQueues()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002113
2114 def test_zuul_refs(self):
2115 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002116 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002117 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2118 M1.setMerged()
2119 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2120 M2.setMerged()
2121
2122 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2123 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2124 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2125 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2126 A.addApproval('CRVW', 2)
2127 B.addApproval('CRVW', 2)
2128 C.addApproval('CRVW', 2)
2129 D.addApproval('CRVW', 2)
2130 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2131 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2132 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2133 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2134
2135 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002136 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002137 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002138 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002139 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002140 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002141 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002142 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002143 self.waitUntilSettled()
2144
James E. Blair7d0dedc2013-02-21 17:26:09 -08002145 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002146 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002147 if x.parameters['ZUUL_CHANGE'] == '3':
2148 a_zref = x.parameters['ZUUL_REF']
2149 if x.parameters['ZUUL_CHANGE'] == '4':
2150 b_zref = x.parameters['ZUUL_REF']
2151 if x.parameters['ZUUL_CHANGE'] == '5':
2152 c_zref = x.parameters['ZUUL_REF']
2153 if x.parameters['ZUUL_CHANGE'] == '6':
2154 d_zref = x.parameters['ZUUL_REF']
2155
2156 # There are... four... refs.
2157 assert a_zref is not None
2158 assert b_zref is not None
2159 assert c_zref is not None
2160 assert d_zref is not None
2161
2162 # And they should all be different
2163 refs = set([a_zref, b_zref, c_zref, d_zref])
2164 assert len(refs) == 4
2165
2166 # a ref should have a, not b, and should not be in project2
Monty Taylorbc758832013-06-17 17:22:42 -04002167 assert self.ref_has_change(a_zref, A)
2168 assert not self.ref_has_change(a_zref, B)
2169 assert not self.ref_has_change(a_zref, M2)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002170
2171 # b ref should have a and b, and should not be in project2
Monty Taylorbc758832013-06-17 17:22:42 -04002172 assert self.ref_has_change(b_zref, A)
2173 assert self.ref_has_change(b_zref, B)
2174 assert not self.ref_has_change(b_zref, M2)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002175
2176 # c ref should have a and b in 1, c in 2
Monty Taylorbc758832013-06-17 17:22:42 -04002177 assert self.ref_has_change(c_zref, A)
2178 assert self.ref_has_change(c_zref, B)
2179 assert self.ref_has_change(c_zref, C)
2180 assert not self.ref_has_change(c_zref, D)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002181
2182 # d ref should have a and b in 1, c and d in 2
Monty Taylorbc758832013-06-17 17:22:42 -04002183 assert self.ref_has_change(d_zref, A)
2184 assert self.ref_has_change(d_zref, B)
2185 assert self.ref_has_change(d_zref, C)
2186 assert self.ref_has_change(d_zref, D)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002187
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002188 self.worker.hold_jobs_in_build = False
2189 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002190 self.waitUntilSettled()
2191
2192 assert A.data['status'] == 'MERGED'
2193 assert A.reported == 2
2194 assert B.data['status'] == 'MERGED'
2195 assert B.reported == 2
2196 assert C.data['status'] == 'MERGED'
2197 assert C.reported == 2
2198 assert D.data['status'] == 'MERGED'
2199 assert D.reported == 2
2200 self.assertEmptyQueues()
James E. Blair70c71582013-03-06 08:50:50 -08002201
James E. Blair412e5582013-04-22 15:50:12 -07002202 def test_statsd(self):
2203 "Test each of the statsd methods used in the scheduler"
2204 import extras
2205 statsd = extras.try_import('statsd.statsd')
2206 statsd.incr('test-incr')
2207 statsd.timing('test-timing', 3)
2208 statsd.gauge('test-guage', 12)
2209 self.assertReportedStat('test-incr', '1|c')
2210 self.assertReportedStat('test-timing', '3|ms')
2211 self.assertReportedStat('test-guage', '12|g')
2212
James E. Blair70c71582013-03-06 08:50:50 -08002213 def test_file_jobs(self):
2214 "Test that file jobs run only when appropriate"
2215 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2216 A.addPatchset(['pip-requires'])
2217 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2218 A.addApproval('CRVW', 2)
2219 B.addApproval('CRVW', 2)
2220 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2221 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2222 self.waitUntilSettled()
2223
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002224 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002225 if x.name == 'project-testfile']
2226
2227 assert len(testfile_jobs) == 1
2228 assert testfile_jobs[0].changes == '1,2'
2229 assert A.data['status'] == 'MERGED'
2230 assert A.reported == 2
2231 assert B.data['status'] == 'MERGED'
2232 assert B.reported == 2
2233 self.assertEmptyQueues()
James E. Blair3c5e5b52013-04-26 11:17:03 -07002234
2235 def test_test_config(self):
2236 "Test that we can test the config"
2237 sched = zuul.scheduler.Scheduler()
2238 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002239
2240 def test_build_description(self):
2241 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002242 self.worker.registerFunction('set_description:' +
2243 self.worker.worker_id)
2244
2245 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2246 A.addApproval('CRVW', 2)
2247 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2248 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002249 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002250 self.log.debug("Description: %s" % desc)
2251 assert re.search("Branch.*master", desc)
2252 assert re.search("Pipeline.*gate", desc)
2253 assert re.search("project-merge.*SUCCESS", desc)
2254 assert re.search("project-test1.*SUCCESS", desc)
2255 assert re.search("project-test2.*SUCCESS", desc)
2256 assert re.search("Reported result.*SUCCESS", desc)
2257
2258 def test_node_label(self):
2259 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002260 self.worker.registerFunction('build:node-project-test1:debian')
2261
2262 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2263 A.addApproval('CRVW', 2)
2264 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2265 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002266
2267 assert self.getJobFromHistory('node-project-merge').node is None
2268 assert self.getJobFromHistory('node-project-test1').node == 'debian'
2269 assert self.getJobFromHistory('node-project-test2').node is None