blob: 7cb93ee1ec848cb1bc9ee2f397fdf5607d82bf6a [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. Blair3cb10702013-08-24 08:56:03 -07001073 def test_initial_pipeline_gauges(self):
1074 "Test that each pipeline reported its length on start"
1075 pipeline_names = self.sched.layout.pipelines.keys()
1076 self.assertNotEqual(len(pipeline_names), 0)
1077 for name in pipeline_names:
1078 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
1079 value='0|g')
1080
James E. Blair42f74822013-05-14 15:18:03 -07001081 def test_duplicate_pipelines(self):
1082 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001083
James E. Blair42f74822013-05-14 15:18:03 -07001084 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1085 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1086 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001087
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001088 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001089 self.history[0].name == 'project-test1'
1090 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001091
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001092 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001093 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001094 self.assertIn('dup1/project-test1', A.messages[0])
1095 self.assertNotIn('dup2/project-test1', A.messages[0])
1096 self.assertNotIn('dup1/project-test1', A.messages[1])
1097 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001098 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001099 self.assertIn('dup1/project-test1', A.messages[1])
1100 self.assertNotIn('dup2/project-test1', A.messages[1])
1101 self.assertNotIn('dup1/project-test1', A.messages[0])
1102 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001103
James E. Blairb0fcae42012-07-17 11:12:10 -07001104 def test_parallel_changes(self):
1105 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001106
1107 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001108 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1109 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1110 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001111 A.addApproval('CRVW', 2)
1112 B.addApproval('CRVW', 2)
1113 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001114
1115 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1116 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1117 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1118
1119 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001120 self.assertEqual(len(self.builds), 1)
1121 self.assertEqual(self.builds[0].name, 'project-merge')
1122 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001123
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001124 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001125 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001126 self.assertEqual(len(self.builds), 3)
1127 self.assertEqual(self.builds[0].name, 'project-test1')
1128 self.assertTrue(self.job_has_changes(self.builds[0], A))
1129 self.assertEqual(self.builds[1].name, 'project-test2')
1130 self.assertTrue(self.job_has_changes(self.builds[1], A))
1131 self.assertEqual(self.builds[2].name, 'project-merge')
1132 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001133
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001134 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001135 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001136 self.assertEqual(len(self.builds), 5)
1137 self.assertEqual(self.builds[0].name, 'project-test1')
1138 self.assertTrue(self.job_has_changes(self.builds[0], A))
1139 self.assertEqual(self.builds[1].name, 'project-test2')
1140 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001141
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001142 self.assertEqual(self.builds[2].name, 'project-test1')
1143 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1144 self.assertEqual(self.builds[3].name, 'project-test2')
1145 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001146
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001147 self.assertEqual(self.builds[4].name, 'project-merge')
1148 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001149
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001150 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001151 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001152 self.assertEqual(len(self.builds), 6)
1153 self.assertEqual(self.builds[0].name, 'project-test1')
1154 self.assertTrue(self.job_has_changes(self.builds[0], A))
1155 self.assertEqual(self.builds[1].name, 'project-test2')
1156 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001157
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001158 self.assertEqual(self.builds[2].name, 'project-test1')
1159 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1160 self.assertEqual(self.builds[3].name, 'project-test2')
1161 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001162
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001163 self.assertEqual(self.builds[4].name, 'project-test1')
1164 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1165 self.assertEqual(self.builds[5].name, 'project-test2')
1166 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001167
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001168 self.worker.hold_jobs_in_build = False
1169 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001170 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001171 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001172
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001173 self.assertEqual(len(self.history), 9)
1174 self.assertEqual(A.data['status'], 'MERGED')
1175 self.assertEqual(B.data['status'], 'MERGED')
1176 self.assertEqual(C.data['status'], 'MERGED')
1177 self.assertEqual(A.reported, 2)
1178 self.assertEqual(B.reported, 2)
1179 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001180
1181 def test_failed_changes(self):
1182 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001183 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001184
James E. Blairb02a3bb2012-07-30 17:49:55 -07001185 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1186 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001187 A.addApproval('CRVW', 2)
1188 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001189
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001190 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001191
James E. Blaire2819012013-06-28 17:17:26 -04001192 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1193 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001194 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001195
1196 self.worker.release('.*-merge')
1197 self.waitUntilSettled()
1198
1199 self.worker.hold_jobs_in_build = False
1200 self.worker.release()
1201
1202 self.waitUntilSettled()
1203 # It's certain that the merge job for change 2 will run, but
1204 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001205 self.assertTrue(len(self.history) > 6)
1206 self.assertEqual(A.data['status'], 'NEW')
1207 self.assertEqual(B.data['status'], 'MERGED')
1208 self.assertEqual(A.reported, 2)
1209 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001210
1211 def test_independent_queues(self):
1212 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001213
1214 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001215 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001216 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1217 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001218 A.addApproval('CRVW', 2)
1219 B.addApproval('CRVW', 2)
1220 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001221
1222 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1223 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1224 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1225
James E. Blairb02a3bb2012-07-30 17:49:55 -07001226 self.waitUntilSettled()
1227
1228 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001229 self.assertEqual(len(self.builds), 2)
1230 self.assertEqual(self.builds[0].name, 'project-merge')
1231 self.assertTrue(self.job_has_changes(self.builds[0], A))
1232 self.assertEqual(self.builds[1].name, 'project1-merge')
1233 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001234
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001235 # Release the current merge builds
1236 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001237 self.waitUntilSettled()
1238 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001239 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001240 self.waitUntilSettled()
1241
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001242 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001243 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001244 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001245
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001246 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001247 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001248 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001249
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001250 self.assertEqual(len(self.history), 11)
1251 self.assertEqual(A.data['status'], 'MERGED')
1252 self.assertEqual(B.data['status'], 'MERGED')
1253 self.assertEqual(C.data['status'], 'MERGED')
1254 self.assertEqual(A.reported, 2)
1255 self.assertEqual(B.reported, 2)
1256 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001257
1258 def test_failed_change_at_head(self):
1259 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001260
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001261 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001262 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1263 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1264 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001265 A.addApproval('CRVW', 2)
1266 B.addApproval('CRVW', 2)
1267 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001268
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001269 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001270
1271 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1272 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1273 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1274
1275 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001276
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001277 self.assertEqual(len(self.builds), 1)
1278 self.assertEqual(self.builds[0].name, 'project-merge')
1279 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001280
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001281 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001282 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001283 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001284 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001285 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001286 self.waitUntilSettled()
1287
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001288 self.assertEqual(len(self.builds), 6)
1289 self.assertEqual(self.builds[0].name, 'project-test1')
1290 self.assertEqual(self.builds[1].name, 'project-test2')
1291 self.assertEqual(self.builds[2].name, 'project-test1')
1292 self.assertEqual(self.builds[3].name, 'project-test2')
1293 self.assertEqual(self.builds[4].name, 'project-test1')
1294 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001295
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001296 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001297 self.waitUntilSettled()
1298
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001299 # project-test2, project-merge for B
1300 self.assertEqual(len(self.builds), 2)
1301 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001302
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001303 self.worker.hold_jobs_in_build = False
1304 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001305 self.waitUntilSettled()
1306
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001307 self.assertEqual(len(self.builds), 0)
1308 self.assertEqual(len(self.history), 15)
1309 self.assertEqual(A.data['status'], 'NEW')
1310 self.assertEqual(B.data['status'], 'MERGED')
1311 self.assertEqual(C.data['status'], 'MERGED')
1312 self.assertEqual(A.reported, 2)
1313 self.assertEqual(B.reported, 2)
1314 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001315
1316 def test_failed_change_at_head_with_queue(self):
1317 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001318
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001319 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001320 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1321 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1322 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001323 A.addApproval('CRVW', 2)
1324 B.addApproval('CRVW', 2)
1325 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001326
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001327 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001328
1329 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1330 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1331 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1332
1333 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001334 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001335 self.assertEqual(len(self.builds), 0)
1336 self.assertEqual(len(queue), 1)
1337 self.assertEqual(queue[0].name, 'build:project-merge')
1338 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001339
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001340 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001341 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001342 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001343 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001344 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001345 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001346 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001347
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001348 self.assertEqual(len(self.builds), 0)
1349 self.assertEqual(len(queue), 6)
1350 self.assertEqual(queue[0].name, 'build:project-test1')
1351 self.assertEqual(queue[1].name, 'build:project-test2')
1352 self.assertEqual(queue[2].name, 'build:project-test1')
1353 self.assertEqual(queue[3].name, 'build:project-test2')
1354 self.assertEqual(queue[4].name, 'build:project-test1')
1355 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001356
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001357 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001358 self.waitUntilSettled()
1359
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001360 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001361 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001362 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1363 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001364
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001365 self.gearman_server.hold_jobs_in_queue = False
1366 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001367 self.waitUntilSettled()
1368
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001369 self.assertEqual(len(self.builds), 0)
1370 self.assertEqual(len(self.history), 11)
1371 self.assertEqual(A.data['status'], 'NEW')
1372 self.assertEqual(B.data['status'], 'MERGED')
1373 self.assertEqual(C.data['status'], 'MERGED')
1374 self.assertEqual(A.reported, 2)
1375 self.assertEqual(B.reported, 2)
1376 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001377
1378 def test_patch_order(self):
1379 "Test that dependent patches are tested in the right order"
1380 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1381 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1382 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1383 A.addApproval('CRVW', 2)
1384 B.addApproval('CRVW', 2)
1385 C.addApproval('CRVW', 2)
1386
1387 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1388 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1389 M2.setMerged()
1390 M1.setMerged()
1391
1392 # C -> B -> A -> M1 -> M2
1393 # M2 is here to make sure it is never queried. If it is, it
1394 # means zuul is walking down the entire history of merged
1395 # changes.
1396
1397 C.setDependsOn(B, 1)
1398 B.setDependsOn(A, 1)
1399 A.setDependsOn(M1, 1)
1400 M1.setDependsOn(M2, 1)
1401
1402 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1403
1404 self.waitUntilSettled()
1405
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001406 self.assertEqual(A.data['status'], 'NEW')
1407 self.assertEqual(B.data['status'], 'NEW')
1408 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001409
1410 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1411 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1412
1413 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001414 self.assertEqual(M2.queried, 0)
1415 self.assertEqual(A.data['status'], 'MERGED')
1416 self.assertEqual(B.data['status'], 'MERGED')
1417 self.assertEqual(C.data['status'], 'MERGED')
1418 self.assertEqual(A.reported, 2)
1419 self.assertEqual(B.reported, 2)
1420 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001421
James E. Blair0e933c52013-07-11 10:18:52 -07001422 def test_trigger_cache(self):
1423 "Test that the trigger cache operates correctly"
1424 self.worker.hold_jobs_in_build = True
1425
1426 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1427 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1428 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1429 A.addApproval('CRVW', 2)
1430 B.addApproval('CRVW', 2)
1431
1432 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1433 M1.setMerged()
1434
1435 B.setDependsOn(A, 1)
1436 A.setDependsOn(M1, 1)
1437
1438 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1439 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1440
1441 self.waitUntilSettled()
1442
1443 for build in self.builds:
1444 if build.parameters['ZUUL_PIPELINE'] == 'check':
1445 build.release()
1446 self.waitUntilSettled()
1447 for build in self.builds:
1448 if build.parameters['ZUUL_PIPELINE'] == 'check':
1449 build.release()
1450 self.waitUntilSettled()
1451
1452 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1453 self.waitUntilSettled()
1454
James E. Blair6c358e72013-07-29 17:06:47 -07001455 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001456 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001457 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001458
1459 self.worker.hold_jobs_in_build = False
1460 self.worker.release()
1461 self.waitUntilSettled()
1462
1463 self.assertEqual(A.data['status'], 'MERGED')
1464 self.assertEqual(B.data['status'], 'MERGED')
1465 self.assertEqual(A.queried, 2) # Initial and isMerged
1466 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1467
James E. Blair8c803f82012-07-31 16:25:42 -07001468 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001469 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001470 # TODO: move to test_gerrit (this is a unit test!)
1471 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001472 trigger = self.sched.layout.pipelines['gate'].trigger
1473 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001474 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001475 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001476
1477 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001478 a = trigger.getChange(1, 2, refresh=True)
1479 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001480
1481 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001482 a = trigger.getChange(1, 2, refresh=True)
1483 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1484 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001485
1486 def test_build_configuration(self):
1487 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001488
1489 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001490 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1491 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1492 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1493 A.addApproval('CRVW', 2)
1494 B.addApproval('CRVW', 2)
1495 C.addApproval('CRVW', 2)
1496 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1497 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1498 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1499 self.waitUntilSettled()
1500
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001501 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001502 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001503 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001504 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001505 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001506 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001507 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001508 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1509 self.gearman_server.hold_jobs_in_queue = False
1510 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001511 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001512
Monty Taylorbc758832013-06-17 17:22:42 -04001513 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001514 repo = git.Repo(path)
1515 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1516 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001517 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001518 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001519
1520 def test_build_configuration_conflict(self):
1521 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001522
1523 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001524 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1525 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001526 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001527 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1528 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001529 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001530 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1531 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001532 A.addApproval('CRVW', 2)
1533 B.addApproval('CRVW', 2)
1534 C.addApproval('CRVW', 2)
1535 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1536 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1537 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1538 self.waitUntilSettled()
1539
James E. Blair6736beb2013-07-11 15:18:15 -07001540 self.assertEqual(A.reported, 1)
1541 self.assertEqual(B.reported, 1)
1542 self.assertEqual(C.reported, 1)
1543
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001544 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001545 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001546 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001547 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001548 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001549 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001550 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001551 self.getParameter(queue[-1], 'ZUUL_REF')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001552 self.gearman_server.hold_jobs_in_queue = False
1553 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001554 self.waitUntilSettled()
1555
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001556 self.assertEqual(A.data['status'], 'MERGED')
1557 self.assertEqual(B.data['status'], 'NEW')
1558 self.assertEqual(C.data['status'], 'MERGED')
1559 self.assertEqual(A.reported, 2)
1560 self.assertEqual(B.reported, 2)
1561 self.assertEqual(C.reported, 2)
James E. Blairdaabed22012-08-15 15:38:57 -07001562
James E. Blair6736beb2013-07-11 15:18:15 -07001563 def test_dequeue_conflict(self):
1564 "Test that the option to dequeue merge conflicts works"
1565
1566 self.gearman_server.hold_jobs_in_queue = True
1567 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1568 A.addPatchset(['conflict'])
1569 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1570 B.addPatchset(['conflict'])
1571 A.addApproval('CRVW', 2)
1572 B.addApproval('CRVW', 2)
1573 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1574 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1575 self.waitUntilSettled()
1576
1577 self.assertEqual(A.reported, 1)
1578 self.assertEqual(B.reported, 2)
1579
1580 self.gearman_server.hold_jobs_in_queue = False
1581 self.gearman_server.release()
1582 self.waitUntilSettled()
1583
1584 self.assertEqual(A.data['status'], 'MERGED')
1585 self.assertEqual(B.data['status'], 'NEW')
1586 self.assertEqual(A.reported, 2)
1587 self.assertEqual(B.reported, 2)
1588
James E. Blairdaabed22012-08-15 15:38:57 -07001589 def test_post(self):
1590 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001591
Zhongyue Luo5d556072012-09-21 02:00:47 +09001592 e = {
1593 "type": "ref-updated",
1594 "submitter": {
1595 "name": "User Name",
1596 },
1597 "refUpdate": {
1598 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1599 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1600 "refName": "master",
1601 "project": "org/project",
1602 }
1603 }
James E. Blairdaabed22012-08-15 15:38:57 -07001604 self.fake_gerrit.addEvent(e)
1605 self.waitUntilSettled()
1606
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001607 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001608 self.assertEqual(len(self.history), 1)
1609 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001610
1611 def test_build_configuration_branch(self):
1612 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001613
1614 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001615 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1616 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1617 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1618 A.addApproval('CRVW', 2)
1619 B.addApproval('CRVW', 2)
1620 C.addApproval('CRVW', 2)
1621 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1622 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1623 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1624 self.waitUntilSettled()
1625
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001626 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001627 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001628 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001629 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001630 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001631 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001632 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001633 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1634 self.gearman_server.hold_jobs_in_queue = False
1635 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001636 self.waitUntilSettled()
1637
Monty Taylorbc758832013-06-17 17:22:42 -04001638 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001639 repo = git.Repo(path)
1640 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1641 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001642 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001643 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001644
1645 def test_build_configuration_branch_interaction(self):
1646 "Test that switching between branches works"
1647 self.test_build_configuration()
1648 self.test_build_configuration_branch()
1649 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001650 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001651 repo = git.Repo(path)
1652 repo.heads.master.commit = repo.commit('init')
1653 self.test_build_configuration()
1654
1655 def test_build_configuration_multi_branch(self):
1656 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001657
1658 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001659 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1660 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1661 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1662 A.addApproval('CRVW', 2)
1663 B.addApproval('CRVW', 2)
1664 C.addApproval('CRVW', 2)
1665 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1666 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1667 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1668 self.waitUntilSettled()
1669
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001670 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001671 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001672 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001673 job_B = None
1674 for job in queue:
1675 if 'project-merge' in job.name:
1676 job_B = job
1677 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001678 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001679 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001680 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001681 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001682 for job in queue:
1683 if 'project-merge' in job.name:
1684 job_C = job
1685 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001686 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001687 self.gearman_server.hold_jobs_in_queue = False
1688 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001689 self.waitUntilSettled()
1690
Monty Taylorbc758832013-06-17 17:22:42 -04001691 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001692 repo = git.Repo(path)
1693
1694 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001695 for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001696 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001697 correct_messages = ['initial commit', 'A-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001698 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001699
1700 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001701 for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001702 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001703 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001704 self.assertEqual(repo_messages, correct_messages)
James E. Blair7f71c802012-08-22 13:04:32 -07001705
1706 def test_one_job_project(self):
1707 "Test that queueing works with one job"
1708 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1709 'master', 'A')
1710 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1711 'master', 'B')
1712 A.addApproval('CRVW', 2)
1713 B.addApproval('CRVW', 2)
1714 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1715 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1716 self.waitUntilSettled()
1717
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001718 self.assertEqual(A.data['status'], 'MERGED')
1719 self.assertEqual(A.reported, 2)
1720 self.assertEqual(B.data['status'], 'MERGED')
1721 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001722
Antoine Musso80edd5a2013-02-13 15:37:53 +01001723 def test_job_from_templates_launched(self):
1724 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001725
Antoine Musso80edd5a2013-02-13 15:37:53 +01001726 A = self.fake_gerrit.addFakeChange(
1727 'org/templated-project', 'master', 'A')
1728 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1729 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001730
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001731 self.assertEqual(self.getJobFromHistory('project-test1').result,
1732 'SUCCESS')
1733 self.assertEqual(self.getJobFromHistory('project-test2').result,
1734 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001735
James E. Blaircaec0c52012-08-22 14:52:22 -07001736 def test_dependent_changes_dequeue(self):
1737 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001738
James E. Blaircaec0c52012-08-22 14:52:22 -07001739 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1740 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1741 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1742 A.addApproval('CRVW', 2)
1743 B.addApproval('CRVW', 2)
1744 C.addApproval('CRVW', 2)
1745
1746 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1747 M1.setMerged()
1748
1749 # C -> B -> A -> M1
1750
1751 C.setDependsOn(B, 1)
1752 B.setDependsOn(A, 1)
1753 A.setDependsOn(M1, 1)
1754
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001755 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001756
1757 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1758 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1759 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1760
1761 self.waitUntilSettled()
1762
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001763 self.assertEqual(A.data['status'], 'NEW')
1764 self.assertEqual(A.reported, 2)
1765 self.assertEqual(B.data['status'], 'NEW')
1766 self.assertEqual(B.reported, 2)
1767 self.assertEqual(C.data['status'], 'NEW')
1768 self.assertEqual(C.reported, 2)
1769 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001770
1771 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001772 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001773 # If it's dequeued more than once, we should see extra
1774 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001775
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001776 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001777 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1778 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1779 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1780 A.addApproval('CRVW', 2)
1781 B.addApproval('CRVW', 2)
1782 C.addApproval('CRVW', 2)
1783
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001784 self.worker.addFailTest('project1-test1', A)
1785 self.worker.addFailTest('project1-test2', A)
1786 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001787
1788 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1789 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1790 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1791
1792 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001793
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001794 self.assertEqual(len(self.builds), 1)
1795 self.assertEqual(self.builds[0].name, 'project1-merge')
1796 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07001797
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001798 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001799 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001800 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001801 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001802 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001803 self.waitUntilSettled()
1804
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001805 self.assertEqual(len(self.builds), 9)
1806 self.assertEqual(self.builds[0].name, 'project1-test1')
1807 self.assertEqual(self.builds[1].name, 'project1-test2')
1808 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
1809 self.assertEqual(self.builds[3].name, 'project1-test1')
1810 self.assertEqual(self.builds[4].name, 'project1-test2')
1811 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
1812 self.assertEqual(self.builds[6].name, 'project1-test1')
1813 self.assertEqual(self.builds[7].name, 'project1-test2')
1814 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001815
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001816 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001817 self.waitUntilSettled()
1818
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001819 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1820 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001821
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001822 self.worker.hold_jobs_in_build = False
1823 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001824 self.waitUntilSettled()
1825
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001826 self.assertEqual(len(self.builds), 0)
1827 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001828
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001829 self.assertEqual(A.data['status'], 'NEW')
1830 self.assertEqual(B.data['status'], 'MERGED')
1831 self.assertEqual(C.data['status'], 'MERGED')
1832 self.assertEqual(A.reported, 2)
1833 self.assertEqual(B.reported, 2)
1834 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001835
1836 def test_nonvoting_job(self):
1837 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001838
James E. Blair4ec821f2012-08-23 15:28:28 -07001839 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1840 'master', 'A')
1841 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001842 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001843 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1844
1845 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001846
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001847 self.assertEqual(A.data['status'], 'MERGED')
1848 self.assertEqual(A.reported, 2)
1849 self.assertEqual(
1850 self.getJobFromHistory('nonvoting-project-merge').result,
1851 'SUCCESS')
1852 self.assertEqual(
1853 self.getJobFromHistory('nonvoting-project-test1').result,
1854 'SUCCESS')
1855 self.assertEqual(
1856 self.getJobFromHistory('nonvoting-project-test2').result,
1857 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001858
1859 def test_check_queue_success(self):
1860 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001861
James E. Blaire0487072012-08-29 17:38:31 -07001862 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1863 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1864
1865 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001866
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001867 self.assertEqual(A.data['status'], 'NEW')
1868 self.assertEqual(A.reported, 1)
1869 self.assertEqual(self.getJobFromHistory('project-merge').result,
1870 'SUCCESS')
1871 self.assertEqual(self.getJobFromHistory('project-test1').result,
1872 'SUCCESS')
1873 self.assertEqual(self.getJobFromHistory('project-test2').result,
1874 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001875
1876 def test_check_queue_failure(self):
1877 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001878
James E. Blaire0487072012-08-29 17:38:31 -07001879 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001880 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001881 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1882
1883 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001884
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001885 self.assertEqual(A.data['status'], 'NEW')
1886 self.assertEqual(A.reported, 1)
1887 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001888 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001889 self.assertEqual(self.getJobFromHistory('project-test1').result,
1890 'SUCCESS')
1891 self.assertEqual(self.getJobFromHistory('project-test2').result,
1892 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001893
1894 def test_dependent_behind_dequeue(self):
1895 "test that dependent changes behind dequeued changes work"
1896 # This complicated test is a reproduction of a real life bug
1897 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001898
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001899 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001900 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1901 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1902 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1903 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1904 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1905 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1906 D.setDependsOn(C, 1)
1907 E.setDependsOn(D, 1)
1908 A.addApproval('CRVW', 2)
1909 B.addApproval('CRVW', 2)
1910 C.addApproval('CRVW', 2)
1911 D.addApproval('CRVW', 2)
1912 E.addApproval('CRVW', 2)
1913 F.addApproval('CRVW', 2)
1914
1915 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001916
1917 # Change object re-use in the gerrit trigger is hidden if
1918 # changes are added in quick succession; waiting makes it more
1919 # like real life.
1920 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1921 self.waitUntilSettled()
1922 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1923 self.waitUntilSettled()
1924
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001925 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001926 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001927 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001928 self.waitUntilSettled()
1929
1930 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1931 self.waitUntilSettled()
1932 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1933 self.waitUntilSettled()
1934 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1935 self.waitUntilSettled()
1936 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1937 self.waitUntilSettled()
1938
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001939 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001940 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001941 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001942 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001943 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001944 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001945 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001946 self.waitUntilSettled()
1947
1948 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001949
1950 # Grab pointers to the jobs we want to release before
1951 # releasing any, because list indexes may change as
1952 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001953 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001954 a.release()
1955 b.release()
1956 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001957 self.waitUntilSettled()
1958
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001959 self.worker.hold_jobs_in_build = False
1960 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07001961 self.waitUntilSettled()
1962
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001963 self.assertEqual(A.data['status'], 'NEW')
1964 self.assertEqual(B.data['status'], 'MERGED')
1965 self.assertEqual(C.data['status'], 'MERGED')
1966 self.assertEqual(D.data['status'], 'MERGED')
1967 self.assertEqual(E.data['status'], 'MERGED')
1968 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001969
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001970 self.assertEqual(A.reported, 2)
1971 self.assertEqual(B.reported, 2)
1972 self.assertEqual(C.reported, 2)
1973 self.assertEqual(D.reported, 2)
1974 self.assertEqual(E.reported, 2)
1975 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001976
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001977 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1978 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001979
1980 def test_merger_repack(self):
1981 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001982
James E. Blair05fed602012-09-07 12:45:24 -07001983 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1984 A.addApproval('CRVW', 2)
1985 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1986 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001987 self.assertEqual(self.getJobFromHistory('project-merge').result,
1988 'SUCCESS')
1989 self.assertEqual(self.getJobFromHistory('project-test1').result,
1990 'SUCCESS')
1991 self.assertEqual(self.getJobFromHistory('project-test2').result,
1992 'SUCCESS')
1993 self.assertEqual(A.data['status'], 'MERGED')
1994 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001995 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07001996 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001997
Monty Taylorbc758832013-06-17 17:22:42 -04001998 path = os.path.join(self.git_root, "org/project")
1999 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002000
2001 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2002 A.addApproval('CRVW', 2)
2003 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2004 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002005 self.assertEqual(self.getJobFromHistory('project-merge').result,
2006 'SUCCESS')
2007 self.assertEqual(self.getJobFromHistory('project-test1').result,
2008 'SUCCESS')
2009 self.assertEqual(self.getJobFromHistory('project-test2').result,
2010 'SUCCESS')
2011 self.assertEqual(A.data['status'], 'MERGED')
2012 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002013
James E. Blair4886f282012-11-15 09:27:33 -08002014 def test_merger_repack_large_change(self):
2015 "Test that the merger works with large changes after a repack"
2016 # https://bugs.launchpad.net/zuul/+bug/1078946
2017 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2018 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002019 path = os.path.join(self.upstream_root, "org/project1")
2020 print repack_repo(path)
2021 path = os.path.join(self.git_root, "org/project1")
2022 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002023
2024 A.addApproval('CRVW', 2)
2025 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2026 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002027 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2028 'SUCCESS')
2029 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2030 'SUCCESS')
2031 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2032 'SUCCESS')
2033 self.assertEqual(A.data['status'], 'MERGED')
2034 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002035
James E. Blair7ee88a22012-09-12 18:59:31 +02002036 def test_nonexistent_job(self):
2037 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002038 # Set to the state immediately after a restart
2039 self.resetGearmanServer()
2040 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002041
2042 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2043 A.addApproval('CRVW', 2)
2044 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2045 # There may be a thread about to report a lost change
2046 while A.reported < 2:
2047 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002048 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002049 self.assertFalse(job_names)
2050 self.assertEqual(A.data['status'], 'NEW')
2051 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002052 self.assertEmptyQueues()
2053
2054 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002055 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002056 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2057 A.addApproval('CRVW', 2)
2058 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2059 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002060 self.assertEqual(self.getJobFromHistory('project-merge').result,
2061 'SUCCESS')
2062 self.assertEqual(self.getJobFromHistory('project-test1').result,
2063 'SUCCESS')
2064 self.assertEqual(self.getJobFromHistory('project-test2').result,
2065 'SUCCESS')
2066 self.assertEqual(A.data['status'], 'MERGED')
2067 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002068
2069 def test_single_nonexistent_post_job(self):
2070 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002071 e = {
2072 "type": "ref-updated",
2073 "submitter": {
2074 "name": "User Name",
2075 },
2076 "refUpdate": {
2077 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2078 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2079 "refName": "master",
2080 "project": "org/project",
2081 }
2082 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002083 # Set to the state immediately after a restart
2084 self.resetGearmanServer()
2085 self.launcher.negative_function_cache_ttl = 0
2086
James E. Blairf62d4282012-12-31 17:01:50 -08002087 self.fake_gerrit.addEvent(e)
2088 self.waitUntilSettled()
2089
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002090 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002091
2092 def test_new_patchset_dequeues_old(self):
2093 "Test that a new patchset causes the old to be dequeued"
2094 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002095 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002096 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2097 M.setMerged()
2098
2099 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2100 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2101 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2102 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2103 A.addApproval('CRVW', 2)
2104 B.addApproval('CRVW', 2)
2105 C.addApproval('CRVW', 2)
2106 D.addApproval('CRVW', 2)
2107
2108 C.setDependsOn(B, 1)
2109 B.setDependsOn(A, 1)
2110 A.setDependsOn(M, 1)
2111
2112 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2113 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2114 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2115 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2116 self.waitUntilSettled()
2117
2118 B.addPatchset()
2119 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2120 self.waitUntilSettled()
2121
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002122 self.worker.hold_jobs_in_build = False
2123 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002124 self.waitUntilSettled()
2125
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002126 self.assertEqual(A.data['status'], 'MERGED')
2127 self.assertEqual(A.reported, 2)
2128 self.assertEqual(B.data['status'], 'NEW')
2129 self.assertEqual(B.reported, 2)
2130 self.assertEqual(C.data['status'], 'NEW')
2131 self.assertEqual(C.reported, 2)
2132 self.assertEqual(D.data['status'], 'MERGED')
2133 self.assertEqual(D.reported, 2)
2134 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002135
2136 def test_new_patchset_dequeues_old_on_head(self):
2137 "Test that a new patchset causes the old to be dequeued (at head)"
2138 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002139 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002140 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2141 M.setMerged()
2142 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2143 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2144 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2145 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2146 A.addApproval('CRVW', 2)
2147 B.addApproval('CRVW', 2)
2148 C.addApproval('CRVW', 2)
2149 D.addApproval('CRVW', 2)
2150
2151 C.setDependsOn(B, 1)
2152 B.setDependsOn(A, 1)
2153 A.setDependsOn(M, 1)
2154
2155 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2156 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2157 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2158 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2159 self.waitUntilSettled()
2160
2161 A.addPatchset()
2162 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2163 self.waitUntilSettled()
2164
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002165 self.worker.hold_jobs_in_build = False
2166 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002167 self.waitUntilSettled()
2168
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002169 self.assertEqual(A.data['status'], 'NEW')
2170 self.assertEqual(A.reported, 2)
2171 self.assertEqual(B.data['status'], 'NEW')
2172 self.assertEqual(B.reported, 2)
2173 self.assertEqual(C.data['status'], 'NEW')
2174 self.assertEqual(C.reported, 2)
2175 self.assertEqual(D.data['status'], 'MERGED')
2176 self.assertEqual(D.reported, 2)
2177 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002178
2179 def test_new_patchset_dequeues_old_without_dependents(self):
2180 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002181 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002182 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2183 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2184 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2185 A.addApproval('CRVW', 2)
2186 B.addApproval('CRVW', 2)
2187 C.addApproval('CRVW', 2)
2188
2189 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2190 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2191 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2192 self.waitUntilSettled()
2193
2194 B.addPatchset()
2195 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2196 self.waitUntilSettled()
2197
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002198 self.worker.hold_jobs_in_build = False
2199 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002200 self.waitUntilSettled()
2201
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002202 self.assertEqual(A.data['status'], 'MERGED')
2203 self.assertEqual(A.reported, 2)
2204 self.assertEqual(B.data['status'], 'NEW')
2205 self.assertEqual(B.reported, 2)
2206 self.assertEqual(C.data['status'], 'MERGED')
2207 self.assertEqual(C.reported, 2)
2208 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002209
2210 def test_new_patchset_dequeues_old_independent_queue(self):
2211 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002212 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002213 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2214 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2215 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2216 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2217 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2218 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2219 self.waitUntilSettled()
2220
2221 B.addPatchset()
2222 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2223 self.waitUntilSettled()
2224
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002225 self.worker.hold_jobs_in_build = False
2226 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002227 self.waitUntilSettled()
2228
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002229 self.assertEqual(A.data['status'], 'NEW')
2230 self.assertEqual(A.reported, 1)
2231 self.assertEqual(B.data['status'], 'NEW')
2232 self.assertEqual(B.reported, 1)
2233 self.assertEqual(C.data['status'], 'NEW')
2234 self.assertEqual(C.reported, 1)
2235 self.assertEqual(len(self.history), 10)
2236 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002237
2238 def test_zuul_refs(self):
2239 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002240 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002241 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2242 M1.setMerged()
2243 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2244 M2.setMerged()
2245
2246 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2247 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2248 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2249 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2250 A.addApproval('CRVW', 2)
2251 B.addApproval('CRVW', 2)
2252 C.addApproval('CRVW', 2)
2253 D.addApproval('CRVW', 2)
2254 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2255 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2256 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2257 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2258
2259 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002260 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002261 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002262 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002263 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002264 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002265 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002266 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002267 self.waitUntilSettled()
2268
James E. Blair7d0dedc2013-02-21 17:26:09 -08002269 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002270 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002271 if x.parameters['ZUUL_CHANGE'] == '3':
2272 a_zref = x.parameters['ZUUL_REF']
2273 if x.parameters['ZUUL_CHANGE'] == '4':
2274 b_zref = x.parameters['ZUUL_REF']
2275 if x.parameters['ZUUL_CHANGE'] == '5':
2276 c_zref = x.parameters['ZUUL_REF']
2277 if x.parameters['ZUUL_CHANGE'] == '6':
2278 d_zref = x.parameters['ZUUL_REF']
2279
2280 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002281 self.assertIsNotNone(a_zref)
2282 self.assertIsNotNone(b_zref)
2283 self.assertIsNotNone(c_zref)
2284 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002285
2286 # And they should all be different
2287 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002288 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002289
2290 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002291 self.assertTrue(self.ref_has_change(a_zref, A))
2292 self.assertFalse(self.ref_has_change(a_zref, B))
2293 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002294
2295 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002296 self.assertTrue(self.ref_has_change(b_zref, A))
2297 self.assertTrue(self.ref_has_change(b_zref, B))
2298 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002299
2300 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002301 self.assertTrue(self.ref_has_change(c_zref, A))
2302 self.assertTrue(self.ref_has_change(c_zref, B))
2303 self.assertTrue(self.ref_has_change(c_zref, C))
2304 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002305
2306 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002307 self.assertTrue(self.ref_has_change(d_zref, A))
2308 self.assertTrue(self.ref_has_change(d_zref, B))
2309 self.assertTrue(self.ref_has_change(d_zref, C))
2310 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002311
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002312 self.worker.hold_jobs_in_build = False
2313 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002314 self.waitUntilSettled()
2315
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002316 self.assertEqual(A.data['status'], 'MERGED')
2317 self.assertEqual(A.reported, 2)
2318 self.assertEqual(B.data['status'], 'MERGED')
2319 self.assertEqual(B.reported, 2)
2320 self.assertEqual(C.data['status'], 'MERGED')
2321 self.assertEqual(C.reported, 2)
2322 self.assertEqual(D.data['status'], 'MERGED')
2323 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002324
James E. Blair412e5582013-04-22 15:50:12 -07002325 def test_statsd(self):
2326 "Test each of the statsd methods used in the scheduler"
2327 import extras
2328 statsd = extras.try_import('statsd.statsd')
2329 statsd.incr('test-incr')
2330 statsd.timing('test-timing', 3)
2331 statsd.gauge('test-guage', 12)
2332 self.assertReportedStat('test-incr', '1|c')
2333 self.assertReportedStat('test-timing', '3|ms')
2334 self.assertReportedStat('test-guage', '12|g')
2335
James E. Blair70c71582013-03-06 08:50:50 -08002336 def test_file_jobs(self):
2337 "Test that file jobs run only when appropriate"
2338 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2339 A.addPatchset(['pip-requires'])
2340 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2341 A.addApproval('CRVW', 2)
2342 B.addApproval('CRVW', 2)
2343 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2344 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2345 self.waitUntilSettled()
2346
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002347 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002348 if x.name == 'project-testfile']
2349
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002350 self.assertEqual(len(testfile_jobs), 1)
2351 self.assertEqual(testfile_jobs[0].changes, '1,2')
2352 self.assertEqual(A.data['status'], 'MERGED')
2353 self.assertEqual(A.reported, 2)
2354 self.assertEqual(B.data['status'], 'MERGED')
2355 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002356
2357 def test_test_config(self):
2358 "Test that we can test the config"
2359 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002360 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002361 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002362 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002363
2364 def test_build_description(self):
2365 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002366 self.worker.registerFunction('set_description:' +
2367 self.worker.worker_id)
2368
2369 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2370 A.addApproval('CRVW', 2)
2371 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2372 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002373 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002374 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002375 self.assertTrue(re.search("Branch.*master", desc))
2376 self.assertTrue(re.search("Pipeline.*gate", desc))
2377 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2378 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2379 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2380 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002381
James E. Blair64ed6f22013-07-10 14:07:23 -07002382 def test_queue_precedence(self):
2383 "Test that queue precedence works"
2384
2385 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002386 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002387 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2388 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2389 A.addApproval('CRVW', 2)
2390 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2391
2392 self.waitUntilSettled()
2393 self.gearman_server.hold_jobs_in_queue = False
2394 self.gearman_server.release()
2395 self.waitUntilSettled()
2396
James E. Blair8de58bd2013-07-18 16:23:33 -07002397 # Run one build at a time to ensure non-race order:
2398 for x in range(6):
2399 self.release(self.builds[0])
2400 self.waitUntilSettled()
2401 self.worker.hold_jobs_in_build = False
2402 self.waitUntilSettled()
2403
James E. Blair64ed6f22013-07-10 14:07:23 -07002404 self.log.debug(self.history)
2405 self.assertEqual(self.history[0].pipeline, 'gate')
2406 self.assertEqual(self.history[1].pipeline, 'check')
2407 self.assertEqual(self.history[2].pipeline, 'gate')
2408 self.assertEqual(self.history[3].pipeline, 'gate')
2409 self.assertEqual(self.history[4].pipeline, 'check')
2410 self.assertEqual(self.history[5].pipeline, 'check')
2411
James E. Blair1843a552013-07-03 14:19:52 -07002412 def test_json_status(self):
2413 "Test that we can retrieve JSON status info"
2414 self.worker.hold_jobs_in_build = True
2415 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2416 A.addApproval('CRVW', 2)
2417 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2418 self.waitUntilSettled()
2419
2420 port = self.webapp.server.socket.getsockname()[1]
2421
2422 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2423 data = f.read()
2424
2425 self.worker.hold_jobs_in_build = False
2426 self.worker.release()
2427 self.waitUntilSettled()
2428
2429 data = json.loads(data)
2430 status_jobs = set()
2431 for p in data['pipelines']:
2432 for q in p['change_queues']:
2433 for head in q['heads']:
2434 for change in head:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002435 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002436 for job in change['jobs']:
2437 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002438 self.assertIn('project-merge', status_jobs)
2439 self.assertIn('project-test1', status_jobs)
2440 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002441
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002442 def test_node_label(self):
2443 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002444 self.worker.registerFunction('build:node-project-test1:debian')
2445
2446 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2447 A.addApproval('CRVW', 2)
2448 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2449 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002450
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002451 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2452 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2453 'debian')
2454 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002455
2456 def test_live_reconfiguration(self):
2457 "Test that live reconfiguration works"
2458 self.worker.hold_jobs_in_build = True
2459 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2460 A.addApproval('CRVW', 2)
2461 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2462 self.waitUntilSettled()
2463
2464 self.sched.reconfigure(self.config)
2465
2466 self.worker.hold_jobs_in_build = False
2467 self.worker.release()
2468 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002469 self.assertEqual(self.getJobFromHistory('project-merge').result,
2470 'SUCCESS')
2471 self.assertEqual(self.getJobFromHistory('project-test1').result,
2472 'SUCCESS')
2473 self.assertEqual(self.getJobFromHistory('project-test2').result,
2474 'SUCCESS')
2475 self.assertEqual(A.data['status'], 'MERGED')
2476 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002477
James E. Blaire712d9f2013-07-31 11:40:11 -07002478 def test_live_reconfiguration_functions(self):
2479 "Test live reconfiguration with a custom function"
2480 self.worker.registerFunction('build:node-project-test1:debian')
2481 self.worker.registerFunction('build:node-project-test1:wheezy')
2482 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2483 A.addApproval('CRVW', 2)
2484 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2485 self.waitUntilSettled()
2486
2487 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2488 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2489 'debian')
2490 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2491
2492 self.config.set('zuul', 'layout_config',
2493 'tests/fixtures/layout-live-'
2494 'reconfiguration-functions.yaml')
2495 self.sched.reconfigure(self.config)
2496 self.worker.build_history = []
2497
2498 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
2499 B.addApproval('CRVW', 2)
2500 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2501 self.waitUntilSettled()
2502
2503 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2504 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2505 'wheezy')
2506 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2507
James E. Blair287c06d2013-07-24 10:39:30 -07002508 def test_delayed_repo_init(self):
2509 self.config.set('zuul', 'layout_config',
2510 'tests/fixtures/layout-delayed-repo-init.yaml')
2511 self.sched.reconfigure(self.config)
2512
2513 self.init_repo("org/new-project")
2514 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2515
2516 A.addApproval('CRVW', 2)
2517 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2518 self.waitUntilSettled()
2519 self.assertEqual(self.getJobFromHistory('project-merge').result,
2520 'SUCCESS')
2521 self.assertEqual(self.getJobFromHistory('project-test1').result,
2522 'SUCCESS')
2523 self.assertEqual(self.getJobFromHistory('project-test2').result,
2524 'SUCCESS')
2525 self.assertEqual(A.data['status'], 'MERGED')
2526 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002527
2528 def test_timer(self):
2529 "Test that a periodic job is triggered"
2530 self.worker.hold_jobs_in_build = True
2531 self.config.set('zuul', 'layout_config',
2532 'tests/fixtures/layout-timer.yaml')
2533 self.sched.reconfigure(self.config)
2534 self.registerJobs()
2535
2536 start = time.time()
2537 failed = True
2538 while ((time.time() - start) < 30):
2539 if len(self.builds) == 2:
2540 failed = False
2541 break
2542 else:
2543 time.sleep(1)
2544
2545 if failed:
2546 raise Exception("Expected jobs never ran")
2547
2548 self.waitUntilSettled()
2549 port = self.webapp.server.socket.getsockname()[1]
2550
2551 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2552 data = f.read()
2553
2554 self.worker.hold_jobs_in_build = False
2555 self.worker.release()
2556 self.waitUntilSettled()
2557
2558 self.assertEqual(self.getJobFromHistory(
2559 'project-bitrot-stable-old').result, 'SUCCESS')
2560 self.assertEqual(self.getJobFromHistory(
2561 'project-bitrot-stable-older').result, 'SUCCESS')
2562
2563 data = json.loads(data)
2564 status_jobs = set()
2565 for p in data['pipelines']:
2566 for q in p['change_queues']:
2567 for head in q['heads']:
2568 for change in head:
2569 self.assertEqual(change['id'], 'None')
2570 for job in change['jobs']:
2571 status_jobs.add(job['name'])
2572 self.assertIn('project-bitrot-stable-old', status_jobs)
2573 self.assertIn('project-bitrot-stable-older', status_jobs)