blob: de000c051bfc83c749e3cf583ec5532bc72cf2b0 [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))
James E. Blairdda6c912013-07-29 14:12:12 -0700651 for job in self.getQueue():
652 cmd, name = job.name.split(':')
653 if cmd != 'build':
654 continue
655 if not regex or re.match(regex, name):
656 self.log.debug("releasing queued job %s" %
657 job.unique)
658 job.waiting = False
659 released = True
660 else:
661 self.log.debug("not releasing queued job %s" %
662 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700663 if released:
664 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700665 qlen = (len(self.high_queue) + len(self.normal_queue) +
666 len(self.low_queue))
667 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700668
669
Monty Taylorbc758832013-06-17 17:22:42 -0400670class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700671 log = logging.getLogger("zuul.test")
672
673 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400674 super(TestScheduler, self).setUp()
675 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
676 try:
677 test_timeout = int(test_timeout)
678 except ValueError:
679 # If timeout value is invalid do not set a timeout.
680 test_timeout = 0
681 if test_timeout > 0:
682 self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
683
684 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
685 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
686 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
687 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
688 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
689 os.environ.get('OS_STDERR_CAPTURE') == '1'):
690 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
691 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Monty Taylore6a6c402013-07-02 09:25:55 -0700692 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
693 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400694 self.useFixture(fixtures.FakeLogger(
695 level=logging.DEBUG,
696 format='%(asctime)s %(name)-32s '
697 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700698 tmp_root = self.useFixture(fixtures.TempDir(
699 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400700 self.test_root = os.path.join(tmp_root, "zuul-test")
701 self.upstream_root = os.path.join(self.test_root, "upstream")
702 self.git_root = os.path.join(self.test_root, "git")
703
704 CONFIG.set('zuul', 'git_dir', self.git_root)
705 if os.path.exists(self.test_root):
706 shutil.rmtree(self.test_root)
707 os.makedirs(self.test_root)
708 os.makedirs(self.upstream_root)
709 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700710
711 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400712 self.init_repo("org/project")
713 self.init_repo("org/project1")
714 self.init_repo("org/project2")
715 self.init_repo("org/project3")
716 self.init_repo("org/one-job-project")
717 self.init_repo("org/nonvoting-project")
718 self.init_repo("org/templated-project")
719 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700720 self.init_repo("org/conflict-project")
James E. Blair412e5582013-04-22 15:50:12 -0700721
722 self.statsd = FakeStatsd()
723 os.environ['STATSD_HOST'] = 'localhost'
724 os.environ['STATSD_PORT'] = str(self.statsd.port)
725 self.statsd.start()
726 # the statsd client object is configured in the statsd module import
727 reload(statsd)
728 reload(zuul.scheduler)
729
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700730 self.gearman_server = FakeGearmanServer()
731
732 self.config = ConfigParser.ConfigParser()
733 cfg = StringIO()
734 CONFIG.write(cfg)
735 cfg.seek(0)
736 self.config.readfp(cfg)
737 self.config.set('gearman', 'port', str(self.gearman_server.port))
738
Monty Taylorbc758832013-06-17 17:22:42 -0400739 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700740 self.worker.addServer('127.0.0.1', self.gearman_server.port)
741 self.gearman_server.worker = self.worker
742
James E. Blairb0fcae42012-07-17 11:12:10 -0700743 self.sched = zuul.scheduler.Scheduler()
744
James E. Blair8cc15a82012-08-01 11:17:57 -0700745 def URLOpenerFactory(*args, **kw):
746 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400747 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700748
James E. Blair8cc15a82012-08-01 11:17:57 -0700749 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700750 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700751
752 zuul.lib.gerrit.Gerrit = FakeGerrit
753
Monty Taylorbc758832013-06-17 17:22:42 -0400754 self.gerrit = FakeGerritTrigger(
755 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700756 self.gerrit.replication_timeout = 1.5
757 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700758 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400759 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700760
James E. Blair1843a552013-07-03 14:19:52 -0700761 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
762
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700763 self.sched.setLauncher(self.launcher)
James E. Blairb0fcae42012-07-17 11:12:10 -0700764 self.sched.setTrigger(self.gerrit)
765
766 self.sched.start()
767 self.sched.reconfigure(self.config)
768 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700769 self.webapp.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700770 self.launcher.gearman.waitForServer()
771 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400772 self.builds = self.worker.running_builds
773 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700774
James E. Blairfee8d652013-06-07 08:57:52 -0700775 self.addCleanup(self.assertFinalState)
776 self.addCleanup(self.shutdown)
777
778 def assertFinalState(self):
779 # Make sure that the change cache is cleared
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400780 self.assertEqual(len(self.sched.trigger._change_cache.keys()), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700781 self.assertEmptyQueues()
782
783 def shutdown(self):
784 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700785 self.launcher.stop()
786 self.worker.shutdown()
787 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700788 self.gerrit.stop()
789 self.sched.stop()
790 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700791 self.statsd.stop()
792 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700793 self.webapp.stop()
794 self.webapp.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700795 threads = threading.enumerate()
796 if len(threads) > 1:
797 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400798 super(TestScheduler, self).tearDown()
799
800 def init_repo(self, project):
801 parts = project.split('/')
802 path = os.path.join(self.upstream_root, *parts[:-1])
803 if not os.path.exists(path):
804 os.makedirs(path)
805 path = os.path.join(self.upstream_root, project)
806 repo = git.Repo.init(path)
807
808 repo.config_writer().set_value('user', 'email', 'user@example.com')
809 repo.config_writer().set_value('user', 'name', 'User Name')
810 repo.config_writer().write()
811
812 fn = os.path.join(path, 'README')
813 f = open(fn, 'w')
814 f.write("test\n")
815 f.close()
816 repo.index.add([fn])
817 repo.index.commit('initial commit')
818 master = repo.create_head('master')
819 repo.create_tag('init')
820
821 mp = repo.create_head('mp')
822 repo.head.reference = mp
823 f = open(fn, 'a')
824 f.write("test mp\n")
825 f.close()
826 repo.index.add([fn])
827 repo.index.commit('mp commit')
828
829 repo.head.reference = master
830 repo.head.reset(index=True, working_tree=True)
831 repo.git.clean('-x', '-f', '-d')
832
833 def ref_has_change(self, ref, change):
834 path = os.path.join(self.git_root, change.project)
835 repo = git.Repo(path)
836 for commit in repo.iter_commits(ref):
837 if commit.message.strip() == ('%s-1' % change.subject):
838 return True
839 return False
840
841 def job_has_changes(self, *args):
842 job = args[0]
843 commits = args[1:]
844 if isinstance(job, FakeBuild):
845 parameters = job.parameters
846 else:
847 parameters = json.loads(job.arguments)
848 project = parameters['ZUUL_PROJECT']
849 path = os.path.join(self.git_root, project)
850 repo = git.Repo(path)
851 ref = parameters['ZUUL_REF']
852 sha = parameters['ZUUL_COMMIT']
853 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
854 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
855 commit_messages = ['%s-1' % commit.subject for commit in commits]
856 for msg in commit_messages:
857 if msg not in repo_messages:
858 return False
859 if repo_shas[0] != sha:
860 return False
861 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700862
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700863 def registerJobs(self):
864 count = 0
James E. Blaireff88162013-07-01 12:44:14 -0400865 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700866 self.worker.registerFunction('build:' + job)
867 count += 1
868 self.worker.registerFunction('stop:' + self.worker.worker_id)
869 count += 1
870
871 while len(self.gearman_server.functions) < count:
872 time.sleep(0)
873
874 def release(self, job):
875 if isinstance(job, FakeBuild):
876 job.release()
877 else:
878 job.waiting = False
879 self.log.debug("Queued job %s released" % job.unique)
880 self.gearman_server.wakeConnections()
881
882 def getParameter(self, job, name):
883 if isinstance(job, FakeBuild):
884 return job.parameters[name]
885 else:
886 parameters = json.loads(job.arguments)
887 return parameters[name]
888
889 def resetGearmanServer(self):
890 self.worker.setFunctions([])
891 while True:
892 done = True
893 for connection in self.gearman_server.active_connections:
894 if connection.functions:
895 done = False
896 if done:
897 break
898 time.sleep(0)
899 self.gearman_server.functions = set()
900
901 def haveAllBuildsReported(self):
902 # See if Zuul is waiting on a meta job to complete
903 if self.launcher.meta_jobs:
904 return False
905 # Find out if every build that the worker has completed has been
906 # reported back to Zuul. If it hasn't then that means a Gearman
907 # event is still in transit and the system is not stable.
908 for build in self.worker.build_history:
909 zbuild = self.launcher.builds.get(build.uuid)
910 if not zbuild:
911 # It has already been reported
912 continue
913 # It hasn't been reported yet.
914 return False
915 # Make sure that none of the worker connections are in GRAB_WAIT
916 for connection in self.worker.active_connections:
917 if connection.state == 'GRAB_WAIT':
918 return False
919 return True
920
921 def areAllBuildsWaiting(self):
922 ret = True
923
924 builds = self.launcher.builds.values()
925 for build in builds:
926 client_job = None
927 for conn in self.launcher.gearman.active_connections:
928 for j in conn.related_jobs.values():
929 if j.unique == build.uuid:
930 client_job = j
931 break
932 if not client_job:
933 self.log.debug("%s is not known to the gearman client" %
934 build)
935 ret = False
936 continue
937 if not client_job.handle:
938 self.log.debug("%s has no handle" % client_job)
939 ret = False
940 continue
941 server_job = self.gearman_server.jobs.get(client_job.handle)
942 if not server_job:
943 self.log.debug("%s is not known to the gearman server" %
944 client_job)
945 ret = False
946 continue
947 if not hasattr(server_job, 'waiting'):
948 self.log.debug("%s is being enqueued" % server_job)
949 ret = False
950 continue
951 if server_job.waiting:
952 continue
953 worker_job = self.worker.gearman_jobs.get(server_job.unique)
954 if worker_job:
955 if worker_job.build.isWaiting():
956 continue
957 else:
958 self.log.debug("%s is running" % worker_job)
959 ret = False
960 else:
961 self.log.debug("%s is unassigned" % server_job)
962 ret = False
963 return ret
964
James E. Blairb0fcae42012-07-17 11:12:10 -0700965 def waitUntilSettled(self):
966 self.log.debug("Waiting until settled...")
967 start = time.time()
968 while True:
969 if time.time() - start > 10:
970 print 'queue status:',
971 print self.sched.trigger_event_queue.empty(),
972 print self.sched.result_event_queue.empty(),
973 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700974 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -0700975 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700976 # Make sure no new events show up while we're checking
977 self.worker.lock.acquire()
978 # have all build states propogated to zuul?
979 if self.haveAllBuildsReported():
980 # Join ensures that the queue is empty _and_ events have been
981 # processed
982 self.fake_gerrit.event_queue.join()
983 self.sched.trigger_event_queue.join()
984 self.sched.result_event_queue.join()
985 if (self.sched.trigger_event_queue.empty() and
986 self.sched.result_event_queue.empty() and
987 self.fake_gerrit.event_queue.empty() and
988 self.areAllBuildsWaiting()):
989 self.worker.lock.release()
990 self.log.debug("...settled.")
991 return
992 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700993 self.sched.wake_event.wait(0.1)
994
James E. Blaird466dc42012-07-31 10:42:56 -0700995 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -0800996 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -0700997 return len(jobs)
998
James E. Blair4ca985f2013-05-30 12:27:43 -0700999 def getJobFromHistory(self, name):
1000 history = self.worker.build_history
1001 for job in history:
1002 if job.name == name:
1003 return job
1004 raise Exception("Unable to find job %s in history" % name)
1005
James E. Blaire0487072012-08-29 17:38:31 -07001006 def assertEmptyQueues(self):
1007 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001008 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001009 for queue in pipeline.queues:
1010 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001011 print 'pipeline %s queue %s contents %s' % (
1012 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001013 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001014 if len(queue.severed_heads) != 0:
1015 print 'heads', queue.severed_heads
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001016 self.assertEqual(len(queue.severed_heads), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001017
James E. Blair66eeebf2013-07-27 17:44:32 -07001018 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001019 start = time.time()
1020 while time.time() < (start + 5):
1021 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001022 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001023 k, v = stat.split(':')
1024 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001025 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001026 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001027 elif value:
1028 if value == v:
1029 return
1030 elif kind:
1031 if v.endswith('|' + kind):
1032 return
James E. Blair412e5582013-04-22 15:50:12 -07001033 time.sleep(0.1)
1034
1035 pprint.pprint(self.statsd.stats)
1036 raise Exception("Key %s not found in reported stats" % key)
1037
James E. Blairb0fcae42012-07-17 11:12:10 -07001038 def test_jobs_launched(self):
1039 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001040
James E. Blairb0fcae42012-07-17 11:12:10 -07001041 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001042 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001043 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1044 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001045 self.assertEqual(self.getJobFromHistory('project-merge').result,
1046 'SUCCESS')
1047 self.assertEqual(self.getJobFromHistory('project-test1').result,
1048 'SUCCESS')
1049 self.assertEqual(self.getJobFromHistory('project-test2').result,
1050 'SUCCESS')
1051 self.assertEqual(A.data['status'], 'MERGED')
1052 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001053
James E. Blair66eeebf2013-07-27 17:44:32 -07001054 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1055 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1056 value='1|g')
1057 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1058 kind='ms')
1059 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1060 value='1|c')
1061 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1062 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1063 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001064 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001065 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001066 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001067 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001068
James E. Blair42f74822013-05-14 15:18:03 -07001069 def test_duplicate_pipelines(self):
1070 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001071
James E. Blair42f74822013-05-14 15:18:03 -07001072 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1073 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1074 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001075
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001076 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001077 self.history[0].name == 'project-test1'
1078 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001079
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001080 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001081 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001082 self.assertIn('dup1/project-test1', A.messages[0])
1083 self.assertNotIn('dup2/project-test1', A.messages[0])
1084 self.assertNotIn('dup1/project-test1', A.messages[1])
1085 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001086 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001087 self.assertIn('dup1/project-test1', A.messages[1])
1088 self.assertNotIn('dup2/project-test1', A.messages[1])
1089 self.assertNotIn('dup1/project-test1', A.messages[0])
1090 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001091
James E. Blairb0fcae42012-07-17 11:12:10 -07001092 def test_parallel_changes(self):
1093 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001094
1095 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001096 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1097 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1098 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001099 A.addApproval('CRVW', 2)
1100 B.addApproval('CRVW', 2)
1101 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001102
1103 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1104 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1105 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1106
1107 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001108 self.assertEqual(len(self.builds), 1)
1109 self.assertEqual(self.builds[0].name, 'project-merge')
1110 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001111
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001112 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001113 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001114 self.assertEqual(len(self.builds), 3)
1115 self.assertEqual(self.builds[0].name, 'project-test1')
1116 self.assertTrue(self.job_has_changes(self.builds[0], A))
1117 self.assertEqual(self.builds[1].name, 'project-test2')
1118 self.assertTrue(self.job_has_changes(self.builds[1], A))
1119 self.assertEqual(self.builds[2].name, 'project-merge')
1120 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001121
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001122 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001123 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001124 self.assertEqual(len(self.builds), 5)
1125 self.assertEqual(self.builds[0].name, 'project-test1')
1126 self.assertTrue(self.job_has_changes(self.builds[0], A))
1127 self.assertEqual(self.builds[1].name, 'project-test2')
1128 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001129
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001130 self.assertEqual(self.builds[2].name, 'project-test1')
1131 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1132 self.assertEqual(self.builds[3].name, 'project-test2')
1133 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001134
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001135 self.assertEqual(self.builds[4].name, 'project-merge')
1136 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001137
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001138 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001139 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001140 self.assertEqual(len(self.builds), 6)
1141 self.assertEqual(self.builds[0].name, 'project-test1')
1142 self.assertTrue(self.job_has_changes(self.builds[0], A))
1143 self.assertEqual(self.builds[1].name, 'project-test2')
1144 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001145
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001146 self.assertEqual(self.builds[2].name, 'project-test1')
1147 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1148 self.assertEqual(self.builds[3].name, 'project-test2')
1149 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001150
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001151 self.assertEqual(self.builds[4].name, 'project-test1')
1152 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1153 self.assertEqual(self.builds[5].name, 'project-test2')
1154 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001155
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001156 self.worker.hold_jobs_in_build = False
1157 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001158 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001159 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001160
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001161 self.assertEqual(len(self.history), 9)
1162 self.assertEqual(A.data['status'], 'MERGED')
1163 self.assertEqual(B.data['status'], 'MERGED')
1164 self.assertEqual(C.data['status'], 'MERGED')
1165 self.assertEqual(A.reported, 2)
1166 self.assertEqual(B.reported, 2)
1167 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001168
1169 def test_failed_changes(self):
1170 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001171 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001172
James E. Blairb02a3bb2012-07-30 17:49:55 -07001173 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1174 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001175 A.addApproval('CRVW', 2)
1176 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001177
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001178 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001179
James E. Blaire2819012013-06-28 17:17:26 -04001180 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1181 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001182 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001183
1184 self.worker.release('.*-merge')
1185 self.waitUntilSettled()
1186
1187 self.worker.hold_jobs_in_build = False
1188 self.worker.release()
1189
1190 self.waitUntilSettled()
1191 # It's certain that the merge job for change 2 will run, but
1192 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001193 self.assertTrue(len(self.history) > 6)
1194 self.assertEqual(A.data['status'], 'NEW')
1195 self.assertEqual(B.data['status'], 'MERGED')
1196 self.assertEqual(A.reported, 2)
1197 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001198
1199 def test_independent_queues(self):
1200 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001201
1202 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001203 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001204 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1205 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001206 A.addApproval('CRVW', 2)
1207 B.addApproval('CRVW', 2)
1208 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001209
1210 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1211 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1212 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1213
James E. Blairb02a3bb2012-07-30 17:49:55 -07001214 self.waitUntilSettled()
1215
1216 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001217 self.assertEqual(len(self.builds), 2)
1218 self.assertEqual(self.builds[0].name, 'project-merge')
1219 self.assertTrue(self.job_has_changes(self.builds[0], A))
1220 self.assertEqual(self.builds[1].name, 'project1-merge')
1221 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001222
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001223 # Release the current merge builds
1224 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001225 self.waitUntilSettled()
1226 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001227 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001228 self.waitUntilSettled()
1229
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001230 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001231 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001232 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001233
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001234 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001235 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001236 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001237
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001238 self.assertEqual(len(self.history), 11)
1239 self.assertEqual(A.data['status'], 'MERGED')
1240 self.assertEqual(B.data['status'], 'MERGED')
1241 self.assertEqual(C.data['status'], 'MERGED')
1242 self.assertEqual(A.reported, 2)
1243 self.assertEqual(B.reported, 2)
1244 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001245
1246 def test_failed_change_at_head(self):
1247 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001248
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001249 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001250 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1251 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1252 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001253 A.addApproval('CRVW', 2)
1254 B.addApproval('CRVW', 2)
1255 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001256
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001257 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001258
1259 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1260 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1261 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1262
1263 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001264
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001265 self.assertEqual(len(self.builds), 1)
1266 self.assertEqual(self.builds[0].name, 'project-merge')
1267 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001268
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001269 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001270 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001271 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001272 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001273 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001274 self.waitUntilSettled()
1275
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001276 self.assertEqual(len(self.builds), 6)
1277 self.assertEqual(self.builds[0].name, 'project-test1')
1278 self.assertEqual(self.builds[1].name, 'project-test2')
1279 self.assertEqual(self.builds[2].name, 'project-test1')
1280 self.assertEqual(self.builds[3].name, 'project-test2')
1281 self.assertEqual(self.builds[4].name, 'project-test1')
1282 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001283
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001284 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001285 self.waitUntilSettled()
1286
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001287 # project-test2, project-merge for B
1288 self.assertEqual(len(self.builds), 2)
1289 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001290
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001291 self.worker.hold_jobs_in_build = False
1292 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001293 self.waitUntilSettled()
1294
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001295 self.assertEqual(len(self.builds), 0)
1296 self.assertEqual(len(self.history), 15)
1297 self.assertEqual(A.data['status'], 'NEW')
1298 self.assertEqual(B.data['status'], 'MERGED')
1299 self.assertEqual(C.data['status'], 'MERGED')
1300 self.assertEqual(A.reported, 2)
1301 self.assertEqual(B.reported, 2)
1302 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001303
1304 def test_failed_change_at_head_with_queue(self):
1305 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001306
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001307 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001308 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1309 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1310 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001311 A.addApproval('CRVW', 2)
1312 B.addApproval('CRVW', 2)
1313 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001314
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001315 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001316
1317 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1318 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1319 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1320
1321 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001322 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001323 self.assertEqual(len(self.builds), 0)
1324 self.assertEqual(len(queue), 1)
1325 self.assertEqual(queue[0].name, 'build:project-merge')
1326 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001327
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001328 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001329 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001330 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001331 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001332 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001333 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001334 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001335
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001336 self.assertEqual(len(self.builds), 0)
1337 self.assertEqual(len(queue), 6)
1338 self.assertEqual(queue[0].name, 'build:project-test1')
1339 self.assertEqual(queue[1].name, 'build:project-test2')
1340 self.assertEqual(queue[2].name, 'build:project-test1')
1341 self.assertEqual(queue[3].name, 'build:project-test2')
1342 self.assertEqual(queue[4].name, 'build:project-test1')
1343 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001344
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001345 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001346 self.waitUntilSettled()
1347
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001348 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001349 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001350 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1351 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001352
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001353 self.gearman_server.hold_jobs_in_queue = False
1354 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001355 self.waitUntilSettled()
1356
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001357 self.assertEqual(len(self.builds), 0)
1358 self.assertEqual(len(self.history), 11)
1359 self.assertEqual(A.data['status'], 'NEW')
1360 self.assertEqual(B.data['status'], 'MERGED')
1361 self.assertEqual(C.data['status'], 'MERGED')
1362 self.assertEqual(A.reported, 2)
1363 self.assertEqual(B.reported, 2)
1364 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001365
1366 def test_patch_order(self):
1367 "Test that dependent patches are tested in the right order"
1368 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1369 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1370 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1371 A.addApproval('CRVW', 2)
1372 B.addApproval('CRVW', 2)
1373 C.addApproval('CRVW', 2)
1374
1375 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1376 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1377 M2.setMerged()
1378 M1.setMerged()
1379
1380 # C -> B -> A -> M1 -> M2
1381 # M2 is here to make sure it is never queried. If it is, it
1382 # means zuul is walking down the entire history of merged
1383 # changes.
1384
1385 C.setDependsOn(B, 1)
1386 B.setDependsOn(A, 1)
1387 A.setDependsOn(M1, 1)
1388 M1.setDependsOn(M2, 1)
1389
1390 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1391
1392 self.waitUntilSettled()
1393
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001394 self.assertEqual(A.data['status'], 'NEW')
1395 self.assertEqual(B.data['status'], 'NEW')
1396 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001397
1398 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1399 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1400
1401 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001402 self.assertEqual(M2.queried, 0)
1403 self.assertEqual(A.data['status'], 'MERGED')
1404 self.assertEqual(B.data['status'], 'MERGED')
1405 self.assertEqual(C.data['status'], 'MERGED')
1406 self.assertEqual(A.reported, 2)
1407 self.assertEqual(B.reported, 2)
1408 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001409
James E. Blair0e933c52013-07-11 10:18:52 -07001410 def test_trigger_cache(self):
1411 "Test that the trigger cache operates correctly"
1412 self.worker.hold_jobs_in_build = True
1413
1414 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1415 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1416 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1417 A.addApproval('CRVW', 2)
1418 B.addApproval('CRVW', 2)
1419
1420 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1421 M1.setMerged()
1422
1423 B.setDependsOn(A, 1)
1424 A.setDependsOn(M1, 1)
1425
1426 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1427 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1428
1429 self.waitUntilSettled()
1430
1431 for build in self.builds:
1432 if build.parameters['ZUUL_PIPELINE'] == 'check':
1433 build.release()
1434 self.waitUntilSettled()
1435 for build in self.builds:
1436 if build.parameters['ZUUL_PIPELINE'] == 'check':
1437 build.release()
1438 self.waitUntilSettled()
1439
1440 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1441 self.waitUntilSettled()
1442
1443 self.log.debug("len %s " % self.sched.trigger._change_cache.keys())
1444 # there should still be changes in the cache
1445 self.assertNotEqual(len(self.sched.trigger._change_cache.keys()), 0)
1446
1447 self.worker.hold_jobs_in_build = False
1448 self.worker.release()
1449 self.waitUntilSettled()
1450
1451 self.assertEqual(A.data['status'], 'MERGED')
1452 self.assertEqual(B.data['status'], 'MERGED')
1453 self.assertEqual(A.queried, 2) # Initial and isMerged
1454 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1455
James E. Blair8c803f82012-07-31 16:25:42 -07001456 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001457 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001458 # TODO: move to test_gerrit (this is a unit test!)
1459 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4aea70c2012-07-26 14:23:24 -07001460 a = self.sched.trigger.getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001461 mgr = self.sched.layout.pipelines['gate'].manager
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001462 self.assertFalse(
1463 self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001464
1465 A.addApproval('CRVW', 2)
James E. Blairfee8d652013-06-07 08:57:52 -07001466 a = self.sched.trigger.getChange(1, 2, refresh=True)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001467 self.assertFalse(
1468 self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001469
1470 A.addApproval('APRV', 1)
James E. Blairfee8d652013-06-07 08:57:52 -07001471 a = self.sched.trigger.getChange(1, 2, refresh=True)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001472 self.assertTrue(
1473 self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blairfee8d652013-06-07 08:57:52 -07001474 self.sched.trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001475
1476 def test_build_configuration(self):
1477 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001478
1479 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001480 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1481 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1482 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1483 A.addApproval('CRVW', 2)
1484 B.addApproval('CRVW', 2)
1485 C.addApproval('CRVW', 2)
1486 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1487 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1488 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1489 self.waitUntilSettled()
1490
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001491 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001492 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001493 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001494 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001495 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001496 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001497 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001498 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1499 self.gearman_server.hold_jobs_in_queue = False
1500 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001501 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001502
Monty Taylorbc758832013-06-17 17:22:42 -04001503 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001504 repo = git.Repo(path)
1505 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1506 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001507 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001508 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001509
1510 def test_build_configuration_conflict(self):
1511 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001512
1513 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001514 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1515 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001516 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001517 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1518 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001519 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001520 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1521 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001522 A.addApproval('CRVW', 2)
1523 B.addApproval('CRVW', 2)
1524 C.addApproval('CRVW', 2)
1525 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1526 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1527 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1528 self.waitUntilSettled()
1529
James E. Blair6736beb2013-07-11 15:18:15 -07001530 self.assertEqual(A.reported, 1)
1531 self.assertEqual(B.reported, 1)
1532 self.assertEqual(C.reported, 1)
1533
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001534 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001535 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001536 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001537 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001538 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001539 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001540 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001541 self.getParameter(queue[-1], 'ZUUL_REF')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001542 self.gearman_server.hold_jobs_in_queue = False
1543 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001544 self.waitUntilSettled()
1545
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001546 self.assertEqual(A.data['status'], 'MERGED')
1547 self.assertEqual(B.data['status'], 'NEW')
1548 self.assertEqual(C.data['status'], 'MERGED')
1549 self.assertEqual(A.reported, 2)
1550 self.assertEqual(B.reported, 2)
1551 self.assertEqual(C.reported, 2)
James E. Blairdaabed22012-08-15 15:38:57 -07001552
James E. Blair6736beb2013-07-11 15:18:15 -07001553 def test_dequeue_conflict(self):
1554 "Test that the option to dequeue merge conflicts works"
1555
1556 self.gearman_server.hold_jobs_in_queue = True
1557 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1558 A.addPatchset(['conflict'])
1559 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1560 B.addPatchset(['conflict'])
1561 A.addApproval('CRVW', 2)
1562 B.addApproval('CRVW', 2)
1563 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1564 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1565 self.waitUntilSettled()
1566
1567 self.assertEqual(A.reported, 1)
1568 self.assertEqual(B.reported, 2)
1569
1570 self.gearman_server.hold_jobs_in_queue = False
1571 self.gearman_server.release()
1572 self.waitUntilSettled()
1573
1574 self.assertEqual(A.data['status'], 'MERGED')
1575 self.assertEqual(B.data['status'], 'NEW')
1576 self.assertEqual(A.reported, 2)
1577 self.assertEqual(B.reported, 2)
1578
James E. Blairdaabed22012-08-15 15:38:57 -07001579 def test_post(self):
1580 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001581
Zhongyue Luo5d556072012-09-21 02:00:47 +09001582 e = {
1583 "type": "ref-updated",
1584 "submitter": {
1585 "name": "User Name",
1586 },
1587 "refUpdate": {
1588 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1589 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1590 "refName": "master",
1591 "project": "org/project",
1592 }
1593 }
James E. Blairdaabed22012-08-15 15:38:57 -07001594 self.fake_gerrit.addEvent(e)
1595 self.waitUntilSettled()
1596
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001597 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001598 self.assertEqual(len(self.history), 1)
1599 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001600
1601 def test_build_configuration_branch(self):
1602 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001603
1604 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001605 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1606 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1607 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1608 A.addApproval('CRVW', 2)
1609 B.addApproval('CRVW', 2)
1610 C.addApproval('CRVW', 2)
1611 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1612 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1613 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1614 self.waitUntilSettled()
1615
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001616 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001617 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001618 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001619 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001620 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001621 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001622 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001623 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1624 self.gearman_server.hold_jobs_in_queue = False
1625 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001626 self.waitUntilSettled()
1627
Monty Taylorbc758832013-06-17 17:22:42 -04001628 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001629 repo = git.Repo(path)
1630 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1631 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001632 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001633 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001634
1635 def test_build_configuration_branch_interaction(self):
1636 "Test that switching between branches works"
1637 self.test_build_configuration()
1638 self.test_build_configuration_branch()
1639 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001640 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001641 repo = git.Repo(path)
1642 repo.heads.master.commit = repo.commit('init')
1643 self.test_build_configuration()
1644
1645 def test_build_configuration_multi_branch(self):
1646 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001647
1648 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001649 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1650 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1651 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1652 A.addApproval('CRVW', 2)
1653 B.addApproval('CRVW', 2)
1654 C.addApproval('CRVW', 2)
1655 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1656 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1657 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1658 self.waitUntilSettled()
1659
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001660 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001661 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001662 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001663 job_B = None
1664 for job in queue:
1665 if 'project-merge' in job.name:
1666 job_B = job
1667 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001668 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001669 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001670 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001671 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001672 for job in queue:
1673 if 'project-merge' in job.name:
1674 job_C = job
1675 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001676 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001677 self.gearman_server.hold_jobs_in_queue = False
1678 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001679 self.waitUntilSettled()
1680
Monty Taylorbc758832013-06-17 17:22:42 -04001681 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001682 repo = git.Repo(path)
1683
1684 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001685 for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001686 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001687 correct_messages = ['initial commit', 'A-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001688 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001689
1690 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001691 for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001692 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001693 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001694 self.assertEqual(repo_messages, correct_messages)
James E. Blair7f71c802012-08-22 13:04:32 -07001695
1696 def test_one_job_project(self):
1697 "Test that queueing works with one job"
1698 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1699 'master', 'A')
1700 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1701 'master', 'B')
1702 A.addApproval('CRVW', 2)
1703 B.addApproval('CRVW', 2)
1704 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1705 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1706 self.waitUntilSettled()
1707
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001708 self.assertEqual(A.data['status'], 'MERGED')
1709 self.assertEqual(A.reported, 2)
1710 self.assertEqual(B.data['status'], 'MERGED')
1711 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001712
Antoine Musso80edd5a2013-02-13 15:37:53 +01001713 def test_job_from_templates_launched(self):
1714 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001715
Antoine Musso80edd5a2013-02-13 15:37:53 +01001716 A = self.fake_gerrit.addFakeChange(
1717 'org/templated-project', 'master', 'A')
1718 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1719 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001720
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001721 self.assertEqual(self.getJobFromHistory('project-test1').result,
1722 'SUCCESS')
1723 self.assertEqual(self.getJobFromHistory('project-test2').result,
1724 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001725
James E. Blaircaec0c52012-08-22 14:52:22 -07001726 def test_dependent_changes_dequeue(self):
1727 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001728
James E. Blaircaec0c52012-08-22 14:52:22 -07001729 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1730 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1731 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1732 A.addApproval('CRVW', 2)
1733 B.addApproval('CRVW', 2)
1734 C.addApproval('CRVW', 2)
1735
1736 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1737 M1.setMerged()
1738
1739 # C -> B -> A -> M1
1740
1741 C.setDependsOn(B, 1)
1742 B.setDependsOn(A, 1)
1743 A.setDependsOn(M1, 1)
1744
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001745 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001746
1747 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1748 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1749 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1750
1751 self.waitUntilSettled()
1752
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001753 self.assertEqual(A.data['status'], 'NEW')
1754 self.assertEqual(A.reported, 2)
1755 self.assertEqual(B.data['status'], 'NEW')
1756 self.assertEqual(B.reported, 2)
1757 self.assertEqual(C.data['status'], 'NEW')
1758 self.assertEqual(C.reported, 2)
1759 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001760
1761 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001762 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001763 # If it's dequeued more than once, we should see extra
1764 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001765
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001766 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001767 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1768 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1769 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1770 A.addApproval('CRVW', 2)
1771 B.addApproval('CRVW', 2)
1772 C.addApproval('CRVW', 2)
1773
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001774 self.worker.addFailTest('project1-test1', A)
1775 self.worker.addFailTest('project1-test2', A)
1776 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001777
1778 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1779 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1780 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1781
1782 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001783
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001784 self.assertEqual(len(self.builds), 1)
1785 self.assertEqual(self.builds[0].name, 'project1-merge')
1786 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07001787
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001788 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001789 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001790 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001791 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001792 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001793 self.waitUntilSettled()
1794
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001795 self.assertEqual(len(self.builds), 9)
1796 self.assertEqual(self.builds[0].name, 'project1-test1')
1797 self.assertEqual(self.builds[1].name, 'project1-test2')
1798 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
1799 self.assertEqual(self.builds[3].name, 'project1-test1')
1800 self.assertEqual(self.builds[4].name, 'project1-test2')
1801 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
1802 self.assertEqual(self.builds[6].name, 'project1-test1')
1803 self.assertEqual(self.builds[7].name, 'project1-test2')
1804 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001805
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001806 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001807 self.waitUntilSettled()
1808
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001809 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1810 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001811
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001812 self.worker.hold_jobs_in_build = False
1813 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001814 self.waitUntilSettled()
1815
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001816 self.assertEqual(len(self.builds), 0)
1817 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001818
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001819 self.assertEqual(A.data['status'], 'NEW')
1820 self.assertEqual(B.data['status'], 'MERGED')
1821 self.assertEqual(C.data['status'], 'MERGED')
1822 self.assertEqual(A.reported, 2)
1823 self.assertEqual(B.reported, 2)
1824 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001825
1826 def test_nonvoting_job(self):
1827 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001828
James E. Blair4ec821f2012-08-23 15:28:28 -07001829 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1830 'master', 'A')
1831 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001832 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001833 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1834
1835 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001836
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001837 self.assertEqual(A.data['status'], 'MERGED')
1838 self.assertEqual(A.reported, 2)
1839 self.assertEqual(
1840 self.getJobFromHistory('nonvoting-project-merge').result,
1841 'SUCCESS')
1842 self.assertEqual(
1843 self.getJobFromHistory('nonvoting-project-test1').result,
1844 'SUCCESS')
1845 self.assertEqual(
1846 self.getJobFromHistory('nonvoting-project-test2').result,
1847 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001848
1849 def test_check_queue_success(self):
1850 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001851
James E. Blaire0487072012-08-29 17:38:31 -07001852 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1853 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1854
1855 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001856
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001857 self.assertEqual(A.data['status'], 'NEW')
1858 self.assertEqual(A.reported, 1)
1859 self.assertEqual(self.getJobFromHistory('project-merge').result,
1860 'SUCCESS')
1861 self.assertEqual(self.getJobFromHistory('project-test1').result,
1862 'SUCCESS')
1863 self.assertEqual(self.getJobFromHistory('project-test2').result,
1864 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001865
1866 def test_check_queue_failure(self):
1867 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001868
James E. Blaire0487072012-08-29 17:38:31 -07001869 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001870 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001871 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1872
1873 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001874
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001875 self.assertEqual(A.data['status'], 'NEW')
1876 self.assertEqual(A.reported, 1)
1877 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001878 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001879 self.assertEqual(self.getJobFromHistory('project-test1').result,
1880 'SUCCESS')
1881 self.assertEqual(self.getJobFromHistory('project-test2').result,
1882 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001883
1884 def test_dependent_behind_dequeue(self):
1885 "test that dependent changes behind dequeued changes work"
1886 # This complicated test is a reproduction of a real life bug
1887 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001888
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001889 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001890 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1891 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1892 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1893 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1894 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1895 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1896 D.setDependsOn(C, 1)
1897 E.setDependsOn(D, 1)
1898 A.addApproval('CRVW', 2)
1899 B.addApproval('CRVW', 2)
1900 C.addApproval('CRVW', 2)
1901 D.addApproval('CRVW', 2)
1902 E.addApproval('CRVW', 2)
1903 F.addApproval('CRVW', 2)
1904
1905 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001906
1907 # Change object re-use in the gerrit trigger is hidden if
1908 # changes are added in quick succession; waiting makes it more
1909 # like real life.
1910 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1911 self.waitUntilSettled()
1912 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1913 self.waitUntilSettled()
1914
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001915 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001916 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001917 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001918 self.waitUntilSettled()
1919
1920 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1921 self.waitUntilSettled()
1922 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1923 self.waitUntilSettled()
1924 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1925 self.waitUntilSettled()
1926 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1927 self.waitUntilSettled()
1928
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001929 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001930 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001931 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001932 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001933 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001934 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001935 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001936 self.waitUntilSettled()
1937
1938 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001939
1940 # Grab pointers to the jobs we want to release before
1941 # releasing any, because list indexes may change as
1942 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001943 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001944 a.release()
1945 b.release()
1946 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001947 self.waitUntilSettled()
1948
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001949 self.worker.hold_jobs_in_build = False
1950 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07001951 self.waitUntilSettled()
1952
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001953 self.assertEqual(A.data['status'], 'NEW')
1954 self.assertEqual(B.data['status'], 'MERGED')
1955 self.assertEqual(C.data['status'], 'MERGED')
1956 self.assertEqual(D.data['status'], 'MERGED')
1957 self.assertEqual(E.data['status'], 'MERGED')
1958 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001959
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001960 self.assertEqual(A.reported, 2)
1961 self.assertEqual(B.reported, 2)
1962 self.assertEqual(C.reported, 2)
1963 self.assertEqual(D.reported, 2)
1964 self.assertEqual(E.reported, 2)
1965 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001966
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001967 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1968 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001969
1970 def test_merger_repack(self):
1971 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001972
James E. Blair05fed602012-09-07 12:45:24 -07001973 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1974 A.addApproval('CRVW', 2)
1975 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1976 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001977 self.assertEqual(self.getJobFromHistory('project-merge').result,
1978 'SUCCESS')
1979 self.assertEqual(self.getJobFromHistory('project-test1').result,
1980 'SUCCESS')
1981 self.assertEqual(self.getJobFromHistory('project-test2').result,
1982 'SUCCESS')
1983 self.assertEqual(A.data['status'], 'MERGED')
1984 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001985 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07001986 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001987
Monty Taylorbc758832013-06-17 17:22:42 -04001988 path = os.path.join(self.git_root, "org/project")
1989 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001990
1991 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1992 A.addApproval('CRVW', 2)
1993 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1994 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001995 self.assertEqual(self.getJobFromHistory('project-merge').result,
1996 'SUCCESS')
1997 self.assertEqual(self.getJobFromHistory('project-test1').result,
1998 'SUCCESS')
1999 self.assertEqual(self.getJobFromHistory('project-test2').result,
2000 'SUCCESS')
2001 self.assertEqual(A.data['status'], 'MERGED')
2002 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002003
James E. Blair4886f282012-11-15 09:27:33 -08002004 def test_merger_repack_large_change(self):
2005 "Test that the merger works with large changes after a repack"
2006 # https://bugs.launchpad.net/zuul/+bug/1078946
2007 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2008 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002009 path = os.path.join(self.upstream_root, "org/project1")
2010 print repack_repo(path)
2011 path = os.path.join(self.git_root, "org/project1")
2012 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002013
2014 A.addApproval('CRVW', 2)
2015 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2016 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002017 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2018 'SUCCESS')
2019 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2020 'SUCCESS')
2021 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2022 'SUCCESS')
2023 self.assertEqual(A.data['status'], 'MERGED')
2024 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002025
James E. Blair7ee88a22012-09-12 18:59:31 +02002026 def test_nonexistent_job(self):
2027 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002028 # Set to the state immediately after a restart
2029 self.resetGearmanServer()
2030 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002031
2032 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2033 A.addApproval('CRVW', 2)
2034 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2035 # There may be a thread about to report a lost change
2036 while A.reported < 2:
2037 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002038 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002039 self.assertFalse(job_names)
2040 self.assertEqual(A.data['status'], 'NEW')
2041 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002042 self.assertEmptyQueues()
2043
2044 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002045 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002046 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2047 A.addApproval('CRVW', 2)
2048 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2049 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002050 self.assertEqual(self.getJobFromHistory('project-merge').result,
2051 'SUCCESS')
2052 self.assertEqual(self.getJobFromHistory('project-test1').result,
2053 'SUCCESS')
2054 self.assertEqual(self.getJobFromHistory('project-test2').result,
2055 'SUCCESS')
2056 self.assertEqual(A.data['status'], 'MERGED')
2057 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002058
2059 def test_single_nonexistent_post_job(self):
2060 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002061 e = {
2062 "type": "ref-updated",
2063 "submitter": {
2064 "name": "User Name",
2065 },
2066 "refUpdate": {
2067 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2068 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2069 "refName": "master",
2070 "project": "org/project",
2071 }
2072 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002073 # Set to the state immediately after a restart
2074 self.resetGearmanServer()
2075 self.launcher.negative_function_cache_ttl = 0
2076
James E. Blairf62d4282012-12-31 17:01:50 -08002077 self.fake_gerrit.addEvent(e)
2078 self.waitUntilSettled()
2079
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002080 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002081
2082 def test_new_patchset_dequeues_old(self):
2083 "Test that a new patchset causes the old to be dequeued"
2084 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002085 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002086 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2087 M.setMerged()
2088
2089 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2090 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2091 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2092 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2093 A.addApproval('CRVW', 2)
2094 B.addApproval('CRVW', 2)
2095 C.addApproval('CRVW', 2)
2096 D.addApproval('CRVW', 2)
2097
2098 C.setDependsOn(B, 1)
2099 B.setDependsOn(A, 1)
2100 A.setDependsOn(M, 1)
2101
2102 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2103 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2104 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2105 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2106 self.waitUntilSettled()
2107
2108 B.addPatchset()
2109 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2110 self.waitUntilSettled()
2111
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002112 self.worker.hold_jobs_in_build = False
2113 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002114 self.waitUntilSettled()
2115
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002116 self.assertEqual(A.data['status'], 'MERGED')
2117 self.assertEqual(A.reported, 2)
2118 self.assertEqual(B.data['status'], 'NEW')
2119 self.assertEqual(B.reported, 2)
2120 self.assertEqual(C.data['status'], 'NEW')
2121 self.assertEqual(C.reported, 2)
2122 self.assertEqual(D.data['status'], 'MERGED')
2123 self.assertEqual(D.reported, 2)
2124 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002125
2126 def test_new_patchset_dequeues_old_on_head(self):
2127 "Test that a new patchset causes the old to be dequeued (at head)"
2128 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002129 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002130 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2131 M.setMerged()
2132 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2133 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2134 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2135 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2136 A.addApproval('CRVW', 2)
2137 B.addApproval('CRVW', 2)
2138 C.addApproval('CRVW', 2)
2139 D.addApproval('CRVW', 2)
2140
2141 C.setDependsOn(B, 1)
2142 B.setDependsOn(A, 1)
2143 A.setDependsOn(M, 1)
2144
2145 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2146 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2147 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2148 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2149 self.waitUntilSettled()
2150
2151 A.addPatchset()
2152 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2153 self.waitUntilSettled()
2154
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002155 self.worker.hold_jobs_in_build = False
2156 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002157 self.waitUntilSettled()
2158
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002159 self.assertEqual(A.data['status'], 'NEW')
2160 self.assertEqual(A.reported, 2)
2161 self.assertEqual(B.data['status'], 'NEW')
2162 self.assertEqual(B.reported, 2)
2163 self.assertEqual(C.data['status'], 'NEW')
2164 self.assertEqual(C.reported, 2)
2165 self.assertEqual(D.data['status'], 'MERGED')
2166 self.assertEqual(D.reported, 2)
2167 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002168
2169 def test_new_patchset_dequeues_old_without_dependents(self):
2170 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002171 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002172 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2173 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2174 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2175 A.addApproval('CRVW', 2)
2176 B.addApproval('CRVW', 2)
2177 C.addApproval('CRVW', 2)
2178
2179 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2180 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2181 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2182 self.waitUntilSettled()
2183
2184 B.addPatchset()
2185 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2186 self.waitUntilSettled()
2187
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002188 self.worker.hold_jobs_in_build = False
2189 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002190 self.waitUntilSettled()
2191
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002192 self.assertEqual(A.data['status'], 'MERGED')
2193 self.assertEqual(A.reported, 2)
2194 self.assertEqual(B.data['status'], 'NEW')
2195 self.assertEqual(B.reported, 2)
2196 self.assertEqual(C.data['status'], 'MERGED')
2197 self.assertEqual(C.reported, 2)
2198 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002199
2200 def test_new_patchset_dequeues_old_independent_queue(self):
2201 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002202 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002203 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2204 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2205 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2206 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2207 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2208 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2209 self.waitUntilSettled()
2210
2211 B.addPatchset()
2212 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2213 self.waitUntilSettled()
2214
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002215 self.worker.hold_jobs_in_build = False
2216 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002217 self.waitUntilSettled()
2218
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002219 self.assertEqual(A.data['status'], 'NEW')
2220 self.assertEqual(A.reported, 1)
2221 self.assertEqual(B.data['status'], 'NEW')
2222 self.assertEqual(B.reported, 1)
2223 self.assertEqual(C.data['status'], 'NEW')
2224 self.assertEqual(C.reported, 1)
2225 self.assertEqual(len(self.history), 10)
2226 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002227
2228 def test_zuul_refs(self):
2229 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002230 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002231 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2232 M1.setMerged()
2233 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2234 M2.setMerged()
2235
2236 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2237 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2238 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2239 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2240 A.addApproval('CRVW', 2)
2241 B.addApproval('CRVW', 2)
2242 C.addApproval('CRVW', 2)
2243 D.addApproval('CRVW', 2)
2244 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2245 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2246 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2247 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2248
2249 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002250 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002251 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002252 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002253 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002254 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002255 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002256 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002257 self.waitUntilSettled()
2258
James E. Blair7d0dedc2013-02-21 17:26:09 -08002259 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002260 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002261 if x.parameters['ZUUL_CHANGE'] == '3':
2262 a_zref = x.parameters['ZUUL_REF']
2263 if x.parameters['ZUUL_CHANGE'] == '4':
2264 b_zref = x.parameters['ZUUL_REF']
2265 if x.parameters['ZUUL_CHANGE'] == '5':
2266 c_zref = x.parameters['ZUUL_REF']
2267 if x.parameters['ZUUL_CHANGE'] == '6':
2268 d_zref = x.parameters['ZUUL_REF']
2269
2270 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002271 self.assertIsNotNone(a_zref)
2272 self.assertIsNotNone(b_zref)
2273 self.assertIsNotNone(c_zref)
2274 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002275
2276 # And they should all be different
2277 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002278 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002279
2280 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002281 self.assertTrue(self.ref_has_change(a_zref, A))
2282 self.assertFalse(self.ref_has_change(a_zref, B))
2283 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002284
2285 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002286 self.assertTrue(self.ref_has_change(b_zref, A))
2287 self.assertTrue(self.ref_has_change(b_zref, B))
2288 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002289
2290 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002291 self.assertTrue(self.ref_has_change(c_zref, A))
2292 self.assertTrue(self.ref_has_change(c_zref, B))
2293 self.assertTrue(self.ref_has_change(c_zref, C))
2294 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002295
2296 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002297 self.assertTrue(self.ref_has_change(d_zref, A))
2298 self.assertTrue(self.ref_has_change(d_zref, B))
2299 self.assertTrue(self.ref_has_change(d_zref, C))
2300 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002301
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002302 self.worker.hold_jobs_in_build = False
2303 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002304 self.waitUntilSettled()
2305
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002306 self.assertEqual(A.data['status'], 'MERGED')
2307 self.assertEqual(A.reported, 2)
2308 self.assertEqual(B.data['status'], 'MERGED')
2309 self.assertEqual(B.reported, 2)
2310 self.assertEqual(C.data['status'], 'MERGED')
2311 self.assertEqual(C.reported, 2)
2312 self.assertEqual(D.data['status'], 'MERGED')
2313 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002314
James E. Blair412e5582013-04-22 15:50:12 -07002315 def test_statsd(self):
2316 "Test each of the statsd methods used in the scheduler"
2317 import extras
2318 statsd = extras.try_import('statsd.statsd')
2319 statsd.incr('test-incr')
2320 statsd.timing('test-timing', 3)
2321 statsd.gauge('test-guage', 12)
2322 self.assertReportedStat('test-incr', '1|c')
2323 self.assertReportedStat('test-timing', '3|ms')
2324 self.assertReportedStat('test-guage', '12|g')
2325
James E. Blair70c71582013-03-06 08:50:50 -08002326 def test_file_jobs(self):
2327 "Test that file jobs run only when appropriate"
2328 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2329 A.addPatchset(['pip-requires'])
2330 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2331 A.addApproval('CRVW', 2)
2332 B.addApproval('CRVW', 2)
2333 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2334 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2335 self.waitUntilSettled()
2336
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002337 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002338 if x.name == 'project-testfile']
2339
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002340 self.assertEqual(len(testfile_jobs), 1)
2341 self.assertEqual(testfile_jobs[0].changes, '1,2')
2342 self.assertEqual(A.data['status'], 'MERGED')
2343 self.assertEqual(A.reported, 2)
2344 self.assertEqual(B.data['status'], 'MERGED')
2345 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002346
2347 def test_test_config(self):
2348 "Test that we can test the config"
2349 sched = zuul.scheduler.Scheduler()
2350 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002351
2352 def test_build_description(self):
2353 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002354 self.worker.registerFunction('set_description:' +
2355 self.worker.worker_id)
2356
2357 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2358 A.addApproval('CRVW', 2)
2359 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2360 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002361 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002362 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002363 self.assertTrue(re.search("Branch.*master", desc))
2364 self.assertTrue(re.search("Pipeline.*gate", desc))
2365 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2366 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2367 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2368 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002369
James E. Blair64ed6f22013-07-10 14:07:23 -07002370 def test_queue_precedence(self):
2371 "Test that queue precedence works"
2372
2373 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002374 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002375 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2376 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2377 A.addApproval('CRVW', 2)
2378 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2379
2380 self.waitUntilSettled()
2381 self.gearman_server.hold_jobs_in_queue = False
2382 self.gearman_server.release()
2383 self.waitUntilSettled()
2384
James E. Blair8de58bd2013-07-18 16:23:33 -07002385 # Run one build at a time to ensure non-race order:
2386 for x in range(6):
2387 self.release(self.builds[0])
2388 self.waitUntilSettled()
2389 self.worker.hold_jobs_in_build = False
2390 self.waitUntilSettled()
2391
James E. Blair64ed6f22013-07-10 14:07:23 -07002392 self.log.debug(self.history)
2393 self.assertEqual(self.history[0].pipeline, 'gate')
2394 self.assertEqual(self.history[1].pipeline, 'check')
2395 self.assertEqual(self.history[2].pipeline, 'gate')
2396 self.assertEqual(self.history[3].pipeline, 'gate')
2397 self.assertEqual(self.history[4].pipeline, 'check')
2398 self.assertEqual(self.history[5].pipeline, 'check')
2399
James E. Blair1843a552013-07-03 14:19:52 -07002400 def test_json_status(self):
2401 "Test that we can retrieve JSON status info"
2402 self.worker.hold_jobs_in_build = True
2403 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2404 A.addApproval('CRVW', 2)
2405 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2406 self.waitUntilSettled()
2407
2408 port = self.webapp.server.socket.getsockname()[1]
2409
2410 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2411 data = f.read()
2412
2413 self.worker.hold_jobs_in_build = False
2414 self.worker.release()
2415 self.waitUntilSettled()
2416
2417 data = json.loads(data)
2418 status_jobs = set()
2419 for p in data['pipelines']:
2420 for q in p['change_queues']:
2421 for head in q['heads']:
2422 for change in head:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002423 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002424 for job in change['jobs']:
2425 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002426 self.assertIn('project-merge', status_jobs)
2427 self.assertIn('project-test1', status_jobs)
2428 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002429
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002430 def test_node_label(self):
2431 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002432 self.worker.registerFunction('build:node-project-test1:debian')
2433
2434 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2435 A.addApproval('CRVW', 2)
2436 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2437 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002438
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002439 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2440 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2441 'debian')
2442 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002443
2444 def test_live_reconfiguration(self):
2445 "Test that live reconfiguration works"
2446 self.worker.hold_jobs_in_build = True
2447 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2448 A.addApproval('CRVW', 2)
2449 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2450 self.waitUntilSettled()
2451
2452 self.sched.reconfigure(self.config)
2453
2454 self.worker.hold_jobs_in_build = False
2455 self.worker.release()
2456 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002457 self.assertEqual(self.getJobFromHistory('project-merge').result,
2458 'SUCCESS')
2459 self.assertEqual(self.getJobFromHistory('project-test1').result,
2460 'SUCCESS')
2461 self.assertEqual(self.getJobFromHistory('project-test2').result,
2462 'SUCCESS')
2463 self.assertEqual(A.data['status'], 'MERGED')
2464 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002465
2466 def test_delayed_repo_init(self):
2467 self.config.set('zuul', 'layout_config',
2468 'tests/fixtures/layout-delayed-repo-init.yaml')
2469 self.sched.reconfigure(self.config)
2470
2471 self.init_repo("org/new-project")
2472 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2473
2474 A.addApproval('CRVW', 2)
2475 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2476 self.waitUntilSettled()
2477 self.assertEqual(self.getJobFromHistory('project-merge').result,
2478 'SUCCESS')
2479 self.assertEqual(self.getJobFromHistory('project-test1').result,
2480 'SUCCESS')
2481 self.assertEqual(self.getJobFromHistory('project-test2').result,
2482 'SUCCESS')
2483 self.assertEqual(A.data['status'], 'MERGED')
2484 self.assertEqual(A.reported, 2)