blob: 6ae714bcfbf5bc35392eb5b5fbed83ae7ba59d14 [file] [log] [blame]
James E. Blairb0fcae42012-07-17 11:12:10 -07001#!/usr/bin/env python
2
3# Copyright 2012 Hewlett-Packard Development Company, L.P.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
James E. Blairb0fcae42012-07-17 11:12:10 -070017import ConfigParser
Monty Taylorbc758832013-06-17 17:22:42 -040018from cStringIO import StringIO
James E. Blair8cc15a82012-08-01 11:17:57 -070019import hashlib
James E. Blairb0fcae42012-07-17 11:12:10 -070020import json
Monty Taylorbc758832013-06-17 17:22:42 -040021import logging
22import os
James E. Blairb0fcae42012-07-17 11:12:10 -070023import pprint
Monty Taylorbc758832013-06-17 17:22:42 -040024import Queue
25import random
James E. Blairb0fcae42012-07-17 11:12:10 -070026import re
James E. Blair412e5582013-04-22 15:50:12 -070027import select
James E. Blair4886cc12012-07-18 15:39:41 -070028import shutil
James E. Blair412e5582013-04-22 15:50:12 -070029import socket
James E. Blair4886f282012-11-15 09:27:33 -080030import string
Monty Taylorbc758832013-06-17 17:22:42 -040031import subprocess
Monty Taylorbc758832013-06-17 17:22:42 -040032import threading
33import time
James E. Blair1843a552013-07-03 14:19:52 -070034import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040035import urllib2
36import urlparse
37
James E. Blair4886cc12012-07-18 15:39:41 -070038import git
James E. Blair1f4c2bb2013-04-26 08:40:46 -070039import gear
Monty Taylorbc758832013-06-17 17:22:42 -040040import fixtures
41import statsd
42import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070043
James E. Blairb0fcae42012-07-17 11:12:10 -070044import zuul.scheduler
James E. Blair1843a552013-07-03 14:19:52 -070045import zuul.webapp
James E. Blair1f4c2bb2013-04-26 08:40:46 -070046import zuul.launcher.gearman
James E. Blairb0fcae42012-07-17 11:12:10 -070047import zuul.trigger.gerrit
James E. Blair63bb0ef2013-07-29 17:14:51 -070048import zuul.trigger.timer
James E. Blairb0fcae42012-07-17 11:12:10 -070049
50FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
51 'fixtures')
52CONFIG = ConfigParser.ConfigParser()
53CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
54
55CONFIG.set('zuul', 'layout_config',
56 os.path.join(FIXTURE_DIR, "layout.yaml"))
57
James E. Blair1f4c2bb2013-04-26 08:40:46 -070058logging.basicConfig(level=logging.DEBUG,
59 format='%(asctime)s %(name)-32s '
60 '%(levelname)-8s %(message)s')
James E. Blairb0fcae42012-07-17 11:12:10 -070061
62
Monty Taylorbc758832013-06-17 17:22:42 -040063def repack_repo(path):
64 output = subprocess.Popen(
65 ['git', '--git-dir=%s/.git' % path, 'repack', '-afd'],
66 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
67 out = output.communicate()
68 if output.returncode:
69 raise Exception("git repack returned %d" % output.returncode)
70 return out
71
72
James E. Blair8cc15a82012-08-01 11:17:57 -070073def random_sha1():
74 return hashlib.sha1(str(random.random())).hexdigest()
75
76
James E. Blair4886cc12012-07-18 15:39:41 -070077class ChangeReference(git.Reference):
78 _common_path_default = "refs/changes"
79 _points_to_commits_only = True
80
81
James E. Blairb0fcae42012-07-17 11:12:10 -070082class FakeChange(object):
James E. Blair8c803f82012-07-31 16:25:42 -070083 categories = {'APRV': ('Approved', -1, 1),
84 'CRVW': ('Code-Review', -2, 2),
85 'VRFY': ('Verified', -2, 2)}
James E. Blairb0fcae42012-07-17 11:12:10 -070086
Monty Taylorbc758832013-06-17 17:22:42 -040087 def __init__(self, gerrit, number, project, branch, subject,
88 status='NEW', upstream_root=None):
James E. Blair8cc15a82012-08-01 11:17:57 -070089 self.gerrit = gerrit
James E. Blaird466dc42012-07-31 10:42:56 -070090 self.reported = 0
James E. Blair8c803f82012-07-31 16:25:42 -070091 self.queried = 0
James E. Blairb0fcae42012-07-17 11:12:10 -070092 self.patchsets = []
James E. Blairb0fcae42012-07-17 11:12:10 -070093 self.number = number
94 self.project = project
95 self.branch = branch
96 self.subject = subject
97 self.latest_patchset = 0
James E. Blair8c803f82012-07-31 16:25:42 -070098 self.depends_on_change = None
99 self.needed_by_changes = []
James E. Blair127bc182012-08-28 15:55:15 -0700100 self.fail_merge = False
James E. Blair42f74822013-05-14 15:18:03 -0700101 self.messages = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700102 self.data = {
103 'branch': branch,
104 'comments': [],
105 'commitMessage': subject,
106 'createdOn': time.time(),
James E. Blair8cc15a82012-08-01 11:17:57 -0700107 'id': 'I' + random_sha1(),
James E. Blairb0fcae42012-07-17 11:12:10 -0700108 'lastUpdated': time.time(),
109 'number': str(number),
110 'open': True,
111 'owner': {'email': 'user@example.com',
112 'name': 'User Name',
113 'username': 'username'},
114 'patchSets': self.patchsets,
115 'project': project,
116 'status': status,
117 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700118 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700119 'url': 'https://hostname/%s' % number}
120
Monty Taylorbc758832013-06-17 17:22:42 -0400121 self.upstream_root = upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700122 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700123 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700124
Monty Taylorbc758832013-06-17 17:22:42 -0400125 def add_fake_change_to_repo(self, msg, fn, large):
126 path = os.path.join(self.upstream_root, self.project)
127 repo = git.Repo(path)
128 ref = ChangeReference.create(repo, '1/%s/%s' % (self.number,
129 self.latest_patchset),
130 'refs/tags/init')
131 repo.head.reference = ref
132 repo.head.reset(index=True, working_tree=True)
133 repo.git.clean('-x', '-f', '-d')
134
135 path = os.path.join(self.upstream_root, self.project)
136 if not large:
137 fn = os.path.join(path, fn)
138 f = open(fn, 'w')
139 f.write("test %s %s %s\n" %
140 (self.branch, self.number, self.latest_patchset))
141 f.close()
142 repo.index.add([fn])
143 else:
144 for fni in range(100):
145 fn = os.path.join(path, str(fni))
146 f = open(fn, 'w')
147 for ci in range(4096):
148 f.write(random.choice(string.printable))
149 f.close()
150 repo.index.add([fn])
151
James E. Blair287c06d2013-07-24 10:39:30 -0700152 r = repo.index.commit(msg)
153 repo.head.reference = 'master'
154 repo.head.reset(index=True, working_tree=True)
155 repo.git.clean('-x', '-f', '-d')
156 return r
Monty Taylorbc758832013-06-17 17:22:42 -0400157
James E. Blair70c71582013-03-06 08:50:50 -0800158 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700159 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700160 if files:
161 fn = files[0]
162 else:
163 fn = '%s-%s' % (self.branch, self.number)
164 msg = self.subject + '-' + str(self.latest_patchset)
Monty Taylorbc758832013-06-17 17:22:42 -0400165 c = self.add_fake_change_to_repo(msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800166 ps_files = [{'file': '/COMMIT_MSG',
167 'type': 'ADDED'},
168 {'file': 'README',
169 'type': 'MODIFIED'}]
170 for f in files:
171 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700172 d = {'approvals': [],
173 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800174 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700175 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700176 'ref': 'refs/changes/1/%s/%s' % (self.number,
177 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700178 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700179 'uploader': {'email': 'user@example.com',
180 'name': 'User name',
181 'username': 'user'}}
182 self.data['currentPatchSet'] = d
183 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700184 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700185
James E. Blaire0487072012-08-29 17:38:31 -0700186 def getPatchsetCreatedEvent(self, patchset):
187 event = {"type": "patchset-created",
188 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800189 "branch": self.branch,
190 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
191 "number": str(self.number),
192 "subject": self.subject,
193 "owner": {"name": "User Name"},
194 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700195 "patchSet": self.patchsets[patchset - 1],
196 "uploader": {"name": "User Name"}}
197 return event
198
James E. Blair42f74822013-05-14 15:18:03 -0700199 def getChangeRestoredEvent(self):
200 event = {"type": "change-restored",
201 "change": {"project": self.project,
202 "branch": self.branch,
203 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
204 "number": str(self.number),
205 "subject": self.subject,
206 "owner": {"name": "User Name"},
207 "url": "https://hostname/3"},
208 "restorer": {"name": "User Name"},
209 "reason": ""}
210 return event
211
James E. Blairb0fcae42012-07-17 11:12:10 -0700212 def addApproval(self, category, value):
James E. Blair8c803f82012-07-31 16:25:42 -0700213 approval = {'description': self.categories[category][0],
214 'type': category,
215 'value': str(value)}
216 self.patchsets[-1]['approvals'].append(approval)
217 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700218 'author': {'email': 'user@example.com',
219 'name': 'User Name',
220 'username': 'username'},
221 'change': {'branch': self.branch,
222 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
223 'number': str(self.number),
224 'owner': {'email': 'user@example.com',
225 'name': 'User Name',
226 'username': 'username'},
227 'project': self.project,
228 'subject': self.subject,
229 'topic': 'master',
230 'url': 'https://hostname/459'},
231 'comment': '',
232 'patchSet': self.patchsets[-1],
233 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700234 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700235 return json.loads(json.dumps(event))
236
James E. Blair8c803f82012-07-31 16:25:42 -0700237 def getSubmitRecords(self):
238 status = {}
239 for cat in self.categories.keys():
240 status[cat] = 0
241
242 for a in self.patchsets[-1]['approvals']:
243 cur = status[a['type']]
244 cat_min, cat_max = self.categories[a['type']][1:]
245 new = int(a['value'])
246 if new == cat_min:
247 cur = new
248 elif abs(new) > abs(cur):
249 cur = new
250 status[a['type']] = cur
251
252 labels = []
253 ok = True
254 for typ, cat in self.categories.items():
255 cur = status[typ]
256 cat_min, cat_max = cat[1:]
257 if cur == cat_min:
258 value = 'REJECT'
259 ok = False
260 elif cur == cat_max:
261 value = 'OK'
262 else:
263 value = 'NEED'
264 ok = False
265 labels.append({'label': cat[0], 'status': value})
266 if ok:
267 return [{'status': 'OK'}]
268 return [{'status': 'NOT_READY',
269 'labels': labels}]
270
271 def setDependsOn(self, other, patchset):
272 self.depends_on_change = other
273 d = {'id': other.data['id'],
274 'number': other.data['number'],
275 'ref': other.patchsets[patchset - 1]['ref']
276 }
277 self.data['dependsOn'] = [d]
278
279 other.needed_by_changes.append(self)
280 needed = other.data.get('neededBy', [])
281 d = {'id': self.data['id'],
282 'number': self.data['number'],
283 'ref': self.patchsets[patchset - 1]['ref'],
284 'revision': self.patchsets[patchset - 1]['revision']
285 }
286 needed.append(d)
287 other.data['neededBy'] = needed
288
James E. Blairb0fcae42012-07-17 11:12:10 -0700289 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700290 self.queried += 1
291 d = self.data.get('dependsOn')
292 if d:
293 d = d[0]
294 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
295 d['isCurrentPatchSet'] = True
296 else:
297 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700298 return json.loads(json.dumps(self.data))
299
300 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800301 if (self.depends_on_change and
302 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700303 return
James E. Blair127bc182012-08-28 15:55:15 -0700304 if self.fail_merge:
305 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700306 self.data['status'] = 'MERGED'
307 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700308
Monty Taylorbc758832013-06-17 17:22:42 -0400309 path = os.path.join(self.upstream_root, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700310 repo = git.Repo(path)
311 repo.heads[self.branch].commit = \
312 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700313
James E. Blaird466dc42012-07-31 10:42:56 -0700314 def setReported(self):
315 self.reported += 1
316
James E. Blairb0fcae42012-07-17 11:12:10 -0700317
318class FakeGerrit(object):
319 def __init__(self, *args, **kw):
320 self.event_queue = Queue.Queue()
321 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
322 self.change_number = 0
323 self.changes = {}
324
325 def addFakeChange(self, project, branch, subject):
326 self.change_number += 1
Monty Taylorbc758832013-06-17 17:22:42 -0400327 c = FakeChange(self, self.change_number, project, branch, subject,
328 upstream_root=self.upstream_root)
James E. Blairb0fcae42012-07-17 11:12:10 -0700329 self.changes[self.change_number] = c
330 return c
331
332 def addEvent(self, data):
333 return self.event_queue.put(data)
334
335 def getEvent(self):
336 return self.event_queue.get()
337
338 def eventDone(self):
339 self.event_queue.task_done()
340
341 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700342 number, ps = changeid.split(',')
343 change = self.changes[int(number)]
James E. Blair42f74822013-05-14 15:18:03 -0700344 change.messages.append(message)
James E. Blairb0fcae42012-07-17 11:12:10 -0700345 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700346 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700347 if message:
348 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700349
350 def query(self, number):
351 change = self.changes[int(number)]
352 return change.query()
353
354 def startWatching(self, *args, **kw):
355 pass
356
357
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700358class BuildHistory(object):
359 def __init__(self, **kw):
360 self.__dict__.update(kw)
James E. Blairb0fcae42012-07-17 11:12:10 -0700361
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700362 def __repr__(self):
363 return ("<Completed build, result: %s name: %s #%s changes: %s>" %
364 (self.result, self.name, self.number, self.changes))
James E. Blairb0fcae42012-07-17 11:12:10 -0700365
366
James E. Blair8cc15a82012-08-01 11:17:57 -0700367class FakeURLOpener(object):
Monty Taylorbc758832013-06-17 17:22:42 -0400368 def __init__(self, upstream_root, fake_gerrit, url):
369 self.upstream_root = upstream_root
James E. Blair8cc15a82012-08-01 11:17:57 -0700370 self.fake_gerrit = fake_gerrit
371 self.url = url
372
373 def read(self):
374 res = urlparse.urlparse(self.url)
375 path = res.path
376 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200377 ret = '001e# service=git-upload-pack\n'
378 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
379 'multi_ack thin-pack side-band side-band-64k ofs-delta '
380 'shallow no-progress include-tag multi_ack_detailed no-done\n')
Monty Taylorbc758832013-06-17 17:22:42 -0400381 path = os.path.join(self.upstream_root, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700382 repo = git.Repo(path)
383 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200384 r = ref.object.hexsha + ' ' + ref.path + '\n'
385 ret += '%04x%s' % (len(r) + 4, r)
386 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700387 return ret
388
389
James E. Blair4886cc12012-07-18 15:39:41 -0700390class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
Monty Taylorbc758832013-06-17 17:22:42 -0400391 def __init__(self, upstream_root, *args):
392 super(FakeGerritTrigger, self).__init__(*args)
393 self.upstream_root = upstream_root
394
James E. Blair4886cc12012-07-18 15:39:41 -0700395 def getGitUrl(self, project):
Monty Taylorbc758832013-06-17 17:22:42 -0400396 return os.path.join(self.upstream_root, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700397
398
James E. Blair412e5582013-04-22 15:50:12 -0700399class FakeStatsd(threading.Thread):
400 def __init__(self):
401 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400402 self.daemon = True
James E. Blair412e5582013-04-22 15:50:12 -0700403 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
404 self.sock.bind(('', 0))
405 self.port = self.sock.getsockname()[1]
406 self.wake_read, self.wake_write = os.pipe()
407 self.stats = []
408
409 def run(self):
410 while True:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700411 poll = select.poll()
412 poll.register(self.sock, select.POLLIN)
413 poll.register(self.wake_read, select.POLLIN)
414 ret = poll.poll()
415 for (fd, event) in ret:
416 if fd == self.sock.fileno():
James E. Blair412e5582013-04-22 15:50:12 -0700417 data = self.sock.recvfrom(1024)
418 if not data:
419 return
420 self.stats.append(data[0])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700421 if fd == self.wake_read:
James E. Blair412e5582013-04-22 15:50:12 -0700422 return
423
424 def stop(self):
425 os.write(self.wake_write, '1\n')
426
427
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700428class FakeBuild(threading.Thread):
429 log = logging.getLogger("zuul.test")
430
431 def __init__(self, worker, job, number, node):
432 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400433 self.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700434 self.worker = worker
435 self.job = job
436 self.name = job.name.split(':')[1]
437 self.number = number
438 self.node = node
439 self.parameters = json.loads(job.arguments)
440 self.unique = self.parameters['ZUUL_UUID']
441 self.wait_condition = threading.Condition()
442 self.waiting = False
443 self.aborted = False
444 self.created = time.time()
445 self.description = ''
446
447 def release(self):
448 self.wait_condition.acquire()
449 self.wait_condition.notify()
450 self.waiting = False
451 self.log.debug("Build %s released" % self.unique)
452 self.wait_condition.release()
453
454 def isWaiting(self):
455 self.wait_condition.acquire()
456 if self.waiting:
457 ret = True
458 else:
459 ret = False
460 self.wait_condition.release()
461 return ret
462
463 def _wait(self):
464 self.wait_condition.acquire()
465 self.waiting = True
466 self.log.debug("Build %s waiting" % self.unique)
467 self.wait_condition.wait()
468 self.wait_condition.release()
469
470 def run(self):
471 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700472 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
473 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700474 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700475 'manager': self.worker.worker_id,
Paul Belangerec49b4c2013-07-20 20:32:20 -0400476 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700477
478 self.job.sendWorkData(json.dumps(data))
479 self.job.sendWorkStatus(0, 100)
480
481 if self.worker.hold_jobs_in_build:
482 self._wait()
483 self.log.debug("Build %s continuing" % self.unique)
484
485 self.worker.lock.acquire()
486
487 result = 'SUCCESS'
488 if (('ZUUL_REF' in self.parameters) and
489 self.worker.shouldFailTest(self.name,
490 self.parameters['ZUUL_REF'])):
491 result = 'FAILURE'
492 if self.aborted:
493 result = 'ABORTED'
494
495 data = {'result': result}
496 changes = None
497 if 'ZUUL_CHANGE_IDS' in self.parameters:
498 changes = self.parameters['ZUUL_CHANGE_IDS']
499
500 self.worker.build_history.append(
501 BuildHistory(name=self.name, number=self.number,
502 result=result, changes=changes, node=self.node,
James E. Blair64ed6f22013-07-10 14:07:23 -0700503 uuid=self.unique, description=self.description,
504 pipeline=self.parameters['ZUUL_PIPELINE'])
Paul Belangerec49b4c2013-07-20 20:32:20 -0400505 )
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700506
507 self.job.sendWorkComplete(json.dumps(data))
508 del self.worker.gearman_jobs[self.job.unique]
509 self.worker.running_builds.remove(self)
510 self.worker.lock.release()
511
512
513class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400514 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700515 super(FakeWorker, self).__init__(worker_id)
516 self.gearman_jobs = {}
517 self.build_history = []
518 self.running_builds = []
519 self.build_counter = 0
520 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400521 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700522
523 self.hold_jobs_in_build = False
524 self.lock = threading.Lock()
525 self.__work_thread = threading.Thread(target=self.work)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400526 self.__work_thread.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700527 self.__work_thread.start()
528
529 def handleJob(self, job):
530 parts = job.name.split(":")
531 cmd = parts[0]
532 name = parts[1]
533 if len(parts) > 2:
534 node = parts[2]
535 else:
536 node = None
537 if cmd == 'build':
538 self.handleBuild(job, name, node)
539 elif cmd == 'stop':
540 self.handleStop(job, name)
541 elif cmd == 'set_description':
542 self.handleSetDescription(job, name)
543
544 def handleBuild(self, job, name, node):
545 build = FakeBuild(self, job, self.build_counter, node)
546 job.build = build
547 self.gearman_jobs[job.unique] = job
548 self.build_counter += 1
549
550 self.running_builds.append(build)
551 build.start()
552
553 def handleStop(self, job, name):
554 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700555 parameters = json.loads(job.arguments)
556 name = parameters['name']
557 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700558 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700559 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700560 build.aborted = True
561 build.release()
562 job.sendWorkComplete()
563 return
564 job.sendWorkFail()
565
566 def handleSetDescription(self, job, name):
567 self.log.debug("handle set description")
568 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700569 name = parameters['name']
570 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700571 descr = parameters['html_description']
572 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700573 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700574 build.description = descr
575 job.sendWorkComplete()
576 return
577 for build in self.build_history:
James E. Blair3c483cf2013-06-04 16:30:43 -0700578 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700579 build.description = descr
580 job.sendWorkComplete()
581 return
582 job.sendWorkFail()
583
584 def work(self):
585 while self.running:
586 try:
587 job = self.getJob()
588 except gear.InterruptedError:
589 continue
590 try:
591 self.handleJob(job)
592 except:
593 self.log.exception("Worker exception:")
594
595 def addFailTest(self, name, change):
596 l = self.fail_tests.get(name, [])
597 l.append(change)
598 self.fail_tests[name] = l
599
600 def shouldFailTest(self, name, ref):
601 l = self.fail_tests.get(name, [])
602 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400603 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700604 return True
605 return False
606
607 def release(self, regex=None):
608 builds = self.running_builds[:]
609 self.log.debug("releasing build %s (%s)" % (regex,
James E. Blair78e31b32013-07-09 09:11:34 -0700610 len(self.running_builds)))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700611 for build in builds:
612 if not regex or re.match(regex, build.name):
613 self.log.debug("releasing build %s" %
614 (build.parameters['ZUUL_UUID']))
615 build.release()
616 else:
617 self.log.debug("not releasing build %s" %
618 (build.parameters['ZUUL_UUID']))
619 self.log.debug("done releasing builds %s (%s)" %
620 (regex, len(self.running_builds)))
621
622
623class FakeGearmanServer(gear.Server):
624 def __init__(self):
625 self.hold_jobs_in_queue = False
626 super(FakeGearmanServer, self).__init__(0)
627
628 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700629 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
630 for job in queue:
631 if not hasattr(job, 'waiting'):
632 if job.name.startswith('build:'):
633 job.waiting = self.hold_jobs_in_queue
634 else:
635 job.waiting = False
636 if job.waiting:
637 continue
638 if job.name in connection.functions:
639 if not peek:
640 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400641 connection.related_jobs[job.handle] = job
642 job.worker_connection = connection
643 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700644 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700645 return None
646
647 def release(self, regex=None):
648 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700649 qlen = (len(self.high_queue) + len(self.normal_queue) +
650 len(self.low_queue))
651 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
James E. Blairdda6c912013-07-29 14:12:12 -0700652 for job in self.getQueue():
653 cmd, name = job.name.split(':')
654 if cmd != 'build':
655 continue
656 if not regex or re.match(regex, name):
657 self.log.debug("releasing queued job %s" %
658 job.unique)
659 job.waiting = False
660 released = True
661 else:
662 self.log.debug("not releasing queued job %s" %
663 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700664 if released:
665 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700666 qlen = (len(self.high_queue) + len(self.normal_queue) +
667 len(self.low_queue))
668 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700669
670
Monty Taylorbc758832013-06-17 17:22:42 -0400671class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700672 log = logging.getLogger("zuul.test")
673
674 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400675 super(TestScheduler, self).setUp()
676 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
677 try:
678 test_timeout = int(test_timeout)
679 except ValueError:
680 # If timeout value is invalid do not set a timeout.
681 test_timeout = 0
682 if test_timeout > 0:
683 self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
684
685 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
686 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
687 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
688 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
689 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
690 os.environ.get('OS_STDERR_CAPTURE') == '1'):
691 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
692 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Monty Taylore6a6c402013-07-02 09:25:55 -0700693 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
694 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400695 self.useFixture(fixtures.FakeLogger(
696 level=logging.DEBUG,
697 format='%(asctime)s %(name)-32s '
698 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700699 tmp_root = self.useFixture(fixtures.TempDir(
700 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400701 self.test_root = os.path.join(tmp_root, "zuul-test")
702 self.upstream_root = os.path.join(self.test_root, "upstream")
703 self.git_root = os.path.join(self.test_root, "git")
704
705 CONFIG.set('zuul', 'git_dir', self.git_root)
706 if os.path.exists(self.test_root):
707 shutil.rmtree(self.test_root)
708 os.makedirs(self.test_root)
709 os.makedirs(self.upstream_root)
710 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700711
712 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400713 self.init_repo("org/project")
714 self.init_repo("org/project1")
715 self.init_repo("org/project2")
716 self.init_repo("org/project3")
717 self.init_repo("org/one-job-project")
718 self.init_repo("org/nonvoting-project")
719 self.init_repo("org/templated-project")
720 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700721 self.init_repo("org/conflict-project")
James E. Blair412e5582013-04-22 15:50:12 -0700722
723 self.statsd = FakeStatsd()
724 os.environ['STATSD_HOST'] = 'localhost'
725 os.environ['STATSD_PORT'] = str(self.statsd.port)
726 self.statsd.start()
727 # the statsd client object is configured in the statsd module import
728 reload(statsd)
729 reload(zuul.scheduler)
730
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700731 self.gearman_server = FakeGearmanServer()
732
733 self.config = ConfigParser.ConfigParser()
734 cfg = StringIO()
735 CONFIG.write(cfg)
736 cfg.seek(0)
737 self.config.readfp(cfg)
738 self.config.set('gearman', 'port', str(self.gearman_server.port))
739
Monty Taylorbc758832013-06-17 17:22:42 -0400740 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700741 self.worker.addServer('127.0.0.1', self.gearman_server.port)
742 self.gearman_server.worker = self.worker
743
James E. Blairb0fcae42012-07-17 11:12:10 -0700744 self.sched = zuul.scheduler.Scheduler()
745
James E. Blair8cc15a82012-08-01 11:17:57 -0700746 def URLOpenerFactory(*args, **kw):
747 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400748 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700749
James E. Blair8cc15a82012-08-01 11:17:57 -0700750 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700751 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700752
753 zuul.lib.gerrit.Gerrit = FakeGerrit
754
Monty Taylorbc758832013-06-17 17:22:42 -0400755 self.gerrit = FakeGerritTrigger(
756 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700757 self.gerrit.replication_timeout = 1.5
758 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700759 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400760 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700761
James E. Blair1843a552013-07-03 14:19:52 -0700762 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
763
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700764 self.sched.setLauncher(self.launcher)
James E. Blair6c358e72013-07-29 17:06:47 -0700765 self.sched.registerTrigger(self.gerrit)
James E. Blair63bb0ef2013-07-29 17:14:51 -0700766 self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
767 self.sched.registerTrigger(self.timer)
James E. Blairb0fcae42012-07-17 11:12:10 -0700768
769 self.sched.start()
770 self.sched.reconfigure(self.config)
771 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700772 self.webapp.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700773 self.launcher.gearman.waitForServer()
774 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400775 self.builds = self.worker.running_builds
776 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700777
James E. Blairfee8d652013-06-07 08:57:52 -0700778 self.addCleanup(self.assertFinalState)
779 self.addCleanup(self.shutdown)
780
781 def assertFinalState(self):
782 # Make sure that the change cache is cleared
James E. Blair6c358e72013-07-29 17:06:47 -0700783 self.assertEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700784 self.assertEmptyQueues()
785
786 def shutdown(self):
787 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700788 self.launcher.stop()
789 self.worker.shutdown()
790 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700791 self.gerrit.stop()
James E. Blair63bb0ef2013-07-29 17:14:51 -0700792 self.timer.stop()
James E. Blairb0fcae42012-07-17 11:12:10 -0700793 self.sched.stop()
794 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700795 self.statsd.stop()
796 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700797 self.webapp.stop()
798 self.webapp.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700799 threads = threading.enumerate()
800 if len(threads) > 1:
801 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400802 super(TestScheduler, self).tearDown()
803
804 def init_repo(self, project):
805 parts = project.split('/')
806 path = os.path.join(self.upstream_root, *parts[:-1])
807 if not os.path.exists(path):
808 os.makedirs(path)
809 path = os.path.join(self.upstream_root, project)
810 repo = git.Repo.init(path)
811
812 repo.config_writer().set_value('user', 'email', 'user@example.com')
813 repo.config_writer().set_value('user', 'name', 'User Name')
814 repo.config_writer().write()
815
816 fn = os.path.join(path, 'README')
817 f = open(fn, 'w')
818 f.write("test\n")
819 f.close()
820 repo.index.add([fn])
821 repo.index.commit('initial commit')
822 master = repo.create_head('master')
823 repo.create_tag('init')
824
825 mp = repo.create_head('mp')
826 repo.head.reference = mp
827 f = open(fn, 'a')
828 f.write("test mp\n")
829 f.close()
830 repo.index.add([fn])
831 repo.index.commit('mp commit')
832
833 repo.head.reference = master
834 repo.head.reset(index=True, working_tree=True)
835 repo.git.clean('-x', '-f', '-d')
836
837 def ref_has_change(self, ref, change):
838 path = os.path.join(self.git_root, change.project)
839 repo = git.Repo(path)
840 for commit in repo.iter_commits(ref):
841 if commit.message.strip() == ('%s-1' % change.subject):
842 return True
843 return False
844
845 def job_has_changes(self, *args):
846 job = args[0]
847 commits = args[1:]
848 if isinstance(job, FakeBuild):
849 parameters = job.parameters
850 else:
851 parameters = json.loads(job.arguments)
852 project = parameters['ZUUL_PROJECT']
853 path = os.path.join(self.git_root, project)
854 repo = git.Repo(path)
855 ref = parameters['ZUUL_REF']
856 sha = parameters['ZUUL_COMMIT']
857 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
858 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
859 commit_messages = ['%s-1' % commit.subject for commit in commits]
860 for msg in commit_messages:
861 if msg not in repo_messages:
862 return False
863 if repo_shas[0] != sha:
864 return False
865 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700866
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700867 def registerJobs(self):
868 count = 0
James E. Blaireff88162013-07-01 12:44:14 -0400869 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700870 self.worker.registerFunction('build:' + job)
871 count += 1
872 self.worker.registerFunction('stop:' + self.worker.worker_id)
873 count += 1
874
875 while len(self.gearman_server.functions) < count:
876 time.sleep(0)
877
878 def release(self, job):
879 if isinstance(job, FakeBuild):
880 job.release()
881 else:
882 job.waiting = False
883 self.log.debug("Queued job %s released" % job.unique)
884 self.gearman_server.wakeConnections()
885
886 def getParameter(self, job, name):
887 if isinstance(job, FakeBuild):
888 return job.parameters[name]
889 else:
890 parameters = json.loads(job.arguments)
891 return parameters[name]
892
893 def resetGearmanServer(self):
894 self.worker.setFunctions([])
895 while True:
896 done = True
897 for connection in self.gearman_server.active_connections:
898 if connection.functions:
899 done = False
900 if done:
901 break
902 time.sleep(0)
903 self.gearman_server.functions = set()
904
905 def haveAllBuildsReported(self):
906 # See if Zuul is waiting on a meta job to complete
907 if self.launcher.meta_jobs:
908 return False
909 # Find out if every build that the worker has completed has been
910 # reported back to Zuul. If it hasn't then that means a Gearman
911 # event is still in transit and the system is not stable.
912 for build in self.worker.build_history:
913 zbuild = self.launcher.builds.get(build.uuid)
914 if not zbuild:
915 # It has already been reported
916 continue
917 # It hasn't been reported yet.
918 return False
919 # Make sure that none of the worker connections are in GRAB_WAIT
920 for connection in self.worker.active_connections:
921 if connection.state == 'GRAB_WAIT':
922 return False
923 return True
924
925 def areAllBuildsWaiting(self):
926 ret = True
927
928 builds = self.launcher.builds.values()
929 for build in builds:
930 client_job = None
931 for conn in self.launcher.gearman.active_connections:
932 for j in conn.related_jobs.values():
933 if j.unique == build.uuid:
934 client_job = j
935 break
936 if not client_job:
937 self.log.debug("%s is not known to the gearman client" %
938 build)
939 ret = False
940 continue
941 if not client_job.handle:
942 self.log.debug("%s has no handle" % client_job)
943 ret = False
944 continue
945 server_job = self.gearman_server.jobs.get(client_job.handle)
946 if not server_job:
947 self.log.debug("%s is not known to the gearman server" %
948 client_job)
949 ret = False
950 continue
951 if not hasattr(server_job, 'waiting'):
952 self.log.debug("%s is being enqueued" % server_job)
953 ret = False
954 continue
955 if server_job.waiting:
956 continue
957 worker_job = self.worker.gearman_jobs.get(server_job.unique)
958 if worker_job:
959 if worker_job.build.isWaiting():
960 continue
961 else:
962 self.log.debug("%s is running" % worker_job)
963 ret = False
964 else:
965 self.log.debug("%s is unassigned" % server_job)
966 ret = False
967 return ret
968
James E. Blairb0fcae42012-07-17 11:12:10 -0700969 def waitUntilSettled(self):
970 self.log.debug("Waiting until settled...")
971 start = time.time()
972 while True:
973 if time.time() - start > 10:
974 print 'queue status:',
975 print self.sched.trigger_event_queue.empty(),
976 print self.sched.result_event_queue.empty(),
977 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700978 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -0700979 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700980 # Make sure no new events show up while we're checking
981 self.worker.lock.acquire()
982 # have all build states propogated to zuul?
983 if self.haveAllBuildsReported():
984 # Join ensures that the queue is empty _and_ events have been
985 # processed
986 self.fake_gerrit.event_queue.join()
987 self.sched.trigger_event_queue.join()
988 self.sched.result_event_queue.join()
989 if (self.sched.trigger_event_queue.empty() and
990 self.sched.result_event_queue.empty() and
991 self.fake_gerrit.event_queue.empty() and
992 self.areAllBuildsWaiting()):
993 self.worker.lock.release()
994 self.log.debug("...settled.")
995 return
996 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700997 self.sched.wake_event.wait(0.1)
998
James E. Blaird466dc42012-07-31 10:42:56 -0700999 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -08001000 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -07001001 return len(jobs)
1002
James E. Blair4ca985f2013-05-30 12:27:43 -07001003 def getJobFromHistory(self, name):
1004 history = self.worker.build_history
1005 for job in history:
1006 if job.name == name:
1007 return job
1008 raise Exception("Unable to find job %s in history" % name)
1009
James E. Blaire0487072012-08-29 17:38:31 -07001010 def assertEmptyQueues(self):
1011 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001012 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001013 for queue in pipeline.queues:
1014 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001015 print 'pipeline %s queue %s contents %s' % (
1016 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001017 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001018 if len(queue.severed_heads) != 0:
1019 print 'heads', queue.severed_heads
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001020 self.assertEqual(len(queue.severed_heads), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001021
James E. Blair66eeebf2013-07-27 17:44:32 -07001022 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001023 start = time.time()
1024 while time.time() < (start + 5):
1025 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001026 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001027 k, v = stat.split(':')
1028 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001029 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001030 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001031 elif value:
1032 if value == v:
1033 return
1034 elif kind:
1035 if v.endswith('|' + kind):
1036 return
James E. Blair412e5582013-04-22 15:50:12 -07001037 time.sleep(0.1)
1038
1039 pprint.pprint(self.statsd.stats)
1040 raise Exception("Key %s not found in reported stats" % key)
1041
James E. Blairb0fcae42012-07-17 11:12:10 -07001042 def test_jobs_launched(self):
1043 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001044
James E. Blairb0fcae42012-07-17 11:12:10 -07001045 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001046 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001047 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1048 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001049 self.assertEqual(self.getJobFromHistory('project-merge').result,
1050 'SUCCESS')
1051 self.assertEqual(self.getJobFromHistory('project-test1').result,
1052 'SUCCESS')
1053 self.assertEqual(self.getJobFromHistory('project-test2').result,
1054 'SUCCESS')
1055 self.assertEqual(A.data['status'], 'MERGED')
1056 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001057
James E. Blair66eeebf2013-07-27 17:44:32 -07001058 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1059 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1060 value='1|g')
1061 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1062 kind='ms')
1063 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1064 value='1|c')
1065 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1066 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1067 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001068 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001069 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001070 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001071 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001072
James E. Blair42f74822013-05-14 15:18:03 -07001073 def test_duplicate_pipelines(self):
1074 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001075
James E. Blair42f74822013-05-14 15:18:03 -07001076 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1077 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1078 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001079
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001080 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001081 self.history[0].name == 'project-test1'
1082 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001083
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001084 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001085 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001086 self.assertIn('dup1/project-test1', A.messages[0])
1087 self.assertNotIn('dup2/project-test1', A.messages[0])
1088 self.assertNotIn('dup1/project-test1', A.messages[1])
1089 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001090 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001091 self.assertIn('dup1/project-test1', A.messages[1])
1092 self.assertNotIn('dup2/project-test1', A.messages[1])
1093 self.assertNotIn('dup1/project-test1', A.messages[0])
1094 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001095
James E. Blairb0fcae42012-07-17 11:12:10 -07001096 def test_parallel_changes(self):
1097 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001098
1099 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001100 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1101 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1102 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001103 A.addApproval('CRVW', 2)
1104 B.addApproval('CRVW', 2)
1105 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001106
1107 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1108 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1109 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1110
1111 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001112 self.assertEqual(len(self.builds), 1)
1113 self.assertEqual(self.builds[0].name, 'project-merge')
1114 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001115
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001116 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001117 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001118 self.assertEqual(len(self.builds), 3)
1119 self.assertEqual(self.builds[0].name, 'project-test1')
1120 self.assertTrue(self.job_has_changes(self.builds[0], A))
1121 self.assertEqual(self.builds[1].name, 'project-test2')
1122 self.assertTrue(self.job_has_changes(self.builds[1], A))
1123 self.assertEqual(self.builds[2].name, 'project-merge')
1124 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001125
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001126 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001127 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001128 self.assertEqual(len(self.builds), 5)
1129 self.assertEqual(self.builds[0].name, 'project-test1')
1130 self.assertTrue(self.job_has_changes(self.builds[0], A))
1131 self.assertEqual(self.builds[1].name, 'project-test2')
1132 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001133
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001134 self.assertEqual(self.builds[2].name, 'project-test1')
1135 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1136 self.assertEqual(self.builds[3].name, 'project-test2')
1137 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001138
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001139 self.assertEqual(self.builds[4].name, 'project-merge')
1140 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001141
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001142 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001143 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001144 self.assertEqual(len(self.builds), 6)
1145 self.assertEqual(self.builds[0].name, 'project-test1')
1146 self.assertTrue(self.job_has_changes(self.builds[0], A))
1147 self.assertEqual(self.builds[1].name, 'project-test2')
1148 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001149
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001150 self.assertEqual(self.builds[2].name, 'project-test1')
1151 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1152 self.assertEqual(self.builds[3].name, 'project-test2')
1153 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001154
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001155 self.assertEqual(self.builds[4].name, 'project-test1')
1156 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1157 self.assertEqual(self.builds[5].name, 'project-test2')
1158 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001159
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001160 self.worker.hold_jobs_in_build = False
1161 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001162 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001163 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001164
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001165 self.assertEqual(len(self.history), 9)
1166 self.assertEqual(A.data['status'], 'MERGED')
1167 self.assertEqual(B.data['status'], 'MERGED')
1168 self.assertEqual(C.data['status'], 'MERGED')
1169 self.assertEqual(A.reported, 2)
1170 self.assertEqual(B.reported, 2)
1171 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001172
1173 def test_failed_changes(self):
1174 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001175 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001176
James E. Blairb02a3bb2012-07-30 17:49:55 -07001177 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1178 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001179 A.addApproval('CRVW', 2)
1180 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001181
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001182 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001183
James E. Blaire2819012013-06-28 17:17:26 -04001184 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1185 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001186 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001187
1188 self.worker.release('.*-merge')
1189 self.waitUntilSettled()
1190
1191 self.worker.hold_jobs_in_build = False
1192 self.worker.release()
1193
1194 self.waitUntilSettled()
1195 # It's certain that the merge job for change 2 will run, but
1196 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001197 self.assertTrue(len(self.history) > 6)
1198 self.assertEqual(A.data['status'], 'NEW')
1199 self.assertEqual(B.data['status'], 'MERGED')
1200 self.assertEqual(A.reported, 2)
1201 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001202
1203 def test_independent_queues(self):
1204 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001205
1206 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001207 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001208 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1209 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001210 A.addApproval('CRVW', 2)
1211 B.addApproval('CRVW', 2)
1212 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001213
1214 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1215 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1216 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1217
James E. Blairb02a3bb2012-07-30 17:49:55 -07001218 self.waitUntilSettled()
1219
1220 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001221 self.assertEqual(len(self.builds), 2)
1222 self.assertEqual(self.builds[0].name, 'project-merge')
1223 self.assertTrue(self.job_has_changes(self.builds[0], A))
1224 self.assertEqual(self.builds[1].name, 'project1-merge')
1225 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001226
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001227 # Release the current merge builds
1228 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001229 self.waitUntilSettled()
1230 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001231 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001232 self.waitUntilSettled()
1233
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001234 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001235 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001236 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001237
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001238 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001239 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001240 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001241
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001242 self.assertEqual(len(self.history), 11)
1243 self.assertEqual(A.data['status'], 'MERGED')
1244 self.assertEqual(B.data['status'], 'MERGED')
1245 self.assertEqual(C.data['status'], 'MERGED')
1246 self.assertEqual(A.reported, 2)
1247 self.assertEqual(B.reported, 2)
1248 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001249
1250 def test_failed_change_at_head(self):
1251 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001252
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001253 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001254 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1255 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1256 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001257 A.addApproval('CRVW', 2)
1258 B.addApproval('CRVW', 2)
1259 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001260
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001261 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001262
1263 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1264 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1265 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1266
1267 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001268
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001269 self.assertEqual(len(self.builds), 1)
1270 self.assertEqual(self.builds[0].name, 'project-merge')
1271 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001272
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001273 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001274 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001275 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001276 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001277 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001278 self.waitUntilSettled()
1279
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001280 self.assertEqual(len(self.builds), 6)
1281 self.assertEqual(self.builds[0].name, 'project-test1')
1282 self.assertEqual(self.builds[1].name, 'project-test2')
1283 self.assertEqual(self.builds[2].name, 'project-test1')
1284 self.assertEqual(self.builds[3].name, 'project-test2')
1285 self.assertEqual(self.builds[4].name, 'project-test1')
1286 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001287
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001288 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001289 self.waitUntilSettled()
1290
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001291 # project-test2, project-merge for B
1292 self.assertEqual(len(self.builds), 2)
1293 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001294
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001295 self.worker.hold_jobs_in_build = False
1296 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001297 self.waitUntilSettled()
1298
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001299 self.assertEqual(len(self.builds), 0)
1300 self.assertEqual(len(self.history), 15)
1301 self.assertEqual(A.data['status'], 'NEW')
1302 self.assertEqual(B.data['status'], 'MERGED')
1303 self.assertEqual(C.data['status'], 'MERGED')
1304 self.assertEqual(A.reported, 2)
1305 self.assertEqual(B.reported, 2)
1306 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001307
James E. Blair0aac4872013-08-23 14:02:38 -07001308 def test_failed_change_in_middle(self):
1309 "Test a failed change in the middle of the queue"
1310
1311 self.worker.hold_jobs_in_build = True
1312 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1313 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1314 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1315 A.addApproval('CRVW', 2)
1316 B.addApproval('CRVW', 2)
1317 C.addApproval('CRVW', 2)
1318
1319 self.worker.addFailTest('project-test1', B)
1320
1321 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1322 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1323 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1324
1325 self.waitUntilSettled()
1326
1327 self.worker.release('.*-merge')
1328 self.waitUntilSettled()
1329 self.worker.release('.*-merge')
1330 self.waitUntilSettled()
1331 self.worker.release('.*-merge')
1332 self.waitUntilSettled()
1333
1334 self.assertEqual(len(self.builds), 6)
1335 self.assertEqual(self.builds[0].name, 'project-test1')
1336 self.assertEqual(self.builds[1].name, 'project-test2')
1337 self.assertEqual(self.builds[2].name, 'project-test1')
1338 self.assertEqual(self.builds[3].name, 'project-test2')
1339 self.assertEqual(self.builds[4].name, 'project-test1')
1340 self.assertEqual(self.builds[5].name, 'project-test2')
1341
1342 self.release(self.builds[2])
1343 self.waitUntilSettled()
1344
1345 # project-test1 and project-test2 for A, project-test2 for B
1346 self.assertEqual(len(self.builds), 3)
1347 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1348
1349 # check that build status of aborted jobs are masked ('CANCELED')
1350 items = self.sched.layout.pipelines['gate'].getAllItems()
1351 builds = items[0].current_build_set.getBuilds()
1352 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1353 self.assertEqual(self.countJobResults(builds, None), 2)
1354 builds = items[1].current_build_set.getBuilds()
1355 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1356 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1357 self.assertEqual(self.countJobResults(builds, None), 1)
1358 builds = items[2].current_build_set.getBuilds()
1359 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1360 self.assertEqual(self.countJobResults(builds, 'CANCELED'), 2)
1361
1362 self.worker.hold_jobs_in_build = False
1363 self.worker.release()
1364 self.waitUntilSettled()
1365
1366 self.assertEqual(len(self.builds), 0)
1367 self.assertEqual(len(self.history), 12)
1368 self.assertEqual(A.data['status'], 'MERGED')
1369 self.assertEqual(B.data['status'], 'NEW')
1370 self.assertEqual(C.data['status'], 'MERGED')
1371 self.assertEqual(A.reported, 2)
1372 self.assertEqual(B.reported, 2)
1373 self.assertEqual(C.reported, 2)
1374
James E. Blaird466dc42012-07-31 10:42:56 -07001375 def test_failed_change_at_head_with_queue(self):
1376 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001377
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001378 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001379 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1380 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1381 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001382 A.addApproval('CRVW', 2)
1383 B.addApproval('CRVW', 2)
1384 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001385
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001386 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001387
1388 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1389 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1390 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1391
1392 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001393 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001394 self.assertEqual(len(self.builds), 0)
1395 self.assertEqual(len(queue), 1)
1396 self.assertEqual(queue[0].name, 'build:project-merge')
1397 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001398
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001399 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001400 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001401 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001402 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001403 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001404 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001405 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001406
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001407 self.assertEqual(len(self.builds), 0)
1408 self.assertEqual(len(queue), 6)
1409 self.assertEqual(queue[0].name, 'build:project-test1')
1410 self.assertEqual(queue[1].name, 'build:project-test2')
1411 self.assertEqual(queue[2].name, 'build:project-test1')
1412 self.assertEqual(queue[3].name, 'build:project-test2')
1413 self.assertEqual(queue[4].name, 'build:project-test1')
1414 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001415
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001416 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001417 self.waitUntilSettled()
1418
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001419 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001420 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001421 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1422 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001423
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001424 self.gearman_server.hold_jobs_in_queue = False
1425 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001426 self.waitUntilSettled()
1427
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001428 self.assertEqual(len(self.builds), 0)
1429 self.assertEqual(len(self.history), 11)
1430 self.assertEqual(A.data['status'], 'NEW')
1431 self.assertEqual(B.data['status'], 'MERGED')
1432 self.assertEqual(C.data['status'], 'MERGED')
1433 self.assertEqual(A.reported, 2)
1434 self.assertEqual(B.reported, 2)
1435 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001436
1437 def test_patch_order(self):
1438 "Test that dependent patches are tested in the right order"
1439 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1440 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1441 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1442 A.addApproval('CRVW', 2)
1443 B.addApproval('CRVW', 2)
1444 C.addApproval('CRVW', 2)
1445
1446 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1447 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1448 M2.setMerged()
1449 M1.setMerged()
1450
1451 # C -> B -> A -> M1 -> M2
1452 # M2 is here to make sure it is never queried. If it is, it
1453 # means zuul is walking down the entire history of merged
1454 # changes.
1455
1456 C.setDependsOn(B, 1)
1457 B.setDependsOn(A, 1)
1458 A.setDependsOn(M1, 1)
1459 M1.setDependsOn(M2, 1)
1460
1461 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1462
1463 self.waitUntilSettled()
1464
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001465 self.assertEqual(A.data['status'], 'NEW')
1466 self.assertEqual(B.data['status'], 'NEW')
1467 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001468
1469 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1470 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1471
1472 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001473 self.assertEqual(M2.queried, 0)
1474 self.assertEqual(A.data['status'], 'MERGED')
1475 self.assertEqual(B.data['status'], 'MERGED')
1476 self.assertEqual(C.data['status'], 'MERGED')
1477 self.assertEqual(A.reported, 2)
1478 self.assertEqual(B.reported, 2)
1479 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001480
James E. Blair0e933c52013-07-11 10:18:52 -07001481 def test_trigger_cache(self):
1482 "Test that the trigger cache operates correctly"
1483 self.worker.hold_jobs_in_build = True
1484
1485 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1486 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1487 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1488 A.addApproval('CRVW', 2)
1489 B.addApproval('CRVW', 2)
1490
1491 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1492 M1.setMerged()
1493
1494 B.setDependsOn(A, 1)
1495 A.setDependsOn(M1, 1)
1496
1497 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1498 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1499
1500 self.waitUntilSettled()
1501
1502 for build in self.builds:
1503 if build.parameters['ZUUL_PIPELINE'] == 'check':
1504 build.release()
1505 self.waitUntilSettled()
1506 for build in self.builds:
1507 if build.parameters['ZUUL_PIPELINE'] == 'check':
1508 build.release()
1509 self.waitUntilSettled()
1510
1511 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1512 self.waitUntilSettled()
1513
James E. Blair6c358e72013-07-29 17:06:47 -07001514 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001515 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001516 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001517
1518 self.worker.hold_jobs_in_build = False
1519 self.worker.release()
1520 self.waitUntilSettled()
1521
1522 self.assertEqual(A.data['status'], 'MERGED')
1523 self.assertEqual(B.data['status'], 'MERGED')
1524 self.assertEqual(A.queried, 2) # Initial and isMerged
1525 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1526
James E. Blair8c803f82012-07-31 16:25:42 -07001527 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001528 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001529 # TODO: move to test_gerrit (this is a unit test!)
1530 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001531 trigger = self.sched.layout.pipelines['gate'].trigger
1532 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001533 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001534 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001535
1536 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001537 a = trigger.getChange(1, 2, refresh=True)
1538 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001539
1540 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001541 a = trigger.getChange(1, 2, refresh=True)
1542 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1543 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001544
1545 def test_build_configuration(self):
1546 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001547
1548 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001549 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1550 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1551 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1552 A.addApproval('CRVW', 2)
1553 B.addApproval('CRVW', 2)
1554 C.addApproval('CRVW', 2)
1555 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1556 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1557 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1558 self.waitUntilSettled()
1559
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001560 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001561 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001562 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001563 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001564 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001565 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001566 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001567 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1568 self.gearman_server.hold_jobs_in_queue = False
1569 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001570 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001571
Monty Taylorbc758832013-06-17 17:22:42 -04001572 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001573 repo = git.Repo(path)
1574 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1575 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001576 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001577 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001578
1579 def test_build_configuration_conflict(self):
1580 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001581
1582 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001583 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1584 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001585 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001586 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1587 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001588 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001589 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1590 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001591 A.addApproval('CRVW', 2)
1592 B.addApproval('CRVW', 2)
1593 C.addApproval('CRVW', 2)
1594 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1595 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1596 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1597 self.waitUntilSettled()
1598
James E. Blair6736beb2013-07-11 15:18:15 -07001599 self.assertEqual(A.reported, 1)
1600 self.assertEqual(B.reported, 1)
1601 self.assertEqual(C.reported, 1)
1602
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001603 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001604 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001605 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001606 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001607 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001608 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001609 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001610 self.getParameter(queue[-1], 'ZUUL_REF')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001611 self.gearman_server.hold_jobs_in_queue = False
1612 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001613 self.waitUntilSettled()
1614
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001615 self.assertEqual(A.data['status'], 'MERGED')
1616 self.assertEqual(B.data['status'], 'NEW')
1617 self.assertEqual(C.data['status'], 'MERGED')
1618 self.assertEqual(A.reported, 2)
1619 self.assertEqual(B.reported, 2)
1620 self.assertEqual(C.reported, 2)
James E. Blairdaabed22012-08-15 15:38:57 -07001621
James E. Blair6736beb2013-07-11 15:18:15 -07001622 def test_dequeue_conflict(self):
1623 "Test that the option to dequeue merge conflicts works"
1624
1625 self.gearman_server.hold_jobs_in_queue = True
1626 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1627 A.addPatchset(['conflict'])
1628 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1629 B.addPatchset(['conflict'])
1630 A.addApproval('CRVW', 2)
1631 B.addApproval('CRVW', 2)
1632 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1633 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1634 self.waitUntilSettled()
1635
1636 self.assertEqual(A.reported, 1)
1637 self.assertEqual(B.reported, 2)
1638
1639 self.gearman_server.hold_jobs_in_queue = False
1640 self.gearman_server.release()
1641 self.waitUntilSettled()
1642
1643 self.assertEqual(A.data['status'], 'MERGED')
1644 self.assertEqual(B.data['status'], 'NEW')
1645 self.assertEqual(A.reported, 2)
1646 self.assertEqual(B.reported, 2)
1647
James E. Blairdaabed22012-08-15 15:38:57 -07001648 def test_post(self):
1649 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001650
Zhongyue Luo5d556072012-09-21 02:00:47 +09001651 e = {
1652 "type": "ref-updated",
1653 "submitter": {
1654 "name": "User Name",
1655 },
1656 "refUpdate": {
1657 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1658 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1659 "refName": "master",
1660 "project": "org/project",
1661 }
1662 }
James E. Blairdaabed22012-08-15 15:38:57 -07001663 self.fake_gerrit.addEvent(e)
1664 self.waitUntilSettled()
1665
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001666 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001667 self.assertEqual(len(self.history), 1)
1668 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001669
1670 def test_build_configuration_branch(self):
1671 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001672
1673 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001674 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1675 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1676 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1677 A.addApproval('CRVW', 2)
1678 B.addApproval('CRVW', 2)
1679 C.addApproval('CRVW', 2)
1680 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1681 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1682 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1683 self.waitUntilSettled()
1684
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001685 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001686 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001687 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001688 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001689 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001690 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001691 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001692 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1693 self.gearman_server.hold_jobs_in_queue = False
1694 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001695 self.waitUntilSettled()
1696
Monty Taylorbc758832013-06-17 17:22:42 -04001697 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001698 repo = git.Repo(path)
1699 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1700 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001701 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001702 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001703
1704 def test_build_configuration_branch_interaction(self):
1705 "Test that switching between branches works"
1706 self.test_build_configuration()
1707 self.test_build_configuration_branch()
1708 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001709 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001710 repo = git.Repo(path)
1711 repo.heads.master.commit = repo.commit('init')
1712 self.test_build_configuration()
1713
1714 def test_build_configuration_multi_branch(self):
1715 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001716
1717 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001718 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1719 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1720 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1721 A.addApproval('CRVW', 2)
1722 B.addApproval('CRVW', 2)
1723 C.addApproval('CRVW', 2)
1724 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1725 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1726 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1727 self.waitUntilSettled()
1728
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001729 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001730 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001731 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001732 job_B = None
1733 for job in queue:
1734 if 'project-merge' in job.name:
1735 job_B = job
1736 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001737 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001738 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001739 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001740 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001741 for job in queue:
1742 if 'project-merge' in job.name:
1743 job_C = job
1744 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001745 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001746 self.gearman_server.hold_jobs_in_queue = False
1747 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001748 self.waitUntilSettled()
1749
Monty Taylorbc758832013-06-17 17:22:42 -04001750 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001751 repo = git.Repo(path)
1752
1753 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001754 for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001755 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001756 correct_messages = ['initial commit', 'A-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001757 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001758
1759 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001760 for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001761 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001762 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001763 self.assertEqual(repo_messages, correct_messages)
James E. Blair7f71c802012-08-22 13:04:32 -07001764
1765 def test_one_job_project(self):
1766 "Test that queueing works with one job"
1767 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1768 'master', 'A')
1769 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1770 'master', 'B')
1771 A.addApproval('CRVW', 2)
1772 B.addApproval('CRVW', 2)
1773 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1774 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1775 self.waitUntilSettled()
1776
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001777 self.assertEqual(A.data['status'], 'MERGED')
1778 self.assertEqual(A.reported, 2)
1779 self.assertEqual(B.data['status'], 'MERGED')
1780 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001781
Antoine Musso80edd5a2013-02-13 15:37:53 +01001782 def test_job_from_templates_launched(self):
1783 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001784
Antoine Musso80edd5a2013-02-13 15:37:53 +01001785 A = self.fake_gerrit.addFakeChange(
1786 'org/templated-project', 'master', 'A')
1787 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1788 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001789
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001790 self.assertEqual(self.getJobFromHistory('project-test1').result,
1791 'SUCCESS')
1792 self.assertEqual(self.getJobFromHistory('project-test2').result,
1793 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001794
James E. Blaircaec0c52012-08-22 14:52:22 -07001795 def test_dependent_changes_dequeue(self):
1796 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001797
James E. Blaircaec0c52012-08-22 14:52:22 -07001798 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1799 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1800 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1801 A.addApproval('CRVW', 2)
1802 B.addApproval('CRVW', 2)
1803 C.addApproval('CRVW', 2)
1804
1805 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1806 M1.setMerged()
1807
1808 # C -> B -> A -> M1
1809
1810 C.setDependsOn(B, 1)
1811 B.setDependsOn(A, 1)
1812 A.setDependsOn(M1, 1)
1813
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001814 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001815
1816 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1817 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1818 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1819
1820 self.waitUntilSettled()
1821
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001822 self.assertEqual(A.data['status'], 'NEW')
1823 self.assertEqual(A.reported, 2)
1824 self.assertEqual(B.data['status'], 'NEW')
1825 self.assertEqual(B.reported, 2)
1826 self.assertEqual(C.data['status'], 'NEW')
1827 self.assertEqual(C.reported, 2)
1828 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001829
1830 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001831 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001832 # If it's dequeued more than once, we should see extra
1833 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001834
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001835 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001836 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1837 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1838 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1839 A.addApproval('CRVW', 2)
1840 B.addApproval('CRVW', 2)
1841 C.addApproval('CRVW', 2)
1842
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001843 self.worker.addFailTest('project1-test1', A)
1844 self.worker.addFailTest('project1-test2', A)
1845 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001846
1847 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1848 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1849 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1850
1851 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001852
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001853 self.assertEqual(len(self.builds), 1)
1854 self.assertEqual(self.builds[0].name, 'project1-merge')
1855 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07001856
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001857 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001858 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001859 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001860 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001861 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001862 self.waitUntilSettled()
1863
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001864 self.assertEqual(len(self.builds), 9)
1865 self.assertEqual(self.builds[0].name, 'project1-test1')
1866 self.assertEqual(self.builds[1].name, 'project1-test2')
1867 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
1868 self.assertEqual(self.builds[3].name, 'project1-test1')
1869 self.assertEqual(self.builds[4].name, 'project1-test2')
1870 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
1871 self.assertEqual(self.builds[6].name, 'project1-test1')
1872 self.assertEqual(self.builds[7].name, 'project1-test2')
1873 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001874
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001875 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001876 self.waitUntilSettled()
1877
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001878 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1879 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001880
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001881 self.worker.hold_jobs_in_build = False
1882 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001883 self.waitUntilSettled()
1884
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001885 self.assertEqual(len(self.builds), 0)
1886 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001887
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001888 self.assertEqual(A.data['status'], 'NEW')
1889 self.assertEqual(B.data['status'], 'MERGED')
1890 self.assertEqual(C.data['status'], 'MERGED')
1891 self.assertEqual(A.reported, 2)
1892 self.assertEqual(B.reported, 2)
1893 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001894
1895 def test_nonvoting_job(self):
1896 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001897
James E. Blair4ec821f2012-08-23 15:28:28 -07001898 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1899 'master', 'A')
1900 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001901 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001902 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1903
1904 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001905
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001906 self.assertEqual(A.data['status'], 'MERGED')
1907 self.assertEqual(A.reported, 2)
1908 self.assertEqual(
1909 self.getJobFromHistory('nonvoting-project-merge').result,
1910 'SUCCESS')
1911 self.assertEqual(
1912 self.getJobFromHistory('nonvoting-project-test1').result,
1913 'SUCCESS')
1914 self.assertEqual(
1915 self.getJobFromHistory('nonvoting-project-test2').result,
1916 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001917
1918 def test_check_queue_success(self):
1919 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001920
James E. Blaire0487072012-08-29 17:38:31 -07001921 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1922 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1923
1924 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001925
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001926 self.assertEqual(A.data['status'], 'NEW')
1927 self.assertEqual(A.reported, 1)
1928 self.assertEqual(self.getJobFromHistory('project-merge').result,
1929 'SUCCESS')
1930 self.assertEqual(self.getJobFromHistory('project-test1').result,
1931 'SUCCESS')
1932 self.assertEqual(self.getJobFromHistory('project-test2').result,
1933 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001934
1935 def test_check_queue_failure(self):
1936 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001937
James E. Blaire0487072012-08-29 17:38:31 -07001938 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001939 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001940 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1941
1942 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001943
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001944 self.assertEqual(A.data['status'], 'NEW')
1945 self.assertEqual(A.reported, 1)
1946 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001947 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001948 self.assertEqual(self.getJobFromHistory('project-test1').result,
1949 'SUCCESS')
1950 self.assertEqual(self.getJobFromHistory('project-test2').result,
1951 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001952
1953 def test_dependent_behind_dequeue(self):
1954 "test that dependent changes behind dequeued changes work"
1955 # This complicated test is a reproduction of a real life bug
1956 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001957
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001958 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001959 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1960 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1961 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1962 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1963 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1964 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1965 D.setDependsOn(C, 1)
1966 E.setDependsOn(D, 1)
1967 A.addApproval('CRVW', 2)
1968 B.addApproval('CRVW', 2)
1969 C.addApproval('CRVW', 2)
1970 D.addApproval('CRVW', 2)
1971 E.addApproval('CRVW', 2)
1972 F.addApproval('CRVW', 2)
1973
1974 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001975
1976 # Change object re-use in the gerrit trigger is hidden if
1977 # changes are added in quick succession; waiting makes it more
1978 # like real life.
1979 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1980 self.waitUntilSettled()
1981 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1982 self.waitUntilSettled()
1983
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001984 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001985 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001986 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001987 self.waitUntilSettled()
1988
1989 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1990 self.waitUntilSettled()
1991 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1992 self.waitUntilSettled()
1993 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1994 self.waitUntilSettled()
1995 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1996 self.waitUntilSettled()
1997
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001998 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001999 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002000 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002001 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002002 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002003 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002004 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002005 self.waitUntilSettled()
2006
2007 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002008
2009 # Grab pointers to the jobs we want to release before
2010 # releasing any, because list indexes may change as
2011 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002012 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002013 a.release()
2014 b.release()
2015 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002016 self.waitUntilSettled()
2017
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002018 self.worker.hold_jobs_in_build = False
2019 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002020 self.waitUntilSettled()
2021
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002022 self.assertEqual(A.data['status'], 'NEW')
2023 self.assertEqual(B.data['status'], 'MERGED')
2024 self.assertEqual(C.data['status'], 'MERGED')
2025 self.assertEqual(D.data['status'], 'MERGED')
2026 self.assertEqual(E.data['status'], 'MERGED')
2027 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002028
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002029 self.assertEqual(A.reported, 2)
2030 self.assertEqual(B.reported, 2)
2031 self.assertEqual(C.reported, 2)
2032 self.assertEqual(D.reported, 2)
2033 self.assertEqual(E.reported, 2)
2034 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002035
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002036 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2037 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002038
2039 def test_merger_repack(self):
2040 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002041
James E. Blair05fed602012-09-07 12:45:24 -07002042 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2043 A.addApproval('CRVW', 2)
2044 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2045 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002046 self.assertEqual(self.getJobFromHistory('project-merge').result,
2047 'SUCCESS')
2048 self.assertEqual(self.getJobFromHistory('project-test1').result,
2049 'SUCCESS')
2050 self.assertEqual(self.getJobFromHistory('project-test2').result,
2051 'SUCCESS')
2052 self.assertEqual(A.data['status'], 'MERGED')
2053 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07002054 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002055 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002056
Monty Taylorbc758832013-06-17 17:22:42 -04002057 path = os.path.join(self.git_root, "org/project")
2058 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002059
2060 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2061 A.addApproval('CRVW', 2)
2062 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2063 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002064 self.assertEqual(self.getJobFromHistory('project-merge').result,
2065 'SUCCESS')
2066 self.assertEqual(self.getJobFromHistory('project-test1').result,
2067 'SUCCESS')
2068 self.assertEqual(self.getJobFromHistory('project-test2').result,
2069 'SUCCESS')
2070 self.assertEqual(A.data['status'], 'MERGED')
2071 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002072
James E. Blair4886f282012-11-15 09:27:33 -08002073 def test_merger_repack_large_change(self):
2074 "Test that the merger works with large changes after a repack"
2075 # https://bugs.launchpad.net/zuul/+bug/1078946
2076 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2077 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002078 path = os.path.join(self.upstream_root, "org/project1")
2079 print repack_repo(path)
2080 path = os.path.join(self.git_root, "org/project1")
2081 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002082
2083 A.addApproval('CRVW', 2)
2084 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2085 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002086 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2087 'SUCCESS')
2088 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2089 'SUCCESS')
2090 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2091 'SUCCESS')
2092 self.assertEqual(A.data['status'], 'MERGED')
2093 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002094
James E. Blair7ee88a22012-09-12 18:59:31 +02002095 def test_nonexistent_job(self):
2096 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002097 # Set to the state immediately after a restart
2098 self.resetGearmanServer()
2099 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002100
2101 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2102 A.addApproval('CRVW', 2)
2103 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2104 # There may be a thread about to report a lost change
2105 while A.reported < 2:
2106 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002107 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002108 self.assertFalse(job_names)
2109 self.assertEqual(A.data['status'], 'NEW')
2110 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002111 self.assertEmptyQueues()
2112
2113 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002114 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002115 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2116 A.addApproval('CRVW', 2)
2117 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2118 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002119 self.assertEqual(self.getJobFromHistory('project-merge').result,
2120 'SUCCESS')
2121 self.assertEqual(self.getJobFromHistory('project-test1').result,
2122 'SUCCESS')
2123 self.assertEqual(self.getJobFromHistory('project-test2').result,
2124 'SUCCESS')
2125 self.assertEqual(A.data['status'], 'MERGED')
2126 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002127
2128 def test_single_nonexistent_post_job(self):
2129 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002130 e = {
2131 "type": "ref-updated",
2132 "submitter": {
2133 "name": "User Name",
2134 },
2135 "refUpdate": {
2136 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2137 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2138 "refName": "master",
2139 "project": "org/project",
2140 }
2141 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002142 # Set to the state immediately after a restart
2143 self.resetGearmanServer()
2144 self.launcher.negative_function_cache_ttl = 0
2145
James E. Blairf62d4282012-12-31 17:01:50 -08002146 self.fake_gerrit.addEvent(e)
2147 self.waitUntilSettled()
2148
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002149 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002150
2151 def test_new_patchset_dequeues_old(self):
2152 "Test that a new patchset causes the old to be dequeued"
2153 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002154 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002155 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2156 M.setMerged()
2157
2158 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2159 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2160 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2161 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2162 A.addApproval('CRVW', 2)
2163 B.addApproval('CRVW', 2)
2164 C.addApproval('CRVW', 2)
2165 D.addApproval('CRVW', 2)
2166
2167 C.setDependsOn(B, 1)
2168 B.setDependsOn(A, 1)
2169 A.setDependsOn(M, 1)
2170
2171 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2172 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2173 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2174 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2175 self.waitUntilSettled()
2176
2177 B.addPatchset()
2178 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2179 self.waitUntilSettled()
2180
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002181 self.worker.hold_jobs_in_build = False
2182 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002183 self.waitUntilSettled()
2184
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002185 self.assertEqual(A.data['status'], 'MERGED')
2186 self.assertEqual(A.reported, 2)
2187 self.assertEqual(B.data['status'], 'NEW')
2188 self.assertEqual(B.reported, 2)
2189 self.assertEqual(C.data['status'], 'NEW')
2190 self.assertEqual(C.reported, 2)
2191 self.assertEqual(D.data['status'], 'MERGED')
2192 self.assertEqual(D.reported, 2)
2193 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002194
2195 def test_new_patchset_dequeues_old_on_head(self):
2196 "Test that a new patchset causes the old to be dequeued (at head)"
2197 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002198 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002199 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2200 M.setMerged()
2201 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2202 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2203 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2204 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2205 A.addApproval('CRVW', 2)
2206 B.addApproval('CRVW', 2)
2207 C.addApproval('CRVW', 2)
2208 D.addApproval('CRVW', 2)
2209
2210 C.setDependsOn(B, 1)
2211 B.setDependsOn(A, 1)
2212 A.setDependsOn(M, 1)
2213
2214 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2215 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2216 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2217 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2218 self.waitUntilSettled()
2219
2220 A.addPatchset()
2221 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2222 self.waitUntilSettled()
2223
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002224 self.worker.hold_jobs_in_build = False
2225 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002226 self.waitUntilSettled()
2227
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002228 self.assertEqual(A.data['status'], 'NEW')
2229 self.assertEqual(A.reported, 2)
2230 self.assertEqual(B.data['status'], 'NEW')
2231 self.assertEqual(B.reported, 2)
2232 self.assertEqual(C.data['status'], 'NEW')
2233 self.assertEqual(C.reported, 2)
2234 self.assertEqual(D.data['status'], 'MERGED')
2235 self.assertEqual(D.reported, 2)
2236 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002237
2238 def test_new_patchset_dequeues_old_without_dependents(self):
2239 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002240 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002241 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2242 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2243 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2244 A.addApproval('CRVW', 2)
2245 B.addApproval('CRVW', 2)
2246 C.addApproval('CRVW', 2)
2247
2248 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2249 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2250 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2251 self.waitUntilSettled()
2252
2253 B.addPatchset()
2254 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2255 self.waitUntilSettled()
2256
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002257 self.worker.hold_jobs_in_build = False
2258 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002259 self.waitUntilSettled()
2260
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002261 self.assertEqual(A.data['status'], 'MERGED')
2262 self.assertEqual(A.reported, 2)
2263 self.assertEqual(B.data['status'], 'NEW')
2264 self.assertEqual(B.reported, 2)
2265 self.assertEqual(C.data['status'], 'MERGED')
2266 self.assertEqual(C.reported, 2)
2267 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002268
2269 def test_new_patchset_dequeues_old_independent_queue(self):
2270 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002271 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002272 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2273 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2274 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2275 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2276 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2277 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2278 self.waitUntilSettled()
2279
2280 B.addPatchset()
2281 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2282 self.waitUntilSettled()
2283
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002284 self.worker.hold_jobs_in_build = False
2285 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002286 self.waitUntilSettled()
2287
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002288 self.assertEqual(A.data['status'], 'NEW')
2289 self.assertEqual(A.reported, 1)
2290 self.assertEqual(B.data['status'], 'NEW')
2291 self.assertEqual(B.reported, 1)
2292 self.assertEqual(C.data['status'], 'NEW')
2293 self.assertEqual(C.reported, 1)
2294 self.assertEqual(len(self.history), 10)
2295 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002296
2297 def test_zuul_refs(self):
2298 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002299 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002300 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2301 M1.setMerged()
2302 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2303 M2.setMerged()
2304
2305 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2306 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2307 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2308 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2309 A.addApproval('CRVW', 2)
2310 B.addApproval('CRVW', 2)
2311 C.addApproval('CRVW', 2)
2312 D.addApproval('CRVW', 2)
2313 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2314 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2315 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2316 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2317
2318 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002319 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002320 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002321 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002322 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002323 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002324 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002325 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002326 self.waitUntilSettled()
2327
James E. Blair7d0dedc2013-02-21 17:26:09 -08002328 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002329 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002330 if x.parameters['ZUUL_CHANGE'] == '3':
2331 a_zref = x.parameters['ZUUL_REF']
2332 if x.parameters['ZUUL_CHANGE'] == '4':
2333 b_zref = x.parameters['ZUUL_REF']
2334 if x.parameters['ZUUL_CHANGE'] == '5':
2335 c_zref = x.parameters['ZUUL_REF']
2336 if x.parameters['ZUUL_CHANGE'] == '6':
2337 d_zref = x.parameters['ZUUL_REF']
2338
2339 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002340 self.assertIsNotNone(a_zref)
2341 self.assertIsNotNone(b_zref)
2342 self.assertIsNotNone(c_zref)
2343 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002344
2345 # And they should all be different
2346 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002347 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002348
2349 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002350 self.assertTrue(self.ref_has_change(a_zref, A))
2351 self.assertFalse(self.ref_has_change(a_zref, B))
2352 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002353
2354 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002355 self.assertTrue(self.ref_has_change(b_zref, A))
2356 self.assertTrue(self.ref_has_change(b_zref, B))
2357 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002358
2359 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002360 self.assertTrue(self.ref_has_change(c_zref, A))
2361 self.assertTrue(self.ref_has_change(c_zref, B))
2362 self.assertTrue(self.ref_has_change(c_zref, C))
2363 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002364
2365 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002366 self.assertTrue(self.ref_has_change(d_zref, A))
2367 self.assertTrue(self.ref_has_change(d_zref, B))
2368 self.assertTrue(self.ref_has_change(d_zref, C))
2369 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002370
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002371 self.worker.hold_jobs_in_build = False
2372 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002373 self.waitUntilSettled()
2374
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002375 self.assertEqual(A.data['status'], 'MERGED')
2376 self.assertEqual(A.reported, 2)
2377 self.assertEqual(B.data['status'], 'MERGED')
2378 self.assertEqual(B.reported, 2)
2379 self.assertEqual(C.data['status'], 'MERGED')
2380 self.assertEqual(C.reported, 2)
2381 self.assertEqual(D.data['status'], 'MERGED')
2382 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002383
James E. Blair412e5582013-04-22 15:50:12 -07002384 def test_statsd(self):
2385 "Test each of the statsd methods used in the scheduler"
2386 import extras
2387 statsd = extras.try_import('statsd.statsd')
2388 statsd.incr('test-incr')
2389 statsd.timing('test-timing', 3)
2390 statsd.gauge('test-guage', 12)
2391 self.assertReportedStat('test-incr', '1|c')
2392 self.assertReportedStat('test-timing', '3|ms')
2393 self.assertReportedStat('test-guage', '12|g')
2394
James E. Blair70c71582013-03-06 08:50:50 -08002395 def test_file_jobs(self):
2396 "Test that file jobs run only when appropriate"
2397 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2398 A.addPatchset(['pip-requires'])
2399 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2400 A.addApproval('CRVW', 2)
2401 B.addApproval('CRVW', 2)
2402 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2403 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2404 self.waitUntilSettled()
2405
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002406 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002407 if x.name == 'project-testfile']
2408
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002409 self.assertEqual(len(testfile_jobs), 1)
2410 self.assertEqual(testfile_jobs[0].changes, '1,2')
2411 self.assertEqual(A.data['status'], 'MERGED')
2412 self.assertEqual(A.reported, 2)
2413 self.assertEqual(B.data['status'], 'MERGED')
2414 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002415
2416 def test_test_config(self):
2417 "Test that we can test the config"
2418 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002419 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002420 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002421 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002422
2423 def test_build_description(self):
2424 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002425 self.worker.registerFunction('set_description:' +
2426 self.worker.worker_id)
2427
2428 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2429 A.addApproval('CRVW', 2)
2430 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2431 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002432 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002433 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002434 self.assertTrue(re.search("Branch.*master", desc))
2435 self.assertTrue(re.search("Pipeline.*gate", desc))
2436 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2437 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2438 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2439 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002440
James E. Blair64ed6f22013-07-10 14:07:23 -07002441 def test_queue_precedence(self):
2442 "Test that queue precedence works"
2443
2444 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002445 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002446 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2447 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2448 A.addApproval('CRVW', 2)
2449 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2450
2451 self.waitUntilSettled()
2452 self.gearman_server.hold_jobs_in_queue = False
2453 self.gearman_server.release()
2454 self.waitUntilSettled()
2455
James E. Blair8de58bd2013-07-18 16:23:33 -07002456 # Run one build at a time to ensure non-race order:
2457 for x in range(6):
2458 self.release(self.builds[0])
2459 self.waitUntilSettled()
2460 self.worker.hold_jobs_in_build = False
2461 self.waitUntilSettled()
2462
James E. Blair64ed6f22013-07-10 14:07:23 -07002463 self.log.debug(self.history)
2464 self.assertEqual(self.history[0].pipeline, 'gate')
2465 self.assertEqual(self.history[1].pipeline, 'check')
2466 self.assertEqual(self.history[2].pipeline, 'gate')
2467 self.assertEqual(self.history[3].pipeline, 'gate')
2468 self.assertEqual(self.history[4].pipeline, 'check')
2469 self.assertEqual(self.history[5].pipeline, 'check')
2470
James E. Blair1843a552013-07-03 14:19:52 -07002471 def test_json_status(self):
2472 "Test that we can retrieve JSON status info"
2473 self.worker.hold_jobs_in_build = True
2474 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2475 A.addApproval('CRVW', 2)
2476 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2477 self.waitUntilSettled()
2478
2479 port = self.webapp.server.socket.getsockname()[1]
2480
2481 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2482 data = f.read()
2483
2484 self.worker.hold_jobs_in_build = False
2485 self.worker.release()
2486 self.waitUntilSettled()
2487
2488 data = json.loads(data)
2489 status_jobs = set()
2490 for p in data['pipelines']:
2491 for q in p['change_queues']:
2492 for head in q['heads']:
2493 for change in head:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002494 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002495 for job in change['jobs']:
2496 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002497 self.assertIn('project-merge', status_jobs)
2498 self.assertIn('project-test1', status_jobs)
2499 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002500
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002501 def test_node_label(self):
2502 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002503 self.worker.registerFunction('build:node-project-test1:debian')
2504
2505 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2506 A.addApproval('CRVW', 2)
2507 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2508 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002509
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002510 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2511 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2512 'debian')
2513 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002514
2515 def test_live_reconfiguration(self):
2516 "Test that live reconfiguration works"
2517 self.worker.hold_jobs_in_build = True
2518 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2519 A.addApproval('CRVW', 2)
2520 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2521 self.waitUntilSettled()
2522
2523 self.sched.reconfigure(self.config)
2524
2525 self.worker.hold_jobs_in_build = False
2526 self.worker.release()
2527 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002528 self.assertEqual(self.getJobFromHistory('project-merge').result,
2529 'SUCCESS')
2530 self.assertEqual(self.getJobFromHistory('project-test1').result,
2531 'SUCCESS')
2532 self.assertEqual(self.getJobFromHistory('project-test2').result,
2533 'SUCCESS')
2534 self.assertEqual(A.data['status'], 'MERGED')
2535 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002536
James E. Blaire712d9f2013-07-31 11:40:11 -07002537 def test_live_reconfiguration_functions(self):
2538 "Test live reconfiguration with a custom function"
2539 self.worker.registerFunction('build:node-project-test1:debian')
2540 self.worker.registerFunction('build:node-project-test1:wheezy')
2541 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2542 A.addApproval('CRVW', 2)
2543 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2544 self.waitUntilSettled()
2545
2546 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2547 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2548 'debian')
2549 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2550
2551 self.config.set('zuul', 'layout_config',
2552 'tests/fixtures/layout-live-'
2553 'reconfiguration-functions.yaml')
2554 self.sched.reconfigure(self.config)
2555 self.worker.build_history = []
2556
2557 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
2558 B.addApproval('CRVW', 2)
2559 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2560 self.waitUntilSettled()
2561
2562 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2563 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2564 'wheezy')
2565 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2566
James E. Blair287c06d2013-07-24 10:39:30 -07002567 def test_delayed_repo_init(self):
2568 self.config.set('zuul', 'layout_config',
2569 'tests/fixtures/layout-delayed-repo-init.yaml')
2570 self.sched.reconfigure(self.config)
2571
2572 self.init_repo("org/new-project")
2573 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2574
2575 A.addApproval('CRVW', 2)
2576 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2577 self.waitUntilSettled()
2578 self.assertEqual(self.getJobFromHistory('project-merge').result,
2579 'SUCCESS')
2580 self.assertEqual(self.getJobFromHistory('project-test1').result,
2581 'SUCCESS')
2582 self.assertEqual(self.getJobFromHistory('project-test2').result,
2583 'SUCCESS')
2584 self.assertEqual(A.data['status'], 'MERGED')
2585 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002586
2587 def test_timer(self):
2588 "Test that a periodic job is triggered"
2589 self.worker.hold_jobs_in_build = True
2590 self.config.set('zuul', 'layout_config',
2591 'tests/fixtures/layout-timer.yaml')
2592 self.sched.reconfigure(self.config)
2593 self.registerJobs()
2594
2595 start = time.time()
2596 failed = True
2597 while ((time.time() - start) < 30):
2598 if len(self.builds) == 2:
2599 failed = False
2600 break
2601 else:
2602 time.sleep(1)
2603
2604 if failed:
2605 raise Exception("Expected jobs never ran")
2606
2607 self.waitUntilSettled()
2608 port = self.webapp.server.socket.getsockname()[1]
2609
2610 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2611 data = f.read()
2612
2613 self.worker.hold_jobs_in_build = False
2614 self.worker.release()
2615 self.waitUntilSettled()
2616
2617 self.assertEqual(self.getJobFromHistory(
2618 'project-bitrot-stable-old').result, 'SUCCESS')
2619 self.assertEqual(self.getJobFromHistory(
2620 'project-bitrot-stable-older').result, 'SUCCESS')
2621
2622 data = json.loads(data)
2623 status_jobs = set()
2624 for p in data['pipelines']:
2625 for q in p['change_queues']:
2626 for head in q['heads']:
2627 for change in head:
2628 self.assertEqual(change['id'], 'None')
2629 for job in change['jobs']:
2630 status_jobs.add(job['name'])
2631 self.assertIn('project-bitrot-stable-old', status_jobs)
2632 self.assertIn('project-bitrot-stable-older', status_jobs)