blob: 395ff2595a1219d5788d64c571c9905c29365d3d [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
Joshua Hesketh1879cf72013-08-19 14:13:15 +100047import zuul.reporter.gerrit
Joshua Hesketh5fea8672013-08-19 17:32:01 +100048import zuul.reporter.smtp
James E. Blairb0fcae42012-07-17 11:12:10 -070049import zuul.trigger.gerrit
James E. Blair63bb0ef2013-07-29 17:14:51 -070050import zuul.trigger.timer
James E. Blairb0fcae42012-07-17 11:12:10 -070051
52FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
53 'fixtures')
54CONFIG = ConfigParser.ConfigParser()
55CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
56
57CONFIG.set('zuul', 'layout_config',
58 os.path.join(FIXTURE_DIR, "layout.yaml"))
59
James E. Blair1f4c2bb2013-04-26 08:40:46 -070060logging.basicConfig(level=logging.DEBUG,
61 format='%(asctime)s %(name)-32s '
62 '%(levelname)-8s %(message)s')
James E. Blairb0fcae42012-07-17 11:12:10 -070063
64
Monty Taylorbc758832013-06-17 17:22:42 -040065def repack_repo(path):
66 output = subprocess.Popen(
67 ['git', '--git-dir=%s/.git' % path, 'repack', '-afd'],
68 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
69 out = output.communicate()
70 if output.returncode:
71 raise Exception("git repack returned %d" % output.returncode)
72 return out
73
74
James E. Blair8cc15a82012-08-01 11:17:57 -070075def random_sha1():
76 return hashlib.sha1(str(random.random())).hexdigest()
77
78
James E. Blair4886cc12012-07-18 15:39:41 -070079class ChangeReference(git.Reference):
80 _common_path_default = "refs/changes"
81 _points_to_commits_only = True
82
83
James E. Blairb0fcae42012-07-17 11:12:10 -070084class FakeChange(object):
James E. Blair8c803f82012-07-31 16:25:42 -070085 categories = {'APRV': ('Approved', -1, 1),
86 'CRVW': ('Code-Review', -2, 2),
87 'VRFY': ('Verified', -2, 2)}
James E. Blairb0fcae42012-07-17 11:12:10 -070088
Monty Taylorbc758832013-06-17 17:22:42 -040089 def __init__(self, gerrit, number, project, branch, subject,
90 status='NEW', upstream_root=None):
James E. Blair8cc15a82012-08-01 11:17:57 -070091 self.gerrit = gerrit
James E. Blaird466dc42012-07-31 10:42:56 -070092 self.reported = 0
James E. Blair8c803f82012-07-31 16:25:42 -070093 self.queried = 0
James E. Blairb0fcae42012-07-17 11:12:10 -070094 self.patchsets = []
James E. Blairb0fcae42012-07-17 11:12:10 -070095 self.number = number
96 self.project = project
97 self.branch = branch
98 self.subject = subject
99 self.latest_patchset = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700100 self.depends_on_change = None
101 self.needed_by_changes = []
James E. Blair127bc182012-08-28 15:55:15 -0700102 self.fail_merge = False
James E. Blair42f74822013-05-14 15:18:03 -0700103 self.messages = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700104 self.data = {
105 'branch': branch,
106 'comments': [],
107 'commitMessage': subject,
108 'createdOn': time.time(),
James E. Blair8cc15a82012-08-01 11:17:57 -0700109 'id': 'I' + random_sha1(),
James E. Blairb0fcae42012-07-17 11:12:10 -0700110 'lastUpdated': time.time(),
111 'number': str(number),
112 'open': True,
113 'owner': {'email': 'user@example.com',
114 'name': 'User Name',
115 'username': 'username'},
116 'patchSets': self.patchsets,
117 'project': project,
118 'status': status,
119 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700120 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700121 'url': 'https://hostname/%s' % number}
122
Monty Taylorbc758832013-06-17 17:22:42 -0400123 self.upstream_root = upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700124 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700125 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700126
Monty Taylorbc758832013-06-17 17:22:42 -0400127 def add_fake_change_to_repo(self, msg, fn, large):
128 path = os.path.join(self.upstream_root, self.project)
129 repo = git.Repo(path)
130 ref = ChangeReference.create(repo, '1/%s/%s' % (self.number,
131 self.latest_patchset),
132 'refs/tags/init')
133 repo.head.reference = ref
134 repo.head.reset(index=True, working_tree=True)
135 repo.git.clean('-x', '-f', '-d')
136
137 path = os.path.join(self.upstream_root, self.project)
138 if not large:
139 fn = os.path.join(path, fn)
140 f = open(fn, 'w')
141 f.write("test %s %s %s\n" %
142 (self.branch, self.number, self.latest_patchset))
143 f.close()
144 repo.index.add([fn])
145 else:
146 for fni in range(100):
147 fn = os.path.join(path, str(fni))
148 f = open(fn, 'w')
149 for ci in range(4096):
150 f.write(random.choice(string.printable))
151 f.close()
152 repo.index.add([fn])
153
James E. Blair287c06d2013-07-24 10:39:30 -0700154 r = repo.index.commit(msg)
155 repo.head.reference = 'master'
156 repo.head.reset(index=True, working_tree=True)
157 repo.git.clean('-x', '-f', '-d')
158 return r
Monty Taylorbc758832013-06-17 17:22:42 -0400159
James E. Blair70c71582013-03-06 08:50:50 -0800160 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700161 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700162 if files:
163 fn = files[0]
164 else:
165 fn = '%s-%s' % (self.branch, self.number)
166 msg = self.subject + '-' + str(self.latest_patchset)
Monty Taylorbc758832013-06-17 17:22:42 -0400167 c = self.add_fake_change_to_repo(msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800168 ps_files = [{'file': '/COMMIT_MSG',
169 'type': 'ADDED'},
170 {'file': 'README',
171 'type': 'MODIFIED'}]
172 for f in files:
173 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700174 d = {'approvals': [],
175 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800176 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700177 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700178 'ref': 'refs/changes/1/%s/%s' % (self.number,
179 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700180 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700181 'uploader': {'email': 'user@example.com',
182 'name': 'User name',
183 'username': 'user'}}
184 self.data['currentPatchSet'] = d
185 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700186 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700187
James E. Blaire0487072012-08-29 17:38:31 -0700188 def getPatchsetCreatedEvent(self, patchset):
189 event = {"type": "patchset-created",
190 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800191 "branch": self.branch,
192 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
193 "number": str(self.number),
194 "subject": self.subject,
195 "owner": {"name": "User Name"},
196 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700197 "patchSet": self.patchsets[patchset - 1],
198 "uploader": {"name": "User Name"}}
199 return event
200
James E. Blair42f74822013-05-14 15:18:03 -0700201 def getChangeRestoredEvent(self):
202 event = {"type": "change-restored",
203 "change": {"project": self.project,
204 "branch": self.branch,
205 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
206 "number": str(self.number),
207 "subject": self.subject,
208 "owner": {"name": "User Name"},
209 "url": "https://hostname/3"},
210 "restorer": {"name": "User Name"},
211 "reason": ""}
212 return event
213
James E. Blairb0fcae42012-07-17 11:12:10 -0700214 def addApproval(self, category, value):
James E. Blair8c803f82012-07-31 16:25:42 -0700215 approval = {'description': self.categories[category][0],
216 'type': category,
217 'value': str(value)}
218 self.patchsets[-1]['approvals'].append(approval)
219 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700220 'author': {'email': 'user@example.com',
221 'name': 'User Name',
222 'username': 'username'},
223 'change': {'branch': self.branch,
224 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
225 'number': str(self.number),
226 'owner': {'email': 'user@example.com',
227 'name': 'User Name',
228 'username': 'username'},
229 'project': self.project,
230 'subject': self.subject,
231 'topic': 'master',
232 'url': 'https://hostname/459'},
233 'comment': '',
234 'patchSet': self.patchsets[-1],
235 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700236 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700237 return json.loads(json.dumps(event))
238
James E. Blair8c803f82012-07-31 16:25:42 -0700239 def getSubmitRecords(self):
240 status = {}
241 for cat in self.categories.keys():
242 status[cat] = 0
243
244 for a in self.patchsets[-1]['approvals']:
245 cur = status[a['type']]
246 cat_min, cat_max = self.categories[a['type']][1:]
247 new = int(a['value'])
248 if new == cat_min:
249 cur = new
250 elif abs(new) > abs(cur):
251 cur = new
252 status[a['type']] = cur
253
254 labels = []
255 ok = True
256 for typ, cat in self.categories.items():
257 cur = status[typ]
258 cat_min, cat_max = cat[1:]
259 if cur == cat_min:
260 value = 'REJECT'
261 ok = False
262 elif cur == cat_max:
263 value = 'OK'
264 else:
265 value = 'NEED'
266 ok = False
267 labels.append({'label': cat[0], 'status': value})
268 if ok:
269 return [{'status': 'OK'}]
270 return [{'status': 'NOT_READY',
271 'labels': labels}]
272
273 def setDependsOn(self, other, patchset):
274 self.depends_on_change = other
275 d = {'id': other.data['id'],
276 'number': other.data['number'],
277 'ref': other.patchsets[patchset - 1]['ref']
278 }
279 self.data['dependsOn'] = [d]
280
281 other.needed_by_changes.append(self)
282 needed = other.data.get('neededBy', [])
283 d = {'id': self.data['id'],
284 'number': self.data['number'],
285 'ref': self.patchsets[patchset - 1]['ref'],
286 'revision': self.patchsets[patchset - 1]['revision']
287 }
288 needed.append(d)
289 other.data['neededBy'] = needed
290
James E. Blairb0fcae42012-07-17 11:12:10 -0700291 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700292 self.queried += 1
293 d = self.data.get('dependsOn')
294 if d:
295 d = d[0]
296 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
297 d['isCurrentPatchSet'] = True
298 else:
299 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700300 return json.loads(json.dumps(self.data))
301
302 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800303 if (self.depends_on_change and
304 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700305 return
James E. Blair127bc182012-08-28 15:55:15 -0700306 if self.fail_merge:
307 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700308 self.data['status'] = 'MERGED'
309 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700310
Monty Taylorbc758832013-06-17 17:22:42 -0400311 path = os.path.join(self.upstream_root, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700312 repo = git.Repo(path)
313 repo.heads[self.branch].commit = \
314 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700315
James E. Blaird466dc42012-07-31 10:42:56 -0700316 def setReported(self):
317 self.reported += 1
318
James E. Blairb0fcae42012-07-17 11:12:10 -0700319
320class FakeGerrit(object):
321 def __init__(self, *args, **kw):
322 self.event_queue = Queue.Queue()
323 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
324 self.change_number = 0
325 self.changes = {}
326
327 def addFakeChange(self, project, branch, subject):
328 self.change_number += 1
Monty Taylorbc758832013-06-17 17:22:42 -0400329 c = FakeChange(self, self.change_number, project, branch, subject,
330 upstream_root=self.upstream_root)
James E. Blairb0fcae42012-07-17 11:12:10 -0700331 self.changes[self.change_number] = c
332 return c
333
334 def addEvent(self, data):
335 return self.event_queue.put(data)
336
337 def getEvent(self):
338 return self.event_queue.get()
339
340 def eventDone(self):
341 self.event_queue.task_done()
342
343 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700344 number, ps = changeid.split(',')
345 change = self.changes[int(number)]
James E. Blair42f74822013-05-14 15:18:03 -0700346 change.messages.append(message)
James E. Blairb0fcae42012-07-17 11:12:10 -0700347 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700348 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700349 if message:
350 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700351
352 def query(self, number):
353 change = self.changes[int(number)]
354 return change.query()
355
356 def startWatching(self, *args, **kw):
357 pass
358
359
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700360class BuildHistory(object):
361 def __init__(self, **kw):
362 self.__dict__.update(kw)
James E. Blairb0fcae42012-07-17 11:12:10 -0700363
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700364 def __repr__(self):
365 return ("<Completed build, result: %s name: %s #%s changes: %s>" %
366 (self.result, self.name, self.number, self.changes))
James E. Blairb0fcae42012-07-17 11:12:10 -0700367
368
James E. Blair8cc15a82012-08-01 11:17:57 -0700369class FakeURLOpener(object):
Monty Taylorbc758832013-06-17 17:22:42 -0400370 def __init__(self, upstream_root, fake_gerrit, url):
371 self.upstream_root = upstream_root
James E. Blair8cc15a82012-08-01 11:17:57 -0700372 self.fake_gerrit = fake_gerrit
373 self.url = url
374
375 def read(self):
376 res = urlparse.urlparse(self.url)
377 path = res.path
378 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200379 ret = '001e# service=git-upload-pack\n'
380 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
381 'multi_ack thin-pack side-band side-band-64k ofs-delta '
382 'shallow no-progress include-tag multi_ack_detailed no-done\n')
Monty Taylorbc758832013-06-17 17:22:42 -0400383 path = os.path.join(self.upstream_root, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700384 repo = git.Repo(path)
385 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200386 r = ref.object.hexsha + ' ' + ref.path + '\n'
387 ret += '%04x%s' % (len(r) + 4, r)
388 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700389 return ret
390
391
James E. Blair4886cc12012-07-18 15:39:41 -0700392class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000393 name = 'gerrit'
394
Monty Taylorbc758832013-06-17 17:22:42 -0400395 def __init__(self, upstream_root, *args):
396 super(FakeGerritTrigger, self).__init__(*args)
397 self.upstream_root = upstream_root
398
James E. Blair4886cc12012-07-18 15:39:41 -0700399 def getGitUrl(self, project):
Monty Taylorbc758832013-06-17 17:22:42 -0400400 return os.path.join(self.upstream_root, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700401
402
James E. Blair412e5582013-04-22 15:50:12 -0700403class FakeStatsd(threading.Thread):
404 def __init__(self):
405 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400406 self.daemon = True
James E. Blair412e5582013-04-22 15:50:12 -0700407 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
408 self.sock.bind(('', 0))
409 self.port = self.sock.getsockname()[1]
410 self.wake_read, self.wake_write = os.pipe()
411 self.stats = []
412
413 def run(self):
414 while True:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700415 poll = select.poll()
416 poll.register(self.sock, select.POLLIN)
417 poll.register(self.wake_read, select.POLLIN)
418 ret = poll.poll()
419 for (fd, event) in ret:
420 if fd == self.sock.fileno():
James E. Blair412e5582013-04-22 15:50:12 -0700421 data = self.sock.recvfrom(1024)
422 if not data:
423 return
424 self.stats.append(data[0])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700425 if fd == self.wake_read:
James E. Blair412e5582013-04-22 15:50:12 -0700426 return
427
428 def stop(self):
429 os.write(self.wake_write, '1\n')
430
431
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700432class FakeBuild(threading.Thread):
433 log = logging.getLogger("zuul.test")
434
435 def __init__(self, worker, job, number, node):
436 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400437 self.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700438 self.worker = worker
439 self.job = job
440 self.name = job.name.split(':')[1]
441 self.number = number
442 self.node = node
443 self.parameters = json.loads(job.arguments)
444 self.unique = self.parameters['ZUUL_UUID']
445 self.wait_condition = threading.Condition()
446 self.waiting = False
447 self.aborted = False
448 self.created = time.time()
449 self.description = ''
James E. Blair4a28a882013-08-23 15:17:33 -0700450 self.run_error = False
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700451
452 def release(self):
453 self.wait_condition.acquire()
454 self.wait_condition.notify()
455 self.waiting = False
456 self.log.debug("Build %s released" % self.unique)
457 self.wait_condition.release()
458
459 def isWaiting(self):
460 self.wait_condition.acquire()
461 if self.waiting:
462 ret = True
463 else:
464 ret = False
465 self.wait_condition.release()
466 return ret
467
468 def _wait(self):
469 self.wait_condition.acquire()
470 self.waiting = True
471 self.log.debug("Build %s waiting" % self.unique)
472 self.wait_condition.wait()
473 self.wait_condition.release()
474
475 def run(self):
476 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700477 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
478 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700479 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700480 'manager': self.worker.worker_id,
Paul Belangerec49b4c2013-07-20 20:32:20 -0400481 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700482
483 self.job.sendWorkData(json.dumps(data))
484 self.job.sendWorkStatus(0, 100)
485
486 if self.worker.hold_jobs_in_build:
487 self._wait()
488 self.log.debug("Build %s continuing" % self.unique)
489
490 self.worker.lock.acquire()
491
492 result = 'SUCCESS'
493 if (('ZUUL_REF' in self.parameters) and
494 self.worker.shouldFailTest(self.name,
495 self.parameters['ZUUL_REF'])):
496 result = 'FAILURE'
497 if self.aborted:
498 result = 'ABORTED'
499
James E. Blair4a28a882013-08-23 15:17:33 -0700500 if self.run_error:
501 work_fail = True
502 result = 'RUN_ERROR'
503 else:
504 data['result'] = result
505 work_fail = False
506
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700507 changes = None
508 if 'ZUUL_CHANGE_IDS' in self.parameters:
509 changes = self.parameters['ZUUL_CHANGE_IDS']
510
511 self.worker.build_history.append(
512 BuildHistory(name=self.name, number=self.number,
513 result=result, changes=changes, node=self.node,
James E. Blair64ed6f22013-07-10 14:07:23 -0700514 uuid=self.unique, description=self.description,
515 pipeline=self.parameters['ZUUL_PIPELINE'])
Paul Belangerec49b4c2013-07-20 20:32:20 -0400516 )
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700517
James E. Blair4a28a882013-08-23 15:17:33 -0700518 self.job.sendWorkData(json.dumps(data))
519 if work_fail:
520 self.job.sendWorkFail()
521 else:
522 self.job.sendWorkComplete(json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700523 del self.worker.gearman_jobs[self.job.unique]
524 self.worker.running_builds.remove(self)
525 self.worker.lock.release()
526
527
528class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400529 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700530 super(FakeWorker, self).__init__(worker_id)
531 self.gearman_jobs = {}
532 self.build_history = []
533 self.running_builds = []
534 self.build_counter = 0
535 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400536 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700537
538 self.hold_jobs_in_build = False
539 self.lock = threading.Lock()
540 self.__work_thread = threading.Thread(target=self.work)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400541 self.__work_thread.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700542 self.__work_thread.start()
543
544 def handleJob(self, job):
545 parts = job.name.split(":")
546 cmd = parts[0]
547 name = parts[1]
548 if len(parts) > 2:
549 node = parts[2]
550 else:
551 node = None
552 if cmd == 'build':
553 self.handleBuild(job, name, node)
554 elif cmd == 'stop':
555 self.handleStop(job, name)
556 elif cmd == 'set_description':
557 self.handleSetDescription(job, name)
558
559 def handleBuild(self, job, name, node):
560 build = FakeBuild(self, job, self.build_counter, node)
561 job.build = build
562 self.gearman_jobs[job.unique] = job
563 self.build_counter += 1
564
565 self.running_builds.append(build)
566 build.start()
567
568 def handleStop(self, job, name):
569 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700570 parameters = json.loads(job.arguments)
571 name = parameters['name']
572 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700573 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700574 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700575 build.aborted = True
576 build.release()
577 job.sendWorkComplete()
578 return
579 job.sendWorkFail()
580
581 def handleSetDescription(self, job, name):
582 self.log.debug("handle set description")
583 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700584 name = parameters['name']
585 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700586 descr = parameters['html_description']
587 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700588 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700589 build.description = descr
590 job.sendWorkComplete()
591 return
592 for build in self.build_history:
James E. Blair3c483cf2013-06-04 16:30:43 -0700593 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700594 build.description = descr
595 job.sendWorkComplete()
596 return
597 job.sendWorkFail()
598
599 def work(self):
600 while self.running:
601 try:
602 job = self.getJob()
603 except gear.InterruptedError:
604 continue
605 try:
606 self.handleJob(job)
607 except:
608 self.log.exception("Worker exception:")
609
610 def addFailTest(self, name, change):
611 l = self.fail_tests.get(name, [])
612 l.append(change)
613 self.fail_tests[name] = l
614
615 def shouldFailTest(self, name, ref):
616 l = self.fail_tests.get(name, [])
617 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400618 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700619 return True
620 return False
621
622 def release(self, regex=None):
623 builds = self.running_builds[:]
624 self.log.debug("releasing build %s (%s)" % (regex,
James E. Blair78e31b32013-07-09 09:11:34 -0700625 len(self.running_builds)))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700626 for build in builds:
627 if not regex or re.match(regex, build.name):
628 self.log.debug("releasing build %s" %
629 (build.parameters['ZUUL_UUID']))
630 build.release()
631 else:
632 self.log.debug("not releasing build %s" %
633 (build.parameters['ZUUL_UUID']))
634 self.log.debug("done releasing builds %s (%s)" %
635 (regex, len(self.running_builds)))
636
637
638class FakeGearmanServer(gear.Server):
639 def __init__(self):
640 self.hold_jobs_in_queue = False
641 super(FakeGearmanServer, self).__init__(0)
642
643 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700644 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
645 for job in queue:
646 if not hasattr(job, 'waiting'):
647 if job.name.startswith('build:'):
648 job.waiting = self.hold_jobs_in_queue
649 else:
650 job.waiting = False
651 if job.waiting:
652 continue
653 if job.name in connection.functions:
654 if not peek:
655 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400656 connection.related_jobs[job.handle] = job
657 job.worker_connection = connection
658 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700659 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700660 return None
661
662 def release(self, regex=None):
663 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700664 qlen = (len(self.high_queue) + len(self.normal_queue) +
665 len(self.low_queue))
666 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
James E. Blairdda6c912013-07-29 14:12:12 -0700667 for job in self.getQueue():
668 cmd, name = job.name.split(':')
669 if cmd != 'build':
670 continue
671 if not regex or re.match(regex, name):
672 self.log.debug("releasing queued job %s" %
673 job.unique)
674 job.waiting = False
675 released = True
676 else:
677 self.log.debug("not releasing queued job %s" %
678 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700679 if released:
680 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700681 qlen = (len(self.high_queue) + len(self.normal_queue) +
682 len(self.low_queue))
683 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700684
685
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000686class FakeSMTP(object):
687 log = logging.getLogger('zuul.FakeSMTP')
688 messages = []
689
690 def __init__(self, server, port):
691 self.server = server
692 self.port = port
693
694 def sendmail(self, from_email, to_email, msg):
695 self.log.info("Sending email from %s, to %s, with msg %s" % (
696 from_email, to_email, msg))
697
698 headers = msg.split('\n\n', 1)[0]
699 body = msg.split('\n\n', 1)[1]
700
701 FakeSMTP.messages.append(dict(
702 from_email=from_email,
703 to_email=to_email,
704 msg=msg,
705 headers=headers,
706 body=body,
707 ))
708
709 return True
710
711 def quit(self):
712 return True
713
714
Monty Taylorbc758832013-06-17 17:22:42 -0400715class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700716 log = logging.getLogger("zuul.test")
717
718 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400719 super(TestScheduler, self).setUp()
720 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
721 try:
722 test_timeout = int(test_timeout)
723 except ValueError:
724 # If timeout value is invalid do not set a timeout.
725 test_timeout = 0
726 if test_timeout > 0:
727 self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
728
729 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
730 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
731 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
732 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
733 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
734 os.environ.get('OS_STDERR_CAPTURE') == '1'):
735 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
736 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Monty Taylore6a6c402013-07-02 09:25:55 -0700737 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
738 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400739 self.useFixture(fixtures.FakeLogger(
740 level=logging.DEBUG,
741 format='%(asctime)s %(name)-32s '
742 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700743 tmp_root = self.useFixture(fixtures.TempDir(
744 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400745 self.test_root = os.path.join(tmp_root, "zuul-test")
746 self.upstream_root = os.path.join(self.test_root, "upstream")
747 self.git_root = os.path.join(self.test_root, "git")
748
749 CONFIG.set('zuul', 'git_dir', self.git_root)
750 if os.path.exists(self.test_root):
751 shutil.rmtree(self.test_root)
752 os.makedirs(self.test_root)
753 os.makedirs(self.upstream_root)
754 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700755
756 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400757 self.init_repo("org/project")
758 self.init_repo("org/project1")
759 self.init_repo("org/project2")
760 self.init_repo("org/project3")
761 self.init_repo("org/one-job-project")
762 self.init_repo("org/nonvoting-project")
763 self.init_repo("org/templated-project")
764 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700765 self.init_repo("org/conflict-project")
James E. Blair412e5582013-04-22 15:50:12 -0700766
767 self.statsd = FakeStatsd()
768 os.environ['STATSD_HOST'] = 'localhost'
769 os.environ['STATSD_PORT'] = str(self.statsd.port)
770 self.statsd.start()
771 # the statsd client object is configured in the statsd module import
772 reload(statsd)
773 reload(zuul.scheduler)
774
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700775 self.gearman_server = FakeGearmanServer()
776
777 self.config = ConfigParser.ConfigParser()
778 cfg = StringIO()
779 CONFIG.write(cfg)
780 cfg.seek(0)
781 self.config.readfp(cfg)
782 self.config.set('gearman', 'port', str(self.gearman_server.port))
783
Monty Taylorbc758832013-06-17 17:22:42 -0400784 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700785 self.worker.addServer('127.0.0.1', self.gearman_server.port)
786 self.gearman_server.worker = self.worker
787
James E. Blairb0fcae42012-07-17 11:12:10 -0700788 self.sched = zuul.scheduler.Scheduler()
789
James E. Blair8cc15a82012-08-01 11:17:57 -0700790 def URLOpenerFactory(*args, **kw):
791 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400792 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700793
James E. Blair8cc15a82012-08-01 11:17:57 -0700794 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700795 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700796
797 zuul.lib.gerrit.Gerrit = FakeGerrit
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000798 self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTP))
James E. Blairb0fcae42012-07-17 11:12:10 -0700799
Monty Taylorbc758832013-06-17 17:22:42 -0400800 self.gerrit = FakeGerritTrigger(
801 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700802 self.gerrit.replication_timeout = 1.5
803 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700804 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400805 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700806
James E. Blair1843a552013-07-03 14:19:52 -0700807 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
808
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700809 self.sched.setLauncher(self.launcher)
James E. Blair6c358e72013-07-29 17:06:47 -0700810 self.sched.registerTrigger(self.gerrit)
James E. Blair63bb0ef2013-07-29 17:14:51 -0700811 self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
812 self.sched.registerTrigger(self.timer)
James E. Blairb0fcae42012-07-17 11:12:10 -0700813
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000814 self.sched.registerReporter(
815 zuul.reporter.gerrit.Reporter(self.gerrit))
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000816 self.smtp_reporter = zuul.reporter.smtp.Reporter(
817 self.config.get('smtp', 'default_from'),
818 self.config.get('smtp', 'default_to'),
819 self.config.get('smtp', 'server'))
820 self.sched.registerReporter(self.smtp_reporter)
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000821
James E. Blairb0fcae42012-07-17 11:12:10 -0700822 self.sched.start()
823 self.sched.reconfigure(self.config)
824 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700825 self.webapp.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700826 self.launcher.gearman.waitForServer()
827 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400828 self.builds = self.worker.running_builds
829 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700830
James E. Blairfee8d652013-06-07 08:57:52 -0700831 self.addCleanup(self.assertFinalState)
832 self.addCleanup(self.shutdown)
833
834 def assertFinalState(self):
835 # Make sure that the change cache is cleared
James E. Blair6c358e72013-07-29 17:06:47 -0700836 self.assertEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700837 self.assertEmptyQueues()
838
839 def shutdown(self):
840 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700841 self.launcher.stop()
842 self.worker.shutdown()
843 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700844 self.gerrit.stop()
James E. Blair63bb0ef2013-07-29 17:14:51 -0700845 self.timer.stop()
James E. Blairb0fcae42012-07-17 11:12:10 -0700846 self.sched.stop()
847 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700848 self.statsd.stop()
849 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700850 self.webapp.stop()
851 self.webapp.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700852 threads = threading.enumerate()
853 if len(threads) > 1:
854 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400855 super(TestScheduler, self).tearDown()
856
857 def init_repo(self, project):
858 parts = project.split('/')
859 path = os.path.join(self.upstream_root, *parts[:-1])
860 if not os.path.exists(path):
861 os.makedirs(path)
862 path = os.path.join(self.upstream_root, project)
863 repo = git.Repo.init(path)
864
865 repo.config_writer().set_value('user', 'email', 'user@example.com')
866 repo.config_writer().set_value('user', 'name', 'User Name')
867 repo.config_writer().write()
868
869 fn = os.path.join(path, 'README')
870 f = open(fn, 'w')
871 f.write("test\n")
872 f.close()
873 repo.index.add([fn])
874 repo.index.commit('initial commit')
875 master = repo.create_head('master')
876 repo.create_tag('init')
877
878 mp = repo.create_head('mp')
879 repo.head.reference = mp
880 f = open(fn, 'a')
881 f.write("test mp\n")
882 f.close()
883 repo.index.add([fn])
884 repo.index.commit('mp commit')
885
886 repo.head.reference = master
887 repo.head.reset(index=True, working_tree=True)
888 repo.git.clean('-x', '-f', '-d')
889
890 def ref_has_change(self, ref, change):
891 path = os.path.join(self.git_root, change.project)
892 repo = git.Repo(path)
893 for commit in repo.iter_commits(ref):
894 if commit.message.strip() == ('%s-1' % change.subject):
895 return True
896 return False
897
898 def job_has_changes(self, *args):
899 job = args[0]
900 commits = args[1:]
901 if isinstance(job, FakeBuild):
902 parameters = job.parameters
903 else:
904 parameters = json.loads(job.arguments)
905 project = parameters['ZUUL_PROJECT']
906 path = os.path.join(self.git_root, project)
907 repo = git.Repo(path)
908 ref = parameters['ZUUL_REF']
909 sha = parameters['ZUUL_COMMIT']
910 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
911 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
912 commit_messages = ['%s-1' % commit.subject for commit in commits]
913 for msg in commit_messages:
914 if msg not in repo_messages:
915 return False
916 if repo_shas[0] != sha:
917 return False
918 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700919
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700920 def registerJobs(self):
921 count = 0
James E. Blaireff88162013-07-01 12:44:14 -0400922 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700923 self.worker.registerFunction('build:' + job)
924 count += 1
925 self.worker.registerFunction('stop:' + self.worker.worker_id)
926 count += 1
927
928 while len(self.gearman_server.functions) < count:
929 time.sleep(0)
930
931 def release(self, job):
932 if isinstance(job, FakeBuild):
933 job.release()
934 else:
935 job.waiting = False
936 self.log.debug("Queued job %s released" % job.unique)
937 self.gearman_server.wakeConnections()
938
939 def getParameter(self, job, name):
940 if isinstance(job, FakeBuild):
941 return job.parameters[name]
942 else:
943 parameters = json.loads(job.arguments)
944 return parameters[name]
945
946 def resetGearmanServer(self):
947 self.worker.setFunctions([])
948 while True:
949 done = True
950 for connection in self.gearman_server.active_connections:
951 if connection.functions:
952 done = False
953 if done:
954 break
955 time.sleep(0)
956 self.gearman_server.functions = set()
957
958 def haveAllBuildsReported(self):
959 # See if Zuul is waiting on a meta job to complete
960 if self.launcher.meta_jobs:
961 return False
962 # Find out if every build that the worker has completed has been
963 # reported back to Zuul. If it hasn't then that means a Gearman
964 # event is still in transit and the system is not stable.
965 for build in self.worker.build_history:
966 zbuild = self.launcher.builds.get(build.uuid)
967 if not zbuild:
968 # It has already been reported
969 continue
970 # It hasn't been reported yet.
971 return False
972 # Make sure that none of the worker connections are in GRAB_WAIT
973 for connection in self.worker.active_connections:
974 if connection.state == 'GRAB_WAIT':
975 return False
976 return True
977
978 def areAllBuildsWaiting(self):
979 ret = True
980
981 builds = self.launcher.builds.values()
982 for build in builds:
983 client_job = None
984 for conn in self.launcher.gearman.active_connections:
985 for j in conn.related_jobs.values():
986 if j.unique == build.uuid:
987 client_job = j
988 break
989 if not client_job:
990 self.log.debug("%s is not known to the gearman client" %
991 build)
992 ret = False
993 continue
994 if not client_job.handle:
995 self.log.debug("%s has no handle" % client_job)
996 ret = False
997 continue
998 server_job = self.gearman_server.jobs.get(client_job.handle)
999 if not server_job:
1000 self.log.debug("%s is not known to the gearman server" %
1001 client_job)
1002 ret = False
1003 continue
1004 if not hasattr(server_job, 'waiting'):
1005 self.log.debug("%s is being enqueued" % server_job)
1006 ret = False
1007 continue
1008 if server_job.waiting:
1009 continue
1010 worker_job = self.worker.gearman_jobs.get(server_job.unique)
1011 if worker_job:
1012 if worker_job.build.isWaiting():
1013 continue
1014 else:
1015 self.log.debug("%s is running" % worker_job)
1016 ret = False
1017 else:
1018 self.log.debug("%s is unassigned" % server_job)
1019 ret = False
1020 return ret
1021
James E. Blairb0fcae42012-07-17 11:12:10 -07001022 def waitUntilSettled(self):
1023 self.log.debug("Waiting until settled...")
1024 start = time.time()
1025 while True:
1026 if time.time() - start > 10:
1027 print 'queue status:',
1028 print self.sched.trigger_event_queue.empty(),
1029 print self.sched.result_event_queue.empty(),
1030 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001031 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -07001032 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001033 # Make sure no new events show up while we're checking
1034 self.worker.lock.acquire()
1035 # have all build states propogated to zuul?
1036 if self.haveAllBuildsReported():
1037 # Join ensures that the queue is empty _and_ events have been
1038 # processed
1039 self.fake_gerrit.event_queue.join()
1040 self.sched.trigger_event_queue.join()
1041 self.sched.result_event_queue.join()
1042 if (self.sched.trigger_event_queue.empty() and
1043 self.sched.result_event_queue.empty() and
1044 self.fake_gerrit.event_queue.empty() and
1045 self.areAllBuildsWaiting()):
1046 self.worker.lock.release()
1047 self.log.debug("...settled.")
1048 return
1049 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001050 self.sched.wake_event.wait(0.1)
1051
James E. Blaird466dc42012-07-31 10:42:56 -07001052 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -08001053 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -07001054 return len(jobs)
1055
James E. Blair4ca985f2013-05-30 12:27:43 -07001056 def getJobFromHistory(self, name):
1057 history = self.worker.build_history
1058 for job in history:
1059 if job.name == name:
1060 return job
1061 raise Exception("Unable to find job %s in history" % name)
1062
James E. Blaire0487072012-08-29 17:38:31 -07001063 def assertEmptyQueues(self):
1064 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001065 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001066 for queue in pipeline.queues:
1067 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001068 print 'pipeline %s queue %s contents %s' % (
1069 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001070 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001071
James E. Blair66eeebf2013-07-27 17:44:32 -07001072 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001073 start = time.time()
1074 while time.time() < (start + 5):
1075 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001076 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001077 k, v = stat.split(':')
1078 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001079 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001080 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001081 elif value:
1082 if value == v:
1083 return
1084 elif kind:
1085 if v.endswith('|' + kind):
1086 return
James E. Blair412e5582013-04-22 15:50:12 -07001087 time.sleep(0.1)
1088
1089 pprint.pprint(self.statsd.stats)
1090 raise Exception("Key %s not found in reported stats" % key)
1091
James E. Blairb0fcae42012-07-17 11:12:10 -07001092 def test_jobs_launched(self):
1093 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001094
James E. Blairb0fcae42012-07-17 11:12:10 -07001095 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001096 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001097 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1098 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001099 self.assertEqual(self.getJobFromHistory('project-merge').result,
1100 'SUCCESS')
1101 self.assertEqual(self.getJobFromHistory('project-test1').result,
1102 'SUCCESS')
1103 self.assertEqual(self.getJobFromHistory('project-test2').result,
1104 'SUCCESS')
1105 self.assertEqual(A.data['status'], 'MERGED')
1106 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001107
James E. Blair66eeebf2013-07-27 17:44:32 -07001108 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1109 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1110 value='1|g')
1111 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1112 kind='ms')
1113 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1114 value='1|c')
1115 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1116 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1117 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001118 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001119 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001120 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001121 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001122
James E. Blair3cb10702013-08-24 08:56:03 -07001123 def test_initial_pipeline_gauges(self):
1124 "Test that each pipeline reported its length on start"
1125 pipeline_names = self.sched.layout.pipelines.keys()
1126 self.assertNotEqual(len(pipeline_names), 0)
1127 for name in pipeline_names:
1128 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
1129 value='0|g')
1130
James E. Blair42f74822013-05-14 15:18:03 -07001131 def test_duplicate_pipelines(self):
1132 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001133
James E. Blair42f74822013-05-14 15:18:03 -07001134 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1135 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1136 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001137
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001138 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001139 self.history[0].name == 'project-test1'
1140 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001141
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001142 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001143 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001144 self.assertIn('dup1/project-test1', A.messages[0])
1145 self.assertNotIn('dup2/project-test1', A.messages[0])
1146 self.assertNotIn('dup1/project-test1', A.messages[1])
1147 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001148 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001149 self.assertIn('dup1/project-test1', A.messages[1])
1150 self.assertNotIn('dup2/project-test1', A.messages[1])
1151 self.assertNotIn('dup1/project-test1', A.messages[0])
1152 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001153
James E. Blairb0fcae42012-07-17 11:12:10 -07001154 def test_parallel_changes(self):
1155 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001156
1157 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001158 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1159 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1160 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001161 A.addApproval('CRVW', 2)
1162 B.addApproval('CRVW', 2)
1163 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001164
1165 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1166 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1167 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1168
1169 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001170 self.assertEqual(len(self.builds), 1)
1171 self.assertEqual(self.builds[0].name, 'project-merge')
1172 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001173
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001174 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001175 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001176 self.assertEqual(len(self.builds), 3)
1177 self.assertEqual(self.builds[0].name, 'project-test1')
1178 self.assertTrue(self.job_has_changes(self.builds[0], A))
1179 self.assertEqual(self.builds[1].name, 'project-test2')
1180 self.assertTrue(self.job_has_changes(self.builds[1], A))
1181 self.assertEqual(self.builds[2].name, 'project-merge')
1182 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001183
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001184 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001185 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001186 self.assertEqual(len(self.builds), 5)
1187 self.assertEqual(self.builds[0].name, 'project-test1')
1188 self.assertTrue(self.job_has_changes(self.builds[0], A))
1189 self.assertEqual(self.builds[1].name, 'project-test2')
1190 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001191
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001192 self.assertEqual(self.builds[2].name, 'project-test1')
1193 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1194 self.assertEqual(self.builds[3].name, 'project-test2')
1195 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001196
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001197 self.assertEqual(self.builds[4].name, 'project-merge')
1198 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001199
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001200 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001201 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001202 self.assertEqual(len(self.builds), 6)
1203 self.assertEqual(self.builds[0].name, 'project-test1')
1204 self.assertTrue(self.job_has_changes(self.builds[0], A))
1205 self.assertEqual(self.builds[1].name, 'project-test2')
1206 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001207
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001208 self.assertEqual(self.builds[2].name, 'project-test1')
1209 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1210 self.assertEqual(self.builds[3].name, 'project-test2')
1211 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001212
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001213 self.assertEqual(self.builds[4].name, 'project-test1')
1214 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1215 self.assertEqual(self.builds[5].name, 'project-test2')
1216 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001217
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001218 self.worker.hold_jobs_in_build = False
1219 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001220 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001221 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001222
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001223 self.assertEqual(len(self.history), 9)
1224 self.assertEqual(A.data['status'], 'MERGED')
1225 self.assertEqual(B.data['status'], 'MERGED')
1226 self.assertEqual(C.data['status'], 'MERGED')
1227 self.assertEqual(A.reported, 2)
1228 self.assertEqual(B.reported, 2)
1229 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001230
1231 def test_failed_changes(self):
1232 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001233 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001234
James E. Blairb02a3bb2012-07-30 17:49:55 -07001235 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1236 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001237 A.addApproval('CRVW', 2)
1238 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001239
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001240 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001241
James E. Blaire2819012013-06-28 17:17:26 -04001242 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1243 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001244 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001245
1246 self.worker.release('.*-merge')
1247 self.waitUntilSettled()
1248
1249 self.worker.hold_jobs_in_build = False
1250 self.worker.release()
1251
1252 self.waitUntilSettled()
1253 # It's certain that the merge job for change 2 will run, but
1254 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001255 self.assertTrue(len(self.history) > 6)
1256 self.assertEqual(A.data['status'], 'NEW')
1257 self.assertEqual(B.data['status'], 'MERGED')
1258 self.assertEqual(A.reported, 2)
1259 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001260
1261 def test_independent_queues(self):
1262 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001263
1264 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001265 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001266 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1267 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001268 A.addApproval('CRVW', 2)
1269 B.addApproval('CRVW', 2)
1270 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001271
1272 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1273 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1274 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1275
James E. Blairb02a3bb2012-07-30 17:49:55 -07001276 self.waitUntilSettled()
1277
1278 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001279 self.assertEqual(len(self.builds), 2)
1280 self.assertEqual(self.builds[0].name, 'project-merge')
1281 self.assertTrue(self.job_has_changes(self.builds[0], A))
1282 self.assertEqual(self.builds[1].name, 'project1-merge')
1283 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001284
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001285 # Release the current merge builds
1286 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001287 self.waitUntilSettled()
1288 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001289 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001290 self.waitUntilSettled()
1291
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001292 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001293 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001294 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001295
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001296 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001297 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001298 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001299
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001300 self.assertEqual(len(self.history), 11)
1301 self.assertEqual(A.data['status'], 'MERGED')
1302 self.assertEqual(B.data['status'], 'MERGED')
1303 self.assertEqual(C.data['status'], 'MERGED')
1304 self.assertEqual(A.reported, 2)
1305 self.assertEqual(B.reported, 2)
1306 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001307
1308 def test_failed_change_at_head(self):
1309 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001310
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001311 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001312 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1313 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1314 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001315 A.addApproval('CRVW', 2)
1316 B.addApproval('CRVW', 2)
1317 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001318
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001319 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001320
1321 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1322 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1323 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1324
1325 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001326
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001327 self.assertEqual(len(self.builds), 1)
1328 self.assertEqual(self.builds[0].name, 'project-merge')
1329 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001330
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001331 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001332 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001333 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001334 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001335 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001336 self.waitUntilSettled()
1337
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001338 self.assertEqual(len(self.builds), 6)
1339 self.assertEqual(self.builds[0].name, 'project-test1')
1340 self.assertEqual(self.builds[1].name, 'project-test2')
1341 self.assertEqual(self.builds[2].name, 'project-test1')
1342 self.assertEqual(self.builds[3].name, 'project-test2')
1343 self.assertEqual(self.builds[4].name, 'project-test1')
1344 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001345
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001346 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001347 self.waitUntilSettled()
1348
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001349 # project-test2, project-merge for B
1350 self.assertEqual(len(self.builds), 2)
1351 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001352
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001353 self.worker.hold_jobs_in_build = False
1354 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001355 self.waitUntilSettled()
1356
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001357 self.assertEqual(len(self.builds), 0)
1358 self.assertEqual(len(self.history), 15)
1359 self.assertEqual(A.data['status'], 'NEW')
1360 self.assertEqual(B.data['status'], 'MERGED')
1361 self.assertEqual(C.data['status'], 'MERGED')
1362 self.assertEqual(A.reported, 2)
1363 self.assertEqual(B.reported, 2)
1364 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001365
James E. Blair0aac4872013-08-23 14:02:38 -07001366 def test_failed_change_in_middle(self):
1367 "Test a failed change in the middle of the queue"
1368
1369 self.worker.hold_jobs_in_build = True
1370 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1371 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1372 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1373 A.addApproval('CRVW', 2)
1374 B.addApproval('CRVW', 2)
1375 C.addApproval('CRVW', 2)
1376
1377 self.worker.addFailTest('project-test1', B)
1378
1379 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1380 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1381 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1382
1383 self.waitUntilSettled()
1384
1385 self.worker.release('.*-merge')
1386 self.waitUntilSettled()
1387 self.worker.release('.*-merge')
1388 self.waitUntilSettled()
1389 self.worker.release('.*-merge')
1390 self.waitUntilSettled()
1391
1392 self.assertEqual(len(self.builds), 6)
1393 self.assertEqual(self.builds[0].name, 'project-test1')
1394 self.assertEqual(self.builds[1].name, 'project-test2')
1395 self.assertEqual(self.builds[2].name, 'project-test1')
1396 self.assertEqual(self.builds[3].name, 'project-test2')
1397 self.assertEqual(self.builds[4].name, 'project-test1')
1398 self.assertEqual(self.builds[5].name, 'project-test2')
1399
1400 self.release(self.builds[2])
1401 self.waitUntilSettled()
1402
James E. Blair972e3c72013-08-29 12:04:55 -07001403 # project-test1 and project-test2 for A
1404 # project-test2 for B
1405 # project-merge for C (without B)
1406 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -07001407 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1408
James E. Blair972e3c72013-08-29 12:04:55 -07001409 self.worker.release('.*-merge')
1410 self.waitUntilSettled()
1411
1412 # project-test1 and project-test2 for A
1413 # project-test2 for B
1414 # project-test1 and project-test2 for C
1415 self.assertEqual(len(self.builds), 5)
1416
James E. Blair0aac4872013-08-23 14:02:38 -07001417 items = self.sched.layout.pipelines['gate'].getAllItems()
1418 builds = items[0].current_build_set.getBuilds()
1419 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1420 self.assertEqual(self.countJobResults(builds, None), 2)
1421 builds = items[1].current_build_set.getBuilds()
1422 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1423 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1424 self.assertEqual(self.countJobResults(builds, None), 1)
1425 builds = items[2].current_build_set.getBuilds()
1426 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -07001427 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -07001428
1429 self.worker.hold_jobs_in_build = False
1430 self.worker.release()
1431 self.waitUntilSettled()
1432
1433 self.assertEqual(len(self.builds), 0)
1434 self.assertEqual(len(self.history), 12)
1435 self.assertEqual(A.data['status'], 'MERGED')
1436 self.assertEqual(B.data['status'], 'NEW')
1437 self.assertEqual(C.data['status'], 'MERGED')
1438 self.assertEqual(A.reported, 2)
1439 self.assertEqual(B.reported, 2)
1440 self.assertEqual(C.reported, 2)
1441
James E. Blaird466dc42012-07-31 10:42:56 -07001442 def test_failed_change_at_head_with_queue(self):
1443 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001444
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001445 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001446 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1447 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1448 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001449 A.addApproval('CRVW', 2)
1450 B.addApproval('CRVW', 2)
1451 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001452
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001453 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001454
1455 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1456 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1457 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1458
1459 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001460 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001461 self.assertEqual(len(self.builds), 0)
1462 self.assertEqual(len(queue), 1)
1463 self.assertEqual(queue[0].name, 'build:project-merge')
1464 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001465
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001466 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001467 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001468 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001469 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001470 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001471 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001472 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001473
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001474 self.assertEqual(len(self.builds), 0)
1475 self.assertEqual(len(queue), 6)
1476 self.assertEqual(queue[0].name, 'build:project-test1')
1477 self.assertEqual(queue[1].name, 'build:project-test2')
1478 self.assertEqual(queue[2].name, 'build:project-test1')
1479 self.assertEqual(queue[3].name, 'build:project-test2')
1480 self.assertEqual(queue[4].name, 'build:project-test1')
1481 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001482
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001483 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001484 self.waitUntilSettled()
1485
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001486 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001487 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001488 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1489 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001490
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001491 self.gearman_server.hold_jobs_in_queue = False
1492 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001493 self.waitUntilSettled()
1494
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001495 self.assertEqual(len(self.builds), 0)
1496 self.assertEqual(len(self.history), 11)
1497 self.assertEqual(A.data['status'], 'NEW')
1498 self.assertEqual(B.data['status'], 'MERGED')
1499 self.assertEqual(C.data['status'], 'MERGED')
1500 self.assertEqual(A.reported, 2)
1501 self.assertEqual(B.reported, 2)
1502 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001503
1504 def test_patch_order(self):
1505 "Test that dependent patches are tested in the right order"
1506 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1507 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1508 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1509 A.addApproval('CRVW', 2)
1510 B.addApproval('CRVW', 2)
1511 C.addApproval('CRVW', 2)
1512
1513 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1514 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1515 M2.setMerged()
1516 M1.setMerged()
1517
1518 # C -> B -> A -> M1 -> M2
1519 # M2 is here to make sure it is never queried. If it is, it
1520 # means zuul is walking down the entire history of merged
1521 # changes.
1522
1523 C.setDependsOn(B, 1)
1524 B.setDependsOn(A, 1)
1525 A.setDependsOn(M1, 1)
1526 M1.setDependsOn(M2, 1)
1527
1528 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1529
1530 self.waitUntilSettled()
1531
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001532 self.assertEqual(A.data['status'], 'NEW')
1533 self.assertEqual(B.data['status'], 'NEW')
1534 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001535
1536 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1537 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1538
1539 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001540 self.assertEqual(M2.queried, 0)
1541 self.assertEqual(A.data['status'], 'MERGED')
1542 self.assertEqual(B.data['status'], 'MERGED')
1543 self.assertEqual(C.data['status'], 'MERGED')
1544 self.assertEqual(A.reported, 2)
1545 self.assertEqual(B.reported, 2)
1546 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001547
James E. Blair0e933c52013-07-11 10:18:52 -07001548 def test_trigger_cache(self):
1549 "Test that the trigger cache operates correctly"
1550 self.worker.hold_jobs_in_build = True
1551
1552 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1553 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1554 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1555 A.addApproval('CRVW', 2)
1556 B.addApproval('CRVW', 2)
1557
1558 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1559 M1.setMerged()
1560
1561 B.setDependsOn(A, 1)
1562 A.setDependsOn(M1, 1)
1563
1564 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1565 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1566
1567 self.waitUntilSettled()
1568
1569 for build in self.builds:
1570 if build.parameters['ZUUL_PIPELINE'] == 'check':
1571 build.release()
1572 self.waitUntilSettled()
1573 for build in self.builds:
1574 if build.parameters['ZUUL_PIPELINE'] == 'check':
1575 build.release()
1576 self.waitUntilSettled()
1577
1578 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1579 self.waitUntilSettled()
1580
James E. Blair6c358e72013-07-29 17:06:47 -07001581 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001582 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001583 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001584
1585 self.worker.hold_jobs_in_build = False
1586 self.worker.release()
1587 self.waitUntilSettled()
1588
1589 self.assertEqual(A.data['status'], 'MERGED')
1590 self.assertEqual(B.data['status'], 'MERGED')
1591 self.assertEqual(A.queried, 2) # Initial and isMerged
1592 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1593
James E. Blair8c803f82012-07-31 16:25:42 -07001594 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001595 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001596 # TODO: move to test_gerrit (this is a unit test!)
1597 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001598 trigger = self.sched.layout.pipelines['gate'].trigger
1599 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001600 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001601 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001602
1603 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001604 a = trigger.getChange(1, 2, refresh=True)
1605 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001606
1607 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001608 a = trigger.getChange(1, 2, refresh=True)
1609 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1610 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001611
1612 def test_build_configuration(self):
1613 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001614
1615 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001616 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1617 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1618 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1619 A.addApproval('CRVW', 2)
1620 B.addApproval('CRVW', 2)
1621 C.addApproval('CRVW', 2)
1622 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1623 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1624 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1625 self.waitUntilSettled()
1626
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001627 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001628 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001629 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001630 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001631 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001632 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001633 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001634 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1635 self.gearman_server.hold_jobs_in_queue = False
1636 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001637 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001638
Monty Taylorbc758832013-06-17 17:22:42 -04001639 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001640 repo = git.Repo(path)
1641 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1642 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001643 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001644 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001645
1646 def test_build_configuration_conflict(self):
1647 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001648
1649 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001650 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1651 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001652 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001653 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1654 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001655 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001656 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1657 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001658 A.addApproval('CRVW', 2)
1659 B.addApproval('CRVW', 2)
1660 C.addApproval('CRVW', 2)
1661 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1662 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1663 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1664 self.waitUntilSettled()
1665
James E. Blair6736beb2013-07-11 15:18:15 -07001666 self.assertEqual(A.reported, 1)
1667 self.assertEqual(B.reported, 1)
1668 self.assertEqual(C.reported, 1)
1669
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001670 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001671 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001672 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001673 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001674 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001675 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001676
1677 self.assertEqual(len(self.history), 2) # A and C merge jobs
1678
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001679 self.gearman_server.hold_jobs_in_queue = False
1680 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001681 self.waitUntilSettled()
1682
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001683 self.assertEqual(A.data['status'], 'MERGED')
1684 self.assertEqual(B.data['status'], 'NEW')
1685 self.assertEqual(C.data['status'], 'MERGED')
1686 self.assertEqual(A.reported, 2)
1687 self.assertEqual(B.reported, 2)
1688 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001689 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001690
James E. Blairdaabed22012-08-15 15:38:57 -07001691 def test_post(self):
1692 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001693
Zhongyue Luo5d556072012-09-21 02:00:47 +09001694 e = {
1695 "type": "ref-updated",
1696 "submitter": {
1697 "name": "User Name",
1698 },
1699 "refUpdate": {
1700 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1701 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1702 "refName": "master",
1703 "project": "org/project",
1704 }
1705 }
James E. Blairdaabed22012-08-15 15:38:57 -07001706 self.fake_gerrit.addEvent(e)
1707 self.waitUntilSettled()
1708
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001709 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001710 self.assertEqual(len(self.history), 1)
1711 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001712
1713 def test_build_configuration_branch(self):
1714 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001715
1716 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001717 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1718 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1719 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1720 A.addApproval('CRVW', 2)
1721 B.addApproval('CRVW', 2)
1722 C.addApproval('CRVW', 2)
1723 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1724 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1725 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1726 self.waitUntilSettled()
1727
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001728 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001729 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001730 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001731 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001732 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001733 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001734 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001735 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1736 self.gearman_server.hold_jobs_in_queue = False
1737 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001738 self.waitUntilSettled()
1739
Monty Taylorbc758832013-06-17 17:22:42 -04001740 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001741 repo = git.Repo(path)
1742 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1743 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001744 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001745 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001746
1747 def test_build_configuration_branch_interaction(self):
1748 "Test that switching between branches works"
1749 self.test_build_configuration()
1750 self.test_build_configuration_branch()
1751 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001752 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001753 repo = git.Repo(path)
1754 repo.heads.master.commit = repo.commit('init')
1755 self.test_build_configuration()
1756
1757 def test_build_configuration_multi_branch(self):
1758 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001759
1760 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001761 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1762 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1763 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1764 A.addApproval('CRVW', 2)
1765 B.addApproval('CRVW', 2)
1766 C.addApproval('CRVW', 2)
1767 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1768 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1769 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1770 self.waitUntilSettled()
1771
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001772 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001773 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001774 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001775 job_B = None
1776 for job in queue:
1777 if 'project-merge' in job.name:
1778 job_B = job
1779 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001780 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001781 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001782 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001783 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001784 for job in queue:
1785 if 'project-merge' in job.name:
1786 job_C = job
1787 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001788 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001789 self.gearman_server.hold_jobs_in_queue = False
1790 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001791 self.waitUntilSettled()
1792
Monty Taylorbc758832013-06-17 17:22:42 -04001793 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001794 repo = git.Repo(path)
1795
1796 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001797 for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001798 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001799 correct_messages = ['initial commit', 'A-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001800 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001801
1802 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001803 for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001804 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001805 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001806 self.assertEqual(repo_messages, correct_messages)
James E. Blair7f71c802012-08-22 13:04:32 -07001807
1808 def test_one_job_project(self):
1809 "Test that queueing works with one job"
1810 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1811 'master', 'A')
1812 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1813 'master', 'B')
1814 A.addApproval('CRVW', 2)
1815 B.addApproval('CRVW', 2)
1816 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1817 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1818 self.waitUntilSettled()
1819
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001820 self.assertEqual(A.data['status'], 'MERGED')
1821 self.assertEqual(A.reported, 2)
1822 self.assertEqual(B.data['status'], 'MERGED')
1823 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001824
Antoine Musso80edd5a2013-02-13 15:37:53 +01001825 def test_job_from_templates_launched(self):
1826 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001827
Antoine Musso80edd5a2013-02-13 15:37:53 +01001828 A = self.fake_gerrit.addFakeChange(
1829 'org/templated-project', 'master', 'A')
1830 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1831 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001832
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001833 self.assertEqual(self.getJobFromHistory('project-test1').result,
1834 'SUCCESS')
1835 self.assertEqual(self.getJobFromHistory('project-test2').result,
1836 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001837
James E. Blaircaec0c52012-08-22 14:52:22 -07001838 def test_dependent_changes_dequeue(self):
1839 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001840
James E. Blaircaec0c52012-08-22 14:52:22 -07001841 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1842 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1843 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1844 A.addApproval('CRVW', 2)
1845 B.addApproval('CRVW', 2)
1846 C.addApproval('CRVW', 2)
1847
1848 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1849 M1.setMerged()
1850
1851 # C -> B -> A -> M1
1852
1853 C.setDependsOn(B, 1)
1854 B.setDependsOn(A, 1)
1855 A.setDependsOn(M1, 1)
1856
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001857 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001858
1859 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1860 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1861 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1862
1863 self.waitUntilSettled()
1864
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001865 self.assertEqual(A.data['status'], 'NEW')
1866 self.assertEqual(A.reported, 2)
1867 self.assertEqual(B.data['status'], 'NEW')
1868 self.assertEqual(B.reported, 2)
1869 self.assertEqual(C.data['status'], 'NEW')
1870 self.assertEqual(C.reported, 2)
1871 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001872
James E. Blair972e3c72013-08-29 12:04:55 -07001873 def test_failing_dependent_changes(self):
1874 "Test that failing dependent patches are taken out of stream"
1875 self.worker.hold_jobs_in_build = True
1876 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1877 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1878 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1879 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1880 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
1881 A.addApproval('CRVW', 2)
1882 B.addApproval('CRVW', 2)
1883 C.addApproval('CRVW', 2)
1884 D.addApproval('CRVW', 2)
1885 E.addApproval('CRVW', 2)
1886
1887 # E, D -> C -> B, A
1888
1889 D.setDependsOn(C, 1)
1890 C.setDependsOn(B, 1)
1891
1892 self.worker.addFailTest('project-test1', B)
1893
1894 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1895 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1896 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1897 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1898 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1899
1900 self.waitUntilSettled()
1901 self.worker.release('.*-merge')
1902 self.waitUntilSettled()
1903 self.worker.release('.*-merge')
1904 self.waitUntilSettled()
1905 self.worker.release('.*-merge')
1906 self.waitUntilSettled()
1907 self.worker.release('.*-merge')
1908 self.waitUntilSettled()
1909 self.worker.release('.*-merge')
1910 self.waitUntilSettled()
1911
1912 self.worker.hold_jobs_in_build = False
1913 for build in self.builds:
1914 if build.parameters['ZUUL_CHANGE'] != '1':
1915 build.release()
1916 self.waitUntilSettled()
1917
1918 self.worker.release()
1919 self.waitUntilSettled()
1920
1921 self.assertEqual(A.data['status'], 'MERGED')
1922 self.assertEqual(A.reported, 2)
1923 self.assertEqual(B.data['status'], 'NEW')
1924 self.assertEqual(B.reported, 2)
1925 self.assertEqual(C.data['status'], 'NEW')
1926 self.assertEqual(C.reported, 2)
1927 self.assertEqual(D.data['status'], 'NEW')
1928 self.assertEqual(D.reported, 2)
1929 self.assertEqual(E.data['status'], 'MERGED')
1930 self.assertEqual(E.reported, 2)
1931 self.assertEqual(len(self.history), 18)
1932
James E. Blairec590122012-08-22 15:19:31 -07001933 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001934 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001935 # If it's dequeued more than once, we should see extra
1936 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001937
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001938 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001939 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1940 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1941 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1942 A.addApproval('CRVW', 2)
1943 B.addApproval('CRVW', 2)
1944 C.addApproval('CRVW', 2)
1945
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001946 self.worker.addFailTest('project1-test1', A)
1947 self.worker.addFailTest('project1-test2', A)
1948 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001949
1950 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1951 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1952 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1953
1954 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001955
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001956 self.assertEqual(len(self.builds), 1)
1957 self.assertEqual(self.builds[0].name, 'project1-merge')
1958 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07001959
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001960 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001961 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001962 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001963 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001964 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001965 self.waitUntilSettled()
1966
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001967 self.assertEqual(len(self.builds), 9)
1968 self.assertEqual(self.builds[0].name, 'project1-test1')
1969 self.assertEqual(self.builds[1].name, 'project1-test2')
1970 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
1971 self.assertEqual(self.builds[3].name, 'project1-test1')
1972 self.assertEqual(self.builds[4].name, 'project1-test2')
1973 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
1974 self.assertEqual(self.builds[6].name, 'project1-test1')
1975 self.assertEqual(self.builds[7].name, 'project1-test2')
1976 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001977
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001978 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001979 self.waitUntilSettled()
1980
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001981 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1982 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001983
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001984 self.worker.hold_jobs_in_build = False
1985 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001986 self.waitUntilSettled()
1987
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001988 self.assertEqual(len(self.builds), 0)
1989 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001990
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001991 self.assertEqual(A.data['status'], 'NEW')
1992 self.assertEqual(B.data['status'], 'MERGED')
1993 self.assertEqual(C.data['status'], 'MERGED')
1994 self.assertEqual(A.reported, 2)
1995 self.assertEqual(B.reported, 2)
1996 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001997
1998 def test_nonvoting_job(self):
1999 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002000
James E. Blair4ec821f2012-08-23 15:28:28 -07002001 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
2002 'master', 'A')
2003 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002004 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07002005 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2006
2007 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07002008
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002009 self.assertEqual(A.data['status'], 'MERGED')
2010 self.assertEqual(A.reported, 2)
2011 self.assertEqual(
2012 self.getJobFromHistory('nonvoting-project-merge').result,
2013 'SUCCESS')
2014 self.assertEqual(
2015 self.getJobFromHistory('nonvoting-project-test1').result,
2016 'SUCCESS')
2017 self.assertEqual(
2018 self.getJobFromHistory('nonvoting-project-test2').result,
2019 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07002020
2021 def test_check_queue_success(self):
2022 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002023
James E. Blaire0487072012-08-29 17:38:31 -07002024 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2025 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2026
2027 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002028
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002029 self.assertEqual(A.data['status'], 'NEW')
2030 self.assertEqual(A.reported, 1)
2031 self.assertEqual(self.getJobFromHistory('project-merge').result,
2032 'SUCCESS')
2033 self.assertEqual(self.getJobFromHistory('project-test1').result,
2034 'SUCCESS')
2035 self.assertEqual(self.getJobFromHistory('project-test2').result,
2036 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07002037
2038 def test_check_queue_failure(self):
2039 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002040
James E. Blaire0487072012-08-29 17:38:31 -07002041 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002042 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07002043 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2044
2045 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002046
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002047 self.assertEqual(A.data['status'], 'NEW')
2048 self.assertEqual(A.reported, 1)
2049 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07002050 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002051 self.assertEqual(self.getJobFromHistory('project-test1').result,
2052 'SUCCESS')
2053 self.assertEqual(self.getJobFromHistory('project-test2').result,
2054 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07002055
2056 def test_dependent_behind_dequeue(self):
2057 "test that dependent changes behind dequeued changes work"
2058 # This complicated test is a reproduction of a real life bug
2059 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07002060
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002061 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07002062 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2063 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2064 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2065 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2066 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
2067 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
2068 D.setDependsOn(C, 1)
2069 E.setDependsOn(D, 1)
2070 A.addApproval('CRVW', 2)
2071 B.addApproval('CRVW', 2)
2072 C.addApproval('CRVW', 2)
2073 D.addApproval('CRVW', 2)
2074 E.addApproval('CRVW', 2)
2075 F.addApproval('CRVW', 2)
2076
2077 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07002078
2079 # Change object re-use in the gerrit trigger is hidden if
2080 # changes are added in quick succession; waiting makes it more
2081 # like real life.
2082 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2083 self.waitUntilSettled()
2084 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2085 self.waitUntilSettled()
2086
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002087 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002088 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002089 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002090 self.waitUntilSettled()
2091
2092 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2093 self.waitUntilSettled()
2094 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2095 self.waitUntilSettled()
2096 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2097 self.waitUntilSettled()
2098 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
2099 self.waitUntilSettled()
2100
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002101 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002102 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002103 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002104 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002105 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002106 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002107 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002108 self.waitUntilSettled()
2109
2110 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002111
2112 # Grab pointers to the jobs we want to release before
2113 # releasing any, because list indexes may change as
2114 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002115 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002116 a.release()
2117 b.release()
2118 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002119 self.waitUntilSettled()
2120
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002121 self.worker.hold_jobs_in_build = False
2122 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002123 self.waitUntilSettled()
2124
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002125 self.assertEqual(A.data['status'], 'NEW')
2126 self.assertEqual(B.data['status'], 'MERGED')
2127 self.assertEqual(C.data['status'], 'MERGED')
2128 self.assertEqual(D.data['status'], 'MERGED')
2129 self.assertEqual(E.data['status'], 'MERGED')
2130 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002131
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002132 self.assertEqual(A.reported, 2)
2133 self.assertEqual(B.reported, 2)
2134 self.assertEqual(C.reported, 2)
2135 self.assertEqual(D.reported, 2)
2136 self.assertEqual(E.reported, 2)
2137 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002138
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002139 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2140 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002141
2142 def test_merger_repack(self):
2143 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002144
James E. Blair05fed602012-09-07 12:45:24 -07002145 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2146 A.addApproval('CRVW', 2)
2147 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2148 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002149 self.assertEqual(self.getJobFromHistory('project-merge').result,
2150 'SUCCESS')
2151 self.assertEqual(self.getJobFromHistory('project-test1').result,
2152 'SUCCESS')
2153 self.assertEqual(self.getJobFromHistory('project-test2').result,
2154 'SUCCESS')
2155 self.assertEqual(A.data['status'], 'MERGED')
2156 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07002157 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002158 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002159
Monty Taylorbc758832013-06-17 17:22:42 -04002160 path = os.path.join(self.git_root, "org/project")
2161 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002162
2163 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2164 A.addApproval('CRVW', 2)
2165 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2166 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002167 self.assertEqual(self.getJobFromHistory('project-merge').result,
2168 'SUCCESS')
2169 self.assertEqual(self.getJobFromHistory('project-test1').result,
2170 'SUCCESS')
2171 self.assertEqual(self.getJobFromHistory('project-test2').result,
2172 'SUCCESS')
2173 self.assertEqual(A.data['status'], 'MERGED')
2174 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002175
James E. Blair4886f282012-11-15 09:27:33 -08002176 def test_merger_repack_large_change(self):
2177 "Test that the merger works with large changes after a repack"
2178 # https://bugs.launchpad.net/zuul/+bug/1078946
2179 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2180 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002181 path = os.path.join(self.upstream_root, "org/project1")
2182 print repack_repo(path)
2183 path = os.path.join(self.git_root, "org/project1")
2184 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002185
2186 A.addApproval('CRVW', 2)
2187 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2188 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002189 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2190 'SUCCESS')
2191 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2192 'SUCCESS')
2193 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2194 'SUCCESS')
2195 self.assertEqual(A.data['status'], 'MERGED')
2196 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002197
James E. Blair7ee88a22012-09-12 18:59:31 +02002198 def test_nonexistent_job(self):
2199 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002200 # Set to the state immediately after a restart
2201 self.resetGearmanServer()
2202 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002203
2204 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2205 A.addApproval('CRVW', 2)
2206 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2207 # There may be a thread about to report a lost change
2208 while A.reported < 2:
2209 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002210 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002211 self.assertFalse(job_names)
2212 self.assertEqual(A.data['status'], 'NEW')
2213 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002214 self.assertEmptyQueues()
2215
2216 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002217 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002218 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2219 A.addApproval('CRVW', 2)
2220 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2221 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002222 self.assertEqual(self.getJobFromHistory('project-merge').result,
2223 'SUCCESS')
2224 self.assertEqual(self.getJobFromHistory('project-test1').result,
2225 'SUCCESS')
2226 self.assertEqual(self.getJobFromHistory('project-test2').result,
2227 'SUCCESS')
2228 self.assertEqual(A.data['status'], 'MERGED')
2229 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002230
2231 def test_single_nonexistent_post_job(self):
2232 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002233 e = {
2234 "type": "ref-updated",
2235 "submitter": {
2236 "name": "User Name",
2237 },
2238 "refUpdate": {
2239 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2240 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2241 "refName": "master",
2242 "project": "org/project",
2243 }
2244 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002245 # Set to the state immediately after a restart
2246 self.resetGearmanServer()
2247 self.launcher.negative_function_cache_ttl = 0
2248
James E. Blairf62d4282012-12-31 17:01:50 -08002249 self.fake_gerrit.addEvent(e)
2250 self.waitUntilSettled()
2251
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002252 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002253
2254 def test_new_patchset_dequeues_old(self):
2255 "Test that a new patchset causes the old to be dequeued"
2256 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002257 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002258 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2259 M.setMerged()
2260
2261 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2262 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2263 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2264 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2265 A.addApproval('CRVW', 2)
2266 B.addApproval('CRVW', 2)
2267 C.addApproval('CRVW', 2)
2268 D.addApproval('CRVW', 2)
2269
2270 C.setDependsOn(B, 1)
2271 B.setDependsOn(A, 1)
2272 A.setDependsOn(M, 1)
2273
2274 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2275 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2276 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2277 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2278 self.waitUntilSettled()
2279
2280 B.addPatchset()
2281 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2282 self.waitUntilSettled()
2283
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002284 self.worker.hold_jobs_in_build = False
2285 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002286 self.waitUntilSettled()
2287
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002288 self.assertEqual(A.data['status'], 'MERGED')
2289 self.assertEqual(A.reported, 2)
2290 self.assertEqual(B.data['status'], 'NEW')
2291 self.assertEqual(B.reported, 2)
2292 self.assertEqual(C.data['status'], 'NEW')
2293 self.assertEqual(C.reported, 2)
2294 self.assertEqual(D.data['status'], 'MERGED')
2295 self.assertEqual(D.reported, 2)
2296 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002297
2298 def test_new_patchset_dequeues_old_on_head(self):
2299 "Test that a new patchset causes the old to be dequeued (at head)"
2300 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002301 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002302 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2303 M.setMerged()
2304 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2305 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2306 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2307 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2308 A.addApproval('CRVW', 2)
2309 B.addApproval('CRVW', 2)
2310 C.addApproval('CRVW', 2)
2311 D.addApproval('CRVW', 2)
2312
2313 C.setDependsOn(B, 1)
2314 B.setDependsOn(A, 1)
2315 A.setDependsOn(M, 1)
2316
2317 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2318 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2319 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2320 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2321 self.waitUntilSettled()
2322
2323 A.addPatchset()
2324 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2325 self.waitUntilSettled()
2326
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002327 self.worker.hold_jobs_in_build = False
2328 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002329 self.waitUntilSettled()
2330
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002331 self.assertEqual(A.data['status'], 'NEW')
2332 self.assertEqual(A.reported, 2)
2333 self.assertEqual(B.data['status'], 'NEW')
2334 self.assertEqual(B.reported, 2)
2335 self.assertEqual(C.data['status'], 'NEW')
2336 self.assertEqual(C.reported, 2)
2337 self.assertEqual(D.data['status'], 'MERGED')
2338 self.assertEqual(D.reported, 2)
2339 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002340
2341 def test_new_patchset_dequeues_old_without_dependents(self):
2342 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002343 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002344 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2345 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2346 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2347 A.addApproval('CRVW', 2)
2348 B.addApproval('CRVW', 2)
2349 C.addApproval('CRVW', 2)
2350
2351 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2352 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2353 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2354 self.waitUntilSettled()
2355
2356 B.addPatchset()
2357 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2358 self.waitUntilSettled()
2359
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002360 self.worker.hold_jobs_in_build = False
2361 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002362 self.waitUntilSettled()
2363
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002364 self.assertEqual(A.data['status'], 'MERGED')
2365 self.assertEqual(A.reported, 2)
2366 self.assertEqual(B.data['status'], 'NEW')
2367 self.assertEqual(B.reported, 2)
2368 self.assertEqual(C.data['status'], 'MERGED')
2369 self.assertEqual(C.reported, 2)
2370 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002371
2372 def test_new_patchset_dequeues_old_independent_queue(self):
2373 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002374 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002375 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2376 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2377 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2378 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2379 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2380 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2381 self.waitUntilSettled()
2382
2383 B.addPatchset()
2384 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2385 self.waitUntilSettled()
2386
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002387 self.worker.hold_jobs_in_build = False
2388 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002389 self.waitUntilSettled()
2390
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002391 self.assertEqual(A.data['status'], 'NEW')
2392 self.assertEqual(A.reported, 1)
2393 self.assertEqual(B.data['status'], 'NEW')
2394 self.assertEqual(B.reported, 1)
2395 self.assertEqual(C.data['status'], 'NEW')
2396 self.assertEqual(C.reported, 1)
2397 self.assertEqual(len(self.history), 10)
2398 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002399
2400 def test_zuul_refs(self):
2401 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002402 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002403 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2404 M1.setMerged()
2405 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2406 M2.setMerged()
2407
2408 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2409 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2410 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2411 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2412 A.addApproval('CRVW', 2)
2413 B.addApproval('CRVW', 2)
2414 C.addApproval('CRVW', 2)
2415 D.addApproval('CRVW', 2)
2416 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2417 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2418 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2419 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2420
2421 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002422 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002423 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002424 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002425 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002426 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002427 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002428 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002429 self.waitUntilSettled()
2430
James E. Blair7d0dedc2013-02-21 17:26:09 -08002431 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002432 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002433 if x.parameters['ZUUL_CHANGE'] == '3':
2434 a_zref = x.parameters['ZUUL_REF']
2435 if x.parameters['ZUUL_CHANGE'] == '4':
2436 b_zref = x.parameters['ZUUL_REF']
2437 if x.parameters['ZUUL_CHANGE'] == '5':
2438 c_zref = x.parameters['ZUUL_REF']
2439 if x.parameters['ZUUL_CHANGE'] == '6':
2440 d_zref = x.parameters['ZUUL_REF']
2441
2442 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002443 self.assertIsNotNone(a_zref)
2444 self.assertIsNotNone(b_zref)
2445 self.assertIsNotNone(c_zref)
2446 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002447
2448 # And they should all be different
2449 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002450 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002451
2452 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002453 self.assertTrue(self.ref_has_change(a_zref, A))
2454 self.assertFalse(self.ref_has_change(a_zref, B))
2455 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002456
2457 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002458 self.assertTrue(self.ref_has_change(b_zref, A))
2459 self.assertTrue(self.ref_has_change(b_zref, B))
2460 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002461
2462 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002463 self.assertTrue(self.ref_has_change(c_zref, A))
2464 self.assertTrue(self.ref_has_change(c_zref, B))
2465 self.assertTrue(self.ref_has_change(c_zref, C))
2466 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002467
2468 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002469 self.assertTrue(self.ref_has_change(d_zref, A))
2470 self.assertTrue(self.ref_has_change(d_zref, B))
2471 self.assertTrue(self.ref_has_change(d_zref, C))
2472 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002473
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002474 self.worker.hold_jobs_in_build = False
2475 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002476 self.waitUntilSettled()
2477
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002478 self.assertEqual(A.data['status'], 'MERGED')
2479 self.assertEqual(A.reported, 2)
2480 self.assertEqual(B.data['status'], 'MERGED')
2481 self.assertEqual(B.reported, 2)
2482 self.assertEqual(C.data['status'], 'MERGED')
2483 self.assertEqual(C.reported, 2)
2484 self.assertEqual(D.data['status'], 'MERGED')
2485 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002486
James E. Blair4a28a882013-08-23 15:17:33 -07002487 def test_rerun_on_error(self):
2488 "Test that if a worker fails to run a job, it is run again"
2489 self.worker.hold_jobs_in_build = True
2490 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2491 A.addApproval('CRVW', 2)
2492 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2493 self.waitUntilSettled()
2494
2495 self.builds[0].run_error = True
2496 self.worker.hold_jobs_in_build = False
2497 self.worker.release()
2498 self.waitUntilSettled()
2499 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2500 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2501
James E. Blair412e5582013-04-22 15:50:12 -07002502 def test_statsd(self):
2503 "Test each of the statsd methods used in the scheduler"
2504 import extras
2505 statsd = extras.try_import('statsd.statsd')
2506 statsd.incr('test-incr')
2507 statsd.timing('test-timing', 3)
2508 statsd.gauge('test-guage', 12)
2509 self.assertReportedStat('test-incr', '1|c')
2510 self.assertReportedStat('test-timing', '3|ms')
2511 self.assertReportedStat('test-guage', '12|g')
2512
James E. Blair70c71582013-03-06 08:50:50 -08002513 def test_file_jobs(self):
2514 "Test that file jobs run only when appropriate"
2515 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2516 A.addPatchset(['pip-requires'])
2517 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2518 A.addApproval('CRVW', 2)
2519 B.addApproval('CRVW', 2)
2520 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2521 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2522 self.waitUntilSettled()
2523
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002524 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002525 if x.name == 'project-testfile']
2526
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002527 self.assertEqual(len(testfile_jobs), 1)
2528 self.assertEqual(testfile_jobs[0].changes, '1,2')
2529 self.assertEqual(A.data['status'], 'MERGED')
2530 self.assertEqual(A.reported, 2)
2531 self.assertEqual(B.data['status'], 'MERGED')
2532 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002533
2534 def test_test_config(self):
2535 "Test that we can test the config"
2536 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002537 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002538 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002539 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002540
2541 def test_build_description(self):
2542 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002543 self.worker.registerFunction('set_description:' +
2544 self.worker.worker_id)
2545
2546 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2547 A.addApproval('CRVW', 2)
2548 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2549 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002550 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002551 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002552 self.assertTrue(re.search("Branch.*master", desc))
2553 self.assertTrue(re.search("Pipeline.*gate", desc))
2554 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2555 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2556 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2557 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002558
James E. Blair64ed6f22013-07-10 14:07:23 -07002559 def test_queue_precedence(self):
2560 "Test that queue precedence works"
2561
2562 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002563 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002564 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2565 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2566 A.addApproval('CRVW', 2)
2567 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2568
2569 self.waitUntilSettled()
2570 self.gearman_server.hold_jobs_in_queue = False
2571 self.gearman_server.release()
2572 self.waitUntilSettled()
2573
James E. Blair8de58bd2013-07-18 16:23:33 -07002574 # Run one build at a time to ensure non-race order:
2575 for x in range(6):
2576 self.release(self.builds[0])
2577 self.waitUntilSettled()
2578 self.worker.hold_jobs_in_build = False
2579 self.waitUntilSettled()
2580
James E. Blair64ed6f22013-07-10 14:07:23 -07002581 self.log.debug(self.history)
2582 self.assertEqual(self.history[0].pipeline, 'gate')
2583 self.assertEqual(self.history[1].pipeline, 'check')
2584 self.assertEqual(self.history[2].pipeline, 'gate')
2585 self.assertEqual(self.history[3].pipeline, 'gate')
2586 self.assertEqual(self.history[4].pipeline, 'check')
2587 self.assertEqual(self.history[5].pipeline, 'check')
2588
James E. Blair1843a552013-07-03 14:19:52 -07002589 def test_json_status(self):
2590 "Test that we can retrieve JSON status info"
2591 self.worker.hold_jobs_in_build = True
2592 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2593 A.addApproval('CRVW', 2)
2594 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2595 self.waitUntilSettled()
2596
2597 port = self.webapp.server.socket.getsockname()[1]
2598
2599 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2600 data = f.read()
2601
2602 self.worker.hold_jobs_in_build = False
2603 self.worker.release()
2604 self.waitUntilSettled()
2605
2606 data = json.loads(data)
2607 status_jobs = set()
2608 for p in data['pipelines']:
2609 for q in p['change_queues']:
2610 for head in q['heads']:
2611 for change in head:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002612 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002613 for job in change['jobs']:
2614 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002615 self.assertIn('project-merge', status_jobs)
2616 self.assertIn('project-test1', status_jobs)
2617 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002618
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002619 def test_node_label(self):
2620 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002621 self.worker.registerFunction('build:node-project-test1:debian')
2622
2623 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2624 A.addApproval('CRVW', 2)
2625 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2626 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002627
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002628 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2629 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2630 'debian')
2631 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002632
2633 def test_live_reconfiguration(self):
2634 "Test that live reconfiguration works"
2635 self.worker.hold_jobs_in_build = True
2636 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2637 A.addApproval('CRVW', 2)
2638 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2639 self.waitUntilSettled()
2640
2641 self.sched.reconfigure(self.config)
2642
2643 self.worker.hold_jobs_in_build = False
2644 self.worker.release()
2645 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002646 self.assertEqual(self.getJobFromHistory('project-merge').result,
2647 'SUCCESS')
2648 self.assertEqual(self.getJobFromHistory('project-test1').result,
2649 'SUCCESS')
2650 self.assertEqual(self.getJobFromHistory('project-test2').result,
2651 'SUCCESS')
2652 self.assertEqual(A.data['status'], 'MERGED')
2653 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002654
James E. Blaire712d9f2013-07-31 11:40:11 -07002655 def test_live_reconfiguration_functions(self):
2656 "Test live reconfiguration with a custom function"
2657 self.worker.registerFunction('build:node-project-test1:debian')
2658 self.worker.registerFunction('build:node-project-test1:wheezy')
2659 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2660 A.addApproval('CRVW', 2)
2661 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2662 self.waitUntilSettled()
2663
2664 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2665 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2666 'debian')
2667 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2668
2669 self.config.set('zuul', 'layout_config',
2670 'tests/fixtures/layout-live-'
2671 'reconfiguration-functions.yaml')
2672 self.sched.reconfigure(self.config)
2673 self.worker.build_history = []
2674
2675 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
2676 B.addApproval('CRVW', 2)
2677 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2678 self.waitUntilSettled()
2679
2680 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2681 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2682 'wheezy')
2683 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2684
James E. Blair287c06d2013-07-24 10:39:30 -07002685 def test_delayed_repo_init(self):
2686 self.config.set('zuul', 'layout_config',
2687 'tests/fixtures/layout-delayed-repo-init.yaml')
2688 self.sched.reconfigure(self.config)
2689
2690 self.init_repo("org/new-project")
2691 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2692
2693 A.addApproval('CRVW', 2)
2694 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2695 self.waitUntilSettled()
2696 self.assertEqual(self.getJobFromHistory('project-merge').result,
2697 'SUCCESS')
2698 self.assertEqual(self.getJobFromHistory('project-test1').result,
2699 'SUCCESS')
2700 self.assertEqual(self.getJobFromHistory('project-test2').result,
2701 'SUCCESS')
2702 self.assertEqual(A.data['status'], 'MERGED')
2703 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002704
2705 def test_timer(self):
2706 "Test that a periodic job is triggered"
2707 self.worker.hold_jobs_in_build = True
2708 self.config.set('zuul', 'layout_config',
2709 'tests/fixtures/layout-timer.yaml')
2710 self.sched.reconfigure(self.config)
2711 self.registerJobs()
2712
2713 start = time.time()
2714 failed = True
2715 while ((time.time() - start) < 30):
2716 if len(self.builds) == 2:
2717 failed = False
2718 break
2719 else:
2720 time.sleep(1)
2721
2722 if failed:
2723 raise Exception("Expected jobs never ran")
2724
2725 self.waitUntilSettled()
2726 port = self.webapp.server.socket.getsockname()[1]
2727
2728 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2729 data = f.read()
2730
2731 self.worker.hold_jobs_in_build = False
2732 self.worker.release()
2733 self.waitUntilSettled()
2734
2735 self.assertEqual(self.getJobFromHistory(
2736 'project-bitrot-stable-old').result, 'SUCCESS')
2737 self.assertEqual(self.getJobFromHistory(
2738 'project-bitrot-stable-older').result, 'SUCCESS')
2739
2740 data = json.loads(data)
2741 status_jobs = set()
2742 for p in data['pipelines']:
2743 for q in p['change_queues']:
2744 for head in q['heads']:
2745 for change in head:
2746 self.assertEqual(change['id'], 'None')
2747 for job in change['jobs']:
2748 status_jobs.add(job['name'])
2749 self.assertIn('project-bitrot-stable-old', status_jobs)
2750 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002751
2752 def test_check_smtp_pool(self):
2753 self.config.set('zuul', 'layout_config',
2754 'tests/fixtures/layout-smtp.yaml')
2755 self.sched.reconfigure(self.config)
2756
2757 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2758 self.waitUntilSettled()
2759
2760 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2761 self.waitUntilSettled()
2762
2763 self.assertEqual(len(FakeSMTP.messages), 2)
2764
2765 # A.messages only holds what FakeGerrit places in it. Thus we
2766 # work on the knowledge of what the first message should be as
2767 # it is only configured to go to SMTP.
2768
2769 self.assertEqual('zuul@example.com',
2770 FakeSMTP.messages[0]['from_email'])
2771 self.assertEqual(['you@example.com'],
2772 FakeSMTP.messages[0]['to_email'])
2773 self.assertEqual('Starting check jobs.',
2774 FakeSMTP.messages[0]['body'])
2775
2776 self.assertEqual('zuul_from@example.com',
2777 FakeSMTP.messages[1]['from_email'])
2778 self.assertEqual(['alternative_me@example.com'],
2779 FakeSMTP.messages[1]['to_email'])
2780 self.assertEqual(A.messages[0],
2781 FakeSMTP.messages[1]['body'])