blob: 85eb8adec054716513ca8281c2eb28237f6b2ddc [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,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700475 }
476
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'])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700504 )
505
506 self.job.sendWorkComplete(json.dumps(data))
507 del self.worker.gearman_jobs[self.job.unique]
508 self.worker.running_builds.remove(self)
509 self.worker.lock.release()
510
511
512class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400513 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700514 super(FakeWorker, self).__init__(worker_id)
515 self.gearman_jobs = {}
516 self.build_history = []
517 self.running_builds = []
518 self.build_counter = 0
519 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400520 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700521
522 self.hold_jobs_in_build = False
523 self.lock = threading.Lock()
524 self.__work_thread = threading.Thread(target=self.work)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400525 self.__work_thread.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700526 self.__work_thread.start()
527
528 def handleJob(self, job):
529 parts = job.name.split(":")
530 cmd = parts[0]
531 name = parts[1]
532 if len(parts) > 2:
533 node = parts[2]
534 else:
535 node = None
536 if cmd == 'build':
537 self.handleBuild(job, name, node)
538 elif cmd == 'stop':
539 self.handleStop(job, name)
540 elif cmd == 'set_description':
541 self.handleSetDescription(job, name)
542
543 def handleBuild(self, job, name, node):
544 build = FakeBuild(self, job, self.build_counter, node)
545 job.build = build
546 self.gearman_jobs[job.unique] = job
547 self.build_counter += 1
548
549 self.running_builds.append(build)
550 build.start()
551
552 def handleStop(self, job, name):
553 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700554 parameters = json.loads(job.arguments)
555 name = parameters['name']
556 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700557 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700558 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700559 build.aborted = True
560 build.release()
561 job.sendWorkComplete()
562 return
563 job.sendWorkFail()
564
565 def handleSetDescription(self, job, name):
566 self.log.debug("handle set description")
567 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700568 name = parameters['name']
569 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700570 descr = parameters['html_description']
571 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700572 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700573 build.description = descr
574 job.sendWorkComplete()
575 return
576 for build in self.build_history:
James E. Blair3c483cf2013-06-04 16:30:43 -0700577 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700578 build.description = descr
579 job.sendWorkComplete()
580 return
581 job.sendWorkFail()
582
583 def work(self):
584 while self.running:
585 try:
586 job = self.getJob()
587 except gear.InterruptedError:
588 continue
589 try:
590 self.handleJob(job)
591 except:
592 self.log.exception("Worker exception:")
593
594 def addFailTest(self, name, change):
595 l = self.fail_tests.get(name, [])
596 l.append(change)
597 self.fail_tests[name] = l
598
599 def shouldFailTest(self, name, ref):
600 l = self.fail_tests.get(name, [])
601 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400602 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700603 return True
604 return False
605
606 def release(self, regex=None):
607 builds = self.running_builds[:]
608 self.log.debug("releasing build %s (%s)" % (regex,
James E. Blair78e31b32013-07-09 09:11:34 -0700609 len(self.running_builds)))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700610 for build in builds:
611 if not regex or re.match(regex, build.name):
612 self.log.debug("releasing build %s" %
613 (build.parameters['ZUUL_UUID']))
614 build.release()
615 else:
616 self.log.debug("not releasing build %s" %
617 (build.parameters['ZUUL_UUID']))
618 self.log.debug("done releasing builds %s (%s)" %
619 (regex, len(self.running_builds)))
620
621
622class FakeGearmanServer(gear.Server):
623 def __init__(self):
624 self.hold_jobs_in_queue = False
625 super(FakeGearmanServer, self).__init__(0)
626
627 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700628 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
629 for job in queue:
630 if not hasattr(job, 'waiting'):
631 if job.name.startswith('build:'):
632 job.waiting = self.hold_jobs_in_queue
633 else:
634 job.waiting = False
635 if job.waiting:
636 continue
637 if job.name in connection.functions:
638 if not peek:
639 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400640 connection.related_jobs[job.handle] = job
641 job.worker_connection = connection
642 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700643 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700644 return None
645
646 def release(self, regex=None):
647 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700648 qlen = (len(self.high_queue) + len(self.normal_queue) +
649 len(self.low_queue))
650 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
651 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
652 queue = queue[:]
653 for job in queue:
654 cmd, name = job.name.split(':')
655 if cmd != 'build':
656 continue
657 if not regex or re.match(regex, name):
658 self.log.debug("releasing queued job %s" %
659 job.unique)
660 job.waiting = False
661 released = True
662 else:
663 self.log.debug("not releasing queued job %s" %
664 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700665 if released:
666 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700667 qlen = (len(self.high_queue) + len(self.normal_queue) +
668 len(self.low_queue))
669 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700670
671
Monty Taylorbc758832013-06-17 17:22:42 -0400672class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700673 log = logging.getLogger("zuul.test")
674
675 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400676 super(TestScheduler, self).setUp()
677 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
678 try:
679 test_timeout = int(test_timeout)
680 except ValueError:
681 # If timeout value is invalid do not set a timeout.
682 test_timeout = 0
683 if test_timeout > 0:
684 self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
685
686 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
687 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
688 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
689 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
690 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
691 os.environ.get('OS_STDERR_CAPTURE') == '1'):
692 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
693 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Monty Taylore6a6c402013-07-02 09:25:55 -0700694 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
695 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400696 self.useFixture(fixtures.FakeLogger(
697 level=logging.DEBUG,
698 format='%(asctime)s %(name)-32s '
699 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700700 tmp_root = self.useFixture(fixtures.TempDir(
701 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400702 self.test_root = os.path.join(tmp_root, "zuul-test")
703 self.upstream_root = os.path.join(self.test_root, "upstream")
704 self.git_root = os.path.join(self.test_root, "git")
705
706 CONFIG.set('zuul', 'git_dir', self.git_root)
707 if os.path.exists(self.test_root):
708 shutil.rmtree(self.test_root)
709 os.makedirs(self.test_root)
710 os.makedirs(self.upstream_root)
711 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700712
713 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400714 self.init_repo("org/project")
715 self.init_repo("org/project1")
716 self.init_repo("org/project2")
717 self.init_repo("org/project3")
718 self.init_repo("org/one-job-project")
719 self.init_repo("org/nonvoting-project")
720 self.init_repo("org/templated-project")
721 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700722 self.init_repo("org/conflict-project")
James E. Blair412e5582013-04-22 15:50:12 -0700723
724 self.statsd = FakeStatsd()
725 os.environ['STATSD_HOST'] = 'localhost'
726 os.environ['STATSD_PORT'] = str(self.statsd.port)
727 self.statsd.start()
728 # the statsd client object is configured in the statsd module import
729 reload(statsd)
730 reload(zuul.scheduler)
731
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700732 self.gearman_server = FakeGearmanServer()
733
734 self.config = ConfigParser.ConfigParser()
735 cfg = StringIO()
736 CONFIG.write(cfg)
737 cfg.seek(0)
738 self.config.readfp(cfg)
739 self.config.set('gearman', 'port', str(self.gearman_server.port))
740
Monty Taylorbc758832013-06-17 17:22:42 -0400741 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700742 self.worker.addServer('127.0.0.1', self.gearman_server.port)
743 self.gearman_server.worker = self.worker
744
James E. Blairb0fcae42012-07-17 11:12:10 -0700745 self.sched = zuul.scheduler.Scheduler()
746
James E. Blair8cc15a82012-08-01 11:17:57 -0700747 def URLOpenerFactory(*args, **kw):
748 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400749 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700750
James E. Blair8cc15a82012-08-01 11:17:57 -0700751 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700752 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700753
754 zuul.lib.gerrit.Gerrit = FakeGerrit
755
Monty Taylorbc758832013-06-17 17:22:42 -0400756 self.gerrit = FakeGerritTrigger(
757 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700758 self.gerrit.replication_timeout = 1.5
759 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700760 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400761 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700762
James E. Blair1843a552013-07-03 14:19:52 -0700763 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
764
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700765 self.sched.setLauncher(self.launcher)
James E. Blairb0fcae42012-07-17 11:12:10 -0700766 self.sched.setTrigger(self.gerrit)
767
768 self.sched.start()
769 self.sched.reconfigure(self.config)
770 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700771 self.webapp.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700772 self.launcher.gearman.waitForServer()
773 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400774 self.builds = self.worker.running_builds
775 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700776
James E. Blairfee8d652013-06-07 08:57:52 -0700777 self.addCleanup(self.assertFinalState)
778 self.addCleanup(self.shutdown)
779
780 def assertFinalState(self):
781 # Make sure that the change cache is cleared
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400782 self.assertEqual(len(self.sched.trigger._change_cache.keys()), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700783 self.assertEmptyQueues()
784
785 def shutdown(self):
786 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700787 self.launcher.stop()
788 self.worker.shutdown()
789 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700790 self.gerrit.stop()
791 self.sched.stop()
792 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700793 self.statsd.stop()
794 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700795 self.webapp.stop()
796 self.webapp.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700797 threads = threading.enumerate()
798 if len(threads) > 1:
799 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400800 super(TestScheduler, self).tearDown()
801
802 def init_repo(self, project):
803 parts = project.split('/')
804 path = os.path.join(self.upstream_root, *parts[:-1])
805 if not os.path.exists(path):
806 os.makedirs(path)
807 path = os.path.join(self.upstream_root, project)
808 repo = git.Repo.init(path)
809
810 repo.config_writer().set_value('user', 'email', 'user@example.com')
811 repo.config_writer().set_value('user', 'name', 'User Name')
812 repo.config_writer().write()
813
814 fn = os.path.join(path, 'README')
815 f = open(fn, 'w')
816 f.write("test\n")
817 f.close()
818 repo.index.add([fn])
819 repo.index.commit('initial commit')
820 master = repo.create_head('master')
821 repo.create_tag('init')
822
823 mp = repo.create_head('mp')
824 repo.head.reference = mp
825 f = open(fn, 'a')
826 f.write("test mp\n")
827 f.close()
828 repo.index.add([fn])
829 repo.index.commit('mp commit')
830
831 repo.head.reference = master
832 repo.head.reset(index=True, working_tree=True)
833 repo.git.clean('-x', '-f', '-d')
834
835 def ref_has_change(self, ref, change):
836 path = os.path.join(self.git_root, change.project)
837 repo = git.Repo(path)
838 for commit in repo.iter_commits(ref):
839 if commit.message.strip() == ('%s-1' % change.subject):
840 return True
841 return False
842
843 def job_has_changes(self, *args):
844 job = args[0]
845 commits = args[1:]
846 if isinstance(job, FakeBuild):
847 parameters = job.parameters
848 else:
849 parameters = json.loads(job.arguments)
850 project = parameters['ZUUL_PROJECT']
851 path = os.path.join(self.git_root, project)
852 repo = git.Repo(path)
853 ref = parameters['ZUUL_REF']
854 sha = parameters['ZUUL_COMMIT']
855 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
856 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
857 commit_messages = ['%s-1' % commit.subject for commit in commits]
858 for msg in commit_messages:
859 if msg not in repo_messages:
860 return False
861 if repo_shas[0] != sha:
862 return False
863 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700864
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700865 def registerJobs(self):
866 count = 0
James E. Blaireff88162013-07-01 12:44:14 -0400867 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700868 self.worker.registerFunction('build:' + job)
869 count += 1
870 self.worker.registerFunction('stop:' + self.worker.worker_id)
871 count += 1
872
873 while len(self.gearman_server.functions) < count:
874 time.sleep(0)
875
876 def release(self, job):
877 if isinstance(job, FakeBuild):
878 job.release()
879 else:
880 job.waiting = False
881 self.log.debug("Queued job %s released" % job.unique)
882 self.gearman_server.wakeConnections()
883
884 def getParameter(self, job, name):
885 if isinstance(job, FakeBuild):
886 return job.parameters[name]
887 else:
888 parameters = json.loads(job.arguments)
889 return parameters[name]
890
891 def resetGearmanServer(self):
892 self.worker.setFunctions([])
893 while True:
894 done = True
895 for connection in self.gearman_server.active_connections:
896 if connection.functions:
897 done = False
898 if done:
899 break
900 time.sleep(0)
901 self.gearman_server.functions = set()
902
903 def haveAllBuildsReported(self):
904 # See if Zuul is waiting on a meta job to complete
905 if self.launcher.meta_jobs:
906 return False
907 # Find out if every build that the worker has completed has been
908 # reported back to Zuul. If it hasn't then that means a Gearman
909 # event is still in transit and the system is not stable.
910 for build in self.worker.build_history:
911 zbuild = self.launcher.builds.get(build.uuid)
912 if not zbuild:
913 # It has already been reported
914 continue
915 # It hasn't been reported yet.
916 return False
917 # Make sure that none of the worker connections are in GRAB_WAIT
918 for connection in self.worker.active_connections:
919 if connection.state == 'GRAB_WAIT':
920 return False
921 return True
922
923 def areAllBuildsWaiting(self):
924 ret = True
925
926 builds = self.launcher.builds.values()
927 for build in builds:
928 client_job = None
929 for conn in self.launcher.gearman.active_connections:
930 for j in conn.related_jobs.values():
931 if j.unique == build.uuid:
932 client_job = j
933 break
934 if not client_job:
935 self.log.debug("%s is not known to the gearman client" %
936 build)
937 ret = False
938 continue
939 if not client_job.handle:
940 self.log.debug("%s has no handle" % client_job)
941 ret = False
942 continue
943 server_job = self.gearman_server.jobs.get(client_job.handle)
944 if not server_job:
945 self.log.debug("%s is not known to the gearman server" %
946 client_job)
947 ret = False
948 continue
949 if not hasattr(server_job, 'waiting'):
950 self.log.debug("%s is being enqueued" % server_job)
951 ret = False
952 continue
953 if server_job.waiting:
954 continue
955 worker_job = self.worker.gearman_jobs.get(server_job.unique)
956 if worker_job:
957 if worker_job.build.isWaiting():
958 continue
959 else:
960 self.log.debug("%s is running" % worker_job)
961 ret = False
962 else:
963 self.log.debug("%s is unassigned" % server_job)
964 ret = False
965 return ret
966
James E. Blairb0fcae42012-07-17 11:12:10 -0700967 def waitUntilSettled(self):
968 self.log.debug("Waiting until settled...")
969 start = time.time()
970 while True:
971 if time.time() - start > 10:
972 print 'queue status:',
973 print self.sched.trigger_event_queue.empty(),
974 print self.sched.result_event_queue.empty(),
975 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700976 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -0700977 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700978 # Make sure no new events show up while we're checking
979 self.worker.lock.acquire()
980 # have all build states propogated to zuul?
981 if self.haveAllBuildsReported():
982 # Join ensures that the queue is empty _and_ events have been
983 # processed
984 self.fake_gerrit.event_queue.join()
985 self.sched.trigger_event_queue.join()
986 self.sched.result_event_queue.join()
987 if (self.sched.trigger_event_queue.empty() and
988 self.sched.result_event_queue.empty() and
989 self.fake_gerrit.event_queue.empty() and
990 self.areAllBuildsWaiting()):
991 self.worker.lock.release()
992 self.log.debug("...settled.")
993 return
994 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700995 self.sched.wake_event.wait(0.1)
996
James E. Blaird466dc42012-07-31 10:42:56 -0700997 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -0800998 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -0700999 return len(jobs)
1000
James E. Blair4ca985f2013-05-30 12:27:43 -07001001 def getJobFromHistory(self, name):
1002 history = self.worker.build_history
1003 for job in history:
1004 if job.name == name:
1005 return job
1006 raise Exception("Unable to find job %s in history" % name)
1007
James E. Blaire0487072012-08-29 17:38:31 -07001008 def assertEmptyQueues(self):
1009 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001010 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001011 for queue in pipeline.queues:
1012 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001013 print 'pipeline %s queue %s contents %s' % (
1014 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001015 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001016 if len(queue.severed_heads) != 0:
1017 print 'heads', queue.severed_heads
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001018 self.assertEqual(len(queue.severed_heads), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001019
James E. Blair412e5582013-04-22 15:50:12 -07001020 def assertReportedStat(self, key, value=None):
1021 start = time.time()
1022 while time.time() < (start + 5):
1023 for stat in self.statsd.stats:
1024 k, v = stat.split(':')
1025 if key == k:
1026 if value is None:
1027 return
1028 if value == v:
1029 return
1030 time.sleep(0.1)
1031
1032 pprint.pprint(self.statsd.stats)
1033 raise Exception("Key %s not found in reported stats" % key)
1034
James E. Blairb0fcae42012-07-17 11:12:10 -07001035 def test_jobs_launched(self):
1036 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001037
James E. Blairb0fcae42012-07-17 11:12:10 -07001038 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001039 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001040 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1041 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001042 self.assertEqual(self.getJobFromHistory('project-merge').result,
1043 'SUCCESS')
1044 self.assertEqual(self.getJobFromHistory('project-test1').result,
1045 'SUCCESS')
1046 self.assertEqual(self.getJobFromHistory('project-test2').result,
1047 'SUCCESS')
1048 self.assertEqual(A.data['status'], 'MERGED')
1049 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001050
James E. Blair412e5582013-04-22 15:50:12 -07001051 self.assertReportedStat('gerrit.event.comment-added', '1|c')
1052 self.assertReportedStat('zuul.pipeline.gate.current_changes', '1|g')
1053 self.assertReportedStat('zuul.job.project-merge')
1054 self.assertReportedStat('zuul.pipeline.gate.resident_time')
1055 self.assertReportedStat('zuul.pipeline.gate.total_changes', '1|c')
1056 self.assertReportedStat(
1057 'zuul.pipeline.gate.org.project.resident_time')
1058 self.assertReportedStat(
1059 'zuul.pipeline.gate.org.project.total_changes', '1|c')
1060
James E. Blair42f74822013-05-14 15:18:03 -07001061 def test_duplicate_pipelines(self):
1062 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001063
James E. Blair42f74822013-05-14 15:18:03 -07001064 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1065 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1066 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001067
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001068 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001069 self.history[0].name == 'project-test1'
1070 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001071
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001072 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001073 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001074 self.assertIn('dup1/project-test1', A.messages[0])
1075 self.assertNotIn('dup2/project-test1', A.messages[0])
1076 self.assertNotIn('dup1/project-test1', A.messages[1])
1077 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001078 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001079 self.assertIn('dup1/project-test1', A.messages[1])
1080 self.assertNotIn('dup2/project-test1', A.messages[1])
1081 self.assertNotIn('dup1/project-test1', A.messages[0])
1082 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001083
James E. Blairb0fcae42012-07-17 11:12:10 -07001084 def test_parallel_changes(self):
1085 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001086
1087 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001088 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1089 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1090 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001091 A.addApproval('CRVW', 2)
1092 B.addApproval('CRVW', 2)
1093 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001094
1095 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1096 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1097 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1098
1099 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001100 self.assertEqual(len(self.builds), 1)
1101 self.assertEqual(self.builds[0].name, 'project-merge')
1102 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001103
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001104 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001105 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001106 self.assertEqual(len(self.builds), 3)
1107 self.assertEqual(self.builds[0].name, 'project-test1')
1108 self.assertTrue(self.job_has_changes(self.builds[0], A))
1109 self.assertEqual(self.builds[1].name, 'project-test2')
1110 self.assertTrue(self.job_has_changes(self.builds[1], A))
1111 self.assertEqual(self.builds[2].name, 'project-merge')
1112 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001113
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001114 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001115 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001116 self.assertEqual(len(self.builds), 5)
1117 self.assertEqual(self.builds[0].name, 'project-test1')
1118 self.assertTrue(self.job_has_changes(self.builds[0], A))
1119 self.assertEqual(self.builds[1].name, 'project-test2')
1120 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001121
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001122 self.assertEqual(self.builds[2].name, 'project-test1')
1123 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1124 self.assertEqual(self.builds[3].name, 'project-test2')
1125 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001126
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001127 self.assertEqual(self.builds[4].name, 'project-merge')
1128 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001129
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001130 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001131 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001132 self.assertEqual(len(self.builds), 6)
1133 self.assertEqual(self.builds[0].name, 'project-test1')
1134 self.assertTrue(self.job_has_changes(self.builds[0], A))
1135 self.assertEqual(self.builds[1].name, 'project-test2')
1136 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001137
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001138 self.assertEqual(self.builds[2].name, 'project-test1')
1139 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1140 self.assertEqual(self.builds[3].name, 'project-test2')
1141 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001142
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001143 self.assertEqual(self.builds[4].name, 'project-test1')
1144 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1145 self.assertEqual(self.builds[5].name, 'project-test2')
1146 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001147
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001148 self.worker.hold_jobs_in_build = False
1149 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001150 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001151 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001152
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001153 self.assertEqual(len(self.history), 9)
1154 self.assertEqual(A.data['status'], 'MERGED')
1155 self.assertEqual(B.data['status'], 'MERGED')
1156 self.assertEqual(C.data['status'], 'MERGED')
1157 self.assertEqual(A.reported, 2)
1158 self.assertEqual(B.reported, 2)
1159 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001160
1161 def test_failed_changes(self):
1162 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001163 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001164
James E. Blairb02a3bb2012-07-30 17:49:55 -07001165 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1166 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001167 A.addApproval('CRVW', 2)
1168 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001169
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001170 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001171
James E. Blaire2819012013-06-28 17:17:26 -04001172 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1173 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001174 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001175
1176 self.worker.release('.*-merge')
1177 self.waitUntilSettled()
1178
1179 self.worker.hold_jobs_in_build = False
1180 self.worker.release()
1181
1182 self.waitUntilSettled()
1183 # It's certain that the merge job for change 2 will run, but
1184 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001185 self.assertTrue(len(self.history) > 6)
1186 self.assertEqual(A.data['status'], 'NEW')
1187 self.assertEqual(B.data['status'], 'MERGED')
1188 self.assertEqual(A.reported, 2)
1189 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001190
1191 def test_independent_queues(self):
1192 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001193
1194 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001195 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001196 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1197 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001198 A.addApproval('CRVW', 2)
1199 B.addApproval('CRVW', 2)
1200 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001201
1202 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1203 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1204 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1205
James E. Blairb02a3bb2012-07-30 17:49:55 -07001206 self.waitUntilSettled()
1207
1208 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001209 self.assertEqual(len(self.builds), 2)
1210 self.assertEqual(self.builds[0].name, 'project-merge')
1211 self.assertTrue(self.job_has_changes(self.builds[0], A))
1212 self.assertEqual(self.builds[1].name, 'project1-merge')
1213 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001214
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001215 # Release the current merge builds
1216 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001217 self.waitUntilSettled()
1218 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001219 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001220 self.waitUntilSettled()
1221
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001222 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001223 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001224 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001225
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001226 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001227 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001228 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001229
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001230 self.assertEqual(len(self.history), 11)
1231 self.assertEqual(A.data['status'], 'MERGED')
1232 self.assertEqual(B.data['status'], 'MERGED')
1233 self.assertEqual(C.data['status'], 'MERGED')
1234 self.assertEqual(A.reported, 2)
1235 self.assertEqual(B.reported, 2)
1236 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001237
1238 def test_failed_change_at_head(self):
1239 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001240
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001241 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001242 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1243 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1244 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001245 A.addApproval('CRVW', 2)
1246 B.addApproval('CRVW', 2)
1247 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001248
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001249 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001250
1251 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1252 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1253 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1254
1255 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001256
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001257 self.assertEqual(len(self.builds), 1)
1258 self.assertEqual(self.builds[0].name, 'project-merge')
1259 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001260
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001261 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001262 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001263 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001264 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001265 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001266 self.waitUntilSettled()
1267
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001268 self.assertEqual(len(self.builds), 6)
1269 self.assertEqual(self.builds[0].name, 'project-test1')
1270 self.assertEqual(self.builds[1].name, 'project-test2')
1271 self.assertEqual(self.builds[2].name, 'project-test1')
1272 self.assertEqual(self.builds[3].name, 'project-test2')
1273 self.assertEqual(self.builds[4].name, 'project-test1')
1274 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001275
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001276 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001277 self.waitUntilSettled()
1278
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001279 # project-test2, project-merge for B
1280 self.assertEqual(len(self.builds), 2)
1281 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001282
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001283 self.worker.hold_jobs_in_build = False
1284 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001285 self.waitUntilSettled()
1286
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001287 self.assertEqual(len(self.builds), 0)
1288 self.assertEqual(len(self.history), 15)
1289 self.assertEqual(A.data['status'], 'NEW')
1290 self.assertEqual(B.data['status'], 'MERGED')
1291 self.assertEqual(C.data['status'], 'MERGED')
1292 self.assertEqual(A.reported, 2)
1293 self.assertEqual(B.reported, 2)
1294 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001295
1296 def test_failed_change_at_head_with_queue(self):
1297 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001298
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001299 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001300 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1301 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1302 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001303 A.addApproval('CRVW', 2)
1304 B.addApproval('CRVW', 2)
1305 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001306
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001307 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001308
1309 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1310 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1311 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1312
1313 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001314 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001315 self.assertEqual(len(self.builds), 0)
1316 self.assertEqual(len(queue), 1)
1317 self.assertEqual(queue[0].name, 'build:project-merge')
1318 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001319
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001320 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001321 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001322 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001323 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001324 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001325 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001326 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001327
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001328 self.assertEqual(len(self.builds), 0)
1329 self.assertEqual(len(queue), 6)
1330 self.assertEqual(queue[0].name, 'build:project-test1')
1331 self.assertEqual(queue[1].name, 'build:project-test2')
1332 self.assertEqual(queue[2].name, 'build:project-test1')
1333 self.assertEqual(queue[3].name, 'build:project-test2')
1334 self.assertEqual(queue[4].name, 'build:project-test1')
1335 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001336
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001337 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001338 self.waitUntilSettled()
1339
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001340 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001341 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001342 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1343 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001344
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001345 self.gearman_server.hold_jobs_in_queue = False
1346 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001347 self.waitUntilSettled()
1348
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001349 self.assertEqual(len(self.builds), 0)
1350 self.assertEqual(len(self.history), 11)
1351 self.assertEqual(A.data['status'], 'NEW')
1352 self.assertEqual(B.data['status'], 'MERGED')
1353 self.assertEqual(C.data['status'], 'MERGED')
1354 self.assertEqual(A.reported, 2)
1355 self.assertEqual(B.reported, 2)
1356 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001357
1358 def test_patch_order(self):
1359 "Test that dependent patches are tested in the right order"
1360 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1361 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1362 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1363 A.addApproval('CRVW', 2)
1364 B.addApproval('CRVW', 2)
1365 C.addApproval('CRVW', 2)
1366
1367 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1368 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1369 M2.setMerged()
1370 M1.setMerged()
1371
1372 # C -> B -> A -> M1 -> M2
1373 # M2 is here to make sure it is never queried. If it is, it
1374 # means zuul is walking down the entire history of merged
1375 # changes.
1376
1377 C.setDependsOn(B, 1)
1378 B.setDependsOn(A, 1)
1379 A.setDependsOn(M1, 1)
1380 M1.setDependsOn(M2, 1)
1381
1382 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1383
1384 self.waitUntilSettled()
1385
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001386 self.assertEqual(A.data['status'], 'NEW')
1387 self.assertEqual(B.data['status'], 'NEW')
1388 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001389
1390 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1391 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1392
1393 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001394 self.assertEqual(M2.queried, 0)
1395 self.assertEqual(A.data['status'], 'MERGED')
1396 self.assertEqual(B.data['status'], 'MERGED')
1397 self.assertEqual(C.data['status'], 'MERGED')
1398 self.assertEqual(A.reported, 2)
1399 self.assertEqual(B.reported, 2)
1400 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001401
James E. Blair0e933c52013-07-11 10:18:52 -07001402 def test_trigger_cache(self):
1403 "Test that the trigger cache operates correctly"
1404 self.worker.hold_jobs_in_build = True
1405
1406 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1407 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1408 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1409 A.addApproval('CRVW', 2)
1410 B.addApproval('CRVW', 2)
1411
1412 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1413 M1.setMerged()
1414
1415 B.setDependsOn(A, 1)
1416 A.setDependsOn(M1, 1)
1417
1418 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1419 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1420
1421 self.waitUntilSettled()
1422
1423 for build in self.builds:
1424 if build.parameters['ZUUL_PIPELINE'] == 'check':
1425 build.release()
1426 self.waitUntilSettled()
1427 for build in self.builds:
1428 if build.parameters['ZUUL_PIPELINE'] == 'check':
1429 build.release()
1430 self.waitUntilSettled()
1431
1432 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1433 self.waitUntilSettled()
1434
1435 self.log.debug("len %s " % self.sched.trigger._change_cache.keys())
1436 # there should still be changes in the cache
1437 self.assertNotEqual(len(self.sched.trigger._change_cache.keys()), 0)
1438
1439 self.worker.hold_jobs_in_build = False
1440 self.worker.release()
1441 self.waitUntilSettled()
1442
1443 self.assertEqual(A.data['status'], 'MERGED')
1444 self.assertEqual(B.data['status'], 'MERGED')
1445 self.assertEqual(A.queried, 2) # Initial and isMerged
1446 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1447
James E. Blair8c803f82012-07-31 16:25:42 -07001448 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001449 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001450 # TODO: move to test_gerrit (this is a unit test!)
1451 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4aea70c2012-07-26 14:23:24 -07001452 a = self.sched.trigger.getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001453 mgr = self.sched.layout.pipelines['gate'].manager
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001454 self.assertFalse(
1455 self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001456
1457 A.addApproval('CRVW', 2)
James E. Blairfee8d652013-06-07 08:57:52 -07001458 a = self.sched.trigger.getChange(1, 2, refresh=True)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001459 self.assertFalse(
1460 self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001461
1462 A.addApproval('APRV', 1)
James E. Blairfee8d652013-06-07 08:57:52 -07001463 a = self.sched.trigger.getChange(1, 2, refresh=True)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001464 self.assertTrue(
1465 self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blairfee8d652013-06-07 08:57:52 -07001466 self.sched.trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001467
1468 def test_build_configuration(self):
1469 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001470
1471 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001472 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1473 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1474 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1475 A.addApproval('CRVW', 2)
1476 B.addApproval('CRVW', 2)
1477 C.addApproval('CRVW', 2)
1478 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1479 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1480 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1481 self.waitUntilSettled()
1482
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001483 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001484 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001485 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001486 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001487 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001488 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001489 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001490 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1491 self.gearman_server.hold_jobs_in_queue = False
1492 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001493 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001494
Monty Taylorbc758832013-06-17 17:22:42 -04001495 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001496 repo = git.Repo(path)
1497 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1498 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001499 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001500 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001501
1502 def test_build_configuration_conflict(self):
1503 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001504
1505 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001506 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1507 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001508 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001509 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1510 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001511 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001512 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1513 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001514 A.addApproval('CRVW', 2)
1515 B.addApproval('CRVW', 2)
1516 C.addApproval('CRVW', 2)
1517 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1518 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1519 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1520 self.waitUntilSettled()
1521
James E. Blair6736beb2013-07-11 15:18:15 -07001522 self.assertEqual(A.reported, 1)
1523 self.assertEqual(B.reported, 1)
1524 self.assertEqual(C.reported, 1)
1525
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001526 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001527 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001528 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001529 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001530 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001531 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001532 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001533 self.getParameter(queue[-1], 'ZUUL_REF')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001534 self.gearman_server.hold_jobs_in_queue = False
1535 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001536 self.waitUntilSettled()
1537
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001538 self.assertEqual(A.data['status'], 'MERGED')
1539 self.assertEqual(B.data['status'], 'NEW')
1540 self.assertEqual(C.data['status'], 'MERGED')
1541 self.assertEqual(A.reported, 2)
1542 self.assertEqual(B.reported, 2)
1543 self.assertEqual(C.reported, 2)
James E. Blairdaabed22012-08-15 15:38:57 -07001544
James E. Blair6736beb2013-07-11 15:18:15 -07001545 def test_dequeue_conflict(self):
1546 "Test that the option to dequeue merge conflicts works"
1547
1548 self.gearman_server.hold_jobs_in_queue = True
1549 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1550 A.addPatchset(['conflict'])
1551 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1552 B.addPatchset(['conflict'])
1553 A.addApproval('CRVW', 2)
1554 B.addApproval('CRVW', 2)
1555 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1556 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1557 self.waitUntilSettled()
1558
1559 self.assertEqual(A.reported, 1)
1560 self.assertEqual(B.reported, 2)
1561
1562 self.gearman_server.hold_jobs_in_queue = False
1563 self.gearman_server.release()
1564 self.waitUntilSettled()
1565
1566 self.assertEqual(A.data['status'], 'MERGED')
1567 self.assertEqual(B.data['status'], 'NEW')
1568 self.assertEqual(A.reported, 2)
1569 self.assertEqual(B.reported, 2)
1570
James E. Blairdaabed22012-08-15 15:38:57 -07001571 def test_post(self):
1572 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001573
Zhongyue Luo5d556072012-09-21 02:00:47 +09001574 e = {
1575 "type": "ref-updated",
1576 "submitter": {
1577 "name": "User Name",
1578 },
1579 "refUpdate": {
1580 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1581 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1582 "refName": "master",
1583 "project": "org/project",
1584 }
1585 }
James E. Blairdaabed22012-08-15 15:38:57 -07001586 self.fake_gerrit.addEvent(e)
1587 self.waitUntilSettled()
1588
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001589 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001590 self.assertEqual(len(self.history), 1)
1591 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001592
1593 def test_build_configuration_branch(self):
1594 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001595
1596 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001597 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1598 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1599 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1600 A.addApproval('CRVW', 2)
1601 B.addApproval('CRVW', 2)
1602 C.addApproval('CRVW', 2)
1603 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1604 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1605 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1606 self.waitUntilSettled()
1607
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001608 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001609 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001610 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001611 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001612 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001613 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001614 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001615 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1616 self.gearman_server.hold_jobs_in_queue = False
1617 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001618 self.waitUntilSettled()
1619
Monty Taylorbc758832013-06-17 17:22:42 -04001620 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001621 repo = git.Repo(path)
1622 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1623 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001624 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001625 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001626
1627 def test_build_configuration_branch_interaction(self):
1628 "Test that switching between branches works"
1629 self.test_build_configuration()
1630 self.test_build_configuration_branch()
1631 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001632 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001633 repo = git.Repo(path)
1634 repo.heads.master.commit = repo.commit('init')
1635 self.test_build_configuration()
1636
1637 def test_build_configuration_multi_branch(self):
1638 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001639
1640 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001641 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1642 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1643 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1644 A.addApproval('CRVW', 2)
1645 B.addApproval('CRVW', 2)
1646 C.addApproval('CRVW', 2)
1647 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1648 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1649 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1650 self.waitUntilSettled()
1651
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001652 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001653 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001654 queue = self.gearman_server.getQueue()
James E. Blairf750aa02013-07-15 14:11:24 -07001655 ref_B = self.getParameter(queue[-1], 'ZUUL_REF')
1656 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001657 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001658 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001659 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001660 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001661 queue = self.gearman_server.getQueue()
James E. Blairf750aa02013-07-15 14:11:24 -07001662 ref_C = self.getParameter(queue[-1], 'ZUUL_REF')
1663 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001664 self.gearman_server.hold_jobs_in_queue = False
1665 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001666 self.waitUntilSettled()
1667
Monty Taylorbc758832013-06-17 17:22:42 -04001668 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001669 repo = git.Repo(path)
1670
1671 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001672 for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001673 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001674 correct_messages = ['initial commit', 'A-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001675 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001676
1677 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001678 for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001679 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001680 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001681 self.assertEqual(repo_messages, correct_messages)
James E. Blair7f71c802012-08-22 13:04:32 -07001682
1683 def test_one_job_project(self):
1684 "Test that queueing works with one job"
1685 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1686 'master', 'A')
1687 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1688 'master', 'B')
1689 A.addApproval('CRVW', 2)
1690 B.addApproval('CRVW', 2)
1691 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1692 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1693 self.waitUntilSettled()
1694
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001695 self.assertEqual(A.data['status'], 'MERGED')
1696 self.assertEqual(A.reported, 2)
1697 self.assertEqual(B.data['status'], 'MERGED')
1698 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001699
Antoine Musso80edd5a2013-02-13 15:37:53 +01001700 def test_job_from_templates_launched(self):
1701 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001702
Antoine Musso80edd5a2013-02-13 15:37:53 +01001703 A = self.fake_gerrit.addFakeChange(
1704 'org/templated-project', 'master', 'A')
1705 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1706 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001707
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001708 self.assertEqual(self.getJobFromHistory('project-test1').result,
1709 'SUCCESS')
1710 self.assertEqual(self.getJobFromHistory('project-test2').result,
1711 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001712
James E. Blaircaec0c52012-08-22 14:52:22 -07001713 def test_dependent_changes_dequeue(self):
1714 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001715
James E. Blaircaec0c52012-08-22 14:52:22 -07001716 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1717 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1718 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1719 A.addApproval('CRVW', 2)
1720 B.addApproval('CRVW', 2)
1721 C.addApproval('CRVW', 2)
1722
1723 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1724 M1.setMerged()
1725
1726 # C -> B -> A -> M1
1727
1728 C.setDependsOn(B, 1)
1729 B.setDependsOn(A, 1)
1730 A.setDependsOn(M1, 1)
1731
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001732 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001733
1734 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1735 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1736 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1737
1738 self.waitUntilSettled()
1739
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001740 self.assertEqual(A.data['status'], 'NEW')
1741 self.assertEqual(A.reported, 2)
1742 self.assertEqual(B.data['status'], 'NEW')
1743 self.assertEqual(B.reported, 2)
1744 self.assertEqual(C.data['status'], 'NEW')
1745 self.assertEqual(C.reported, 2)
1746 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001747
1748 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001749 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001750 # If it's dequeued more than once, we should see extra
1751 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001752
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001753 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001754 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1755 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1756 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1757 A.addApproval('CRVW', 2)
1758 B.addApproval('CRVW', 2)
1759 C.addApproval('CRVW', 2)
1760
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001761 self.worker.addFailTest('project1-test1', A)
1762 self.worker.addFailTest('project1-test2', A)
1763 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001764
1765 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1766 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1767 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1768
1769 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001770
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001771 self.assertEqual(len(self.builds), 1)
1772 self.assertEqual(self.builds[0].name, 'project1-merge')
1773 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07001774
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001775 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001776 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001777 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001778 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001779 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001780 self.waitUntilSettled()
1781
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001782 self.assertEqual(len(self.builds), 9)
1783 self.assertEqual(self.builds[0].name, 'project1-test1')
1784 self.assertEqual(self.builds[1].name, 'project1-test2')
1785 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
1786 self.assertEqual(self.builds[3].name, 'project1-test1')
1787 self.assertEqual(self.builds[4].name, 'project1-test2')
1788 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
1789 self.assertEqual(self.builds[6].name, 'project1-test1')
1790 self.assertEqual(self.builds[7].name, 'project1-test2')
1791 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001792
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001793 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001794 self.waitUntilSettled()
1795
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001796 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1797 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001798
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001799 self.worker.hold_jobs_in_build = False
1800 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001801 self.waitUntilSettled()
1802
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001803 self.assertEqual(len(self.builds), 0)
1804 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001805
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001806 self.assertEqual(A.data['status'], 'NEW')
1807 self.assertEqual(B.data['status'], 'MERGED')
1808 self.assertEqual(C.data['status'], 'MERGED')
1809 self.assertEqual(A.reported, 2)
1810 self.assertEqual(B.reported, 2)
1811 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001812
1813 def test_nonvoting_job(self):
1814 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001815
James E. Blair4ec821f2012-08-23 15:28:28 -07001816 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1817 'master', 'A')
1818 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001819 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001820 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1821
1822 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001823
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001824 self.assertEqual(A.data['status'], 'MERGED')
1825 self.assertEqual(A.reported, 2)
1826 self.assertEqual(
1827 self.getJobFromHistory('nonvoting-project-merge').result,
1828 'SUCCESS')
1829 self.assertEqual(
1830 self.getJobFromHistory('nonvoting-project-test1').result,
1831 'SUCCESS')
1832 self.assertEqual(
1833 self.getJobFromHistory('nonvoting-project-test2').result,
1834 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001835
1836 def test_check_queue_success(self):
1837 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001838
James E. Blaire0487072012-08-29 17:38:31 -07001839 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1840 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1841
1842 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001843
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001844 self.assertEqual(A.data['status'], 'NEW')
1845 self.assertEqual(A.reported, 1)
1846 self.assertEqual(self.getJobFromHistory('project-merge').result,
1847 'SUCCESS')
1848 self.assertEqual(self.getJobFromHistory('project-test1').result,
1849 'SUCCESS')
1850 self.assertEqual(self.getJobFromHistory('project-test2').result,
1851 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001852
1853 def test_check_queue_failure(self):
1854 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001855
James E. Blaire0487072012-08-29 17:38:31 -07001856 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001857 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001858 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1859
1860 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001861
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001862 self.assertEqual(A.data['status'], 'NEW')
1863 self.assertEqual(A.reported, 1)
1864 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001865 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001866 self.assertEqual(self.getJobFromHistory('project-test1').result,
1867 'SUCCESS')
1868 self.assertEqual(self.getJobFromHistory('project-test2').result,
1869 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001870
1871 def test_dependent_behind_dequeue(self):
1872 "test that dependent changes behind dequeued changes work"
1873 # This complicated test is a reproduction of a real life bug
1874 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001875
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001876 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001877 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1878 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1879 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1880 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1881 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1882 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1883 D.setDependsOn(C, 1)
1884 E.setDependsOn(D, 1)
1885 A.addApproval('CRVW', 2)
1886 B.addApproval('CRVW', 2)
1887 C.addApproval('CRVW', 2)
1888 D.addApproval('CRVW', 2)
1889 E.addApproval('CRVW', 2)
1890 F.addApproval('CRVW', 2)
1891
1892 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001893
1894 # Change object re-use in the gerrit trigger is hidden if
1895 # changes are added in quick succession; waiting makes it more
1896 # like real life.
1897 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1898 self.waitUntilSettled()
1899 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1900 self.waitUntilSettled()
1901
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001902 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001903 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001904 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001905 self.waitUntilSettled()
1906
1907 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1908 self.waitUntilSettled()
1909 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1910 self.waitUntilSettled()
1911 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1912 self.waitUntilSettled()
1913 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1914 self.waitUntilSettled()
1915
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001916 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001917 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001918 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001919 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001920 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001921 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001922 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001923 self.waitUntilSettled()
1924
1925 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001926
1927 # Grab pointers to the jobs we want to release before
1928 # releasing any, because list indexes may change as
1929 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001930 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001931 a.release()
1932 b.release()
1933 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001934 self.waitUntilSettled()
1935
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001936 self.worker.hold_jobs_in_build = False
1937 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07001938 self.waitUntilSettled()
1939
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001940 self.assertEqual(A.data['status'], 'NEW')
1941 self.assertEqual(B.data['status'], 'MERGED')
1942 self.assertEqual(C.data['status'], 'MERGED')
1943 self.assertEqual(D.data['status'], 'MERGED')
1944 self.assertEqual(E.data['status'], 'MERGED')
1945 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001946
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001947 self.assertEqual(A.reported, 2)
1948 self.assertEqual(B.reported, 2)
1949 self.assertEqual(C.reported, 2)
1950 self.assertEqual(D.reported, 2)
1951 self.assertEqual(E.reported, 2)
1952 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001953
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001954 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1955 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001956
1957 def test_merger_repack(self):
1958 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001959
James E. Blair05fed602012-09-07 12:45:24 -07001960 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1961 A.addApproval('CRVW', 2)
1962 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1963 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001964 self.assertEqual(self.getJobFromHistory('project-merge').result,
1965 'SUCCESS')
1966 self.assertEqual(self.getJobFromHistory('project-test1').result,
1967 'SUCCESS')
1968 self.assertEqual(self.getJobFromHistory('project-test2').result,
1969 'SUCCESS')
1970 self.assertEqual(A.data['status'], 'MERGED')
1971 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001972 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07001973 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001974
Monty Taylorbc758832013-06-17 17:22:42 -04001975 path = os.path.join(self.git_root, "org/project")
1976 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001977
1978 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1979 A.addApproval('CRVW', 2)
1980 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1981 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001982 self.assertEqual(self.getJobFromHistory('project-merge').result,
1983 'SUCCESS')
1984 self.assertEqual(self.getJobFromHistory('project-test1').result,
1985 'SUCCESS')
1986 self.assertEqual(self.getJobFromHistory('project-test2').result,
1987 'SUCCESS')
1988 self.assertEqual(A.data['status'], 'MERGED')
1989 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001990
James E. Blair4886f282012-11-15 09:27:33 -08001991 def test_merger_repack_large_change(self):
1992 "Test that the merger works with large changes after a repack"
1993 # https://bugs.launchpad.net/zuul/+bug/1078946
1994 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1995 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001996 path = os.path.join(self.upstream_root, "org/project1")
1997 print repack_repo(path)
1998 path = os.path.join(self.git_root, "org/project1")
1999 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002000
2001 A.addApproval('CRVW', 2)
2002 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2003 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002004 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2005 'SUCCESS')
2006 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2007 'SUCCESS')
2008 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2009 'SUCCESS')
2010 self.assertEqual(A.data['status'], 'MERGED')
2011 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002012
James E. Blair7ee88a22012-09-12 18:59:31 +02002013 def test_nonexistent_job(self):
2014 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002015 # Set to the state immediately after a restart
2016 self.resetGearmanServer()
2017 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002018
2019 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2020 A.addApproval('CRVW', 2)
2021 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2022 # There may be a thread about to report a lost change
2023 while A.reported < 2:
2024 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002025 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002026 self.assertFalse(job_names)
2027 self.assertEqual(A.data['status'], 'NEW')
2028 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002029 self.assertEmptyQueues()
2030
2031 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002032 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002033 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2034 A.addApproval('CRVW', 2)
2035 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2036 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002037 self.assertEqual(self.getJobFromHistory('project-merge').result,
2038 'SUCCESS')
2039 self.assertEqual(self.getJobFromHistory('project-test1').result,
2040 'SUCCESS')
2041 self.assertEqual(self.getJobFromHistory('project-test2').result,
2042 'SUCCESS')
2043 self.assertEqual(A.data['status'], 'MERGED')
2044 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002045
2046 def test_single_nonexistent_post_job(self):
2047 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002048 e = {
2049 "type": "ref-updated",
2050 "submitter": {
2051 "name": "User Name",
2052 },
2053 "refUpdate": {
2054 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2055 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2056 "refName": "master",
2057 "project": "org/project",
2058 }
2059 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002060 # Set to the state immediately after a restart
2061 self.resetGearmanServer()
2062 self.launcher.negative_function_cache_ttl = 0
2063
James E. Blairf62d4282012-12-31 17:01:50 -08002064 self.fake_gerrit.addEvent(e)
2065 self.waitUntilSettled()
2066
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002067 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002068
2069 def test_new_patchset_dequeues_old(self):
2070 "Test that a new patchset causes the old to be dequeued"
2071 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002072 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002073 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2074 M.setMerged()
2075
2076 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2077 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2078 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2079 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2080 A.addApproval('CRVW', 2)
2081 B.addApproval('CRVW', 2)
2082 C.addApproval('CRVW', 2)
2083 D.addApproval('CRVW', 2)
2084
2085 C.setDependsOn(B, 1)
2086 B.setDependsOn(A, 1)
2087 A.setDependsOn(M, 1)
2088
2089 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2090 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2091 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2092 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2093 self.waitUntilSettled()
2094
2095 B.addPatchset()
2096 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2097 self.waitUntilSettled()
2098
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002099 self.worker.hold_jobs_in_build = False
2100 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002101 self.waitUntilSettled()
2102
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002103 self.assertEqual(A.data['status'], 'MERGED')
2104 self.assertEqual(A.reported, 2)
2105 self.assertEqual(B.data['status'], 'NEW')
2106 self.assertEqual(B.reported, 2)
2107 self.assertEqual(C.data['status'], 'NEW')
2108 self.assertEqual(C.reported, 2)
2109 self.assertEqual(D.data['status'], 'MERGED')
2110 self.assertEqual(D.reported, 2)
2111 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002112
2113 def test_new_patchset_dequeues_old_on_head(self):
2114 "Test that a new patchset causes the old to be dequeued (at head)"
2115 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002116 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002117 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2118 M.setMerged()
2119 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2120 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2121 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2122 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2123 A.addApproval('CRVW', 2)
2124 B.addApproval('CRVW', 2)
2125 C.addApproval('CRVW', 2)
2126 D.addApproval('CRVW', 2)
2127
2128 C.setDependsOn(B, 1)
2129 B.setDependsOn(A, 1)
2130 A.setDependsOn(M, 1)
2131
2132 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2133 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2134 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2135 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2136 self.waitUntilSettled()
2137
2138 A.addPatchset()
2139 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2140 self.waitUntilSettled()
2141
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002142 self.worker.hold_jobs_in_build = False
2143 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002144 self.waitUntilSettled()
2145
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002146 self.assertEqual(A.data['status'], 'NEW')
2147 self.assertEqual(A.reported, 2)
2148 self.assertEqual(B.data['status'], 'NEW')
2149 self.assertEqual(B.reported, 2)
2150 self.assertEqual(C.data['status'], 'NEW')
2151 self.assertEqual(C.reported, 2)
2152 self.assertEqual(D.data['status'], 'MERGED')
2153 self.assertEqual(D.reported, 2)
2154 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002155
2156 def test_new_patchset_dequeues_old_without_dependents(self):
2157 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002158 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002159 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2160 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2161 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2162 A.addApproval('CRVW', 2)
2163 B.addApproval('CRVW', 2)
2164 C.addApproval('CRVW', 2)
2165
2166 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2167 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2168 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2169 self.waitUntilSettled()
2170
2171 B.addPatchset()
2172 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2173 self.waitUntilSettled()
2174
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002175 self.worker.hold_jobs_in_build = False
2176 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002177 self.waitUntilSettled()
2178
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002179 self.assertEqual(A.data['status'], 'MERGED')
2180 self.assertEqual(A.reported, 2)
2181 self.assertEqual(B.data['status'], 'NEW')
2182 self.assertEqual(B.reported, 2)
2183 self.assertEqual(C.data['status'], 'MERGED')
2184 self.assertEqual(C.reported, 2)
2185 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002186
2187 def test_new_patchset_dequeues_old_independent_queue(self):
2188 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002189 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002190 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2191 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2192 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2193 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2194 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2195 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2196 self.waitUntilSettled()
2197
2198 B.addPatchset()
2199 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2200 self.waitUntilSettled()
2201
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002202 self.worker.hold_jobs_in_build = False
2203 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002204 self.waitUntilSettled()
2205
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002206 self.assertEqual(A.data['status'], 'NEW')
2207 self.assertEqual(A.reported, 1)
2208 self.assertEqual(B.data['status'], 'NEW')
2209 self.assertEqual(B.reported, 1)
2210 self.assertEqual(C.data['status'], 'NEW')
2211 self.assertEqual(C.reported, 1)
2212 self.assertEqual(len(self.history), 10)
2213 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002214
2215 def test_zuul_refs(self):
2216 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002217 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002218 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2219 M1.setMerged()
2220 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2221 M2.setMerged()
2222
2223 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2224 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2225 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2226 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2227 A.addApproval('CRVW', 2)
2228 B.addApproval('CRVW', 2)
2229 C.addApproval('CRVW', 2)
2230 D.addApproval('CRVW', 2)
2231 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2232 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2233 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2234 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2235
2236 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002237 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002238 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002239 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002240 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002241 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002242 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002243 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002244 self.waitUntilSettled()
2245
James E. Blair7d0dedc2013-02-21 17:26:09 -08002246 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002247 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002248 if x.parameters['ZUUL_CHANGE'] == '3':
2249 a_zref = x.parameters['ZUUL_REF']
2250 if x.parameters['ZUUL_CHANGE'] == '4':
2251 b_zref = x.parameters['ZUUL_REF']
2252 if x.parameters['ZUUL_CHANGE'] == '5':
2253 c_zref = x.parameters['ZUUL_REF']
2254 if x.parameters['ZUUL_CHANGE'] == '6':
2255 d_zref = x.parameters['ZUUL_REF']
2256
2257 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002258 self.assertIsNotNone(a_zref)
2259 self.assertIsNotNone(b_zref)
2260 self.assertIsNotNone(c_zref)
2261 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002262
2263 # And they should all be different
2264 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002265 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002266
2267 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002268 self.assertTrue(self.ref_has_change(a_zref, A))
2269 self.assertFalse(self.ref_has_change(a_zref, B))
2270 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002271
2272 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002273 self.assertTrue(self.ref_has_change(b_zref, A))
2274 self.assertTrue(self.ref_has_change(b_zref, B))
2275 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002276
2277 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002278 self.assertTrue(self.ref_has_change(c_zref, A))
2279 self.assertTrue(self.ref_has_change(c_zref, B))
2280 self.assertTrue(self.ref_has_change(c_zref, C))
2281 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002282
2283 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002284 self.assertTrue(self.ref_has_change(d_zref, A))
2285 self.assertTrue(self.ref_has_change(d_zref, B))
2286 self.assertTrue(self.ref_has_change(d_zref, C))
2287 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002288
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002289 self.worker.hold_jobs_in_build = False
2290 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002291 self.waitUntilSettled()
2292
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002293 self.assertEqual(A.data['status'], 'MERGED')
2294 self.assertEqual(A.reported, 2)
2295 self.assertEqual(B.data['status'], 'MERGED')
2296 self.assertEqual(B.reported, 2)
2297 self.assertEqual(C.data['status'], 'MERGED')
2298 self.assertEqual(C.reported, 2)
2299 self.assertEqual(D.data['status'], 'MERGED')
2300 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002301
James E. Blair412e5582013-04-22 15:50:12 -07002302 def test_statsd(self):
2303 "Test each of the statsd methods used in the scheduler"
2304 import extras
2305 statsd = extras.try_import('statsd.statsd')
2306 statsd.incr('test-incr')
2307 statsd.timing('test-timing', 3)
2308 statsd.gauge('test-guage', 12)
2309 self.assertReportedStat('test-incr', '1|c')
2310 self.assertReportedStat('test-timing', '3|ms')
2311 self.assertReportedStat('test-guage', '12|g')
2312
James E. Blair70c71582013-03-06 08:50:50 -08002313 def test_file_jobs(self):
2314 "Test that file jobs run only when appropriate"
2315 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2316 A.addPatchset(['pip-requires'])
2317 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2318 A.addApproval('CRVW', 2)
2319 B.addApproval('CRVW', 2)
2320 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2321 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2322 self.waitUntilSettled()
2323
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002324 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002325 if x.name == 'project-testfile']
2326
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002327 self.assertEqual(len(testfile_jobs), 1)
2328 self.assertEqual(testfile_jobs[0].changes, '1,2')
2329 self.assertEqual(A.data['status'], 'MERGED')
2330 self.assertEqual(A.reported, 2)
2331 self.assertEqual(B.data['status'], 'MERGED')
2332 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002333
2334 def test_test_config(self):
2335 "Test that we can test the config"
2336 sched = zuul.scheduler.Scheduler()
2337 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002338
2339 def test_build_description(self):
2340 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002341 self.worker.registerFunction('set_description:' +
2342 self.worker.worker_id)
2343
2344 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2345 A.addApproval('CRVW', 2)
2346 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2347 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002348 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002349 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002350 self.assertTrue(re.search("Branch.*master", desc))
2351 self.assertTrue(re.search("Pipeline.*gate", desc))
2352 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2353 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2354 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2355 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002356
James E. Blair64ed6f22013-07-10 14:07:23 -07002357 def test_queue_precedence(self):
2358 "Test that queue precedence works"
2359
2360 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002361 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002362 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2363 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2364 A.addApproval('CRVW', 2)
2365 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2366
2367 self.waitUntilSettled()
2368 self.gearman_server.hold_jobs_in_queue = False
2369 self.gearman_server.release()
2370 self.waitUntilSettled()
2371
James E. Blair8de58bd2013-07-18 16:23:33 -07002372 # Run one build at a time to ensure non-race order:
2373 for x in range(6):
2374 self.release(self.builds[0])
2375 self.waitUntilSettled()
2376 self.worker.hold_jobs_in_build = False
2377 self.waitUntilSettled()
2378
James E. Blair64ed6f22013-07-10 14:07:23 -07002379 self.log.debug(self.history)
2380 self.assertEqual(self.history[0].pipeline, 'gate')
2381 self.assertEqual(self.history[1].pipeline, 'check')
2382 self.assertEqual(self.history[2].pipeline, 'gate')
2383 self.assertEqual(self.history[3].pipeline, 'gate')
2384 self.assertEqual(self.history[4].pipeline, 'check')
2385 self.assertEqual(self.history[5].pipeline, 'check')
2386
James E. Blair1843a552013-07-03 14:19:52 -07002387 def test_json_status(self):
2388 "Test that we can retrieve JSON status info"
2389 self.worker.hold_jobs_in_build = True
2390 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2391 A.addApproval('CRVW', 2)
2392 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2393 self.waitUntilSettled()
2394
2395 port = self.webapp.server.socket.getsockname()[1]
2396
2397 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2398 data = f.read()
2399
2400 self.worker.hold_jobs_in_build = False
2401 self.worker.release()
2402 self.waitUntilSettled()
2403
2404 data = json.loads(data)
2405 status_jobs = set()
2406 for p in data['pipelines']:
2407 for q in p['change_queues']:
2408 for head in q['heads']:
2409 for change in head:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002410 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002411 for job in change['jobs']:
2412 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002413 self.assertIn('project-merge', status_jobs)
2414 self.assertIn('project-test1', status_jobs)
2415 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002416
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002417 def test_node_label(self):
2418 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002419 self.worker.registerFunction('build:node-project-test1:debian')
2420
2421 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2422 A.addApproval('CRVW', 2)
2423 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2424 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002425
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002426 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2427 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2428 'debian')
2429 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002430
2431 def test_live_reconfiguration(self):
2432 "Test that live reconfiguration works"
2433 self.worker.hold_jobs_in_build = True
2434 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2435 A.addApproval('CRVW', 2)
2436 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2437 self.waitUntilSettled()
2438
2439 self.sched.reconfigure(self.config)
2440
2441 self.worker.hold_jobs_in_build = False
2442 self.worker.release()
2443 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002444 self.assertEqual(self.getJobFromHistory('project-merge').result,
2445 'SUCCESS')
2446 self.assertEqual(self.getJobFromHistory('project-test1').result,
2447 'SUCCESS')
2448 self.assertEqual(self.getJobFromHistory('project-test2').result,
2449 'SUCCESS')
2450 self.assertEqual(A.data['status'], 'MERGED')
2451 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002452
2453 def test_delayed_repo_init(self):
2454 self.config.set('zuul', 'layout_config',
2455 'tests/fixtures/layout-delayed-repo-init.yaml')
2456 self.sched.reconfigure(self.config)
2457
2458 self.init_repo("org/new-project")
2459 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2460
2461 A.addApproval('CRVW', 2)
2462 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2463 self.waitUntilSettled()
2464 self.assertEqual(self.getJobFromHistory('project-merge').result,
2465 'SUCCESS')
2466 self.assertEqual(self.getJobFromHistory('project-test1').result,
2467 'SUCCESS')
2468 self.assertEqual(self.getJobFromHistory('project-test2').result,
2469 'SUCCESS')
2470 self.assertEqual(A.data['status'], 'MERGED')
2471 self.assertEqual(A.reported, 2)