blob: 70b68c54e927fb5d9657f2e0d0cc959412b83402 [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 if len(queue.severed_heads) != 0:
1072 print 'heads', queue.severed_heads
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001073 self.assertEqual(len(queue.severed_heads), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001074
James E. Blair66eeebf2013-07-27 17:44:32 -07001075 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001076 start = time.time()
1077 while time.time() < (start + 5):
1078 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001079 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001080 k, v = stat.split(':')
1081 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001082 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001083 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001084 elif value:
1085 if value == v:
1086 return
1087 elif kind:
1088 if v.endswith('|' + kind):
1089 return
James E. Blair412e5582013-04-22 15:50:12 -07001090 time.sleep(0.1)
1091
1092 pprint.pprint(self.statsd.stats)
1093 raise Exception("Key %s not found in reported stats" % key)
1094
James E. Blairb0fcae42012-07-17 11:12:10 -07001095 def test_jobs_launched(self):
1096 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001097
James E. Blairb0fcae42012-07-17 11:12:10 -07001098 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001099 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001100 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1101 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001102 self.assertEqual(self.getJobFromHistory('project-merge').result,
1103 'SUCCESS')
1104 self.assertEqual(self.getJobFromHistory('project-test1').result,
1105 'SUCCESS')
1106 self.assertEqual(self.getJobFromHistory('project-test2').result,
1107 'SUCCESS')
1108 self.assertEqual(A.data['status'], 'MERGED')
1109 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001110
James E. Blair66eeebf2013-07-27 17:44:32 -07001111 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1112 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1113 value='1|g')
1114 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1115 kind='ms')
1116 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1117 value='1|c')
1118 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1119 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1120 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001121 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001122 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001123 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001124 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001125
James E. Blair3cb10702013-08-24 08:56:03 -07001126 def test_initial_pipeline_gauges(self):
1127 "Test that each pipeline reported its length on start"
1128 pipeline_names = self.sched.layout.pipelines.keys()
1129 self.assertNotEqual(len(pipeline_names), 0)
1130 for name in pipeline_names:
1131 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
1132 value='0|g')
1133
James E. Blair42f74822013-05-14 15:18:03 -07001134 def test_duplicate_pipelines(self):
1135 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001136
James E. Blair42f74822013-05-14 15:18:03 -07001137 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1138 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1139 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001140
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001141 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001142 self.history[0].name == 'project-test1'
1143 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001144
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001145 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001146 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001147 self.assertIn('dup1/project-test1', A.messages[0])
1148 self.assertNotIn('dup2/project-test1', A.messages[0])
1149 self.assertNotIn('dup1/project-test1', A.messages[1])
1150 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001151 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001152 self.assertIn('dup1/project-test1', A.messages[1])
1153 self.assertNotIn('dup2/project-test1', A.messages[1])
1154 self.assertNotIn('dup1/project-test1', A.messages[0])
1155 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001156
James E. Blairb0fcae42012-07-17 11:12:10 -07001157 def test_parallel_changes(self):
1158 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001159
1160 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001161 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1162 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1163 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001164 A.addApproval('CRVW', 2)
1165 B.addApproval('CRVW', 2)
1166 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001167
1168 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1169 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1170 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1171
1172 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001173 self.assertEqual(len(self.builds), 1)
1174 self.assertEqual(self.builds[0].name, 'project-merge')
1175 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001176
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001177 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001178 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001179 self.assertEqual(len(self.builds), 3)
1180 self.assertEqual(self.builds[0].name, 'project-test1')
1181 self.assertTrue(self.job_has_changes(self.builds[0], A))
1182 self.assertEqual(self.builds[1].name, 'project-test2')
1183 self.assertTrue(self.job_has_changes(self.builds[1], A))
1184 self.assertEqual(self.builds[2].name, 'project-merge')
1185 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001186
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001187 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001188 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001189 self.assertEqual(len(self.builds), 5)
1190 self.assertEqual(self.builds[0].name, 'project-test1')
1191 self.assertTrue(self.job_has_changes(self.builds[0], A))
1192 self.assertEqual(self.builds[1].name, 'project-test2')
1193 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001194
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001195 self.assertEqual(self.builds[2].name, 'project-test1')
1196 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1197 self.assertEqual(self.builds[3].name, 'project-test2')
1198 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001199
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001200 self.assertEqual(self.builds[4].name, 'project-merge')
1201 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001202
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001203 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001204 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001205 self.assertEqual(len(self.builds), 6)
1206 self.assertEqual(self.builds[0].name, 'project-test1')
1207 self.assertTrue(self.job_has_changes(self.builds[0], A))
1208 self.assertEqual(self.builds[1].name, 'project-test2')
1209 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001210
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001211 self.assertEqual(self.builds[2].name, 'project-test1')
1212 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1213 self.assertEqual(self.builds[3].name, 'project-test2')
1214 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001215
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001216 self.assertEqual(self.builds[4].name, 'project-test1')
1217 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1218 self.assertEqual(self.builds[5].name, 'project-test2')
1219 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001220
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001221 self.worker.hold_jobs_in_build = False
1222 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001223 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001224 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001225
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001226 self.assertEqual(len(self.history), 9)
1227 self.assertEqual(A.data['status'], 'MERGED')
1228 self.assertEqual(B.data['status'], 'MERGED')
1229 self.assertEqual(C.data['status'], 'MERGED')
1230 self.assertEqual(A.reported, 2)
1231 self.assertEqual(B.reported, 2)
1232 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001233
1234 def test_failed_changes(self):
1235 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001236 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001237
James E. Blairb02a3bb2012-07-30 17:49:55 -07001238 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1239 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001240 A.addApproval('CRVW', 2)
1241 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001242
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001243 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001244
James E. Blaire2819012013-06-28 17:17:26 -04001245 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1246 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001247 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001248
1249 self.worker.release('.*-merge')
1250 self.waitUntilSettled()
1251
1252 self.worker.hold_jobs_in_build = False
1253 self.worker.release()
1254
1255 self.waitUntilSettled()
1256 # It's certain that the merge job for change 2 will run, but
1257 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001258 self.assertTrue(len(self.history) > 6)
1259 self.assertEqual(A.data['status'], 'NEW')
1260 self.assertEqual(B.data['status'], 'MERGED')
1261 self.assertEqual(A.reported, 2)
1262 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001263
1264 def test_independent_queues(self):
1265 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001266
1267 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001268 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001269 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1270 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001271 A.addApproval('CRVW', 2)
1272 B.addApproval('CRVW', 2)
1273 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001274
1275 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1276 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1277 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1278
James E. Blairb02a3bb2012-07-30 17:49:55 -07001279 self.waitUntilSettled()
1280
1281 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001282 self.assertEqual(len(self.builds), 2)
1283 self.assertEqual(self.builds[0].name, 'project-merge')
1284 self.assertTrue(self.job_has_changes(self.builds[0], A))
1285 self.assertEqual(self.builds[1].name, 'project1-merge')
1286 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001287
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001288 # Release the current merge builds
1289 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001290 self.waitUntilSettled()
1291 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001292 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001293 self.waitUntilSettled()
1294
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001295 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001296 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001297 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001298
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001299 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001300 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001301 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001302
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001303 self.assertEqual(len(self.history), 11)
1304 self.assertEqual(A.data['status'], 'MERGED')
1305 self.assertEqual(B.data['status'], 'MERGED')
1306 self.assertEqual(C.data['status'], 'MERGED')
1307 self.assertEqual(A.reported, 2)
1308 self.assertEqual(B.reported, 2)
1309 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001310
1311 def test_failed_change_at_head(self):
1312 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001313
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001314 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001315 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1316 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1317 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001318 A.addApproval('CRVW', 2)
1319 B.addApproval('CRVW', 2)
1320 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001321
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001322 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001323
1324 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1325 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1326 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1327
1328 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001329
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001330 self.assertEqual(len(self.builds), 1)
1331 self.assertEqual(self.builds[0].name, 'project-merge')
1332 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001333
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001334 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001335 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001336 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001337 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001338 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001339 self.waitUntilSettled()
1340
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001341 self.assertEqual(len(self.builds), 6)
1342 self.assertEqual(self.builds[0].name, 'project-test1')
1343 self.assertEqual(self.builds[1].name, 'project-test2')
1344 self.assertEqual(self.builds[2].name, 'project-test1')
1345 self.assertEqual(self.builds[3].name, 'project-test2')
1346 self.assertEqual(self.builds[4].name, 'project-test1')
1347 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001348
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001349 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001350 self.waitUntilSettled()
1351
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001352 # project-test2, project-merge for B
1353 self.assertEqual(len(self.builds), 2)
1354 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001355
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001356 self.worker.hold_jobs_in_build = False
1357 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001358 self.waitUntilSettled()
1359
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001360 self.assertEqual(len(self.builds), 0)
1361 self.assertEqual(len(self.history), 15)
1362 self.assertEqual(A.data['status'], 'NEW')
1363 self.assertEqual(B.data['status'], 'MERGED')
1364 self.assertEqual(C.data['status'], 'MERGED')
1365 self.assertEqual(A.reported, 2)
1366 self.assertEqual(B.reported, 2)
1367 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001368
James E. Blair0aac4872013-08-23 14:02:38 -07001369 def test_failed_change_in_middle(self):
1370 "Test a failed change in the middle of the queue"
1371
1372 self.worker.hold_jobs_in_build = True
1373 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1374 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1375 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1376 A.addApproval('CRVW', 2)
1377 B.addApproval('CRVW', 2)
1378 C.addApproval('CRVW', 2)
1379
1380 self.worker.addFailTest('project-test1', B)
1381
1382 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1383 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1384 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1385
1386 self.waitUntilSettled()
1387
1388 self.worker.release('.*-merge')
1389 self.waitUntilSettled()
1390 self.worker.release('.*-merge')
1391 self.waitUntilSettled()
1392 self.worker.release('.*-merge')
1393 self.waitUntilSettled()
1394
1395 self.assertEqual(len(self.builds), 6)
1396 self.assertEqual(self.builds[0].name, 'project-test1')
1397 self.assertEqual(self.builds[1].name, 'project-test2')
1398 self.assertEqual(self.builds[2].name, 'project-test1')
1399 self.assertEqual(self.builds[3].name, 'project-test2')
1400 self.assertEqual(self.builds[4].name, 'project-test1')
1401 self.assertEqual(self.builds[5].name, 'project-test2')
1402
1403 self.release(self.builds[2])
1404 self.waitUntilSettled()
1405
1406 # project-test1 and project-test2 for A, project-test2 for B
1407 self.assertEqual(len(self.builds), 3)
1408 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1409
1410 # check that build status of aborted jobs are masked ('CANCELED')
1411 items = self.sched.layout.pipelines['gate'].getAllItems()
1412 builds = items[0].current_build_set.getBuilds()
1413 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1414 self.assertEqual(self.countJobResults(builds, None), 2)
1415 builds = items[1].current_build_set.getBuilds()
1416 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1417 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1418 self.assertEqual(self.countJobResults(builds, None), 1)
1419 builds = items[2].current_build_set.getBuilds()
1420 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1421 self.assertEqual(self.countJobResults(builds, 'CANCELED'), 2)
1422
1423 self.worker.hold_jobs_in_build = False
1424 self.worker.release()
1425 self.waitUntilSettled()
1426
1427 self.assertEqual(len(self.builds), 0)
1428 self.assertEqual(len(self.history), 12)
1429 self.assertEqual(A.data['status'], 'MERGED')
1430 self.assertEqual(B.data['status'], 'NEW')
1431 self.assertEqual(C.data['status'], 'MERGED')
1432 self.assertEqual(A.reported, 2)
1433 self.assertEqual(B.reported, 2)
1434 self.assertEqual(C.reported, 2)
1435
James E. Blaird466dc42012-07-31 10:42:56 -07001436 def test_failed_change_at_head_with_queue(self):
1437 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001438
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001439 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001440 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1441 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1442 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001443 A.addApproval('CRVW', 2)
1444 B.addApproval('CRVW', 2)
1445 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001446
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001447 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001448
1449 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1450 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1451 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1452
1453 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001454 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001455 self.assertEqual(len(self.builds), 0)
1456 self.assertEqual(len(queue), 1)
1457 self.assertEqual(queue[0].name, 'build:project-merge')
1458 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001459
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001460 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001461 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001462 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001463 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001464 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001465 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001466 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001467
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001468 self.assertEqual(len(self.builds), 0)
1469 self.assertEqual(len(queue), 6)
1470 self.assertEqual(queue[0].name, 'build:project-test1')
1471 self.assertEqual(queue[1].name, 'build:project-test2')
1472 self.assertEqual(queue[2].name, 'build:project-test1')
1473 self.assertEqual(queue[3].name, 'build:project-test2')
1474 self.assertEqual(queue[4].name, 'build:project-test1')
1475 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001476
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001477 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001478 self.waitUntilSettled()
1479
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001480 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001481 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001482 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1483 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001484
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001485 self.gearman_server.hold_jobs_in_queue = False
1486 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001487 self.waitUntilSettled()
1488
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001489 self.assertEqual(len(self.builds), 0)
1490 self.assertEqual(len(self.history), 11)
1491 self.assertEqual(A.data['status'], 'NEW')
1492 self.assertEqual(B.data['status'], 'MERGED')
1493 self.assertEqual(C.data['status'], 'MERGED')
1494 self.assertEqual(A.reported, 2)
1495 self.assertEqual(B.reported, 2)
1496 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001497
1498 def test_patch_order(self):
1499 "Test that dependent patches are tested in the right order"
1500 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1501 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1502 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1503 A.addApproval('CRVW', 2)
1504 B.addApproval('CRVW', 2)
1505 C.addApproval('CRVW', 2)
1506
1507 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1508 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1509 M2.setMerged()
1510 M1.setMerged()
1511
1512 # C -> B -> A -> M1 -> M2
1513 # M2 is here to make sure it is never queried. If it is, it
1514 # means zuul is walking down the entire history of merged
1515 # changes.
1516
1517 C.setDependsOn(B, 1)
1518 B.setDependsOn(A, 1)
1519 A.setDependsOn(M1, 1)
1520 M1.setDependsOn(M2, 1)
1521
1522 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1523
1524 self.waitUntilSettled()
1525
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001526 self.assertEqual(A.data['status'], 'NEW')
1527 self.assertEqual(B.data['status'], 'NEW')
1528 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001529
1530 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1531 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1532
1533 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001534 self.assertEqual(M2.queried, 0)
1535 self.assertEqual(A.data['status'], 'MERGED')
1536 self.assertEqual(B.data['status'], 'MERGED')
1537 self.assertEqual(C.data['status'], 'MERGED')
1538 self.assertEqual(A.reported, 2)
1539 self.assertEqual(B.reported, 2)
1540 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001541
James E. Blair0e933c52013-07-11 10:18:52 -07001542 def test_trigger_cache(self):
1543 "Test that the trigger cache operates correctly"
1544 self.worker.hold_jobs_in_build = True
1545
1546 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1547 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1548 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1549 A.addApproval('CRVW', 2)
1550 B.addApproval('CRVW', 2)
1551
1552 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1553 M1.setMerged()
1554
1555 B.setDependsOn(A, 1)
1556 A.setDependsOn(M1, 1)
1557
1558 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1559 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1560
1561 self.waitUntilSettled()
1562
1563 for build in self.builds:
1564 if build.parameters['ZUUL_PIPELINE'] == 'check':
1565 build.release()
1566 self.waitUntilSettled()
1567 for build in self.builds:
1568 if build.parameters['ZUUL_PIPELINE'] == 'check':
1569 build.release()
1570 self.waitUntilSettled()
1571
1572 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1573 self.waitUntilSettled()
1574
James E. Blair6c358e72013-07-29 17:06:47 -07001575 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001576 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001577 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001578
1579 self.worker.hold_jobs_in_build = False
1580 self.worker.release()
1581 self.waitUntilSettled()
1582
1583 self.assertEqual(A.data['status'], 'MERGED')
1584 self.assertEqual(B.data['status'], 'MERGED')
1585 self.assertEqual(A.queried, 2) # Initial and isMerged
1586 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1587
James E. Blair8c803f82012-07-31 16:25:42 -07001588 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001589 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001590 # TODO: move to test_gerrit (this is a unit test!)
1591 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001592 trigger = self.sched.layout.pipelines['gate'].trigger
1593 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001594 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001595 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001596
1597 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001598 a = trigger.getChange(1, 2, refresh=True)
1599 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001600
1601 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001602 a = trigger.getChange(1, 2, refresh=True)
1603 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1604 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001605
1606 def test_build_configuration(self):
1607 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001608
1609 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001610 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1611 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1612 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1613 A.addApproval('CRVW', 2)
1614 B.addApproval('CRVW', 2)
1615 C.addApproval('CRVW', 2)
1616 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1617 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1618 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1619 self.waitUntilSettled()
1620
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001621 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001622 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001623 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001624 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001625 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001626 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001627 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001628 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1629 self.gearman_server.hold_jobs_in_queue = False
1630 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001631 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001632
Monty Taylorbc758832013-06-17 17:22:42 -04001633 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001634 repo = git.Repo(path)
1635 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1636 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001637 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001638 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001639
1640 def test_build_configuration_conflict(self):
1641 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001642
1643 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001644 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1645 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001646 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001647 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1648 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001649 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001650 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1651 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001652 A.addApproval('CRVW', 2)
1653 B.addApproval('CRVW', 2)
1654 C.addApproval('CRVW', 2)
1655 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1656 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1657 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1658 self.waitUntilSettled()
1659
James E. Blair6736beb2013-07-11 15:18:15 -07001660 self.assertEqual(A.reported, 1)
1661 self.assertEqual(B.reported, 1)
1662 self.assertEqual(C.reported, 1)
1663
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001664 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001665 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001666 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001667 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001668 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001669 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001670 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001671 self.getParameter(queue[-1], 'ZUUL_REF')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001672 self.gearman_server.hold_jobs_in_queue = False
1673 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001674 self.waitUntilSettled()
1675
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001676 self.assertEqual(A.data['status'], 'MERGED')
1677 self.assertEqual(B.data['status'], 'NEW')
1678 self.assertEqual(C.data['status'], 'MERGED')
1679 self.assertEqual(A.reported, 2)
1680 self.assertEqual(B.reported, 2)
1681 self.assertEqual(C.reported, 2)
James E. Blairdaabed22012-08-15 15:38:57 -07001682
James E. Blair6736beb2013-07-11 15:18:15 -07001683 def test_dequeue_conflict(self):
1684 "Test that the option to dequeue merge conflicts works"
1685
1686 self.gearman_server.hold_jobs_in_queue = True
1687 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1688 A.addPatchset(['conflict'])
1689 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1690 B.addPatchset(['conflict'])
1691 A.addApproval('CRVW', 2)
1692 B.addApproval('CRVW', 2)
1693 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1694 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1695 self.waitUntilSettled()
1696
1697 self.assertEqual(A.reported, 1)
1698 self.assertEqual(B.reported, 2)
1699
1700 self.gearman_server.hold_jobs_in_queue = False
1701 self.gearman_server.release()
1702 self.waitUntilSettled()
1703
1704 self.assertEqual(A.data['status'], 'MERGED')
1705 self.assertEqual(B.data['status'], 'NEW')
1706 self.assertEqual(A.reported, 2)
1707 self.assertEqual(B.reported, 2)
1708
James E. Blairdaabed22012-08-15 15:38:57 -07001709 def test_post(self):
1710 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001711
Zhongyue Luo5d556072012-09-21 02:00:47 +09001712 e = {
1713 "type": "ref-updated",
1714 "submitter": {
1715 "name": "User Name",
1716 },
1717 "refUpdate": {
1718 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1719 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1720 "refName": "master",
1721 "project": "org/project",
1722 }
1723 }
James E. Blairdaabed22012-08-15 15:38:57 -07001724 self.fake_gerrit.addEvent(e)
1725 self.waitUntilSettled()
1726
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001727 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001728 self.assertEqual(len(self.history), 1)
1729 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001730
1731 def test_build_configuration_branch(self):
1732 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001733
1734 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001735 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1736 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1737 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1738 A.addApproval('CRVW', 2)
1739 B.addApproval('CRVW', 2)
1740 C.addApproval('CRVW', 2)
1741 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1742 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1743 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1744 self.waitUntilSettled()
1745
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001746 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001747 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001748 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001749 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001750 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001751 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001752 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001753 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1754 self.gearman_server.hold_jobs_in_queue = False
1755 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001756 self.waitUntilSettled()
1757
Monty Taylorbc758832013-06-17 17:22:42 -04001758 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001759 repo = git.Repo(path)
1760 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1761 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001762 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001763 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001764
1765 def test_build_configuration_branch_interaction(self):
1766 "Test that switching between branches works"
1767 self.test_build_configuration()
1768 self.test_build_configuration_branch()
1769 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001770 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001771 repo = git.Repo(path)
1772 repo.heads.master.commit = repo.commit('init')
1773 self.test_build_configuration()
1774
1775 def test_build_configuration_multi_branch(self):
1776 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001777
1778 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001779 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1780 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1781 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1782 A.addApproval('CRVW', 2)
1783 B.addApproval('CRVW', 2)
1784 C.addApproval('CRVW', 2)
1785 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1786 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1787 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1788 self.waitUntilSettled()
1789
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001790 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001791 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001792 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001793 job_B = None
1794 for job in queue:
1795 if 'project-merge' in job.name:
1796 job_B = job
1797 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001798 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001799 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001800 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001801 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001802 for job in queue:
1803 if 'project-merge' in job.name:
1804 job_C = job
1805 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001806 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001807 self.gearman_server.hold_jobs_in_queue = False
1808 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001809 self.waitUntilSettled()
1810
Monty Taylorbc758832013-06-17 17:22:42 -04001811 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001812 repo = git.Repo(path)
1813
1814 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001815 for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001816 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001817 correct_messages = ['initial commit', 'A-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001818 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001819
1820 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001821 for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001822 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001823 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001824 self.assertEqual(repo_messages, correct_messages)
James E. Blair7f71c802012-08-22 13:04:32 -07001825
1826 def test_one_job_project(self):
1827 "Test that queueing works with one job"
1828 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1829 'master', 'A')
1830 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1831 'master', 'B')
1832 A.addApproval('CRVW', 2)
1833 B.addApproval('CRVW', 2)
1834 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1835 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1836 self.waitUntilSettled()
1837
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001838 self.assertEqual(A.data['status'], 'MERGED')
1839 self.assertEqual(A.reported, 2)
1840 self.assertEqual(B.data['status'], 'MERGED')
1841 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001842
Antoine Musso80edd5a2013-02-13 15:37:53 +01001843 def test_job_from_templates_launched(self):
1844 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001845
Antoine Musso80edd5a2013-02-13 15:37:53 +01001846 A = self.fake_gerrit.addFakeChange(
1847 'org/templated-project', 'master', 'A')
1848 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1849 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001850
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001851 self.assertEqual(self.getJobFromHistory('project-test1').result,
1852 'SUCCESS')
1853 self.assertEqual(self.getJobFromHistory('project-test2').result,
1854 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001855
James E. Blaircaec0c52012-08-22 14:52:22 -07001856 def test_dependent_changes_dequeue(self):
1857 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001858
James E. Blaircaec0c52012-08-22 14:52:22 -07001859 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1860 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1861 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1862 A.addApproval('CRVW', 2)
1863 B.addApproval('CRVW', 2)
1864 C.addApproval('CRVW', 2)
1865
1866 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1867 M1.setMerged()
1868
1869 # C -> B -> A -> M1
1870
1871 C.setDependsOn(B, 1)
1872 B.setDependsOn(A, 1)
1873 A.setDependsOn(M1, 1)
1874
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001875 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001876
1877 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1878 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1879 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1880
1881 self.waitUntilSettled()
1882
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001883 self.assertEqual(A.data['status'], 'NEW')
1884 self.assertEqual(A.reported, 2)
1885 self.assertEqual(B.data['status'], 'NEW')
1886 self.assertEqual(B.reported, 2)
1887 self.assertEqual(C.data['status'], 'NEW')
1888 self.assertEqual(C.reported, 2)
1889 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001890
1891 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001892 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001893 # If it's dequeued more than once, we should see extra
1894 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001895
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001896 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001897 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1898 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1899 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1900 A.addApproval('CRVW', 2)
1901 B.addApproval('CRVW', 2)
1902 C.addApproval('CRVW', 2)
1903
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001904 self.worker.addFailTest('project1-test1', A)
1905 self.worker.addFailTest('project1-test2', A)
1906 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001907
1908 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1909 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1910 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1911
1912 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001913
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001914 self.assertEqual(len(self.builds), 1)
1915 self.assertEqual(self.builds[0].name, 'project1-merge')
1916 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07001917
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001918 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001919 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001920 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001921 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001922 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001923 self.waitUntilSettled()
1924
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001925 self.assertEqual(len(self.builds), 9)
1926 self.assertEqual(self.builds[0].name, 'project1-test1')
1927 self.assertEqual(self.builds[1].name, 'project1-test2')
1928 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
1929 self.assertEqual(self.builds[3].name, 'project1-test1')
1930 self.assertEqual(self.builds[4].name, 'project1-test2')
1931 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
1932 self.assertEqual(self.builds[6].name, 'project1-test1')
1933 self.assertEqual(self.builds[7].name, 'project1-test2')
1934 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001935
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001936 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001937 self.waitUntilSettled()
1938
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001939 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1940 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001941
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001942 self.worker.hold_jobs_in_build = False
1943 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001944 self.waitUntilSettled()
1945
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001946 self.assertEqual(len(self.builds), 0)
1947 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001948
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001949 self.assertEqual(A.data['status'], 'NEW')
1950 self.assertEqual(B.data['status'], 'MERGED')
1951 self.assertEqual(C.data['status'], 'MERGED')
1952 self.assertEqual(A.reported, 2)
1953 self.assertEqual(B.reported, 2)
1954 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001955
1956 def test_nonvoting_job(self):
1957 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001958
James E. Blair4ec821f2012-08-23 15:28:28 -07001959 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1960 'master', 'A')
1961 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001962 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001963 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1964
1965 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001966
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001967 self.assertEqual(A.data['status'], 'MERGED')
1968 self.assertEqual(A.reported, 2)
1969 self.assertEqual(
1970 self.getJobFromHistory('nonvoting-project-merge').result,
1971 'SUCCESS')
1972 self.assertEqual(
1973 self.getJobFromHistory('nonvoting-project-test1').result,
1974 'SUCCESS')
1975 self.assertEqual(
1976 self.getJobFromHistory('nonvoting-project-test2').result,
1977 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001978
1979 def test_check_queue_success(self):
1980 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001981
James E. Blaire0487072012-08-29 17:38:31 -07001982 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1983 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1984
1985 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001986
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001987 self.assertEqual(A.data['status'], 'NEW')
1988 self.assertEqual(A.reported, 1)
1989 self.assertEqual(self.getJobFromHistory('project-merge').result,
1990 'SUCCESS')
1991 self.assertEqual(self.getJobFromHistory('project-test1').result,
1992 'SUCCESS')
1993 self.assertEqual(self.getJobFromHistory('project-test2').result,
1994 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001995
1996 def test_check_queue_failure(self):
1997 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001998
James E. Blaire0487072012-08-29 17:38:31 -07001999 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002000 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07002001 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2002
2003 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002004
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002005 self.assertEqual(A.data['status'], 'NEW')
2006 self.assertEqual(A.reported, 1)
2007 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07002008 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002009 self.assertEqual(self.getJobFromHistory('project-test1').result,
2010 'SUCCESS')
2011 self.assertEqual(self.getJobFromHistory('project-test2').result,
2012 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07002013
2014 def test_dependent_behind_dequeue(self):
2015 "test that dependent changes behind dequeued changes work"
2016 # This complicated test is a reproduction of a real life bug
2017 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07002018
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002019 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07002020 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2021 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2022 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2023 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2024 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
2025 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
2026 D.setDependsOn(C, 1)
2027 E.setDependsOn(D, 1)
2028 A.addApproval('CRVW', 2)
2029 B.addApproval('CRVW', 2)
2030 C.addApproval('CRVW', 2)
2031 D.addApproval('CRVW', 2)
2032 E.addApproval('CRVW', 2)
2033 F.addApproval('CRVW', 2)
2034
2035 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07002036
2037 # Change object re-use in the gerrit trigger is hidden if
2038 # changes are added in quick succession; waiting makes it more
2039 # like real life.
2040 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2041 self.waitUntilSettled()
2042 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2043 self.waitUntilSettled()
2044
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002045 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002046 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002047 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002048 self.waitUntilSettled()
2049
2050 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2051 self.waitUntilSettled()
2052 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2053 self.waitUntilSettled()
2054 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2055 self.waitUntilSettled()
2056 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
2057 self.waitUntilSettled()
2058
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002059 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002060 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002061 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002062 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002063 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002064 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002065 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002066 self.waitUntilSettled()
2067
2068 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002069
2070 # Grab pointers to the jobs we want to release before
2071 # releasing any, because list indexes may change as
2072 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002073 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002074 a.release()
2075 b.release()
2076 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002077 self.waitUntilSettled()
2078
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002079 self.worker.hold_jobs_in_build = False
2080 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002081 self.waitUntilSettled()
2082
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002083 self.assertEqual(A.data['status'], 'NEW')
2084 self.assertEqual(B.data['status'], 'MERGED')
2085 self.assertEqual(C.data['status'], 'MERGED')
2086 self.assertEqual(D.data['status'], 'MERGED')
2087 self.assertEqual(E.data['status'], 'MERGED')
2088 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002089
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002090 self.assertEqual(A.reported, 2)
2091 self.assertEqual(B.reported, 2)
2092 self.assertEqual(C.reported, 2)
2093 self.assertEqual(D.reported, 2)
2094 self.assertEqual(E.reported, 2)
2095 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002096
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002097 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2098 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002099
2100 def test_merger_repack(self):
2101 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002102
James E. Blair05fed602012-09-07 12:45:24 -07002103 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2104 A.addApproval('CRVW', 2)
2105 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2106 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002107 self.assertEqual(self.getJobFromHistory('project-merge').result,
2108 'SUCCESS')
2109 self.assertEqual(self.getJobFromHistory('project-test1').result,
2110 'SUCCESS')
2111 self.assertEqual(self.getJobFromHistory('project-test2').result,
2112 'SUCCESS')
2113 self.assertEqual(A.data['status'], 'MERGED')
2114 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07002115 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002116 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002117
Monty Taylorbc758832013-06-17 17:22:42 -04002118 path = os.path.join(self.git_root, "org/project")
2119 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002120
2121 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2122 A.addApproval('CRVW', 2)
2123 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2124 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002125 self.assertEqual(self.getJobFromHistory('project-merge').result,
2126 'SUCCESS')
2127 self.assertEqual(self.getJobFromHistory('project-test1').result,
2128 'SUCCESS')
2129 self.assertEqual(self.getJobFromHistory('project-test2').result,
2130 'SUCCESS')
2131 self.assertEqual(A.data['status'], 'MERGED')
2132 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002133
James E. Blair4886f282012-11-15 09:27:33 -08002134 def test_merger_repack_large_change(self):
2135 "Test that the merger works with large changes after a repack"
2136 # https://bugs.launchpad.net/zuul/+bug/1078946
2137 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2138 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002139 path = os.path.join(self.upstream_root, "org/project1")
2140 print repack_repo(path)
2141 path = os.path.join(self.git_root, "org/project1")
2142 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002143
2144 A.addApproval('CRVW', 2)
2145 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2146 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002147 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2148 'SUCCESS')
2149 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2150 'SUCCESS')
2151 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2152 'SUCCESS')
2153 self.assertEqual(A.data['status'], 'MERGED')
2154 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002155
James E. Blair7ee88a22012-09-12 18:59:31 +02002156 def test_nonexistent_job(self):
2157 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002158 # Set to the state immediately after a restart
2159 self.resetGearmanServer()
2160 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002161
2162 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2163 A.addApproval('CRVW', 2)
2164 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2165 # There may be a thread about to report a lost change
2166 while A.reported < 2:
2167 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002168 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002169 self.assertFalse(job_names)
2170 self.assertEqual(A.data['status'], 'NEW')
2171 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002172 self.assertEmptyQueues()
2173
2174 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002175 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002176 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2177 A.addApproval('CRVW', 2)
2178 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2179 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002180 self.assertEqual(self.getJobFromHistory('project-merge').result,
2181 'SUCCESS')
2182 self.assertEqual(self.getJobFromHistory('project-test1').result,
2183 'SUCCESS')
2184 self.assertEqual(self.getJobFromHistory('project-test2').result,
2185 'SUCCESS')
2186 self.assertEqual(A.data['status'], 'MERGED')
2187 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002188
2189 def test_single_nonexistent_post_job(self):
2190 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002191 e = {
2192 "type": "ref-updated",
2193 "submitter": {
2194 "name": "User Name",
2195 },
2196 "refUpdate": {
2197 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2198 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2199 "refName": "master",
2200 "project": "org/project",
2201 }
2202 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002203 # Set to the state immediately after a restart
2204 self.resetGearmanServer()
2205 self.launcher.negative_function_cache_ttl = 0
2206
James E. Blairf62d4282012-12-31 17:01:50 -08002207 self.fake_gerrit.addEvent(e)
2208 self.waitUntilSettled()
2209
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002210 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002211
2212 def test_new_patchset_dequeues_old(self):
2213 "Test that a new patchset causes the old to be dequeued"
2214 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002215 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002216 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2217 M.setMerged()
2218
2219 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2220 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2221 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2222 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2223 A.addApproval('CRVW', 2)
2224 B.addApproval('CRVW', 2)
2225 C.addApproval('CRVW', 2)
2226 D.addApproval('CRVW', 2)
2227
2228 C.setDependsOn(B, 1)
2229 B.setDependsOn(A, 1)
2230 A.setDependsOn(M, 1)
2231
2232 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2233 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2234 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2235 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2236 self.waitUntilSettled()
2237
2238 B.addPatchset()
2239 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2240 self.waitUntilSettled()
2241
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002242 self.worker.hold_jobs_in_build = False
2243 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002244 self.waitUntilSettled()
2245
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002246 self.assertEqual(A.data['status'], 'MERGED')
2247 self.assertEqual(A.reported, 2)
2248 self.assertEqual(B.data['status'], 'NEW')
2249 self.assertEqual(B.reported, 2)
2250 self.assertEqual(C.data['status'], 'NEW')
2251 self.assertEqual(C.reported, 2)
2252 self.assertEqual(D.data['status'], 'MERGED')
2253 self.assertEqual(D.reported, 2)
2254 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002255
2256 def test_new_patchset_dequeues_old_on_head(self):
2257 "Test that a new patchset causes the old to be dequeued (at head)"
2258 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002259 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002260 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2261 M.setMerged()
2262 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2263 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2264 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2265 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2266 A.addApproval('CRVW', 2)
2267 B.addApproval('CRVW', 2)
2268 C.addApproval('CRVW', 2)
2269 D.addApproval('CRVW', 2)
2270
2271 C.setDependsOn(B, 1)
2272 B.setDependsOn(A, 1)
2273 A.setDependsOn(M, 1)
2274
2275 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2276 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2277 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2278 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2279 self.waitUntilSettled()
2280
2281 A.addPatchset()
2282 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2283 self.waitUntilSettled()
2284
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002285 self.worker.hold_jobs_in_build = False
2286 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002287 self.waitUntilSettled()
2288
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002289 self.assertEqual(A.data['status'], 'NEW')
2290 self.assertEqual(A.reported, 2)
2291 self.assertEqual(B.data['status'], 'NEW')
2292 self.assertEqual(B.reported, 2)
2293 self.assertEqual(C.data['status'], 'NEW')
2294 self.assertEqual(C.reported, 2)
2295 self.assertEqual(D.data['status'], 'MERGED')
2296 self.assertEqual(D.reported, 2)
2297 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002298
2299 def test_new_patchset_dequeues_old_without_dependents(self):
2300 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002301 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002302 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2303 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2304 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2305 A.addApproval('CRVW', 2)
2306 B.addApproval('CRVW', 2)
2307 C.addApproval('CRVW', 2)
2308
2309 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2310 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2311 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2312 self.waitUntilSettled()
2313
2314 B.addPatchset()
2315 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2316 self.waitUntilSettled()
2317
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002318 self.worker.hold_jobs_in_build = False
2319 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002320 self.waitUntilSettled()
2321
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002322 self.assertEqual(A.data['status'], 'MERGED')
2323 self.assertEqual(A.reported, 2)
2324 self.assertEqual(B.data['status'], 'NEW')
2325 self.assertEqual(B.reported, 2)
2326 self.assertEqual(C.data['status'], 'MERGED')
2327 self.assertEqual(C.reported, 2)
2328 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002329
2330 def test_new_patchset_dequeues_old_independent_queue(self):
2331 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002332 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002333 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2334 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2335 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2336 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2337 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2338 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2339 self.waitUntilSettled()
2340
2341 B.addPatchset()
2342 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2343 self.waitUntilSettled()
2344
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002345 self.worker.hold_jobs_in_build = False
2346 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002347 self.waitUntilSettled()
2348
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002349 self.assertEqual(A.data['status'], 'NEW')
2350 self.assertEqual(A.reported, 1)
2351 self.assertEqual(B.data['status'], 'NEW')
2352 self.assertEqual(B.reported, 1)
2353 self.assertEqual(C.data['status'], 'NEW')
2354 self.assertEqual(C.reported, 1)
2355 self.assertEqual(len(self.history), 10)
2356 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002357
2358 def test_zuul_refs(self):
2359 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002360 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002361 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2362 M1.setMerged()
2363 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2364 M2.setMerged()
2365
2366 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2367 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2368 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2369 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2370 A.addApproval('CRVW', 2)
2371 B.addApproval('CRVW', 2)
2372 C.addApproval('CRVW', 2)
2373 D.addApproval('CRVW', 2)
2374 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2375 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2376 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2377 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2378
2379 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002380 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002381 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002382 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002383 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002384 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002385 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002386 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002387 self.waitUntilSettled()
2388
James E. Blair7d0dedc2013-02-21 17:26:09 -08002389 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002390 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002391 if x.parameters['ZUUL_CHANGE'] == '3':
2392 a_zref = x.parameters['ZUUL_REF']
2393 if x.parameters['ZUUL_CHANGE'] == '4':
2394 b_zref = x.parameters['ZUUL_REF']
2395 if x.parameters['ZUUL_CHANGE'] == '5':
2396 c_zref = x.parameters['ZUUL_REF']
2397 if x.parameters['ZUUL_CHANGE'] == '6':
2398 d_zref = x.parameters['ZUUL_REF']
2399
2400 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002401 self.assertIsNotNone(a_zref)
2402 self.assertIsNotNone(b_zref)
2403 self.assertIsNotNone(c_zref)
2404 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002405
2406 # And they should all be different
2407 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002408 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002409
2410 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002411 self.assertTrue(self.ref_has_change(a_zref, A))
2412 self.assertFalse(self.ref_has_change(a_zref, B))
2413 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002414
2415 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002416 self.assertTrue(self.ref_has_change(b_zref, A))
2417 self.assertTrue(self.ref_has_change(b_zref, B))
2418 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002419
2420 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002421 self.assertTrue(self.ref_has_change(c_zref, A))
2422 self.assertTrue(self.ref_has_change(c_zref, B))
2423 self.assertTrue(self.ref_has_change(c_zref, C))
2424 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002425
2426 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002427 self.assertTrue(self.ref_has_change(d_zref, A))
2428 self.assertTrue(self.ref_has_change(d_zref, B))
2429 self.assertTrue(self.ref_has_change(d_zref, C))
2430 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002431
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002432 self.worker.hold_jobs_in_build = False
2433 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002434 self.waitUntilSettled()
2435
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002436 self.assertEqual(A.data['status'], 'MERGED')
2437 self.assertEqual(A.reported, 2)
2438 self.assertEqual(B.data['status'], 'MERGED')
2439 self.assertEqual(B.reported, 2)
2440 self.assertEqual(C.data['status'], 'MERGED')
2441 self.assertEqual(C.reported, 2)
2442 self.assertEqual(D.data['status'], 'MERGED')
2443 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002444
James E. Blair4a28a882013-08-23 15:17:33 -07002445 def test_rerun_on_error(self):
2446 "Test that if a worker fails to run a job, it is run again"
2447 self.worker.hold_jobs_in_build = True
2448 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2449 A.addApproval('CRVW', 2)
2450 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2451 self.waitUntilSettled()
2452
2453 self.builds[0].run_error = True
2454 self.worker.hold_jobs_in_build = False
2455 self.worker.release()
2456 self.waitUntilSettled()
2457 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2458 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2459
James E. Blair412e5582013-04-22 15:50:12 -07002460 def test_statsd(self):
2461 "Test each of the statsd methods used in the scheduler"
2462 import extras
2463 statsd = extras.try_import('statsd.statsd')
2464 statsd.incr('test-incr')
2465 statsd.timing('test-timing', 3)
2466 statsd.gauge('test-guage', 12)
2467 self.assertReportedStat('test-incr', '1|c')
2468 self.assertReportedStat('test-timing', '3|ms')
2469 self.assertReportedStat('test-guage', '12|g')
2470
James E. Blair70c71582013-03-06 08:50:50 -08002471 def test_file_jobs(self):
2472 "Test that file jobs run only when appropriate"
2473 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2474 A.addPatchset(['pip-requires'])
2475 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2476 A.addApproval('CRVW', 2)
2477 B.addApproval('CRVW', 2)
2478 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2479 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2480 self.waitUntilSettled()
2481
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002482 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002483 if x.name == 'project-testfile']
2484
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002485 self.assertEqual(len(testfile_jobs), 1)
2486 self.assertEqual(testfile_jobs[0].changes, '1,2')
2487 self.assertEqual(A.data['status'], 'MERGED')
2488 self.assertEqual(A.reported, 2)
2489 self.assertEqual(B.data['status'], 'MERGED')
2490 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002491
2492 def test_test_config(self):
2493 "Test that we can test the config"
2494 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002495 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002496 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002497 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002498
2499 def test_build_description(self):
2500 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002501 self.worker.registerFunction('set_description:' +
2502 self.worker.worker_id)
2503
2504 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2505 A.addApproval('CRVW', 2)
2506 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2507 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002508 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002509 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002510 self.assertTrue(re.search("Branch.*master", desc))
2511 self.assertTrue(re.search("Pipeline.*gate", desc))
2512 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2513 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2514 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2515 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002516
James E. Blair64ed6f22013-07-10 14:07:23 -07002517 def test_queue_precedence(self):
2518 "Test that queue precedence works"
2519
2520 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002521 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002522 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2523 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2524 A.addApproval('CRVW', 2)
2525 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2526
2527 self.waitUntilSettled()
2528 self.gearman_server.hold_jobs_in_queue = False
2529 self.gearman_server.release()
2530 self.waitUntilSettled()
2531
James E. Blair8de58bd2013-07-18 16:23:33 -07002532 # Run one build at a time to ensure non-race order:
2533 for x in range(6):
2534 self.release(self.builds[0])
2535 self.waitUntilSettled()
2536 self.worker.hold_jobs_in_build = False
2537 self.waitUntilSettled()
2538
James E. Blair64ed6f22013-07-10 14:07:23 -07002539 self.log.debug(self.history)
2540 self.assertEqual(self.history[0].pipeline, 'gate')
2541 self.assertEqual(self.history[1].pipeline, 'check')
2542 self.assertEqual(self.history[2].pipeline, 'gate')
2543 self.assertEqual(self.history[3].pipeline, 'gate')
2544 self.assertEqual(self.history[4].pipeline, 'check')
2545 self.assertEqual(self.history[5].pipeline, 'check')
2546
James E. Blair1843a552013-07-03 14:19:52 -07002547 def test_json_status(self):
2548 "Test that we can retrieve JSON status info"
2549 self.worker.hold_jobs_in_build = True
2550 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2551 A.addApproval('CRVW', 2)
2552 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2553 self.waitUntilSettled()
2554
2555 port = self.webapp.server.socket.getsockname()[1]
2556
2557 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2558 data = f.read()
2559
2560 self.worker.hold_jobs_in_build = False
2561 self.worker.release()
2562 self.waitUntilSettled()
2563
2564 data = json.loads(data)
2565 status_jobs = set()
2566 for p in data['pipelines']:
2567 for q in p['change_queues']:
2568 for head in q['heads']:
2569 for change in head:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002570 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002571 for job in change['jobs']:
2572 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002573 self.assertIn('project-merge', status_jobs)
2574 self.assertIn('project-test1', status_jobs)
2575 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002576
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002577 def test_node_label(self):
2578 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002579 self.worker.registerFunction('build:node-project-test1:debian')
2580
2581 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2582 A.addApproval('CRVW', 2)
2583 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2584 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002585
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002586 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2587 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2588 'debian')
2589 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002590
2591 def test_live_reconfiguration(self):
2592 "Test that live reconfiguration works"
2593 self.worker.hold_jobs_in_build = True
2594 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2595 A.addApproval('CRVW', 2)
2596 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2597 self.waitUntilSettled()
2598
2599 self.sched.reconfigure(self.config)
2600
2601 self.worker.hold_jobs_in_build = False
2602 self.worker.release()
2603 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002604 self.assertEqual(self.getJobFromHistory('project-merge').result,
2605 'SUCCESS')
2606 self.assertEqual(self.getJobFromHistory('project-test1').result,
2607 'SUCCESS')
2608 self.assertEqual(self.getJobFromHistory('project-test2').result,
2609 'SUCCESS')
2610 self.assertEqual(A.data['status'], 'MERGED')
2611 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002612
James E. Blaire712d9f2013-07-31 11:40:11 -07002613 def test_live_reconfiguration_functions(self):
2614 "Test live reconfiguration with a custom function"
2615 self.worker.registerFunction('build:node-project-test1:debian')
2616 self.worker.registerFunction('build:node-project-test1:wheezy')
2617 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2618 A.addApproval('CRVW', 2)
2619 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2620 self.waitUntilSettled()
2621
2622 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2623 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2624 'debian')
2625 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2626
2627 self.config.set('zuul', 'layout_config',
2628 'tests/fixtures/layout-live-'
2629 'reconfiguration-functions.yaml')
2630 self.sched.reconfigure(self.config)
2631 self.worker.build_history = []
2632
2633 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
2634 B.addApproval('CRVW', 2)
2635 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2636 self.waitUntilSettled()
2637
2638 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2639 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2640 'wheezy')
2641 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2642
James E. Blair287c06d2013-07-24 10:39:30 -07002643 def test_delayed_repo_init(self):
2644 self.config.set('zuul', 'layout_config',
2645 'tests/fixtures/layout-delayed-repo-init.yaml')
2646 self.sched.reconfigure(self.config)
2647
2648 self.init_repo("org/new-project")
2649 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2650
2651 A.addApproval('CRVW', 2)
2652 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2653 self.waitUntilSettled()
2654 self.assertEqual(self.getJobFromHistory('project-merge').result,
2655 'SUCCESS')
2656 self.assertEqual(self.getJobFromHistory('project-test1').result,
2657 'SUCCESS')
2658 self.assertEqual(self.getJobFromHistory('project-test2').result,
2659 'SUCCESS')
2660 self.assertEqual(A.data['status'], 'MERGED')
2661 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002662
2663 def test_timer(self):
2664 "Test that a periodic job is triggered"
2665 self.worker.hold_jobs_in_build = True
2666 self.config.set('zuul', 'layout_config',
2667 'tests/fixtures/layout-timer.yaml')
2668 self.sched.reconfigure(self.config)
2669 self.registerJobs()
2670
2671 start = time.time()
2672 failed = True
2673 while ((time.time() - start) < 30):
2674 if len(self.builds) == 2:
2675 failed = False
2676 break
2677 else:
2678 time.sleep(1)
2679
2680 if failed:
2681 raise Exception("Expected jobs never ran")
2682
2683 self.waitUntilSettled()
2684 port = self.webapp.server.socket.getsockname()[1]
2685
2686 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2687 data = f.read()
2688
2689 self.worker.hold_jobs_in_build = False
2690 self.worker.release()
2691 self.waitUntilSettled()
2692
2693 self.assertEqual(self.getJobFromHistory(
2694 'project-bitrot-stable-old').result, 'SUCCESS')
2695 self.assertEqual(self.getJobFromHistory(
2696 'project-bitrot-stable-older').result, 'SUCCESS')
2697
2698 data = json.loads(data)
2699 status_jobs = set()
2700 for p in data['pipelines']:
2701 for q in p['change_queues']:
2702 for head in q['heads']:
2703 for change in head:
2704 self.assertEqual(change['id'], 'None')
2705 for job in change['jobs']:
2706 status_jobs.add(job['name'])
2707 self.assertIn('project-bitrot-stable-old', status_jobs)
2708 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002709
2710 def test_check_smtp_pool(self):
2711 self.config.set('zuul', 'layout_config',
2712 'tests/fixtures/layout-smtp.yaml')
2713 self.sched.reconfigure(self.config)
2714
2715 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2716 self.waitUntilSettled()
2717
2718 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2719 self.waitUntilSettled()
2720
2721 self.assertEqual(len(FakeSMTP.messages), 2)
2722
2723 # A.messages only holds what FakeGerrit places in it. Thus we
2724 # work on the knowledge of what the first message should be as
2725 # it is only configured to go to SMTP.
2726
2727 self.assertEqual('zuul@example.com',
2728 FakeSMTP.messages[0]['from_email'])
2729 self.assertEqual(['you@example.com'],
2730 FakeSMTP.messages[0]['to_email'])
2731 self.assertEqual('Starting check jobs.',
2732 FakeSMTP.messages[0]['body'])
2733
2734 self.assertEqual('zuul_from@example.com',
2735 FakeSMTP.messages[1]['from_email'])
2736 self.assertEqual(['alternative_me@example.com'],
2737 FakeSMTP.messages[1]['to_email'])
2738 self.assertEqual(A.messages[0],
2739 FakeSMTP.messages[1]['body'])