blob: 70956b4e46e6de841ec7a4469aefce6c137a362d [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
James E. Blairfef71632013-09-23 11:15:47 -07001504 def test_two_failed_changes_at_head(self):
1505 "Test that changes are reparented correctly if 2 fail at head"
1506
1507 self.worker.hold_jobs_in_build = True
1508 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1509 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1510 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1511 A.addApproval('CRVW', 2)
1512 B.addApproval('CRVW', 2)
1513 C.addApproval('CRVW', 2)
1514
1515 self.worker.addFailTest('project-test1', A)
1516 self.worker.addFailTest('project-test1', B)
1517
1518 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1519 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1520 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1521 self.waitUntilSettled()
1522
1523 self.worker.release('.*-merge')
1524 self.waitUntilSettled()
1525 self.worker.release('.*-merge')
1526 self.waitUntilSettled()
1527 self.worker.release('.*-merge')
1528 self.waitUntilSettled()
1529
1530 self.assertEqual(len(self.builds), 6)
1531 self.assertEqual(self.builds[0].name, 'project-test1')
1532 self.assertEqual(self.builds[1].name, 'project-test2')
1533 self.assertEqual(self.builds[2].name, 'project-test1')
1534 self.assertEqual(self.builds[3].name, 'project-test2')
1535 self.assertEqual(self.builds[4].name, 'project-test1')
1536 self.assertEqual(self.builds[5].name, 'project-test2')
1537
1538 self.assertTrue(self.job_has_changes(self.builds[0], A))
1539 self.assertTrue(self.job_has_changes(self.builds[2], A))
1540 self.assertTrue(self.job_has_changes(self.builds[2], B))
1541 self.assertTrue(self.job_has_changes(self.builds[4], A))
1542 self.assertTrue(self.job_has_changes(self.builds[4], B))
1543 self.assertTrue(self.job_has_changes(self.builds[4], C))
1544
1545 # Fail change B first
1546 self.release(self.builds[2])
1547 self.waitUntilSettled()
1548
1549 # restart of C after B failure
1550 self.worker.release('.*-merge')
1551 self.waitUntilSettled()
1552
1553 self.assertEqual(len(self.builds), 5)
1554 self.assertEqual(self.builds[0].name, 'project-test1')
1555 self.assertEqual(self.builds[1].name, 'project-test2')
1556 self.assertEqual(self.builds[2].name, 'project-test2')
1557 self.assertEqual(self.builds[3].name, 'project-test1')
1558 self.assertEqual(self.builds[4].name, 'project-test2')
1559
1560 self.assertTrue(self.job_has_changes(self.builds[1], A))
1561 self.assertTrue(self.job_has_changes(self.builds[2], A))
1562 self.assertTrue(self.job_has_changes(self.builds[2], B))
1563 self.assertTrue(self.job_has_changes(self.builds[4], A))
1564 self.assertFalse(self.job_has_changes(self.builds[4], B))
1565 self.assertTrue(self.job_has_changes(self.builds[4], C))
1566
1567 # Finish running all passing jobs for change A
1568 self.release(self.builds[1])
1569 self.waitUntilSettled()
1570 # Fail and report change A
1571 self.release(self.builds[0])
1572 self.waitUntilSettled()
1573
1574 # restart of B,C after A failure
1575 self.worker.release('.*-merge')
1576 self.waitUntilSettled()
1577 self.worker.release('.*-merge')
1578 self.waitUntilSettled()
1579
1580 self.assertEqual(len(self.builds), 4)
1581 self.assertEqual(self.builds[0].name, 'project-test1') # B
1582 self.assertEqual(self.builds[1].name, 'project-test2') # B
1583 self.assertEqual(self.builds[2].name, 'project-test1') # C
1584 self.assertEqual(self.builds[3].name, 'project-test2') # C
1585
1586 self.assertFalse(self.job_has_changes(self.builds[1], A))
1587 self.assertTrue(self.job_has_changes(self.builds[1], B))
1588 self.assertFalse(self.job_has_changes(self.builds[1], C))
1589
1590 self.assertFalse(self.job_has_changes(self.builds[2], A))
1591 # After A failed and B and C restarted, B should be back in
1592 # C's tests because it has not failed yet.
1593 self.assertTrue(self.job_has_changes(self.builds[2], B))
1594 self.assertTrue(self.job_has_changes(self.builds[2], C))
1595
1596 self.worker.hold_jobs_in_build = False
1597 self.worker.release()
1598 self.waitUntilSettled()
1599
1600 self.assertEqual(len(self.builds), 0)
1601 self.assertEqual(len(self.history), 21)
1602 self.assertEqual(A.data['status'], 'NEW')
1603 self.assertEqual(B.data['status'], 'NEW')
1604 self.assertEqual(C.data['status'], 'MERGED')
1605 self.assertEqual(A.reported, 2)
1606 self.assertEqual(B.reported, 2)
1607 self.assertEqual(C.reported, 2)
1608
James E. Blair8c803f82012-07-31 16:25:42 -07001609 def test_patch_order(self):
1610 "Test that dependent patches are tested in the right order"
1611 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1612 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1613 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1614 A.addApproval('CRVW', 2)
1615 B.addApproval('CRVW', 2)
1616 C.addApproval('CRVW', 2)
1617
1618 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1619 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1620 M2.setMerged()
1621 M1.setMerged()
1622
1623 # C -> B -> A -> M1 -> M2
1624 # M2 is here to make sure it is never queried. If it is, it
1625 # means zuul is walking down the entire history of merged
1626 # changes.
1627
1628 C.setDependsOn(B, 1)
1629 B.setDependsOn(A, 1)
1630 A.setDependsOn(M1, 1)
1631 M1.setDependsOn(M2, 1)
1632
1633 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1634
1635 self.waitUntilSettled()
1636
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001637 self.assertEqual(A.data['status'], 'NEW')
1638 self.assertEqual(B.data['status'], 'NEW')
1639 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001640
1641 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1642 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1643
1644 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001645 self.assertEqual(M2.queried, 0)
1646 self.assertEqual(A.data['status'], 'MERGED')
1647 self.assertEqual(B.data['status'], 'MERGED')
1648 self.assertEqual(C.data['status'], 'MERGED')
1649 self.assertEqual(A.reported, 2)
1650 self.assertEqual(B.reported, 2)
1651 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001652
James E. Blair0e933c52013-07-11 10:18:52 -07001653 def test_trigger_cache(self):
1654 "Test that the trigger cache operates correctly"
1655 self.worker.hold_jobs_in_build = True
1656
1657 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1658 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1659 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1660 A.addApproval('CRVW', 2)
1661 B.addApproval('CRVW', 2)
1662
1663 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1664 M1.setMerged()
1665
1666 B.setDependsOn(A, 1)
1667 A.setDependsOn(M1, 1)
1668
1669 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1670 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1671
1672 self.waitUntilSettled()
1673
1674 for build in self.builds:
1675 if build.parameters['ZUUL_PIPELINE'] == 'check':
1676 build.release()
1677 self.waitUntilSettled()
1678 for build in self.builds:
1679 if build.parameters['ZUUL_PIPELINE'] == 'check':
1680 build.release()
1681 self.waitUntilSettled()
1682
1683 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1684 self.waitUntilSettled()
1685
James E. Blair6c358e72013-07-29 17:06:47 -07001686 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001687 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001688 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001689
1690 self.worker.hold_jobs_in_build = False
1691 self.worker.release()
1692 self.waitUntilSettled()
1693
1694 self.assertEqual(A.data['status'], 'MERGED')
1695 self.assertEqual(B.data['status'], 'MERGED')
1696 self.assertEqual(A.queried, 2) # Initial and isMerged
1697 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1698
James E. Blair8c803f82012-07-31 16:25:42 -07001699 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001700 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001701 # TODO: move to test_gerrit (this is a unit test!)
1702 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001703 trigger = self.sched.layout.pipelines['gate'].trigger
1704 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001705 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001706 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001707
1708 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001709 a = trigger.getChange(1, 2, refresh=True)
1710 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001711
1712 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001713 a = trigger.getChange(1, 2, refresh=True)
1714 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1715 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001716
1717 def test_build_configuration(self):
1718 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001719
1720 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001721 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1722 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1723 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1724 A.addApproval('CRVW', 2)
1725 B.addApproval('CRVW', 2)
1726 C.addApproval('CRVW', 2)
1727 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1728 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1729 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1730 self.waitUntilSettled()
1731
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001732 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001733 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001734 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001735 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001736 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001737 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001738 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001739 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1740 self.gearman_server.hold_jobs_in_queue = False
1741 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001742 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001743
Monty Taylorbc758832013-06-17 17:22:42 -04001744 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001745 repo = git.Repo(path)
1746 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1747 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001748 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001749 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001750
1751 def test_build_configuration_conflict(self):
1752 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001753
1754 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001755 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1756 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001757 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001758 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1759 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001760 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001761 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1762 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001763 A.addApproval('CRVW', 2)
1764 B.addApproval('CRVW', 2)
1765 C.addApproval('CRVW', 2)
1766 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1767 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1768 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1769 self.waitUntilSettled()
1770
James E. Blair6736beb2013-07-11 15:18:15 -07001771 self.assertEqual(A.reported, 1)
1772 self.assertEqual(B.reported, 1)
1773 self.assertEqual(C.reported, 1)
1774
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001775 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001776 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001777 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001778 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001779 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001780 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001781
1782 self.assertEqual(len(self.history), 2) # A and C merge jobs
1783
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001784 self.gearman_server.hold_jobs_in_queue = False
1785 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001786 self.waitUntilSettled()
1787
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001788 self.assertEqual(A.data['status'], 'MERGED')
1789 self.assertEqual(B.data['status'], 'NEW')
1790 self.assertEqual(C.data['status'], 'MERGED')
1791 self.assertEqual(A.reported, 2)
1792 self.assertEqual(B.reported, 2)
1793 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001794 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001795
James E. Blairdaabed22012-08-15 15:38:57 -07001796 def test_post(self):
1797 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001798
Zhongyue Luo5d556072012-09-21 02:00:47 +09001799 e = {
1800 "type": "ref-updated",
1801 "submitter": {
1802 "name": "User Name",
1803 },
1804 "refUpdate": {
1805 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1806 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1807 "refName": "master",
1808 "project": "org/project",
1809 }
1810 }
James E. Blairdaabed22012-08-15 15:38:57 -07001811 self.fake_gerrit.addEvent(e)
1812 self.waitUntilSettled()
1813
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001814 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001815 self.assertEqual(len(self.history), 1)
1816 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001817
1818 def test_build_configuration_branch(self):
1819 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001820
1821 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001822 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1823 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1824 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1825 A.addApproval('CRVW', 2)
1826 B.addApproval('CRVW', 2)
1827 C.addApproval('CRVW', 2)
1828 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1829 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1830 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1831 self.waitUntilSettled()
1832
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001833 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001834 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001835 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001836 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001837 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001838 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001839 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001840 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1841 self.gearman_server.hold_jobs_in_queue = False
1842 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001843 self.waitUntilSettled()
1844
Monty Taylorbc758832013-06-17 17:22:42 -04001845 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001846 repo = git.Repo(path)
1847 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1848 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001849 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001850 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001851
1852 def test_build_configuration_branch_interaction(self):
1853 "Test that switching between branches works"
1854 self.test_build_configuration()
1855 self.test_build_configuration_branch()
1856 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001857 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001858 repo = git.Repo(path)
1859 repo.heads.master.commit = repo.commit('init')
1860 self.test_build_configuration()
1861
1862 def test_build_configuration_multi_branch(self):
1863 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001864
1865 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001866 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1867 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1868 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1869 A.addApproval('CRVW', 2)
1870 B.addApproval('CRVW', 2)
1871 C.addApproval('CRVW', 2)
1872 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1873 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1874 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1875 self.waitUntilSettled()
1876
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001877 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001878 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001879 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001880 job_B = None
1881 for job in queue:
1882 if 'project-merge' in job.name:
1883 job_B = job
1884 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001885 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001886 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001887 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001888 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001889 for job in queue:
1890 if 'project-merge' in job.name:
1891 job_C = job
1892 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001893 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001894 self.gearman_server.hold_jobs_in_queue = False
1895 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001896 self.waitUntilSettled()
1897
Monty Taylorbc758832013-06-17 17:22:42 -04001898 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001899 repo = git.Repo(path)
1900
1901 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001902 for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001903 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001904 correct_messages = ['initial commit', 'A-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001905 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001906
1907 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001908 for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001909 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001910 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001911 self.assertEqual(repo_messages, correct_messages)
James E. Blair7f71c802012-08-22 13:04:32 -07001912
1913 def test_one_job_project(self):
1914 "Test that queueing works with one job"
1915 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1916 'master', 'A')
1917 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1918 'master', 'B')
1919 A.addApproval('CRVW', 2)
1920 B.addApproval('CRVW', 2)
1921 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1922 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1923 self.waitUntilSettled()
1924
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001925 self.assertEqual(A.data['status'], 'MERGED')
1926 self.assertEqual(A.reported, 2)
1927 self.assertEqual(B.data['status'], 'MERGED')
1928 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001929
Antoine Musso80edd5a2013-02-13 15:37:53 +01001930 def test_job_from_templates_launched(self):
1931 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001932
Antoine Musso80edd5a2013-02-13 15:37:53 +01001933 A = self.fake_gerrit.addFakeChange(
1934 'org/templated-project', 'master', 'A')
1935 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1936 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001937
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001938 self.assertEqual(self.getJobFromHistory('project-test1').result,
1939 'SUCCESS')
1940 self.assertEqual(self.getJobFromHistory('project-test2').result,
1941 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001942
James E. Blaircaec0c52012-08-22 14:52:22 -07001943 def test_dependent_changes_dequeue(self):
1944 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001945
James E. Blaircaec0c52012-08-22 14:52:22 -07001946 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1947 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1948 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1949 A.addApproval('CRVW', 2)
1950 B.addApproval('CRVW', 2)
1951 C.addApproval('CRVW', 2)
1952
1953 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1954 M1.setMerged()
1955
1956 # C -> B -> A -> M1
1957
1958 C.setDependsOn(B, 1)
1959 B.setDependsOn(A, 1)
1960 A.setDependsOn(M1, 1)
1961
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001962 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001963
1964 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1965 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1966 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1967
1968 self.waitUntilSettled()
1969
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001970 self.assertEqual(A.data['status'], 'NEW')
1971 self.assertEqual(A.reported, 2)
1972 self.assertEqual(B.data['status'], 'NEW')
1973 self.assertEqual(B.reported, 2)
1974 self.assertEqual(C.data['status'], 'NEW')
1975 self.assertEqual(C.reported, 2)
1976 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001977
James E. Blair972e3c72013-08-29 12:04:55 -07001978 def test_failing_dependent_changes(self):
1979 "Test that failing dependent patches are taken out of stream"
1980 self.worker.hold_jobs_in_build = True
1981 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1982 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1983 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1984 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1985 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
1986 A.addApproval('CRVW', 2)
1987 B.addApproval('CRVW', 2)
1988 C.addApproval('CRVW', 2)
1989 D.addApproval('CRVW', 2)
1990 E.addApproval('CRVW', 2)
1991
1992 # E, D -> C -> B, A
1993
1994 D.setDependsOn(C, 1)
1995 C.setDependsOn(B, 1)
1996
1997 self.worker.addFailTest('project-test1', B)
1998
1999 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2000 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2001 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2002 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2003 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2004
2005 self.waitUntilSettled()
2006 self.worker.release('.*-merge')
2007 self.waitUntilSettled()
2008 self.worker.release('.*-merge')
2009 self.waitUntilSettled()
2010 self.worker.release('.*-merge')
2011 self.waitUntilSettled()
2012 self.worker.release('.*-merge')
2013 self.waitUntilSettled()
2014 self.worker.release('.*-merge')
2015 self.waitUntilSettled()
2016
2017 self.worker.hold_jobs_in_build = False
2018 for build in self.builds:
2019 if build.parameters['ZUUL_CHANGE'] != '1':
2020 build.release()
2021 self.waitUntilSettled()
2022
2023 self.worker.release()
2024 self.waitUntilSettled()
2025
2026 self.assertEqual(A.data['status'], 'MERGED')
2027 self.assertEqual(A.reported, 2)
2028 self.assertEqual(B.data['status'], 'NEW')
2029 self.assertEqual(B.reported, 2)
2030 self.assertEqual(C.data['status'], 'NEW')
2031 self.assertEqual(C.reported, 2)
2032 self.assertEqual(D.data['status'], 'NEW')
2033 self.assertEqual(D.reported, 2)
2034 self.assertEqual(E.data['status'], 'MERGED')
2035 self.assertEqual(E.reported, 2)
2036 self.assertEqual(len(self.history), 18)
2037
James E. Blairec590122012-08-22 15:19:31 -07002038 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08002039 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07002040 # If it's dequeued more than once, we should see extra
2041 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07002042
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002043 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07002044 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2045 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2046 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2047 A.addApproval('CRVW', 2)
2048 B.addApproval('CRVW', 2)
2049 C.addApproval('CRVW', 2)
2050
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002051 self.worker.addFailTest('project1-test1', A)
2052 self.worker.addFailTest('project1-test2', A)
2053 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07002054
2055 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2056 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2057 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2058
2059 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07002060
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002061 self.assertEqual(len(self.builds), 1)
2062 self.assertEqual(self.builds[0].name, 'project1-merge')
2063 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07002064
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002065 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002066 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002067 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002068 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002069 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002070 self.waitUntilSettled()
2071
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002072 self.assertEqual(len(self.builds), 9)
2073 self.assertEqual(self.builds[0].name, 'project1-test1')
2074 self.assertEqual(self.builds[1].name, 'project1-test2')
2075 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
2076 self.assertEqual(self.builds[3].name, 'project1-test1')
2077 self.assertEqual(self.builds[4].name, 'project1-test2')
2078 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
2079 self.assertEqual(self.builds[6].name, 'project1-test1')
2080 self.assertEqual(self.builds[7].name, 'project1-test2')
2081 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07002082
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002083 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07002084 self.waitUntilSettled()
2085
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002086 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
2087 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07002088
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002089 self.worker.hold_jobs_in_build = False
2090 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07002091 self.waitUntilSettled()
2092
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002093 self.assertEqual(len(self.builds), 0)
2094 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07002095
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002096 self.assertEqual(A.data['status'], 'NEW')
2097 self.assertEqual(B.data['status'], 'MERGED')
2098 self.assertEqual(C.data['status'], 'MERGED')
2099 self.assertEqual(A.reported, 2)
2100 self.assertEqual(B.reported, 2)
2101 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07002102
2103 def test_nonvoting_job(self):
2104 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002105
James E. Blair4ec821f2012-08-23 15:28:28 -07002106 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
2107 'master', 'A')
2108 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002109 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07002110 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2111
2112 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07002113
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002114 self.assertEqual(A.data['status'], 'MERGED')
2115 self.assertEqual(A.reported, 2)
2116 self.assertEqual(
2117 self.getJobFromHistory('nonvoting-project-merge').result,
2118 'SUCCESS')
2119 self.assertEqual(
2120 self.getJobFromHistory('nonvoting-project-test1').result,
2121 'SUCCESS')
2122 self.assertEqual(
2123 self.getJobFromHistory('nonvoting-project-test2').result,
2124 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07002125
2126 def test_check_queue_success(self):
2127 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002128
James E. Blaire0487072012-08-29 17:38:31 -07002129 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2130 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2131
2132 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002133
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002134 self.assertEqual(A.data['status'], 'NEW')
2135 self.assertEqual(A.reported, 1)
2136 self.assertEqual(self.getJobFromHistory('project-merge').result,
2137 'SUCCESS')
2138 self.assertEqual(self.getJobFromHistory('project-test1').result,
2139 'SUCCESS')
2140 self.assertEqual(self.getJobFromHistory('project-test2').result,
2141 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07002142
2143 def test_check_queue_failure(self):
2144 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002145
James E. Blaire0487072012-08-29 17:38:31 -07002146 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002147 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07002148 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2149
2150 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002151
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002152 self.assertEqual(A.data['status'], 'NEW')
2153 self.assertEqual(A.reported, 1)
2154 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07002155 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002156 self.assertEqual(self.getJobFromHistory('project-test1').result,
2157 'SUCCESS')
2158 self.assertEqual(self.getJobFromHistory('project-test2').result,
2159 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07002160
2161 def test_dependent_behind_dequeue(self):
2162 "test that dependent changes behind dequeued changes work"
2163 # This complicated test is a reproduction of a real life bug
2164 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07002165
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002166 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07002167 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2168 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2169 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2170 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2171 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
2172 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
2173 D.setDependsOn(C, 1)
2174 E.setDependsOn(D, 1)
2175 A.addApproval('CRVW', 2)
2176 B.addApproval('CRVW', 2)
2177 C.addApproval('CRVW', 2)
2178 D.addApproval('CRVW', 2)
2179 E.addApproval('CRVW', 2)
2180 F.addApproval('CRVW', 2)
2181
2182 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07002183
2184 # Change object re-use in the gerrit trigger is hidden if
2185 # changes are added in quick succession; waiting makes it more
2186 # like real life.
2187 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2188 self.waitUntilSettled()
2189 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2190 self.waitUntilSettled()
2191
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002192 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002193 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002194 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002195 self.waitUntilSettled()
2196
2197 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2198 self.waitUntilSettled()
2199 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2200 self.waitUntilSettled()
2201 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2202 self.waitUntilSettled()
2203 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
2204 self.waitUntilSettled()
2205
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002206 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002207 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002208 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002209 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002210 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002211 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002212 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002213 self.waitUntilSettled()
2214
2215 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002216
2217 # Grab pointers to the jobs we want to release before
2218 # releasing any, because list indexes may change as
2219 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002220 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002221 a.release()
2222 b.release()
2223 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002224 self.waitUntilSettled()
2225
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002226 self.worker.hold_jobs_in_build = False
2227 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002228 self.waitUntilSettled()
2229
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002230 self.assertEqual(A.data['status'], 'NEW')
2231 self.assertEqual(B.data['status'], 'MERGED')
2232 self.assertEqual(C.data['status'], 'MERGED')
2233 self.assertEqual(D.data['status'], 'MERGED')
2234 self.assertEqual(E.data['status'], 'MERGED')
2235 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002236
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002237 self.assertEqual(A.reported, 2)
2238 self.assertEqual(B.reported, 2)
2239 self.assertEqual(C.reported, 2)
2240 self.assertEqual(D.reported, 2)
2241 self.assertEqual(E.reported, 2)
2242 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002243
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002244 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2245 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002246
2247 def test_merger_repack(self):
2248 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002249
James E. Blair05fed602012-09-07 12:45:24 -07002250 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2251 A.addApproval('CRVW', 2)
2252 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2253 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002254 self.assertEqual(self.getJobFromHistory('project-merge').result,
2255 'SUCCESS')
2256 self.assertEqual(self.getJobFromHistory('project-test1').result,
2257 'SUCCESS')
2258 self.assertEqual(self.getJobFromHistory('project-test2').result,
2259 'SUCCESS')
2260 self.assertEqual(A.data['status'], 'MERGED')
2261 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07002262 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002263 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002264
Monty Taylorbc758832013-06-17 17:22:42 -04002265 path = os.path.join(self.git_root, "org/project")
2266 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002267
2268 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2269 A.addApproval('CRVW', 2)
2270 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2271 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002272 self.assertEqual(self.getJobFromHistory('project-merge').result,
2273 'SUCCESS')
2274 self.assertEqual(self.getJobFromHistory('project-test1').result,
2275 'SUCCESS')
2276 self.assertEqual(self.getJobFromHistory('project-test2').result,
2277 'SUCCESS')
2278 self.assertEqual(A.data['status'], 'MERGED')
2279 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002280
James E. Blair4886f282012-11-15 09:27:33 -08002281 def test_merger_repack_large_change(self):
2282 "Test that the merger works with large changes after a repack"
2283 # https://bugs.launchpad.net/zuul/+bug/1078946
2284 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2285 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002286 path = os.path.join(self.upstream_root, "org/project1")
2287 print repack_repo(path)
2288 path = os.path.join(self.git_root, "org/project1")
2289 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002290
2291 A.addApproval('CRVW', 2)
2292 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2293 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002294 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2295 'SUCCESS')
2296 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2297 'SUCCESS')
2298 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2299 'SUCCESS')
2300 self.assertEqual(A.data['status'], 'MERGED')
2301 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002302
James E. Blair7ee88a22012-09-12 18:59:31 +02002303 def test_nonexistent_job(self):
2304 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002305 # Set to the state immediately after a restart
2306 self.resetGearmanServer()
2307 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002308
2309 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2310 A.addApproval('CRVW', 2)
2311 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2312 # There may be a thread about to report a lost change
2313 while A.reported < 2:
2314 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002315 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002316 self.assertFalse(job_names)
2317 self.assertEqual(A.data['status'], 'NEW')
2318 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002319 self.assertEmptyQueues()
2320
2321 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002322 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002323 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2324 A.addApproval('CRVW', 2)
2325 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2326 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002327 self.assertEqual(self.getJobFromHistory('project-merge').result,
2328 'SUCCESS')
2329 self.assertEqual(self.getJobFromHistory('project-test1').result,
2330 'SUCCESS')
2331 self.assertEqual(self.getJobFromHistory('project-test2').result,
2332 'SUCCESS')
2333 self.assertEqual(A.data['status'], 'MERGED')
2334 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002335
2336 def test_single_nonexistent_post_job(self):
2337 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002338 e = {
2339 "type": "ref-updated",
2340 "submitter": {
2341 "name": "User Name",
2342 },
2343 "refUpdate": {
2344 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2345 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2346 "refName": "master",
2347 "project": "org/project",
2348 }
2349 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002350 # Set to the state immediately after a restart
2351 self.resetGearmanServer()
2352 self.launcher.negative_function_cache_ttl = 0
2353
James E. Blairf62d4282012-12-31 17:01:50 -08002354 self.fake_gerrit.addEvent(e)
2355 self.waitUntilSettled()
2356
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002357 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002358
2359 def test_new_patchset_dequeues_old(self):
2360 "Test that a new patchset causes the old to be dequeued"
2361 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002362 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002363 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2364 M.setMerged()
2365
2366 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2367 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2368 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2369 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2370 A.addApproval('CRVW', 2)
2371 B.addApproval('CRVW', 2)
2372 C.addApproval('CRVW', 2)
2373 D.addApproval('CRVW', 2)
2374
2375 C.setDependsOn(B, 1)
2376 B.setDependsOn(A, 1)
2377 A.setDependsOn(M, 1)
2378
2379 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2380 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2381 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2382 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2383 self.waitUntilSettled()
2384
2385 B.addPatchset()
2386 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2387 self.waitUntilSettled()
2388
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002389 self.worker.hold_jobs_in_build = False
2390 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002391 self.waitUntilSettled()
2392
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002393 self.assertEqual(A.data['status'], 'MERGED')
2394 self.assertEqual(A.reported, 2)
2395 self.assertEqual(B.data['status'], 'NEW')
2396 self.assertEqual(B.reported, 2)
2397 self.assertEqual(C.data['status'], 'NEW')
2398 self.assertEqual(C.reported, 2)
2399 self.assertEqual(D.data['status'], 'MERGED')
2400 self.assertEqual(D.reported, 2)
2401 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002402
2403 def test_new_patchset_dequeues_old_on_head(self):
2404 "Test that a new patchset causes the old to be dequeued (at head)"
2405 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002406 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002407 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2408 M.setMerged()
2409 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2410 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2411 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2412 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2413 A.addApproval('CRVW', 2)
2414 B.addApproval('CRVW', 2)
2415 C.addApproval('CRVW', 2)
2416 D.addApproval('CRVW', 2)
2417
2418 C.setDependsOn(B, 1)
2419 B.setDependsOn(A, 1)
2420 A.setDependsOn(M, 1)
2421
2422 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2423 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2424 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2425 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2426 self.waitUntilSettled()
2427
2428 A.addPatchset()
2429 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2430 self.waitUntilSettled()
2431
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002432 self.worker.hold_jobs_in_build = False
2433 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002434 self.waitUntilSettled()
2435
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002436 self.assertEqual(A.data['status'], 'NEW')
2437 self.assertEqual(A.reported, 2)
2438 self.assertEqual(B.data['status'], 'NEW')
2439 self.assertEqual(B.reported, 2)
2440 self.assertEqual(C.data['status'], 'NEW')
2441 self.assertEqual(C.reported, 2)
2442 self.assertEqual(D.data['status'], 'MERGED')
2443 self.assertEqual(D.reported, 2)
2444 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002445
2446 def test_new_patchset_dequeues_old_without_dependents(self):
2447 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002448 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002449 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2450 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2451 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2452 A.addApproval('CRVW', 2)
2453 B.addApproval('CRVW', 2)
2454 C.addApproval('CRVW', 2)
2455
2456 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2457 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2458 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2459 self.waitUntilSettled()
2460
2461 B.addPatchset()
2462 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2463 self.waitUntilSettled()
2464
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002465 self.worker.hold_jobs_in_build = False
2466 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002467 self.waitUntilSettled()
2468
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002469 self.assertEqual(A.data['status'], 'MERGED')
2470 self.assertEqual(A.reported, 2)
2471 self.assertEqual(B.data['status'], 'NEW')
2472 self.assertEqual(B.reported, 2)
2473 self.assertEqual(C.data['status'], 'MERGED')
2474 self.assertEqual(C.reported, 2)
2475 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002476
2477 def test_new_patchset_dequeues_old_independent_queue(self):
2478 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002479 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002480 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2481 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2482 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2483 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2484 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2485 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2486 self.waitUntilSettled()
2487
2488 B.addPatchset()
2489 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2490 self.waitUntilSettled()
2491
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002492 self.worker.hold_jobs_in_build = False
2493 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002494 self.waitUntilSettled()
2495
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002496 self.assertEqual(A.data['status'], 'NEW')
2497 self.assertEqual(A.reported, 1)
2498 self.assertEqual(B.data['status'], 'NEW')
2499 self.assertEqual(B.reported, 1)
2500 self.assertEqual(C.data['status'], 'NEW')
2501 self.assertEqual(C.reported, 1)
2502 self.assertEqual(len(self.history), 10)
2503 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002504
2505 def test_zuul_refs(self):
2506 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002507 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002508 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2509 M1.setMerged()
2510 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2511 M2.setMerged()
2512
2513 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2514 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2515 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2516 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2517 A.addApproval('CRVW', 2)
2518 B.addApproval('CRVW', 2)
2519 C.addApproval('CRVW', 2)
2520 D.addApproval('CRVW', 2)
2521 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2522 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2523 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2524 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2525
2526 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002527 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002528 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002529 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002530 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002531 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002532 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002533 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002534 self.waitUntilSettled()
2535
James E. Blair7d0dedc2013-02-21 17:26:09 -08002536 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002537 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002538 if x.parameters['ZUUL_CHANGE'] == '3':
2539 a_zref = x.parameters['ZUUL_REF']
2540 if x.parameters['ZUUL_CHANGE'] == '4':
2541 b_zref = x.parameters['ZUUL_REF']
2542 if x.parameters['ZUUL_CHANGE'] == '5':
2543 c_zref = x.parameters['ZUUL_REF']
2544 if x.parameters['ZUUL_CHANGE'] == '6':
2545 d_zref = x.parameters['ZUUL_REF']
2546
2547 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002548 self.assertIsNotNone(a_zref)
2549 self.assertIsNotNone(b_zref)
2550 self.assertIsNotNone(c_zref)
2551 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002552
2553 # And they should all be different
2554 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002555 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002556
2557 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002558 self.assertTrue(self.ref_has_change(a_zref, A))
2559 self.assertFalse(self.ref_has_change(a_zref, B))
2560 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002561
2562 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002563 self.assertTrue(self.ref_has_change(b_zref, A))
2564 self.assertTrue(self.ref_has_change(b_zref, B))
2565 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002566
2567 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002568 self.assertTrue(self.ref_has_change(c_zref, A))
2569 self.assertTrue(self.ref_has_change(c_zref, B))
2570 self.assertTrue(self.ref_has_change(c_zref, C))
2571 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002572
2573 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002574 self.assertTrue(self.ref_has_change(d_zref, A))
2575 self.assertTrue(self.ref_has_change(d_zref, B))
2576 self.assertTrue(self.ref_has_change(d_zref, C))
2577 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002578
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002579 self.worker.hold_jobs_in_build = False
2580 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002581 self.waitUntilSettled()
2582
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002583 self.assertEqual(A.data['status'], 'MERGED')
2584 self.assertEqual(A.reported, 2)
2585 self.assertEqual(B.data['status'], 'MERGED')
2586 self.assertEqual(B.reported, 2)
2587 self.assertEqual(C.data['status'], 'MERGED')
2588 self.assertEqual(C.reported, 2)
2589 self.assertEqual(D.data['status'], 'MERGED')
2590 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002591
James E. Blair4a28a882013-08-23 15:17:33 -07002592 def test_rerun_on_error(self):
2593 "Test that if a worker fails to run a job, it is run again"
2594 self.worker.hold_jobs_in_build = True
2595 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2596 A.addApproval('CRVW', 2)
2597 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2598 self.waitUntilSettled()
2599
2600 self.builds[0].run_error = True
2601 self.worker.hold_jobs_in_build = False
2602 self.worker.release()
2603 self.waitUntilSettled()
2604 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2605 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2606
James E. Blair412e5582013-04-22 15:50:12 -07002607 def test_statsd(self):
2608 "Test each of the statsd methods used in the scheduler"
2609 import extras
2610 statsd = extras.try_import('statsd.statsd')
2611 statsd.incr('test-incr')
2612 statsd.timing('test-timing', 3)
2613 statsd.gauge('test-guage', 12)
2614 self.assertReportedStat('test-incr', '1|c')
2615 self.assertReportedStat('test-timing', '3|ms')
2616 self.assertReportedStat('test-guage', '12|g')
2617
James E. Blair70c71582013-03-06 08:50:50 -08002618 def test_file_jobs(self):
2619 "Test that file jobs run only when appropriate"
2620 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2621 A.addPatchset(['pip-requires'])
2622 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2623 A.addApproval('CRVW', 2)
2624 B.addApproval('CRVW', 2)
2625 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2626 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2627 self.waitUntilSettled()
2628
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002629 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002630 if x.name == 'project-testfile']
2631
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002632 self.assertEqual(len(testfile_jobs), 1)
2633 self.assertEqual(testfile_jobs[0].changes, '1,2')
2634 self.assertEqual(A.data['status'], 'MERGED')
2635 self.assertEqual(A.reported, 2)
2636 self.assertEqual(B.data['status'], 'MERGED')
2637 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002638
2639 def test_test_config(self):
2640 "Test that we can test the config"
2641 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002642 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002643 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002644 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002645
2646 def test_build_description(self):
2647 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002648 self.worker.registerFunction('set_description:' +
2649 self.worker.worker_id)
2650
2651 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2652 A.addApproval('CRVW', 2)
2653 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2654 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002655 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002656 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002657 self.assertTrue(re.search("Branch.*master", desc))
2658 self.assertTrue(re.search("Pipeline.*gate", desc))
2659 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2660 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2661 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2662 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002663
James E. Blair64ed6f22013-07-10 14:07:23 -07002664 def test_queue_precedence(self):
2665 "Test that queue precedence works"
2666
2667 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002668 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002669 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2670 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2671 A.addApproval('CRVW', 2)
2672 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2673
2674 self.waitUntilSettled()
2675 self.gearman_server.hold_jobs_in_queue = False
2676 self.gearman_server.release()
2677 self.waitUntilSettled()
2678
James E. Blair8de58bd2013-07-18 16:23:33 -07002679 # Run one build at a time to ensure non-race order:
2680 for x in range(6):
2681 self.release(self.builds[0])
2682 self.waitUntilSettled()
2683 self.worker.hold_jobs_in_build = False
2684 self.waitUntilSettled()
2685
James E. Blair64ed6f22013-07-10 14:07:23 -07002686 self.log.debug(self.history)
2687 self.assertEqual(self.history[0].pipeline, 'gate')
2688 self.assertEqual(self.history[1].pipeline, 'check')
2689 self.assertEqual(self.history[2].pipeline, 'gate')
2690 self.assertEqual(self.history[3].pipeline, 'gate')
2691 self.assertEqual(self.history[4].pipeline, 'check')
2692 self.assertEqual(self.history[5].pipeline, 'check')
2693
James E. Blair1843a552013-07-03 14:19:52 -07002694 def test_json_status(self):
2695 "Test that we can retrieve JSON status info"
2696 self.worker.hold_jobs_in_build = True
2697 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2698 A.addApproval('CRVW', 2)
2699 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2700 self.waitUntilSettled()
2701
2702 port = self.webapp.server.socket.getsockname()[1]
2703
2704 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2705 data = f.read()
2706
2707 self.worker.hold_jobs_in_build = False
2708 self.worker.release()
2709 self.waitUntilSettled()
2710
2711 data = json.loads(data)
2712 status_jobs = set()
2713 for p in data['pipelines']:
2714 for q in p['change_queues']:
2715 for head in q['heads']:
2716 for change in head:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002717 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002718 for job in change['jobs']:
2719 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002720 self.assertIn('project-merge', status_jobs)
2721 self.assertIn('project-test1', status_jobs)
2722 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002723
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002724 def test_node_label(self):
2725 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002726 self.worker.registerFunction('build:node-project-test1:debian')
2727
2728 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2729 A.addApproval('CRVW', 2)
2730 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2731 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002732
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002733 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2734 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2735 'debian')
2736 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002737
2738 def test_live_reconfiguration(self):
2739 "Test that live reconfiguration works"
2740 self.worker.hold_jobs_in_build = True
2741 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2742 A.addApproval('CRVW', 2)
2743 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2744 self.waitUntilSettled()
2745
2746 self.sched.reconfigure(self.config)
2747
2748 self.worker.hold_jobs_in_build = False
2749 self.worker.release()
2750 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002751 self.assertEqual(self.getJobFromHistory('project-merge').result,
2752 'SUCCESS')
2753 self.assertEqual(self.getJobFromHistory('project-test1').result,
2754 'SUCCESS')
2755 self.assertEqual(self.getJobFromHistory('project-test2').result,
2756 'SUCCESS')
2757 self.assertEqual(A.data['status'], 'MERGED')
2758 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002759
James E. Blaire712d9f2013-07-31 11:40:11 -07002760 def test_live_reconfiguration_functions(self):
2761 "Test live reconfiguration with a custom function"
2762 self.worker.registerFunction('build:node-project-test1:debian')
2763 self.worker.registerFunction('build:node-project-test1:wheezy')
2764 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2765 A.addApproval('CRVW', 2)
2766 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2767 self.waitUntilSettled()
2768
2769 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2770 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2771 'debian')
2772 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2773
2774 self.config.set('zuul', 'layout_config',
2775 'tests/fixtures/layout-live-'
2776 'reconfiguration-functions.yaml')
2777 self.sched.reconfigure(self.config)
2778 self.worker.build_history = []
2779
2780 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
2781 B.addApproval('CRVW', 2)
2782 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2783 self.waitUntilSettled()
2784
2785 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2786 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2787 'wheezy')
2788 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2789
James E. Blair287c06d2013-07-24 10:39:30 -07002790 def test_delayed_repo_init(self):
2791 self.config.set('zuul', 'layout_config',
2792 'tests/fixtures/layout-delayed-repo-init.yaml')
2793 self.sched.reconfigure(self.config)
2794
2795 self.init_repo("org/new-project")
2796 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2797
2798 A.addApproval('CRVW', 2)
2799 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2800 self.waitUntilSettled()
2801 self.assertEqual(self.getJobFromHistory('project-merge').result,
2802 'SUCCESS')
2803 self.assertEqual(self.getJobFromHistory('project-test1').result,
2804 'SUCCESS')
2805 self.assertEqual(self.getJobFromHistory('project-test2').result,
2806 'SUCCESS')
2807 self.assertEqual(A.data['status'], 'MERGED')
2808 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002809
2810 def test_timer(self):
2811 "Test that a periodic job is triggered"
2812 self.worker.hold_jobs_in_build = True
2813 self.config.set('zuul', 'layout_config',
2814 'tests/fixtures/layout-timer.yaml')
2815 self.sched.reconfigure(self.config)
2816 self.registerJobs()
2817
2818 start = time.time()
2819 failed = True
2820 while ((time.time() - start) < 30):
2821 if len(self.builds) == 2:
2822 failed = False
2823 break
2824 else:
2825 time.sleep(1)
2826
2827 if failed:
2828 raise Exception("Expected jobs never ran")
2829
2830 self.waitUntilSettled()
2831 port = self.webapp.server.socket.getsockname()[1]
2832
2833 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2834 data = f.read()
2835
2836 self.worker.hold_jobs_in_build = False
2837 self.worker.release()
2838 self.waitUntilSettled()
2839
2840 self.assertEqual(self.getJobFromHistory(
2841 'project-bitrot-stable-old').result, 'SUCCESS')
2842 self.assertEqual(self.getJobFromHistory(
2843 'project-bitrot-stable-older').result, 'SUCCESS')
2844
2845 data = json.loads(data)
2846 status_jobs = set()
2847 for p in data['pipelines']:
2848 for q in p['change_queues']:
2849 for head in q['heads']:
2850 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002851 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002852 for job in change['jobs']:
2853 status_jobs.add(job['name'])
2854 self.assertIn('project-bitrot-stable-old', status_jobs)
2855 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002856
2857 def test_check_smtp_pool(self):
2858 self.config.set('zuul', 'layout_config',
2859 'tests/fixtures/layout-smtp.yaml')
2860 self.sched.reconfigure(self.config)
2861
2862 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2863 self.waitUntilSettled()
2864
2865 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2866 self.waitUntilSettled()
2867
2868 self.assertEqual(len(FakeSMTP.messages), 2)
2869
2870 # A.messages only holds what FakeGerrit places in it. Thus we
2871 # work on the knowledge of what the first message should be as
2872 # it is only configured to go to SMTP.
2873
2874 self.assertEqual('zuul@example.com',
2875 FakeSMTP.messages[0]['from_email'])
2876 self.assertEqual(['you@example.com'],
2877 FakeSMTP.messages[0]['to_email'])
2878 self.assertEqual('Starting check jobs.',
2879 FakeSMTP.messages[0]['body'])
2880
2881 self.assertEqual('zuul_from@example.com',
2882 FakeSMTP.messages[1]['from_email'])
2883 self.assertEqual(['alternative_me@example.com'],
2884 FakeSMTP.messages[1]['to_email'])
2885 self.assertEqual(A.messages[0],
2886 FakeSMTP.messages[1]['body'])