blob: c7f730d03334a5394ebb79fc6a9aea0f383c52ea [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. Blair3cb10702013-08-24 08:56:03 -07001084 def test_initial_pipeline_gauges(self):
1085 "Test that each pipeline reported its length on start"
1086 pipeline_names = self.sched.layout.pipelines.keys()
1087 self.assertNotEqual(len(pipeline_names), 0)
1088 for name in pipeline_names:
1089 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
1090 value='0|g')
1091
James E. Blair42f74822013-05-14 15:18:03 -07001092 def test_duplicate_pipelines(self):
1093 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001094
James E. Blair42f74822013-05-14 15:18:03 -07001095 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1096 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1097 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001098
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001099 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001100 self.history[0].name == 'project-test1'
1101 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001102
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001103 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001104 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001105 self.assertIn('dup1/project-test1', A.messages[0])
1106 self.assertNotIn('dup2/project-test1', A.messages[0])
1107 self.assertNotIn('dup1/project-test1', A.messages[1])
1108 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001109 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001110 self.assertIn('dup1/project-test1', A.messages[1])
1111 self.assertNotIn('dup2/project-test1', A.messages[1])
1112 self.assertNotIn('dup1/project-test1', A.messages[0])
1113 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001114
James E. Blairb0fcae42012-07-17 11:12:10 -07001115 def test_parallel_changes(self):
1116 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001117
1118 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001119 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1120 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1121 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001122 A.addApproval('CRVW', 2)
1123 B.addApproval('CRVW', 2)
1124 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001125
1126 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1127 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1128 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1129
1130 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001131 self.assertEqual(len(self.builds), 1)
1132 self.assertEqual(self.builds[0].name, 'project-merge')
1133 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001134
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001135 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001136 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001137 self.assertEqual(len(self.builds), 3)
1138 self.assertEqual(self.builds[0].name, 'project-test1')
1139 self.assertTrue(self.job_has_changes(self.builds[0], A))
1140 self.assertEqual(self.builds[1].name, 'project-test2')
1141 self.assertTrue(self.job_has_changes(self.builds[1], A))
1142 self.assertEqual(self.builds[2].name, 'project-merge')
1143 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001144
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001145 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001146 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001147 self.assertEqual(len(self.builds), 5)
1148 self.assertEqual(self.builds[0].name, 'project-test1')
1149 self.assertTrue(self.job_has_changes(self.builds[0], A))
1150 self.assertEqual(self.builds[1].name, 'project-test2')
1151 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001152
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001153 self.assertEqual(self.builds[2].name, 'project-test1')
1154 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1155 self.assertEqual(self.builds[3].name, 'project-test2')
1156 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001157
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001158 self.assertEqual(self.builds[4].name, 'project-merge')
1159 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001160
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001161 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001162 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001163 self.assertEqual(len(self.builds), 6)
1164 self.assertEqual(self.builds[0].name, 'project-test1')
1165 self.assertTrue(self.job_has_changes(self.builds[0], A))
1166 self.assertEqual(self.builds[1].name, 'project-test2')
1167 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001168
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001169 self.assertEqual(self.builds[2].name, 'project-test1')
1170 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1171 self.assertEqual(self.builds[3].name, 'project-test2')
1172 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001173
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001174 self.assertEqual(self.builds[4].name, 'project-test1')
1175 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1176 self.assertEqual(self.builds[5].name, 'project-test2')
1177 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001178
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001179 self.worker.hold_jobs_in_build = False
1180 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001181 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001182 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001183
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001184 self.assertEqual(len(self.history), 9)
1185 self.assertEqual(A.data['status'], 'MERGED')
1186 self.assertEqual(B.data['status'], 'MERGED')
1187 self.assertEqual(C.data['status'], 'MERGED')
1188 self.assertEqual(A.reported, 2)
1189 self.assertEqual(B.reported, 2)
1190 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001191
1192 def test_failed_changes(self):
1193 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001194 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001195
James E. Blairb02a3bb2012-07-30 17:49:55 -07001196 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1197 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001198 A.addApproval('CRVW', 2)
1199 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001200
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001201 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001202
James E. Blaire2819012013-06-28 17:17:26 -04001203 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1204 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001205 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001206
1207 self.worker.release('.*-merge')
1208 self.waitUntilSettled()
1209
1210 self.worker.hold_jobs_in_build = False
1211 self.worker.release()
1212
1213 self.waitUntilSettled()
1214 # It's certain that the merge job for change 2 will run, but
1215 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001216 self.assertTrue(len(self.history) > 6)
1217 self.assertEqual(A.data['status'], 'NEW')
1218 self.assertEqual(B.data['status'], 'MERGED')
1219 self.assertEqual(A.reported, 2)
1220 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001221
1222 def test_independent_queues(self):
1223 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001224
1225 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001226 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001227 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1228 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001229 A.addApproval('CRVW', 2)
1230 B.addApproval('CRVW', 2)
1231 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001232
1233 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1234 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1235 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1236
James E. Blairb02a3bb2012-07-30 17:49:55 -07001237 self.waitUntilSettled()
1238
1239 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001240 self.assertEqual(len(self.builds), 2)
1241 self.assertEqual(self.builds[0].name, 'project-merge')
1242 self.assertTrue(self.job_has_changes(self.builds[0], A))
1243 self.assertEqual(self.builds[1].name, 'project1-merge')
1244 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001245
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001246 # Release the current merge builds
1247 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001248 self.waitUntilSettled()
1249 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001250 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001251 self.waitUntilSettled()
1252
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001253 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001254 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001255 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001256
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001257 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001258 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001259 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001260
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001261 self.assertEqual(len(self.history), 11)
1262 self.assertEqual(A.data['status'], 'MERGED')
1263 self.assertEqual(B.data['status'], 'MERGED')
1264 self.assertEqual(C.data['status'], 'MERGED')
1265 self.assertEqual(A.reported, 2)
1266 self.assertEqual(B.reported, 2)
1267 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001268
1269 def test_failed_change_at_head(self):
1270 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001271
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001272 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001273 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1274 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1275 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001276 A.addApproval('CRVW', 2)
1277 B.addApproval('CRVW', 2)
1278 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001279
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001280 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001281
1282 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1283 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1284 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1285
1286 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001287
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001288 self.assertEqual(len(self.builds), 1)
1289 self.assertEqual(self.builds[0].name, 'project-merge')
1290 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001291
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001292 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001293 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001294 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001295 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001296 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001297 self.waitUntilSettled()
1298
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001299 self.assertEqual(len(self.builds), 6)
1300 self.assertEqual(self.builds[0].name, 'project-test1')
1301 self.assertEqual(self.builds[1].name, 'project-test2')
1302 self.assertEqual(self.builds[2].name, 'project-test1')
1303 self.assertEqual(self.builds[3].name, 'project-test2')
1304 self.assertEqual(self.builds[4].name, 'project-test1')
1305 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001306
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001307 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001308 self.waitUntilSettled()
1309
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001310 # project-test2, project-merge for B
1311 self.assertEqual(len(self.builds), 2)
1312 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001313
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001314 self.worker.hold_jobs_in_build = False
1315 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001316 self.waitUntilSettled()
1317
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001318 self.assertEqual(len(self.builds), 0)
1319 self.assertEqual(len(self.history), 15)
1320 self.assertEqual(A.data['status'], 'NEW')
1321 self.assertEqual(B.data['status'], 'MERGED')
1322 self.assertEqual(C.data['status'], 'MERGED')
1323 self.assertEqual(A.reported, 2)
1324 self.assertEqual(B.reported, 2)
1325 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001326
James E. Blair0aac4872013-08-23 14:02:38 -07001327 def test_failed_change_in_middle(self):
1328 "Test a failed change in the middle of the queue"
1329
1330 self.worker.hold_jobs_in_build = True
1331 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1332 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1333 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1334 A.addApproval('CRVW', 2)
1335 B.addApproval('CRVW', 2)
1336 C.addApproval('CRVW', 2)
1337
1338 self.worker.addFailTest('project-test1', B)
1339
1340 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1341 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1342 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1343
1344 self.waitUntilSettled()
1345
1346 self.worker.release('.*-merge')
1347 self.waitUntilSettled()
1348 self.worker.release('.*-merge')
1349 self.waitUntilSettled()
1350 self.worker.release('.*-merge')
1351 self.waitUntilSettled()
1352
1353 self.assertEqual(len(self.builds), 6)
1354 self.assertEqual(self.builds[0].name, 'project-test1')
1355 self.assertEqual(self.builds[1].name, 'project-test2')
1356 self.assertEqual(self.builds[2].name, 'project-test1')
1357 self.assertEqual(self.builds[3].name, 'project-test2')
1358 self.assertEqual(self.builds[4].name, 'project-test1')
1359 self.assertEqual(self.builds[5].name, 'project-test2')
1360
1361 self.release(self.builds[2])
1362 self.waitUntilSettled()
1363
1364 # project-test1 and project-test2 for A, project-test2 for B
1365 self.assertEqual(len(self.builds), 3)
1366 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1367
1368 # check that build status of aborted jobs are masked ('CANCELED')
1369 items = self.sched.layout.pipelines['gate'].getAllItems()
1370 builds = items[0].current_build_set.getBuilds()
1371 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1372 self.assertEqual(self.countJobResults(builds, None), 2)
1373 builds = items[1].current_build_set.getBuilds()
1374 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1375 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1376 self.assertEqual(self.countJobResults(builds, None), 1)
1377 builds = items[2].current_build_set.getBuilds()
1378 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1379 self.assertEqual(self.countJobResults(builds, 'CANCELED'), 2)
1380
1381 self.worker.hold_jobs_in_build = False
1382 self.worker.release()
1383 self.waitUntilSettled()
1384
1385 self.assertEqual(len(self.builds), 0)
1386 self.assertEqual(len(self.history), 12)
1387 self.assertEqual(A.data['status'], 'MERGED')
1388 self.assertEqual(B.data['status'], 'NEW')
1389 self.assertEqual(C.data['status'], 'MERGED')
1390 self.assertEqual(A.reported, 2)
1391 self.assertEqual(B.reported, 2)
1392 self.assertEqual(C.reported, 2)
1393
James E. Blaird466dc42012-07-31 10:42:56 -07001394 def test_failed_change_at_head_with_queue(self):
1395 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001396
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001397 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001398 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1399 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1400 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001401 A.addApproval('CRVW', 2)
1402 B.addApproval('CRVW', 2)
1403 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001404
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001405 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001406
1407 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1408 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1409 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1410
1411 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001412 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001413 self.assertEqual(len(self.builds), 0)
1414 self.assertEqual(len(queue), 1)
1415 self.assertEqual(queue[0].name, 'build:project-merge')
1416 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001417
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001418 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001419 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001420 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001421 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001422 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001423 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001424 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001425
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001426 self.assertEqual(len(self.builds), 0)
1427 self.assertEqual(len(queue), 6)
1428 self.assertEqual(queue[0].name, 'build:project-test1')
1429 self.assertEqual(queue[1].name, 'build:project-test2')
1430 self.assertEqual(queue[2].name, 'build:project-test1')
1431 self.assertEqual(queue[3].name, 'build:project-test2')
1432 self.assertEqual(queue[4].name, 'build:project-test1')
1433 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001434
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001435 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001436 self.waitUntilSettled()
1437
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001438 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001439 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001440 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1441 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001442
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001443 self.gearman_server.hold_jobs_in_queue = False
1444 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001445 self.waitUntilSettled()
1446
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001447 self.assertEqual(len(self.builds), 0)
1448 self.assertEqual(len(self.history), 11)
1449 self.assertEqual(A.data['status'], 'NEW')
1450 self.assertEqual(B.data['status'], 'MERGED')
1451 self.assertEqual(C.data['status'], 'MERGED')
1452 self.assertEqual(A.reported, 2)
1453 self.assertEqual(B.reported, 2)
1454 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001455
1456 def test_patch_order(self):
1457 "Test that dependent patches are tested in the right order"
1458 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1459 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1460 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1461 A.addApproval('CRVW', 2)
1462 B.addApproval('CRVW', 2)
1463 C.addApproval('CRVW', 2)
1464
1465 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1466 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1467 M2.setMerged()
1468 M1.setMerged()
1469
1470 # C -> B -> A -> M1 -> M2
1471 # M2 is here to make sure it is never queried. If it is, it
1472 # means zuul is walking down the entire history of merged
1473 # changes.
1474
1475 C.setDependsOn(B, 1)
1476 B.setDependsOn(A, 1)
1477 A.setDependsOn(M1, 1)
1478 M1.setDependsOn(M2, 1)
1479
1480 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1481
1482 self.waitUntilSettled()
1483
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001484 self.assertEqual(A.data['status'], 'NEW')
1485 self.assertEqual(B.data['status'], 'NEW')
1486 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001487
1488 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1489 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1490
1491 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001492 self.assertEqual(M2.queried, 0)
1493 self.assertEqual(A.data['status'], 'MERGED')
1494 self.assertEqual(B.data['status'], 'MERGED')
1495 self.assertEqual(C.data['status'], 'MERGED')
1496 self.assertEqual(A.reported, 2)
1497 self.assertEqual(B.reported, 2)
1498 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001499
James E. Blair0e933c52013-07-11 10:18:52 -07001500 def test_trigger_cache(self):
1501 "Test that the trigger cache operates correctly"
1502 self.worker.hold_jobs_in_build = True
1503
1504 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1505 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1506 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1507 A.addApproval('CRVW', 2)
1508 B.addApproval('CRVW', 2)
1509
1510 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1511 M1.setMerged()
1512
1513 B.setDependsOn(A, 1)
1514 A.setDependsOn(M1, 1)
1515
1516 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1517 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1518
1519 self.waitUntilSettled()
1520
1521 for build in self.builds:
1522 if build.parameters['ZUUL_PIPELINE'] == 'check':
1523 build.release()
1524 self.waitUntilSettled()
1525 for build in self.builds:
1526 if build.parameters['ZUUL_PIPELINE'] == 'check':
1527 build.release()
1528 self.waitUntilSettled()
1529
1530 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1531 self.waitUntilSettled()
1532
James E. Blair6c358e72013-07-29 17:06:47 -07001533 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001534 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001535 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001536
1537 self.worker.hold_jobs_in_build = False
1538 self.worker.release()
1539 self.waitUntilSettled()
1540
1541 self.assertEqual(A.data['status'], 'MERGED')
1542 self.assertEqual(B.data['status'], 'MERGED')
1543 self.assertEqual(A.queried, 2) # Initial and isMerged
1544 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1545
James E. Blair8c803f82012-07-31 16:25:42 -07001546 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001547 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001548 # TODO: move to test_gerrit (this is a unit test!)
1549 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001550 trigger = self.sched.layout.pipelines['gate'].trigger
1551 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001552 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001553 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001554
1555 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001556 a = trigger.getChange(1, 2, refresh=True)
1557 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001558
1559 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001560 a = trigger.getChange(1, 2, refresh=True)
1561 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1562 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001563
1564 def test_build_configuration(self):
1565 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001566
1567 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001568 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1569 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1570 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1571 A.addApproval('CRVW', 2)
1572 B.addApproval('CRVW', 2)
1573 C.addApproval('CRVW', 2)
1574 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1575 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1576 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1577 self.waitUntilSettled()
1578
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001579 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001580 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001581 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001582 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001583 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001584 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001585 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001586 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1587 self.gearman_server.hold_jobs_in_queue = False
1588 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001589 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001590
Monty Taylorbc758832013-06-17 17:22:42 -04001591 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001592 repo = git.Repo(path)
1593 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1594 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001595 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001596 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001597
1598 def test_build_configuration_conflict(self):
1599 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001600
1601 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001602 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1603 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001604 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001605 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1606 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001607 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001608 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1609 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001610 A.addApproval('CRVW', 2)
1611 B.addApproval('CRVW', 2)
1612 C.addApproval('CRVW', 2)
1613 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1614 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1615 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1616 self.waitUntilSettled()
1617
James E. Blair6736beb2013-07-11 15:18:15 -07001618 self.assertEqual(A.reported, 1)
1619 self.assertEqual(B.reported, 1)
1620 self.assertEqual(C.reported, 1)
1621
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001622 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001623 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001624 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001625 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001626 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001627 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001628 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001629 self.getParameter(queue[-1], 'ZUUL_REF')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001630 self.gearman_server.hold_jobs_in_queue = False
1631 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001632 self.waitUntilSettled()
1633
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001634 self.assertEqual(A.data['status'], 'MERGED')
1635 self.assertEqual(B.data['status'], 'NEW')
1636 self.assertEqual(C.data['status'], 'MERGED')
1637 self.assertEqual(A.reported, 2)
1638 self.assertEqual(B.reported, 2)
1639 self.assertEqual(C.reported, 2)
James E. Blairdaabed22012-08-15 15:38:57 -07001640
James E. Blair6736beb2013-07-11 15:18:15 -07001641 def test_dequeue_conflict(self):
1642 "Test that the option to dequeue merge conflicts works"
1643
1644 self.gearman_server.hold_jobs_in_queue = True
1645 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1646 A.addPatchset(['conflict'])
1647 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1648 B.addPatchset(['conflict'])
1649 A.addApproval('CRVW', 2)
1650 B.addApproval('CRVW', 2)
1651 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1652 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1653 self.waitUntilSettled()
1654
1655 self.assertEqual(A.reported, 1)
1656 self.assertEqual(B.reported, 2)
1657
1658 self.gearman_server.hold_jobs_in_queue = False
1659 self.gearman_server.release()
1660 self.waitUntilSettled()
1661
1662 self.assertEqual(A.data['status'], 'MERGED')
1663 self.assertEqual(B.data['status'], 'NEW')
1664 self.assertEqual(A.reported, 2)
1665 self.assertEqual(B.reported, 2)
1666
James E. Blairdaabed22012-08-15 15:38:57 -07001667 def test_post(self):
1668 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001669
Zhongyue Luo5d556072012-09-21 02:00:47 +09001670 e = {
1671 "type": "ref-updated",
1672 "submitter": {
1673 "name": "User Name",
1674 },
1675 "refUpdate": {
1676 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1677 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1678 "refName": "master",
1679 "project": "org/project",
1680 }
1681 }
James E. Blairdaabed22012-08-15 15:38:57 -07001682 self.fake_gerrit.addEvent(e)
1683 self.waitUntilSettled()
1684
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001685 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001686 self.assertEqual(len(self.history), 1)
1687 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001688
1689 def test_build_configuration_branch(self):
1690 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001691
1692 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001693 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1694 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1695 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1696 A.addApproval('CRVW', 2)
1697 B.addApproval('CRVW', 2)
1698 C.addApproval('CRVW', 2)
1699 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1700 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1701 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1702 self.waitUntilSettled()
1703
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001704 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001705 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001706 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001707 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001708 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001709 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001710 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001711 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1712 self.gearman_server.hold_jobs_in_queue = False
1713 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001714 self.waitUntilSettled()
1715
Monty Taylorbc758832013-06-17 17:22:42 -04001716 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001717 repo = git.Repo(path)
1718 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1719 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001720 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001721 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001722
1723 def test_build_configuration_branch_interaction(self):
1724 "Test that switching between branches works"
1725 self.test_build_configuration()
1726 self.test_build_configuration_branch()
1727 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001728 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001729 repo = git.Repo(path)
1730 repo.heads.master.commit = repo.commit('init')
1731 self.test_build_configuration()
1732
1733 def test_build_configuration_multi_branch(self):
1734 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001735
1736 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001737 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1738 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1739 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1740 A.addApproval('CRVW', 2)
1741 B.addApproval('CRVW', 2)
1742 C.addApproval('CRVW', 2)
1743 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1744 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1745 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1746 self.waitUntilSettled()
1747
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001748 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001749 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001750 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001751 job_B = None
1752 for job in queue:
1753 if 'project-merge' in job.name:
1754 job_B = job
1755 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001756 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001757 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001758 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001759 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001760 for job in queue:
1761 if 'project-merge' in job.name:
1762 job_C = job
1763 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001764 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001765 self.gearman_server.hold_jobs_in_queue = False
1766 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001767 self.waitUntilSettled()
1768
Monty Taylorbc758832013-06-17 17:22:42 -04001769 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001770 repo = git.Repo(path)
1771
1772 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001773 for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001774 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001775 correct_messages = ['initial commit', 'A-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001776 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001777
1778 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001779 for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001780 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001781 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001782 self.assertEqual(repo_messages, correct_messages)
James E. Blair7f71c802012-08-22 13:04:32 -07001783
1784 def test_one_job_project(self):
1785 "Test that queueing works with one job"
1786 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1787 'master', 'A')
1788 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1789 'master', 'B')
1790 A.addApproval('CRVW', 2)
1791 B.addApproval('CRVW', 2)
1792 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1793 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1794 self.waitUntilSettled()
1795
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001796 self.assertEqual(A.data['status'], 'MERGED')
1797 self.assertEqual(A.reported, 2)
1798 self.assertEqual(B.data['status'], 'MERGED')
1799 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001800
Antoine Musso80edd5a2013-02-13 15:37:53 +01001801 def test_job_from_templates_launched(self):
1802 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001803
Antoine Musso80edd5a2013-02-13 15:37:53 +01001804 A = self.fake_gerrit.addFakeChange(
1805 'org/templated-project', 'master', 'A')
1806 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1807 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001808
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001809 self.assertEqual(self.getJobFromHistory('project-test1').result,
1810 'SUCCESS')
1811 self.assertEqual(self.getJobFromHistory('project-test2').result,
1812 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001813
James E. Blaircaec0c52012-08-22 14:52:22 -07001814 def test_dependent_changes_dequeue(self):
1815 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001816
James E. Blaircaec0c52012-08-22 14:52:22 -07001817 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1818 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1819 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1820 A.addApproval('CRVW', 2)
1821 B.addApproval('CRVW', 2)
1822 C.addApproval('CRVW', 2)
1823
1824 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1825 M1.setMerged()
1826
1827 # C -> B -> A -> M1
1828
1829 C.setDependsOn(B, 1)
1830 B.setDependsOn(A, 1)
1831 A.setDependsOn(M1, 1)
1832
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001833 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001834
1835 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1836 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1837 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1838
1839 self.waitUntilSettled()
1840
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001841 self.assertEqual(A.data['status'], 'NEW')
1842 self.assertEqual(A.reported, 2)
1843 self.assertEqual(B.data['status'], 'NEW')
1844 self.assertEqual(B.reported, 2)
1845 self.assertEqual(C.data['status'], 'NEW')
1846 self.assertEqual(C.reported, 2)
1847 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001848
1849 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001850 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001851 # If it's dequeued more than once, we should see extra
1852 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001853
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001854 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001855 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1856 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1857 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1858 A.addApproval('CRVW', 2)
1859 B.addApproval('CRVW', 2)
1860 C.addApproval('CRVW', 2)
1861
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001862 self.worker.addFailTest('project1-test1', A)
1863 self.worker.addFailTest('project1-test2', A)
1864 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001865
1866 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1867 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1868 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1869
1870 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001871
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001872 self.assertEqual(len(self.builds), 1)
1873 self.assertEqual(self.builds[0].name, 'project1-merge')
1874 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07001875
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001876 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001877 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001878 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001879 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001880 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001881 self.waitUntilSettled()
1882
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001883 self.assertEqual(len(self.builds), 9)
1884 self.assertEqual(self.builds[0].name, 'project1-test1')
1885 self.assertEqual(self.builds[1].name, 'project1-test2')
1886 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
1887 self.assertEqual(self.builds[3].name, 'project1-test1')
1888 self.assertEqual(self.builds[4].name, 'project1-test2')
1889 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
1890 self.assertEqual(self.builds[6].name, 'project1-test1')
1891 self.assertEqual(self.builds[7].name, 'project1-test2')
1892 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001893
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001894 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001895 self.waitUntilSettled()
1896
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001897 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1898 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001899
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001900 self.worker.hold_jobs_in_build = False
1901 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001902 self.waitUntilSettled()
1903
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001904 self.assertEqual(len(self.builds), 0)
1905 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001906
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001907 self.assertEqual(A.data['status'], 'NEW')
1908 self.assertEqual(B.data['status'], 'MERGED')
1909 self.assertEqual(C.data['status'], 'MERGED')
1910 self.assertEqual(A.reported, 2)
1911 self.assertEqual(B.reported, 2)
1912 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001913
1914 def test_nonvoting_job(self):
1915 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001916
James E. Blair4ec821f2012-08-23 15:28:28 -07001917 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1918 'master', 'A')
1919 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001920 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001921 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1922
1923 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001924
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001925 self.assertEqual(A.data['status'], 'MERGED')
1926 self.assertEqual(A.reported, 2)
1927 self.assertEqual(
1928 self.getJobFromHistory('nonvoting-project-merge').result,
1929 'SUCCESS')
1930 self.assertEqual(
1931 self.getJobFromHistory('nonvoting-project-test1').result,
1932 'SUCCESS')
1933 self.assertEqual(
1934 self.getJobFromHistory('nonvoting-project-test2').result,
1935 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001936
1937 def test_check_queue_success(self):
1938 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001939
James E. Blaire0487072012-08-29 17:38:31 -07001940 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1941 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1942
1943 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001944
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001945 self.assertEqual(A.data['status'], 'NEW')
1946 self.assertEqual(A.reported, 1)
1947 self.assertEqual(self.getJobFromHistory('project-merge').result,
1948 'SUCCESS')
1949 self.assertEqual(self.getJobFromHistory('project-test1').result,
1950 'SUCCESS')
1951 self.assertEqual(self.getJobFromHistory('project-test2').result,
1952 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001953
1954 def test_check_queue_failure(self):
1955 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001956
James E. Blaire0487072012-08-29 17:38:31 -07001957 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001958 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001959 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1960
1961 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001962
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001963 self.assertEqual(A.data['status'], 'NEW')
1964 self.assertEqual(A.reported, 1)
1965 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001966 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001967 self.assertEqual(self.getJobFromHistory('project-test1').result,
1968 'SUCCESS')
1969 self.assertEqual(self.getJobFromHistory('project-test2').result,
1970 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001971
1972 def test_dependent_behind_dequeue(self):
1973 "test that dependent changes behind dequeued changes work"
1974 # This complicated test is a reproduction of a real life bug
1975 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001976
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001977 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001978 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1979 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1980 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1981 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1982 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1983 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1984 D.setDependsOn(C, 1)
1985 E.setDependsOn(D, 1)
1986 A.addApproval('CRVW', 2)
1987 B.addApproval('CRVW', 2)
1988 C.addApproval('CRVW', 2)
1989 D.addApproval('CRVW', 2)
1990 E.addApproval('CRVW', 2)
1991 F.addApproval('CRVW', 2)
1992
1993 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001994
1995 # Change object re-use in the gerrit trigger is hidden if
1996 # changes are added in quick succession; waiting makes it more
1997 # like real life.
1998 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1999 self.waitUntilSettled()
2000 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2001 self.waitUntilSettled()
2002
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002003 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002004 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002005 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002006 self.waitUntilSettled()
2007
2008 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2009 self.waitUntilSettled()
2010 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2011 self.waitUntilSettled()
2012 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2013 self.waitUntilSettled()
2014 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
2015 self.waitUntilSettled()
2016
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002017 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002018 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002019 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002020 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002021 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002022 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002023 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002024 self.waitUntilSettled()
2025
2026 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002027
2028 # Grab pointers to the jobs we want to release before
2029 # releasing any, because list indexes may change as
2030 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002031 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002032 a.release()
2033 b.release()
2034 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002035 self.waitUntilSettled()
2036
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002037 self.worker.hold_jobs_in_build = False
2038 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002039 self.waitUntilSettled()
2040
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002041 self.assertEqual(A.data['status'], 'NEW')
2042 self.assertEqual(B.data['status'], 'MERGED')
2043 self.assertEqual(C.data['status'], 'MERGED')
2044 self.assertEqual(D.data['status'], 'MERGED')
2045 self.assertEqual(E.data['status'], 'MERGED')
2046 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002047
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002048 self.assertEqual(A.reported, 2)
2049 self.assertEqual(B.reported, 2)
2050 self.assertEqual(C.reported, 2)
2051 self.assertEqual(D.reported, 2)
2052 self.assertEqual(E.reported, 2)
2053 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002054
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002055 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2056 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002057
2058 def test_merger_repack(self):
2059 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002060
James E. Blair05fed602012-09-07 12:45:24 -07002061 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2062 A.addApproval('CRVW', 2)
2063 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2064 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002065 self.assertEqual(self.getJobFromHistory('project-merge').result,
2066 'SUCCESS')
2067 self.assertEqual(self.getJobFromHistory('project-test1').result,
2068 'SUCCESS')
2069 self.assertEqual(self.getJobFromHistory('project-test2').result,
2070 'SUCCESS')
2071 self.assertEqual(A.data['status'], 'MERGED')
2072 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07002073 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002074 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002075
Monty Taylorbc758832013-06-17 17:22:42 -04002076 path = os.path.join(self.git_root, "org/project")
2077 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002078
2079 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2080 A.addApproval('CRVW', 2)
2081 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2082 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002083 self.assertEqual(self.getJobFromHistory('project-merge').result,
2084 'SUCCESS')
2085 self.assertEqual(self.getJobFromHistory('project-test1').result,
2086 'SUCCESS')
2087 self.assertEqual(self.getJobFromHistory('project-test2').result,
2088 'SUCCESS')
2089 self.assertEqual(A.data['status'], 'MERGED')
2090 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002091
James E. Blair4886f282012-11-15 09:27:33 -08002092 def test_merger_repack_large_change(self):
2093 "Test that the merger works with large changes after a repack"
2094 # https://bugs.launchpad.net/zuul/+bug/1078946
2095 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2096 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002097 path = os.path.join(self.upstream_root, "org/project1")
2098 print repack_repo(path)
2099 path = os.path.join(self.git_root, "org/project1")
2100 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002101
2102 A.addApproval('CRVW', 2)
2103 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2104 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002105 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2106 'SUCCESS')
2107 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2108 'SUCCESS')
2109 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2110 'SUCCESS')
2111 self.assertEqual(A.data['status'], 'MERGED')
2112 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002113
James E. Blair7ee88a22012-09-12 18:59:31 +02002114 def test_nonexistent_job(self):
2115 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002116 # Set to the state immediately after a restart
2117 self.resetGearmanServer()
2118 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002119
2120 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2121 A.addApproval('CRVW', 2)
2122 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2123 # There may be a thread about to report a lost change
2124 while A.reported < 2:
2125 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002126 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002127 self.assertFalse(job_names)
2128 self.assertEqual(A.data['status'], 'NEW')
2129 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002130 self.assertEmptyQueues()
2131
2132 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002133 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002134 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2135 A.addApproval('CRVW', 2)
2136 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2137 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002138 self.assertEqual(self.getJobFromHistory('project-merge').result,
2139 'SUCCESS')
2140 self.assertEqual(self.getJobFromHistory('project-test1').result,
2141 'SUCCESS')
2142 self.assertEqual(self.getJobFromHistory('project-test2').result,
2143 'SUCCESS')
2144 self.assertEqual(A.data['status'], 'MERGED')
2145 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002146
2147 def test_single_nonexistent_post_job(self):
2148 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002149 e = {
2150 "type": "ref-updated",
2151 "submitter": {
2152 "name": "User Name",
2153 },
2154 "refUpdate": {
2155 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2156 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2157 "refName": "master",
2158 "project": "org/project",
2159 }
2160 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002161 # Set to the state immediately after a restart
2162 self.resetGearmanServer()
2163 self.launcher.negative_function_cache_ttl = 0
2164
James E. Blairf62d4282012-12-31 17:01:50 -08002165 self.fake_gerrit.addEvent(e)
2166 self.waitUntilSettled()
2167
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002168 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002169
2170 def test_new_patchset_dequeues_old(self):
2171 "Test that a new patchset causes the old to be dequeued"
2172 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002173 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002174 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2175 M.setMerged()
2176
2177 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2178 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2179 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2180 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2181 A.addApproval('CRVW', 2)
2182 B.addApproval('CRVW', 2)
2183 C.addApproval('CRVW', 2)
2184 D.addApproval('CRVW', 2)
2185
2186 C.setDependsOn(B, 1)
2187 B.setDependsOn(A, 1)
2188 A.setDependsOn(M, 1)
2189
2190 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2191 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2192 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2193 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2194 self.waitUntilSettled()
2195
2196 B.addPatchset()
2197 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2198 self.waitUntilSettled()
2199
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002200 self.worker.hold_jobs_in_build = False
2201 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002202 self.waitUntilSettled()
2203
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002204 self.assertEqual(A.data['status'], 'MERGED')
2205 self.assertEqual(A.reported, 2)
2206 self.assertEqual(B.data['status'], 'NEW')
2207 self.assertEqual(B.reported, 2)
2208 self.assertEqual(C.data['status'], 'NEW')
2209 self.assertEqual(C.reported, 2)
2210 self.assertEqual(D.data['status'], 'MERGED')
2211 self.assertEqual(D.reported, 2)
2212 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002213
2214 def test_new_patchset_dequeues_old_on_head(self):
2215 "Test that a new patchset causes the old to be dequeued (at head)"
2216 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002217 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002218 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2219 M.setMerged()
2220 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2221 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2222 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2223 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2224 A.addApproval('CRVW', 2)
2225 B.addApproval('CRVW', 2)
2226 C.addApproval('CRVW', 2)
2227 D.addApproval('CRVW', 2)
2228
2229 C.setDependsOn(B, 1)
2230 B.setDependsOn(A, 1)
2231 A.setDependsOn(M, 1)
2232
2233 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2234 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2235 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2236 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2237 self.waitUntilSettled()
2238
2239 A.addPatchset()
2240 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2241 self.waitUntilSettled()
2242
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002243 self.worker.hold_jobs_in_build = False
2244 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002245 self.waitUntilSettled()
2246
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002247 self.assertEqual(A.data['status'], 'NEW')
2248 self.assertEqual(A.reported, 2)
2249 self.assertEqual(B.data['status'], 'NEW')
2250 self.assertEqual(B.reported, 2)
2251 self.assertEqual(C.data['status'], 'NEW')
2252 self.assertEqual(C.reported, 2)
2253 self.assertEqual(D.data['status'], 'MERGED')
2254 self.assertEqual(D.reported, 2)
2255 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002256
2257 def test_new_patchset_dequeues_old_without_dependents(self):
2258 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002259 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002260 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2261 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2262 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2263 A.addApproval('CRVW', 2)
2264 B.addApproval('CRVW', 2)
2265 C.addApproval('CRVW', 2)
2266
2267 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2268 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2269 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2270 self.waitUntilSettled()
2271
2272 B.addPatchset()
2273 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2274 self.waitUntilSettled()
2275
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002276 self.worker.hold_jobs_in_build = False
2277 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002278 self.waitUntilSettled()
2279
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002280 self.assertEqual(A.data['status'], 'MERGED')
2281 self.assertEqual(A.reported, 2)
2282 self.assertEqual(B.data['status'], 'NEW')
2283 self.assertEqual(B.reported, 2)
2284 self.assertEqual(C.data['status'], 'MERGED')
2285 self.assertEqual(C.reported, 2)
2286 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002287
2288 def test_new_patchset_dequeues_old_independent_queue(self):
2289 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002290 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002291 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2292 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2293 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2294 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2295 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2296 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2297 self.waitUntilSettled()
2298
2299 B.addPatchset()
2300 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2301 self.waitUntilSettled()
2302
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002303 self.worker.hold_jobs_in_build = False
2304 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002305 self.waitUntilSettled()
2306
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002307 self.assertEqual(A.data['status'], 'NEW')
2308 self.assertEqual(A.reported, 1)
2309 self.assertEqual(B.data['status'], 'NEW')
2310 self.assertEqual(B.reported, 1)
2311 self.assertEqual(C.data['status'], 'NEW')
2312 self.assertEqual(C.reported, 1)
2313 self.assertEqual(len(self.history), 10)
2314 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002315
2316 def test_zuul_refs(self):
2317 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002318 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002319 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2320 M1.setMerged()
2321 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2322 M2.setMerged()
2323
2324 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2325 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2326 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2327 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2328 A.addApproval('CRVW', 2)
2329 B.addApproval('CRVW', 2)
2330 C.addApproval('CRVW', 2)
2331 D.addApproval('CRVW', 2)
2332 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2333 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2334 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2335 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2336
2337 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002338 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002339 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002340 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002341 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002342 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002343 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002344 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002345 self.waitUntilSettled()
2346
James E. Blair7d0dedc2013-02-21 17:26:09 -08002347 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002348 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002349 if x.parameters['ZUUL_CHANGE'] == '3':
2350 a_zref = x.parameters['ZUUL_REF']
2351 if x.parameters['ZUUL_CHANGE'] == '4':
2352 b_zref = x.parameters['ZUUL_REF']
2353 if x.parameters['ZUUL_CHANGE'] == '5':
2354 c_zref = x.parameters['ZUUL_REF']
2355 if x.parameters['ZUUL_CHANGE'] == '6':
2356 d_zref = x.parameters['ZUUL_REF']
2357
2358 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002359 self.assertIsNotNone(a_zref)
2360 self.assertIsNotNone(b_zref)
2361 self.assertIsNotNone(c_zref)
2362 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002363
2364 # And they should all be different
2365 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002366 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002367
2368 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002369 self.assertTrue(self.ref_has_change(a_zref, A))
2370 self.assertFalse(self.ref_has_change(a_zref, B))
2371 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002372
2373 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002374 self.assertTrue(self.ref_has_change(b_zref, A))
2375 self.assertTrue(self.ref_has_change(b_zref, B))
2376 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002377
2378 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002379 self.assertTrue(self.ref_has_change(c_zref, A))
2380 self.assertTrue(self.ref_has_change(c_zref, B))
2381 self.assertTrue(self.ref_has_change(c_zref, C))
2382 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002383
2384 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002385 self.assertTrue(self.ref_has_change(d_zref, A))
2386 self.assertTrue(self.ref_has_change(d_zref, B))
2387 self.assertTrue(self.ref_has_change(d_zref, C))
2388 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002389
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002390 self.worker.hold_jobs_in_build = False
2391 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002392 self.waitUntilSettled()
2393
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002394 self.assertEqual(A.data['status'], 'MERGED')
2395 self.assertEqual(A.reported, 2)
2396 self.assertEqual(B.data['status'], 'MERGED')
2397 self.assertEqual(B.reported, 2)
2398 self.assertEqual(C.data['status'], 'MERGED')
2399 self.assertEqual(C.reported, 2)
2400 self.assertEqual(D.data['status'], 'MERGED')
2401 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002402
James E. Blair4a28a882013-08-23 15:17:33 -07002403 def test_rerun_on_error(self):
2404 "Test that if a worker fails to run a job, it is run again"
2405 self.worker.hold_jobs_in_build = True
2406 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2407 A.addApproval('CRVW', 2)
2408 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2409 self.waitUntilSettled()
2410
2411 self.builds[0].run_error = True
2412 self.worker.hold_jobs_in_build = False
2413 self.worker.release()
2414 self.waitUntilSettled()
2415 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2416 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2417
James E. Blair412e5582013-04-22 15:50:12 -07002418 def test_statsd(self):
2419 "Test each of the statsd methods used in the scheduler"
2420 import extras
2421 statsd = extras.try_import('statsd.statsd')
2422 statsd.incr('test-incr')
2423 statsd.timing('test-timing', 3)
2424 statsd.gauge('test-guage', 12)
2425 self.assertReportedStat('test-incr', '1|c')
2426 self.assertReportedStat('test-timing', '3|ms')
2427 self.assertReportedStat('test-guage', '12|g')
2428
James E. Blair70c71582013-03-06 08:50:50 -08002429 def test_file_jobs(self):
2430 "Test that file jobs run only when appropriate"
2431 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2432 A.addPatchset(['pip-requires'])
2433 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2434 A.addApproval('CRVW', 2)
2435 B.addApproval('CRVW', 2)
2436 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2437 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2438 self.waitUntilSettled()
2439
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002440 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002441 if x.name == 'project-testfile']
2442
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002443 self.assertEqual(len(testfile_jobs), 1)
2444 self.assertEqual(testfile_jobs[0].changes, '1,2')
2445 self.assertEqual(A.data['status'], 'MERGED')
2446 self.assertEqual(A.reported, 2)
2447 self.assertEqual(B.data['status'], 'MERGED')
2448 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002449
2450 def test_test_config(self):
2451 "Test that we can test the config"
2452 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002453 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002454 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002455 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002456
2457 def test_build_description(self):
2458 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002459 self.worker.registerFunction('set_description:' +
2460 self.worker.worker_id)
2461
2462 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2463 A.addApproval('CRVW', 2)
2464 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2465 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002466 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002467 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002468 self.assertTrue(re.search("Branch.*master", desc))
2469 self.assertTrue(re.search("Pipeline.*gate", desc))
2470 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2471 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2472 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2473 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002474
James E. Blair64ed6f22013-07-10 14:07:23 -07002475 def test_queue_precedence(self):
2476 "Test that queue precedence works"
2477
2478 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002479 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002480 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2481 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2482 A.addApproval('CRVW', 2)
2483 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2484
2485 self.waitUntilSettled()
2486 self.gearman_server.hold_jobs_in_queue = False
2487 self.gearman_server.release()
2488 self.waitUntilSettled()
2489
James E. Blair8de58bd2013-07-18 16:23:33 -07002490 # Run one build at a time to ensure non-race order:
2491 for x in range(6):
2492 self.release(self.builds[0])
2493 self.waitUntilSettled()
2494 self.worker.hold_jobs_in_build = False
2495 self.waitUntilSettled()
2496
James E. Blair64ed6f22013-07-10 14:07:23 -07002497 self.log.debug(self.history)
2498 self.assertEqual(self.history[0].pipeline, 'gate')
2499 self.assertEqual(self.history[1].pipeline, 'check')
2500 self.assertEqual(self.history[2].pipeline, 'gate')
2501 self.assertEqual(self.history[3].pipeline, 'gate')
2502 self.assertEqual(self.history[4].pipeline, 'check')
2503 self.assertEqual(self.history[5].pipeline, 'check')
2504
James E. Blair1843a552013-07-03 14:19:52 -07002505 def test_json_status(self):
2506 "Test that we can retrieve JSON status info"
2507 self.worker.hold_jobs_in_build = True
2508 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2509 A.addApproval('CRVW', 2)
2510 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2511 self.waitUntilSettled()
2512
2513 port = self.webapp.server.socket.getsockname()[1]
2514
2515 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2516 data = f.read()
2517
2518 self.worker.hold_jobs_in_build = False
2519 self.worker.release()
2520 self.waitUntilSettled()
2521
2522 data = json.loads(data)
2523 status_jobs = set()
2524 for p in data['pipelines']:
2525 for q in p['change_queues']:
2526 for head in q['heads']:
2527 for change in head:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002528 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002529 for job in change['jobs']:
2530 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002531 self.assertIn('project-merge', status_jobs)
2532 self.assertIn('project-test1', status_jobs)
2533 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002534
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002535 def test_node_label(self):
2536 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002537 self.worker.registerFunction('build:node-project-test1:debian')
2538
2539 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2540 A.addApproval('CRVW', 2)
2541 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2542 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002543
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002544 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2545 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2546 'debian')
2547 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002548
2549 def test_live_reconfiguration(self):
2550 "Test that live reconfiguration works"
2551 self.worker.hold_jobs_in_build = True
2552 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2553 A.addApproval('CRVW', 2)
2554 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2555 self.waitUntilSettled()
2556
2557 self.sched.reconfigure(self.config)
2558
2559 self.worker.hold_jobs_in_build = False
2560 self.worker.release()
2561 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002562 self.assertEqual(self.getJobFromHistory('project-merge').result,
2563 'SUCCESS')
2564 self.assertEqual(self.getJobFromHistory('project-test1').result,
2565 'SUCCESS')
2566 self.assertEqual(self.getJobFromHistory('project-test2').result,
2567 'SUCCESS')
2568 self.assertEqual(A.data['status'], 'MERGED')
2569 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002570
James E. Blaire712d9f2013-07-31 11:40:11 -07002571 def test_live_reconfiguration_functions(self):
2572 "Test live reconfiguration with a custom function"
2573 self.worker.registerFunction('build:node-project-test1:debian')
2574 self.worker.registerFunction('build:node-project-test1:wheezy')
2575 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2576 A.addApproval('CRVW', 2)
2577 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2578 self.waitUntilSettled()
2579
2580 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2581 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2582 'debian')
2583 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2584
2585 self.config.set('zuul', 'layout_config',
2586 'tests/fixtures/layout-live-'
2587 'reconfiguration-functions.yaml')
2588 self.sched.reconfigure(self.config)
2589 self.worker.build_history = []
2590
2591 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
2592 B.addApproval('CRVW', 2)
2593 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2594 self.waitUntilSettled()
2595
2596 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2597 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2598 'wheezy')
2599 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2600
James E. Blair287c06d2013-07-24 10:39:30 -07002601 def test_delayed_repo_init(self):
2602 self.config.set('zuul', 'layout_config',
2603 'tests/fixtures/layout-delayed-repo-init.yaml')
2604 self.sched.reconfigure(self.config)
2605
2606 self.init_repo("org/new-project")
2607 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2608
2609 A.addApproval('CRVW', 2)
2610 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2611 self.waitUntilSettled()
2612 self.assertEqual(self.getJobFromHistory('project-merge').result,
2613 'SUCCESS')
2614 self.assertEqual(self.getJobFromHistory('project-test1').result,
2615 'SUCCESS')
2616 self.assertEqual(self.getJobFromHistory('project-test2').result,
2617 'SUCCESS')
2618 self.assertEqual(A.data['status'], 'MERGED')
2619 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002620
2621 def test_timer(self):
2622 "Test that a periodic job is triggered"
2623 self.worker.hold_jobs_in_build = True
2624 self.config.set('zuul', 'layout_config',
2625 'tests/fixtures/layout-timer.yaml')
2626 self.sched.reconfigure(self.config)
2627 self.registerJobs()
2628
2629 start = time.time()
2630 failed = True
2631 while ((time.time() - start) < 30):
2632 if len(self.builds) == 2:
2633 failed = False
2634 break
2635 else:
2636 time.sleep(1)
2637
2638 if failed:
2639 raise Exception("Expected jobs never ran")
2640
2641 self.waitUntilSettled()
2642 port = self.webapp.server.socket.getsockname()[1]
2643
2644 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2645 data = f.read()
2646
2647 self.worker.hold_jobs_in_build = False
2648 self.worker.release()
2649 self.waitUntilSettled()
2650
2651 self.assertEqual(self.getJobFromHistory(
2652 'project-bitrot-stable-old').result, 'SUCCESS')
2653 self.assertEqual(self.getJobFromHistory(
2654 'project-bitrot-stable-older').result, 'SUCCESS')
2655
2656 data = json.loads(data)
2657 status_jobs = set()
2658 for p in data['pipelines']:
2659 for q in p['change_queues']:
2660 for head in q['heads']:
2661 for change in head:
2662 self.assertEqual(change['id'], 'None')
2663 for job in change['jobs']:
2664 status_jobs.add(job['name'])
2665 self.assertIn('project-bitrot-stable-old', status_jobs)
2666 self.assertIn('project-bitrot-stable-older', status_jobs)