blob: 48baaeff97751e65938e9514d9440dd630404cd1 [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
Monty Taylorbc758832013-06-17 17:22:42 -040032import threading
33import time
James E. Blair1843a552013-07-03 14:19:52 -070034import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040035import 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. Blair1843a552013-07-03 14:19:52 -070045import zuul.webapp
James E. Blair1f4c2bb2013-04-26 08:40:46 -070046import zuul.launcher.gearman
James E. Blairb0fcae42012-07-17 11:12:10 -070047import zuul.trigger.gerrit
James E. Blair63bb0ef2013-07-29 17:14:51 -070048import zuul.trigger.timer
James E. Blairb0fcae42012-07-17 11:12:10 -070049
50FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
51 'fixtures')
52CONFIG = ConfigParser.ConfigParser()
53CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
54
55CONFIG.set('zuul', 'layout_config',
56 os.path.join(FIXTURE_DIR, "layout.yaml"))
57
James E. Blair1f4c2bb2013-04-26 08:40:46 -070058logging.basicConfig(level=logging.DEBUG,
59 format='%(asctime)s %(name)-32s '
60 '%(levelname)-8s %(message)s')
James E. Blairb0fcae42012-07-17 11:12:10 -070061
62
Monty Taylorbc758832013-06-17 17:22:42 -040063def repack_repo(path):
64 output = subprocess.Popen(
65 ['git', '--git-dir=%s/.git' % path, 'repack', '-afd'],
66 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
67 out = output.communicate()
68 if output.returncode:
69 raise Exception("git repack returned %d" % output.returncode)
70 return out
71
72
James E. Blair8cc15a82012-08-01 11:17:57 -070073def random_sha1():
74 return hashlib.sha1(str(random.random())).hexdigest()
75
76
James E. Blair4886cc12012-07-18 15:39:41 -070077class ChangeReference(git.Reference):
78 _common_path_default = "refs/changes"
79 _points_to_commits_only = True
80
81
James E. Blairb0fcae42012-07-17 11:12:10 -070082class FakeChange(object):
James E. Blair8c803f82012-07-31 16:25:42 -070083 categories = {'APRV': ('Approved', -1, 1),
84 'CRVW': ('Code-Review', -2, 2),
85 'VRFY': ('Verified', -2, 2)}
James E. Blairb0fcae42012-07-17 11:12:10 -070086
Monty Taylorbc758832013-06-17 17:22:42 -040087 def __init__(self, gerrit, number, project, branch, subject,
88 status='NEW', upstream_root=None):
James E. Blair8cc15a82012-08-01 11:17:57 -070089 self.gerrit = gerrit
James E. Blaird466dc42012-07-31 10:42:56 -070090 self.reported = 0
James E. Blair8c803f82012-07-31 16:25:42 -070091 self.queried = 0
James E. Blairb0fcae42012-07-17 11:12:10 -070092 self.patchsets = []
James E. Blairb0fcae42012-07-17 11:12:10 -070093 self.number = number
94 self.project = project
95 self.branch = branch
96 self.subject = subject
97 self.latest_patchset = 0
James E. Blair8c803f82012-07-31 16:25:42 -070098 self.depends_on_change = None
99 self.needed_by_changes = []
James E. Blair127bc182012-08-28 15:55:15 -0700100 self.fail_merge = False
James E. Blair42f74822013-05-14 15:18:03 -0700101 self.messages = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700102 self.data = {
103 'branch': branch,
104 'comments': [],
105 'commitMessage': subject,
106 'createdOn': time.time(),
James E. Blair8cc15a82012-08-01 11:17:57 -0700107 'id': 'I' + random_sha1(),
James E. Blairb0fcae42012-07-17 11:12:10 -0700108 'lastUpdated': time.time(),
109 'number': str(number),
110 'open': True,
111 'owner': {'email': 'user@example.com',
112 'name': 'User Name',
113 'username': 'username'},
114 'patchSets': self.patchsets,
115 'project': project,
116 'status': status,
117 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700118 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700119 'url': 'https://hostname/%s' % number}
120
Monty Taylorbc758832013-06-17 17:22:42 -0400121 self.upstream_root = upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700122 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700123 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700124
Monty Taylorbc758832013-06-17 17:22:42 -0400125 def add_fake_change_to_repo(self, msg, fn, large):
126 path = os.path.join(self.upstream_root, self.project)
127 repo = git.Repo(path)
128 ref = ChangeReference.create(repo, '1/%s/%s' % (self.number,
129 self.latest_patchset),
130 'refs/tags/init')
131 repo.head.reference = ref
132 repo.head.reset(index=True, working_tree=True)
133 repo.git.clean('-x', '-f', '-d')
134
135 path = os.path.join(self.upstream_root, self.project)
136 if not large:
137 fn = os.path.join(path, fn)
138 f = open(fn, 'w')
139 f.write("test %s %s %s\n" %
140 (self.branch, self.number, self.latest_patchset))
141 f.close()
142 repo.index.add([fn])
143 else:
144 for fni in range(100):
145 fn = os.path.join(path, str(fni))
146 f = open(fn, 'w')
147 for ci in range(4096):
148 f.write(random.choice(string.printable))
149 f.close()
150 repo.index.add([fn])
151
James E. Blair287c06d2013-07-24 10:39:30 -0700152 r = repo.index.commit(msg)
153 repo.head.reference = 'master'
154 repo.head.reset(index=True, working_tree=True)
155 repo.git.clean('-x', '-f', '-d')
156 return r
Monty Taylorbc758832013-06-17 17:22:42 -0400157
James E. Blair70c71582013-03-06 08:50:50 -0800158 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700159 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700160 if files:
161 fn = files[0]
162 else:
163 fn = '%s-%s' % (self.branch, self.number)
164 msg = self.subject + '-' + str(self.latest_patchset)
Monty Taylorbc758832013-06-17 17:22:42 -0400165 c = self.add_fake_change_to_repo(msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800166 ps_files = [{'file': '/COMMIT_MSG',
167 'type': 'ADDED'},
168 {'file': 'README',
169 'type': 'MODIFIED'}]
170 for f in files:
171 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700172 d = {'approvals': [],
173 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800174 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700175 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700176 'ref': 'refs/changes/1/%s/%s' % (self.number,
177 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700178 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700179 'uploader': {'email': 'user@example.com',
180 'name': 'User name',
181 'username': 'user'}}
182 self.data['currentPatchSet'] = d
183 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700184 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700185
James E. Blaire0487072012-08-29 17:38:31 -0700186 def getPatchsetCreatedEvent(self, patchset):
187 event = {"type": "patchset-created",
188 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800189 "branch": self.branch,
190 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
191 "number": str(self.number),
192 "subject": self.subject,
193 "owner": {"name": "User Name"},
194 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700195 "patchSet": self.patchsets[patchset - 1],
196 "uploader": {"name": "User Name"}}
197 return event
198
James E. Blair42f74822013-05-14 15:18:03 -0700199 def getChangeRestoredEvent(self):
200 event = {"type": "change-restored",
201 "change": {"project": self.project,
202 "branch": self.branch,
203 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
204 "number": str(self.number),
205 "subject": self.subject,
206 "owner": {"name": "User Name"},
207 "url": "https://hostname/3"},
208 "restorer": {"name": "User Name"},
209 "reason": ""}
210 return event
211
James E. Blairb0fcae42012-07-17 11:12:10 -0700212 def addApproval(self, category, value):
James E. Blair8c803f82012-07-31 16:25:42 -0700213 approval = {'description': self.categories[category][0],
214 'type': category,
215 'value': str(value)}
216 self.patchsets[-1]['approvals'].append(approval)
217 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700218 'author': {'email': 'user@example.com',
219 'name': 'User Name',
220 'username': 'username'},
221 'change': {'branch': self.branch,
222 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
223 'number': str(self.number),
224 'owner': {'email': 'user@example.com',
225 'name': 'User Name',
226 'username': 'username'},
227 'project': self.project,
228 'subject': self.subject,
229 'topic': 'master',
230 'url': 'https://hostname/459'},
231 'comment': '',
232 'patchSet': self.patchsets[-1],
233 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700234 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700235 return json.loads(json.dumps(event))
236
James E. Blair8c803f82012-07-31 16:25:42 -0700237 def getSubmitRecords(self):
238 status = {}
239 for cat in self.categories.keys():
240 status[cat] = 0
241
242 for a in self.patchsets[-1]['approvals']:
243 cur = status[a['type']]
244 cat_min, cat_max = self.categories[a['type']][1:]
245 new = int(a['value'])
246 if new == cat_min:
247 cur = new
248 elif abs(new) > abs(cur):
249 cur = new
250 status[a['type']] = cur
251
252 labels = []
253 ok = True
254 for typ, cat in self.categories.items():
255 cur = status[typ]
256 cat_min, cat_max = cat[1:]
257 if cur == cat_min:
258 value = 'REJECT'
259 ok = False
260 elif cur == cat_max:
261 value = 'OK'
262 else:
263 value = 'NEED'
264 ok = False
265 labels.append({'label': cat[0], 'status': value})
266 if ok:
267 return [{'status': 'OK'}]
268 return [{'status': 'NOT_READY',
269 'labels': labels}]
270
271 def setDependsOn(self, other, patchset):
272 self.depends_on_change = other
273 d = {'id': other.data['id'],
274 'number': other.data['number'],
275 'ref': other.patchsets[patchset - 1]['ref']
276 }
277 self.data['dependsOn'] = [d]
278
279 other.needed_by_changes.append(self)
280 needed = other.data.get('neededBy', [])
281 d = {'id': self.data['id'],
282 'number': self.data['number'],
283 'ref': self.patchsets[patchset - 1]['ref'],
284 'revision': self.patchsets[patchset - 1]['revision']
285 }
286 needed.append(d)
287 other.data['neededBy'] = needed
288
James E. Blairb0fcae42012-07-17 11:12:10 -0700289 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700290 self.queried += 1
291 d = self.data.get('dependsOn')
292 if d:
293 d = d[0]
294 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
295 d['isCurrentPatchSet'] = True
296 else:
297 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700298 return json.loads(json.dumps(self.data))
299
300 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800301 if (self.depends_on_change and
302 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700303 return
James E. Blair127bc182012-08-28 15:55:15 -0700304 if self.fail_merge:
305 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700306 self.data['status'] = 'MERGED'
307 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700308
Monty Taylorbc758832013-06-17 17:22:42 -0400309 path = os.path.join(self.upstream_root, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700310 repo = git.Repo(path)
311 repo.heads[self.branch].commit = \
312 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700313
James E. Blaird466dc42012-07-31 10:42:56 -0700314 def setReported(self):
315 self.reported += 1
316
James E. Blairb0fcae42012-07-17 11:12:10 -0700317
318class FakeGerrit(object):
319 def __init__(self, *args, **kw):
320 self.event_queue = Queue.Queue()
321 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
322 self.change_number = 0
323 self.changes = {}
324
325 def addFakeChange(self, project, branch, subject):
326 self.change_number += 1
Monty Taylorbc758832013-06-17 17:22:42 -0400327 c = FakeChange(self, self.change_number, project, branch, subject,
328 upstream_root=self.upstream_root)
James E. Blairb0fcae42012-07-17 11:12:10 -0700329 self.changes[self.change_number] = c
330 return c
331
332 def addEvent(self, data):
333 return self.event_queue.put(data)
334
335 def getEvent(self):
336 return self.event_queue.get()
337
338 def eventDone(self):
339 self.event_queue.task_done()
340
341 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700342 number, ps = changeid.split(',')
343 change = self.changes[int(number)]
James E. Blair42f74822013-05-14 15:18:03 -0700344 change.messages.append(message)
James E. Blairb0fcae42012-07-17 11:12:10 -0700345 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700346 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700347 if message:
348 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700349
350 def query(self, number):
351 change = self.changes[int(number)]
352 return change.query()
353
354 def startWatching(self, *args, **kw):
355 pass
356
357
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700358class BuildHistory(object):
359 def __init__(self, **kw):
360 self.__dict__.update(kw)
James E. Blairb0fcae42012-07-17 11:12:10 -0700361
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700362 def __repr__(self):
363 return ("<Completed build, result: %s name: %s #%s changes: %s>" %
364 (self.result, self.name, self.number, self.changes))
James E. Blairb0fcae42012-07-17 11:12:10 -0700365
366
James E. Blair8cc15a82012-08-01 11:17:57 -0700367class FakeURLOpener(object):
Monty Taylorbc758832013-06-17 17:22:42 -0400368 def __init__(self, upstream_root, fake_gerrit, url):
369 self.upstream_root = upstream_root
James E. Blair8cc15a82012-08-01 11:17:57 -0700370 self.fake_gerrit = fake_gerrit
371 self.url = url
372
373 def read(self):
374 res = urlparse.urlparse(self.url)
375 path = res.path
376 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200377 ret = '001e# service=git-upload-pack\n'
378 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
379 'multi_ack thin-pack side-band side-band-64k ofs-delta '
380 'shallow no-progress include-tag multi_ack_detailed no-done\n')
Monty Taylorbc758832013-06-17 17:22:42 -0400381 path = os.path.join(self.upstream_root, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700382 repo = git.Repo(path)
383 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200384 r = ref.object.hexsha + ' ' + ref.path + '\n'
385 ret += '%04x%s' % (len(r) + 4, r)
386 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700387 return ret
388
389
James E. Blair4886cc12012-07-18 15:39:41 -0700390class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
Monty Taylorbc758832013-06-17 17:22:42 -0400391 def __init__(self, upstream_root, *args):
392 super(FakeGerritTrigger, self).__init__(*args)
393 self.upstream_root = upstream_root
394
James E. Blair4886cc12012-07-18 15:39:41 -0700395 def getGitUrl(self, project):
Monty Taylorbc758832013-06-17 17:22:42 -0400396 return os.path.join(self.upstream_root, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700397
398
James E. Blair412e5582013-04-22 15:50:12 -0700399class FakeStatsd(threading.Thread):
400 def __init__(self):
401 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400402 self.daemon = True
James E. Blair412e5582013-04-22 15:50:12 -0700403 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
404 self.sock.bind(('', 0))
405 self.port = self.sock.getsockname()[1]
406 self.wake_read, self.wake_write = os.pipe()
407 self.stats = []
408
409 def run(self):
410 while True:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700411 poll = select.poll()
412 poll.register(self.sock, select.POLLIN)
413 poll.register(self.wake_read, select.POLLIN)
414 ret = poll.poll()
415 for (fd, event) in ret:
416 if fd == self.sock.fileno():
James E. Blair412e5582013-04-22 15:50:12 -0700417 data = self.sock.recvfrom(1024)
418 if not data:
419 return
420 self.stats.append(data[0])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700421 if fd == self.wake_read:
James E. Blair412e5582013-04-22 15:50:12 -0700422 return
423
424 def stop(self):
425 os.write(self.wake_write, '1\n')
426
427
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700428class FakeBuild(threading.Thread):
429 log = logging.getLogger("zuul.test")
430
431 def __init__(self, worker, job, number, node):
432 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400433 self.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700434 self.worker = worker
435 self.job = job
436 self.name = job.name.split(':')[1]
437 self.number = number
438 self.node = node
439 self.parameters = json.loads(job.arguments)
440 self.unique = self.parameters['ZUUL_UUID']
441 self.wait_condition = threading.Condition()
442 self.waiting = False
443 self.aborted = False
444 self.created = time.time()
445 self.description = ''
446
447 def release(self):
448 self.wait_condition.acquire()
449 self.wait_condition.notify()
450 self.waiting = False
451 self.log.debug("Build %s released" % self.unique)
452 self.wait_condition.release()
453
454 def isWaiting(self):
455 self.wait_condition.acquire()
456 if self.waiting:
457 ret = True
458 else:
459 ret = False
460 self.wait_condition.release()
461 return ret
462
463 def _wait(self):
464 self.wait_condition.acquire()
465 self.waiting = True
466 self.log.debug("Build %s waiting" % self.unique)
467 self.wait_condition.wait()
468 self.wait_condition.release()
469
470 def run(self):
471 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700472 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
473 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700474 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700475 'manager': self.worker.worker_id,
Paul Belangerec49b4c2013-07-20 20:32:20 -0400476 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700477
478 self.job.sendWorkData(json.dumps(data))
479 self.job.sendWorkStatus(0, 100)
480
481 if self.worker.hold_jobs_in_build:
482 self._wait()
483 self.log.debug("Build %s continuing" % self.unique)
484
485 self.worker.lock.acquire()
486
487 result = 'SUCCESS'
488 if (('ZUUL_REF' in self.parameters) and
489 self.worker.shouldFailTest(self.name,
490 self.parameters['ZUUL_REF'])):
491 result = 'FAILURE'
492 if self.aborted:
493 result = 'ABORTED'
494
495 data = {'result': result}
496 changes = None
497 if 'ZUUL_CHANGE_IDS' in self.parameters:
498 changes = self.parameters['ZUUL_CHANGE_IDS']
499
500 self.worker.build_history.append(
501 BuildHistory(name=self.name, number=self.number,
502 result=result, changes=changes, node=self.node,
James E. Blair64ed6f22013-07-10 14:07:23 -0700503 uuid=self.unique, description=self.description,
504 pipeline=self.parameters['ZUUL_PIPELINE'])
Paul Belangerec49b4c2013-07-20 20:32:20 -0400505 )
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700506
507 self.job.sendWorkComplete(json.dumps(data))
508 del self.worker.gearman_jobs[self.job.unique]
509 self.worker.running_builds.remove(self)
510 self.worker.lock.release()
511
512
513class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400514 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700515 super(FakeWorker, self).__init__(worker_id)
516 self.gearman_jobs = {}
517 self.build_history = []
518 self.running_builds = []
519 self.build_counter = 0
520 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400521 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700522
523 self.hold_jobs_in_build = False
524 self.lock = threading.Lock()
525 self.__work_thread = threading.Thread(target=self.work)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400526 self.__work_thread.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700527 self.__work_thread.start()
528
529 def handleJob(self, job):
530 parts = job.name.split(":")
531 cmd = parts[0]
532 name = parts[1]
533 if len(parts) > 2:
534 node = parts[2]
535 else:
536 node = None
537 if cmd == 'build':
538 self.handleBuild(job, name, node)
539 elif cmd == 'stop':
540 self.handleStop(job, name)
541 elif cmd == 'set_description':
542 self.handleSetDescription(job, name)
543
544 def handleBuild(self, job, name, node):
545 build = FakeBuild(self, job, self.build_counter, node)
546 job.build = build
547 self.gearman_jobs[job.unique] = job
548 self.build_counter += 1
549
550 self.running_builds.append(build)
551 build.start()
552
553 def handleStop(self, job, name):
554 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700555 parameters = json.loads(job.arguments)
556 name = parameters['name']
557 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700558 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700559 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700560 build.aborted = True
561 build.release()
562 job.sendWorkComplete()
563 return
564 job.sendWorkFail()
565
566 def handleSetDescription(self, job, name):
567 self.log.debug("handle set description")
568 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700569 name = parameters['name']
570 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700571 descr = parameters['html_description']
572 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700573 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700574 build.description = descr
575 job.sendWorkComplete()
576 return
577 for build in self.build_history:
James E. Blair3c483cf2013-06-04 16:30:43 -0700578 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700579 build.description = descr
580 job.sendWorkComplete()
581 return
582 job.sendWorkFail()
583
584 def work(self):
585 while self.running:
586 try:
587 job = self.getJob()
588 except gear.InterruptedError:
589 continue
590 try:
591 self.handleJob(job)
592 except:
593 self.log.exception("Worker exception:")
594
595 def addFailTest(self, name, change):
596 l = self.fail_tests.get(name, [])
597 l.append(change)
598 self.fail_tests[name] = l
599
600 def shouldFailTest(self, name, ref):
601 l = self.fail_tests.get(name, [])
602 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400603 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700604 return True
605 return False
606
607 def release(self, regex=None):
608 builds = self.running_builds[:]
609 self.log.debug("releasing build %s (%s)" % (regex,
James E. Blair78e31b32013-07-09 09:11:34 -0700610 len(self.running_builds)))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700611 for build in builds:
612 if not regex or re.match(regex, build.name):
613 self.log.debug("releasing build %s" %
614 (build.parameters['ZUUL_UUID']))
615 build.release()
616 else:
617 self.log.debug("not releasing build %s" %
618 (build.parameters['ZUUL_UUID']))
619 self.log.debug("done releasing builds %s (%s)" %
620 (regex, len(self.running_builds)))
621
622
623class FakeGearmanServer(gear.Server):
624 def __init__(self):
625 self.hold_jobs_in_queue = False
626 super(FakeGearmanServer, self).__init__(0)
627
628 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700629 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
630 for job in queue:
631 if not hasattr(job, 'waiting'):
632 if job.name.startswith('build:'):
633 job.waiting = self.hold_jobs_in_queue
634 else:
635 job.waiting = False
636 if job.waiting:
637 continue
638 if job.name in connection.functions:
639 if not peek:
640 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400641 connection.related_jobs[job.handle] = job
642 job.worker_connection = connection
643 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700644 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700645 return None
646
647 def release(self, regex=None):
648 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700649 qlen = (len(self.high_queue) + len(self.normal_queue) +
650 len(self.low_queue))
651 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
James E. Blairdda6c912013-07-29 14:12:12 -0700652 for job in self.getQueue():
653 cmd, name = job.name.split(':')
654 if cmd != 'build':
655 continue
656 if not regex or re.match(regex, name):
657 self.log.debug("releasing queued job %s" %
658 job.unique)
659 job.waiting = False
660 released = True
661 else:
662 self.log.debug("not releasing queued job %s" %
663 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700664 if released:
665 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700666 qlen = (len(self.high_queue) + len(self.normal_queue) +
667 len(self.low_queue))
668 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700669
670
Monty Taylorbc758832013-06-17 17:22:42 -0400671class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700672 log = logging.getLogger("zuul.test")
673
674 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400675 super(TestScheduler, self).setUp()
676 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
677 try:
678 test_timeout = int(test_timeout)
679 except ValueError:
680 # If timeout value is invalid do not set a timeout.
681 test_timeout = 0
682 if test_timeout > 0:
683 self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
684
685 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
686 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
687 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
688 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
689 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
690 os.environ.get('OS_STDERR_CAPTURE') == '1'):
691 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
692 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Monty Taylore6a6c402013-07-02 09:25:55 -0700693 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
694 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400695 self.useFixture(fixtures.FakeLogger(
696 level=logging.DEBUG,
697 format='%(asctime)s %(name)-32s '
698 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700699 tmp_root = self.useFixture(fixtures.TempDir(
700 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400701 self.test_root = os.path.join(tmp_root, "zuul-test")
702 self.upstream_root = os.path.join(self.test_root, "upstream")
703 self.git_root = os.path.join(self.test_root, "git")
704
705 CONFIG.set('zuul', 'git_dir', self.git_root)
706 if os.path.exists(self.test_root):
707 shutil.rmtree(self.test_root)
708 os.makedirs(self.test_root)
709 os.makedirs(self.upstream_root)
710 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700711
712 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400713 self.init_repo("org/project")
714 self.init_repo("org/project1")
715 self.init_repo("org/project2")
716 self.init_repo("org/project3")
717 self.init_repo("org/one-job-project")
718 self.init_repo("org/nonvoting-project")
719 self.init_repo("org/templated-project")
720 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700721 self.init_repo("org/conflict-project")
James E. Blair412e5582013-04-22 15:50:12 -0700722
723 self.statsd = FakeStatsd()
724 os.environ['STATSD_HOST'] = 'localhost'
725 os.environ['STATSD_PORT'] = str(self.statsd.port)
726 self.statsd.start()
727 # the statsd client object is configured in the statsd module import
728 reload(statsd)
729 reload(zuul.scheduler)
730
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700731 self.gearman_server = FakeGearmanServer()
732
733 self.config = ConfigParser.ConfigParser()
734 cfg = StringIO()
735 CONFIG.write(cfg)
736 cfg.seek(0)
737 self.config.readfp(cfg)
738 self.config.set('gearman', 'port', str(self.gearman_server.port))
739
Monty Taylorbc758832013-06-17 17:22:42 -0400740 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700741 self.worker.addServer('127.0.0.1', self.gearman_server.port)
742 self.gearman_server.worker = self.worker
743
James E. Blairb0fcae42012-07-17 11:12:10 -0700744 self.sched = zuul.scheduler.Scheduler()
745
James E. Blair8cc15a82012-08-01 11:17:57 -0700746 def URLOpenerFactory(*args, **kw):
747 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400748 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700749
James E. Blair8cc15a82012-08-01 11:17:57 -0700750 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700751 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700752
753 zuul.lib.gerrit.Gerrit = FakeGerrit
754
Monty Taylorbc758832013-06-17 17:22:42 -0400755 self.gerrit = FakeGerritTrigger(
756 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700757 self.gerrit.replication_timeout = 1.5
758 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700759 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400760 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700761
James E. Blair1843a552013-07-03 14:19:52 -0700762 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
763
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700764 self.sched.setLauncher(self.launcher)
James E. Blair6c358e72013-07-29 17:06:47 -0700765 self.sched.registerTrigger(self.gerrit)
James E. Blair63bb0ef2013-07-29 17:14:51 -0700766 self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
767 self.sched.registerTrigger(self.timer)
James E. Blairb0fcae42012-07-17 11:12:10 -0700768
769 self.sched.start()
770 self.sched.reconfigure(self.config)
771 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700772 self.webapp.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700773 self.launcher.gearman.waitForServer()
774 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400775 self.builds = self.worker.running_builds
776 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700777
James E. Blairfee8d652013-06-07 08:57:52 -0700778 self.addCleanup(self.assertFinalState)
779 self.addCleanup(self.shutdown)
780
781 def assertFinalState(self):
782 # Make sure that the change cache is cleared
James E. Blair6c358e72013-07-29 17:06:47 -0700783 self.assertEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700784 self.assertEmptyQueues()
785
786 def shutdown(self):
787 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700788 self.launcher.stop()
789 self.worker.shutdown()
790 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700791 self.gerrit.stop()
James E. Blair63bb0ef2013-07-29 17:14:51 -0700792 self.timer.stop()
James E. Blairb0fcae42012-07-17 11:12:10 -0700793 self.sched.stop()
794 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700795 self.statsd.stop()
796 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700797 self.webapp.stop()
798 self.webapp.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700799 threads = threading.enumerate()
800 if len(threads) > 1:
801 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400802 super(TestScheduler, self).tearDown()
803
804 def init_repo(self, project):
805 parts = project.split('/')
806 path = os.path.join(self.upstream_root, *parts[:-1])
807 if not os.path.exists(path):
808 os.makedirs(path)
809 path = os.path.join(self.upstream_root, project)
810 repo = git.Repo.init(path)
811
812 repo.config_writer().set_value('user', 'email', 'user@example.com')
813 repo.config_writer().set_value('user', 'name', 'User Name')
814 repo.config_writer().write()
815
816 fn = os.path.join(path, 'README')
817 f = open(fn, 'w')
818 f.write("test\n")
819 f.close()
820 repo.index.add([fn])
821 repo.index.commit('initial commit')
822 master = repo.create_head('master')
823 repo.create_tag('init')
824
825 mp = repo.create_head('mp')
826 repo.head.reference = mp
827 f = open(fn, 'a')
828 f.write("test mp\n")
829 f.close()
830 repo.index.add([fn])
831 repo.index.commit('mp commit')
832
833 repo.head.reference = master
834 repo.head.reset(index=True, working_tree=True)
835 repo.git.clean('-x', '-f', '-d')
836
837 def ref_has_change(self, ref, change):
838 path = os.path.join(self.git_root, change.project)
839 repo = git.Repo(path)
840 for commit in repo.iter_commits(ref):
841 if commit.message.strip() == ('%s-1' % change.subject):
842 return True
843 return False
844
845 def job_has_changes(self, *args):
846 job = args[0]
847 commits = args[1:]
848 if isinstance(job, FakeBuild):
849 parameters = job.parameters
850 else:
851 parameters = json.loads(job.arguments)
852 project = parameters['ZUUL_PROJECT']
853 path = os.path.join(self.git_root, project)
854 repo = git.Repo(path)
855 ref = parameters['ZUUL_REF']
856 sha = parameters['ZUUL_COMMIT']
857 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
858 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
859 commit_messages = ['%s-1' % commit.subject for commit in commits]
860 for msg in commit_messages:
861 if msg not in repo_messages:
862 return False
863 if repo_shas[0] != sha:
864 return False
865 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700866
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700867 def registerJobs(self):
868 count = 0
James E. Blaireff88162013-07-01 12:44:14 -0400869 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700870 self.worker.registerFunction('build:' + job)
871 count += 1
872 self.worker.registerFunction('stop:' + self.worker.worker_id)
873 count += 1
874
875 while len(self.gearman_server.functions) < count:
876 time.sleep(0)
877
878 def release(self, job):
879 if isinstance(job, FakeBuild):
880 job.release()
881 else:
882 job.waiting = False
883 self.log.debug("Queued job %s released" % job.unique)
884 self.gearman_server.wakeConnections()
885
886 def getParameter(self, job, name):
887 if isinstance(job, FakeBuild):
888 return job.parameters[name]
889 else:
890 parameters = json.loads(job.arguments)
891 return parameters[name]
892
893 def resetGearmanServer(self):
894 self.worker.setFunctions([])
895 while True:
896 done = True
897 for connection in self.gearman_server.active_connections:
898 if connection.functions:
899 done = False
900 if done:
901 break
902 time.sleep(0)
903 self.gearman_server.functions = set()
904
905 def haveAllBuildsReported(self):
906 # See if Zuul is waiting on a meta job to complete
907 if self.launcher.meta_jobs:
908 return False
909 # Find out if every build that the worker has completed has been
910 # reported back to Zuul. If it hasn't then that means a Gearman
911 # event is still in transit and the system is not stable.
912 for build in self.worker.build_history:
913 zbuild = self.launcher.builds.get(build.uuid)
914 if not zbuild:
915 # It has already been reported
916 continue
917 # It hasn't been reported yet.
918 return False
919 # Make sure that none of the worker connections are in GRAB_WAIT
920 for connection in self.worker.active_connections:
921 if connection.state == 'GRAB_WAIT':
922 return False
923 return True
924
925 def areAllBuildsWaiting(self):
926 ret = True
927
928 builds = self.launcher.builds.values()
929 for build in builds:
930 client_job = None
931 for conn in self.launcher.gearman.active_connections:
932 for j in conn.related_jobs.values():
933 if j.unique == build.uuid:
934 client_job = j
935 break
936 if not client_job:
937 self.log.debug("%s is not known to the gearman client" %
938 build)
939 ret = False
940 continue
941 if not client_job.handle:
942 self.log.debug("%s has no handle" % client_job)
943 ret = False
944 continue
945 server_job = self.gearman_server.jobs.get(client_job.handle)
946 if not server_job:
947 self.log.debug("%s is not known to the gearman server" %
948 client_job)
949 ret = False
950 continue
951 if not hasattr(server_job, 'waiting'):
952 self.log.debug("%s is being enqueued" % server_job)
953 ret = False
954 continue
955 if server_job.waiting:
956 continue
957 worker_job = self.worker.gearman_jobs.get(server_job.unique)
958 if worker_job:
959 if worker_job.build.isWaiting():
960 continue
961 else:
962 self.log.debug("%s is running" % worker_job)
963 ret = False
964 else:
965 self.log.debug("%s is unassigned" % server_job)
966 ret = False
967 return ret
968
James E. Blairb0fcae42012-07-17 11:12:10 -0700969 def waitUntilSettled(self):
970 self.log.debug("Waiting until settled...")
971 start = time.time()
972 while True:
973 if time.time() - start > 10:
974 print 'queue status:',
975 print self.sched.trigger_event_queue.empty(),
976 print self.sched.result_event_queue.empty(),
977 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700978 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -0700979 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700980 # Make sure no new events show up while we're checking
981 self.worker.lock.acquire()
982 # have all build states propogated to zuul?
983 if self.haveAllBuildsReported():
984 # Join ensures that the queue is empty _and_ events have been
985 # processed
986 self.fake_gerrit.event_queue.join()
987 self.sched.trigger_event_queue.join()
988 self.sched.result_event_queue.join()
989 if (self.sched.trigger_event_queue.empty() and
990 self.sched.result_event_queue.empty() and
991 self.fake_gerrit.event_queue.empty() and
992 self.areAllBuildsWaiting()):
993 self.worker.lock.release()
994 self.log.debug("...settled.")
995 return
996 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700997 self.sched.wake_event.wait(0.1)
998
James E. Blaird466dc42012-07-31 10:42:56 -0700999 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -08001000 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -07001001 return len(jobs)
1002
James E. Blair4ca985f2013-05-30 12:27:43 -07001003 def getJobFromHistory(self, name):
1004 history = self.worker.build_history
1005 for job in history:
1006 if job.name == name:
1007 return job
1008 raise Exception("Unable to find job %s in history" % name)
1009
James E. Blaire0487072012-08-29 17:38:31 -07001010 def assertEmptyQueues(self):
1011 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001012 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001013 for queue in pipeline.queues:
1014 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001015 print 'pipeline %s queue %s contents %s' % (
1016 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001017 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001018 if len(queue.severed_heads) != 0:
1019 print 'heads', queue.severed_heads
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001020 self.assertEqual(len(queue.severed_heads), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001021
James E. Blair66eeebf2013-07-27 17:44:32 -07001022 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001023 start = time.time()
1024 while time.time() < (start + 5):
1025 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001026 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001027 k, v = stat.split(':')
1028 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001029 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001030 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001031 elif value:
1032 if value == v:
1033 return
1034 elif kind:
1035 if v.endswith('|' + kind):
1036 return
James E. Blair412e5582013-04-22 15:50:12 -07001037 time.sleep(0.1)
1038
1039 pprint.pprint(self.statsd.stats)
1040 raise Exception("Key %s not found in reported stats" % key)
1041
James E. Blairb0fcae42012-07-17 11:12:10 -07001042 def test_jobs_launched(self):
1043 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001044
James E. Blairb0fcae42012-07-17 11:12:10 -07001045 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001046 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001047 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1048 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001049 self.assertEqual(self.getJobFromHistory('project-merge').result,
1050 'SUCCESS')
1051 self.assertEqual(self.getJobFromHistory('project-test1').result,
1052 'SUCCESS')
1053 self.assertEqual(self.getJobFromHistory('project-test2').result,
1054 'SUCCESS')
1055 self.assertEqual(A.data['status'], 'MERGED')
1056 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001057
James E. Blair66eeebf2013-07-27 17:44:32 -07001058 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1059 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1060 value='1|g')
1061 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1062 kind='ms')
1063 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1064 value='1|c')
1065 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1066 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1067 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001068 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001069 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001070 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001071 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001072
James E. Blair42f74822013-05-14 15:18:03 -07001073 def test_duplicate_pipelines(self):
1074 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001075
James E. Blair42f74822013-05-14 15:18:03 -07001076 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1077 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1078 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001079
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001080 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001081 self.history[0].name == 'project-test1'
1082 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001083
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001084 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001085 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001086 self.assertIn('dup1/project-test1', A.messages[0])
1087 self.assertNotIn('dup2/project-test1', A.messages[0])
1088 self.assertNotIn('dup1/project-test1', A.messages[1])
1089 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001090 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001091 self.assertIn('dup1/project-test1', A.messages[1])
1092 self.assertNotIn('dup2/project-test1', A.messages[1])
1093 self.assertNotIn('dup1/project-test1', A.messages[0])
1094 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001095
James E. Blairb0fcae42012-07-17 11:12:10 -07001096 def test_parallel_changes(self):
1097 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001098
1099 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001100 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1101 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1102 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001103 A.addApproval('CRVW', 2)
1104 B.addApproval('CRVW', 2)
1105 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001106
1107 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1108 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1109 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1110
1111 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001112 self.assertEqual(len(self.builds), 1)
1113 self.assertEqual(self.builds[0].name, 'project-merge')
1114 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001115
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001116 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001117 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001118 self.assertEqual(len(self.builds), 3)
1119 self.assertEqual(self.builds[0].name, 'project-test1')
1120 self.assertTrue(self.job_has_changes(self.builds[0], A))
1121 self.assertEqual(self.builds[1].name, 'project-test2')
1122 self.assertTrue(self.job_has_changes(self.builds[1], A))
1123 self.assertEqual(self.builds[2].name, 'project-merge')
1124 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001125
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001126 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001127 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001128 self.assertEqual(len(self.builds), 5)
1129 self.assertEqual(self.builds[0].name, 'project-test1')
1130 self.assertTrue(self.job_has_changes(self.builds[0], A))
1131 self.assertEqual(self.builds[1].name, 'project-test2')
1132 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001133
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001134 self.assertEqual(self.builds[2].name, 'project-test1')
1135 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1136 self.assertEqual(self.builds[3].name, 'project-test2')
1137 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001138
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001139 self.assertEqual(self.builds[4].name, 'project-merge')
1140 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001141
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001142 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001143 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001144 self.assertEqual(len(self.builds), 6)
1145 self.assertEqual(self.builds[0].name, 'project-test1')
1146 self.assertTrue(self.job_has_changes(self.builds[0], A))
1147 self.assertEqual(self.builds[1].name, 'project-test2')
1148 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001149
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001150 self.assertEqual(self.builds[2].name, 'project-test1')
1151 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1152 self.assertEqual(self.builds[3].name, 'project-test2')
1153 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001154
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001155 self.assertEqual(self.builds[4].name, 'project-test1')
1156 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1157 self.assertEqual(self.builds[5].name, 'project-test2')
1158 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001159
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001160 self.worker.hold_jobs_in_build = False
1161 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001162 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001163 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001164
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001165 self.assertEqual(len(self.history), 9)
1166 self.assertEqual(A.data['status'], 'MERGED')
1167 self.assertEqual(B.data['status'], 'MERGED')
1168 self.assertEqual(C.data['status'], 'MERGED')
1169 self.assertEqual(A.reported, 2)
1170 self.assertEqual(B.reported, 2)
1171 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001172
1173 def test_failed_changes(self):
1174 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001175 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001176
James E. Blairb02a3bb2012-07-30 17:49:55 -07001177 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1178 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001179 A.addApproval('CRVW', 2)
1180 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001181
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001182 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001183
James E. Blaire2819012013-06-28 17:17:26 -04001184 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1185 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001186 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001187
1188 self.worker.release('.*-merge')
1189 self.waitUntilSettled()
1190
1191 self.worker.hold_jobs_in_build = False
1192 self.worker.release()
1193
1194 self.waitUntilSettled()
1195 # It's certain that the merge job for change 2 will run, but
1196 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001197 self.assertTrue(len(self.history) > 6)
1198 self.assertEqual(A.data['status'], 'NEW')
1199 self.assertEqual(B.data['status'], 'MERGED')
1200 self.assertEqual(A.reported, 2)
1201 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001202
1203 def test_independent_queues(self):
1204 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001205
1206 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001207 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001208 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1209 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001210 A.addApproval('CRVW', 2)
1211 B.addApproval('CRVW', 2)
1212 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001213
1214 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1215 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1216 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1217
James E. Blairb02a3bb2012-07-30 17:49:55 -07001218 self.waitUntilSettled()
1219
1220 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001221 self.assertEqual(len(self.builds), 2)
1222 self.assertEqual(self.builds[0].name, 'project-merge')
1223 self.assertTrue(self.job_has_changes(self.builds[0], A))
1224 self.assertEqual(self.builds[1].name, 'project1-merge')
1225 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001226
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001227 # Release the current merge builds
1228 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001229 self.waitUntilSettled()
1230 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001231 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001232 self.waitUntilSettled()
1233
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001234 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001235 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001236 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001237
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001238 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001239 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001240 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001241
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001242 self.assertEqual(len(self.history), 11)
1243 self.assertEqual(A.data['status'], 'MERGED')
1244 self.assertEqual(B.data['status'], 'MERGED')
1245 self.assertEqual(C.data['status'], 'MERGED')
1246 self.assertEqual(A.reported, 2)
1247 self.assertEqual(B.reported, 2)
1248 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001249
1250 def test_failed_change_at_head(self):
1251 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001252
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001253 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001254 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1255 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1256 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001257 A.addApproval('CRVW', 2)
1258 B.addApproval('CRVW', 2)
1259 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001260
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001261 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001262
1263 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1264 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1265 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1266
1267 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001268
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001269 self.assertEqual(len(self.builds), 1)
1270 self.assertEqual(self.builds[0].name, 'project-merge')
1271 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001272
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001273 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001274 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001275 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001276 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001277 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001278 self.waitUntilSettled()
1279
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001280 self.assertEqual(len(self.builds), 6)
1281 self.assertEqual(self.builds[0].name, 'project-test1')
1282 self.assertEqual(self.builds[1].name, 'project-test2')
1283 self.assertEqual(self.builds[2].name, 'project-test1')
1284 self.assertEqual(self.builds[3].name, 'project-test2')
1285 self.assertEqual(self.builds[4].name, 'project-test1')
1286 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001287
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001288 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001289 self.waitUntilSettled()
1290
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001291 # project-test2, project-merge for B
1292 self.assertEqual(len(self.builds), 2)
1293 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001294
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001295 self.worker.hold_jobs_in_build = False
1296 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001297 self.waitUntilSettled()
1298
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001299 self.assertEqual(len(self.builds), 0)
1300 self.assertEqual(len(self.history), 15)
1301 self.assertEqual(A.data['status'], 'NEW')
1302 self.assertEqual(B.data['status'], 'MERGED')
1303 self.assertEqual(C.data['status'], 'MERGED')
1304 self.assertEqual(A.reported, 2)
1305 self.assertEqual(B.reported, 2)
1306 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001307
1308 def test_failed_change_at_head_with_queue(self):
1309 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001310
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001311 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001312 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1313 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1314 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001315 A.addApproval('CRVW', 2)
1316 B.addApproval('CRVW', 2)
1317 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001318
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001319 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001320
1321 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1322 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1323 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1324
1325 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001326 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001327 self.assertEqual(len(self.builds), 0)
1328 self.assertEqual(len(queue), 1)
1329 self.assertEqual(queue[0].name, 'build:project-merge')
1330 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001331
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001332 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001333 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001334 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001335 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001336 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001337 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001338 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001339
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001340 self.assertEqual(len(self.builds), 0)
1341 self.assertEqual(len(queue), 6)
1342 self.assertEqual(queue[0].name, 'build:project-test1')
1343 self.assertEqual(queue[1].name, 'build:project-test2')
1344 self.assertEqual(queue[2].name, 'build:project-test1')
1345 self.assertEqual(queue[3].name, 'build:project-test2')
1346 self.assertEqual(queue[4].name, 'build:project-test1')
1347 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001348
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001349 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001350 self.waitUntilSettled()
1351
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001352 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001353 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001354 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1355 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001356
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001357 self.gearman_server.hold_jobs_in_queue = False
1358 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001359 self.waitUntilSettled()
1360
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001361 self.assertEqual(len(self.builds), 0)
1362 self.assertEqual(len(self.history), 11)
1363 self.assertEqual(A.data['status'], 'NEW')
1364 self.assertEqual(B.data['status'], 'MERGED')
1365 self.assertEqual(C.data['status'], 'MERGED')
1366 self.assertEqual(A.reported, 2)
1367 self.assertEqual(B.reported, 2)
1368 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001369
1370 def test_patch_order(self):
1371 "Test that dependent patches are tested in the right order"
1372 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1373 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1374 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1375 A.addApproval('CRVW', 2)
1376 B.addApproval('CRVW', 2)
1377 C.addApproval('CRVW', 2)
1378
1379 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1380 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1381 M2.setMerged()
1382 M1.setMerged()
1383
1384 # C -> B -> A -> M1 -> M2
1385 # M2 is here to make sure it is never queried. If it is, it
1386 # means zuul is walking down the entire history of merged
1387 # changes.
1388
1389 C.setDependsOn(B, 1)
1390 B.setDependsOn(A, 1)
1391 A.setDependsOn(M1, 1)
1392 M1.setDependsOn(M2, 1)
1393
1394 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1395
1396 self.waitUntilSettled()
1397
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001398 self.assertEqual(A.data['status'], 'NEW')
1399 self.assertEqual(B.data['status'], 'NEW')
1400 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001401
1402 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1403 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1404
1405 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001406 self.assertEqual(M2.queried, 0)
1407 self.assertEqual(A.data['status'], 'MERGED')
1408 self.assertEqual(B.data['status'], 'MERGED')
1409 self.assertEqual(C.data['status'], 'MERGED')
1410 self.assertEqual(A.reported, 2)
1411 self.assertEqual(B.reported, 2)
1412 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001413
James E. Blair0e933c52013-07-11 10:18:52 -07001414 def test_trigger_cache(self):
1415 "Test that the trigger cache operates correctly"
1416 self.worker.hold_jobs_in_build = True
1417
1418 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1419 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1420 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1421 A.addApproval('CRVW', 2)
1422 B.addApproval('CRVW', 2)
1423
1424 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1425 M1.setMerged()
1426
1427 B.setDependsOn(A, 1)
1428 A.setDependsOn(M1, 1)
1429
1430 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1431 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1432
1433 self.waitUntilSettled()
1434
1435 for build in self.builds:
1436 if build.parameters['ZUUL_PIPELINE'] == 'check':
1437 build.release()
1438 self.waitUntilSettled()
1439 for build in self.builds:
1440 if build.parameters['ZUUL_PIPELINE'] == 'check':
1441 build.release()
1442 self.waitUntilSettled()
1443
1444 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1445 self.waitUntilSettled()
1446
James E. Blair6c358e72013-07-29 17:06:47 -07001447 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001448 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001449 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001450
1451 self.worker.hold_jobs_in_build = False
1452 self.worker.release()
1453 self.waitUntilSettled()
1454
1455 self.assertEqual(A.data['status'], 'MERGED')
1456 self.assertEqual(B.data['status'], 'MERGED')
1457 self.assertEqual(A.queried, 2) # Initial and isMerged
1458 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1459
James E. Blair8c803f82012-07-31 16:25:42 -07001460 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001461 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001462 # TODO: move to test_gerrit (this is a unit test!)
1463 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001464 trigger = self.sched.layout.pipelines['gate'].trigger
1465 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001466 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001467 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001468
1469 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001470 a = trigger.getChange(1, 2, refresh=True)
1471 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001472
1473 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001474 a = trigger.getChange(1, 2, refresh=True)
1475 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1476 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001477
1478 def test_build_configuration(self):
1479 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001480
1481 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001482 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1483 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1484 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1485 A.addApproval('CRVW', 2)
1486 B.addApproval('CRVW', 2)
1487 C.addApproval('CRVW', 2)
1488 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1489 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1490 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1491 self.waitUntilSettled()
1492
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001493 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001494 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001495 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001496 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001497 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001498 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001499 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001500 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1501 self.gearman_server.hold_jobs_in_queue = False
1502 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001503 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001504
Monty Taylorbc758832013-06-17 17:22:42 -04001505 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001506 repo = git.Repo(path)
1507 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1508 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001509 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001510 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001511
1512 def test_build_configuration_conflict(self):
1513 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001514
1515 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001516 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1517 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001518 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001519 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1520 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001521 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001522 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1523 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001524 A.addApproval('CRVW', 2)
1525 B.addApproval('CRVW', 2)
1526 C.addApproval('CRVW', 2)
1527 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1528 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1529 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1530 self.waitUntilSettled()
1531
James E. Blair6736beb2013-07-11 15:18:15 -07001532 self.assertEqual(A.reported, 1)
1533 self.assertEqual(B.reported, 1)
1534 self.assertEqual(C.reported, 1)
1535
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001536 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001537 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001538 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001539 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001540 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001541 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001542 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001543 self.getParameter(queue[-1], 'ZUUL_REF')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001544 self.gearman_server.hold_jobs_in_queue = False
1545 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001546 self.waitUntilSettled()
1547
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001548 self.assertEqual(A.data['status'], 'MERGED')
1549 self.assertEqual(B.data['status'], 'NEW')
1550 self.assertEqual(C.data['status'], 'MERGED')
1551 self.assertEqual(A.reported, 2)
1552 self.assertEqual(B.reported, 2)
1553 self.assertEqual(C.reported, 2)
James E. Blairdaabed22012-08-15 15:38:57 -07001554
James E. Blair6736beb2013-07-11 15:18:15 -07001555 def test_dequeue_conflict(self):
1556 "Test that the option to dequeue merge conflicts works"
1557
1558 self.gearman_server.hold_jobs_in_queue = True
1559 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1560 A.addPatchset(['conflict'])
1561 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1562 B.addPatchset(['conflict'])
1563 A.addApproval('CRVW', 2)
1564 B.addApproval('CRVW', 2)
1565 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1566 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1567 self.waitUntilSettled()
1568
1569 self.assertEqual(A.reported, 1)
1570 self.assertEqual(B.reported, 2)
1571
1572 self.gearman_server.hold_jobs_in_queue = False
1573 self.gearman_server.release()
1574 self.waitUntilSettled()
1575
1576 self.assertEqual(A.data['status'], 'MERGED')
1577 self.assertEqual(B.data['status'], 'NEW')
1578 self.assertEqual(A.reported, 2)
1579 self.assertEqual(B.reported, 2)
1580
James E. Blairdaabed22012-08-15 15:38:57 -07001581 def test_post(self):
1582 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001583
Zhongyue Luo5d556072012-09-21 02:00:47 +09001584 e = {
1585 "type": "ref-updated",
1586 "submitter": {
1587 "name": "User Name",
1588 },
1589 "refUpdate": {
1590 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1591 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1592 "refName": "master",
1593 "project": "org/project",
1594 }
1595 }
James E. Blairdaabed22012-08-15 15:38:57 -07001596 self.fake_gerrit.addEvent(e)
1597 self.waitUntilSettled()
1598
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001599 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001600 self.assertEqual(len(self.history), 1)
1601 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001602
1603 def test_build_configuration_branch(self):
1604 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001605
1606 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001607 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1608 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1609 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1610 A.addApproval('CRVW', 2)
1611 B.addApproval('CRVW', 2)
1612 C.addApproval('CRVW', 2)
1613 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1614 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1615 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1616 self.waitUntilSettled()
1617
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001618 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001619 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001620 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001621 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001622 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001623 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001624 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001625 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1626 self.gearman_server.hold_jobs_in_queue = False
1627 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001628 self.waitUntilSettled()
1629
Monty Taylorbc758832013-06-17 17:22:42 -04001630 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001631 repo = git.Repo(path)
1632 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1633 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001634 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001635 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001636
1637 def test_build_configuration_branch_interaction(self):
1638 "Test that switching between branches works"
1639 self.test_build_configuration()
1640 self.test_build_configuration_branch()
1641 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001642 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001643 repo = git.Repo(path)
1644 repo.heads.master.commit = repo.commit('init')
1645 self.test_build_configuration()
1646
1647 def test_build_configuration_multi_branch(self):
1648 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001649
1650 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001651 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1652 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1653 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1654 A.addApproval('CRVW', 2)
1655 B.addApproval('CRVW', 2)
1656 C.addApproval('CRVW', 2)
1657 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1658 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1659 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1660 self.waitUntilSettled()
1661
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001662 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001663 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001664 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001665 job_B = None
1666 for job in queue:
1667 if 'project-merge' in job.name:
1668 job_B = job
1669 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001670 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001671 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001672 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001673 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001674 for job in queue:
1675 if 'project-merge' in job.name:
1676 job_C = job
1677 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001678 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001679 self.gearman_server.hold_jobs_in_queue = False
1680 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001681 self.waitUntilSettled()
1682
Monty Taylorbc758832013-06-17 17:22:42 -04001683 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001684 repo = git.Repo(path)
1685
1686 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001687 for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001688 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001689 correct_messages = ['initial commit', 'A-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001690 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001691
1692 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001693 for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001694 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001695 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001696 self.assertEqual(repo_messages, correct_messages)
James E. Blair7f71c802012-08-22 13:04:32 -07001697
1698 def test_one_job_project(self):
1699 "Test that queueing works with one job"
1700 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1701 'master', 'A')
1702 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1703 'master', 'B')
1704 A.addApproval('CRVW', 2)
1705 B.addApproval('CRVW', 2)
1706 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1707 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1708 self.waitUntilSettled()
1709
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001710 self.assertEqual(A.data['status'], 'MERGED')
1711 self.assertEqual(A.reported, 2)
1712 self.assertEqual(B.data['status'], 'MERGED')
1713 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001714
Antoine Musso80edd5a2013-02-13 15:37:53 +01001715 def test_job_from_templates_launched(self):
1716 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001717
Antoine Musso80edd5a2013-02-13 15:37:53 +01001718 A = self.fake_gerrit.addFakeChange(
1719 'org/templated-project', 'master', 'A')
1720 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1721 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001722
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001723 self.assertEqual(self.getJobFromHistory('project-test1').result,
1724 'SUCCESS')
1725 self.assertEqual(self.getJobFromHistory('project-test2').result,
1726 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001727
James E. Blaircaec0c52012-08-22 14:52:22 -07001728 def test_dependent_changes_dequeue(self):
1729 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001730
James E. Blaircaec0c52012-08-22 14:52:22 -07001731 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1732 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1733 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1734 A.addApproval('CRVW', 2)
1735 B.addApproval('CRVW', 2)
1736 C.addApproval('CRVW', 2)
1737
1738 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1739 M1.setMerged()
1740
1741 # C -> B -> A -> M1
1742
1743 C.setDependsOn(B, 1)
1744 B.setDependsOn(A, 1)
1745 A.setDependsOn(M1, 1)
1746
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001747 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001748
1749 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1750 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1751 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1752
1753 self.waitUntilSettled()
1754
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001755 self.assertEqual(A.data['status'], 'NEW')
1756 self.assertEqual(A.reported, 2)
1757 self.assertEqual(B.data['status'], 'NEW')
1758 self.assertEqual(B.reported, 2)
1759 self.assertEqual(C.data['status'], 'NEW')
1760 self.assertEqual(C.reported, 2)
1761 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001762
1763 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001764 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001765 # If it's dequeued more than once, we should see extra
1766 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001767
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001768 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001769 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1770 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1771 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1772 A.addApproval('CRVW', 2)
1773 B.addApproval('CRVW', 2)
1774 C.addApproval('CRVW', 2)
1775
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001776 self.worker.addFailTest('project1-test1', A)
1777 self.worker.addFailTest('project1-test2', A)
1778 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001779
1780 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1781 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1782 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1783
1784 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001785
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001786 self.assertEqual(len(self.builds), 1)
1787 self.assertEqual(self.builds[0].name, 'project1-merge')
1788 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07001789
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001790 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001791 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001792 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001793 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001794 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001795 self.waitUntilSettled()
1796
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001797 self.assertEqual(len(self.builds), 9)
1798 self.assertEqual(self.builds[0].name, 'project1-test1')
1799 self.assertEqual(self.builds[1].name, 'project1-test2')
1800 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
1801 self.assertEqual(self.builds[3].name, 'project1-test1')
1802 self.assertEqual(self.builds[4].name, 'project1-test2')
1803 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
1804 self.assertEqual(self.builds[6].name, 'project1-test1')
1805 self.assertEqual(self.builds[7].name, 'project1-test2')
1806 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001807
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001808 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001809 self.waitUntilSettled()
1810
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001811 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1812 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001813
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001814 self.worker.hold_jobs_in_build = False
1815 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001816 self.waitUntilSettled()
1817
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001818 self.assertEqual(len(self.builds), 0)
1819 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001820
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001821 self.assertEqual(A.data['status'], 'NEW')
1822 self.assertEqual(B.data['status'], 'MERGED')
1823 self.assertEqual(C.data['status'], 'MERGED')
1824 self.assertEqual(A.reported, 2)
1825 self.assertEqual(B.reported, 2)
1826 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001827
1828 def test_nonvoting_job(self):
1829 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001830
James E. Blair4ec821f2012-08-23 15:28:28 -07001831 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1832 'master', 'A')
1833 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001834 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001835 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1836
1837 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001838
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001839 self.assertEqual(A.data['status'], 'MERGED')
1840 self.assertEqual(A.reported, 2)
1841 self.assertEqual(
1842 self.getJobFromHistory('nonvoting-project-merge').result,
1843 'SUCCESS')
1844 self.assertEqual(
1845 self.getJobFromHistory('nonvoting-project-test1').result,
1846 'SUCCESS')
1847 self.assertEqual(
1848 self.getJobFromHistory('nonvoting-project-test2').result,
1849 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001850
1851 def test_check_queue_success(self):
1852 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001853
James E. Blaire0487072012-08-29 17:38:31 -07001854 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1855 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1856
1857 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001858
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001859 self.assertEqual(A.data['status'], 'NEW')
1860 self.assertEqual(A.reported, 1)
1861 self.assertEqual(self.getJobFromHistory('project-merge').result,
1862 'SUCCESS')
1863 self.assertEqual(self.getJobFromHistory('project-test1').result,
1864 'SUCCESS')
1865 self.assertEqual(self.getJobFromHistory('project-test2').result,
1866 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001867
1868 def test_check_queue_failure(self):
1869 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001870
James E. Blaire0487072012-08-29 17:38:31 -07001871 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001872 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001873 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1874
1875 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001876
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001877 self.assertEqual(A.data['status'], 'NEW')
1878 self.assertEqual(A.reported, 1)
1879 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001880 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001881 self.assertEqual(self.getJobFromHistory('project-test1').result,
1882 'SUCCESS')
1883 self.assertEqual(self.getJobFromHistory('project-test2').result,
1884 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001885
1886 def test_dependent_behind_dequeue(self):
1887 "test that dependent changes behind dequeued changes work"
1888 # This complicated test is a reproduction of a real life bug
1889 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001890
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001891 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001892 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1893 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1894 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1895 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1896 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1897 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1898 D.setDependsOn(C, 1)
1899 E.setDependsOn(D, 1)
1900 A.addApproval('CRVW', 2)
1901 B.addApproval('CRVW', 2)
1902 C.addApproval('CRVW', 2)
1903 D.addApproval('CRVW', 2)
1904 E.addApproval('CRVW', 2)
1905 F.addApproval('CRVW', 2)
1906
1907 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001908
1909 # Change object re-use in the gerrit trigger is hidden if
1910 # changes are added in quick succession; waiting makes it more
1911 # like real life.
1912 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1913 self.waitUntilSettled()
1914 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1915 self.waitUntilSettled()
1916
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001917 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001918 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001919 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001920 self.waitUntilSettled()
1921
1922 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1923 self.waitUntilSettled()
1924 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1925 self.waitUntilSettled()
1926 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1927 self.waitUntilSettled()
1928 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1929 self.waitUntilSettled()
1930
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001931 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001932 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001933 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001934 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001935 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001936 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001937 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001938 self.waitUntilSettled()
1939
1940 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001941
1942 # Grab pointers to the jobs we want to release before
1943 # releasing any, because list indexes may change as
1944 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001945 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001946 a.release()
1947 b.release()
1948 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001949 self.waitUntilSettled()
1950
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001951 self.worker.hold_jobs_in_build = False
1952 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07001953 self.waitUntilSettled()
1954
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001955 self.assertEqual(A.data['status'], 'NEW')
1956 self.assertEqual(B.data['status'], 'MERGED')
1957 self.assertEqual(C.data['status'], 'MERGED')
1958 self.assertEqual(D.data['status'], 'MERGED')
1959 self.assertEqual(E.data['status'], 'MERGED')
1960 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001961
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001962 self.assertEqual(A.reported, 2)
1963 self.assertEqual(B.reported, 2)
1964 self.assertEqual(C.reported, 2)
1965 self.assertEqual(D.reported, 2)
1966 self.assertEqual(E.reported, 2)
1967 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001968
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001969 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1970 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001971
1972 def test_merger_repack(self):
1973 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001974
James E. Blair05fed602012-09-07 12:45:24 -07001975 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1976 A.addApproval('CRVW', 2)
1977 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1978 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001979 self.assertEqual(self.getJobFromHistory('project-merge').result,
1980 'SUCCESS')
1981 self.assertEqual(self.getJobFromHistory('project-test1').result,
1982 'SUCCESS')
1983 self.assertEqual(self.getJobFromHistory('project-test2').result,
1984 'SUCCESS')
1985 self.assertEqual(A.data['status'], 'MERGED')
1986 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001987 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07001988 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001989
Monty Taylorbc758832013-06-17 17:22:42 -04001990 path = os.path.join(self.git_root, "org/project")
1991 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001992
1993 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1994 A.addApproval('CRVW', 2)
1995 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1996 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001997 self.assertEqual(self.getJobFromHistory('project-merge').result,
1998 'SUCCESS')
1999 self.assertEqual(self.getJobFromHistory('project-test1').result,
2000 'SUCCESS')
2001 self.assertEqual(self.getJobFromHistory('project-test2').result,
2002 'SUCCESS')
2003 self.assertEqual(A.data['status'], 'MERGED')
2004 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002005
James E. Blair4886f282012-11-15 09:27:33 -08002006 def test_merger_repack_large_change(self):
2007 "Test that the merger works with large changes after a repack"
2008 # https://bugs.launchpad.net/zuul/+bug/1078946
2009 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2010 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002011 path = os.path.join(self.upstream_root, "org/project1")
2012 print repack_repo(path)
2013 path = os.path.join(self.git_root, "org/project1")
2014 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002015
2016 A.addApproval('CRVW', 2)
2017 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2018 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002019 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2020 'SUCCESS')
2021 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2022 'SUCCESS')
2023 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2024 'SUCCESS')
2025 self.assertEqual(A.data['status'], 'MERGED')
2026 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002027
James E. Blair7ee88a22012-09-12 18:59:31 +02002028 def test_nonexistent_job(self):
2029 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002030 # Set to the state immediately after a restart
2031 self.resetGearmanServer()
2032 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002033
2034 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2035 A.addApproval('CRVW', 2)
2036 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2037 # There may be a thread about to report a lost change
2038 while A.reported < 2:
2039 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002040 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002041 self.assertFalse(job_names)
2042 self.assertEqual(A.data['status'], 'NEW')
2043 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002044 self.assertEmptyQueues()
2045
2046 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002047 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002048 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2049 A.addApproval('CRVW', 2)
2050 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2051 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002052 self.assertEqual(self.getJobFromHistory('project-merge').result,
2053 'SUCCESS')
2054 self.assertEqual(self.getJobFromHistory('project-test1').result,
2055 'SUCCESS')
2056 self.assertEqual(self.getJobFromHistory('project-test2').result,
2057 'SUCCESS')
2058 self.assertEqual(A.data['status'], 'MERGED')
2059 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002060
2061 def test_single_nonexistent_post_job(self):
2062 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002063 e = {
2064 "type": "ref-updated",
2065 "submitter": {
2066 "name": "User Name",
2067 },
2068 "refUpdate": {
2069 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2070 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2071 "refName": "master",
2072 "project": "org/project",
2073 }
2074 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002075 # Set to the state immediately after a restart
2076 self.resetGearmanServer()
2077 self.launcher.negative_function_cache_ttl = 0
2078
James E. Blairf62d4282012-12-31 17:01:50 -08002079 self.fake_gerrit.addEvent(e)
2080 self.waitUntilSettled()
2081
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002082 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002083
2084 def test_new_patchset_dequeues_old(self):
2085 "Test that a new patchset causes the old to be dequeued"
2086 # D -> C (depends on B) -> B (depends on A) -> A -> M
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 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2089 M.setMerged()
2090
2091 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2092 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2093 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2094 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2095 A.addApproval('CRVW', 2)
2096 B.addApproval('CRVW', 2)
2097 C.addApproval('CRVW', 2)
2098 D.addApproval('CRVW', 2)
2099
2100 C.setDependsOn(B, 1)
2101 B.setDependsOn(A, 1)
2102 A.setDependsOn(M, 1)
2103
2104 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2105 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2106 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2107 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2108 self.waitUntilSettled()
2109
2110 B.addPatchset()
2111 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2112 self.waitUntilSettled()
2113
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002114 self.worker.hold_jobs_in_build = False
2115 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002116 self.waitUntilSettled()
2117
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002118 self.assertEqual(A.data['status'], 'MERGED')
2119 self.assertEqual(A.reported, 2)
2120 self.assertEqual(B.data['status'], 'NEW')
2121 self.assertEqual(B.reported, 2)
2122 self.assertEqual(C.data['status'], 'NEW')
2123 self.assertEqual(C.reported, 2)
2124 self.assertEqual(D.data['status'], 'MERGED')
2125 self.assertEqual(D.reported, 2)
2126 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002127
2128 def test_new_patchset_dequeues_old_on_head(self):
2129 "Test that a new patchset causes the old to be dequeued (at head)"
2130 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002131 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002132 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2133 M.setMerged()
2134 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2135 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2136 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2137 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2138 A.addApproval('CRVW', 2)
2139 B.addApproval('CRVW', 2)
2140 C.addApproval('CRVW', 2)
2141 D.addApproval('CRVW', 2)
2142
2143 C.setDependsOn(B, 1)
2144 B.setDependsOn(A, 1)
2145 A.setDependsOn(M, 1)
2146
2147 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2148 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2149 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2150 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2151 self.waitUntilSettled()
2152
2153 A.addPatchset()
2154 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2155 self.waitUntilSettled()
2156
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002157 self.worker.hold_jobs_in_build = False
2158 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002159 self.waitUntilSettled()
2160
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002161 self.assertEqual(A.data['status'], 'NEW')
2162 self.assertEqual(A.reported, 2)
2163 self.assertEqual(B.data['status'], 'NEW')
2164 self.assertEqual(B.reported, 2)
2165 self.assertEqual(C.data['status'], 'NEW')
2166 self.assertEqual(C.reported, 2)
2167 self.assertEqual(D.data['status'], 'MERGED')
2168 self.assertEqual(D.reported, 2)
2169 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002170
2171 def test_new_patchset_dequeues_old_without_dependents(self):
2172 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002173 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002174 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2175 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2176 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2177 A.addApproval('CRVW', 2)
2178 B.addApproval('CRVW', 2)
2179 C.addApproval('CRVW', 2)
2180
2181 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2182 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2183 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2184 self.waitUntilSettled()
2185
2186 B.addPatchset()
2187 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2188 self.waitUntilSettled()
2189
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002190 self.worker.hold_jobs_in_build = False
2191 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002192 self.waitUntilSettled()
2193
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002194 self.assertEqual(A.data['status'], 'MERGED')
2195 self.assertEqual(A.reported, 2)
2196 self.assertEqual(B.data['status'], 'NEW')
2197 self.assertEqual(B.reported, 2)
2198 self.assertEqual(C.data['status'], 'MERGED')
2199 self.assertEqual(C.reported, 2)
2200 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002201
2202 def test_new_patchset_dequeues_old_independent_queue(self):
2203 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002204 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002205 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2206 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2207 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2208 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2209 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2210 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2211 self.waitUntilSettled()
2212
2213 B.addPatchset()
2214 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2215 self.waitUntilSettled()
2216
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002217 self.worker.hold_jobs_in_build = False
2218 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002219 self.waitUntilSettled()
2220
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002221 self.assertEqual(A.data['status'], 'NEW')
2222 self.assertEqual(A.reported, 1)
2223 self.assertEqual(B.data['status'], 'NEW')
2224 self.assertEqual(B.reported, 1)
2225 self.assertEqual(C.data['status'], 'NEW')
2226 self.assertEqual(C.reported, 1)
2227 self.assertEqual(len(self.history), 10)
2228 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002229
2230 def test_zuul_refs(self):
2231 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002232 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002233 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2234 M1.setMerged()
2235 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2236 M2.setMerged()
2237
2238 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2239 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2240 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2241 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2242 A.addApproval('CRVW', 2)
2243 B.addApproval('CRVW', 2)
2244 C.addApproval('CRVW', 2)
2245 D.addApproval('CRVW', 2)
2246 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2247 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2248 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2249 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2250
2251 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002252 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002253 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002254 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002255 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002256 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002257 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002258 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002259 self.waitUntilSettled()
2260
James E. Blair7d0dedc2013-02-21 17:26:09 -08002261 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002262 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002263 if x.parameters['ZUUL_CHANGE'] == '3':
2264 a_zref = x.parameters['ZUUL_REF']
2265 if x.parameters['ZUUL_CHANGE'] == '4':
2266 b_zref = x.parameters['ZUUL_REF']
2267 if x.parameters['ZUUL_CHANGE'] == '5':
2268 c_zref = x.parameters['ZUUL_REF']
2269 if x.parameters['ZUUL_CHANGE'] == '6':
2270 d_zref = x.parameters['ZUUL_REF']
2271
2272 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002273 self.assertIsNotNone(a_zref)
2274 self.assertIsNotNone(b_zref)
2275 self.assertIsNotNone(c_zref)
2276 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002277
2278 # And they should all be different
2279 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002280 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002281
2282 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002283 self.assertTrue(self.ref_has_change(a_zref, A))
2284 self.assertFalse(self.ref_has_change(a_zref, B))
2285 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002286
2287 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002288 self.assertTrue(self.ref_has_change(b_zref, A))
2289 self.assertTrue(self.ref_has_change(b_zref, B))
2290 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002291
2292 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002293 self.assertTrue(self.ref_has_change(c_zref, A))
2294 self.assertTrue(self.ref_has_change(c_zref, B))
2295 self.assertTrue(self.ref_has_change(c_zref, C))
2296 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002297
2298 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002299 self.assertTrue(self.ref_has_change(d_zref, A))
2300 self.assertTrue(self.ref_has_change(d_zref, B))
2301 self.assertTrue(self.ref_has_change(d_zref, C))
2302 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002303
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002304 self.worker.hold_jobs_in_build = False
2305 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002306 self.waitUntilSettled()
2307
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002308 self.assertEqual(A.data['status'], 'MERGED')
2309 self.assertEqual(A.reported, 2)
2310 self.assertEqual(B.data['status'], 'MERGED')
2311 self.assertEqual(B.reported, 2)
2312 self.assertEqual(C.data['status'], 'MERGED')
2313 self.assertEqual(C.reported, 2)
2314 self.assertEqual(D.data['status'], 'MERGED')
2315 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002316
James E. Blair412e5582013-04-22 15:50:12 -07002317 def test_statsd(self):
2318 "Test each of the statsd methods used in the scheduler"
2319 import extras
2320 statsd = extras.try_import('statsd.statsd')
2321 statsd.incr('test-incr')
2322 statsd.timing('test-timing', 3)
2323 statsd.gauge('test-guage', 12)
2324 self.assertReportedStat('test-incr', '1|c')
2325 self.assertReportedStat('test-timing', '3|ms')
2326 self.assertReportedStat('test-guage', '12|g')
2327
James E. Blair70c71582013-03-06 08:50:50 -08002328 def test_file_jobs(self):
2329 "Test that file jobs run only when appropriate"
2330 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2331 A.addPatchset(['pip-requires'])
2332 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2333 A.addApproval('CRVW', 2)
2334 B.addApproval('CRVW', 2)
2335 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2336 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2337 self.waitUntilSettled()
2338
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002339 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002340 if x.name == 'project-testfile']
2341
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002342 self.assertEqual(len(testfile_jobs), 1)
2343 self.assertEqual(testfile_jobs[0].changes, '1,2')
2344 self.assertEqual(A.data['status'], 'MERGED')
2345 self.assertEqual(A.reported, 2)
2346 self.assertEqual(B.data['status'], 'MERGED')
2347 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002348
2349 def test_test_config(self):
2350 "Test that we can test the config"
2351 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002352 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002353 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002354 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002355
2356 def test_build_description(self):
2357 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002358 self.worker.registerFunction('set_description:' +
2359 self.worker.worker_id)
2360
2361 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2362 A.addApproval('CRVW', 2)
2363 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2364 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002365 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002366 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002367 self.assertTrue(re.search("Branch.*master", desc))
2368 self.assertTrue(re.search("Pipeline.*gate", desc))
2369 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2370 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2371 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2372 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002373
James E. Blair64ed6f22013-07-10 14:07:23 -07002374 def test_queue_precedence(self):
2375 "Test that queue precedence works"
2376
2377 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002378 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002379 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2380 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2381 A.addApproval('CRVW', 2)
2382 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2383
2384 self.waitUntilSettled()
2385 self.gearman_server.hold_jobs_in_queue = False
2386 self.gearman_server.release()
2387 self.waitUntilSettled()
2388
James E. Blair8de58bd2013-07-18 16:23:33 -07002389 # Run one build at a time to ensure non-race order:
2390 for x in range(6):
2391 self.release(self.builds[0])
2392 self.waitUntilSettled()
2393 self.worker.hold_jobs_in_build = False
2394 self.waitUntilSettled()
2395
James E. Blair64ed6f22013-07-10 14:07:23 -07002396 self.log.debug(self.history)
2397 self.assertEqual(self.history[0].pipeline, 'gate')
2398 self.assertEqual(self.history[1].pipeline, 'check')
2399 self.assertEqual(self.history[2].pipeline, 'gate')
2400 self.assertEqual(self.history[3].pipeline, 'gate')
2401 self.assertEqual(self.history[4].pipeline, 'check')
2402 self.assertEqual(self.history[5].pipeline, 'check')
2403
James E. Blair1843a552013-07-03 14:19:52 -07002404 def test_json_status(self):
2405 "Test that we can retrieve JSON status info"
2406 self.worker.hold_jobs_in_build = True
2407 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2408 A.addApproval('CRVW', 2)
2409 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2410 self.waitUntilSettled()
2411
2412 port = self.webapp.server.socket.getsockname()[1]
2413
2414 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2415 data = f.read()
2416
2417 self.worker.hold_jobs_in_build = False
2418 self.worker.release()
2419 self.waitUntilSettled()
2420
2421 data = json.loads(data)
2422 status_jobs = set()
2423 for p in data['pipelines']:
2424 for q in p['change_queues']:
2425 for head in q['heads']:
2426 for change in head:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002427 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002428 for job in change['jobs']:
2429 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002430 self.assertIn('project-merge', status_jobs)
2431 self.assertIn('project-test1', status_jobs)
2432 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002433
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002434 def test_node_label(self):
2435 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002436 self.worker.registerFunction('build:node-project-test1:debian')
2437
2438 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2439 A.addApproval('CRVW', 2)
2440 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2441 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002442
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002443 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2444 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2445 'debian')
2446 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002447
2448 def test_live_reconfiguration(self):
2449 "Test that live reconfiguration works"
2450 self.worker.hold_jobs_in_build = True
2451 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2452 A.addApproval('CRVW', 2)
2453 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2454 self.waitUntilSettled()
2455
2456 self.sched.reconfigure(self.config)
2457
2458 self.worker.hold_jobs_in_build = False
2459 self.worker.release()
2460 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002461 self.assertEqual(self.getJobFromHistory('project-merge').result,
2462 'SUCCESS')
2463 self.assertEqual(self.getJobFromHistory('project-test1').result,
2464 'SUCCESS')
2465 self.assertEqual(self.getJobFromHistory('project-test2').result,
2466 'SUCCESS')
2467 self.assertEqual(A.data['status'], 'MERGED')
2468 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002469
James E. Blaire712d9f2013-07-31 11:40:11 -07002470 def test_live_reconfiguration_functions(self):
2471 "Test live reconfiguration with a custom function"
2472 self.worker.registerFunction('build:node-project-test1:debian')
2473 self.worker.registerFunction('build:node-project-test1:wheezy')
2474 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2475 A.addApproval('CRVW', 2)
2476 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2477 self.waitUntilSettled()
2478
2479 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2480 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2481 'debian')
2482 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2483
2484 self.config.set('zuul', 'layout_config',
2485 'tests/fixtures/layout-live-'
2486 'reconfiguration-functions.yaml')
2487 self.sched.reconfigure(self.config)
2488 self.worker.build_history = []
2489
2490 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
2491 B.addApproval('CRVW', 2)
2492 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2493 self.waitUntilSettled()
2494
2495 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2496 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2497 'wheezy')
2498 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2499
James E. Blair287c06d2013-07-24 10:39:30 -07002500 def test_delayed_repo_init(self):
2501 self.config.set('zuul', 'layout_config',
2502 'tests/fixtures/layout-delayed-repo-init.yaml')
2503 self.sched.reconfigure(self.config)
2504
2505 self.init_repo("org/new-project")
2506 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2507
2508 A.addApproval('CRVW', 2)
2509 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2510 self.waitUntilSettled()
2511 self.assertEqual(self.getJobFromHistory('project-merge').result,
2512 'SUCCESS')
2513 self.assertEqual(self.getJobFromHistory('project-test1').result,
2514 'SUCCESS')
2515 self.assertEqual(self.getJobFromHistory('project-test2').result,
2516 'SUCCESS')
2517 self.assertEqual(A.data['status'], 'MERGED')
2518 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002519
2520 def test_timer(self):
2521 "Test that a periodic job is triggered"
2522 self.worker.hold_jobs_in_build = True
2523 self.config.set('zuul', 'layout_config',
2524 'tests/fixtures/layout-timer.yaml')
2525 self.sched.reconfigure(self.config)
2526 self.registerJobs()
2527
2528 start = time.time()
2529 failed = True
2530 while ((time.time() - start) < 30):
2531 if len(self.builds) == 2:
2532 failed = False
2533 break
2534 else:
2535 time.sleep(1)
2536
2537 if failed:
2538 raise Exception("Expected jobs never ran")
2539
2540 self.waitUntilSettled()
2541 port = self.webapp.server.socket.getsockname()[1]
2542
2543 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2544 data = f.read()
2545
2546 self.worker.hold_jobs_in_build = False
2547 self.worker.release()
2548 self.waitUntilSettled()
2549
2550 self.assertEqual(self.getJobFromHistory(
2551 'project-bitrot-stable-old').result, 'SUCCESS')
2552 self.assertEqual(self.getJobFromHistory(
2553 'project-bitrot-stable-older').result, 'SUCCESS')
2554
2555 data = json.loads(data)
2556 status_jobs = set()
2557 for p in data['pipelines']:
2558 for q in p['change_queues']:
2559 for head in q['heads']:
2560 for change in head:
2561 self.assertEqual(change['id'], 'None')
2562 for job in change['jobs']:
2563 status_jobs.add(job['name'])
2564 self.assertIn('project-bitrot-stable-old', status_jobs)
2565 self.assertIn('project-bitrot-stable-older', status_jobs)