blob: ca80ab98362b0b28d7223207640f9ce8fa823cba [file] [log] [blame]
James E. Blairb0fcae42012-07-17 11:12:10 -07001#!/usr/bin/env python
2
3# Copyright 2012 Hewlett-Packard Development Company, L.P.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
James E. Blairb0fcae42012-07-17 11:12:10 -070017import ConfigParser
Monty Taylorbc758832013-06-17 17:22:42 -040018from cStringIO import StringIO
James E. Blair8cc15a82012-08-01 11:17:57 -070019import hashlib
James E. Blairb0fcae42012-07-17 11:12:10 -070020import json
Monty Taylorbc758832013-06-17 17:22:42 -040021import logging
22import os
James E. Blairb0fcae42012-07-17 11:12:10 -070023import pprint
Monty Taylorbc758832013-06-17 17:22:42 -040024import Queue
25import random
James E. Blairb0fcae42012-07-17 11:12:10 -070026import re
James E. Blair412e5582013-04-22 15:50:12 -070027import select
James E. Blair4886cc12012-07-18 15:39:41 -070028import shutil
James E. Blair412e5582013-04-22 15:50:12 -070029import socket
James E. Blair4886f282012-11-15 09:27:33 -080030import string
Monty Taylorbc758832013-06-17 17:22:42 -040031import subprocess
Monty Taylorbc758832013-06-17 17:22:42 -040032import threading
33import time
James E. Blair1843a552013-07-03 14:19:52 -070034import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040035import urllib2
36import urlparse
37
James E. Blair4886cc12012-07-18 15:39:41 -070038import git
James E. Blair1f4c2bb2013-04-26 08:40:46 -070039import gear
Monty Taylorbc758832013-06-17 17:22:42 -040040import fixtures
41import statsd
42import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070043
James E. Blairb0fcae42012-07-17 11:12:10 -070044import zuul.scheduler
James E. Blair1843a552013-07-03 14:19:52 -070045import zuul.webapp
James E. Blair1f4c2bb2013-04-26 08:40:46 -070046import zuul.launcher.gearman
James E. Blairb0fcae42012-07-17 11:12:10 -070047import zuul.trigger.gerrit
48
49FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
50 'fixtures')
51CONFIG = ConfigParser.ConfigParser()
52CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
53
54CONFIG.set('zuul', 'layout_config',
55 os.path.join(FIXTURE_DIR, "layout.yaml"))
56
James E. Blair1f4c2bb2013-04-26 08:40:46 -070057logging.basicConfig(level=logging.DEBUG,
58 format='%(asctime)s %(name)-32s '
59 '%(levelname)-8s %(message)s')
James E. Blairb0fcae42012-07-17 11:12:10 -070060
61
Monty Taylorbc758832013-06-17 17:22:42 -040062def repack_repo(path):
63 output = subprocess.Popen(
64 ['git', '--git-dir=%s/.git' % path, 'repack', '-afd'],
65 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
66 out = output.communicate()
67 if output.returncode:
68 raise Exception("git repack returned %d" % output.returncode)
69 return out
70
71
James E. Blair8cc15a82012-08-01 11:17:57 -070072def random_sha1():
73 return hashlib.sha1(str(random.random())).hexdigest()
74
75
James E. Blair4886cc12012-07-18 15:39:41 -070076class ChangeReference(git.Reference):
77 _common_path_default = "refs/changes"
78 _points_to_commits_only = True
79
80
James E. Blairb0fcae42012-07-17 11:12:10 -070081class FakeChange(object):
James E. Blair8c803f82012-07-31 16:25:42 -070082 categories = {'APRV': ('Approved', -1, 1),
83 'CRVW': ('Code-Review', -2, 2),
84 'VRFY': ('Verified', -2, 2)}
James E. Blairb0fcae42012-07-17 11:12:10 -070085
Monty Taylorbc758832013-06-17 17:22:42 -040086 def __init__(self, gerrit, number, project, branch, subject,
87 status='NEW', upstream_root=None):
James E. Blair8cc15a82012-08-01 11:17:57 -070088 self.gerrit = gerrit
James E. Blaird466dc42012-07-31 10:42:56 -070089 self.reported = 0
James E. Blair8c803f82012-07-31 16:25:42 -070090 self.queried = 0
James E. Blairb0fcae42012-07-17 11:12:10 -070091 self.patchsets = []
James E. Blairb0fcae42012-07-17 11:12:10 -070092 self.number = number
93 self.project = project
94 self.branch = branch
95 self.subject = subject
96 self.latest_patchset = 0
James E. Blair8c803f82012-07-31 16:25:42 -070097 self.depends_on_change = None
98 self.needed_by_changes = []
James E. Blair127bc182012-08-28 15:55:15 -070099 self.fail_merge = False
James E. Blair42f74822013-05-14 15:18:03 -0700100 self.messages = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700101 self.data = {
102 'branch': branch,
103 'comments': [],
104 'commitMessage': subject,
105 'createdOn': time.time(),
James E. Blair8cc15a82012-08-01 11:17:57 -0700106 'id': 'I' + random_sha1(),
James E. Blairb0fcae42012-07-17 11:12:10 -0700107 'lastUpdated': time.time(),
108 'number': str(number),
109 'open': True,
110 'owner': {'email': 'user@example.com',
111 'name': 'User Name',
112 'username': 'username'},
113 'patchSets': self.patchsets,
114 'project': project,
115 'status': status,
116 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700117 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700118 'url': 'https://hostname/%s' % number}
119
Monty Taylorbc758832013-06-17 17:22:42 -0400120 self.upstream_root = upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700121 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700122 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700123
Monty Taylorbc758832013-06-17 17:22:42 -0400124 def add_fake_change_to_repo(self, msg, fn, large):
125 path = os.path.join(self.upstream_root, self.project)
126 repo = git.Repo(path)
127 ref = ChangeReference.create(repo, '1/%s/%s' % (self.number,
128 self.latest_patchset),
129 'refs/tags/init')
130 repo.head.reference = ref
131 repo.head.reset(index=True, working_tree=True)
132 repo.git.clean('-x', '-f', '-d')
133
134 path = os.path.join(self.upstream_root, self.project)
135 if not large:
136 fn = os.path.join(path, fn)
137 f = open(fn, 'w')
138 f.write("test %s %s %s\n" %
139 (self.branch, self.number, self.latest_patchset))
140 f.close()
141 repo.index.add([fn])
142 else:
143 for fni in range(100):
144 fn = os.path.join(path, str(fni))
145 f = open(fn, 'w')
146 for ci in range(4096):
147 f.write(random.choice(string.printable))
148 f.close()
149 repo.index.add([fn])
150
151 return repo.index.commit(msg)
152
James E. Blair70c71582013-03-06 08:50:50 -0800153 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700154 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700155 if files:
156 fn = files[0]
157 else:
158 fn = '%s-%s' % (self.branch, self.number)
159 msg = self.subject + '-' + str(self.latest_patchset)
Monty Taylorbc758832013-06-17 17:22:42 -0400160 c = self.add_fake_change_to_repo(msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800161 ps_files = [{'file': '/COMMIT_MSG',
162 'type': 'ADDED'},
163 {'file': 'README',
164 'type': 'MODIFIED'}]
165 for f in files:
166 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700167 d = {'approvals': [],
168 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800169 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700170 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700171 'ref': 'refs/changes/1/%s/%s' % (self.number,
172 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700173 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700174 'uploader': {'email': 'user@example.com',
175 'name': 'User name',
176 'username': 'user'}}
177 self.data['currentPatchSet'] = d
178 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700179 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700180
James E. Blaire0487072012-08-29 17:38:31 -0700181 def getPatchsetCreatedEvent(self, patchset):
182 event = {"type": "patchset-created",
183 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800184 "branch": self.branch,
185 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
186 "number": str(self.number),
187 "subject": self.subject,
188 "owner": {"name": "User Name"},
189 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700190 "patchSet": self.patchsets[patchset - 1],
191 "uploader": {"name": "User Name"}}
192 return event
193
James E. Blair42f74822013-05-14 15:18:03 -0700194 def getChangeRestoredEvent(self):
195 event = {"type": "change-restored",
196 "change": {"project": self.project,
197 "branch": self.branch,
198 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
199 "number": str(self.number),
200 "subject": self.subject,
201 "owner": {"name": "User Name"},
202 "url": "https://hostname/3"},
203 "restorer": {"name": "User Name"},
204 "reason": ""}
205 return event
206
James E. Blairb0fcae42012-07-17 11:12:10 -0700207 def addApproval(self, category, value):
James E. Blair8c803f82012-07-31 16:25:42 -0700208 approval = {'description': self.categories[category][0],
209 'type': category,
210 'value': str(value)}
211 self.patchsets[-1]['approvals'].append(approval)
212 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700213 'author': {'email': 'user@example.com',
214 'name': 'User Name',
215 'username': 'username'},
216 'change': {'branch': self.branch,
217 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
218 'number': str(self.number),
219 'owner': {'email': 'user@example.com',
220 'name': 'User Name',
221 'username': 'username'},
222 'project': self.project,
223 'subject': self.subject,
224 'topic': 'master',
225 'url': 'https://hostname/459'},
226 'comment': '',
227 'patchSet': self.patchsets[-1],
228 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700229 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700230 return json.loads(json.dumps(event))
231
James E. Blair8c803f82012-07-31 16:25:42 -0700232 def getSubmitRecords(self):
233 status = {}
234 for cat in self.categories.keys():
235 status[cat] = 0
236
237 for a in self.patchsets[-1]['approvals']:
238 cur = status[a['type']]
239 cat_min, cat_max = self.categories[a['type']][1:]
240 new = int(a['value'])
241 if new == cat_min:
242 cur = new
243 elif abs(new) > abs(cur):
244 cur = new
245 status[a['type']] = cur
246
247 labels = []
248 ok = True
249 for typ, cat in self.categories.items():
250 cur = status[typ]
251 cat_min, cat_max = cat[1:]
252 if cur == cat_min:
253 value = 'REJECT'
254 ok = False
255 elif cur == cat_max:
256 value = 'OK'
257 else:
258 value = 'NEED'
259 ok = False
260 labels.append({'label': cat[0], 'status': value})
261 if ok:
262 return [{'status': 'OK'}]
263 return [{'status': 'NOT_READY',
264 'labels': labels}]
265
266 def setDependsOn(self, other, patchset):
267 self.depends_on_change = other
268 d = {'id': other.data['id'],
269 'number': other.data['number'],
270 'ref': other.patchsets[patchset - 1]['ref']
271 }
272 self.data['dependsOn'] = [d]
273
274 other.needed_by_changes.append(self)
275 needed = other.data.get('neededBy', [])
276 d = {'id': self.data['id'],
277 'number': self.data['number'],
278 'ref': self.patchsets[patchset - 1]['ref'],
279 'revision': self.patchsets[patchset - 1]['revision']
280 }
281 needed.append(d)
282 other.data['neededBy'] = needed
283
James E. Blairb0fcae42012-07-17 11:12:10 -0700284 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700285 self.queried += 1
286 d = self.data.get('dependsOn')
287 if d:
288 d = d[0]
289 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
290 d['isCurrentPatchSet'] = True
291 else:
292 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700293 return json.loads(json.dumps(self.data))
294
295 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800296 if (self.depends_on_change and
297 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700298 return
James E. Blair127bc182012-08-28 15:55:15 -0700299 if self.fail_merge:
300 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700301 self.data['status'] = 'MERGED'
302 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700303
Monty Taylorbc758832013-06-17 17:22:42 -0400304 path = os.path.join(self.upstream_root, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700305 repo = git.Repo(path)
306 repo.heads[self.branch].commit = \
307 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700308
James E. Blaird466dc42012-07-31 10:42:56 -0700309 def setReported(self):
310 self.reported += 1
311
James E. Blairb0fcae42012-07-17 11:12:10 -0700312
313class FakeGerrit(object):
314 def __init__(self, *args, **kw):
315 self.event_queue = Queue.Queue()
316 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
317 self.change_number = 0
318 self.changes = {}
319
320 def addFakeChange(self, project, branch, subject):
321 self.change_number += 1
Monty Taylorbc758832013-06-17 17:22:42 -0400322 c = FakeChange(self, self.change_number, project, branch, subject,
323 upstream_root=self.upstream_root)
James E. Blairb0fcae42012-07-17 11:12:10 -0700324 self.changes[self.change_number] = c
325 return c
326
327 def addEvent(self, data):
328 return self.event_queue.put(data)
329
330 def getEvent(self):
331 return self.event_queue.get()
332
333 def eventDone(self):
334 self.event_queue.task_done()
335
336 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700337 number, ps = changeid.split(',')
338 change = self.changes[int(number)]
James E. Blair42f74822013-05-14 15:18:03 -0700339 change.messages.append(message)
James E. Blairb0fcae42012-07-17 11:12:10 -0700340 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700341 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700342 if message:
343 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700344
345 def query(self, number):
346 change = self.changes[int(number)]
347 return change.query()
348
349 def startWatching(self, *args, **kw):
350 pass
351
352
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700353class BuildHistory(object):
354 def __init__(self, **kw):
355 self.__dict__.update(kw)
James E. Blairb0fcae42012-07-17 11:12:10 -0700356
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700357 def __repr__(self):
358 return ("<Completed build, result: %s name: %s #%s changes: %s>" %
359 (self.result, self.name, self.number, self.changes))
James E. Blairb0fcae42012-07-17 11:12:10 -0700360
361
James E. Blair8cc15a82012-08-01 11:17:57 -0700362class FakeURLOpener(object):
Monty Taylorbc758832013-06-17 17:22:42 -0400363 def __init__(self, upstream_root, fake_gerrit, url):
364 self.upstream_root = upstream_root
James E. Blair8cc15a82012-08-01 11:17:57 -0700365 self.fake_gerrit = fake_gerrit
366 self.url = url
367
368 def read(self):
369 res = urlparse.urlparse(self.url)
370 path = res.path
371 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200372 ret = '001e# service=git-upload-pack\n'
373 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
374 'multi_ack thin-pack side-band side-band-64k ofs-delta '
375 'shallow no-progress include-tag multi_ack_detailed no-done\n')
Monty Taylorbc758832013-06-17 17:22:42 -0400376 path = os.path.join(self.upstream_root, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700377 repo = git.Repo(path)
378 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200379 r = ref.object.hexsha + ' ' + ref.path + '\n'
380 ret += '%04x%s' % (len(r) + 4, r)
381 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700382 return ret
383
384
James E. Blair4886cc12012-07-18 15:39:41 -0700385class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
Monty Taylorbc758832013-06-17 17:22:42 -0400386 def __init__(self, upstream_root, *args):
387 super(FakeGerritTrigger, self).__init__(*args)
388 self.upstream_root = upstream_root
389
James E. Blair4886cc12012-07-18 15:39:41 -0700390 def getGitUrl(self, project):
Monty Taylorbc758832013-06-17 17:22:42 -0400391 return os.path.join(self.upstream_root, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700392
393
James E. Blair412e5582013-04-22 15:50:12 -0700394class FakeStatsd(threading.Thread):
395 def __init__(self):
396 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400397 self.daemon = True
James E. Blair412e5582013-04-22 15:50:12 -0700398 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
399 self.sock.bind(('', 0))
400 self.port = self.sock.getsockname()[1]
401 self.wake_read, self.wake_write = os.pipe()
402 self.stats = []
403
404 def run(self):
405 while True:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700406 poll = select.poll()
407 poll.register(self.sock, select.POLLIN)
408 poll.register(self.wake_read, select.POLLIN)
409 ret = poll.poll()
410 for (fd, event) in ret:
411 if fd == self.sock.fileno():
James E. Blair412e5582013-04-22 15:50:12 -0700412 data = self.sock.recvfrom(1024)
413 if not data:
414 return
415 self.stats.append(data[0])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700416 if fd == self.wake_read:
James E. Blair412e5582013-04-22 15:50:12 -0700417 return
418
419 def stop(self):
420 os.write(self.wake_write, '1\n')
421
422
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700423class FakeBuild(threading.Thread):
424 log = logging.getLogger("zuul.test")
425
426 def __init__(self, worker, job, number, node):
427 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400428 self.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700429 self.worker = worker
430 self.job = job
431 self.name = job.name.split(':')[1]
432 self.number = number
433 self.node = node
434 self.parameters = json.loads(job.arguments)
435 self.unique = self.parameters['ZUUL_UUID']
436 self.wait_condition = threading.Condition()
437 self.waiting = False
438 self.aborted = False
439 self.created = time.time()
440 self.description = ''
441
442 def release(self):
443 self.wait_condition.acquire()
444 self.wait_condition.notify()
445 self.waiting = False
446 self.log.debug("Build %s released" % self.unique)
447 self.wait_condition.release()
448
449 def isWaiting(self):
450 self.wait_condition.acquire()
451 if self.waiting:
452 ret = True
453 else:
454 ret = False
455 self.wait_condition.release()
456 return ret
457
458 def _wait(self):
459 self.wait_condition.acquire()
460 self.waiting = True
461 self.log.debug("Build %s waiting" % self.unique)
462 self.wait_condition.wait()
463 self.wait_condition.release()
464
465 def run(self):
466 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700467 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
468 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700469 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700470 'manager': self.worker.worker_id,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700471 }
472
473 self.job.sendWorkData(json.dumps(data))
474 self.job.sendWorkStatus(0, 100)
475
476 if self.worker.hold_jobs_in_build:
477 self._wait()
478 self.log.debug("Build %s continuing" % self.unique)
479
480 self.worker.lock.acquire()
481
482 result = 'SUCCESS'
483 if (('ZUUL_REF' in self.parameters) and
484 self.worker.shouldFailTest(self.name,
485 self.parameters['ZUUL_REF'])):
486 result = 'FAILURE'
487 if self.aborted:
488 result = 'ABORTED'
489
490 data = {'result': result}
491 changes = None
492 if 'ZUUL_CHANGE_IDS' in self.parameters:
493 changes = self.parameters['ZUUL_CHANGE_IDS']
494
495 self.worker.build_history.append(
496 BuildHistory(name=self.name, number=self.number,
497 result=result, changes=changes, node=self.node,
James E. Blair64ed6f22013-07-10 14:07:23 -0700498 uuid=self.unique, description=self.description,
499 pipeline=self.parameters['ZUUL_PIPELINE'])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700500 )
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,
James E. Blair78e31b32013-07-09 09:11:34 -0700605 len(self.running_builds)))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700606 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))
Monty Taylore6a6c402013-07-02 09:25:55 -0700690 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
691 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400692 self.useFixture(fixtures.FakeLogger(
693 level=logging.DEBUG,
694 format='%(asctime)s %(name)-32s '
695 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700696 tmp_root = self.useFixture(fixtures.TempDir(
697 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400698 self.test_root = os.path.join(tmp_root, "zuul-test")
699 self.upstream_root = os.path.join(self.test_root, "upstream")
700 self.git_root = os.path.join(self.test_root, "git")
701
702 CONFIG.set('zuul', 'git_dir', self.git_root)
703 if os.path.exists(self.test_root):
704 shutil.rmtree(self.test_root)
705 os.makedirs(self.test_root)
706 os.makedirs(self.upstream_root)
707 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700708
709 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400710 self.init_repo("org/project")
711 self.init_repo("org/project1")
712 self.init_repo("org/project2")
713 self.init_repo("org/project3")
714 self.init_repo("org/one-job-project")
715 self.init_repo("org/nonvoting-project")
716 self.init_repo("org/templated-project")
717 self.init_repo("org/node-project")
James E. Blair412e5582013-04-22 15:50:12 -0700718
719 self.statsd = FakeStatsd()
720 os.environ['STATSD_HOST'] = 'localhost'
721 os.environ['STATSD_PORT'] = str(self.statsd.port)
722 self.statsd.start()
723 # the statsd client object is configured in the statsd module import
724 reload(statsd)
725 reload(zuul.scheduler)
726
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700727 self.gearman_server = FakeGearmanServer()
728
729 self.config = ConfigParser.ConfigParser()
730 cfg = StringIO()
731 CONFIG.write(cfg)
732 cfg.seek(0)
733 self.config.readfp(cfg)
734 self.config.set('gearman', 'port', str(self.gearman_server.port))
735
Monty Taylorbc758832013-06-17 17:22:42 -0400736 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700737 self.worker.addServer('127.0.0.1', self.gearman_server.port)
738 self.gearman_server.worker = self.worker
739
James E. Blairb0fcae42012-07-17 11:12:10 -0700740 self.sched = zuul.scheduler.Scheduler()
741
James E. Blair8cc15a82012-08-01 11:17:57 -0700742 def URLOpenerFactory(*args, **kw):
743 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400744 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700745
James E. Blair8cc15a82012-08-01 11:17:57 -0700746 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700747 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700748
749 zuul.lib.gerrit.Gerrit = FakeGerrit
750
Monty Taylorbc758832013-06-17 17:22:42 -0400751 self.gerrit = FakeGerritTrigger(
752 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700753 self.gerrit.replication_timeout = 1.5
754 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700755 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400756 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700757
James E. Blair1843a552013-07-03 14:19:52 -0700758 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
759
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700760 self.sched.setLauncher(self.launcher)
James E. Blairb0fcae42012-07-17 11:12:10 -0700761 self.sched.setTrigger(self.gerrit)
762
763 self.sched.start()
764 self.sched.reconfigure(self.config)
765 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700766 self.webapp.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700767 self.launcher.gearman.waitForServer()
768 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400769 self.builds = self.worker.running_builds
770 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700771
James E. Blairfee8d652013-06-07 08:57:52 -0700772 self.addCleanup(self.assertFinalState)
773 self.addCleanup(self.shutdown)
774
775 def assertFinalState(self):
776 # Make sure that the change cache is cleared
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400777 self.assertEqual(len(self.sched.trigger._change_cache.keys()), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700778 self.assertEmptyQueues()
779
780 def shutdown(self):
781 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700782 self.launcher.stop()
783 self.worker.shutdown()
784 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700785 self.gerrit.stop()
786 self.sched.stop()
787 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700788 self.statsd.stop()
789 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700790 self.webapp.stop()
791 self.webapp.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700792 threads = threading.enumerate()
793 if len(threads) > 1:
794 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400795 super(TestScheduler, self).tearDown()
796
797 def init_repo(self, project):
798 parts = project.split('/')
799 path = os.path.join(self.upstream_root, *parts[:-1])
800 if not os.path.exists(path):
801 os.makedirs(path)
802 path = os.path.join(self.upstream_root, project)
803 repo = git.Repo.init(path)
804
805 repo.config_writer().set_value('user', 'email', 'user@example.com')
806 repo.config_writer().set_value('user', 'name', 'User Name')
807 repo.config_writer().write()
808
809 fn = os.path.join(path, 'README')
810 f = open(fn, 'w')
811 f.write("test\n")
812 f.close()
813 repo.index.add([fn])
814 repo.index.commit('initial commit')
815 master = repo.create_head('master')
816 repo.create_tag('init')
817
818 mp = repo.create_head('mp')
819 repo.head.reference = mp
820 f = open(fn, 'a')
821 f.write("test mp\n")
822 f.close()
823 repo.index.add([fn])
824 repo.index.commit('mp commit')
825
826 repo.head.reference = master
827 repo.head.reset(index=True, working_tree=True)
828 repo.git.clean('-x', '-f', '-d')
829
830 def ref_has_change(self, ref, change):
831 path = os.path.join(self.git_root, change.project)
832 repo = git.Repo(path)
833 for commit in repo.iter_commits(ref):
834 if commit.message.strip() == ('%s-1' % change.subject):
835 return True
836 return False
837
838 def job_has_changes(self, *args):
839 job = args[0]
840 commits = args[1:]
841 if isinstance(job, FakeBuild):
842 parameters = job.parameters
843 else:
844 parameters = json.loads(job.arguments)
845 project = parameters['ZUUL_PROJECT']
846 path = os.path.join(self.git_root, project)
847 repo = git.Repo(path)
848 ref = parameters['ZUUL_REF']
849 sha = parameters['ZUUL_COMMIT']
850 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
851 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
852 commit_messages = ['%s-1' % commit.subject for commit in commits]
853 for msg in commit_messages:
854 if msg not in repo_messages:
855 return False
856 if repo_shas[0] != sha:
857 return False
858 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700859
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700860 def registerJobs(self):
861 count = 0
James E. Blaireff88162013-07-01 12:44:14 -0400862 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700863 self.worker.registerFunction('build:' + job)
864 count += 1
865 self.worker.registerFunction('stop:' + self.worker.worker_id)
866 count += 1
867
868 while len(self.gearman_server.functions) < count:
869 time.sleep(0)
870
871 def release(self, job):
872 if isinstance(job, FakeBuild):
873 job.release()
874 else:
875 job.waiting = False
876 self.log.debug("Queued job %s released" % job.unique)
877 self.gearman_server.wakeConnections()
878
879 def getParameter(self, job, name):
880 if isinstance(job, FakeBuild):
881 return job.parameters[name]
882 else:
883 parameters = json.loads(job.arguments)
884 return parameters[name]
885
886 def resetGearmanServer(self):
887 self.worker.setFunctions([])
888 while True:
889 done = True
890 for connection in self.gearman_server.active_connections:
891 if connection.functions:
892 done = False
893 if done:
894 break
895 time.sleep(0)
896 self.gearman_server.functions = set()
897
898 def haveAllBuildsReported(self):
899 # See if Zuul is waiting on a meta job to complete
900 if self.launcher.meta_jobs:
901 return False
902 # Find out if every build that the worker has completed has been
903 # reported back to Zuul. If it hasn't then that means a Gearman
904 # event is still in transit and the system is not stable.
905 for build in self.worker.build_history:
906 zbuild = self.launcher.builds.get(build.uuid)
907 if not zbuild:
908 # It has already been reported
909 continue
910 # It hasn't been reported yet.
911 return False
912 # Make sure that none of the worker connections are in GRAB_WAIT
913 for connection in self.worker.active_connections:
914 if connection.state == 'GRAB_WAIT':
915 return False
916 return True
917
918 def areAllBuildsWaiting(self):
919 ret = True
920
921 builds = self.launcher.builds.values()
922 for build in builds:
923 client_job = None
924 for conn in self.launcher.gearman.active_connections:
925 for j in conn.related_jobs.values():
926 if j.unique == build.uuid:
927 client_job = j
928 break
929 if not client_job:
930 self.log.debug("%s is not known to the gearman client" %
931 build)
932 ret = False
933 continue
934 if not client_job.handle:
935 self.log.debug("%s has no handle" % client_job)
936 ret = False
937 continue
938 server_job = self.gearman_server.jobs.get(client_job.handle)
939 if not server_job:
940 self.log.debug("%s is not known to the gearman server" %
941 client_job)
942 ret = False
943 continue
944 if not hasattr(server_job, 'waiting'):
945 self.log.debug("%s is being enqueued" % server_job)
946 ret = False
947 continue
948 if server_job.waiting:
949 continue
950 worker_job = self.worker.gearman_jobs.get(server_job.unique)
951 if worker_job:
952 if worker_job.build.isWaiting():
953 continue
954 else:
955 self.log.debug("%s is running" % worker_job)
956 ret = False
957 else:
958 self.log.debug("%s is unassigned" % server_job)
959 ret = False
960 return ret
961
James E. Blairb0fcae42012-07-17 11:12:10 -0700962 def waitUntilSettled(self):
963 self.log.debug("Waiting until settled...")
964 start = time.time()
965 while True:
966 if time.time() - start > 10:
967 print 'queue status:',
968 print self.sched.trigger_event_queue.empty(),
969 print self.sched.result_event_queue.empty(),
970 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700971 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -0700972 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700973 # Make sure no new events show up while we're checking
974 self.worker.lock.acquire()
975 # have all build states propogated to zuul?
976 if self.haveAllBuildsReported():
977 # Join ensures that the queue is empty _and_ events have been
978 # processed
979 self.fake_gerrit.event_queue.join()
980 self.sched.trigger_event_queue.join()
981 self.sched.result_event_queue.join()
982 if (self.sched.trigger_event_queue.empty() and
983 self.sched.result_event_queue.empty() and
984 self.fake_gerrit.event_queue.empty() and
985 self.areAllBuildsWaiting()):
986 self.worker.lock.release()
987 self.log.debug("...settled.")
988 return
989 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700990 self.sched.wake_event.wait(0.1)
991
James E. Blaird466dc42012-07-31 10:42:56 -0700992 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -0800993 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -0700994 return len(jobs)
995
James E. Blair4ca985f2013-05-30 12:27:43 -0700996 def getJobFromHistory(self, name):
997 history = self.worker.build_history
998 for job in history:
999 if job.name == name:
1000 return job
1001 raise Exception("Unable to find job %s in history" % name)
1002
James E. Blaire0487072012-08-29 17:38:31 -07001003 def assertEmptyQueues(self):
1004 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001005 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001006 for queue in pipeline.queues:
1007 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001008 print 'pipeline %s queue %s contents %s' % (
1009 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001010 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001011 if len(queue.severed_heads) != 0:
1012 print 'heads', queue.severed_heads
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001013 self.assertEqual(len(queue.severed_heads), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001014
James E. Blair412e5582013-04-22 15:50:12 -07001015 def assertReportedStat(self, key, value=None):
1016 start = time.time()
1017 while time.time() < (start + 5):
1018 for stat in self.statsd.stats:
1019 k, v = stat.split(':')
1020 if key == k:
1021 if value is None:
1022 return
1023 if value == v:
1024 return
1025 time.sleep(0.1)
1026
1027 pprint.pprint(self.statsd.stats)
1028 raise Exception("Key %s not found in reported stats" % key)
1029
James E. Blairb0fcae42012-07-17 11:12:10 -07001030 def test_jobs_launched(self):
1031 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001032
James E. Blairb0fcae42012-07-17 11:12:10 -07001033 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001034 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001035 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1036 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001037 self.assertEqual(self.getJobFromHistory('project-merge').result,
1038 'SUCCESS')
1039 self.assertEqual(self.getJobFromHistory('project-test1').result,
1040 'SUCCESS')
1041 self.assertEqual(self.getJobFromHistory('project-test2').result,
1042 'SUCCESS')
1043 self.assertEqual(A.data['status'], 'MERGED')
1044 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001045
James E. Blair412e5582013-04-22 15:50:12 -07001046 self.assertReportedStat('gerrit.event.comment-added', '1|c')
1047 self.assertReportedStat('zuul.pipeline.gate.current_changes', '1|g')
1048 self.assertReportedStat('zuul.job.project-merge')
1049 self.assertReportedStat('zuul.pipeline.gate.resident_time')
1050 self.assertReportedStat('zuul.pipeline.gate.total_changes', '1|c')
1051 self.assertReportedStat(
1052 'zuul.pipeline.gate.org.project.resident_time')
1053 self.assertReportedStat(
1054 'zuul.pipeline.gate.org.project.total_changes', '1|c')
1055
James E. Blair42f74822013-05-14 15:18:03 -07001056 def test_duplicate_pipelines(self):
1057 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001058
James E. Blair42f74822013-05-14 15:18:03 -07001059 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1060 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1061 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001062
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001063 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001064 self.history[0].name == 'project-test1'
1065 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001066
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001067 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001068 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001069 self.assertIn('dup1/project-test1', A.messages[0])
1070 self.assertNotIn('dup2/project-test1', A.messages[0])
1071 self.assertNotIn('dup1/project-test1', A.messages[1])
1072 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001073 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001074 self.assertIn('dup1/project-test1', A.messages[1])
1075 self.assertNotIn('dup2/project-test1', A.messages[1])
1076 self.assertNotIn('dup1/project-test1', A.messages[0])
1077 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001078
James E. Blairb0fcae42012-07-17 11:12:10 -07001079 def test_parallel_changes(self):
1080 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001081
1082 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001083 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1084 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1085 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001086 A.addApproval('CRVW', 2)
1087 B.addApproval('CRVW', 2)
1088 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001089
1090 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1091 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1092 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1093
1094 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001095 self.assertEqual(len(self.builds), 1)
1096 self.assertEqual(self.builds[0].name, 'project-merge')
1097 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001098
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001099 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001100 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001101 self.assertEqual(len(self.builds), 3)
1102 self.assertEqual(self.builds[0].name, 'project-test1')
1103 self.assertTrue(self.job_has_changes(self.builds[0], A))
1104 self.assertEqual(self.builds[1].name, 'project-test2')
1105 self.assertTrue(self.job_has_changes(self.builds[1], A))
1106 self.assertEqual(self.builds[2].name, 'project-merge')
1107 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001108
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001109 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001110 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001111 self.assertEqual(len(self.builds), 5)
1112 self.assertEqual(self.builds[0].name, 'project-test1')
1113 self.assertTrue(self.job_has_changes(self.builds[0], A))
1114 self.assertEqual(self.builds[1].name, 'project-test2')
1115 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001116
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001117 self.assertEqual(self.builds[2].name, 'project-test1')
1118 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1119 self.assertEqual(self.builds[3].name, 'project-test2')
1120 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001121
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001122 self.assertEqual(self.builds[4].name, 'project-merge')
1123 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001124
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001125 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001126 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001127 self.assertEqual(len(self.builds), 6)
1128 self.assertEqual(self.builds[0].name, 'project-test1')
1129 self.assertTrue(self.job_has_changes(self.builds[0], A))
1130 self.assertEqual(self.builds[1].name, 'project-test2')
1131 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001132
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001133 self.assertEqual(self.builds[2].name, 'project-test1')
1134 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1135 self.assertEqual(self.builds[3].name, 'project-test2')
1136 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001137
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001138 self.assertEqual(self.builds[4].name, 'project-test1')
1139 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1140 self.assertEqual(self.builds[5].name, 'project-test2')
1141 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001142
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001143 self.worker.hold_jobs_in_build = False
1144 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001145 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001146 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001147
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001148 self.assertEqual(len(self.history), 9)
1149 self.assertEqual(A.data['status'], 'MERGED')
1150 self.assertEqual(B.data['status'], 'MERGED')
1151 self.assertEqual(C.data['status'], 'MERGED')
1152 self.assertEqual(A.reported, 2)
1153 self.assertEqual(B.reported, 2)
1154 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001155
1156 def test_failed_changes(self):
1157 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001158 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001159
James E. Blairb02a3bb2012-07-30 17:49:55 -07001160 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1161 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001162 A.addApproval('CRVW', 2)
1163 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001164
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001165 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001166
James E. Blaire2819012013-06-28 17:17:26 -04001167 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1168 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001169 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001170
1171 self.worker.release('.*-merge')
1172 self.waitUntilSettled()
1173
1174 self.worker.hold_jobs_in_build = False
1175 self.worker.release()
1176
1177 self.waitUntilSettled()
1178 # It's certain that the merge job for change 2 will run, but
1179 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001180 self.assertTrue(len(self.history) > 6)
1181 self.assertEqual(A.data['status'], 'NEW')
1182 self.assertEqual(B.data['status'], 'MERGED')
1183 self.assertEqual(A.reported, 2)
1184 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001185
1186 def test_independent_queues(self):
1187 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001188
1189 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001190 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001191 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1192 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001193 A.addApproval('CRVW', 2)
1194 B.addApproval('CRVW', 2)
1195 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001196
1197 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1198 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1199 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1200
James E. Blairb02a3bb2012-07-30 17:49:55 -07001201 self.waitUntilSettled()
1202
1203 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001204 self.assertEqual(len(self.builds), 2)
1205 self.assertEqual(self.builds[0].name, 'project-merge')
1206 self.assertTrue(self.job_has_changes(self.builds[0], A))
1207 self.assertEqual(self.builds[1].name, 'project1-merge')
1208 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001209
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001210 # Release the current merge builds
1211 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001212 self.waitUntilSettled()
1213 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001214 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001215 self.waitUntilSettled()
1216
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001217 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001218 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001219 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001220
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001221 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001222 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001223 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001224
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001225 self.assertEqual(len(self.history), 11)
1226 self.assertEqual(A.data['status'], 'MERGED')
1227 self.assertEqual(B.data['status'], 'MERGED')
1228 self.assertEqual(C.data['status'], 'MERGED')
1229 self.assertEqual(A.reported, 2)
1230 self.assertEqual(B.reported, 2)
1231 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001232
1233 def test_failed_change_at_head(self):
1234 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001235
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001236 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001237 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1238 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1239 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001240 A.addApproval('CRVW', 2)
1241 B.addApproval('CRVW', 2)
1242 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001243
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001244 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001245
1246 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1247 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1248 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1249
1250 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001251
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001252 self.assertEqual(len(self.builds), 1)
1253 self.assertEqual(self.builds[0].name, 'project-merge')
1254 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001255
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()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001260 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001261 self.waitUntilSettled()
1262
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001263 self.assertEqual(len(self.builds), 6)
1264 self.assertEqual(self.builds[0].name, 'project-test1')
1265 self.assertEqual(self.builds[1].name, 'project-test2')
1266 self.assertEqual(self.builds[2].name, 'project-test1')
1267 self.assertEqual(self.builds[3].name, 'project-test2')
1268 self.assertEqual(self.builds[4].name, 'project-test1')
1269 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001270
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001271 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001272 self.waitUntilSettled()
1273
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001274 # project-test2, project-merge for B
1275 self.assertEqual(len(self.builds), 2)
1276 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001277
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001278 self.worker.hold_jobs_in_build = False
1279 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001280 self.waitUntilSettled()
1281
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001282 self.assertEqual(len(self.builds), 0)
1283 self.assertEqual(len(self.history), 15)
1284 self.assertEqual(A.data['status'], 'NEW')
1285 self.assertEqual(B.data['status'], 'MERGED')
1286 self.assertEqual(C.data['status'], 'MERGED')
1287 self.assertEqual(A.reported, 2)
1288 self.assertEqual(B.reported, 2)
1289 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001290
1291 def test_failed_change_at_head_with_queue(self):
1292 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001293
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001294 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001295 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1296 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1297 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001298 A.addApproval('CRVW', 2)
1299 B.addApproval('CRVW', 2)
1300 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001301
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001302 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001303
1304 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1305 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1306 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1307
1308 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001309 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001310 self.assertEqual(len(self.builds), 0)
1311 self.assertEqual(len(queue), 1)
1312 self.assertEqual(queue[0].name, 'build:project-merge')
1313 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001314
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. Blair1f4c2bb2013-04-26 08:40:46 -07001319 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001320 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001321 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001322
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001323 self.assertEqual(len(self.builds), 0)
1324 self.assertEqual(len(queue), 6)
1325 self.assertEqual(queue[0].name, 'build:project-test1')
1326 self.assertEqual(queue[1].name, 'build:project-test2')
1327 self.assertEqual(queue[2].name, 'build:project-test1')
1328 self.assertEqual(queue[3].name, 'build:project-test2')
1329 self.assertEqual(queue[4].name, 'build:project-test1')
1330 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001331
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001332 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001333 self.waitUntilSettled()
1334
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001335 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001336 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001337 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1338 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001339
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001340 self.gearman_server.hold_jobs_in_queue = False
1341 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001342 self.waitUntilSettled()
1343
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001344 self.assertEqual(len(self.builds), 0)
1345 self.assertEqual(len(self.history), 11)
1346 self.assertEqual(A.data['status'], 'NEW')
1347 self.assertEqual(B.data['status'], 'MERGED')
1348 self.assertEqual(C.data['status'], 'MERGED')
1349 self.assertEqual(A.reported, 2)
1350 self.assertEqual(B.reported, 2)
1351 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001352
1353 def test_patch_order(self):
1354 "Test that dependent patches are tested in the right order"
1355 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1356 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1357 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1358 A.addApproval('CRVW', 2)
1359 B.addApproval('CRVW', 2)
1360 C.addApproval('CRVW', 2)
1361
1362 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1363 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1364 M2.setMerged()
1365 M1.setMerged()
1366
1367 # C -> B -> A -> M1 -> M2
1368 # M2 is here to make sure it is never queried. If it is, it
1369 # means zuul is walking down the entire history of merged
1370 # changes.
1371
1372 C.setDependsOn(B, 1)
1373 B.setDependsOn(A, 1)
1374 A.setDependsOn(M1, 1)
1375 M1.setDependsOn(M2, 1)
1376
1377 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1378
1379 self.waitUntilSettled()
1380
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001381 self.assertEqual(A.data['status'], 'NEW')
1382 self.assertEqual(B.data['status'], 'NEW')
1383 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001384
1385 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1386 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1387
1388 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001389 self.assertEqual(M2.queried, 0)
1390 self.assertEqual(A.data['status'], 'MERGED')
1391 self.assertEqual(B.data['status'], 'MERGED')
1392 self.assertEqual(C.data['status'], 'MERGED')
1393 self.assertEqual(A.reported, 2)
1394 self.assertEqual(B.reported, 2)
1395 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001396
James E. Blair0e933c52013-07-11 10:18:52 -07001397 def test_trigger_cache(self):
1398 "Test that the trigger cache operates correctly"
1399 self.worker.hold_jobs_in_build = True
1400
1401 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1402 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1403 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1404 A.addApproval('CRVW', 2)
1405 B.addApproval('CRVW', 2)
1406
1407 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1408 M1.setMerged()
1409
1410 B.setDependsOn(A, 1)
1411 A.setDependsOn(M1, 1)
1412
1413 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1414 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1415
1416 self.waitUntilSettled()
1417
1418 for build in self.builds:
1419 if build.parameters['ZUUL_PIPELINE'] == 'check':
1420 build.release()
1421 self.waitUntilSettled()
1422 for build in self.builds:
1423 if build.parameters['ZUUL_PIPELINE'] == 'check':
1424 build.release()
1425 self.waitUntilSettled()
1426
1427 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1428 self.waitUntilSettled()
1429
1430 self.log.debug("len %s " % self.sched.trigger._change_cache.keys())
1431 # there should still be changes in the cache
1432 self.assertNotEqual(len(self.sched.trigger._change_cache.keys()), 0)
1433
1434 self.worker.hold_jobs_in_build = False
1435 self.worker.release()
1436 self.waitUntilSettled()
1437
1438 self.assertEqual(A.data['status'], 'MERGED')
1439 self.assertEqual(B.data['status'], 'MERGED')
1440 self.assertEqual(A.queried, 2) # Initial and isMerged
1441 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1442
James E. Blair8c803f82012-07-31 16:25:42 -07001443 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001444 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001445 # TODO: move to test_gerrit (this is a unit test!)
1446 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4aea70c2012-07-26 14:23:24 -07001447 a = self.sched.trigger.getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001448 mgr = self.sched.layout.pipelines['gate'].manager
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001449 self.assertFalse(
1450 self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001451
1452 A.addApproval('CRVW', 2)
James E. Blairfee8d652013-06-07 08:57:52 -07001453 a = self.sched.trigger.getChange(1, 2, refresh=True)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001454 self.assertFalse(
1455 self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001456
1457 A.addApproval('APRV', 1)
James E. Blairfee8d652013-06-07 08:57:52 -07001458 a = self.sched.trigger.getChange(1, 2, refresh=True)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001459 self.assertTrue(
1460 self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blairfee8d652013-06-07 08:57:52 -07001461 self.sched.trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001462
1463 def test_build_configuration(self):
1464 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001465
1466 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001467 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1468 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1469 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1470 A.addApproval('CRVW', 2)
1471 B.addApproval('CRVW', 2)
1472 C.addApproval('CRVW', 2)
1473 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1474 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1475 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1476 self.waitUntilSettled()
1477
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001478 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001479 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001480 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001481 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001482 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001483 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001484 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001485 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1486 self.gearman_server.hold_jobs_in_queue = False
1487 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001488 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001489
Monty Taylorbc758832013-06-17 17:22:42 -04001490 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001491 repo = git.Repo(path)
1492 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1493 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001494 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001495 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001496
1497 def test_build_configuration_conflict(self):
1498 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001499
1500 self.gearman_server.hold_jobs_in_queue = True
James E. Blair973721f2012-08-15 10:19:43 -07001501 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1502 A.addPatchset(['conflict'])
1503 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1504 B.addPatchset(['conflict'])
1505 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1506 A.addApproval('CRVW', 2)
1507 B.addApproval('CRVW', 2)
1508 C.addApproval('CRVW', 2)
1509 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1510 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1511 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1512 self.waitUntilSettled()
1513
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001514 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001515 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001516 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001517 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001518 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001519 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001520 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001521 self.getParameter(queue[-1], 'ZUUL_REF')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001522 self.gearman_server.hold_jobs_in_queue = False
1523 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001524 self.waitUntilSettled()
1525
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001526 self.assertEqual(A.data['status'], 'MERGED')
1527 self.assertEqual(B.data['status'], 'NEW')
1528 self.assertEqual(C.data['status'], 'MERGED')
1529 self.assertEqual(A.reported, 2)
1530 self.assertEqual(B.reported, 2)
1531 self.assertEqual(C.reported, 2)
James E. Blairdaabed22012-08-15 15:38:57 -07001532
1533 def test_post(self):
1534 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001535
Zhongyue Luo5d556072012-09-21 02:00:47 +09001536 e = {
1537 "type": "ref-updated",
1538 "submitter": {
1539 "name": "User Name",
1540 },
1541 "refUpdate": {
1542 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1543 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1544 "refName": "master",
1545 "project": "org/project",
1546 }
1547 }
James E. Blairdaabed22012-08-15 15:38:57 -07001548 self.fake_gerrit.addEvent(e)
1549 self.waitUntilSettled()
1550
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001551 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001552 self.assertEqual(len(self.history), 1)
1553 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001554
1555 def test_build_configuration_branch(self):
1556 "Test that the right commits are on alternate branches"
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', 'mp', 'A')
1560 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1561 C = self.fake_gerrit.addFakeChange('org/project', 'mp', '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. Blair1f4c2bb2013-04-26 08:40:46 -07001572 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001573 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001574 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001575 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001576 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001577 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1578 self.gearman_server.hold_jobs_in_queue = False
1579 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001580 self.waitUntilSettled()
1581
Monty Taylorbc758832013-06-17 17:22:42 -04001582 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001583 repo = git.Repo(path)
1584 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1585 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001586 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001587 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001588
1589 def test_build_configuration_branch_interaction(self):
1590 "Test that switching between branches works"
1591 self.test_build_configuration()
1592 self.test_build_configuration_branch()
1593 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001594 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001595 repo = git.Repo(path)
1596 repo.heads.master.commit = repo.commit('init')
1597 self.test_build_configuration()
1598
1599 def test_build_configuration_multi_branch(self):
1600 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001601
1602 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001603 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1604 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1605 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1606 A.addApproval('CRVW', 2)
1607 B.addApproval('CRVW', 2)
1608 C.addApproval('CRVW', 2)
1609 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1610 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1611 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1612 self.waitUntilSettled()
1613
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001614 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001615 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001616 queue = self.gearman_server.getQueue()
James E. Blairf750aa02013-07-15 14:11:24 -07001617 ref_B = self.getParameter(queue[-1], 'ZUUL_REF')
1618 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001619 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001620 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001621 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001622 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001623 queue = self.gearman_server.getQueue()
James E. Blairf750aa02013-07-15 14:11:24 -07001624 ref_C = self.getParameter(queue[-1], 'ZUUL_REF')
1625 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001626 self.gearman_server.hold_jobs_in_queue = False
1627 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001628 self.waitUntilSettled()
1629
Monty Taylorbc758832013-06-17 17:22:42 -04001630 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001631 repo = git.Repo(path)
1632
1633 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001634 for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001635 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001636 correct_messages = ['initial commit', 'A-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001637 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001638
1639 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001640 for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001641 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001642 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001643 self.assertEqual(repo_messages, correct_messages)
James E. Blair7f71c802012-08-22 13:04:32 -07001644
1645 def test_one_job_project(self):
1646 "Test that queueing works with one job"
1647 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1648 'master', 'A')
1649 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1650 'master', 'B')
1651 A.addApproval('CRVW', 2)
1652 B.addApproval('CRVW', 2)
1653 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1654 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1655 self.waitUntilSettled()
1656
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001657 self.assertEqual(A.data['status'], 'MERGED')
1658 self.assertEqual(A.reported, 2)
1659 self.assertEqual(B.data['status'], 'MERGED')
1660 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001661
Antoine Musso80edd5a2013-02-13 15:37:53 +01001662 def test_job_from_templates_launched(self):
1663 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001664
Antoine Musso80edd5a2013-02-13 15:37:53 +01001665 A = self.fake_gerrit.addFakeChange(
1666 'org/templated-project', 'master', 'A')
1667 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1668 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001669
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001670 self.assertEqual(self.getJobFromHistory('project-test1').result,
1671 'SUCCESS')
1672 self.assertEqual(self.getJobFromHistory('project-test2').result,
1673 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001674
James E. Blaircaec0c52012-08-22 14:52:22 -07001675 def test_dependent_changes_dequeue(self):
1676 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001677
James E. Blaircaec0c52012-08-22 14:52:22 -07001678 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1679 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1680 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1681 A.addApproval('CRVW', 2)
1682 B.addApproval('CRVW', 2)
1683 C.addApproval('CRVW', 2)
1684
1685 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1686 M1.setMerged()
1687
1688 # C -> B -> A -> M1
1689
1690 C.setDependsOn(B, 1)
1691 B.setDependsOn(A, 1)
1692 A.setDependsOn(M1, 1)
1693
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001694 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001695
1696 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1697 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1698 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1699
1700 self.waitUntilSettled()
1701
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001702 self.assertEqual(A.data['status'], 'NEW')
1703 self.assertEqual(A.reported, 2)
1704 self.assertEqual(B.data['status'], 'NEW')
1705 self.assertEqual(B.reported, 2)
1706 self.assertEqual(C.data['status'], 'NEW')
1707 self.assertEqual(C.reported, 2)
1708 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001709
1710 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001711 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001712 # If it's dequeued more than once, we should see extra
1713 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001714
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001715 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001716 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1717 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1718 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1719 A.addApproval('CRVW', 2)
1720 B.addApproval('CRVW', 2)
1721 C.addApproval('CRVW', 2)
1722
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001723 self.worker.addFailTest('project1-test1', A)
1724 self.worker.addFailTest('project1-test2', A)
1725 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001726
1727 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1728 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1729 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1730
1731 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001732
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001733 self.assertEqual(len(self.builds), 1)
1734 self.assertEqual(self.builds[0].name, 'project1-merge')
1735 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07001736
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001737 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001738 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001739 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001740 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001741 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001742 self.waitUntilSettled()
1743
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001744 self.assertEqual(len(self.builds), 9)
1745 self.assertEqual(self.builds[0].name, 'project1-test1')
1746 self.assertEqual(self.builds[1].name, 'project1-test2')
1747 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
1748 self.assertEqual(self.builds[3].name, 'project1-test1')
1749 self.assertEqual(self.builds[4].name, 'project1-test2')
1750 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
1751 self.assertEqual(self.builds[6].name, 'project1-test1')
1752 self.assertEqual(self.builds[7].name, 'project1-test2')
1753 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001754
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001755 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001756 self.waitUntilSettled()
1757
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001758 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1759 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001760
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001761 self.worker.hold_jobs_in_build = False
1762 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001763 self.waitUntilSettled()
1764
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001765 self.assertEqual(len(self.builds), 0)
1766 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001767
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001768 self.assertEqual(A.data['status'], 'NEW')
1769 self.assertEqual(B.data['status'], 'MERGED')
1770 self.assertEqual(C.data['status'], 'MERGED')
1771 self.assertEqual(A.reported, 2)
1772 self.assertEqual(B.reported, 2)
1773 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001774
1775 def test_nonvoting_job(self):
1776 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001777
James E. Blair4ec821f2012-08-23 15:28:28 -07001778 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1779 'master', 'A')
1780 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001781 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001782 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1783
1784 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001785
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001786 self.assertEqual(A.data['status'], 'MERGED')
1787 self.assertEqual(A.reported, 2)
1788 self.assertEqual(
1789 self.getJobFromHistory('nonvoting-project-merge').result,
1790 'SUCCESS')
1791 self.assertEqual(
1792 self.getJobFromHistory('nonvoting-project-test1').result,
1793 'SUCCESS')
1794 self.assertEqual(
1795 self.getJobFromHistory('nonvoting-project-test2').result,
1796 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001797
1798 def test_check_queue_success(self):
1799 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001800
James E. Blaire0487072012-08-29 17:38:31 -07001801 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1802 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1803
1804 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001805
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001806 self.assertEqual(A.data['status'], 'NEW')
1807 self.assertEqual(A.reported, 1)
1808 self.assertEqual(self.getJobFromHistory('project-merge').result,
1809 'SUCCESS')
1810 self.assertEqual(self.getJobFromHistory('project-test1').result,
1811 'SUCCESS')
1812 self.assertEqual(self.getJobFromHistory('project-test2').result,
1813 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001814
1815 def test_check_queue_failure(self):
1816 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001817
James E. Blaire0487072012-08-29 17:38:31 -07001818 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001819 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001820 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1821
1822 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001823
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001824 self.assertEqual(A.data['status'], 'NEW')
1825 self.assertEqual(A.reported, 1)
1826 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001827 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001828 self.assertEqual(self.getJobFromHistory('project-test1').result,
1829 'SUCCESS')
1830 self.assertEqual(self.getJobFromHistory('project-test2').result,
1831 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001832
1833 def test_dependent_behind_dequeue(self):
1834 "test that dependent changes behind dequeued changes work"
1835 # This complicated test is a reproduction of a real life bug
1836 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001837
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001838 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001839 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1840 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1841 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1842 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1843 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1844 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1845 D.setDependsOn(C, 1)
1846 E.setDependsOn(D, 1)
1847 A.addApproval('CRVW', 2)
1848 B.addApproval('CRVW', 2)
1849 C.addApproval('CRVW', 2)
1850 D.addApproval('CRVW', 2)
1851 E.addApproval('CRVW', 2)
1852 F.addApproval('CRVW', 2)
1853
1854 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001855
1856 # Change object re-use in the gerrit trigger is hidden if
1857 # changes are added in quick succession; waiting makes it more
1858 # like real life.
1859 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1860 self.waitUntilSettled()
1861 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1862 self.waitUntilSettled()
1863
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001864 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001865 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001866 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001867 self.waitUntilSettled()
1868
1869 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1870 self.waitUntilSettled()
1871 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1872 self.waitUntilSettled()
1873 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1874 self.waitUntilSettled()
1875 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1876 self.waitUntilSettled()
1877
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001878 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001879 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001880 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001881 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001882 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001883 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001884 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001885 self.waitUntilSettled()
1886
1887 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001888
1889 # Grab pointers to the jobs we want to release before
1890 # releasing any, because list indexes may change as
1891 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001892 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001893 a.release()
1894 b.release()
1895 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001896 self.waitUntilSettled()
1897
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001898 self.worker.hold_jobs_in_build = False
1899 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07001900 self.waitUntilSettled()
1901
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001902 self.assertEqual(A.data['status'], 'NEW')
1903 self.assertEqual(B.data['status'], 'MERGED')
1904 self.assertEqual(C.data['status'], 'MERGED')
1905 self.assertEqual(D.data['status'], 'MERGED')
1906 self.assertEqual(E.data['status'], 'MERGED')
1907 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001908
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001909 self.assertEqual(A.reported, 2)
1910 self.assertEqual(B.reported, 2)
1911 self.assertEqual(C.reported, 2)
1912 self.assertEqual(D.reported, 2)
1913 self.assertEqual(E.reported, 2)
1914 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001915
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001916 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1917 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001918
1919 def test_merger_repack(self):
1920 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001921
James E. Blair05fed602012-09-07 12:45:24 -07001922 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1923 A.addApproval('CRVW', 2)
1924 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1925 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001926 self.assertEqual(self.getJobFromHistory('project-merge').result,
1927 'SUCCESS')
1928 self.assertEqual(self.getJobFromHistory('project-test1').result,
1929 'SUCCESS')
1930 self.assertEqual(self.getJobFromHistory('project-test2').result,
1931 'SUCCESS')
1932 self.assertEqual(A.data['status'], 'MERGED')
1933 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001934 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07001935 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001936
Monty Taylorbc758832013-06-17 17:22:42 -04001937 path = os.path.join(self.git_root, "org/project")
1938 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001939
1940 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1941 A.addApproval('CRVW', 2)
1942 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1943 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001944 self.assertEqual(self.getJobFromHistory('project-merge').result,
1945 'SUCCESS')
1946 self.assertEqual(self.getJobFromHistory('project-test1').result,
1947 'SUCCESS')
1948 self.assertEqual(self.getJobFromHistory('project-test2').result,
1949 'SUCCESS')
1950 self.assertEqual(A.data['status'], 'MERGED')
1951 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001952
James E. Blair4886f282012-11-15 09:27:33 -08001953 def test_merger_repack_large_change(self):
1954 "Test that the merger works with large changes after a repack"
1955 # https://bugs.launchpad.net/zuul/+bug/1078946
1956 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1957 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001958 path = os.path.join(self.upstream_root, "org/project1")
1959 print repack_repo(path)
1960 path = os.path.join(self.git_root, "org/project1")
1961 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001962
1963 A.addApproval('CRVW', 2)
1964 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1965 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001966 self.assertEqual(self.getJobFromHistory('project1-merge').result,
1967 'SUCCESS')
1968 self.assertEqual(self.getJobFromHistory('project1-test1').result,
1969 'SUCCESS')
1970 self.assertEqual(self.getJobFromHistory('project1-test2').result,
1971 'SUCCESS')
1972 self.assertEqual(A.data['status'], 'MERGED')
1973 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001974
James E. Blair7ee88a22012-09-12 18:59:31 +02001975 def test_nonexistent_job(self):
1976 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001977 # Set to the state immediately after a restart
1978 self.resetGearmanServer()
1979 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02001980
1981 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1982 A.addApproval('CRVW', 2)
1983 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1984 # There may be a thread about to report a lost change
1985 while A.reported < 2:
1986 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001987 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001988 self.assertFalse(job_names)
1989 self.assertEqual(A.data['status'], 'NEW')
1990 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001991 self.assertEmptyQueues()
1992
1993 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001994 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02001995 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1996 A.addApproval('CRVW', 2)
1997 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1998 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001999 self.assertEqual(self.getJobFromHistory('project-merge').result,
2000 'SUCCESS')
2001 self.assertEqual(self.getJobFromHistory('project-test1').result,
2002 'SUCCESS')
2003 self.assertEqual(self.getJobFromHistory('project-test2').result,
2004 'SUCCESS')
2005 self.assertEqual(A.data['status'], 'MERGED')
2006 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002007
2008 def test_single_nonexistent_post_job(self):
2009 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002010 e = {
2011 "type": "ref-updated",
2012 "submitter": {
2013 "name": "User Name",
2014 },
2015 "refUpdate": {
2016 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2017 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2018 "refName": "master",
2019 "project": "org/project",
2020 }
2021 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002022 # Set to the state immediately after a restart
2023 self.resetGearmanServer()
2024 self.launcher.negative_function_cache_ttl = 0
2025
James E. Blairf62d4282012-12-31 17:01:50 -08002026 self.fake_gerrit.addEvent(e)
2027 self.waitUntilSettled()
2028
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002029 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002030
2031 def test_new_patchset_dequeues_old(self):
2032 "Test that a new patchset causes the old to be dequeued"
2033 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002034 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002035 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2036 M.setMerged()
2037
2038 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2039 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2040 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2041 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2042 A.addApproval('CRVW', 2)
2043 B.addApproval('CRVW', 2)
2044 C.addApproval('CRVW', 2)
2045 D.addApproval('CRVW', 2)
2046
2047 C.setDependsOn(B, 1)
2048 B.setDependsOn(A, 1)
2049 A.setDependsOn(M, 1)
2050
2051 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2052 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2053 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2054 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2055 self.waitUntilSettled()
2056
2057 B.addPatchset()
2058 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2059 self.waitUntilSettled()
2060
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002061 self.worker.hold_jobs_in_build = False
2062 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002063 self.waitUntilSettled()
2064
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002065 self.assertEqual(A.data['status'], 'MERGED')
2066 self.assertEqual(A.reported, 2)
2067 self.assertEqual(B.data['status'], 'NEW')
2068 self.assertEqual(B.reported, 2)
2069 self.assertEqual(C.data['status'], 'NEW')
2070 self.assertEqual(C.reported, 2)
2071 self.assertEqual(D.data['status'], 'MERGED')
2072 self.assertEqual(D.reported, 2)
2073 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002074
2075 def test_new_patchset_dequeues_old_on_head(self):
2076 "Test that a new patchset causes the old to be dequeued (at head)"
2077 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002078 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002079 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2080 M.setMerged()
2081 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2082 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2083 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2084 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2085 A.addApproval('CRVW', 2)
2086 B.addApproval('CRVW', 2)
2087 C.addApproval('CRVW', 2)
2088 D.addApproval('CRVW', 2)
2089
2090 C.setDependsOn(B, 1)
2091 B.setDependsOn(A, 1)
2092 A.setDependsOn(M, 1)
2093
2094 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2095 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2096 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2097 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2098 self.waitUntilSettled()
2099
2100 A.addPatchset()
2101 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2102 self.waitUntilSettled()
2103
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002104 self.worker.hold_jobs_in_build = False
2105 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002106 self.waitUntilSettled()
2107
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002108 self.assertEqual(A.data['status'], 'NEW')
2109 self.assertEqual(A.reported, 2)
2110 self.assertEqual(B.data['status'], 'NEW')
2111 self.assertEqual(B.reported, 2)
2112 self.assertEqual(C.data['status'], 'NEW')
2113 self.assertEqual(C.reported, 2)
2114 self.assertEqual(D.data['status'], 'MERGED')
2115 self.assertEqual(D.reported, 2)
2116 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002117
2118 def test_new_patchset_dequeues_old_without_dependents(self):
2119 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002120 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002121 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2122 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2123 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2124 A.addApproval('CRVW', 2)
2125 B.addApproval('CRVW', 2)
2126 C.addApproval('CRVW', 2)
2127
2128 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2129 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2130 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2131 self.waitUntilSettled()
2132
2133 B.addPatchset()
2134 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2135 self.waitUntilSettled()
2136
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002137 self.worker.hold_jobs_in_build = False
2138 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002139 self.waitUntilSettled()
2140
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002141 self.assertEqual(A.data['status'], 'MERGED')
2142 self.assertEqual(A.reported, 2)
2143 self.assertEqual(B.data['status'], 'NEW')
2144 self.assertEqual(B.reported, 2)
2145 self.assertEqual(C.data['status'], 'MERGED')
2146 self.assertEqual(C.reported, 2)
2147 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002148
2149 def test_new_patchset_dequeues_old_independent_queue(self):
2150 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002151 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002152 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2153 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2154 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2155 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2156 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2157 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2158 self.waitUntilSettled()
2159
2160 B.addPatchset()
2161 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2162 self.waitUntilSettled()
2163
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002164 self.worker.hold_jobs_in_build = False
2165 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002166 self.waitUntilSettled()
2167
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002168 self.assertEqual(A.data['status'], 'NEW')
2169 self.assertEqual(A.reported, 1)
2170 self.assertEqual(B.data['status'], 'NEW')
2171 self.assertEqual(B.reported, 1)
2172 self.assertEqual(C.data['status'], 'NEW')
2173 self.assertEqual(C.reported, 1)
2174 self.assertEqual(len(self.history), 10)
2175 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002176
2177 def test_zuul_refs(self):
2178 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002179 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002180 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2181 M1.setMerged()
2182 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2183 M2.setMerged()
2184
2185 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2186 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2187 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2188 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2189 A.addApproval('CRVW', 2)
2190 B.addApproval('CRVW', 2)
2191 C.addApproval('CRVW', 2)
2192 D.addApproval('CRVW', 2)
2193 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2194 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2195 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2196 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2197
2198 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002199 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002200 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002201 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002202 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002203 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002204 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002205 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002206 self.waitUntilSettled()
2207
James E. Blair7d0dedc2013-02-21 17:26:09 -08002208 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002209 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002210 if x.parameters['ZUUL_CHANGE'] == '3':
2211 a_zref = x.parameters['ZUUL_REF']
2212 if x.parameters['ZUUL_CHANGE'] == '4':
2213 b_zref = x.parameters['ZUUL_REF']
2214 if x.parameters['ZUUL_CHANGE'] == '5':
2215 c_zref = x.parameters['ZUUL_REF']
2216 if x.parameters['ZUUL_CHANGE'] == '6':
2217 d_zref = x.parameters['ZUUL_REF']
2218
2219 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002220 self.assertIsNotNone(a_zref)
2221 self.assertIsNotNone(b_zref)
2222 self.assertIsNotNone(c_zref)
2223 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002224
2225 # And they should all be different
2226 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002227 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002228
2229 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002230 self.assertTrue(self.ref_has_change(a_zref, A))
2231 self.assertFalse(self.ref_has_change(a_zref, B))
2232 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002233
2234 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002235 self.assertTrue(self.ref_has_change(b_zref, A))
2236 self.assertTrue(self.ref_has_change(b_zref, B))
2237 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002238
2239 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002240 self.assertTrue(self.ref_has_change(c_zref, A))
2241 self.assertTrue(self.ref_has_change(c_zref, B))
2242 self.assertTrue(self.ref_has_change(c_zref, C))
2243 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002244
2245 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002246 self.assertTrue(self.ref_has_change(d_zref, A))
2247 self.assertTrue(self.ref_has_change(d_zref, B))
2248 self.assertTrue(self.ref_has_change(d_zref, C))
2249 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002250
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002251 self.worker.hold_jobs_in_build = False
2252 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002253 self.waitUntilSettled()
2254
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002255 self.assertEqual(A.data['status'], 'MERGED')
2256 self.assertEqual(A.reported, 2)
2257 self.assertEqual(B.data['status'], 'MERGED')
2258 self.assertEqual(B.reported, 2)
2259 self.assertEqual(C.data['status'], 'MERGED')
2260 self.assertEqual(C.reported, 2)
2261 self.assertEqual(D.data['status'], 'MERGED')
2262 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002263
James E. Blair412e5582013-04-22 15:50:12 -07002264 def test_statsd(self):
2265 "Test each of the statsd methods used in the scheduler"
2266 import extras
2267 statsd = extras.try_import('statsd.statsd')
2268 statsd.incr('test-incr')
2269 statsd.timing('test-timing', 3)
2270 statsd.gauge('test-guage', 12)
2271 self.assertReportedStat('test-incr', '1|c')
2272 self.assertReportedStat('test-timing', '3|ms')
2273 self.assertReportedStat('test-guage', '12|g')
2274
James E. Blair70c71582013-03-06 08:50:50 -08002275 def test_file_jobs(self):
2276 "Test that file jobs run only when appropriate"
2277 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2278 A.addPatchset(['pip-requires'])
2279 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2280 A.addApproval('CRVW', 2)
2281 B.addApproval('CRVW', 2)
2282 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2283 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2284 self.waitUntilSettled()
2285
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002286 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002287 if x.name == 'project-testfile']
2288
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002289 self.assertEqual(len(testfile_jobs), 1)
2290 self.assertEqual(testfile_jobs[0].changes, '1,2')
2291 self.assertEqual(A.data['status'], 'MERGED')
2292 self.assertEqual(A.reported, 2)
2293 self.assertEqual(B.data['status'], 'MERGED')
2294 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002295
2296 def test_test_config(self):
2297 "Test that we can test the config"
2298 sched = zuul.scheduler.Scheduler()
2299 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002300
2301 def test_build_description(self):
2302 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002303 self.worker.registerFunction('set_description:' +
2304 self.worker.worker_id)
2305
2306 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2307 A.addApproval('CRVW', 2)
2308 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2309 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002310 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002311 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002312 self.assertTrue(re.search("Branch.*master", desc))
2313 self.assertTrue(re.search("Pipeline.*gate", desc))
2314 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2315 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2316 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2317 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002318
James E. Blair64ed6f22013-07-10 14:07:23 -07002319 def test_queue_precedence(self):
2320 "Test that queue precedence works"
2321
2322 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002323 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002324 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2325 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2326 A.addApproval('CRVW', 2)
2327 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2328
2329 self.waitUntilSettled()
2330 self.gearman_server.hold_jobs_in_queue = False
2331 self.gearman_server.release()
2332 self.waitUntilSettled()
2333
James E. Blair8de58bd2013-07-18 16:23:33 -07002334 # Run one build at a time to ensure non-race order:
2335 for x in range(6):
2336 self.release(self.builds[0])
2337 self.waitUntilSettled()
2338 self.worker.hold_jobs_in_build = False
2339 self.waitUntilSettled()
2340
James E. Blair64ed6f22013-07-10 14:07:23 -07002341 self.log.debug(self.history)
2342 self.assertEqual(self.history[0].pipeline, 'gate')
2343 self.assertEqual(self.history[1].pipeline, 'check')
2344 self.assertEqual(self.history[2].pipeline, 'gate')
2345 self.assertEqual(self.history[3].pipeline, 'gate')
2346 self.assertEqual(self.history[4].pipeline, 'check')
2347 self.assertEqual(self.history[5].pipeline, 'check')
2348
James E. Blair1843a552013-07-03 14:19:52 -07002349 def test_json_status(self):
2350 "Test that we can retrieve JSON status info"
2351 self.worker.hold_jobs_in_build = True
2352 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2353 A.addApproval('CRVW', 2)
2354 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2355 self.waitUntilSettled()
2356
2357 port = self.webapp.server.socket.getsockname()[1]
2358
2359 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2360 data = f.read()
2361
2362 self.worker.hold_jobs_in_build = False
2363 self.worker.release()
2364 self.waitUntilSettled()
2365
2366 data = json.loads(data)
2367 status_jobs = set()
2368 for p in data['pipelines']:
2369 for q in p['change_queues']:
2370 for head in q['heads']:
2371 for change in head:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002372 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002373 for job in change['jobs']:
2374 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002375 self.assertIn('project-merge', status_jobs)
2376 self.assertIn('project-test1', status_jobs)
2377 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002378
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002379 def test_node_label(self):
2380 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002381 self.worker.registerFunction('build:node-project-test1:debian')
2382
2383 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2384 A.addApproval('CRVW', 2)
2385 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2386 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002387
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002388 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2389 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2390 'debian')
2391 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002392
2393 def test_live_reconfiguration(self):
2394 "Test that live reconfiguration works"
2395 self.worker.hold_jobs_in_build = True
2396 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2397 A.addApproval('CRVW', 2)
2398 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2399 self.waitUntilSettled()
2400
2401 self.sched.reconfigure(self.config)
2402
2403 self.worker.hold_jobs_in_build = False
2404 self.worker.release()
2405 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002406 self.assertEqual(self.getJobFromHistory('project-merge').result,
2407 'SUCCESS')
2408 self.assertEqual(self.getJobFromHistory('project-test1').result,
2409 'SUCCESS')
2410 self.assertEqual(self.getJobFromHistory('project-test2').result,
2411 'SUCCESS')
2412 self.assertEqual(A.data['status'], 'MERGED')
2413 self.assertEqual(A.reported, 2)
James E. Blaircdccd972013-07-01 12:10:22 -07002414 self.assertEmptyQueues()