blob: 30c465f1814666b708d2ceff0441a5e6d6c894c6 [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 = ''
James E. Blair4a28a882013-08-23 15:17:33 -0700446 self.run_error = False
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700447
448 def release(self):
449 self.wait_condition.acquire()
450 self.wait_condition.notify()
451 self.waiting = False
452 self.log.debug("Build %s released" % self.unique)
453 self.wait_condition.release()
454
455 def isWaiting(self):
456 self.wait_condition.acquire()
457 if self.waiting:
458 ret = True
459 else:
460 ret = False
461 self.wait_condition.release()
462 return ret
463
464 def _wait(self):
465 self.wait_condition.acquire()
466 self.waiting = True
467 self.log.debug("Build %s waiting" % self.unique)
468 self.wait_condition.wait()
469 self.wait_condition.release()
470
471 def run(self):
472 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700473 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
474 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700475 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700476 'manager': self.worker.worker_id,
Paul Belangerec49b4c2013-07-20 20:32:20 -0400477 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700478
479 self.job.sendWorkData(json.dumps(data))
480 self.job.sendWorkStatus(0, 100)
481
482 if self.worker.hold_jobs_in_build:
483 self._wait()
484 self.log.debug("Build %s continuing" % self.unique)
485
486 self.worker.lock.acquire()
487
488 result = 'SUCCESS'
489 if (('ZUUL_REF' in self.parameters) and
490 self.worker.shouldFailTest(self.name,
491 self.parameters['ZUUL_REF'])):
492 result = 'FAILURE'
493 if self.aborted:
494 result = 'ABORTED'
495
James E. Blair4a28a882013-08-23 15:17:33 -0700496 if self.run_error:
497 work_fail = True
498 result = 'RUN_ERROR'
499 else:
500 data['result'] = result
501 work_fail = False
502
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700503 changes = None
504 if 'ZUUL_CHANGE_IDS' in self.parameters:
505 changes = self.parameters['ZUUL_CHANGE_IDS']
506
507 self.worker.build_history.append(
508 BuildHistory(name=self.name, number=self.number,
509 result=result, changes=changes, node=self.node,
James E. Blair64ed6f22013-07-10 14:07:23 -0700510 uuid=self.unique, description=self.description,
511 pipeline=self.parameters['ZUUL_PIPELINE'])
Paul Belangerec49b4c2013-07-20 20:32:20 -0400512 )
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700513
James E. Blair4a28a882013-08-23 15:17:33 -0700514 self.job.sendWorkData(json.dumps(data))
515 if work_fail:
516 self.job.sendWorkFail()
517 else:
518 self.job.sendWorkComplete(json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700519 del self.worker.gearman_jobs[self.job.unique]
520 self.worker.running_builds.remove(self)
521 self.worker.lock.release()
522
523
524class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400525 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700526 super(FakeWorker, self).__init__(worker_id)
527 self.gearman_jobs = {}
528 self.build_history = []
529 self.running_builds = []
530 self.build_counter = 0
531 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400532 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700533
534 self.hold_jobs_in_build = False
535 self.lock = threading.Lock()
536 self.__work_thread = threading.Thread(target=self.work)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400537 self.__work_thread.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700538 self.__work_thread.start()
539
540 def handleJob(self, job):
541 parts = job.name.split(":")
542 cmd = parts[0]
543 name = parts[1]
544 if len(parts) > 2:
545 node = parts[2]
546 else:
547 node = None
548 if cmd == 'build':
549 self.handleBuild(job, name, node)
550 elif cmd == 'stop':
551 self.handleStop(job, name)
552 elif cmd == 'set_description':
553 self.handleSetDescription(job, name)
554
555 def handleBuild(self, job, name, node):
556 build = FakeBuild(self, job, self.build_counter, node)
557 job.build = build
558 self.gearman_jobs[job.unique] = job
559 self.build_counter += 1
560
561 self.running_builds.append(build)
562 build.start()
563
564 def handleStop(self, job, name):
565 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700566 parameters = json.loads(job.arguments)
567 name = parameters['name']
568 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700569 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700570 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700571 build.aborted = True
572 build.release()
573 job.sendWorkComplete()
574 return
575 job.sendWorkFail()
576
577 def handleSetDescription(self, job, name):
578 self.log.debug("handle set description")
579 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700580 name = parameters['name']
581 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700582 descr = parameters['html_description']
583 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700584 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700585 build.description = descr
586 job.sendWorkComplete()
587 return
588 for build in self.build_history:
James E. Blair3c483cf2013-06-04 16:30:43 -0700589 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700590 build.description = descr
591 job.sendWorkComplete()
592 return
593 job.sendWorkFail()
594
595 def work(self):
596 while self.running:
597 try:
598 job = self.getJob()
599 except gear.InterruptedError:
600 continue
601 try:
602 self.handleJob(job)
603 except:
604 self.log.exception("Worker exception:")
605
606 def addFailTest(self, name, change):
607 l = self.fail_tests.get(name, [])
608 l.append(change)
609 self.fail_tests[name] = l
610
611 def shouldFailTest(self, name, ref):
612 l = self.fail_tests.get(name, [])
613 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400614 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700615 return True
616 return False
617
618 def release(self, regex=None):
619 builds = self.running_builds[:]
620 self.log.debug("releasing build %s (%s)" % (regex,
James E. Blair78e31b32013-07-09 09:11:34 -0700621 len(self.running_builds)))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700622 for build in builds:
623 if not regex or re.match(regex, build.name):
624 self.log.debug("releasing build %s" %
625 (build.parameters['ZUUL_UUID']))
626 build.release()
627 else:
628 self.log.debug("not releasing build %s" %
629 (build.parameters['ZUUL_UUID']))
630 self.log.debug("done releasing builds %s (%s)" %
631 (regex, len(self.running_builds)))
632
633
634class FakeGearmanServer(gear.Server):
635 def __init__(self):
636 self.hold_jobs_in_queue = False
637 super(FakeGearmanServer, self).__init__(0)
638
639 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700640 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
641 for job in queue:
642 if not hasattr(job, 'waiting'):
643 if job.name.startswith('build:'):
644 job.waiting = self.hold_jobs_in_queue
645 else:
646 job.waiting = False
647 if job.waiting:
648 continue
649 if job.name in connection.functions:
650 if not peek:
651 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400652 connection.related_jobs[job.handle] = job
653 job.worker_connection = connection
654 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700655 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700656 return None
657
658 def release(self, regex=None):
659 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700660 qlen = (len(self.high_queue) + len(self.normal_queue) +
661 len(self.low_queue))
662 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
James E. Blairdda6c912013-07-29 14:12:12 -0700663 for job in self.getQueue():
664 cmd, name = job.name.split(':')
665 if cmd != 'build':
666 continue
667 if not regex or re.match(regex, name):
668 self.log.debug("releasing queued job %s" %
669 job.unique)
670 job.waiting = False
671 released = True
672 else:
673 self.log.debug("not releasing queued job %s" %
674 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700675 if released:
676 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700677 qlen = (len(self.high_queue) + len(self.normal_queue) +
678 len(self.low_queue))
679 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700680
681
Monty Taylorbc758832013-06-17 17:22:42 -0400682class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700683 log = logging.getLogger("zuul.test")
684
685 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400686 super(TestScheduler, self).setUp()
687 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
688 try:
689 test_timeout = int(test_timeout)
690 except ValueError:
691 # If timeout value is invalid do not set a timeout.
692 test_timeout = 0
693 if test_timeout > 0:
694 self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
695
696 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
697 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
698 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
699 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
700 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
701 os.environ.get('OS_STDERR_CAPTURE') == '1'):
702 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
703 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Monty Taylore6a6c402013-07-02 09:25:55 -0700704 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
705 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400706 self.useFixture(fixtures.FakeLogger(
707 level=logging.DEBUG,
708 format='%(asctime)s %(name)-32s '
709 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700710 tmp_root = self.useFixture(fixtures.TempDir(
711 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400712 self.test_root = os.path.join(tmp_root, "zuul-test")
713 self.upstream_root = os.path.join(self.test_root, "upstream")
714 self.git_root = os.path.join(self.test_root, "git")
715
716 CONFIG.set('zuul', 'git_dir', self.git_root)
717 if os.path.exists(self.test_root):
718 shutil.rmtree(self.test_root)
719 os.makedirs(self.test_root)
720 os.makedirs(self.upstream_root)
721 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700722
723 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400724 self.init_repo("org/project")
725 self.init_repo("org/project1")
726 self.init_repo("org/project2")
727 self.init_repo("org/project3")
728 self.init_repo("org/one-job-project")
729 self.init_repo("org/nonvoting-project")
730 self.init_repo("org/templated-project")
731 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700732 self.init_repo("org/conflict-project")
James E. Blair412e5582013-04-22 15:50:12 -0700733
734 self.statsd = FakeStatsd()
735 os.environ['STATSD_HOST'] = 'localhost'
736 os.environ['STATSD_PORT'] = str(self.statsd.port)
737 self.statsd.start()
738 # the statsd client object is configured in the statsd module import
739 reload(statsd)
740 reload(zuul.scheduler)
741
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700742 self.gearman_server = FakeGearmanServer()
743
744 self.config = ConfigParser.ConfigParser()
745 cfg = StringIO()
746 CONFIG.write(cfg)
747 cfg.seek(0)
748 self.config.readfp(cfg)
749 self.config.set('gearman', 'port', str(self.gearman_server.port))
750
Monty Taylorbc758832013-06-17 17:22:42 -0400751 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700752 self.worker.addServer('127.0.0.1', self.gearman_server.port)
753 self.gearman_server.worker = self.worker
754
James E. Blairb0fcae42012-07-17 11:12:10 -0700755 self.sched = zuul.scheduler.Scheduler()
756
James E. Blair8cc15a82012-08-01 11:17:57 -0700757 def URLOpenerFactory(*args, **kw):
758 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400759 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700760
James E. Blair8cc15a82012-08-01 11:17:57 -0700761 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700762 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700763
764 zuul.lib.gerrit.Gerrit = FakeGerrit
765
Monty Taylorbc758832013-06-17 17:22:42 -0400766 self.gerrit = FakeGerritTrigger(
767 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700768 self.gerrit.replication_timeout = 1.5
769 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700770 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400771 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700772
James E. Blair1843a552013-07-03 14:19:52 -0700773 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
774
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700775 self.sched.setLauncher(self.launcher)
James E. Blair6c358e72013-07-29 17:06:47 -0700776 self.sched.registerTrigger(self.gerrit)
James E. Blair63bb0ef2013-07-29 17:14:51 -0700777 self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
778 self.sched.registerTrigger(self.timer)
James E. Blairb0fcae42012-07-17 11:12:10 -0700779
780 self.sched.start()
781 self.sched.reconfigure(self.config)
782 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700783 self.webapp.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700784 self.launcher.gearman.waitForServer()
785 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400786 self.builds = self.worker.running_builds
787 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700788
James E. Blairfee8d652013-06-07 08:57:52 -0700789 self.addCleanup(self.assertFinalState)
790 self.addCleanup(self.shutdown)
791
792 def assertFinalState(self):
793 # Make sure that the change cache is cleared
James E. Blair6c358e72013-07-29 17:06:47 -0700794 self.assertEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700795 self.assertEmptyQueues()
796
797 def shutdown(self):
798 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700799 self.launcher.stop()
800 self.worker.shutdown()
801 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700802 self.gerrit.stop()
James E. Blair63bb0ef2013-07-29 17:14:51 -0700803 self.timer.stop()
James E. Blairb0fcae42012-07-17 11:12:10 -0700804 self.sched.stop()
805 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700806 self.statsd.stop()
807 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700808 self.webapp.stop()
809 self.webapp.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700810 threads = threading.enumerate()
811 if len(threads) > 1:
812 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400813 super(TestScheduler, self).tearDown()
814
815 def init_repo(self, project):
816 parts = project.split('/')
817 path = os.path.join(self.upstream_root, *parts[:-1])
818 if not os.path.exists(path):
819 os.makedirs(path)
820 path = os.path.join(self.upstream_root, project)
821 repo = git.Repo.init(path)
822
823 repo.config_writer().set_value('user', 'email', 'user@example.com')
824 repo.config_writer().set_value('user', 'name', 'User Name')
825 repo.config_writer().write()
826
827 fn = os.path.join(path, 'README')
828 f = open(fn, 'w')
829 f.write("test\n")
830 f.close()
831 repo.index.add([fn])
832 repo.index.commit('initial commit')
833 master = repo.create_head('master')
834 repo.create_tag('init')
835
836 mp = repo.create_head('mp')
837 repo.head.reference = mp
838 f = open(fn, 'a')
839 f.write("test mp\n")
840 f.close()
841 repo.index.add([fn])
842 repo.index.commit('mp commit')
843
844 repo.head.reference = master
845 repo.head.reset(index=True, working_tree=True)
846 repo.git.clean('-x', '-f', '-d')
847
848 def ref_has_change(self, ref, change):
849 path = os.path.join(self.git_root, change.project)
850 repo = git.Repo(path)
851 for commit in repo.iter_commits(ref):
852 if commit.message.strip() == ('%s-1' % change.subject):
853 return True
854 return False
855
856 def job_has_changes(self, *args):
857 job = args[0]
858 commits = args[1:]
859 if isinstance(job, FakeBuild):
860 parameters = job.parameters
861 else:
862 parameters = json.loads(job.arguments)
863 project = parameters['ZUUL_PROJECT']
864 path = os.path.join(self.git_root, project)
865 repo = git.Repo(path)
866 ref = parameters['ZUUL_REF']
867 sha = parameters['ZUUL_COMMIT']
868 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
869 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
870 commit_messages = ['%s-1' % commit.subject for commit in commits]
871 for msg in commit_messages:
872 if msg not in repo_messages:
873 return False
874 if repo_shas[0] != sha:
875 return False
876 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700877
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700878 def registerJobs(self):
879 count = 0
James E. Blaireff88162013-07-01 12:44:14 -0400880 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700881 self.worker.registerFunction('build:' + job)
882 count += 1
883 self.worker.registerFunction('stop:' + self.worker.worker_id)
884 count += 1
885
886 while len(self.gearman_server.functions) < count:
887 time.sleep(0)
888
889 def release(self, job):
890 if isinstance(job, FakeBuild):
891 job.release()
892 else:
893 job.waiting = False
894 self.log.debug("Queued job %s released" % job.unique)
895 self.gearman_server.wakeConnections()
896
897 def getParameter(self, job, name):
898 if isinstance(job, FakeBuild):
899 return job.parameters[name]
900 else:
901 parameters = json.loads(job.arguments)
902 return parameters[name]
903
904 def resetGearmanServer(self):
905 self.worker.setFunctions([])
906 while True:
907 done = True
908 for connection in self.gearman_server.active_connections:
909 if connection.functions:
910 done = False
911 if done:
912 break
913 time.sleep(0)
914 self.gearman_server.functions = set()
915
916 def haveAllBuildsReported(self):
917 # See if Zuul is waiting on a meta job to complete
918 if self.launcher.meta_jobs:
919 return False
920 # Find out if every build that the worker has completed has been
921 # reported back to Zuul. If it hasn't then that means a Gearman
922 # event is still in transit and the system is not stable.
923 for build in self.worker.build_history:
924 zbuild = self.launcher.builds.get(build.uuid)
925 if not zbuild:
926 # It has already been reported
927 continue
928 # It hasn't been reported yet.
929 return False
930 # Make sure that none of the worker connections are in GRAB_WAIT
931 for connection in self.worker.active_connections:
932 if connection.state == 'GRAB_WAIT':
933 return False
934 return True
935
936 def areAllBuildsWaiting(self):
937 ret = True
938
939 builds = self.launcher.builds.values()
940 for build in builds:
941 client_job = None
942 for conn in self.launcher.gearman.active_connections:
943 for j in conn.related_jobs.values():
944 if j.unique == build.uuid:
945 client_job = j
946 break
947 if not client_job:
948 self.log.debug("%s is not known to the gearman client" %
949 build)
950 ret = False
951 continue
952 if not client_job.handle:
953 self.log.debug("%s has no handle" % client_job)
954 ret = False
955 continue
956 server_job = self.gearman_server.jobs.get(client_job.handle)
957 if not server_job:
958 self.log.debug("%s is not known to the gearman server" %
959 client_job)
960 ret = False
961 continue
962 if not hasattr(server_job, 'waiting'):
963 self.log.debug("%s is being enqueued" % server_job)
964 ret = False
965 continue
966 if server_job.waiting:
967 continue
968 worker_job = self.worker.gearman_jobs.get(server_job.unique)
969 if worker_job:
970 if worker_job.build.isWaiting():
971 continue
972 else:
973 self.log.debug("%s is running" % worker_job)
974 ret = False
975 else:
976 self.log.debug("%s is unassigned" % server_job)
977 ret = False
978 return ret
979
James E. Blairb0fcae42012-07-17 11:12:10 -0700980 def waitUntilSettled(self):
981 self.log.debug("Waiting until settled...")
982 start = time.time()
983 while True:
984 if time.time() - start > 10:
985 print 'queue status:',
986 print self.sched.trigger_event_queue.empty(),
987 print self.sched.result_event_queue.empty(),
988 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700989 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -0700990 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700991 # Make sure no new events show up while we're checking
992 self.worker.lock.acquire()
993 # have all build states propogated to zuul?
994 if self.haveAllBuildsReported():
995 # Join ensures that the queue is empty _and_ events have been
996 # processed
997 self.fake_gerrit.event_queue.join()
998 self.sched.trigger_event_queue.join()
999 self.sched.result_event_queue.join()
1000 if (self.sched.trigger_event_queue.empty() and
1001 self.sched.result_event_queue.empty() and
1002 self.fake_gerrit.event_queue.empty() and
1003 self.areAllBuildsWaiting()):
1004 self.worker.lock.release()
1005 self.log.debug("...settled.")
1006 return
1007 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001008 self.sched.wake_event.wait(0.1)
1009
James E. Blaird466dc42012-07-31 10:42:56 -07001010 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -08001011 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -07001012 return len(jobs)
1013
James E. Blair4ca985f2013-05-30 12:27:43 -07001014 def getJobFromHistory(self, name):
1015 history = self.worker.build_history
1016 for job in history:
1017 if job.name == name:
1018 return job
1019 raise Exception("Unable to find job %s in history" % name)
1020
James E. Blaire0487072012-08-29 17:38:31 -07001021 def assertEmptyQueues(self):
1022 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001023 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001024 for queue in pipeline.queues:
1025 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001026 print 'pipeline %s queue %s contents %s' % (
1027 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001028 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001029 if len(queue.severed_heads) != 0:
1030 print 'heads', queue.severed_heads
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001031 self.assertEqual(len(queue.severed_heads), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001032
James E. Blair66eeebf2013-07-27 17:44:32 -07001033 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001034 start = time.time()
1035 while time.time() < (start + 5):
1036 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001037 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001038 k, v = stat.split(':')
1039 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001040 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001041 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001042 elif value:
1043 if value == v:
1044 return
1045 elif kind:
1046 if v.endswith('|' + kind):
1047 return
James E. Blair412e5582013-04-22 15:50:12 -07001048 time.sleep(0.1)
1049
1050 pprint.pprint(self.statsd.stats)
1051 raise Exception("Key %s not found in reported stats" % key)
1052
James E. Blairb0fcae42012-07-17 11:12:10 -07001053 def test_jobs_launched(self):
1054 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001055
James E. Blairb0fcae42012-07-17 11:12:10 -07001056 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001057 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001058 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1059 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001060 self.assertEqual(self.getJobFromHistory('project-merge').result,
1061 'SUCCESS')
1062 self.assertEqual(self.getJobFromHistory('project-test1').result,
1063 'SUCCESS')
1064 self.assertEqual(self.getJobFromHistory('project-test2').result,
1065 'SUCCESS')
1066 self.assertEqual(A.data['status'], 'MERGED')
1067 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001068
James E. Blair66eeebf2013-07-27 17:44:32 -07001069 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1070 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1071 value='1|g')
1072 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1073 kind='ms')
1074 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1075 value='1|c')
1076 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1077 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1078 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001079 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001080 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001081 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001082 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001083
James E. Blair42f74822013-05-14 15:18:03 -07001084 def test_duplicate_pipelines(self):
1085 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001086
James E. Blair42f74822013-05-14 15:18:03 -07001087 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1088 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1089 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001090
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001091 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001092 self.history[0].name == 'project-test1'
1093 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001094
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001095 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001096 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001097 self.assertIn('dup1/project-test1', A.messages[0])
1098 self.assertNotIn('dup2/project-test1', A.messages[0])
1099 self.assertNotIn('dup1/project-test1', A.messages[1])
1100 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001101 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001102 self.assertIn('dup1/project-test1', A.messages[1])
1103 self.assertNotIn('dup2/project-test1', A.messages[1])
1104 self.assertNotIn('dup1/project-test1', A.messages[0])
1105 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001106
James E. Blairb0fcae42012-07-17 11:12:10 -07001107 def test_parallel_changes(self):
1108 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001109
1110 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001111 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1112 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1113 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001114 A.addApproval('CRVW', 2)
1115 B.addApproval('CRVW', 2)
1116 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001117
1118 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1119 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1120 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1121
1122 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001123 self.assertEqual(len(self.builds), 1)
1124 self.assertEqual(self.builds[0].name, 'project-merge')
1125 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001126
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001127 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001128 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001129 self.assertEqual(len(self.builds), 3)
1130 self.assertEqual(self.builds[0].name, 'project-test1')
1131 self.assertTrue(self.job_has_changes(self.builds[0], A))
1132 self.assertEqual(self.builds[1].name, 'project-test2')
1133 self.assertTrue(self.job_has_changes(self.builds[1], A))
1134 self.assertEqual(self.builds[2].name, 'project-merge')
1135 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001136
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001137 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001138 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001139 self.assertEqual(len(self.builds), 5)
1140 self.assertEqual(self.builds[0].name, 'project-test1')
1141 self.assertTrue(self.job_has_changes(self.builds[0], A))
1142 self.assertEqual(self.builds[1].name, 'project-test2')
1143 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001144
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001145 self.assertEqual(self.builds[2].name, 'project-test1')
1146 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1147 self.assertEqual(self.builds[3].name, 'project-test2')
1148 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001149
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001150 self.assertEqual(self.builds[4].name, 'project-merge')
1151 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001152
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001153 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001154 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001155 self.assertEqual(len(self.builds), 6)
1156 self.assertEqual(self.builds[0].name, 'project-test1')
1157 self.assertTrue(self.job_has_changes(self.builds[0], A))
1158 self.assertEqual(self.builds[1].name, 'project-test2')
1159 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001160
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001161 self.assertEqual(self.builds[2].name, 'project-test1')
1162 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1163 self.assertEqual(self.builds[3].name, 'project-test2')
1164 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001165
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001166 self.assertEqual(self.builds[4].name, 'project-test1')
1167 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1168 self.assertEqual(self.builds[5].name, 'project-test2')
1169 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001170
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001171 self.worker.hold_jobs_in_build = False
1172 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001173 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001174 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001175
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001176 self.assertEqual(len(self.history), 9)
1177 self.assertEqual(A.data['status'], 'MERGED')
1178 self.assertEqual(B.data['status'], 'MERGED')
1179 self.assertEqual(C.data['status'], 'MERGED')
1180 self.assertEqual(A.reported, 2)
1181 self.assertEqual(B.reported, 2)
1182 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001183
1184 def test_failed_changes(self):
1185 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001186 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001187
James E. Blairb02a3bb2012-07-30 17:49:55 -07001188 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1189 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001190 A.addApproval('CRVW', 2)
1191 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001192
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001193 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001194
James E. Blaire2819012013-06-28 17:17:26 -04001195 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1196 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001197 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001198
1199 self.worker.release('.*-merge')
1200 self.waitUntilSettled()
1201
1202 self.worker.hold_jobs_in_build = False
1203 self.worker.release()
1204
1205 self.waitUntilSettled()
1206 # It's certain that the merge job for change 2 will run, but
1207 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001208 self.assertTrue(len(self.history) > 6)
1209 self.assertEqual(A.data['status'], 'NEW')
1210 self.assertEqual(B.data['status'], 'MERGED')
1211 self.assertEqual(A.reported, 2)
1212 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001213
1214 def test_independent_queues(self):
1215 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001216
1217 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001218 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001219 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1220 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001221 A.addApproval('CRVW', 2)
1222 B.addApproval('CRVW', 2)
1223 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001224
1225 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1226 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1227 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1228
James E. Blairb02a3bb2012-07-30 17:49:55 -07001229 self.waitUntilSettled()
1230
1231 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001232 self.assertEqual(len(self.builds), 2)
1233 self.assertEqual(self.builds[0].name, 'project-merge')
1234 self.assertTrue(self.job_has_changes(self.builds[0], A))
1235 self.assertEqual(self.builds[1].name, 'project1-merge')
1236 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001237
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001238 # Release the current merge builds
1239 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001240 self.waitUntilSettled()
1241 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001242 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001243 self.waitUntilSettled()
1244
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001245 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001246 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001247 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001248
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001249 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001250 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001251 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001252
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001253 self.assertEqual(len(self.history), 11)
1254 self.assertEqual(A.data['status'], 'MERGED')
1255 self.assertEqual(B.data['status'], 'MERGED')
1256 self.assertEqual(C.data['status'], 'MERGED')
1257 self.assertEqual(A.reported, 2)
1258 self.assertEqual(B.reported, 2)
1259 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001260
1261 def test_failed_change_at_head(self):
1262 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001263
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001264 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001265 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1266 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1267 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001268 A.addApproval('CRVW', 2)
1269 B.addApproval('CRVW', 2)
1270 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001271
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001272 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001273
1274 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1275 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1276 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1277
1278 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001279
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001280 self.assertEqual(len(self.builds), 1)
1281 self.assertEqual(self.builds[0].name, 'project-merge')
1282 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001283
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001284 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001285 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001286 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001287 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001288 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001289 self.waitUntilSettled()
1290
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001291 self.assertEqual(len(self.builds), 6)
1292 self.assertEqual(self.builds[0].name, 'project-test1')
1293 self.assertEqual(self.builds[1].name, 'project-test2')
1294 self.assertEqual(self.builds[2].name, 'project-test1')
1295 self.assertEqual(self.builds[3].name, 'project-test2')
1296 self.assertEqual(self.builds[4].name, 'project-test1')
1297 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001298
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001299 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001300 self.waitUntilSettled()
1301
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001302 # project-test2, project-merge for B
1303 self.assertEqual(len(self.builds), 2)
1304 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001305
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001306 self.worker.hold_jobs_in_build = False
1307 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001308 self.waitUntilSettled()
1309
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001310 self.assertEqual(len(self.builds), 0)
1311 self.assertEqual(len(self.history), 15)
1312 self.assertEqual(A.data['status'], 'NEW')
1313 self.assertEqual(B.data['status'], 'MERGED')
1314 self.assertEqual(C.data['status'], 'MERGED')
1315 self.assertEqual(A.reported, 2)
1316 self.assertEqual(B.reported, 2)
1317 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001318
James E. Blair0aac4872013-08-23 14:02:38 -07001319 def test_failed_change_in_middle(self):
1320 "Test a failed change in the middle of the queue"
1321
1322 self.worker.hold_jobs_in_build = True
1323 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1324 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1325 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1326 A.addApproval('CRVW', 2)
1327 B.addApproval('CRVW', 2)
1328 C.addApproval('CRVW', 2)
1329
1330 self.worker.addFailTest('project-test1', B)
1331
1332 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1333 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1334 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1335
1336 self.waitUntilSettled()
1337
1338 self.worker.release('.*-merge')
1339 self.waitUntilSettled()
1340 self.worker.release('.*-merge')
1341 self.waitUntilSettled()
1342 self.worker.release('.*-merge')
1343 self.waitUntilSettled()
1344
1345 self.assertEqual(len(self.builds), 6)
1346 self.assertEqual(self.builds[0].name, 'project-test1')
1347 self.assertEqual(self.builds[1].name, 'project-test2')
1348 self.assertEqual(self.builds[2].name, 'project-test1')
1349 self.assertEqual(self.builds[3].name, 'project-test2')
1350 self.assertEqual(self.builds[4].name, 'project-test1')
1351 self.assertEqual(self.builds[5].name, 'project-test2')
1352
1353 self.release(self.builds[2])
1354 self.waitUntilSettled()
1355
1356 # project-test1 and project-test2 for A, project-test2 for B
1357 self.assertEqual(len(self.builds), 3)
1358 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1359
1360 # check that build status of aborted jobs are masked ('CANCELED')
1361 items = self.sched.layout.pipelines['gate'].getAllItems()
1362 builds = items[0].current_build_set.getBuilds()
1363 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1364 self.assertEqual(self.countJobResults(builds, None), 2)
1365 builds = items[1].current_build_set.getBuilds()
1366 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1367 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1368 self.assertEqual(self.countJobResults(builds, None), 1)
1369 builds = items[2].current_build_set.getBuilds()
1370 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1371 self.assertEqual(self.countJobResults(builds, 'CANCELED'), 2)
1372
1373 self.worker.hold_jobs_in_build = False
1374 self.worker.release()
1375 self.waitUntilSettled()
1376
1377 self.assertEqual(len(self.builds), 0)
1378 self.assertEqual(len(self.history), 12)
1379 self.assertEqual(A.data['status'], 'MERGED')
1380 self.assertEqual(B.data['status'], 'NEW')
1381 self.assertEqual(C.data['status'], 'MERGED')
1382 self.assertEqual(A.reported, 2)
1383 self.assertEqual(B.reported, 2)
1384 self.assertEqual(C.reported, 2)
1385
James E. Blaird466dc42012-07-31 10:42:56 -07001386 def test_failed_change_at_head_with_queue(self):
1387 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001388
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001389 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001390 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1391 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1392 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001393 A.addApproval('CRVW', 2)
1394 B.addApproval('CRVW', 2)
1395 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001396
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001397 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001398
1399 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1400 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1401 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1402
1403 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001404 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001405 self.assertEqual(len(self.builds), 0)
1406 self.assertEqual(len(queue), 1)
1407 self.assertEqual(queue[0].name, 'build:project-merge')
1408 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001409
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001410 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001411 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001412 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001413 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001414 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001415 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001416 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001417
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001418 self.assertEqual(len(self.builds), 0)
1419 self.assertEqual(len(queue), 6)
1420 self.assertEqual(queue[0].name, 'build:project-test1')
1421 self.assertEqual(queue[1].name, 'build:project-test2')
1422 self.assertEqual(queue[2].name, 'build:project-test1')
1423 self.assertEqual(queue[3].name, 'build:project-test2')
1424 self.assertEqual(queue[4].name, 'build:project-test1')
1425 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001426
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001427 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001428 self.waitUntilSettled()
1429
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001430 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001431 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001432 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1433 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001434
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001435 self.gearman_server.hold_jobs_in_queue = False
1436 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001437 self.waitUntilSettled()
1438
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001439 self.assertEqual(len(self.builds), 0)
1440 self.assertEqual(len(self.history), 11)
1441 self.assertEqual(A.data['status'], 'NEW')
1442 self.assertEqual(B.data['status'], 'MERGED')
1443 self.assertEqual(C.data['status'], 'MERGED')
1444 self.assertEqual(A.reported, 2)
1445 self.assertEqual(B.reported, 2)
1446 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001447
1448 def test_patch_order(self):
1449 "Test that dependent patches are tested in the right order"
1450 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1451 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1452 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1453 A.addApproval('CRVW', 2)
1454 B.addApproval('CRVW', 2)
1455 C.addApproval('CRVW', 2)
1456
1457 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1458 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1459 M2.setMerged()
1460 M1.setMerged()
1461
1462 # C -> B -> A -> M1 -> M2
1463 # M2 is here to make sure it is never queried. If it is, it
1464 # means zuul is walking down the entire history of merged
1465 # changes.
1466
1467 C.setDependsOn(B, 1)
1468 B.setDependsOn(A, 1)
1469 A.setDependsOn(M1, 1)
1470 M1.setDependsOn(M2, 1)
1471
1472 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1473
1474 self.waitUntilSettled()
1475
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001476 self.assertEqual(A.data['status'], 'NEW')
1477 self.assertEqual(B.data['status'], 'NEW')
1478 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001479
1480 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1481 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1482
1483 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001484 self.assertEqual(M2.queried, 0)
1485 self.assertEqual(A.data['status'], 'MERGED')
1486 self.assertEqual(B.data['status'], 'MERGED')
1487 self.assertEqual(C.data['status'], 'MERGED')
1488 self.assertEqual(A.reported, 2)
1489 self.assertEqual(B.reported, 2)
1490 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001491
James E. Blair0e933c52013-07-11 10:18:52 -07001492 def test_trigger_cache(self):
1493 "Test that the trigger cache operates correctly"
1494 self.worker.hold_jobs_in_build = True
1495
1496 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1497 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1498 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1499 A.addApproval('CRVW', 2)
1500 B.addApproval('CRVW', 2)
1501
1502 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1503 M1.setMerged()
1504
1505 B.setDependsOn(A, 1)
1506 A.setDependsOn(M1, 1)
1507
1508 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1509 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1510
1511 self.waitUntilSettled()
1512
1513 for build in self.builds:
1514 if build.parameters['ZUUL_PIPELINE'] == 'check':
1515 build.release()
1516 self.waitUntilSettled()
1517 for build in self.builds:
1518 if build.parameters['ZUUL_PIPELINE'] == 'check':
1519 build.release()
1520 self.waitUntilSettled()
1521
1522 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1523 self.waitUntilSettled()
1524
James E. Blair6c358e72013-07-29 17:06:47 -07001525 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001526 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001527 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001528
1529 self.worker.hold_jobs_in_build = False
1530 self.worker.release()
1531 self.waitUntilSettled()
1532
1533 self.assertEqual(A.data['status'], 'MERGED')
1534 self.assertEqual(B.data['status'], 'MERGED')
1535 self.assertEqual(A.queried, 2) # Initial and isMerged
1536 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1537
James E. Blair8c803f82012-07-31 16:25:42 -07001538 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001539 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001540 # TODO: move to test_gerrit (this is a unit test!)
1541 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001542 trigger = self.sched.layout.pipelines['gate'].trigger
1543 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001544 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001545 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001546
1547 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001548 a = trigger.getChange(1, 2, refresh=True)
1549 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001550
1551 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001552 a = trigger.getChange(1, 2, refresh=True)
1553 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1554 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001555
1556 def test_build_configuration(self):
1557 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001558
1559 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001560 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1561 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1562 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1563 A.addApproval('CRVW', 2)
1564 B.addApproval('CRVW', 2)
1565 C.addApproval('CRVW', 2)
1566 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1567 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1568 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1569 self.waitUntilSettled()
1570
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001571 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001572 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001573 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001574 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001575 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001576 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001577 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001578 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1579 self.gearman_server.hold_jobs_in_queue = False
1580 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001581 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001582
Monty Taylorbc758832013-06-17 17:22:42 -04001583 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001584 repo = git.Repo(path)
1585 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1586 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001587 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001588 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001589
1590 def test_build_configuration_conflict(self):
1591 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001592
1593 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001594 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1595 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001596 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001597 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1598 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001599 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001600 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1601 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001602 A.addApproval('CRVW', 2)
1603 B.addApproval('CRVW', 2)
1604 C.addApproval('CRVW', 2)
1605 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1606 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1607 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1608 self.waitUntilSettled()
1609
James E. Blair6736beb2013-07-11 15:18:15 -07001610 self.assertEqual(A.reported, 1)
1611 self.assertEqual(B.reported, 1)
1612 self.assertEqual(C.reported, 1)
1613
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001614 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001615 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001616 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001617 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001618 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001619 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001620 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001621 self.getParameter(queue[-1], 'ZUUL_REF')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001622 self.gearman_server.hold_jobs_in_queue = False
1623 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001624 self.waitUntilSettled()
1625
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001626 self.assertEqual(A.data['status'], 'MERGED')
1627 self.assertEqual(B.data['status'], 'NEW')
1628 self.assertEqual(C.data['status'], 'MERGED')
1629 self.assertEqual(A.reported, 2)
1630 self.assertEqual(B.reported, 2)
1631 self.assertEqual(C.reported, 2)
James E. Blairdaabed22012-08-15 15:38:57 -07001632
James E. Blair6736beb2013-07-11 15:18:15 -07001633 def test_dequeue_conflict(self):
1634 "Test that the option to dequeue merge conflicts works"
1635
1636 self.gearman_server.hold_jobs_in_queue = True
1637 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1638 A.addPatchset(['conflict'])
1639 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1640 B.addPatchset(['conflict'])
1641 A.addApproval('CRVW', 2)
1642 B.addApproval('CRVW', 2)
1643 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1644 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1645 self.waitUntilSettled()
1646
1647 self.assertEqual(A.reported, 1)
1648 self.assertEqual(B.reported, 2)
1649
1650 self.gearman_server.hold_jobs_in_queue = False
1651 self.gearman_server.release()
1652 self.waitUntilSettled()
1653
1654 self.assertEqual(A.data['status'], 'MERGED')
1655 self.assertEqual(B.data['status'], 'NEW')
1656 self.assertEqual(A.reported, 2)
1657 self.assertEqual(B.reported, 2)
1658
James E. Blairdaabed22012-08-15 15:38:57 -07001659 def test_post(self):
1660 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001661
Zhongyue Luo5d556072012-09-21 02:00:47 +09001662 e = {
1663 "type": "ref-updated",
1664 "submitter": {
1665 "name": "User Name",
1666 },
1667 "refUpdate": {
1668 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1669 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1670 "refName": "master",
1671 "project": "org/project",
1672 }
1673 }
James E. Blairdaabed22012-08-15 15:38:57 -07001674 self.fake_gerrit.addEvent(e)
1675 self.waitUntilSettled()
1676
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001677 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001678 self.assertEqual(len(self.history), 1)
1679 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001680
1681 def test_build_configuration_branch(self):
1682 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001683
1684 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001685 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1686 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1687 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1688 A.addApproval('CRVW', 2)
1689 B.addApproval('CRVW', 2)
1690 C.addApproval('CRVW', 2)
1691 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1692 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1693 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1694 self.waitUntilSettled()
1695
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001696 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001697 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001698 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001699 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001700 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001701 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001702 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001703 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1704 self.gearman_server.hold_jobs_in_queue = False
1705 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001706 self.waitUntilSettled()
1707
Monty Taylorbc758832013-06-17 17:22:42 -04001708 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001709 repo = git.Repo(path)
1710 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1711 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001712 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001713 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001714
1715 def test_build_configuration_branch_interaction(self):
1716 "Test that switching between branches works"
1717 self.test_build_configuration()
1718 self.test_build_configuration_branch()
1719 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001720 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001721 repo = git.Repo(path)
1722 repo.heads.master.commit = repo.commit('init')
1723 self.test_build_configuration()
1724
1725 def test_build_configuration_multi_branch(self):
1726 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001727
1728 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001729 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1730 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1731 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1732 A.addApproval('CRVW', 2)
1733 B.addApproval('CRVW', 2)
1734 C.addApproval('CRVW', 2)
1735 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1736 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1737 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1738 self.waitUntilSettled()
1739
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001740 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001741 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001742 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001743 job_B = None
1744 for job in queue:
1745 if 'project-merge' in job.name:
1746 job_B = job
1747 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001748 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001749 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001750 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001751 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001752 for job in queue:
1753 if 'project-merge' in job.name:
1754 job_C = job
1755 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001756 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001757 self.gearman_server.hold_jobs_in_queue = False
1758 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001759 self.waitUntilSettled()
1760
Monty Taylorbc758832013-06-17 17:22:42 -04001761 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001762 repo = git.Repo(path)
1763
1764 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001765 for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001766 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001767 correct_messages = ['initial commit', 'A-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001768 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001769
1770 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001771 for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001772 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001773 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001774 self.assertEqual(repo_messages, correct_messages)
James E. Blair7f71c802012-08-22 13:04:32 -07001775
1776 def test_one_job_project(self):
1777 "Test that queueing works with one job"
1778 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1779 'master', 'A')
1780 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1781 'master', 'B')
1782 A.addApproval('CRVW', 2)
1783 B.addApproval('CRVW', 2)
1784 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1785 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1786 self.waitUntilSettled()
1787
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001788 self.assertEqual(A.data['status'], 'MERGED')
1789 self.assertEqual(A.reported, 2)
1790 self.assertEqual(B.data['status'], 'MERGED')
1791 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001792
Antoine Musso80edd5a2013-02-13 15:37:53 +01001793 def test_job_from_templates_launched(self):
1794 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001795
Antoine Musso80edd5a2013-02-13 15:37:53 +01001796 A = self.fake_gerrit.addFakeChange(
1797 'org/templated-project', 'master', 'A')
1798 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1799 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001800
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001801 self.assertEqual(self.getJobFromHistory('project-test1').result,
1802 'SUCCESS')
1803 self.assertEqual(self.getJobFromHistory('project-test2').result,
1804 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001805
James E. Blaircaec0c52012-08-22 14:52:22 -07001806 def test_dependent_changes_dequeue(self):
1807 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001808
James E. Blaircaec0c52012-08-22 14:52:22 -07001809 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1810 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1811 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1812 A.addApproval('CRVW', 2)
1813 B.addApproval('CRVW', 2)
1814 C.addApproval('CRVW', 2)
1815
1816 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1817 M1.setMerged()
1818
1819 # C -> B -> A -> M1
1820
1821 C.setDependsOn(B, 1)
1822 B.setDependsOn(A, 1)
1823 A.setDependsOn(M1, 1)
1824
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001825 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001826
1827 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1828 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1829 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1830
1831 self.waitUntilSettled()
1832
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001833 self.assertEqual(A.data['status'], 'NEW')
1834 self.assertEqual(A.reported, 2)
1835 self.assertEqual(B.data['status'], 'NEW')
1836 self.assertEqual(B.reported, 2)
1837 self.assertEqual(C.data['status'], 'NEW')
1838 self.assertEqual(C.reported, 2)
1839 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001840
1841 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001842 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001843 # If it's dequeued more than once, we should see extra
1844 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001845
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001846 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001847 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1848 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1849 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1850 A.addApproval('CRVW', 2)
1851 B.addApproval('CRVW', 2)
1852 C.addApproval('CRVW', 2)
1853
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001854 self.worker.addFailTest('project1-test1', A)
1855 self.worker.addFailTest('project1-test2', A)
1856 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001857
1858 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1859 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1860 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1861
1862 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001863
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001864 self.assertEqual(len(self.builds), 1)
1865 self.assertEqual(self.builds[0].name, 'project1-merge')
1866 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07001867
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001868 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001869 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001870 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001871 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001872 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001873 self.waitUntilSettled()
1874
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001875 self.assertEqual(len(self.builds), 9)
1876 self.assertEqual(self.builds[0].name, 'project1-test1')
1877 self.assertEqual(self.builds[1].name, 'project1-test2')
1878 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
1879 self.assertEqual(self.builds[3].name, 'project1-test1')
1880 self.assertEqual(self.builds[4].name, 'project1-test2')
1881 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
1882 self.assertEqual(self.builds[6].name, 'project1-test1')
1883 self.assertEqual(self.builds[7].name, 'project1-test2')
1884 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001885
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001886 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001887 self.waitUntilSettled()
1888
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001889 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1890 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001891
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001892 self.worker.hold_jobs_in_build = False
1893 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001894 self.waitUntilSettled()
1895
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001896 self.assertEqual(len(self.builds), 0)
1897 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001898
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001899 self.assertEqual(A.data['status'], 'NEW')
1900 self.assertEqual(B.data['status'], 'MERGED')
1901 self.assertEqual(C.data['status'], 'MERGED')
1902 self.assertEqual(A.reported, 2)
1903 self.assertEqual(B.reported, 2)
1904 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001905
1906 def test_nonvoting_job(self):
1907 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001908
James E. Blair4ec821f2012-08-23 15:28:28 -07001909 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1910 'master', 'A')
1911 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001912 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001913 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1914
1915 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001916
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001917 self.assertEqual(A.data['status'], 'MERGED')
1918 self.assertEqual(A.reported, 2)
1919 self.assertEqual(
1920 self.getJobFromHistory('nonvoting-project-merge').result,
1921 'SUCCESS')
1922 self.assertEqual(
1923 self.getJobFromHistory('nonvoting-project-test1').result,
1924 'SUCCESS')
1925 self.assertEqual(
1926 self.getJobFromHistory('nonvoting-project-test2').result,
1927 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001928
1929 def test_check_queue_success(self):
1930 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001931
James E. Blaire0487072012-08-29 17:38:31 -07001932 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1933 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1934
1935 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001936
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001937 self.assertEqual(A.data['status'], 'NEW')
1938 self.assertEqual(A.reported, 1)
1939 self.assertEqual(self.getJobFromHistory('project-merge').result,
1940 'SUCCESS')
1941 self.assertEqual(self.getJobFromHistory('project-test1').result,
1942 'SUCCESS')
1943 self.assertEqual(self.getJobFromHistory('project-test2').result,
1944 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001945
1946 def test_check_queue_failure(self):
1947 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001948
James E. Blaire0487072012-08-29 17:38:31 -07001949 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001950 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001951 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1952
1953 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001954
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001955 self.assertEqual(A.data['status'], 'NEW')
1956 self.assertEqual(A.reported, 1)
1957 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001958 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001959 self.assertEqual(self.getJobFromHistory('project-test1').result,
1960 'SUCCESS')
1961 self.assertEqual(self.getJobFromHistory('project-test2').result,
1962 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001963
1964 def test_dependent_behind_dequeue(self):
1965 "test that dependent changes behind dequeued changes work"
1966 # This complicated test is a reproduction of a real life bug
1967 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001968
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001969 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001970 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1971 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1972 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1973 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1974 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1975 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1976 D.setDependsOn(C, 1)
1977 E.setDependsOn(D, 1)
1978 A.addApproval('CRVW', 2)
1979 B.addApproval('CRVW', 2)
1980 C.addApproval('CRVW', 2)
1981 D.addApproval('CRVW', 2)
1982 E.addApproval('CRVW', 2)
1983 F.addApproval('CRVW', 2)
1984
1985 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001986
1987 # Change object re-use in the gerrit trigger is hidden if
1988 # changes are added in quick succession; waiting makes it more
1989 # like real life.
1990 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1991 self.waitUntilSettled()
1992 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1993 self.waitUntilSettled()
1994
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001995 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001996 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001997 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001998 self.waitUntilSettled()
1999
2000 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2001 self.waitUntilSettled()
2002 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2003 self.waitUntilSettled()
2004 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2005 self.waitUntilSettled()
2006 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
2007 self.waitUntilSettled()
2008
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002009 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002010 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002011 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002012 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002013 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002014 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002015 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002016 self.waitUntilSettled()
2017
2018 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002019
2020 # Grab pointers to the jobs we want to release before
2021 # releasing any, because list indexes may change as
2022 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002023 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002024 a.release()
2025 b.release()
2026 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002027 self.waitUntilSettled()
2028
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002029 self.worker.hold_jobs_in_build = False
2030 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002031 self.waitUntilSettled()
2032
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002033 self.assertEqual(A.data['status'], 'NEW')
2034 self.assertEqual(B.data['status'], 'MERGED')
2035 self.assertEqual(C.data['status'], 'MERGED')
2036 self.assertEqual(D.data['status'], 'MERGED')
2037 self.assertEqual(E.data['status'], 'MERGED')
2038 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002039
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002040 self.assertEqual(A.reported, 2)
2041 self.assertEqual(B.reported, 2)
2042 self.assertEqual(C.reported, 2)
2043 self.assertEqual(D.reported, 2)
2044 self.assertEqual(E.reported, 2)
2045 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002046
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002047 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2048 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002049
2050 def test_merger_repack(self):
2051 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002052
James E. Blair05fed602012-09-07 12:45:24 -07002053 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2054 A.addApproval('CRVW', 2)
2055 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2056 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002057 self.assertEqual(self.getJobFromHistory('project-merge').result,
2058 'SUCCESS')
2059 self.assertEqual(self.getJobFromHistory('project-test1').result,
2060 'SUCCESS')
2061 self.assertEqual(self.getJobFromHistory('project-test2').result,
2062 'SUCCESS')
2063 self.assertEqual(A.data['status'], 'MERGED')
2064 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07002065 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002066 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002067
Monty Taylorbc758832013-06-17 17:22:42 -04002068 path = os.path.join(self.git_root, "org/project")
2069 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002070
2071 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2072 A.addApproval('CRVW', 2)
2073 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2074 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002075 self.assertEqual(self.getJobFromHistory('project-merge').result,
2076 'SUCCESS')
2077 self.assertEqual(self.getJobFromHistory('project-test1').result,
2078 'SUCCESS')
2079 self.assertEqual(self.getJobFromHistory('project-test2').result,
2080 'SUCCESS')
2081 self.assertEqual(A.data['status'], 'MERGED')
2082 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002083
James E. Blair4886f282012-11-15 09:27:33 -08002084 def test_merger_repack_large_change(self):
2085 "Test that the merger works with large changes after a repack"
2086 # https://bugs.launchpad.net/zuul/+bug/1078946
2087 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2088 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002089 path = os.path.join(self.upstream_root, "org/project1")
2090 print repack_repo(path)
2091 path = os.path.join(self.git_root, "org/project1")
2092 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002093
2094 A.addApproval('CRVW', 2)
2095 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2096 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002097 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2098 'SUCCESS')
2099 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2100 'SUCCESS')
2101 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2102 'SUCCESS')
2103 self.assertEqual(A.data['status'], 'MERGED')
2104 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002105
James E. Blair7ee88a22012-09-12 18:59:31 +02002106 def test_nonexistent_job(self):
2107 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002108 # Set to the state immediately after a restart
2109 self.resetGearmanServer()
2110 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002111
2112 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2113 A.addApproval('CRVW', 2)
2114 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2115 # There may be a thread about to report a lost change
2116 while A.reported < 2:
2117 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002118 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002119 self.assertFalse(job_names)
2120 self.assertEqual(A.data['status'], 'NEW')
2121 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002122 self.assertEmptyQueues()
2123
2124 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002125 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002126 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2127 A.addApproval('CRVW', 2)
2128 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2129 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002130 self.assertEqual(self.getJobFromHistory('project-merge').result,
2131 'SUCCESS')
2132 self.assertEqual(self.getJobFromHistory('project-test1').result,
2133 'SUCCESS')
2134 self.assertEqual(self.getJobFromHistory('project-test2').result,
2135 'SUCCESS')
2136 self.assertEqual(A.data['status'], 'MERGED')
2137 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002138
2139 def test_single_nonexistent_post_job(self):
2140 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002141 e = {
2142 "type": "ref-updated",
2143 "submitter": {
2144 "name": "User Name",
2145 },
2146 "refUpdate": {
2147 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2148 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2149 "refName": "master",
2150 "project": "org/project",
2151 }
2152 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002153 # Set to the state immediately after a restart
2154 self.resetGearmanServer()
2155 self.launcher.negative_function_cache_ttl = 0
2156
James E. Blairf62d4282012-12-31 17:01:50 -08002157 self.fake_gerrit.addEvent(e)
2158 self.waitUntilSettled()
2159
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002160 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002161
2162 def test_new_patchset_dequeues_old(self):
2163 "Test that a new patchset causes the old to be dequeued"
2164 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002165 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002166 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2167 M.setMerged()
2168
2169 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2170 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2171 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2172 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2173 A.addApproval('CRVW', 2)
2174 B.addApproval('CRVW', 2)
2175 C.addApproval('CRVW', 2)
2176 D.addApproval('CRVW', 2)
2177
2178 C.setDependsOn(B, 1)
2179 B.setDependsOn(A, 1)
2180 A.setDependsOn(M, 1)
2181
2182 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2183 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2184 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2185 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2186 self.waitUntilSettled()
2187
2188 B.addPatchset()
2189 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2190 self.waitUntilSettled()
2191
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002192 self.worker.hold_jobs_in_build = False
2193 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002194 self.waitUntilSettled()
2195
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002196 self.assertEqual(A.data['status'], 'MERGED')
2197 self.assertEqual(A.reported, 2)
2198 self.assertEqual(B.data['status'], 'NEW')
2199 self.assertEqual(B.reported, 2)
2200 self.assertEqual(C.data['status'], 'NEW')
2201 self.assertEqual(C.reported, 2)
2202 self.assertEqual(D.data['status'], 'MERGED')
2203 self.assertEqual(D.reported, 2)
2204 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002205
2206 def test_new_patchset_dequeues_old_on_head(self):
2207 "Test that a new patchset causes the old to be dequeued (at head)"
2208 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002209 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002210 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2211 M.setMerged()
2212 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2213 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2214 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2215 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2216 A.addApproval('CRVW', 2)
2217 B.addApproval('CRVW', 2)
2218 C.addApproval('CRVW', 2)
2219 D.addApproval('CRVW', 2)
2220
2221 C.setDependsOn(B, 1)
2222 B.setDependsOn(A, 1)
2223 A.setDependsOn(M, 1)
2224
2225 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2226 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2227 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2228 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2229 self.waitUntilSettled()
2230
2231 A.addPatchset()
2232 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2233 self.waitUntilSettled()
2234
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002235 self.worker.hold_jobs_in_build = False
2236 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002237 self.waitUntilSettled()
2238
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002239 self.assertEqual(A.data['status'], 'NEW')
2240 self.assertEqual(A.reported, 2)
2241 self.assertEqual(B.data['status'], 'NEW')
2242 self.assertEqual(B.reported, 2)
2243 self.assertEqual(C.data['status'], 'NEW')
2244 self.assertEqual(C.reported, 2)
2245 self.assertEqual(D.data['status'], 'MERGED')
2246 self.assertEqual(D.reported, 2)
2247 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002248
2249 def test_new_patchset_dequeues_old_without_dependents(self):
2250 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002251 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002252 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2253 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2254 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2255 A.addApproval('CRVW', 2)
2256 B.addApproval('CRVW', 2)
2257 C.addApproval('CRVW', 2)
2258
2259 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2260 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2261 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2262 self.waitUntilSettled()
2263
2264 B.addPatchset()
2265 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2266 self.waitUntilSettled()
2267
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002268 self.worker.hold_jobs_in_build = False
2269 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002270 self.waitUntilSettled()
2271
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002272 self.assertEqual(A.data['status'], 'MERGED')
2273 self.assertEqual(A.reported, 2)
2274 self.assertEqual(B.data['status'], 'NEW')
2275 self.assertEqual(B.reported, 2)
2276 self.assertEqual(C.data['status'], 'MERGED')
2277 self.assertEqual(C.reported, 2)
2278 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002279
2280 def test_new_patchset_dequeues_old_independent_queue(self):
2281 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002282 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002283 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2284 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2285 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2286 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2287 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2288 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2289 self.waitUntilSettled()
2290
2291 B.addPatchset()
2292 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2293 self.waitUntilSettled()
2294
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002295 self.worker.hold_jobs_in_build = False
2296 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002297 self.waitUntilSettled()
2298
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002299 self.assertEqual(A.data['status'], 'NEW')
2300 self.assertEqual(A.reported, 1)
2301 self.assertEqual(B.data['status'], 'NEW')
2302 self.assertEqual(B.reported, 1)
2303 self.assertEqual(C.data['status'], 'NEW')
2304 self.assertEqual(C.reported, 1)
2305 self.assertEqual(len(self.history), 10)
2306 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002307
2308 def test_zuul_refs(self):
2309 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002310 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002311 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2312 M1.setMerged()
2313 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2314 M2.setMerged()
2315
2316 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2317 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2318 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2319 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2320 A.addApproval('CRVW', 2)
2321 B.addApproval('CRVW', 2)
2322 C.addApproval('CRVW', 2)
2323 D.addApproval('CRVW', 2)
2324 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2325 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2326 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2327 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2328
2329 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002330 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002331 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002332 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002333 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002334 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002335 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002336 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002337 self.waitUntilSettled()
2338
James E. Blair7d0dedc2013-02-21 17:26:09 -08002339 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002340 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002341 if x.parameters['ZUUL_CHANGE'] == '3':
2342 a_zref = x.parameters['ZUUL_REF']
2343 if x.parameters['ZUUL_CHANGE'] == '4':
2344 b_zref = x.parameters['ZUUL_REF']
2345 if x.parameters['ZUUL_CHANGE'] == '5':
2346 c_zref = x.parameters['ZUUL_REF']
2347 if x.parameters['ZUUL_CHANGE'] == '6':
2348 d_zref = x.parameters['ZUUL_REF']
2349
2350 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002351 self.assertIsNotNone(a_zref)
2352 self.assertIsNotNone(b_zref)
2353 self.assertIsNotNone(c_zref)
2354 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002355
2356 # And they should all be different
2357 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002358 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002359
2360 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002361 self.assertTrue(self.ref_has_change(a_zref, A))
2362 self.assertFalse(self.ref_has_change(a_zref, B))
2363 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002364
2365 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002366 self.assertTrue(self.ref_has_change(b_zref, A))
2367 self.assertTrue(self.ref_has_change(b_zref, B))
2368 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002369
2370 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002371 self.assertTrue(self.ref_has_change(c_zref, A))
2372 self.assertTrue(self.ref_has_change(c_zref, B))
2373 self.assertTrue(self.ref_has_change(c_zref, C))
2374 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002375
2376 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002377 self.assertTrue(self.ref_has_change(d_zref, A))
2378 self.assertTrue(self.ref_has_change(d_zref, B))
2379 self.assertTrue(self.ref_has_change(d_zref, C))
2380 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002381
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002382 self.worker.hold_jobs_in_build = False
2383 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002384 self.waitUntilSettled()
2385
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002386 self.assertEqual(A.data['status'], 'MERGED')
2387 self.assertEqual(A.reported, 2)
2388 self.assertEqual(B.data['status'], 'MERGED')
2389 self.assertEqual(B.reported, 2)
2390 self.assertEqual(C.data['status'], 'MERGED')
2391 self.assertEqual(C.reported, 2)
2392 self.assertEqual(D.data['status'], 'MERGED')
2393 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002394
James E. Blair4a28a882013-08-23 15:17:33 -07002395 def test_rerun_on_error(self):
2396 "Test that if a worker fails to run a job, it is run again"
2397 self.worker.hold_jobs_in_build = True
2398 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2399 A.addApproval('CRVW', 2)
2400 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2401 self.waitUntilSettled()
2402
2403 self.builds[0].run_error = True
2404 self.worker.hold_jobs_in_build = False
2405 self.worker.release()
2406 self.waitUntilSettled()
2407 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2408 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2409
James E. Blair412e5582013-04-22 15:50:12 -07002410 def test_statsd(self):
2411 "Test each of the statsd methods used in the scheduler"
2412 import extras
2413 statsd = extras.try_import('statsd.statsd')
2414 statsd.incr('test-incr')
2415 statsd.timing('test-timing', 3)
2416 statsd.gauge('test-guage', 12)
2417 self.assertReportedStat('test-incr', '1|c')
2418 self.assertReportedStat('test-timing', '3|ms')
2419 self.assertReportedStat('test-guage', '12|g')
2420
James E. Blair70c71582013-03-06 08:50:50 -08002421 def test_file_jobs(self):
2422 "Test that file jobs run only when appropriate"
2423 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2424 A.addPatchset(['pip-requires'])
2425 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2426 A.addApproval('CRVW', 2)
2427 B.addApproval('CRVW', 2)
2428 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2429 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2430 self.waitUntilSettled()
2431
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002432 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002433 if x.name == 'project-testfile']
2434
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002435 self.assertEqual(len(testfile_jobs), 1)
2436 self.assertEqual(testfile_jobs[0].changes, '1,2')
2437 self.assertEqual(A.data['status'], 'MERGED')
2438 self.assertEqual(A.reported, 2)
2439 self.assertEqual(B.data['status'], 'MERGED')
2440 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002441
2442 def test_test_config(self):
2443 "Test that we can test the config"
2444 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002445 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002446 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002447 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002448
2449 def test_build_description(self):
2450 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002451 self.worker.registerFunction('set_description:' +
2452 self.worker.worker_id)
2453
2454 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2455 A.addApproval('CRVW', 2)
2456 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2457 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002458 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002459 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002460 self.assertTrue(re.search("Branch.*master", desc))
2461 self.assertTrue(re.search("Pipeline.*gate", desc))
2462 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2463 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2464 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2465 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002466
James E. Blair64ed6f22013-07-10 14:07:23 -07002467 def test_queue_precedence(self):
2468 "Test that queue precedence works"
2469
2470 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002471 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002472 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2473 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2474 A.addApproval('CRVW', 2)
2475 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2476
2477 self.waitUntilSettled()
2478 self.gearman_server.hold_jobs_in_queue = False
2479 self.gearman_server.release()
2480 self.waitUntilSettled()
2481
James E. Blair8de58bd2013-07-18 16:23:33 -07002482 # Run one build at a time to ensure non-race order:
2483 for x in range(6):
2484 self.release(self.builds[0])
2485 self.waitUntilSettled()
2486 self.worker.hold_jobs_in_build = False
2487 self.waitUntilSettled()
2488
James E. Blair64ed6f22013-07-10 14:07:23 -07002489 self.log.debug(self.history)
2490 self.assertEqual(self.history[0].pipeline, 'gate')
2491 self.assertEqual(self.history[1].pipeline, 'check')
2492 self.assertEqual(self.history[2].pipeline, 'gate')
2493 self.assertEqual(self.history[3].pipeline, 'gate')
2494 self.assertEqual(self.history[4].pipeline, 'check')
2495 self.assertEqual(self.history[5].pipeline, 'check')
2496
James E. Blair1843a552013-07-03 14:19:52 -07002497 def test_json_status(self):
2498 "Test that we can retrieve JSON status info"
2499 self.worker.hold_jobs_in_build = True
2500 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2501 A.addApproval('CRVW', 2)
2502 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2503 self.waitUntilSettled()
2504
2505 port = self.webapp.server.socket.getsockname()[1]
2506
2507 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2508 data = f.read()
2509
2510 self.worker.hold_jobs_in_build = False
2511 self.worker.release()
2512 self.waitUntilSettled()
2513
2514 data = json.loads(data)
2515 status_jobs = set()
2516 for p in data['pipelines']:
2517 for q in p['change_queues']:
2518 for head in q['heads']:
2519 for change in head:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002520 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002521 for job in change['jobs']:
2522 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002523 self.assertIn('project-merge', status_jobs)
2524 self.assertIn('project-test1', status_jobs)
2525 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002526
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002527 def test_node_label(self):
2528 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002529 self.worker.registerFunction('build:node-project-test1:debian')
2530
2531 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2532 A.addApproval('CRVW', 2)
2533 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2534 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002535
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002536 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2537 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2538 'debian')
2539 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002540
2541 def test_live_reconfiguration(self):
2542 "Test that live reconfiguration works"
2543 self.worker.hold_jobs_in_build = True
2544 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2545 A.addApproval('CRVW', 2)
2546 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2547 self.waitUntilSettled()
2548
2549 self.sched.reconfigure(self.config)
2550
2551 self.worker.hold_jobs_in_build = False
2552 self.worker.release()
2553 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002554 self.assertEqual(self.getJobFromHistory('project-merge').result,
2555 'SUCCESS')
2556 self.assertEqual(self.getJobFromHistory('project-test1').result,
2557 'SUCCESS')
2558 self.assertEqual(self.getJobFromHistory('project-test2').result,
2559 'SUCCESS')
2560 self.assertEqual(A.data['status'], 'MERGED')
2561 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002562
James E. Blaire712d9f2013-07-31 11:40:11 -07002563 def test_live_reconfiguration_functions(self):
2564 "Test live reconfiguration with a custom function"
2565 self.worker.registerFunction('build:node-project-test1:debian')
2566 self.worker.registerFunction('build:node-project-test1:wheezy')
2567 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2568 A.addApproval('CRVW', 2)
2569 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2570 self.waitUntilSettled()
2571
2572 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2573 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2574 'debian')
2575 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2576
2577 self.config.set('zuul', 'layout_config',
2578 'tests/fixtures/layout-live-'
2579 'reconfiguration-functions.yaml')
2580 self.sched.reconfigure(self.config)
2581 self.worker.build_history = []
2582
2583 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
2584 B.addApproval('CRVW', 2)
2585 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2586 self.waitUntilSettled()
2587
2588 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2589 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2590 'wheezy')
2591 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2592
James E. Blair287c06d2013-07-24 10:39:30 -07002593 def test_delayed_repo_init(self):
2594 self.config.set('zuul', 'layout_config',
2595 'tests/fixtures/layout-delayed-repo-init.yaml')
2596 self.sched.reconfigure(self.config)
2597
2598 self.init_repo("org/new-project")
2599 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2600
2601 A.addApproval('CRVW', 2)
2602 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2603 self.waitUntilSettled()
2604 self.assertEqual(self.getJobFromHistory('project-merge').result,
2605 'SUCCESS')
2606 self.assertEqual(self.getJobFromHistory('project-test1').result,
2607 'SUCCESS')
2608 self.assertEqual(self.getJobFromHistory('project-test2').result,
2609 'SUCCESS')
2610 self.assertEqual(A.data['status'], 'MERGED')
2611 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002612
2613 def test_timer(self):
2614 "Test that a periodic job is triggered"
2615 self.worker.hold_jobs_in_build = True
2616 self.config.set('zuul', 'layout_config',
2617 'tests/fixtures/layout-timer.yaml')
2618 self.sched.reconfigure(self.config)
2619 self.registerJobs()
2620
2621 start = time.time()
2622 failed = True
2623 while ((time.time() - start) < 30):
2624 if len(self.builds) == 2:
2625 failed = False
2626 break
2627 else:
2628 time.sleep(1)
2629
2630 if failed:
2631 raise Exception("Expected jobs never ran")
2632
2633 self.waitUntilSettled()
2634 port = self.webapp.server.socket.getsockname()[1]
2635
2636 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2637 data = f.read()
2638
2639 self.worker.hold_jobs_in_build = False
2640 self.worker.release()
2641 self.waitUntilSettled()
2642
2643 self.assertEqual(self.getJobFromHistory(
2644 'project-bitrot-stable-old').result, 'SUCCESS')
2645 self.assertEqual(self.getJobFromHistory(
2646 'project-bitrot-stable-older').result, 'SUCCESS')
2647
2648 data = json.loads(data)
2649 status_jobs = set()
2650 for p in data['pipelines']:
2651 for q in p['change_queues']:
2652 for head in q['heads']:
2653 for change in head:
2654 self.assertEqual(change['id'], 'None')
2655 for job in change['jobs']:
2656 status_jobs.add(job['name'])
2657 self.assertIn('project-bitrot-stable-old', status_jobs)
2658 self.assertIn('project-bitrot-stable-older', status_jobs)