blob: 07d5c7d4f7e03b38f7672bb205aef6d4fb694f64 [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
32import tempfile
33import threading
34import time
James E. Blair1843a552013-07-03 14:19:52 -070035import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040036import urllib2
37import urlparse
38
James E. Blair4886cc12012-07-18 15:39:41 -070039import git
James E. Blair1f4c2bb2013-04-26 08:40:46 -070040import gear
Monty Taylorbc758832013-06-17 17:22:42 -040041import fixtures
42import statsd
43import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070044
James E. Blairb0fcae42012-07-17 11:12:10 -070045import zuul.scheduler
James E. Blair1843a552013-07-03 14:19:52 -070046import zuul.webapp
James E. Blair1f4c2bb2013-04-26 08:40:46 -070047import zuul.launcher.gearman
James E. Blairb0fcae42012-07-17 11:12:10 -070048import zuul.trigger.gerrit
49
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
152 return repo.index.commit(msg)
153
James E. Blair70c71582013-03-06 08:50:50 -0800154 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700155 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700156 if files:
157 fn = files[0]
158 else:
159 fn = '%s-%s' % (self.branch, self.number)
160 msg = self.subject + '-' + str(self.latest_patchset)
Monty Taylorbc758832013-06-17 17:22:42 -0400161 c = self.add_fake_change_to_repo(msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800162 ps_files = [{'file': '/COMMIT_MSG',
163 'type': 'ADDED'},
164 {'file': 'README',
165 'type': 'MODIFIED'}]
166 for f in files:
167 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700168 d = {'approvals': [],
169 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800170 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700171 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700172 'ref': 'refs/changes/1/%s/%s' % (self.number,
173 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700174 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700175 'uploader': {'email': 'user@example.com',
176 'name': 'User name',
177 'username': 'user'}}
178 self.data['currentPatchSet'] = d
179 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700180 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700181
James E. Blaire0487072012-08-29 17:38:31 -0700182 def getPatchsetCreatedEvent(self, patchset):
183 event = {"type": "patchset-created",
184 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800185 "branch": self.branch,
186 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
187 "number": str(self.number),
188 "subject": self.subject,
189 "owner": {"name": "User Name"},
190 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700191 "patchSet": self.patchsets[patchset - 1],
192 "uploader": {"name": "User Name"}}
193 return event
194
James E. Blair42f74822013-05-14 15:18:03 -0700195 def getChangeRestoredEvent(self):
196 event = {"type": "change-restored",
197 "change": {"project": self.project,
198 "branch": self.branch,
199 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
200 "number": str(self.number),
201 "subject": self.subject,
202 "owner": {"name": "User Name"},
203 "url": "https://hostname/3"},
204 "restorer": {"name": "User Name"},
205 "reason": ""}
206 return event
207
James E. Blairb0fcae42012-07-17 11:12:10 -0700208 def addApproval(self, category, value):
James E. Blair8c803f82012-07-31 16:25:42 -0700209 approval = {'description': self.categories[category][0],
210 'type': category,
211 'value': str(value)}
212 self.patchsets[-1]['approvals'].append(approval)
213 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700214 'author': {'email': 'user@example.com',
215 'name': 'User Name',
216 'username': 'username'},
217 'change': {'branch': self.branch,
218 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
219 'number': str(self.number),
220 'owner': {'email': 'user@example.com',
221 'name': 'User Name',
222 'username': 'username'},
223 'project': self.project,
224 'subject': self.subject,
225 'topic': 'master',
226 'url': 'https://hostname/459'},
227 'comment': '',
228 'patchSet': self.patchsets[-1],
229 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700230 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700231 return json.loads(json.dumps(event))
232
James E. Blair8c803f82012-07-31 16:25:42 -0700233 def getSubmitRecords(self):
234 status = {}
235 for cat in self.categories.keys():
236 status[cat] = 0
237
238 for a in self.patchsets[-1]['approvals']:
239 cur = status[a['type']]
240 cat_min, cat_max = self.categories[a['type']][1:]
241 new = int(a['value'])
242 if new == cat_min:
243 cur = new
244 elif abs(new) > abs(cur):
245 cur = new
246 status[a['type']] = cur
247
248 labels = []
249 ok = True
250 for typ, cat in self.categories.items():
251 cur = status[typ]
252 cat_min, cat_max = cat[1:]
253 if cur == cat_min:
254 value = 'REJECT'
255 ok = False
256 elif cur == cat_max:
257 value = 'OK'
258 else:
259 value = 'NEED'
260 ok = False
261 labels.append({'label': cat[0], 'status': value})
262 if ok:
263 return [{'status': 'OK'}]
264 return [{'status': 'NOT_READY',
265 'labels': labels}]
266
267 def setDependsOn(self, other, patchset):
268 self.depends_on_change = other
269 d = {'id': other.data['id'],
270 'number': other.data['number'],
271 'ref': other.patchsets[patchset - 1]['ref']
272 }
273 self.data['dependsOn'] = [d]
274
275 other.needed_by_changes.append(self)
276 needed = other.data.get('neededBy', [])
277 d = {'id': self.data['id'],
278 'number': self.data['number'],
279 'ref': self.patchsets[patchset - 1]['ref'],
280 'revision': self.patchsets[patchset - 1]['revision']
281 }
282 needed.append(d)
283 other.data['neededBy'] = needed
284
James E. Blairb0fcae42012-07-17 11:12:10 -0700285 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700286 self.queried += 1
287 d = self.data.get('dependsOn')
288 if d:
289 d = d[0]
290 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
291 d['isCurrentPatchSet'] = True
292 else:
293 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700294 return json.loads(json.dumps(self.data))
295
296 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800297 if (self.depends_on_change and
298 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700299 return
James E. Blair127bc182012-08-28 15:55:15 -0700300 if self.fail_merge:
301 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700302 self.data['status'] = 'MERGED'
303 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700304
Monty Taylorbc758832013-06-17 17:22:42 -0400305 path = os.path.join(self.upstream_root, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700306 repo = git.Repo(path)
307 repo.heads[self.branch].commit = \
308 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700309
James E. Blaird466dc42012-07-31 10:42:56 -0700310 def setReported(self):
311 self.reported += 1
312
James E. Blairb0fcae42012-07-17 11:12:10 -0700313
314class FakeGerrit(object):
315 def __init__(self, *args, **kw):
316 self.event_queue = Queue.Queue()
317 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
318 self.change_number = 0
319 self.changes = {}
320
321 def addFakeChange(self, project, branch, subject):
322 self.change_number += 1
Monty Taylorbc758832013-06-17 17:22:42 -0400323 c = FakeChange(self, self.change_number, project, branch, subject,
324 upstream_root=self.upstream_root)
James E. Blairb0fcae42012-07-17 11:12:10 -0700325 self.changes[self.change_number] = c
326 return c
327
328 def addEvent(self, data):
329 return self.event_queue.put(data)
330
331 def getEvent(self):
332 return self.event_queue.get()
333
334 def eventDone(self):
335 self.event_queue.task_done()
336
337 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700338 number, ps = changeid.split(',')
339 change = self.changes[int(number)]
James E. Blair42f74822013-05-14 15:18:03 -0700340 change.messages.append(message)
James E. Blairb0fcae42012-07-17 11:12:10 -0700341 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700342 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700343 if message:
344 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700345
346 def query(self, number):
347 change = self.changes[int(number)]
348 return change.query()
349
350 def startWatching(self, *args, **kw):
351 pass
352
353
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700354class BuildHistory(object):
355 def __init__(self, **kw):
356 self.__dict__.update(kw)
James E. Blairb0fcae42012-07-17 11:12:10 -0700357
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700358 def __repr__(self):
359 return ("<Completed build, result: %s name: %s #%s changes: %s>" %
360 (self.result, self.name, self.number, self.changes))
James E. Blairb0fcae42012-07-17 11:12:10 -0700361
362
James E. Blair8cc15a82012-08-01 11:17:57 -0700363class FakeURLOpener(object):
Monty Taylorbc758832013-06-17 17:22:42 -0400364 def __init__(self, upstream_root, fake_gerrit, url):
365 self.upstream_root = upstream_root
James E. Blair8cc15a82012-08-01 11:17:57 -0700366 self.fake_gerrit = fake_gerrit
367 self.url = url
368
369 def read(self):
370 res = urlparse.urlparse(self.url)
371 path = res.path
372 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200373 ret = '001e# service=git-upload-pack\n'
374 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
375 'multi_ack thin-pack side-band side-band-64k ofs-delta '
376 'shallow no-progress include-tag multi_ack_detailed no-done\n')
Monty Taylorbc758832013-06-17 17:22:42 -0400377 path = os.path.join(self.upstream_root, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700378 repo = git.Repo(path)
379 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200380 r = ref.object.hexsha + ' ' + ref.path + '\n'
381 ret += '%04x%s' % (len(r) + 4, r)
382 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700383 return ret
384
385
James E. Blair4886cc12012-07-18 15:39:41 -0700386class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
Monty Taylorbc758832013-06-17 17:22:42 -0400387 def __init__(self, upstream_root, *args):
388 super(FakeGerritTrigger, self).__init__(*args)
389 self.upstream_root = upstream_root
390
James E. Blair4886cc12012-07-18 15:39:41 -0700391 def getGitUrl(self, project):
Monty Taylorbc758832013-06-17 17:22:42 -0400392 return os.path.join(self.upstream_root, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700393
394
James E. Blair412e5582013-04-22 15:50:12 -0700395class FakeStatsd(threading.Thread):
396 def __init__(self):
397 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400398 self.daemon = True
James E. Blair412e5582013-04-22 15:50:12 -0700399 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
400 self.sock.bind(('', 0))
401 self.port = self.sock.getsockname()[1]
402 self.wake_read, self.wake_write = os.pipe()
403 self.stats = []
404
405 def run(self):
406 while True:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700407 poll = select.poll()
408 poll.register(self.sock, select.POLLIN)
409 poll.register(self.wake_read, select.POLLIN)
410 ret = poll.poll()
411 for (fd, event) in ret:
412 if fd == self.sock.fileno():
James E. Blair412e5582013-04-22 15:50:12 -0700413 data = self.sock.recvfrom(1024)
414 if not data:
415 return
416 self.stats.append(data[0])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700417 if fd == self.wake_read:
James E. Blair412e5582013-04-22 15:50:12 -0700418 return
419
420 def stop(self):
421 os.write(self.wake_write, '1\n')
422
423
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700424class FakeBuild(threading.Thread):
425 log = logging.getLogger("zuul.test")
426
427 def __init__(self, worker, job, number, node):
428 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400429 self.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700430 self.worker = worker
431 self.job = job
432 self.name = job.name.split(':')[1]
433 self.number = number
434 self.node = node
435 self.parameters = json.loads(job.arguments)
436 self.unique = self.parameters['ZUUL_UUID']
437 self.wait_condition = threading.Condition()
438 self.waiting = False
439 self.aborted = False
440 self.created = time.time()
441 self.description = ''
442
443 def release(self):
444 self.wait_condition.acquire()
445 self.wait_condition.notify()
446 self.waiting = False
447 self.log.debug("Build %s released" % self.unique)
448 self.wait_condition.release()
449
450 def isWaiting(self):
451 self.wait_condition.acquire()
452 if self.waiting:
453 ret = True
454 else:
455 ret = False
456 self.wait_condition.release()
457 return ret
458
459 def _wait(self):
460 self.wait_condition.acquire()
461 self.waiting = True
462 self.log.debug("Build %s waiting" % self.unique)
463 self.wait_condition.wait()
464 self.wait_condition.release()
465
466 def run(self):
467 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700468 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
469 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700470 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700471 'manager': self.worker.worker_id,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700472 }
473
474 self.job.sendWorkData(json.dumps(data))
475 self.job.sendWorkStatus(0, 100)
476
477 if self.worker.hold_jobs_in_build:
478 self._wait()
479 self.log.debug("Build %s continuing" % self.unique)
480
481 self.worker.lock.acquire()
482
483 result = 'SUCCESS'
484 if (('ZUUL_REF' in self.parameters) and
485 self.worker.shouldFailTest(self.name,
486 self.parameters['ZUUL_REF'])):
487 result = 'FAILURE'
488 if self.aborted:
489 result = 'ABORTED'
490
491 data = {'result': result}
492 changes = None
493 if 'ZUUL_CHANGE_IDS' in self.parameters:
494 changes = self.parameters['ZUUL_CHANGE_IDS']
495
496 self.worker.build_history.append(
497 BuildHistory(name=self.name, number=self.number,
498 result=result, changes=changes, node=self.node,
499 uuid=self.unique, description=self.description)
500 )
501
502 self.job.sendWorkComplete(json.dumps(data))
503 del self.worker.gearman_jobs[self.job.unique]
504 self.worker.running_builds.remove(self)
505 self.worker.lock.release()
506
507
508class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400509 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700510 super(FakeWorker, self).__init__(worker_id)
511 self.gearman_jobs = {}
512 self.build_history = []
513 self.running_builds = []
514 self.build_counter = 0
515 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400516 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700517
518 self.hold_jobs_in_build = False
519 self.lock = threading.Lock()
520 self.__work_thread = threading.Thread(target=self.work)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400521 self.__work_thread.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700522 self.__work_thread.start()
523
524 def handleJob(self, job):
525 parts = job.name.split(":")
526 cmd = parts[0]
527 name = parts[1]
528 if len(parts) > 2:
529 node = parts[2]
530 else:
531 node = None
532 if cmd == 'build':
533 self.handleBuild(job, name, node)
534 elif cmd == 'stop':
535 self.handleStop(job, name)
536 elif cmd == 'set_description':
537 self.handleSetDescription(job, name)
538
539 def handleBuild(self, job, name, node):
540 build = FakeBuild(self, job, self.build_counter, node)
541 job.build = build
542 self.gearman_jobs[job.unique] = job
543 self.build_counter += 1
544
545 self.running_builds.append(build)
546 build.start()
547
548 def handleStop(self, job, name):
549 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700550 parameters = json.loads(job.arguments)
551 name = parameters['name']
552 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700553 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700554 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700555 build.aborted = True
556 build.release()
557 job.sendWorkComplete()
558 return
559 job.sendWorkFail()
560
561 def handleSetDescription(self, job, name):
562 self.log.debug("handle set description")
563 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700564 name = parameters['name']
565 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700566 descr = parameters['html_description']
567 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700568 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700569 build.description = descr
570 job.sendWorkComplete()
571 return
572 for build in self.build_history:
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 job.sendWorkFail()
578
579 def work(self):
580 while self.running:
581 try:
582 job = self.getJob()
583 except gear.InterruptedError:
584 continue
585 try:
586 self.handleJob(job)
587 except:
588 self.log.exception("Worker exception:")
589
590 def addFailTest(self, name, change):
591 l = self.fail_tests.get(name, [])
592 l.append(change)
593 self.fail_tests[name] = l
594
595 def shouldFailTest(self, name, ref):
596 l = self.fail_tests.get(name, [])
597 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400598 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700599 return True
600 return False
601
602 def release(self, regex=None):
603 builds = self.running_builds[:]
604 self.log.debug("releasing build %s (%s)" % (regex,
605 len(self.running_builds)))
606 for build in builds:
607 if not regex or re.match(regex, build.name):
608 self.log.debug("releasing build %s" %
609 (build.parameters['ZUUL_UUID']))
610 build.release()
611 else:
612 self.log.debug("not releasing build %s" %
613 (build.parameters['ZUUL_UUID']))
614 self.log.debug("done releasing builds %s (%s)" %
615 (regex, len(self.running_builds)))
616
617
618class FakeGearmanServer(gear.Server):
619 def __init__(self):
620 self.hold_jobs_in_queue = False
621 super(FakeGearmanServer, self).__init__(0)
622
623 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700624 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
625 for job in queue:
626 if not hasattr(job, 'waiting'):
627 if job.name.startswith('build:'):
628 job.waiting = self.hold_jobs_in_queue
629 else:
630 job.waiting = False
631 if job.waiting:
632 continue
633 if job.name in connection.functions:
634 if not peek:
635 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400636 connection.related_jobs[job.handle] = job
637 job.worker_connection = connection
638 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700639 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700640 return None
641
642 def release(self, regex=None):
643 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700644 qlen = (len(self.high_queue) + len(self.normal_queue) +
645 len(self.low_queue))
646 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
647 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
648 queue = queue[:]
649 for job in queue:
650 cmd, name = job.name.split(':')
651 if cmd != 'build':
652 continue
653 if not regex or re.match(regex, name):
654 self.log.debug("releasing queued job %s" %
655 job.unique)
656 job.waiting = False
657 released = True
658 else:
659 self.log.debug("not releasing queued job %s" %
660 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700661 if released:
662 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700663 qlen = (len(self.high_queue) + len(self.normal_queue) +
664 len(self.low_queue))
665 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700666
667
Monty Taylorbc758832013-06-17 17:22:42 -0400668class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700669 log = logging.getLogger("zuul.test")
670
671 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400672 super(TestScheduler, self).setUp()
673 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
674 try:
675 test_timeout = int(test_timeout)
676 except ValueError:
677 # If timeout value is invalid do not set a timeout.
678 test_timeout = 0
679 if test_timeout > 0:
680 self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
681
682 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
683 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
684 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
685 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
686 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
687 os.environ.get('OS_STDERR_CAPTURE') == '1'):
688 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
689 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
690 self.useFixture(fixtures.NestedTempfile())
Monty Taylore6a6c402013-07-02 09:25:55 -0700691 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
692 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400693 self.useFixture(fixtures.FakeLogger(
694 level=logging.DEBUG,
695 format='%(asctime)s %(name)-32s '
696 '%(levelname)-8s %(message)s'))
697 tmp_root = tempfile.mkdtemp(dir=os.environ.get("ZUUL_TEST_ROOT",
698 '/tmp'))
Monty Taylorbc758832013-06-17 17:22:42 -0400699 self.test_root = os.path.join(tmp_root, "zuul-test")
700 self.upstream_root = os.path.join(self.test_root, "upstream")
701 self.git_root = os.path.join(self.test_root, "git")
702
703 CONFIG.set('zuul', 'git_dir', self.git_root)
704 if os.path.exists(self.test_root):
705 shutil.rmtree(self.test_root)
706 os.makedirs(self.test_root)
707 os.makedirs(self.upstream_root)
708 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700709
710 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400711 self.init_repo("org/project")
712 self.init_repo("org/project1")
713 self.init_repo("org/project2")
714 self.init_repo("org/project3")
715 self.init_repo("org/one-job-project")
716 self.init_repo("org/nonvoting-project")
717 self.init_repo("org/templated-project")
718 self.init_repo("org/node-project")
James E. Blair412e5582013-04-22 15:50:12 -0700719
720 self.statsd = FakeStatsd()
721 os.environ['STATSD_HOST'] = 'localhost'
722 os.environ['STATSD_PORT'] = str(self.statsd.port)
723 self.statsd.start()
724 # the statsd client object is configured in the statsd module import
725 reload(statsd)
726 reload(zuul.scheduler)
727
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700728 self.gearman_server = FakeGearmanServer()
729
730 self.config = ConfigParser.ConfigParser()
731 cfg = StringIO()
732 CONFIG.write(cfg)
733 cfg.seek(0)
734 self.config.readfp(cfg)
735 self.config.set('gearman', 'port', str(self.gearman_server.port))
736
Monty Taylorbc758832013-06-17 17:22:42 -0400737 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700738 self.worker.addServer('127.0.0.1', self.gearman_server.port)
739 self.gearman_server.worker = self.worker
740
James E. Blairb0fcae42012-07-17 11:12:10 -0700741 self.sched = zuul.scheduler.Scheduler()
742
James E. Blair8cc15a82012-08-01 11:17:57 -0700743 def URLOpenerFactory(*args, **kw):
744 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400745 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700746
James E. Blair8cc15a82012-08-01 11:17:57 -0700747 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700748 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700749
750 zuul.lib.gerrit.Gerrit = FakeGerrit
751
Monty Taylorbc758832013-06-17 17:22:42 -0400752 self.gerrit = FakeGerritTrigger(
753 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700754 self.gerrit.replication_timeout = 1.5
755 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700756 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400757 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700758
James E. Blair1843a552013-07-03 14:19:52 -0700759 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
760
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700761 self.sched.setLauncher(self.launcher)
James E. Blairb0fcae42012-07-17 11:12:10 -0700762 self.sched.setTrigger(self.gerrit)
763
764 self.sched.start()
765 self.sched.reconfigure(self.config)
766 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700767 self.webapp.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700768 self.launcher.gearman.waitForServer()
769 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400770 self.builds = self.worker.running_builds
771 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700772
773 def tearDown(self):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700774 self.launcher.stop()
775 self.worker.shutdown()
776 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700777 self.gerrit.stop()
778 self.sched.stop()
779 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700780 self.statsd.stop()
781 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700782 self.webapp.stop()
783 self.webapp.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700784 threads = threading.enumerate()
785 if len(threads) > 1:
786 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400787 super(TestScheduler, self).tearDown()
788
789 def init_repo(self, project):
790 parts = project.split('/')
791 path = os.path.join(self.upstream_root, *parts[:-1])
792 if not os.path.exists(path):
793 os.makedirs(path)
794 path = os.path.join(self.upstream_root, project)
795 repo = git.Repo.init(path)
796
797 repo.config_writer().set_value('user', 'email', 'user@example.com')
798 repo.config_writer().set_value('user', 'name', 'User Name')
799 repo.config_writer().write()
800
801 fn = os.path.join(path, 'README')
802 f = open(fn, 'w')
803 f.write("test\n")
804 f.close()
805 repo.index.add([fn])
806 repo.index.commit('initial commit')
807 master = repo.create_head('master')
808 repo.create_tag('init')
809
810 mp = repo.create_head('mp')
811 repo.head.reference = mp
812 f = open(fn, 'a')
813 f.write("test mp\n")
814 f.close()
815 repo.index.add([fn])
816 repo.index.commit('mp commit')
817
818 repo.head.reference = master
819 repo.head.reset(index=True, working_tree=True)
820 repo.git.clean('-x', '-f', '-d')
821
822 def ref_has_change(self, ref, change):
823 path = os.path.join(self.git_root, change.project)
824 repo = git.Repo(path)
825 for commit in repo.iter_commits(ref):
826 if commit.message.strip() == ('%s-1' % change.subject):
827 return True
828 return False
829
830 def job_has_changes(self, *args):
831 job = args[0]
832 commits = args[1:]
833 if isinstance(job, FakeBuild):
834 parameters = job.parameters
835 else:
836 parameters = json.loads(job.arguments)
837 project = parameters['ZUUL_PROJECT']
838 path = os.path.join(self.git_root, project)
839 repo = git.Repo(path)
840 ref = parameters['ZUUL_REF']
841 sha = parameters['ZUUL_COMMIT']
842 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
843 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
844 commit_messages = ['%s-1' % commit.subject for commit in commits]
845 for msg in commit_messages:
846 if msg not in repo_messages:
847 return False
848 if repo_shas[0] != sha:
849 return False
850 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700851
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700852 def registerJobs(self):
853 count = 0
854 for job in self.sched.jobs.keys():
855 self.worker.registerFunction('build:' + job)
856 count += 1
857 self.worker.registerFunction('stop:' + self.worker.worker_id)
858 count += 1
859
860 while len(self.gearman_server.functions) < count:
861 time.sleep(0)
862
863 def release(self, job):
864 if isinstance(job, FakeBuild):
865 job.release()
866 else:
867 job.waiting = False
868 self.log.debug("Queued job %s released" % job.unique)
869 self.gearman_server.wakeConnections()
870
871 def getParameter(self, job, name):
872 if isinstance(job, FakeBuild):
873 return job.parameters[name]
874 else:
875 parameters = json.loads(job.arguments)
876 return parameters[name]
877
878 def resetGearmanServer(self):
879 self.worker.setFunctions([])
880 while True:
881 done = True
882 for connection in self.gearman_server.active_connections:
883 if connection.functions:
884 done = False
885 if done:
886 break
887 time.sleep(0)
888 self.gearman_server.functions = set()
889
890 def haveAllBuildsReported(self):
891 # See if Zuul is waiting on a meta job to complete
892 if self.launcher.meta_jobs:
893 return False
894 # Find out if every build that the worker has completed has been
895 # reported back to Zuul. If it hasn't then that means a Gearman
896 # event is still in transit and the system is not stable.
897 for build in self.worker.build_history:
898 zbuild = self.launcher.builds.get(build.uuid)
899 if not zbuild:
900 # It has already been reported
901 continue
902 # It hasn't been reported yet.
903 return False
904 # Make sure that none of the worker connections are in GRAB_WAIT
905 for connection in self.worker.active_connections:
906 if connection.state == 'GRAB_WAIT':
907 return False
908 return True
909
910 def areAllBuildsWaiting(self):
911 ret = True
912
913 builds = self.launcher.builds.values()
914 for build in builds:
915 client_job = None
916 for conn in self.launcher.gearman.active_connections:
917 for j in conn.related_jobs.values():
918 if j.unique == build.uuid:
919 client_job = j
920 break
921 if not client_job:
922 self.log.debug("%s is not known to the gearman client" %
923 build)
924 ret = False
925 continue
926 if not client_job.handle:
927 self.log.debug("%s has no handle" % client_job)
928 ret = False
929 continue
930 server_job = self.gearman_server.jobs.get(client_job.handle)
931 if not server_job:
932 self.log.debug("%s is not known to the gearman server" %
933 client_job)
934 ret = False
935 continue
936 if not hasattr(server_job, 'waiting'):
937 self.log.debug("%s is being enqueued" % server_job)
938 ret = False
939 continue
940 if server_job.waiting:
941 continue
942 worker_job = self.worker.gearman_jobs.get(server_job.unique)
943 if worker_job:
944 if worker_job.build.isWaiting():
945 continue
946 else:
947 self.log.debug("%s is running" % worker_job)
948 ret = False
949 else:
950 self.log.debug("%s is unassigned" % server_job)
951 ret = False
952 return ret
953
James E. Blairb0fcae42012-07-17 11:12:10 -0700954 def waitUntilSettled(self):
955 self.log.debug("Waiting until settled...")
956 start = time.time()
957 while True:
958 if time.time() - start > 10:
959 print 'queue status:',
960 print self.sched.trigger_event_queue.empty(),
961 print self.sched.result_event_queue.empty(),
962 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700963 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -0700964 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700965 # Make sure no new events show up while we're checking
966 self.worker.lock.acquire()
967 # have all build states propogated to zuul?
968 if self.haveAllBuildsReported():
969 # Join ensures that the queue is empty _and_ events have been
970 # processed
971 self.fake_gerrit.event_queue.join()
972 self.sched.trigger_event_queue.join()
973 self.sched.result_event_queue.join()
974 if (self.sched.trigger_event_queue.empty() and
975 self.sched.result_event_queue.empty() and
976 self.fake_gerrit.event_queue.empty() and
977 self.areAllBuildsWaiting()):
978 self.worker.lock.release()
979 self.log.debug("...settled.")
980 return
981 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700982 self.sched.wake_event.wait(0.1)
983
James E. Blaird466dc42012-07-31 10:42:56 -0700984 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -0800985 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -0700986 return len(jobs)
987
James E. Blair4ca985f2013-05-30 12:27:43 -0700988 def getJobFromHistory(self, name):
989 history = self.worker.build_history
990 for job in history:
991 if job.name == name:
992 return job
993 raise Exception("Unable to find job %s in history" % name)
994
James E. Blaire0487072012-08-29 17:38:31 -0700995 def assertEmptyQueues(self):
996 # Make sure there are no orphaned jobs
997 for pipeline in self.sched.pipelines.values():
998 for queue in pipeline.queues:
999 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001000 print 'pipeline %s queue %s contents %s' % (
1001 pipeline.name, queue.name, queue.queue)
James E. Blaire0487072012-08-29 17:38:31 -07001002 assert len(queue.queue) == 0
1003 if len(queue.severed_heads) != 0:
1004 print 'heads', queue.severed_heads
1005 assert len(queue.severed_heads) == 0
1006
James E. Blair412e5582013-04-22 15:50:12 -07001007 def assertReportedStat(self, key, value=None):
1008 start = time.time()
1009 while time.time() < (start + 5):
1010 for stat in self.statsd.stats:
1011 k, v = stat.split(':')
1012 if key == k:
1013 if value is None:
1014 return
1015 if value == v:
1016 return
1017 time.sleep(0.1)
1018
1019 pprint.pprint(self.statsd.stats)
1020 raise Exception("Key %s not found in reported stats" % key)
1021
James E. Blairb0fcae42012-07-17 11:12:10 -07001022 def test_jobs_launched(self):
1023 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001024
James E. Blairb0fcae42012-07-17 11:12:10 -07001025 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001026 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001027 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1028 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001029 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1030 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1031 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blairb0fcae42012-07-17 11:12:10 -07001032 assert A.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -07001033 assert A.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001034 self.assertEmptyQueues()
James E. Blairb0fcae42012-07-17 11:12:10 -07001035
James E. Blair412e5582013-04-22 15:50:12 -07001036 self.assertReportedStat('gerrit.event.comment-added', '1|c')
1037 self.assertReportedStat('zuul.pipeline.gate.current_changes', '1|g')
1038 self.assertReportedStat('zuul.job.project-merge')
1039 self.assertReportedStat('zuul.pipeline.gate.resident_time')
1040 self.assertReportedStat('zuul.pipeline.gate.total_changes', '1|c')
1041 self.assertReportedStat(
1042 'zuul.pipeline.gate.org.project.resident_time')
1043 self.assertReportedStat(
1044 'zuul.pipeline.gate.org.project.total_changes', '1|c')
1045
James E. Blair42f74822013-05-14 15:18:03 -07001046 def test_duplicate_pipelines(self):
1047 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001048
James E. Blair42f74822013-05-14 15:18:03 -07001049 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1050 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1051 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001052
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001053 print self.builds
James E. Blair42f74822013-05-14 15:18:03 -07001054 print A.messages
1055
1056 self.assertEmptyQueues()
1057
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001058 assert len(self.history) == 2
1059 self.history[0].name == 'project-test1'
1060 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001061
1062 assert len(A.messages) == 2
1063 if 'dup1/project-test1' in A.messages[0]:
1064 assert 'dup1/project-test1' in A.messages[0]
1065 assert 'dup2/project-test1' not in A.messages[0]
1066 assert 'dup1/project-test1' not in A.messages[1]
1067 assert 'dup2/project-test1' in A.messages[1]
1068 else:
1069 assert 'dup1/project-test1' in A.messages[1]
1070 assert 'dup2/project-test1' not in A.messages[1]
1071 assert 'dup1/project-test1' not in A.messages[0]
1072 assert 'dup2/project-test1' in A.messages[0]
1073
James E. Blairb0fcae42012-07-17 11:12:10 -07001074 def test_parallel_changes(self):
1075 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001076
1077 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001078 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1079 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1080 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001081 A.addApproval('CRVW', 2)
1082 B.addApproval('CRVW', 2)
1083 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001084
1085 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1086 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1087 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1088
1089 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001090 assert len(self.builds) == 1
1091 assert self.builds[0].name == 'project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001092 assert self.job_has_changes(self.builds[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -07001093
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001094 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001095 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001096 assert len(self.builds) == 3
1097 assert self.builds[0].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001098 assert self.job_has_changes(self.builds[0], A)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001099 assert self.builds[1].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001100 assert self.job_has_changes(self.builds[1], A)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001101 assert self.builds[2].name == 'project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001102 assert self.job_has_changes(self.builds[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -07001103
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001104 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001105 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001106 assert len(self.builds) == 5
1107 assert self.builds[0].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001108 assert self.job_has_changes(self.builds[0], A)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001109 assert self.builds[1].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001110 assert self.job_has_changes(self.builds[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -07001111
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001112 assert self.builds[2].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001113 assert self.job_has_changes(self.builds[2], A, B)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001114 assert self.builds[3].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001115 assert self.job_has_changes(self.builds[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -07001116
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001117 assert self.builds[4].name == 'project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001118 assert self.job_has_changes(self.builds[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -07001119
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001120 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001121 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001122 assert len(self.builds) == 6
1123 assert self.builds[0].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001124 assert self.job_has_changes(self.builds[0], A)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001125 assert self.builds[1].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001126 assert self.job_has_changes(self.builds[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -07001127
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001128 assert self.builds[2].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001129 assert self.job_has_changes(self.builds[2], A, B)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001130 assert self.builds[3].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001131 assert self.job_has_changes(self.builds[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -07001132
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001133 assert self.builds[4].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001134 assert self.job_has_changes(self.builds[4], A, B, C)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001135 assert self.builds[5].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001136 assert self.job_has_changes(self.builds[5], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -07001137
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001138 self.worker.hold_jobs_in_build = False
1139 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001140 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001141 assert len(self.builds) == 0
James E. Blairb0fcae42012-07-17 11:12:10 -07001142
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001143 assert len(self.history) == 9
James E. Blairb0fcae42012-07-17 11:12:10 -07001144 assert A.data['status'] == 'MERGED'
1145 assert B.data['status'] == 'MERGED'
1146 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -07001147 assert A.reported == 2
1148 assert B.reported == 2
1149 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001150 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001151
1152 def test_failed_changes(self):
1153 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001154 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001155
James E. Blairb02a3bb2012-07-30 17:49:55 -07001156 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1157 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001158 A.addApproval('CRVW', 2)
1159 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001160
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001161 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001162
James E. Blaire2819012013-06-28 17:17:26 -04001163 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1164 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001165 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001166
1167 self.worker.release('.*-merge')
1168 self.waitUntilSettled()
1169
1170 self.worker.hold_jobs_in_build = False
1171 self.worker.release()
1172
1173 self.waitUntilSettled()
1174 # It's certain that the merge job for change 2 will run, but
1175 # the test1 and test2 jobs may or may not run.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001176 assert len(self.history) > 6
James E. Blairb02a3bb2012-07-30 17:49:55 -07001177 assert A.data['status'] == 'NEW'
1178 assert B.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -07001179 assert A.reported == 2
1180 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001181 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001182
1183 def test_independent_queues(self):
1184 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001185
1186 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001187 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001188 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1189 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001190 A.addApproval('CRVW', 2)
1191 B.addApproval('CRVW', 2)
1192 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001193
1194 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1195 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1196 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1197
James E. Blairb02a3bb2012-07-30 17:49:55 -07001198 self.waitUntilSettled()
1199
1200 # There should be one merge job at the head of each queue running
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001201 assert len(self.builds) == 2
1202 assert self.builds[0].name == 'project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001203 assert self.job_has_changes(self.builds[0], A)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001204 assert self.builds[1].name == 'project1-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001205 assert self.job_has_changes(self.builds[1], B)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001206
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001207 # Release the current merge builds
1208 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001209 self.waitUntilSettled()
1210 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001211 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001212 self.waitUntilSettled()
1213
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001214 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001215 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001216 assert len(self.builds) == 8
James E. Blairb02a3bb2012-07-30 17:49:55 -07001217
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001218 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001219 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001220 assert len(self.builds) == 0
James E. Blairb02a3bb2012-07-30 17:49:55 -07001221
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001222 assert len(self.history) == 11
James E. Blairb02a3bb2012-07-30 17:49:55 -07001223 assert A.data['status'] == 'MERGED'
1224 assert B.data['status'] == 'MERGED'
1225 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -07001226 assert A.reported == 2
1227 assert B.reported == 2
1228 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001229 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -07001230
1231 def test_failed_change_at_head(self):
1232 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001233
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001234 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001235 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1236 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1237 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001238 A.addApproval('CRVW', 2)
1239 B.addApproval('CRVW', 2)
1240 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001241
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001242 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001243
1244 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1245 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1246 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1247
1248 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001249
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001250 assert len(self.builds) == 1
1251 assert self.builds[0].name == 'project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001252 assert self.job_has_changes(self.builds[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -07001253
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001254 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001255 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001256 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001257 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001258 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001259 self.waitUntilSettled()
1260
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001261 assert len(self.builds) == 6
1262 assert self.builds[0].name == 'project-test1'
1263 assert self.builds[1].name == 'project-test2'
1264 assert self.builds[2].name == 'project-test1'
1265 assert self.builds[3].name == 'project-test2'
1266 assert self.builds[4].name == 'project-test1'
1267 assert self.builds[5].name == 'project-test2'
James E. Blaird466dc42012-07-31 10:42:56 -07001268
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001269 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001270 self.waitUntilSettled()
1271
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001272 assert len(self.builds) == 2 # project-test2, project-merge for B
1273 assert self.countJobResults(self.history, 'ABORTED') == 4
James E. Blaird466dc42012-07-31 10:42:56 -07001274
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001275 self.worker.hold_jobs_in_build = False
1276 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001277 self.waitUntilSettled()
1278
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001279 assert len(self.builds) == 0
1280 assert len(self.history) == 15
James E. Blaird466dc42012-07-31 10:42:56 -07001281 assert A.data['status'] == 'NEW'
1282 assert B.data['status'] == 'MERGED'
1283 assert C.data['status'] == 'MERGED'
1284 assert A.reported == 2
1285 assert B.reported == 2
1286 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001287 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -07001288
1289 def test_failed_change_at_head_with_queue(self):
1290 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001291
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001292 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001293 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1294 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1295 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001296 A.addApproval('CRVW', 2)
1297 B.addApproval('CRVW', 2)
1298 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001299
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001300 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001301
1302 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1303 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1304 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1305
1306 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001307 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001308 assert len(self.builds) == 0
James E. Blaird466dc42012-07-31 10:42:56 -07001309 assert len(queue) == 1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001310 assert queue[0].name == 'build:project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001311 assert self.job_has_changes(queue[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -07001312
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001313 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001314 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001315 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001316 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001317 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001318 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001319 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001320
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001321 assert len(self.builds) == 0
James E. Blaird466dc42012-07-31 10:42:56 -07001322 assert len(queue) == 6
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001323 assert queue[0].name == 'build:project-test1'
1324 assert queue[1].name == 'build:project-test2'
1325 assert queue[2].name == 'build:project-test1'
1326 assert queue[3].name == 'build:project-test2'
1327 assert queue[4].name == 'build:project-test1'
1328 assert queue[5].name == 'build:project-test2'
James E. Blaird466dc42012-07-31 10:42:56 -07001329
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001330 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001331 self.waitUntilSettled()
1332
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001333 assert len(self.builds) == 0
James E. Blair701c5b42013-06-06 09:34:59 -07001334 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001335 assert len(queue) == 2 # project-test2, project-merge for B
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001336 assert self.countJobResults(self.history, 'ABORTED') == 0
James E. Blaird466dc42012-07-31 10:42:56 -07001337
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001338 self.gearman_server.hold_jobs_in_queue = False
1339 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001340 self.waitUntilSettled()
1341
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001342 assert len(self.builds) == 0
1343 assert len(self.history) == 11
James E. Blaird466dc42012-07-31 10:42:56 -07001344 assert A.data['status'] == 'NEW'
1345 assert B.data['status'] == 'MERGED'
1346 assert C.data['status'] == 'MERGED'
1347 assert A.reported == 2
1348 assert B.reported == 2
1349 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001350 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001351
1352 def test_patch_order(self):
1353 "Test that dependent patches are tested in the right order"
1354 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1355 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1356 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1357 A.addApproval('CRVW', 2)
1358 B.addApproval('CRVW', 2)
1359 C.addApproval('CRVW', 2)
1360
1361 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1362 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1363 M2.setMerged()
1364 M1.setMerged()
1365
1366 # C -> B -> A -> M1 -> M2
1367 # M2 is here to make sure it is never queried. If it is, it
1368 # means zuul is walking down the entire history of merged
1369 # changes.
1370
1371 C.setDependsOn(B, 1)
1372 B.setDependsOn(A, 1)
1373 A.setDependsOn(M1, 1)
1374 M1.setDependsOn(M2, 1)
1375
1376 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1377
1378 self.waitUntilSettled()
1379
1380 assert A.data['status'] == 'NEW'
1381 assert B.data['status'] == 'NEW'
1382 assert C.data['status'] == 'NEW'
1383
1384 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1385 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1386
1387 self.waitUntilSettled()
1388 assert M2.queried == 0
1389 assert A.data['status'] == 'MERGED'
1390 assert B.data['status'] == 'MERGED'
1391 assert C.data['status'] == 'MERGED'
1392 assert A.reported == 2
1393 assert B.reported == 2
1394 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001395 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001396
1397 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001398 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001399 # TODO: move to test_gerrit (this is a unit test!)
1400 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4aea70c2012-07-26 14:23:24 -07001401 a = self.sched.trigger.getChange(1, 2)
1402 mgr = self.sched.pipelines['gate'].manager
1403 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001404
1405 A.addApproval('CRVW', 2)
James E. Blair4aea70c2012-07-26 14:23:24 -07001406 a = self.sched.trigger.getChange(1, 2)
1407 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001408
1409 A.addApproval('APRV', 1)
James E. Blair4aea70c2012-07-26 14:23:24 -07001410 a = self.sched.trigger.getChange(1, 2)
1411 assert self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blaire0487072012-08-29 17:38:31 -07001412 self.assertEmptyQueues()
James E. Blair4886cc12012-07-18 15:39:41 -07001413
1414 def test_build_configuration(self):
1415 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001416
1417 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001418 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1419 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1420 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1421 A.addApproval('CRVW', 2)
1422 B.addApproval('CRVW', 2)
1423 C.addApproval('CRVW', 2)
1424 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1425 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1426 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1427 self.waitUntilSettled()
1428
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001429 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001430 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001431 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001432 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001433 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001434 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001435 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001436 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1437 self.gearman_server.hold_jobs_in_queue = False
1438 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001439 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001440
Monty Taylorbc758832013-06-17 17:22:42 -04001441 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001442 repo = git.Repo(path)
1443 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1444 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001445 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
1446 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001447 self.assertEmptyQueues()
James E. Blair973721f2012-08-15 10:19:43 -07001448
1449 def test_build_configuration_conflict(self):
1450 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001451
1452 self.gearman_server.hold_jobs_in_queue = True
James E. Blair973721f2012-08-15 10:19:43 -07001453 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1454 A.addPatchset(['conflict'])
1455 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1456 B.addPatchset(['conflict'])
1457 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1458 A.addApproval('CRVW', 2)
1459 B.addApproval('CRVW', 2)
1460 C.addApproval('CRVW', 2)
1461 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1462 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1463 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1464 self.waitUntilSettled()
1465
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001466 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001467 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001468 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001469 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001470 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001471 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001472 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001473 self.getParameter(queue[-1], 'ZUUL_REF')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001474 self.gearman_server.hold_jobs_in_queue = False
1475 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001476 self.waitUntilSettled()
1477
1478 assert A.data['status'] == 'MERGED'
1479 assert B.data['status'] == 'NEW'
1480 assert C.data['status'] == 'MERGED'
1481 assert A.reported == 2
1482 assert B.reported == 2
1483 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001484 self.assertEmptyQueues()
James E. Blairdaabed22012-08-15 15:38:57 -07001485
1486 def test_post(self):
1487 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001488
Zhongyue Luo5d556072012-09-21 02:00:47 +09001489 e = {
1490 "type": "ref-updated",
1491 "submitter": {
1492 "name": "User Name",
1493 },
1494 "refUpdate": {
1495 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1496 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1497 "refName": "master",
1498 "project": "org/project",
1499 }
1500 }
James E. Blairdaabed22012-08-15 15:38:57 -07001501 self.fake_gerrit.addEvent(e)
1502 self.waitUntilSettled()
1503
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001504 job_names = [x.name for x in self.history]
1505 assert len(self.history) == 1
James E. Blairdaabed22012-08-15 15:38:57 -07001506 assert 'project-post' in job_names
James E. Blaire0487072012-08-29 17:38:31 -07001507 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001508
1509 def test_build_configuration_branch(self):
1510 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001511
1512 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001513 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1514 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1515 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1516 A.addApproval('CRVW', 2)
1517 B.addApproval('CRVW', 2)
1518 C.addApproval('CRVW', 2)
1519 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1520 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1521 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1522 self.waitUntilSettled()
1523
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001524 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001525 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001526 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001527 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001528 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001529 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001530 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001531 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1532 self.gearman_server.hold_jobs_in_queue = False
1533 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001534 self.waitUntilSettled()
1535
Monty Taylorbc758832013-06-17 17:22:42 -04001536 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001537 repo = git.Repo(path)
1538 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1539 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001540 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
1541 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001542 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001543
1544 def test_build_configuration_branch_interaction(self):
1545 "Test that switching between branches works"
1546 self.test_build_configuration()
1547 self.test_build_configuration_branch()
1548 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001549 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001550 repo = git.Repo(path)
1551 repo.heads.master.commit = repo.commit('init')
1552 self.test_build_configuration()
James E. Blaire0487072012-08-29 17:38:31 -07001553 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001554
1555 def test_build_configuration_multi_branch(self):
1556 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001557
1558 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001559 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1560 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1561 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1562 A.addApproval('CRVW', 2)
1563 B.addApproval('CRVW', 2)
1564 C.addApproval('CRVW', 2)
1565 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1566 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1567 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1568 self.waitUntilSettled()
1569
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001570 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001571 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001572 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001573 ref_mp = self.getParameter(queue[-1], 'ZUUL_REF')
1574 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001575 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001576 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001577 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001578 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001579 ref_master = self.getParameter(queue[-1], 'ZUUL_REF')
1580 self.gearman_server.hold_jobs_in_queue = False
1581 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001582 self.waitUntilSettled()
1583
Monty Taylorbc758832013-06-17 17:22:42 -04001584 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001585 repo = git.Repo(path)
1586
1587 repo_messages = [c.message.strip()
1588 for c in repo.iter_commits(ref_master)]
1589 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001590 correct_messages = ['initial commit', 'A-1', 'C-1']
1591 assert repo_messages == correct_messages
1592
1593 repo_messages = [c.message.strip()
1594 for c in repo.iter_commits(ref_mp)]
1595 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001596 correct_messages = ['initial commit', 'mp commit', 'B-1']
1597 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001598 self.assertEmptyQueues()
James E. Blair7f71c802012-08-22 13:04:32 -07001599
1600 def test_one_job_project(self):
1601 "Test that queueing works with one job"
1602 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1603 'master', 'A')
1604 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1605 'master', 'B')
1606 A.addApproval('CRVW', 2)
1607 B.addApproval('CRVW', 2)
1608 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1609 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1610 self.waitUntilSettled()
1611
James E. Blair7f71c802012-08-22 13:04:32 -07001612 assert A.data['status'] == 'MERGED'
1613 assert A.reported == 2
1614 assert B.data['status'] == 'MERGED'
1615 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001616 self.assertEmptyQueues()
James E. Blaircaec0c52012-08-22 14:52:22 -07001617
Antoine Musso80edd5a2013-02-13 15:37:53 +01001618 def test_job_from_templates_launched(self):
1619 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001620
Antoine Musso80edd5a2013-02-13 15:37:53 +01001621 A = self.fake_gerrit.addFakeChange(
1622 'org/templated-project', 'master', 'A')
1623 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1624 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001625
James E. Blair4ca985f2013-05-30 12:27:43 -07001626 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1627 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
Antoine Musso80edd5a2013-02-13 15:37:53 +01001628
James E. Blaircaec0c52012-08-22 14:52:22 -07001629 def test_dependent_changes_dequeue(self):
1630 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001631
James E. Blaircaec0c52012-08-22 14:52:22 -07001632 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1633 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1634 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1635 A.addApproval('CRVW', 2)
1636 B.addApproval('CRVW', 2)
1637 C.addApproval('CRVW', 2)
1638
1639 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1640 M1.setMerged()
1641
1642 # C -> B -> A -> M1
1643
1644 C.setDependsOn(B, 1)
1645 B.setDependsOn(A, 1)
1646 A.setDependsOn(M1, 1)
1647
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001648 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001649
1650 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1651 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1652 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1653
1654 self.waitUntilSettled()
1655
James E. Blairec590122012-08-22 15:19:31 -07001656 assert A.data['status'] == 'NEW'
1657 assert A.reported == 2
1658 assert B.data['status'] == 'NEW'
1659 assert B.reported == 2
1660 assert C.data['status'] == 'NEW'
1661 assert C.reported == 2
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001662 assert len(self.history) == 1
James E. Blaire0487072012-08-29 17:38:31 -07001663 self.assertEmptyQueues()
James E. Blairec590122012-08-22 15:19:31 -07001664
1665 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001666 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001667 # If it's dequeued more than once, we should see extra
1668 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001669
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001670 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001671 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1672 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1673 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1674 A.addApproval('CRVW', 2)
1675 B.addApproval('CRVW', 2)
1676 C.addApproval('CRVW', 2)
1677
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001678 self.worker.addFailTest('project1-test1', A)
1679 self.worker.addFailTest('project1-test2', A)
1680 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001681
1682 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1683 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1684 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1685
1686 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001687
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001688 assert len(self.builds) == 1
1689 assert self.builds[0].name == 'project1-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001690 assert self.job_has_changes(self.builds[0], A)
James E. Blairec590122012-08-22 15:19:31 -07001691
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001692 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001693 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001694 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001695 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001696 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001697 self.waitUntilSettled()
1698
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001699 assert len(self.builds) == 9
1700 assert self.builds[0].name == 'project1-test1'
1701 assert self.builds[1].name == 'project1-test2'
1702 assert self.builds[2].name == 'project1-project2-integration'
1703 assert self.builds[3].name == 'project1-test1'
1704 assert self.builds[4].name == 'project1-test2'
1705 assert self.builds[5].name == 'project1-project2-integration'
1706 assert self.builds[6].name == 'project1-test1'
1707 assert self.builds[7].name == 'project1-test2'
1708 assert self.builds[8].name == 'project1-project2-integration'
James E. Blairec590122012-08-22 15:19:31 -07001709
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001710 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001711 self.waitUntilSettled()
1712
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001713 assert len(self.builds) == 3 # test2, integration, merge for B
1714 assert self.countJobResults(self.history, 'ABORTED') == 6
James E. Blairec590122012-08-22 15:19:31 -07001715
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001716 self.worker.hold_jobs_in_build = False
1717 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001718 self.waitUntilSettled()
1719
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001720 assert len(self.builds) == 0
1721 assert len(self.history) == 20
James E. Blaircaec0c52012-08-22 14:52:22 -07001722
1723 assert A.data['status'] == 'NEW'
James E. Blairec590122012-08-22 15:19:31 -07001724 assert B.data['status'] == 'MERGED'
1725 assert C.data['status'] == 'MERGED'
1726 assert A.reported == 2
1727 assert B.reported == 2
1728 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001729 self.assertEmptyQueues()
James E. Blair4ec821f2012-08-23 15:28:28 -07001730
1731 def test_nonvoting_job(self):
1732 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001733
James E. Blair4ec821f2012-08-23 15:28:28 -07001734 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1735 'master', 'A')
1736 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001737 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001738 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1739
1740 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001741
1742 assert A.data['status'] == 'MERGED'
1743 assert A.reported == 2
James E. Blair4ca985f2013-05-30 12:27:43 -07001744 assert (self.getJobFromHistory('nonvoting-project-merge').result ==
1745 'SUCCESS')
1746 assert (self.getJobFromHistory('nonvoting-project-test1').result ==
1747 'SUCCESS')
1748 assert (self.getJobFromHistory('nonvoting-project-test2').result ==
1749 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001750 self.assertEmptyQueues()
1751
1752 def test_check_queue_success(self):
1753 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001754
James E. Blaire0487072012-08-29 17:38:31 -07001755 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1756 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1757
1758 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001759
1760 assert A.data['status'] == 'NEW'
1761 assert A.reported == 1
James E. Blair4ca985f2013-05-30 12:27:43 -07001762 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1763 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1764 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blaire0487072012-08-29 17:38:31 -07001765 self.assertEmptyQueues()
1766
1767 def test_check_queue_failure(self):
1768 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001769
James E. Blaire0487072012-08-29 17:38:31 -07001770 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001771 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001772 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1773
1774 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001775
1776 assert A.data['status'] == 'NEW'
1777 assert A.reported == 1
James E. Blair4ca985f2013-05-30 12:27:43 -07001778 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1779 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1780 assert self.getJobFromHistory('project-test2').result == 'FAILURE'
James E. Blaire0487072012-08-29 17:38:31 -07001781 self.assertEmptyQueues()
James E. Blair127bc182012-08-28 15:55:15 -07001782
1783 def test_dependent_behind_dequeue(self):
1784 "test that dependent changes behind dequeued changes work"
1785 # This complicated test is a reproduction of a real life bug
1786 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001787
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001788 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001789 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1790 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1791 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1792 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1793 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1794 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1795 D.setDependsOn(C, 1)
1796 E.setDependsOn(D, 1)
1797 A.addApproval('CRVW', 2)
1798 B.addApproval('CRVW', 2)
1799 C.addApproval('CRVW', 2)
1800 D.addApproval('CRVW', 2)
1801 E.addApproval('CRVW', 2)
1802 F.addApproval('CRVW', 2)
1803
1804 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001805
1806 # Change object re-use in the gerrit trigger is hidden if
1807 # changes are added in quick succession; waiting makes it more
1808 # like real life.
1809 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1810 self.waitUntilSettled()
1811 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1812 self.waitUntilSettled()
1813
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001814 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001815 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001816 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001817 self.waitUntilSettled()
1818
1819 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1820 self.waitUntilSettled()
1821 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1822 self.waitUntilSettled()
1823 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1824 self.waitUntilSettled()
1825 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1826 self.waitUntilSettled()
1827
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001828 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001829 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001830 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001831 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001832 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001833 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001834 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001835 self.waitUntilSettled()
1836
1837 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001838
1839 # Grab pointers to the jobs we want to release before
1840 # releasing any, because list indexes may change as
1841 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001842 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001843 a.release()
1844 b.release()
1845 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001846 self.waitUntilSettled()
1847
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001848 self.worker.hold_jobs_in_build = False
1849 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07001850 self.waitUntilSettled()
1851
James E. Blair127bc182012-08-28 15:55:15 -07001852 assert A.data['status'] == 'NEW'
1853 assert B.data['status'] == 'MERGED'
1854 assert C.data['status'] == 'MERGED'
1855 assert D.data['status'] == 'MERGED'
1856 assert E.data['status'] == 'MERGED'
1857 assert F.data['status'] == 'MERGED'
1858
1859 assert A.reported == 2
1860 assert B.reported == 2
1861 assert C.reported == 2
1862 assert D.reported == 2
1863 assert E.reported == 2
1864 assert F.reported == 2
1865
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001866 assert self.countJobResults(self.history, 'ABORTED') == 15
1867 assert len(self.history) == 44
James E. Blaire0487072012-08-29 17:38:31 -07001868 self.assertEmptyQueues()
James E. Blair05fed602012-09-07 12:45:24 -07001869
1870 def test_merger_repack(self):
1871 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001872
James E. Blair05fed602012-09-07 12:45:24 -07001873 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1874 A.addApproval('CRVW', 2)
1875 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1876 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001877 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1878 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1879 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blair05fed602012-09-07 12:45:24 -07001880 assert A.data['status'] == 'MERGED'
1881 assert A.reported == 2
1882 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07001883 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001884
Monty Taylorbc758832013-06-17 17:22:42 -04001885 path = os.path.join(self.git_root, "org/project")
1886 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001887
1888 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1889 A.addApproval('CRVW', 2)
1890 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1891 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001892 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1893 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1894 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blair05fed602012-09-07 12:45:24 -07001895 assert A.data['status'] == 'MERGED'
1896 assert A.reported == 2
1897 self.assertEmptyQueues()
James E. Blair7ee88a22012-09-12 18:59:31 +02001898
James E. Blair4886f282012-11-15 09:27:33 -08001899 def test_merger_repack_large_change(self):
1900 "Test that the merger works with large changes after a repack"
1901 # https://bugs.launchpad.net/zuul/+bug/1078946
1902 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1903 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001904 path = os.path.join(self.upstream_root, "org/project1")
1905 print repack_repo(path)
1906 path = os.path.join(self.git_root, "org/project1")
1907 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001908
1909 A.addApproval('CRVW', 2)
1910 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1911 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001912 assert self.getJobFromHistory('project1-merge').result == 'SUCCESS'
1913 assert self.getJobFromHistory('project1-test1').result == 'SUCCESS'
1914 assert self.getJobFromHistory('project1-test2').result == 'SUCCESS'
James E. Blair4886f282012-11-15 09:27:33 -08001915 assert A.data['status'] == 'MERGED'
1916 assert A.reported == 2
1917 self.assertEmptyQueues()
1918
James E. Blair7ee88a22012-09-12 18:59:31 +02001919 def test_nonexistent_job(self):
1920 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001921 # Set to the state immediately after a restart
1922 self.resetGearmanServer()
1923 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02001924
1925 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1926 A.addApproval('CRVW', 2)
1927 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1928 # There may be a thread about to report a lost change
1929 while A.reported < 2:
1930 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001931 job_names = [x.name for x in self.history]
James E. Blair7ee88a22012-09-12 18:59:31 +02001932 assert not job_names
1933 assert A.data['status'] == 'NEW'
1934 assert A.reported == 2
1935 self.assertEmptyQueues()
1936
1937 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001938 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02001939 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1940 A.addApproval('CRVW', 2)
1941 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1942 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001943 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1944 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1945 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blair7ee88a22012-09-12 18:59:31 +02001946 assert A.data['status'] == 'MERGED'
1947 assert A.reported == 2
1948 self.assertEmptyQueues()
James E. Blairf62d4282012-12-31 17:01:50 -08001949
1950 def test_single_nonexistent_post_job(self):
1951 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08001952 e = {
1953 "type": "ref-updated",
1954 "submitter": {
1955 "name": "User Name",
1956 },
1957 "refUpdate": {
1958 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1959 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1960 "refName": "master",
1961 "project": "org/project",
1962 }
1963 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001964 # Set to the state immediately after a restart
1965 self.resetGearmanServer()
1966 self.launcher.negative_function_cache_ttl = 0
1967
James E. Blairf62d4282012-12-31 17:01:50 -08001968 self.fake_gerrit.addEvent(e)
1969 self.waitUntilSettled()
1970
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001971 assert len(self.history) == 0
James E. Blairf62d4282012-12-31 17:01:50 -08001972 self.assertEmptyQueues()
James E. Blair2fa50962013-01-30 21:50:41 -08001973
1974 def test_new_patchset_dequeues_old(self):
1975 "Test that a new patchset causes the old to be dequeued"
1976 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001977 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001978 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1979 M.setMerged()
1980
1981 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1982 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1983 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1984 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1985 A.addApproval('CRVW', 2)
1986 B.addApproval('CRVW', 2)
1987 C.addApproval('CRVW', 2)
1988 D.addApproval('CRVW', 2)
1989
1990 C.setDependsOn(B, 1)
1991 B.setDependsOn(A, 1)
1992 A.setDependsOn(M, 1)
1993
1994 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1995 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1996 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1997 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1998 self.waitUntilSettled()
1999
2000 B.addPatchset()
2001 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2002 self.waitUntilSettled()
2003
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002004 self.worker.hold_jobs_in_build = False
2005 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002006 self.waitUntilSettled()
2007
James E. Blair2fa50962013-01-30 21:50:41 -08002008 assert A.data['status'] == 'MERGED'
2009 assert A.reported == 2
2010 assert B.data['status'] == 'NEW'
2011 assert B.reported == 2
2012 assert C.data['status'] == 'NEW'
2013 assert C.reported == 2
2014 assert D.data['status'] == 'MERGED'
2015 assert D.reported == 2
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002016 assert len(self.history) == 9 # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002017 self.assertEmptyQueues()
2018
2019 def test_new_patchset_dequeues_old_on_head(self):
2020 "Test that a new patchset causes the old to be dequeued (at head)"
2021 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002022 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002023 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2024 M.setMerged()
2025 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2026 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2027 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2028 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2029 A.addApproval('CRVW', 2)
2030 B.addApproval('CRVW', 2)
2031 C.addApproval('CRVW', 2)
2032 D.addApproval('CRVW', 2)
2033
2034 C.setDependsOn(B, 1)
2035 B.setDependsOn(A, 1)
2036 A.setDependsOn(M, 1)
2037
2038 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2039 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2040 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2041 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2042 self.waitUntilSettled()
2043
2044 A.addPatchset()
2045 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2046 self.waitUntilSettled()
2047
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002048 self.worker.hold_jobs_in_build = False
2049 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002050 self.waitUntilSettled()
2051
James E. Blair2fa50962013-01-30 21:50:41 -08002052 assert A.data['status'] == 'NEW'
2053 assert A.reported == 2
2054 assert B.data['status'] == 'NEW'
2055 assert B.reported == 2
2056 assert C.data['status'] == 'NEW'
2057 assert C.reported == 2
2058 assert D.data['status'] == 'MERGED'
2059 assert D.reported == 2
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002060 assert len(self.history) == 7
James E. Blair2fa50962013-01-30 21:50:41 -08002061 self.assertEmptyQueues()
2062
2063 def test_new_patchset_dequeues_old_without_dependents(self):
2064 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002065 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002066 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2067 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2068 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2069 A.addApproval('CRVW', 2)
2070 B.addApproval('CRVW', 2)
2071 C.addApproval('CRVW', 2)
2072
2073 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2074 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2075 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2076 self.waitUntilSettled()
2077
2078 B.addPatchset()
2079 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2080 self.waitUntilSettled()
2081
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002082 self.worker.hold_jobs_in_build = False
2083 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002084 self.waitUntilSettled()
2085
James E. Blair2fa50962013-01-30 21:50:41 -08002086 assert A.data['status'] == 'MERGED'
2087 assert A.reported == 2
2088 assert B.data['status'] == 'NEW'
2089 assert B.reported == 2
2090 assert C.data['status'] == 'MERGED'
2091 assert C.reported == 2
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002092 assert len(self.history) == 9
James E. Blair2fa50962013-01-30 21:50:41 -08002093 self.assertEmptyQueues()
2094
2095 def test_new_patchset_dequeues_old_independent_queue(self):
2096 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002097 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002098 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2099 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2100 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2101 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2102 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2103 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2104 self.waitUntilSettled()
2105
2106 B.addPatchset()
2107 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2108 self.waitUntilSettled()
2109
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002110 self.worker.hold_jobs_in_build = False
2111 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002112 self.waitUntilSettled()
2113
James E. Blair2fa50962013-01-30 21:50:41 -08002114 assert A.data['status'] == 'NEW'
2115 assert A.reported == 1
2116 assert B.data['status'] == 'NEW'
2117 assert B.reported == 1
2118 assert C.data['status'] == 'NEW'
2119 assert C.reported == 1
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002120 assert len(self.history) == 10
2121 assert self.countJobResults(self.history, 'ABORTED') == 1
James E. Blair2fa50962013-01-30 21:50:41 -08002122 self.assertEmptyQueues()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002123
2124 def test_zuul_refs(self):
2125 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002126 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002127 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2128 M1.setMerged()
2129 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2130 M2.setMerged()
2131
2132 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2133 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2134 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2135 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2136 A.addApproval('CRVW', 2)
2137 B.addApproval('CRVW', 2)
2138 C.addApproval('CRVW', 2)
2139 D.addApproval('CRVW', 2)
2140 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2141 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2142 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2143 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2144
2145 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002146 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002147 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002148 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002149 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002150 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002151 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002152 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002153 self.waitUntilSettled()
2154
James E. Blair7d0dedc2013-02-21 17:26:09 -08002155 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002156 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002157 if x.parameters['ZUUL_CHANGE'] == '3':
2158 a_zref = x.parameters['ZUUL_REF']
2159 if x.parameters['ZUUL_CHANGE'] == '4':
2160 b_zref = x.parameters['ZUUL_REF']
2161 if x.parameters['ZUUL_CHANGE'] == '5':
2162 c_zref = x.parameters['ZUUL_REF']
2163 if x.parameters['ZUUL_CHANGE'] == '6':
2164 d_zref = x.parameters['ZUUL_REF']
2165
2166 # There are... four... refs.
2167 assert a_zref is not None
2168 assert b_zref is not None
2169 assert c_zref is not None
2170 assert d_zref is not None
2171
2172 # And they should all be different
2173 refs = set([a_zref, b_zref, c_zref, d_zref])
2174 assert len(refs) == 4
2175
2176 # a ref should have a, not b, and should not be in project2
Monty Taylorbc758832013-06-17 17:22:42 -04002177 assert self.ref_has_change(a_zref, A)
2178 assert not self.ref_has_change(a_zref, B)
2179 assert not self.ref_has_change(a_zref, M2)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002180
2181 # b ref should have a and b, and should not be in project2
Monty Taylorbc758832013-06-17 17:22:42 -04002182 assert self.ref_has_change(b_zref, A)
2183 assert self.ref_has_change(b_zref, B)
2184 assert not self.ref_has_change(b_zref, M2)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002185
2186 # c ref should have a and b in 1, c in 2
Monty Taylorbc758832013-06-17 17:22:42 -04002187 assert self.ref_has_change(c_zref, A)
2188 assert self.ref_has_change(c_zref, B)
2189 assert self.ref_has_change(c_zref, C)
2190 assert not self.ref_has_change(c_zref, D)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002191
2192 # d ref should have a and b in 1, c and d in 2
Monty Taylorbc758832013-06-17 17:22:42 -04002193 assert self.ref_has_change(d_zref, A)
2194 assert self.ref_has_change(d_zref, B)
2195 assert self.ref_has_change(d_zref, C)
2196 assert self.ref_has_change(d_zref, D)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002197
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002198 self.worker.hold_jobs_in_build = False
2199 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002200 self.waitUntilSettled()
2201
2202 assert A.data['status'] == 'MERGED'
2203 assert A.reported == 2
2204 assert B.data['status'] == 'MERGED'
2205 assert B.reported == 2
2206 assert C.data['status'] == 'MERGED'
2207 assert C.reported == 2
2208 assert D.data['status'] == 'MERGED'
2209 assert D.reported == 2
2210 self.assertEmptyQueues()
James E. Blair70c71582013-03-06 08:50:50 -08002211
James E. Blair412e5582013-04-22 15:50:12 -07002212 def test_statsd(self):
2213 "Test each of the statsd methods used in the scheduler"
2214 import extras
2215 statsd = extras.try_import('statsd.statsd')
2216 statsd.incr('test-incr')
2217 statsd.timing('test-timing', 3)
2218 statsd.gauge('test-guage', 12)
2219 self.assertReportedStat('test-incr', '1|c')
2220 self.assertReportedStat('test-timing', '3|ms')
2221 self.assertReportedStat('test-guage', '12|g')
2222
James E. Blair70c71582013-03-06 08:50:50 -08002223 def test_file_jobs(self):
2224 "Test that file jobs run only when appropriate"
2225 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2226 A.addPatchset(['pip-requires'])
2227 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2228 A.addApproval('CRVW', 2)
2229 B.addApproval('CRVW', 2)
2230 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2231 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2232 self.waitUntilSettled()
2233
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002234 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002235 if x.name == 'project-testfile']
2236
2237 assert len(testfile_jobs) == 1
2238 assert testfile_jobs[0].changes == '1,2'
2239 assert A.data['status'] == 'MERGED'
2240 assert A.reported == 2
2241 assert B.data['status'] == 'MERGED'
2242 assert B.reported == 2
2243 self.assertEmptyQueues()
James E. Blair3c5e5b52013-04-26 11:17:03 -07002244
2245 def test_test_config(self):
2246 "Test that we can test the config"
2247 sched = zuul.scheduler.Scheduler()
2248 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002249
2250 def test_build_description(self):
2251 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002252 self.worker.registerFunction('set_description:' +
2253 self.worker.worker_id)
2254
2255 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2256 A.addApproval('CRVW', 2)
2257 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2258 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002259 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002260 self.log.debug("Description: %s" % desc)
2261 assert re.search("Branch.*master", desc)
2262 assert re.search("Pipeline.*gate", desc)
2263 assert re.search("project-merge.*SUCCESS", desc)
2264 assert re.search("project-test1.*SUCCESS", desc)
2265 assert re.search("project-test2.*SUCCESS", desc)
2266 assert re.search("Reported result.*SUCCESS", desc)
2267
James E. Blair1843a552013-07-03 14:19:52 -07002268 def test_json_status(self):
2269 "Test that we can retrieve JSON status info"
2270 self.worker.hold_jobs_in_build = True
2271 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2272 A.addApproval('CRVW', 2)
2273 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2274 self.waitUntilSettled()
2275
2276 port = self.webapp.server.socket.getsockname()[1]
2277
2278 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2279 data = f.read()
2280
2281 self.worker.hold_jobs_in_build = False
2282 self.worker.release()
2283 self.waitUntilSettled()
2284
2285 data = json.loads(data)
2286 status_jobs = set()
2287 for p in data['pipelines']:
2288 for q in p['change_queues']:
2289 for head in q['heads']:
2290 for change in head:
2291 assert change['id'] == '1,1'
2292 for job in change['jobs']:
2293 status_jobs.add(job['name'])
2294 assert 'project-merge' in status_jobs
2295 assert 'project-test1' in status_jobs
2296 assert 'project-test2' in status_jobs
2297
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002298 def test_node_label(self):
2299 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002300 self.worker.registerFunction('build:node-project-test1:debian')
2301
2302 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2303 A.addApproval('CRVW', 2)
2304 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2305 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002306
2307 assert self.getJobFromHistory('node-project-merge').node is None
2308 assert self.getJobFromHistory('node-project-test1').node == 'debian'
2309 assert self.getJobFromHistory('node-project-test2').node is None