blob: 334776b2f720941e2849f2e30ce2429a790d0d81 [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
James E. Blair287c06d2013-07-24 10:39:30 -0700151 r = repo.index.commit(msg)
152 repo.head.reference = 'master'
153 repo.head.reset(index=True, working_tree=True)
154 repo.git.clean('-x', '-f', '-d')
155 return r
Monty Taylorbc758832013-06-17 17:22:42 -0400156
James E. Blair70c71582013-03-06 08:50:50 -0800157 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700158 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700159 if files:
160 fn = files[0]
161 else:
162 fn = '%s-%s' % (self.branch, self.number)
163 msg = self.subject + '-' + str(self.latest_patchset)
Monty Taylorbc758832013-06-17 17:22:42 -0400164 c = self.add_fake_change_to_repo(msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800165 ps_files = [{'file': '/COMMIT_MSG',
166 'type': 'ADDED'},
167 {'file': 'README',
168 'type': 'MODIFIED'}]
169 for f in files:
170 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700171 d = {'approvals': [],
172 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800173 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700174 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700175 'ref': 'refs/changes/1/%s/%s' % (self.number,
176 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700177 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700178 'uploader': {'email': 'user@example.com',
179 'name': 'User name',
180 'username': 'user'}}
181 self.data['currentPatchSet'] = d
182 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700183 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700184
James E. Blaire0487072012-08-29 17:38:31 -0700185 def getPatchsetCreatedEvent(self, patchset):
186 event = {"type": "patchset-created",
187 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800188 "branch": self.branch,
189 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
190 "number": str(self.number),
191 "subject": self.subject,
192 "owner": {"name": "User Name"},
193 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700194 "patchSet": self.patchsets[patchset - 1],
195 "uploader": {"name": "User Name"}}
196 return event
197
James E. Blair42f74822013-05-14 15:18:03 -0700198 def getChangeRestoredEvent(self):
199 event = {"type": "change-restored",
200 "change": {"project": self.project,
201 "branch": self.branch,
202 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
203 "number": str(self.number),
204 "subject": self.subject,
205 "owner": {"name": "User Name"},
206 "url": "https://hostname/3"},
207 "restorer": {"name": "User Name"},
208 "reason": ""}
209 return event
210
James E. Blairb0fcae42012-07-17 11:12:10 -0700211 def addApproval(self, category, value):
James E. Blair8c803f82012-07-31 16:25:42 -0700212 approval = {'description': self.categories[category][0],
213 'type': category,
214 'value': str(value)}
215 self.patchsets[-1]['approvals'].append(approval)
216 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700217 'author': {'email': 'user@example.com',
218 'name': 'User Name',
219 'username': 'username'},
220 'change': {'branch': self.branch,
221 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
222 'number': str(self.number),
223 'owner': {'email': 'user@example.com',
224 'name': 'User Name',
225 'username': 'username'},
226 'project': self.project,
227 'subject': self.subject,
228 'topic': 'master',
229 'url': 'https://hostname/459'},
230 'comment': '',
231 'patchSet': self.patchsets[-1],
232 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700233 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700234 return json.loads(json.dumps(event))
235
James E. Blair8c803f82012-07-31 16:25:42 -0700236 def getSubmitRecords(self):
237 status = {}
238 for cat in self.categories.keys():
239 status[cat] = 0
240
241 for a in self.patchsets[-1]['approvals']:
242 cur = status[a['type']]
243 cat_min, cat_max = self.categories[a['type']][1:]
244 new = int(a['value'])
245 if new == cat_min:
246 cur = new
247 elif abs(new) > abs(cur):
248 cur = new
249 status[a['type']] = cur
250
251 labels = []
252 ok = True
253 for typ, cat in self.categories.items():
254 cur = status[typ]
255 cat_min, cat_max = cat[1:]
256 if cur == cat_min:
257 value = 'REJECT'
258 ok = False
259 elif cur == cat_max:
260 value = 'OK'
261 else:
262 value = 'NEED'
263 ok = False
264 labels.append({'label': cat[0], 'status': value})
265 if ok:
266 return [{'status': 'OK'}]
267 return [{'status': 'NOT_READY',
268 'labels': labels}]
269
270 def setDependsOn(self, other, patchset):
271 self.depends_on_change = other
272 d = {'id': other.data['id'],
273 'number': other.data['number'],
274 'ref': other.patchsets[patchset - 1]['ref']
275 }
276 self.data['dependsOn'] = [d]
277
278 other.needed_by_changes.append(self)
279 needed = other.data.get('neededBy', [])
280 d = {'id': self.data['id'],
281 'number': self.data['number'],
282 'ref': self.patchsets[patchset - 1]['ref'],
283 'revision': self.patchsets[patchset - 1]['revision']
284 }
285 needed.append(d)
286 other.data['neededBy'] = needed
287
James E. Blairb0fcae42012-07-17 11:12:10 -0700288 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700289 self.queried += 1
290 d = self.data.get('dependsOn')
291 if d:
292 d = d[0]
293 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
294 d['isCurrentPatchSet'] = True
295 else:
296 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700297 return json.loads(json.dumps(self.data))
298
299 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800300 if (self.depends_on_change and
301 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700302 return
James E. Blair127bc182012-08-28 15:55:15 -0700303 if self.fail_merge:
304 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700305 self.data['status'] = 'MERGED'
306 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700307
Monty Taylorbc758832013-06-17 17:22:42 -0400308 path = os.path.join(self.upstream_root, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700309 repo = git.Repo(path)
310 repo.heads[self.branch].commit = \
311 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700312
James E. Blaird466dc42012-07-31 10:42:56 -0700313 def setReported(self):
314 self.reported += 1
315
James E. Blairb0fcae42012-07-17 11:12:10 -0700316
317class FakeGerrit(object):
318 def __init__(self, *args, **kw):
319 self.event_queue = Queue.Queue()
320 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
321 self.change_number = 0
322 self.changes = {}
323
324 def addFakeChange(self, project, branch, subject):
325 self.change_number += 1
Monty Taylorbc758832013-06-17 17:22:42 -0400326 c = FakeChange(self, self.change_number, project, branch, subject,
327 upstream_root=self.upstream_root)
James E. Blairb0fcae42012-07-17 11:12:10 -0700328 self.changes[self.change_number] = c
329 return c
330
331 def addEvent(self, data):
332 return self.event_queue.put(data)
333
334 def getEvent(self):
335 return self.event_queue.get()
336
337 def eventDone(self):
338 self.event_queue.task_done()
339
340 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700341 number, ps = changeid.split(',')
342 change = self.changes[int(number)]
James E. Blair42f74822013-05-14 15:18:03 -0700343 change.messages.append(message)
James E. Blairb0fcae42012-07-17 11:12:10 -0700344 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700345 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700346 if message:
347 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700348
349 def query(self, number):
350 change = self.changes[int(number)]
351 return change.query()
352
353 def startWatching(self, *args, **kw):
354 pass
355
356
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700357class BuildHistory(object):
358 def __init__(self, **kw):
359 self.__dict__.update(kw)
James E. Blairb0fcae42012-07-17 11:12:10 -0700360
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700361 def __repr__(self):
362 return ("<Completed build, result: %s name: %s #%s changes: %s>" %
363 (self.result, self.name, self.number, self.changes))
James E. Blairb0fcae42012-07-17 11:12:10 -0700364
365
James E. Blair8cc15a82012-08-01 11:17:57 -0700366class FakeURLOpener(object):
Monty Taylorbc758832013-06-17 17:22:42 -0400367 def __init__(self, upstream_root, fake_gerrit, url):
368 self.upstream_root = upstream_root
James E. Blair8cc15a82012-08-01 11:17:57 -0700369 self.fake_gerrit = fake_gerrit
370 self.url = url
371
372 def read(self):
373 res = urlparse.urlparse(self.url)
374 path = res.path
375 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200376 ret = '001e# service=git-upload-pack\n'
377 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
378 'multi_ack thin-pack side-band side-band-64k ofs-delta '
379 'shallow no-progress include-tag multi_ack_detailed no-done\n')
Monty Taylorbc758832013-06-17 17:22:42 -0400380 path = os.path.join(self.upstream_root, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700381 repo = git.Repo(path)
382 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200383 r = ref.object.hexsha + ' ' + ref.path + '\n'
384 ret += '%04x%s' % (len(r) + 4, r)
385 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700386 return ret
387
388
James E. Blair4886cc12012-07-18 15:39:41 -0700389class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
Monty Taylorbc758832013-06-17 17:22:42 -0400390 def __init__(self, upstream_root, *args):
391 super(FakeGerritTrigger, self).__init__(*args)
392 self.upstream_root = upstream_root
393
James E. Blair4886cc12012-07-18 15:39:41 -0700394 def getGitUrl(self, project):
Monty Taylorbc758832013-06-17 17:22:42 -0400395 return os.path.join(self.upstream_root, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700396
397
James E. Blair412e5582013-04-22 15:50:12 -0700398class FakeStatsd(threading.Thread):
399 def __init__(self):
400 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400401 self.daemon = True
James E. Blair412e5582013-04-22 15:50:12 -0700402 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
403 self.sock.bind(('', 0))
404 self.port = self.sock.getsockname()[1]
405 self.wake_read, self.wake_write = os.pipe()
406 self.stats = []
407
408 def run(self):
409 while True:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700410 poll = select.poll()
411 poll.register(self.sock, select.POLLIN)
412 poll.register(self.wake_read, select.POLLIN)
413 ret = poll.poll()
414 for (fd, event) in ret:
415 if fd == self.sock.fileno():
James E. Blair412e5582013-04-22 15:50:12 -0700416 data = self.sock.recvfrom(1024)
417 if not data:
418 return
419 self.stats.append(data[0])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700420 if fd == self.wake_read:
James E. Blair412e5582013-04-22 15:50:12 -0700421 return
422
423 def stop(self):
424 os.write(self.wake_write, '1\n')
425
426
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700427class FakeBuild(threading.Thread):
428 log = logging.getLogger("zuul.test")
429
430 def __init__(self, worker, job, number, node):
431 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400432 self.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700433 self.worker = worker
434 self.job = job
435 self.name = job.name.split(':')[1]
436 self.number = number
437 self.node = node
438 self.parameters = json.loads(job.arguments)
439 self.unique = self.parameters['ZUUL_UUID']
440 self.wait_condition = threading.Condition()
441 self.waiting = False
442 self.aborted = False
443 self.created = time.time()
444 self.description = ''
445
446 def release(self):
447 self.wait_condition.acquire()
448 self.wait_condition.notify()
449 self.waiting = False
450 self.log.debug("Build %s released" % self.unique)
451 self.wait_condition.release()
452
453 def isWaiting(self):
454 self.wait_condition.acquire()
455 if self.waiting:
456 ret = True
457 else:
458 ret = False
459 self.wait_condition.release()
460 return ret
461
462 def _wait(self):
463 self.wait_condition.acquire()
464 self.waiting = True
465 self.log.debug("Build %s waiting" % self.unique)
466 self.wait_condition.wait()
467 self.wait_condition.release()
468
469 def run(self):
470 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700471 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
472 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700473 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700474 'manager': self.worker.worker_id,
Paul Belangerec49b4c2013-07-20 20:32:20 -0400475 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700476
477 self.job.sendWorkData(json.dumps(data))
478 self.job.sendWorkStatus(0, 100)
479
480 if self.worker.hold_jobs_in_build:
481 self._wait()
482 self.log.debug("Build %s continuing" % self.unique)
483
484 self.worker.lock.acquire()
485
486 result = 'SUCCESS'
487 if (('ZUUL_REF' in self.parameters) and
488 self.worker.shouldFailTest(self.name,
489 self.parameters['ZUUL_REF'])):
490 result = 'FAILURE'
491 if self.aborted:
492 result = 'ABORTED'
493
494 data = {'result': result}
495 changes = None
496 if 'ZUUL_CHANGE_IDS' in self.parameters:
497 changes = self.parameters['ZUUL_CHANGE_IDS']
498
499 self.worker.build_history.append(
500 BuildHistory(name=self.name, number=self.number,
501 result=result, changes=changes, node=self.node,
James E. Blair64ed6f22013-07-10 14:07:23 -0700502 uuid=self.unique, description=self.description,
503 pipeline=self.parameters['ZUUL_PIPELINE'])
Paul Belangerec49b4c2013-07-20 20:32:20 -0400504 )
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700505
506 self.job.sendWorkComplete(json.dumps(data))
507 del self.worker.gearman_jobs[self.job.unique]
508 self.worker.running_builds.remove(self)
509 self.worker.lock.release()
510
511
512class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400513 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700514 super(FakeWorker, self).__init__(worker_id)
515 self.gearman_jobs = {}
516 self.build_history = []
517 self.running_builds = []
518 self.build_counter = 0
519 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400520 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700521
522 self.hold_jobs_in_build = False
523 self.lock = threading.Lock()
524 self.__work_thread = threading.Thread(target=self.work)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400525 self.__work_thread.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700526 self.__work_thread.start()
527
528 def handleJob(self, job):
529 parts = job.name.split(":")
530 cmd = parts[0]
531 name = parts[1]
532 if len(parts) > 2:
533 node = parts[2]
534 else:
535 node = None
536 if cmd == 'build':
537 self.handleBuild(job, name, node)
538 elif cmd == 'stop':
539 self.handleStop(job, name)
540 elif cmd == 'set_description':
541 self.handleSetDescription(job, name)
542
543 def handleBuild(self, job, name, node):
544 build = FakeBuild(self, job, self.build_counter, node)
545 job.build = build
546 self.gearman_jobs[job.unique] = job
547 self.build_counter += 1
548
549 self.running_builds.append(build)
550 build.start()
551
552 def handleStop(self, job, name):
553 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700554 parameters = json.loads(job.arguments)
555 name = parameters['name']
556 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700557 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700558 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700559 build.aborted = True
560 build.release()
561 job.sendWorkComplete()
562 return
563 job.sendWorkFail()
564
565 def handleSetDescription(self, job, name):
566 self.log.debug("handle set description")
567 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700568 name = parameters['name']
569 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700570 descr = parameters['html_description']
571 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700572 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700573 build.description = descr
574 job.sendWorkComplete()
575 return
576 for build in self.build_history:
James E. Blair3c483cf2013-06-04 16:30:43 -0700577 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700578 build.description = descr
579 job.sendWorkComplete()
580 return
581 job.sendWorkFail()
582
583 def work(self):
584 while self.running:
585 try:
586 job = self.getJob()
587 except gear.InterruptedError:
588 continue
589 try:
590 self.handleJob(job)
591 except:
592 self.log.exception("Worker exception:")
593
594 def addFailTest(self, name, change):
595 l = self.fail_tests.get(name, [])
596 l.append(change)
597 self.fail_tests[name] = l
598
599 def shouldFailTest(self, name, ref):
600 l = self.fail_tests.get(name, [])
601 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400602 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700603 return True
604 return False
605
606 def release(self, regex=None):
607 builds = self.running_builds[:]
608 self.log.debug("releasing build %s (%s)" % (regex,
James E. Blair78e31b32013-07-09 09:11:34 -0700609 len(self.running_builds)))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700610 for build in builds:
611 if not regex or re.match(regex, build.name):
612 self.log.debug("releasing build %s" %
613 (build.parameters['ZUUL_UUID']))
614 build.release()
615 else:
616 self.log.debug("not releasing build %s" %
617 (build.parameters['ZUUL_UUID']))
618 self.log.debug("done releasing builds %s (%s)" %
619 (regex, len(self.running_builds)))
620
621
622class FakeGearmanServer(gear.Server):
623 def __init__(self):
624 self.hold_jobs_in_queue = False
625 super(FakeGearmanServer, self).__init__(0)
626
627 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700628 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
629 for job in queue:
630 if not hasattr(job, 'waiting'):
631 if job.name.startswith('build:'):
632 job.waiting = self.hold_jobs_in_queue
633 else:
634 job.waiting = False
635 if job.waiting:
636 continue
637 if job.name in connection.functions:
638 if not peek:
639 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400640 connection.related_jobs[job.handle] = job
641 job.worker_connection = connection
642 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700643 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700644 return None
645
646 def release(self, regex=None):
647 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700648 qlen = (len(self.high_queue) + len(self.normal_queue) +
649 len(self.low_queue))
650 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
651 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
652 queue = queue[:]
653 for job in queue:
654 cmd, name = job.name.split(':')
655 if cmd != 'build':
656 continue
657 if not regex or re.match(regex, name):
658 self.log.debug("releasing queued job %s" %
659 job.unique)
660 job.waiting = False
661 released = True
662 else:
663 self.log.debug("not releasing queued job %s" %
664 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700665 if released:
666 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700667 qlen = (len(self.high_queue) + len(self.normal_queue) +
668 len(self.low_queue))
669 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700670
671
Monty Taylorbc758832013-06-17 17:22:42 -0400672class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700673 log = logging.getLogger("zuul.test")
674
675 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400676 super(TestScheduler, self).setUp()
677 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
678 try:
679 test_timeout = int(test_timeout)
680 except ValueError:
681 # If timeout value is invalid do not set a timeout.
682 test_timeout = 0
683 if test_timeout > 0:
684 self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
685
686 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
687 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
688 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
689 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
690 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
691 os.environ.get('OS_STDERR_CAPTURE') == '1'):
692 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
693 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Monty Taylore6a6c402013-07-02 09:25:55 -0700694 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
695 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400696 self.useFixture(fixtures.FakeLogger(
697 level=logging.DEBUG,
698 format='%(asctime)s %(name)-32s '
699 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700700 tmp_root = self.useFixture(fixtures.TempDir(
701 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400702 self.test_root = os.path.join(tmp_root, "zuul-test")
703 self.upstream_root = os.path.join(self.test_root, "upstream")
704 self.git_root = os.path.join(self.test_root, "git")
705
706 CONFIG.set('zuul', 'git_dir', self.git_root)
707 if os.path.exists(self.test_root):
708 shutil.rmtree(self.test_root)
709 os.makedirs(self.test_root)
710 os.makedirs(self.upstream_root)
711 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700712
713 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400714 self.init_repo("org/project")
715 self.init_repo("org/project1")
716 self.init_repo("org/project2")
717 self.init_repo("org/project3")
718 self.init_repo("org/one-job-project")
719 self.init_repo("org/nonvoting-project")
720 self.init_repo("org/templated-project")
721 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700722 self.init_repo("org/conflict-project")
James E. Blair412e5582013-04-22 15:50:12 -0700723
724 self.statsd = FakeStatsd()
725 os.environ['STATSD_HOST'] = 'localhost'
726 os.environ['STATSD_PORT'] = str(self.statsd.port)
727 self.statsd.start()
728 # the statsd client object is configured in the statsd module import
729 reload(statsd)
730 reload(zuul.scheduler)
731
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700732 self.gearman_server = FakeGearmanServer()
733
734 self.config = ConfigParser.ConfigParser()
735 cfg = StringIO()
736 CONFIG.write(cfg)
737 cfg.seek(0)
738 self.config.readfp(cfg)
739 self.config.set('gearman', 'port', str(self.gearman_server.port))
740
Monty Taylorbc758832013-06-17 17:22:42 -0400741 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700742 self.worker.addServer('127.0.0.1', self.gearman_server.port)
743 self.gearman_server.worker = self.worker
744
James E. Blairb0fcae42012-07-17 11:12:10 -0700745 self.sched = zuul.scheduler.Scheduler()
746
James E. Blair8cc15a82012-08-01 11:17:57 -0700747 def URLOpenerFactory(*args, **kw):
748 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400749 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700750
James E. Blair8cc15a82012-08-01 11:17:57 -0700751 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700752 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700753
754 zuul.lib.gerrit.Gerrit = FakeGerrit
755
Monty Taylorbc758832013-06-17 17:22:42 -0400756 self.gerrit = FakeGerritTrigger(
757 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700758 self.gerrit.replication_timeout = 1.5
759 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700760 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400761 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700762
James E. Blair1843a552013-07-03 14:19:52 -0700763 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
764
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700765 self.sched.setLauncher(self.launcher)
James E. Blairb0fcae42012-07-17 11:12:10 -0700766 self.sched.setTrigger(self.gerrit)
767
768 self.sched.start()
769 self.sched.reconfigure(self.config)
770 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700771 self.webapp.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700772 self.launcher.gearman.waitForServer()
773 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400774 self.builds = self.worker.running_builds
775 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700776
James E. Blairfee8d652013-06-07 08:57:52 -0700777 self.addCleanup(self.assertFinalState)
778 self.addCleanup(self.shutdown)
779
780 def assertFinalState(self):
781 # Make sure that the change cache is cleared
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400782 self.assertEqual(len(self.sched.trigger._change_cache.keys()), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700783 self.assertEmptyQueues()
784
785 def shutdown(self):
786 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700787 self.launcher.stop()
788 self.worker.shutdown()
789 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700790 self.gerrit.stop()
791 self.sched.stop()
792 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700793 self.statsd.stop()
794 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700795 self.webapp.stop()
796 self.webapp.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700797 threads = threading.enumerate()
798 if len(threads) > 1:
799 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400800 super(TestScheduler, self).tearDown()
801
802 def init_repo(self, project):
803 parts = project.split('/')
804 path = os.path.join(self.upstream_root, *parts[:-1])
805 if not os.path.exists(path):
806 os.makedirs(path)
807 path = os.path.join(self.upstream_root, project)
808 repo = git.Repo.init(path)
809
810 repo.config_writer().set_value('user', 'email', 'user@example.com')
811 repo.config_writer().set_value('user', 'name', 'User Name')
812 repo.config_writer().write()
813
814 fn = os.path.join(path, 'README')
815 f = open(fn, 'w')
816 f.write("test\n")
817 f.close()
818 repo.index.add([fn])
819 repo.index.commit('initial commit')
820 master = repo.create_head('master')
821 repo.create_tag('init')
822
823 mp = repo.create_head('mp')
824 repo.head.reference = mp
825 f = open(fn, 'a')
826 f.write("test mp\n")
827 f.close()
828 repo.index.add([fn])
829 repo.index.commit('mp commit')
830
831 repo.head.reference = master
832 repo.head.reset(index=True, working_tree=True)
833 repo.git.clean('-x', '-f', '-d')
834
835 def ref_has_change(self, ref, change):
836 path = os.path.join(self.git_root, change.project)
837 repo = git.Repo(path)
838 for commit in repo.iter_commits(ref):
839 if commit.message.strip() == ('%s-1' % change.subject):
840 return True
841 return False
842
843 def job_has_changes(self, *args):
844 job = args[0]
845 commits = args[1:]
846 if isinstance(job, FakeBuild):
847 parameters = job.parameters
848 else:
849 parameters = json.loads(job.arguments)
850 project = parameters['ZUUL_PROJECT']
851 path = os.path.join(self.git_root, project)
852 repo = git.Repo(path)
853 ref = parameters['ZUUL_REF']
854 sha = parameters['ZUUL_COMMIT']
855 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
856 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
857 commit_messages = ['%s-1' % commit.subject for commit in commits]
858 for msg in commit_messages:
859 if msg not in repo_messages:
860 return False
861 if repo_shas[0] != sha:
862 return False
863 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700864
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700865 def registerJobs(self):
866 count = 0
James E. Blaireff88162013-07-01 12:44:14 -0400867 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700868 self.worker.registerFunction('build:' + job)
869 count += 1
870 self.worker.registerFunction('stop:' + self.worker.worker_id)
871 count += 1
872
873 while len(self.gearman_server.functions) < count:
874 time.sleep(0)
875
876 def release(self, job):
877 if isinstance(job, FakeBuild):
878 job.release()
879 else:
880 job.waiting = False
881 self.log.debug("Queued job %s released" % job.unique)
882 self.gearman_server.wakeConnections()
883
884 def getParameter(self, job, name):
885 if isinstance(job, FakeBuild):
886 return job.parameters[name]
887 else:
888 parameters = json.loads(job.arguments)
889 return parameters[name]
890
891 def resetGearmanServer(self):
892 self.worker.setFunctions([])
893 while True:
894 done = True
895 for connection in self.gearman_server.active_connections:
896 if connection.functions:
897 done = False
898 if done:
899 break
900 time.sleep(0)
901 self.gearman_server.functions = set()
902
903 def haveAllBuildsReported(self):
904 # See if Zuul is waiting on a meta job to complete
905 if self.launcher.meta_jobs:
906 return False
907 # Find out if every build that the worker has completed has been
908 # reported back to Zuul. If it hasn't then that means a Gearman
909 # event is still in transit and the system is not stable.
910 for build in self.worker.build_history:
911 zbuild = self.launcher.builds.get(build.uuid)
912 if not zbuild:
913 # It has already been reported
914 continue
915 # It hasn't been reported yet.
916 return False
917 # Make sure that none of the worker connections are in GRAB_WAIT
918 for connection in self.worker.active_connections:
919 if connection.state == 'GRAB_WAIT':
920 return False
921 return True
922
923 def areAllBuildsWaiting(self):
924 ret = True
925
926 builds = self.launcher.builds.values()
927 for build in builds:
928 client_job = None
929 for conn in self.launcher.gearman.active_connections:
930 for j in conn.related_jobs.values():
931 if j.unique == build.uuid:
932 client_job = j
933 break
934 if not client_job:
935 self.log.debug("%s is not known to the gearman client" %
936 build)
937 ret = False
938 continue
939 if not client_job.handle:
940 self.log.debug("%s has no handle" % client_job)
941 ret = False
942 continue
943 server_job = self.gearman_server.jobs.get(client_job.handle)
944 if not server_job:
945 self.log.debug("%s is not known to the gearman server" %
946 client_job)
947 ret = False
948 continue
949 if not hasattr(server_job, 'waiting'):
950 self.log.debug("%s is being enqueued" % server_job)
951 ret = False
952 continue
953 if server_job.waiting:
954 continue
955 worker_job = self.worker.gearman_jobs.get(server_job.unique)
956 if worker_job:
957 if worker_job.build.isWaiting():
958 continue
959 else:
960 self.log.debug("%s is running" % worker_job)
961 ret = False
962 else:
963 self.log.debug("%s is unassigned" % server_job)
964 ret = False
965 return ret
966
James E. Blairb0fcae42012-07-17 11:12:10 -0700967 def waitUntilSettled(self):
968 self.log.debug("Waiting until settled...")
969 start = time.time()
970 while True:
971 if time.time() - start > 10:
972 print 'queue status:',
973 print self.sched.trigger_event_queue.empty(),
974 print self.sched.result_event_queue.empty(),
975 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700976 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -0700977 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700978 # Make sure no new events show up while we're checking
979 self.worker.lock.acquire()
980 # have all build states propogated to zuul?
981 if self.haveAllBuildsReported():
982 # Join ensures that the queue is empty _and_ events have been
983 # processed
984 self.fake_gerrit.event_queue.join()
985 self.sched.trigger_event_queue.join()
986 self.sched.result_event_queue.join()
987 if (self.sched.trigger_event_queue.empty() and
988 self.sched.result_event_queue.empty() and
989 self.fake_gerrit.event_queue.empty() and
990 self.areAllBuildsWaiting()):
991 self.worker.lock.release()
992 self.log.debug("...settled.")
993 return
994 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700995 self.sched.wake_event.wait(0.1)
996
James E. Blaird466dc42012-07-31 10:42:56 -0700997 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -0800998 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -0700999 return len(jobs)
1000
James E. Blair4ca985f2013-05-30 12:27:43 -07001001 def getJobFromHistory(self, name):
1002 history = self.worker.build_history
1003 for job in history:
1004 if job.name == name:
1005 return job
1006 raise Exception("Unable to find job %s in history" % name)
1007
James E. Blaire0487072012-08-29 17:38:31 -07001008 def assertEmptyQueues(self):
1009 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001010 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001011 for queue in pipeline.queues:
1012 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001013 print 'pipeline %s queue %s contents %s' % (
1014 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001015 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001016 if len(queue.severed_heads) != 0:
1017 print 'heads', queue.severed_heads
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001018 self.assertEqual(len(queue.severed_heads), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001019
James E. Blair412e5582013-04-22 15:50:12 -07001020 def assertReportedStat(self, key, value=None):
1021 start = time.time()
1022 while time.time() < (start + 5):
1023 for stat in self.statsd.stats:
1024 k, v = stat.split(':')
1025 if key == k:
1026 if value is None:
1027 return
1028 if value == v:
1029 return
1030 time.sleep(0.1)
1031
1032 pprint.pprint(self.statsd.stats)
1033 raise Exception("Key %s not found in reported stats" % key)
1034
James E. Blairb0fcae42012-07-17 11:12:10 -07001035 def test_jobs_launched(self):
1036 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001037
James E. Blairb0fcae42012-07-17 11:12:10 -07001038 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001039 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001040 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1041 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001042 self.assertEqual(self.getJobFromHistory('project-merge').result,
1043 'SUCCESS')
1044 self.assertEqual(self.getJobFromHistory('project-test1').result,
1045 'SUCCESS')
1046 self.assertEqual(self.getJobFromHistory('project-test2').result,
1047 'SUCCESS')
1048 self.assertEqual(A.data['status'], 'MERGED')
1049 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001050
James E. Blair412e5582013-04-22 15:50:12 -07001051 self.assertReportedStat('gerrit.event.comment-added', '1|c')
1052 self.assertReportedStat('zuul.pipeline.gate.current_changes', '1|g')
1053 self.assertReportedStat('zuul.job.project-merge')
1054 self.assertReportedStat('zuul.pipeline.gate.resident_time')
1055 self.assertReportedStat('zuul.pipeline.gate.total_changes', '1|c')
1056 self.assertReportedStat(
1057 'zuul.pipeline.gate.org.project.resident_time')
1058 self.assertReportedStat(
1059 'zuul.pipeline.gate.org.project.total_changes', '1|c')
1060
James E. Blair42f74822013-05-14 15:18:03 -07001061 def test_duplicate_pipelines(self):
1062 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001063
James E. Blair42f74822013-05-14 15:18:03 -07001064 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1065 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1066 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001067
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001068 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001069 self.history[0].name == 'project-test1'
1070 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001071
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001072 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001073 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001074 self.assertIn('dup1/project-test1', A.messages[0])
1075 self.assertNotIn('dup2/project-test1', A.messages[0])
1076 self.assertNotIn('dup1/project-test1', A.messages[1])
1077 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001078 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001079 self.assertIn('dup1/project-test1', A.messages[1])
1080 self.assertNotIn('dup2/project-test1', A.messages[1])
1081 self.assertNotIn('dup1/project-test1', A.messages[0])
1082 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001083
James E. Blairb0fcae42012-07-17 11:12:10 -07001084 def test_parallel_changes(self):
1085 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001086
1087 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001088 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1089 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1090 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001091 A.addApproval('CRVW', 2)
1092 B.addApproval('CRVW', 2)
1093 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001094
1095 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1096 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1097 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1098
1099 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001100 self.assertEqual(len(self.builds), 1)
1101 self.assertEqual(self.builds[0].name, 'project-merge')
1102 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001103
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001104 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001105 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001106 self.assertEqual(len(self.builds), 3)
1107 self.assertEqual(self.builds[0].name, 'project-test1')
1108 self.assertTrue(self.job_has_changes(self.builds[0], A))
1109 self.assertEqual(self.builds[1].name, 'project-test2')
1110 self.assertTrue(self.job_has_changes(self.builds[1], A))
1111 self.assertEqual(self.builds[2].name, 'project-merge')
1112 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001113
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001114 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001115 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001116 self.assertEqual(len(self.builds), 5)
1117 self.assertEqual(self.builds[0].name, 'project-test1')
1118 self.assertTrue(self.job_has_changes(self.builds[0], A))
1119 self.assertEqual(self.builds[1].name, 'project-test2')
1120 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001121
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001122 self.assertEqual(self.builds[2].name, 'project-test1')
1123 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1124 self.assertEqual(self.builds[3].name, 'project-test2')
1125 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001126
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001127 self.assertEqual(self.builds[4].name, 'project-merge')
1128 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001129
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001130 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001131 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001132 self.assertEqual(len(self.builds), 6)
1133 self.assertEqual(self.builds[0].name, 'project-test1')
1134 self.assertTrue(self.job_has_changes(self.builds[0], A))
1135 self.assertEqual(self.builds[1].name, 'project-test2')
1136 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001137
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001138 self.assertEqual(self.builds[2].name, 'project-test1')
1139 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1140 self.assertEqual(self.builds[3].name, 'project-test2')
1141 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001142
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001143 self.assertEqual(self.builds[4].name, 'project-test1')
1144 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1145 self.assertEqual(self.builds[5].name, 'project-test2')
1146 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001147
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001148 self.worker.hold_jobs_in_build = False
1149 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001150 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001151 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001152
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001153 self.assertEqual(len(self.history), 9)
1154 self.assertEqual(A.data['status'], 'MERGED')
1155 self.assertEqual(B.data['status'], 'MERGED')
1156 self.assertEqual(C.data['status'], 'MERGED')
1157 self.assertEqual(A.reported, 2)
1158 self.assertEqual(B.reported, 2)
1159 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001160
1161 def test_failed_changes(self):
1162 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001163 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001164
James E. Blairb02a3bb2012-07-30 17:49:55 -07001165 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1166 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001167 A.addApproval('CRVW', 2)
1168 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001169
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001170 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001171
James E. Blaire2819012013-06-28 17:17:26 -04001172 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1173 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001174 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001175
1176 self.worker.release('.*-merge')
1177 self.waitUntilSettled()
1178
1179 self.worker.hold_jobs_in_build = False
1180 self.worker.release()
1181
1182 self.waitUntilSettled()
1183 # It's certain that the merge job for change 2 will run, but
1184 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001185 self.assertTrue(len(self.history) > 6)
1186 self.assertEqual(A.data['status'], 'NEW')
1187 self.assertEqual(B.data['status'], 'MERGED')
1188 self.assertEqual(A.reported, 2)
1189 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001190
1191 def test_independent_queues(self):
1192 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001193
1194 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001195 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001196 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1197 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001198 A.addApproval('CRVW', 2)
1199 B.addApproval('CRVW', 2)
1200 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001201
1202 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1203 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1204 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1205
James E. Blairb02a3bb2012-07-30 17:49:55 -07001206 self.waitUntilSettled()
1207
1208 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001209 self.assertEqual(len(self.builds), 2)
1210 self.assertEqual(self.builds[0].name, 'project-merge')
1211 self.assertTrue(self.job_has_changes(self.builds[0], A))
1212 self.assertEqual(self.builds[1].name, 'project1-merge')
1213 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001214
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001215 # Release the current merge builds
1216 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001217 self.waitUntilSettled()
1218 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001219 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001220 self.waitUntilSettled()
1221
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001222 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001223 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001224 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001225
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001226 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001227 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001228 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001229
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001230 self.assertEqual(len(self.history), 11)
1231 self.assertEqual(A.data['status'], 'MERGED')
1232 self.assertEqual(B.data['status'], 'MERGED')
1233 self.assertEqual(C.data['status'], 'MERGED')
1234 self.assertEqual(A.reported, 2)
1235 self.assertEqual(B.reported, 2)
1236 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001237
1238 def test_failed_change_at_head(self):
1239 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001240
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001241 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001242 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1243 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1244 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001245 A.addApproval('CRVW', 2)
1246 B.addApproval('CRVW', 2)
1247 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001248
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001249 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001250
1251 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1252 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1253 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1254
1255 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001256
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001257 self.assertEqual(len(self.builds), 1)
1258 self.assertEqual(self.builds[0].name, 'project-merge')
1259 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001260
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001261 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001262 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001263 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001264 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001265 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001266 self.waitUntilSettled()
1267
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001268 self.assertEqual(len(self.builds), 6)
1269 self.assertEqual(self.builds[0].name, 'project-test1')
1270 self.assertEqual(self.builds[1].name, 'project-test2')
1271 self.assertEqual(self.builds[2].name, 'project-test1')
1272 self.assertEqual(self.builds[3].name, 'project-test2')
1273 self.assertEqual(self.builds[4].name, 'project-test1')
1274 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001275
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001276 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001277 self.waitUntilSettled()
1278
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001279 # project-test2, project-merge for B
1280 self.assertEqual(len(self.builds), 2)
1281 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001282
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001283 self.worker.hold_jobs_in_build = False
1284 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001285 self.waitUntilSettled()
1286
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001287 self.assertEqual(len(self.builds), 0)
1288 self.assertEqual(len(self.history), 15)
1289 self.assertEqual(A.data['status'], 'NEW')
1290 self.assertEqual(B.data['status'], 'MERGED')
1291 self.assertEqual(C.data['status'], 'MERGED')
1292 self.assertEqual(A.reported, 2)
1293 self.assertEqual(B.reported, 2)
1294 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001295
1296 def test_failed_change_at_head_with_queue(self):
1297 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001298
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001299 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001300 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1301 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1302 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001303 A.addApproval('CRVW', 2)
1304 B.addApproval('CRVW', 2)
1305 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001306
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001307 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001308
1309 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1310 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1311 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1312
1313 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001314 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001315 self.assertEqual(len(self.builds), 0)
1316 self.assertEqual(len(queue), 1)
1317 self.assertEqual(queue[0].name, 'build:project-merge')
1318 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001319
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001320 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001321 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001322 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001323 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001324 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001325 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001326 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001327
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001328 self.assertEqual(len(self.builds), 0)
1329 self.assertEqual(len(queue), 6)
1330 self.assertEqual(queue[0].name, 'build:project-test1')
1331 self.assertEqual(queue[1].name, 'build:project-test2')
1332 self.assertEqual(queue[2].name, 'build:project-test1')
1333 self.assertEqual(queue[3].name, 'build:project-test2')
1334 self.assertEqual(queue[4].name, 'build:project-test1')
1335 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001336
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001337 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001338 self.waitUntilSettled()
1339
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001340 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001341 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001342 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1343 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001344
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001345 self.gearman_server.hold_jobs_in_queue = False
1346 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001347 self.waitUntilSettled()
1348
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001349 self.assertEqual(len(self.builds), 0)
1350 self.assertEqual(len(self.history), 11)
1351 self.assertEqual(A.data['status'], 'NEW')
1352 self.assertEqual(B.data['status'], 'MERGED')
1353 self.assertEqual(C.data['status'], 'MERGED')
1354 self.assertEqual(A.reported, 2)
1355 self.assertEqual(B.reported, 2)
1356 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001357
1358 def test_patch_order(self):
1359 "Test that dependent patches are tested in the right order"
1360 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1361 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1362 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1363 A.addApproval('CRVW', 2)
1364 B.addApproval('CRVW', 2)
1365 C.addApproval('CRVW', 2)
1366
1367 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1368 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1369 M2.setMerged()
1370 M1.setMerged()
1371
1372 # C -> B -> A -> M1 -> M2
1373 # M2 is here to make sure it is never queried. If it is, it
1374 # means zuul is walking down the entire history of merged
1375 # changes.
1376
1377 C.setDependsOn(B, 1)
1378 B.setDependsOn(A, 1)
1379 A.setDependsOn(M1, 1)
1380 M1.setDependsOn(M2, 1)
1381
1382 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1383
1384 self.waitUntilSettled()
1385
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001386 self.assertEqual(A.data['status'], 'NEW')
1387 self.assertEqual(B.data['status'], 'NEW')
1388 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001389
1390 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1391 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1392
1393 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001394 self.assertEqual(M2.queried, 0)
1395 self.assertEqual(A.data['status'], 'MERGED')
1396 self.assertEqual(B.data['status'], 'MERGED')
1397 self.assertEqual(C.data['status'], 'MERGED')
1398 self.assertEqual(A.reported, 2)
1399 self.assertEqual(B.reported, 2)
1400 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001401
James E. Blair0e933c52013-07-11 10:18:52 -07001402 def test_trigger_cache(self):
1403 "Test that the trigger cache operates correctly"
1404 self.worker.hold_jobs_in_build = True
1405
1406 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1407 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1408 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1409 A.addApproval('CRVW', 2)
1410 B.addApproval('CRVW', 2)
1411
1412 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1413 M1.setMerged()
1414
1415 B.setDependsOn(A, 1)
1416 A.setDependsOn(M1, 1)
1417
1418 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1419 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1420
1421 self.waitUntilSettled()
1422
1423 for build in self.builds:
1424 if build.parameters['ZUUL_PIPELINE'] == 'check':
1425 build.release()
1426 self.waitUntilSettled()
1427 for build in self.builds:
1428 if build.parameters['ZUUL_PIPELINE'] == 'check':
1429 build.release()
1430 self.waitUntilSettled()
1431
1432 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1433 self.waitUntilSettled()
1434
1435 self.log.debug("len %s " % self.sched.trigger._change_cache.keys())
1436 # there should still be changes in the cache
1437 self.assertNotEqual(len(self.sched.trigger._change_cache.keys()), 0)
1438
1439 self.worker.hold_jobs_in_build = False
1440 self.worker.release()
1441 self.waitUntilSettled()
1442
1443 self.assertEqual(A.data['status'], 'MERGED')
1444 self.assertEqual(B.data['status'], 'MERGED')
1445 self.assertEqual(A.queried, 2) # Initial and isMerged
1446 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1447
James E. Blair8c803f82012-07-31 16:25:42 -07001448 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001449 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001450 # TODO: move to test_gerrit (this is a unit test!)
1451 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4aea70c2012-07-26 14:23:24 -07001452 a = self.sched.trigger.getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001453 mgr = self.sched.layout.pipelines['gate'].manager
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('CRVW', 2)
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.assertFalse(
1460 self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001461
1462 A.addApproval('APRV', 1)
James E. Blairfee8d652013-06-07 08:57:52 -07001463 a = self.sched.trigger.getChange(1, 2, refresh=True)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001464 self.assertTrue(
1465 self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blairfee8d652013-06-07 08:57:52 -07001466 self.sched.trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001467
1468 def test_build_configuration(self):
1469 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001470
1471 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001472 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1473 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1474 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1475 A.addApproval('CRVW', 2)
1476 B.addApproval('CRVW', 2)
1477 C.addApproval('CRVW', 2)
1478 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1479 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1480 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1481 self.waitUntilSettled()
1482
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001483 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001484 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001485 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001486 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001487 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001488 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001489 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001490 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1491 self.gearman_server.hold_jobs_in_queue = False
1492 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001493 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001494
Monty Taylorbc758832013-06-17 17:22:42 -04001495 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001496 repo = git.Repo(path)
1497 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1498 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001499 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001500 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001501
1502 def test_build_configuration_conflict(self):
1503 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001504
1505 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001506 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1507 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001508 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001509 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1510 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001511 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001512 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1513 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001514 A.addApproval('CRVW', 2)
1515 B.addApproval('CRVW', 2)
1516 C.addApproval('CRVW', 2)
1517 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1518 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1519 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1520 self.waitUntilSettled()
1521
James E. Blair6736beb2013-07-11 15:18:15 -07001522 self.assertEqual(A.reported, 1)
1523 self.assertEqual(B.reported, 1)
1524 self.assertEqual(C.reported, 1)
1525
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001526 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001527 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001528 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001529 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001530 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001531 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001532 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001533 self.getParameter(queue[-1], 'ZUUL_REF')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001534 self.gearman_server.hold_jobs_in_queue = False
1535 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001536 self.waitUntilSettled()
1537
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001538 self.assertEqual(A.data['status'], 'MERGED')
1539 self.assertEqual(B.data['status'], 'NEW')
1540 self.assertEqual(C.data['status'], 'MERGED')
1541 self.assertEqual(A.reported, 2)
1542 self.assertEqual(B.reported, 2)
1543 self.assertEqual(C.reported, 2)
James E. Blairdaabed22012-08-15 15:38:57 -07001544
James E. Blair6736beb2013-07-11 15:18:15 -07001545 def test_dequeue_conflict(self):
1546 "Test that the option to dequeue merge conflicts works"
1547
1548 self.gearman_server.hold_jobs_in_queue = True
1549 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1550 A.addPatchset(['conflict'])
1551 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1552 B.addPatchset(['conflict'])
1553 A.addApproval('CRVW', 2)
1554 B.addApproval('CRVW', 2)
1555 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1556 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1557 self.waitUntilSettled()
1558
1559 self.assertEqual(A.reported, 1)
1560 self.assertEqual(B.reported, 2)
1561
1562 self.gearman_server.hold_jobs_in_queue = False
1563 self.gearman_server.release()
1564 self.waitUntilSettled()
1565
1566 self.assertEqual(A.data['status'], 'MERGED')
1567 self.assertEqual(B.data['status'], 'NEW')
1568 self.assertEqual(A.reported, 2)
1569 self.assertEqual(B.reported, 2)
1570
James E. Blairdaabed22012-08-15 15:38:57 -07001571 def test_post(self):
1572 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001573
Zhongyue Luo5d556072012-09-21 02:00:47 +09001574 e = {
1575 "type": "ref-updated",
1576 "submitter": {
1577 "name": "User Name",
1578 },
1579 "refUpdate": {
1580 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1581 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1582 "refName": "master",
1583 "project": "org/project",
1584 }
1585 }
James E. Blairdaabed22012-08-15 15:38:57 -07001586 self.fake_gerrit.addEvent(e)
1587 self.waitUntilSettled()
1588
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001589 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001590 self.assertEqual(len(self.history), 1)
1591 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001592
1593 def test_build_configuration_branch(self):
1594 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001595
1596 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001597 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1598 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1599 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1600 A.addApproval('CRVW', 2)
1601 B.addApproval('CRVW', 2)
1602 C.addApproval('CRVW', 2)
1603 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1604 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1605 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1606 self.waitUntilSettled()
1607
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001608 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001609 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001610 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001611 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001612 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001613 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001614 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001615 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1616 self.gearman_server.hold_jobs_in_queue = False
1617 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001618 self.waitUntilSettled()
1619
Monty Taylorbc758832013-06-17 17:22:42 -04001620 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001621 repo = git.Repo(path)
1622 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1623 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001624 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001625 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001626
1627 def test_build_configuration_branch_interaction(self):
1628 "Test that switching between branches works"
1629 self.test_build_configuration()
1630 self.test_build_configuration_branch()
1631 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001632 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001633 repo = git.Repo(path)
1634 repo.heads.master.commit = repo.commit('init')
1635 self.test_build_configuration()
1636
1637 def test_build_configuration_multi_branch(self):
1638 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001639
1640 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001641 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1642 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1643 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1644 A.addApproval('CRVW', 2)
1645 B.addApproval('CRVW', 2)
1646 C.addApproval('CRVW', 2)
1647 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1648 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1649 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1650 self.waitUntilSettled()
1651
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001652 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001653 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001654 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001655 job_B = None
1656 for job in queue:
1657 if 'project-merge' in job.name:
1658 job_B = job
1659 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001660 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001661 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001662 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001663 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001664 for job in queue:
1665 if 'project-merge' in job.name:
1666 job_C = job
1667 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001668 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001669 self.gearman_server.hold_jobs_in_queue = False
1670 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001671 self.waitUntilSettled()
1672
Monty Taylorbc758832013-06-17 17:22:42 -04001673 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001674 repo = git.Repo(path)
1675
1676 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001677 for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001678 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001679 correct_messages = ['initial commit', 'A-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001680 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001681
1682 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001683 for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001684 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001685 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001686 self.assertEqual(repo_messages, correct_messages)
James E. Blair7f71c802012-08-22 13:04:32 -07001687
1688 def test_one_job_project(self):
1689 "Test that queueing works with one job"
1690 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1691 'master', 'A')
1692 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1693 'master', 'B')
1694 A.addApproval('CRVW', 2)
1695 B.addApproval('CRVW', 2)
1696 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1697 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1698 self.waitUntilSettled()
1699
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001700 self.assertEqual(A.data['status'], 'MERGED')
1701 self.assertEqual(A.reported, 2)
1702 self.assertEqual(B.data['status'], 'MERGED')
1703 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001704
Antoine Musso80edd5a2013-02-13 15:37:53 +01001705 def test_job_from_templates_launched(self):
1706 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001707
Antoine Musso80edd5a2013-02-13 15:37:53 +01001708 A = self.fake_gerrit.addFakeChange(
1709 'org/templated-project', 'master', 'A')
1710 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1711 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001712
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001713 self.assertEqual(self.getJobFromHistory('project-test1').result,
1714 'SUCCESS')
1715 self.assertEqual(self.getJobFromHistory('project-test2').result,
1716 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001717
James E. Blaircaec0c52012-08-22 14:52:22 -07001718 def test_dependent_changes_dequeue(self):
1719 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001720
James E. Blaircaec0c52012-08-22 14:52:22 -07001721 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1722 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1723 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1724 A.addApproval('CRVW', 2)
1725 B.addApproval('CRVW', 2)
1726 C.addApproval('CRVW', 2)
1727
1728 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1729 M1.setMerged()
1730
1731 # C -> B -> A -> M1
1732
1733 C.setDependsOn(B, 1)
1734 B.setDependsOn(A, 1)
1735 A.setDependsOn(M1, 1)
1736
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001737 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001738
1739 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1740 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1741 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1742
1743 self.waitUntilSettled()
1744
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001745 self.assertEqual(A.data['status'], 'NEW')
1746 self.assertEqual(A.reported, 2)
1747 self.assertEqual(B.data['status'], 'NEW')
1748 self.assertEqual(B.reported, 2)
1749 self.assertEqual(C.data['status'], 'NEW')
1750 self.assertEqual(C.reported, 2)
1751 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001752
1753 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001754 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001755 # If it's dequeued more than once, we should see extra
1756 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001757
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001758 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001759 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1760 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1761 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1762 A.addApproval('CRVW', 2)
1763 B.addApproval('CRVW', 2)
1764 C.addApproval('CRVW', 2)
1765
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001766 self.worker.addFailTest('project1-test1', A)
1767 self.worker.addFailTest('project1-test2', A)
1768 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001769
1770 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1771 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1772 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1773
1774 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001775
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001776 self.assertEqual(len(self.builds), 1)
1777 self.assertEqual(self.builds[0].name, 'project1-merge')
1778 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07001779
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001780 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001781 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001782 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001783 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001784 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001785 self.waitUntilSettled()
1786
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001787 self.assertEqual(len(self.builds), 9)
1788 self.assertEqual(self.builds[0].name, 'project1-test1')
1789 self.assertEqual(self.builds[1].name, 'project1-test2')
1790 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
1791 self.assertEqual(self.builds[3].name, 'project1-test1')
1792 self.assertEqual(self.builds[4].name, 'project1-test2')
1793 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
1794 self.assertEqual(self.builds[6].name, 'project1-test1')
1795 self.assertEqual(self.builds[7].name, 'project1-test2')
1796 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001797
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001798 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001799 self.waitUntilSettled()
1800
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001801 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1802 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001803
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001804 self.worker.hold_jobs_in_build = False
1805 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001806 self.waitUntilSettled()
1807
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001808 self.assertEqual(len(self.builds), 0)
1809 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001810
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001811 self.assertEqual(A.data['status'], 'NEW')
1812 self.assertEqual(B.data['status'], 'MERGED')
1813 self.assertEqual(C.data['status'], 'MERGED')
1814 self.assertEqual(A.reported, 2)
1815 self.assertEqual(B.reported, 2)
1816 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001817
1818 def test_nonvoting_job(self):
1819 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001820
James E. Blair4ec821f2012-08-23 15:28:28 -07001821 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1822 'master', 'A')
1823 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001824 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001825 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1826
1827 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001828
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001829 self.assertEqual(A.data['status'], 'MERGED')
1830 self.assertEqual(A.reported, 2)
1831 self.assertEqual(
1832 self.getJobFromHistory('nonvoting-project-merge').result,
1833 'SUCCESS')
1834 self.assertEqual(
1835 self.getJobFromHistory('nonvoting-project-test1').result,
1836 'SUCCESS')
1837 self.assertEqual(
1838 self.getJobFromHistory('nonvoting-project-test2').result,
1839 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001840
1841 def test_check_queue_success(self):
1842 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001843
James E. Blaire0487072012-08-29 17:38:31 -07001844 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1845 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1846
1847 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001848
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001849 self.assertEqual(A.data['status'], 'NEW')
1850 self.assertEqual(A.reported, 1)
1851 self.assertEqual(self.getJobFromHistory('project-merge').result,
1852 'SUCCESS')
1853 self.assertEqual(self.getJobFromHistory('project-test1').result,
1854 'SUCCESS')
1855 self.assertEqual(self.getJobFromHistory('project-test2').result,
1856 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001857
1858 def test_check_queue_failure(self):
1859 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001860
James E. Blaire0487072012-08-29 17:38:31 -07001861 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001862 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001863 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1864
1865 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001866
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001867 self.assertEqual(A.data['status'], 'NEW')
1868 self.assertEqual(A.reported, 1)
1869 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001870 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001871 self.assertEqual(self.getJobFromHistory('project-test1').result,
1872 'SUCCESS')
1873 self.assertEqual(self.getJobFromHistory('project-test2').result,
1874 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001875
1876 def test_dependent_behind_dequeue(self):
1877 "test that dependent changes behind dequeued changes work"
1878 # This complicated test is a reproduction of a real life bug
1879 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001880
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001881 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001882 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1883 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1884 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1885 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1886 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1887 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1888 D.setDependsOn(C, 1)
1889 E.setDependsOn(D, 1)
1890 A.addApproval('CRVW', 2)
1891 B.addApproval('CRVW', 2)
1892 C.addApproval('CRVW', 2)
1893 D.addApproval('CRVW', 2)
1894 E.addApproval('CRVW', 2)
1895 F.addApproval('CRVW', 2)
1896
1897 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001898
1899 # Change object re-use in the gerrit trigger is hidden if
1900 # changes are added in quick succession; waiting makes it more
1901 # like real life.
1902 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1903 self.waitUntilSettled()
1904 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1905 self.waitUntilSettled()
1906
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001907 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001908 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001909 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001910 self.waitUntilSettled()
1911
1912 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1913 self.waitUntilSettled()
1914 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1915 self.waitUntilSettled()
1916 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1917 self.waitUntilSettled()
1918 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1919 self.waitUntilSettled()
1920
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001921 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001922 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001923 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001924 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001925 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001926 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001927 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001928 self.waitUntilSettled()
1929
1930 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001931
1932 # Grab pointers to the jobs we want to release before
1933 # releasing any, because list indexes may change as
1934 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001935 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001936 a.release()
1937 b.release()
1938 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001939 self.waitUntilSettled()
1940
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001941 self.worker.hold_jobs_in_build = False
1942 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07001943 self.waitUntilSettled()
1944
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001945 self.assertEqual(A.data['status'], 'NEW')
1946 self.assertEqual(B.data['status'], 'MERGED')
1947 self.assertEqual(C.data['status'], 'MERGED')
1948 self.assertEqual(D.data['status'], 'MERGED')
1949 self.assertEqual(E.data['status'], 'MERGED')
1950 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001951
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001952 self.assertEqual(A.reported, 2)
1953 self.assertEqual(B.reported, 2)
1954 self.assertEqual(C.reported, 2)
1955 self.assertEqual(D.reported, 2)
1956 self.assertEqual(E.reported, 2)
1957 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001958
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001959 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1960 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001961
1962 def test_merger_repack(self):
1963 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001964
James E. Blair05fed602012-09-07 12:45:24 -07001965 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1966 A.addApproval('CRVW', 2)
1967 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1968 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001969 self.assertEqual(self.getJobFromHistory('project-merge').result,
1970 'SUCCESS')
1971 self.assertEqual(self.getJobFromHistory('project-test1').result,
1972 'SUCCESS')
1973 self.assertEqual(self.getJobFromHistory('project-test2').result,
1974 'SUCCESS')
1975 self.assertEqual(A.data['status'], 'MERGED')
1976 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001977 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07001978 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001979
Monty Taylorbc758832013-06-17 17:22:42 -04001980 path = os.path.join(self.git_root, "org/project")
1981 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001982
1983 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1984 A.addApproval('CRVW', 2)
1985 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1986 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001987 self.assertEqual(self.getJobFromHistory('project-merge').result,
1988 'SUCCESS')
1989 self.assertEqual(self.getJobFromHistory('project-test1').result,
1990 'SUCCESS')
1991 self.assertEqual(self.getJobFromHistory('project-test2').result,
1992 'SUCCESS')
1993 self.assertEqual(A.data['status'], 'MERGED')
1994 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001995
James E. Blair4886f282012-11-15 09:27:33 -08001996 def test_merger_repack_large_change(self):
1997 "Test that the merger works with large changes after a repack"
1998 # https://bugs.launchpad.net/zuul/+bug/1078946
1999 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2000 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002001 path = os.path.join(self.upstream_root, "org/project1")
2002 print repack_repo(path)
2003 path = os.path.join(self.git_root, "org/project1")
2004 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002005
2006 A.addApproval('CRVW', 2)
2007 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2008 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002009 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2010 'SUCCESS')
2011 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2012 'SUCCESS')
2013 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2014 'SUCCESS')
2015 self.assertEqual(A.data['status'], 'MERGED')
2016 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002017
James E. Blair7ee88a22012-09-12 18:59:31 +02002018 def test_nonexistent_job(self):
2019 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002020 # Set to the state immediately after a restart
2021 self.resetGearmanServer()
2022 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002023
2024 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2025 A.addApproval('CRVW', 2)
2026 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2027 # There may be a thread about to report a lost change
2028 while A.reported < 2:
2029 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002030 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002031 self.assertFalse(job_names)
2032 self.assertEqual(A.data['status'], 'NEW')
2033 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002034 self.assertEmptyQueues()
2035
2036 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002037 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002038 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2039 A.addApproval('CRVW', 2)
2040 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2041 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002042 self.assertEqual(self.getJobFromHistory('project-merge').result,
2043 'SUCCESS')
2044 self.assertEqual(self.getJobFromHistory('project-test1').result,
2045 'SUCCESS')
2046 self.assertEqual(self.getJobFromHistory('project-test2').result,
2047 'SUCCESS')
2048 self.assertEqual(A.data['status'], 'MERGED')
2049 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002050
2051 def test_single_nonexistent_post_job(self):
2052 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002053 e = {
2054 "type": "ref-updated",
2055 "submitter": {
2056 "name": "User Name",
2057 },
2058 "refUpdate": {
2059 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2060 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2061 "refName": "master",
2062 "project": "org/project",
2063 }
2064 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002065 # Set to the state immediately after a restart
2066 self.resetGearmanServer()
2067 self.launcher.negative_function_cache_ttl = 0
2068
James E. Blairf62d4282012-12-31 17:01:50 -08002069 self.fake_gerrit.addEvent(e)
2070 self.waitUntilSettled()
2071
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002072 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002073
2074 def test_new_patchset_dequeues_old(self):
2075 "Test that a new patchset causes the old to be dequeued"
2076 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002077 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002078 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2079 M.setMerged()
2080
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 B.addPatchset()
2101 self.fake_gerrit.addEvent(B.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'], 'MERGED')
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), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002117
2118 def test_new_patchset_dequeues_old_on_head(self):
2119 "Test that a new patchset causes the old to be dequeued (at head)"
2120 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002121 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002122 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2123 M.setMerged()
2124 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2125 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2126 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2127 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2128 A.addApproval('CRVW', 2)
2129 B.addApproval('CRVW', 2)
2130 C.addApproval('CRVW', 2)
2131 D.addApproval('CRVW', 2)
2132
2133 C.setDependsOn(B, 1)
2134 B.setDependsOn(A, 1)
2135 A.setDependsOn(M, 1)
2136
2137 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2138 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2139 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2140 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2141 self.waitUntilSettled()
2142
2143 A.addPatchset()
2144 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2145 self.waitUntilSettled()
2146
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002147 self.worker.hold_jobs_in_build = False
2148 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002149 self.waitUntilSettled()
2150
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002151 self.assertEqual(A.data['status'], 'NEW')
2152 self.assertEqual(A.reported, 2)
2153 self.assertEqual(B.data['status'], 'NEW')
2154 self.assertEqual(B.reported, 2)
2155 self.assertEqual(C.data['status'], 'NEW')
2156 self.assertEqual(C.reported, 2)
2157 self.assertEqual(D.data['status'], 'MERGED')
2158 self.assertEqual(D.reported, 2)
2159 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002160
2161 def test_new_patchset_dequeues_old_without_dependents(self):
2162 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002163 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002164 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2165 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2166 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2167 A.addApproval('CRVW', 2)
2168 B.addApproval('CRVW', 2)
2169 C.addApproval('CRVW', 2)
2170
2171 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2172 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2173 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2174 self.waitUntilSettled()
2175
2176 B.addPatchset()
2177 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2178 self.waitUntilSettled()
2179
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002180 self.worker.hold_jobs_in_build = False
2181 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002182 self.waitUntilSettled()
2183
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002184 self.assertEqual(A.data['status'], 'MERGED')
2185 self.assertEqual(A.reported, 2)
2186 self.assertEqual(B.data['status'], 'NEW')
2187 self.assertEqual(B.reported, 2)
2188 self.assertEqual(C.data['status'], 'MERGED')
2189 self.assertEqual(C.reported, 2)
2190 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002191
2192 def test_new_patchset_dequeues_old_independent_queue(self):
2193 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002194 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002195 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2196 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2197 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2198 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2199 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2200 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2201 self.waitUntilSettled()
2202
2203 B.addPatchset()
2204 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2205 self.waitUntilSettled()
2206
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002207 self.worker.hold_jobs_in_build = False
2208 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002209 self.waitUntilSettled()
2210
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002211 self.assertEqual(A.data['status'], 'NEW')
2212 self.assertEqual(A.reported, 1)
2213 self.assertEqual(B.data['status'], 'NEW')
2214 self.assertEqual(B.reported, 1)
2215 self.assertEqual(C.data['status'], 'NEW')
2216 self.assertEqual(C.reported, 1)
2217 self.assertEqual(len(self.history), 10)
2218 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002219
2220 def test_zuul_refs(self):
2221 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002222 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002223 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2224 M1.setMerged()
2225 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2226 M2.setMerged()
2227
2228 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2229 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2230 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2231 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2232 A.addApproval('CRVW', 2)
2233 B.addApproval('CRVW', 2)
2234 C.addApproval('CRVW', 2)
2235 D.addApproval('CRVW', 2)
2236 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2237 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2238 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2239 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2240
2241 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002242 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002243 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002244 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002245 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002246 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002247 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002248 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002249 self.waitUntilSettled()
2250
James E. Blair7d0dedc2013-02-21 17:26:09 -08002251 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002252 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002253 if x.parameters['ZUUL_CHANGE'] == '3':
2254 a_zref = x.parameters['ZUUL_REF']
2255 if x.parameters['ZUUL_CHANGE'] == '4':
2256 b_zref = x.parameters['ZUUL_REF']
2257 if x.parameters['ZUUL_CHANGE'] == '5':
2258 c_zref = x.parameters['ZUUL_REF']
2259 if x.parameters['ZUUL_CHANGE'] == '6':
2260 d_zref = x.parameters['ZUUL_REF']
2261
2262 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002263 self.assertIsNotNone(a_zref)
2264 self.assertIsNotNone(b_zref)
2265 self.assertIsNotNone(c_zref)
2266 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002267
2268 # And they should all be different
2269 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002270 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002271
2272 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002273 self.assertTrue(self.ref_has_change(a_zref, A))
2274 self.assertFalse(self.ref_has_change(a_zref, B))
2275 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002276
2277 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002278 self.assertTrue(self.ref_has_change(b_zref, A))
2279 self.assertTrue(self.ref_has_change(b_zref, B))
2280 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002281
2282 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002283 self.assertTrue(self.ref_has_change(c_zref, A))
2284 self.assertTrue(self.ref_has_change(c_zref, B))
2285 self.assertTrue(self.ref_has_change(c_zref, C))
2286 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002287
2288 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002289 self.assertTrue(self.ref_has_change(d_zref, A))
2290 self.assertTrue(self.ref_has_change(d_zref, B))
2291 self.assertTrue(self.ref_has_change(d_zref, C))
2292 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002293
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002294 self.worker.hold_jobs_in_build = False
2295 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002296 self.waitUntilSettled()
2297
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002298 self.assertEqual(A.data['status'], 'MERGED')
2299 self.assertEqual(A.reported, 2)
2300 self.assertEqual(B.data['status'], 'MERGED')
2301 self.assertEqual(B.reported, 2)
2302 self.assertEqual(C.data['status'], 'MERGED')
2303 self.assertEqual(C.reported, 2)
2304 self.assertEqual(D.data['status'], 'MERGED')
2305 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002306
James E. Blair412e5582013-04-22 15:50:12 -07002307 def test_statsd(self):
2308 "Test each of the statsd methods used in the scheduler"
2309 import extras
2310 statsd = extras.try_import('statsd.statsd')
2311 statsd.incr('test-incr')
2312 statsd.timing('test-timing', 3)
2313 statsd.gauge('test-guage', 12)
2314 self.assertReportedStat('test-incr', '1|c')
2315 self.assertReportedStat('test-timing', '3|ms')
2316 self.assertReportedStat('test-guage', '12|g')
2317
James E. Blair70c71582013-03-06 08:50:50 -08002318 def test_file_jobs(self):
2319 "Test that file jobs run only when appropriate"
2320 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2321 A.addPatchset(['pip-requires'])
2322 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2323 A.addApproval('CRVW', 2)
2324 B.addApproval('CRVW', 2)
2325 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2326 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2327 self.waitUntilSettled()
2328
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002329 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002330 if x.name == 'project-testfile']
2331
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002332 self.assertEqual(len(testfile_jobs), 1)
2333 self.assertEqual(testfile_jobs[0].changes, '1,2')
2334 self.assertEqual(A.data['status'], 'MERGED')
2335 self.assertEqual(A.reported, 2)
2336 self.assertEqual(B.data['status'], 'MERGED')
2337 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002338
2339 def test_test_config(self):
2340 "Test that we can test the config"
2341 sched = zuul.scheduler.Scheduler()
2342 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002343
2344 def test_build_description(self):
2345 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002346 self.worker.registerFunction('set_description:' +
2347 self.worker.worker_id)
2348
2349 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2350 A.addApproval('CRVW', 2)
2351 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2352 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002353 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002354 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002355 self.assertTrue(re.search("Branch.*master", desc))
2356 self.assertTrue(re.search("Pipeline.*gate", desc))
2357 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2358 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2359 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2360 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002361
James E. Blair64ed6f22013-07-10 14:07:23 -07002362 def test_queue_precedence(self):
2363 "Test that queue precedence works"
2364
2365 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002366 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002367 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2368 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2369 A.addApproval('CRVW', 2)
2370 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2371
2372 self.waitUntilSettled()
2373 self.gearman_server.hold_jobs_in_queue = False
2374 self.gearman_server.release()
2375 self.waitUntilSettled()
2376
James E. Blair8de58bd2013-07-18 16:23:33 -07002377 # Run one build at a time to ensure non-race order:
2378 for x in range(6):
2379 self.release(self.builds[0])
2380 self.waitUntilSettled()
2381 self.worker.hold_jobs_in_build = False
2382 self.waitUntilSettled()
2383
James E. Blair64ed6f22013-07-10 14:07:23 -07002384 self.log.debug(self.history)
2385 self.assertEqual(self.history[0].pipeline, 'gate')
2386 self.assertEqual(self.history[1].pipeline, 'check')
2387 self.assertEqual(self.history[2].pipeline, 'gate')
2388 self.assertEqual(self.history[3].pipeline, 'gate')
2389 self.assertEqual(self.history[4].pipeline, 'check')
2390 self.assertEqual(self.history[5].pipeline, 'check')
2391
James E. Blair1843a552013-07-03 14:19:52 -07002392 def test_json_status(self):
2393 "Test that we can retrieve JSON status info"
2394 self.worker.hold_jobs_in_build = True
2395 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2396 A.addApproval('CRVW', 2)
2397 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2398 self.waitUntilSettled()
2399
2400 port = self.webapp.server.socket.getsockname()[1]
2401
2402 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2403 data = f.read()
2404
2405 self.worker.hold_jobs_in_build = False
2406 self.worker.release()
2407 self.waitUntilSettled()
2408
2409 data = json.loads(data)
2410 status_jobs = set()
2411 for p in data['pipelines']:
2412 for q in p['change_queues']:
2413 for head in q['heads']:
2414 for change in head:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002415 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002416 for job in change['jobs']:
2417 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002418 self.assertIn('project-merge', status_jobs)
2419 self.assertIn('project-test1', status_jobs)
2420 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002421
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002422 def test_node_label(self):
2423 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002424 self.worker.registerFunction('build:node-project-test1:debian')
2425
2426 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2427 A.addApproval('CRVW', 2)
2428 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2429 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002430
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002431 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2432 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2433 'debian')
2434 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002435
2436 def test_live_reconfiguration(self):
2437 "Test that live reconfiguration works"
2438 self.worker.hold_jobs_in_build = True
2439 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2440 A.addApproval('CRVW', 2)
2441 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2442 self.waitUntilSettled()
2443
2444 self.sched.reconfigure(self.config)
2445
2446 self.worker.hold_jobs_in_build = False
2447 self.worker.release()
2448 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002449 self.assertEqual(self.getJobFromHistory('project-merge').result,
2450 'SUCCESS')
2451 self.assertEqual(self.getJobFromHistory('project-test1').result,
2452 'SUCCESS')
2453 self.assertEqual(self.getJobFromHistory('project-test2').result,
2454 'SUCCESS')
2455 self.assertEqual(A.data['status'], 'MERGED')
2456 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002457
2458 def test_delayed_repo_init(self):
2459 self.config.set('zuul', 'layout_config',
2460 'tests/fixtures/layout-delayed-repo-init.yaml')
2461 self.sched.reconfigure(self.config)
2462
2463 self.init_repo("org/new-project")
2464 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2465
2466 A.addApproval('CRVW', 2)
2467 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2468 self.waitUntilSettled()
2469 self.assertEqual(self.getJobFromHistory('project-merge').result,
2470 'SUCCESS')
2471 self.assertEqual(self.getJobFromHistory('project-test1').result,
2472 'SUCCESS')
2473 self.assertEqual(self.getJobFromHistory('project-test2').result,
2474 'SUCCESS')
2475 self.assertEqual(A.data['status'], 'MERGED')
2476 self.assertEqual(A.reported, 2)