blob: 4de5d05991af0bc98bd2859c76c1bcf68970a217 [file] [log] [blame]
James E. Blairb0fcae42012-07-17 11:12:10 -07001#!/usr/bin/env python
2
3# Copyright 2012 Hewlett-Packard Development Company, L.P.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
James E. Blairb0fcae42012-07-17 11:12:10 -070017import ConfigParser
Monty Taylorbc758832013-06-17 17:22:42 -040018from cStringIO import StringIO
James E. Blair8cc15a82012-08-01 11:17:57 -070019import hashlib
James E. Blairb0fcae42012-07-17 11:12:10 -070020import json
Monty Taylorbc758832013-06-17 17:22:42 -040021import logging
22import os
James E. Blairb0fcae42012-07-17 11:12:10 -070023import pprint
Monty Taylorbc758832013-06-17 17:22:42 -040024import Queue
25import random
James E. Blairb0fcae42012-07-17 11:12:10 -070026import re
James E. Blair412e5582013-04-22 15:50:12 -070027import select
James E. Blair4886cc12012-07-18 15:39:41 -070028import shutil
James E. Blair412e5582013-04-22 15:50:12 -070029import socket
James E. Blair4886f282012-11-15 09:27:33 -080030import string
Monty Taylorbc758832013-06-17 17:22:42 -040031import subprocess
32import tempfile
33import threading
34import time
James E. Blair1843a552013-07-03 14:19:52 -070035import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040036import urllib2
37import urlparse
38
James E. Blair4886cc12012-07-18 15:39:41 -070039import git
James E. Blair1f4c2bb2013-04-26 08:40:46 -070040import gear
Monty Taylorbc758832013-06-17 17:22:42 -040041import fixtures
42import statsd
43import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070044
James E. Blairb0fcae42012-07-17 11:12:10 -070045import zuul.scheduler
James E. Blair1843a552013-07-03 14:19:52 -070046import zuul.webapp
James E. Blair1f4c2bb2013-04-26 08:40:46 -070047import zuul.launcher.gearman
James E. Blairb0fcae42012-07-17 11:12:10 -070048import zuul.trigger.gerrit
49
50FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
51 'fixtures')
52CONFIG = ConfigParser.ConfigParser()
53CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
54
55CONFIG.set('zuul', 'layout_config',
56 os.path.join(FIXTURE_DIR, "layout.yaml"))
57
James E. Blair1f4c2bb2013-04-26 08:40:46 -070058logging.basicConfig(level=logging.DEBUG,
59 format='%(asctime)s %(name)-32s '
60 '%(levelname)-8s %(message)s')
James E. Blairb0fcae42012-07-17 11:12:10 -070061
62
Monty Taylorbc758832013-06-17 17:22:42 -040063def repack_repo(path):
64 output = subprocess.Popen(
65 ['git', '--git-dir=%s/.git' % path, 'repack', '-afd'],
66 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
67 out = output.communicate()
68 if output.returncode:
69 raise Exception("git repack returned %d" % output.returncode)
70 return out
71
72
James E. Blair8cc15a82012-08-01 11:17:57 -070073def random_sha1():
74 return hashlib.sha1(str(random.random())).hexdigest()
75
76
James E. Blair4886cc12012-07-18 15:39:41 -070077class ChangeReference(git.Reference):
78 _common_path_default = "refs/changes"
79 _points_to_commits_only = True
80
81
James E. Blairb0fcae42012-07-17 11:12:10 -070082class FakeChange(object):
James E. Blair8c803f82012-07-31 16:25:42 -070083 categories = {'APRV': ('Approved', -1, 1),
84 'CRVW': ('Code-Review', -2, 2),
85 'VRFY': ('Verified', -2, 2)}
James E. Blairb0fcae42012-07-17 11:12:10 -070086
Monty Taylorbc758832013-06-17 17:22:42 -040087 def __init__(self, gerrit, number, project, branch, subject,
88 status='NEW', upstream_root=None):
James E. Blair8cc15a82012-08-01 11:17:57 -070089 self.gerrit = gerrit
James E. Blaird466dc42012-07-31 10:42:56 -070090 self.reported = 0
James E. Blair8c803f82012-07-31 16:25:42 -070091 self.queried = 0
James E. Blairb0fcae42012-07-17 11:12:10 -070092 self.patchsets = []
James E. Blairb0fcae42012-07-17 11:12:10 -070093 self.number = number
94 self.project = project
95 self.branch = branch
96 self.subject = subject
97 self.latest_patchset = 0
James E. Blair8c803f82012-07-31 16:25:42 -070098 self.depends_on_change = None
99 self.needed_by_changes = []
James E. Blair127bc182012-08-28 15:55:15 -0700100 self.fail_merge = False
James E. Blair42f74822013-05-14 15:18:03 -0700101 self.messages = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700102 self.data = {
103 'branch': branch,
104 'comments': [],
105 'commitMessage': subject,
106 'createdOn': time.time(),
James E. Blair8cc15a82012-08-01 11:17:57 -0700107 'id': 'I' + random_sha1(),
James E. Blairb0fcae42012-07-17 11:12:10 -0700108 'lastUpdated': time.time(),
109 'number': str(number),
110 'open': True,
111 'owner': {'email': 'user@example.com',
112 'name': 'User Name',
113 'username': 'username'},
114 'patchSets': self.patchsets,
115 'project': project,
116 'status': status,
117 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700118 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700119 'url': 'https://hostname/%s' % number}
120
Monty Taylorbc758832013-06-17 17:22:42 -0400121 self.upstream_root = upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700122 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700123 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700124
Monty Taylorbc758832013-06-17 17:22:42 -0400125 def add_fake_change_to_repo(self, msg, fn, large):
126 path = os.path.join(self.upstream_root, self.project)
127 repo = git.Repo(path)
128 ref = ChangeReference.create(repo, '1/%s/%s' % (self.number,
129 self.latest_patchset),
130 'refs/tags/init')
131 repo.head.reference = ref
132 repo.head.reset(index=True, working_tree=True)
133 repo.git.clean('-x', '-f', '-d')
134
135 path = os.path.join(self.upstream_root, self.project)
136 if not large:
137 fn = os.path.join(path, fn)
138 f = open(fn, 'w')
139 f.write("test %s %s %s\n" %
140 (self.branch, self.number, self.latest_patchset))
141 f.close()
142 repo.index.add([fn])
143 else:
144 for fni in range(100):
145 fn = os.path.join(path, str(fni))
146 f = open(fn, 'w')
147 for ci in range(4096):
148 f.write(random.choice(string.printable))
149 f.close()
150 repo.index.add([fn])
151
152 return repo.index.commit(msg)
153
James E. Blair70c71582013-03-06 08:50:50 -0800154 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700155 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700156 if files:
157 fn = files[0]
158 else:
159 fn = '%s-%s' % (self.branch, self.number)
160 msg = self.subject + '-' + str(self.latest_patchset)
Monty Taylorbc758832013-06-17 17:22:42 -0400161 c = self.add_fake_change_to_repo(msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800162 ps_files = [{'file': '/COMMIT_MSG',
163 'type': 'ADDED'},
164 {'file': 'README',
165 'type': 'MODIFIED'}]
166 for f in files:
167 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700168 d = {'approvals': [],
169 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800170 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700171 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700172 'ref': 'refs/changes/1/%s/%s' % (self.number,
173 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700174 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700175 'uploader': {'email': 'user@example.com',
176 'name': 'User name',
177 'username': 'user'}}
178 self.data['currentPatchSet'] = d
179 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700180 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700181
James E. Blaire0487072012-08-29 17:38:31 -0700182 def getPatchsetCreatedEvent(self, patchset):
183 event = {"type": "patchset-created",
184 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800185 "branch": self.branch,
186 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
187 "number": str(self.number),
188 "subject": self.subject,
189 "owner": {"name": "User Name"},
190 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700191 "patchSet": self.patchsets[patchset - 1],
192 "uploader": {"name": "User Name"}}
193 return event
194
James E. Blair42f74822013-05-14 15:18:03 -0700195 def getChangeRestoredEvent(self):
196 event = {"type": "change-restored",
197 "change": {"project": self.project,
198 "branch": self.branch,
199 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
200 "number": str(self.number),
201 "subject": self.subject,
202 "owner": {"name": "User Name"},
203 "url": "https://hostname/3"},
204 "restorer": {"name": "User Name"},
205 "reason": ""}
206 return event
207
James E. Blairb0fcae42012-07-17 11:12:10 -0700208 def addApproval(self, category, value):
James E. Blair8c803f82012-07-31 16:25:42 -0700209 approval = {'description': self.categories[category][0],
210 'type': category,
211 'value': str(value)}
212 self.patchsets[-1]['approvals'].append(approval)
213 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700214 'author': {'email': 'user@example.com',
215 'name': 'User Name',
216 'username': 'username'},
217 'change': {'branch': self.branch,
218 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
219 'number': str(self.number),
220 'owner': {'email': 'user@example.com',
221 'name': 'User Name',
222 'username': 'username'},
223 'project': self.project,
224 'subject': self.subject,
225 'topic': 'master',
226 'url': 'https://hostname/459'},
227 'comment': '',
228 'patchSet': self.patchsets[-1],
229 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700230 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700231 return json.loads(json.dumps(event))
232
James E. Blair8c803f82012-07-31 16:25:42 -0700233 def getSubmitRecords(self):
234 status = {}
235 for cat in self.categories.keys():
236 status[cat] = 0
237
238 for a in self.patchsets[-1]['approvals']:
239 cur = status[a['type']]
240 cat_min, cat_max = self.categories[a['type']][1:]
241 new = int(a['value'])
242 if new == cat_min:
243 cur = new
244 elif abs(new) > abs(cur):
245 cur = new
246 status[a['type']] = cur
247
248 labels = []
249 ok = True
250 for typ, cat in self.categories.items():
251 cur = status[typ]
252 cat_min, cat_max = cat[1:]
253 if cur == cat_min:
254 value = 'REJECT'
255 ok = False
256 elif cur == cat_max:
257 value = 'OK'
258 else:
259 value = 'NEED'
260 ok = False
261 labels.append({'label': cat[0], 'status': value})
262 if ok:
263 return [{'status': 'OK'}]
264 return [{'status': 'NOT_READY',
265 'labels': labels}]
266
267 def setDependsOn(self, other, patchset):
268 self.depends_on_change = other
269 d = {'id': other.data['id'],
270 'number': other.data['number'],
271 'ref': other.patchsets[patchset - 1]['ref']
272 }
273 self.data['dependsOn'] = [d]
274
275 other.needed_by_changes.append(self)
276 needed = other.data.get('neededBy', [])
277 d = {'id': self.data['id'],
278 'number': self.data['number'],
279 'ref': self.patchsets[patchset - 1]['ref'],
280 'revision': self.patchsets[patchset - 1]['revision']
281 }
282 needed.append(d)
283 other.data['neededBy'] = needed
284
James E. Blairb0fcae42012-07-17 11:12:10 -0700285 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700286 self.queried += 1
287 d = self.data.get('dependsOn')
288 if d:
289 d = d[0]
290 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
291 d['isCurrentPatchSet'] = True
292 else:
293 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700294 return json.loads(json.dumps(self.data))
295
296 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800297 if (self.depends_on_change and
298 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700299 return
James E. Blair127bc182012-08-28 15:55:15 -0700300 if self.fail_merge:
301 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700302 self.data['status'] = 'MERGED'
303 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700304
Monty Taylorbc758832013-06-17 17:22:42 -0400305 path = os.path.join(self.upstream_root, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700306 repo = git.Repo(path)
307 repo.heads[self.branch].commit = \
308 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700309
James E. Blaird466dc42012-07-31 10:42:56 -0700310 def setReported(self):
311 self.reported += 1
312
James E. Blairb0fcae42012-07-17 11:12:10 -0700313
314class FakeGerrit(object):
315 def __init__(self, *args, **kw):
316 self.event_queue = Queue.Queue()
317 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
318 self.change_number = 0
319 self.changes = {}
320
321 def addFakeChange(self, project, branch, subject):
322 self.change_number += 1
Monty Taylorbc758832013-06-17 17:22:42 -0400323 c = FakeChange(self, self.change_number, project, branch, subject,
324 upstream_root=self.upstream_root)
James E. Blairb0fcae42012-07-17 11:12:10 -0700325 self.changes[self.change_number] = c
326 return c
327
328 def addEvent(self, data):
329 return self.event_queue.put(data)
330
331 def getEvent(self):
332 return self.event_queue.get()
333
334 def eventDone(self):
335 self.event_queue.task_done()
336
337 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700338 number, ps = changeid.split(',')
339 change = self.changes[int(number)]
James E. Blair42f74822013-05-14 15:18:03 -0700340 change.messages.append(message)
James E. Blairb0fcae42012-07-17 11:12:10 -0700341 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700342 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700343 if message:
344 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700345
346 def query(self, number):
347 change = self.changes[int(number)]
348 return change.query()
349
350 def startWatching(self, *args, **kw):
351 pass
352
353
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700354class BuildHistory(object):
355 def __init__(self, **kw):
356 self.__dict__.update(kw)
James E. Blairb0fcae42012-07-17 11:12:10 -0700357
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700358 def __repr__(self):
359 return ("<Completed build, result: %s name: %s #%s changes: %s>" %
360 (self.result, self.name, self.number, self.changes))
James E. Blairb0fcae42012-07-17 11:12:10 -0700361
362
James E. Blair8cc15a82012-08-01 11:17:57 -0700363class FakeURLOpener(object):
Monty Taylorbc758832013-06-17 17:22:42 -0400364 def __init__(self, upstream_root, fake_gerrit, url):
365 self.upstream_root = upstream_root
James E. Blair8cc15a82012-08-01 11:17:57 -0700366 self.fake_gerrit = fake_gerrit
367 self.url = url
368
369 def read(self):
370 res = urlparse.urlparse(self.url)
371 path = res.path
372 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200373 ret = '001e# service=git-upload-pack\n'
374 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
375 'multi_ack thin-pack side-band side-band-64k ofs-delta '
376 'shallow no-progress include-tag multi_ack_detailed no-done\n')
Monty Taylorbc758832013-06-17 17:22:42 -0400377 path = os.path.join(self.upstream_root, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700378 repo = git.Repo(path)
379 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200380 r = ref.object.hexsha + ' ' + ref.path + '\n'
381 ret += '%04x%s' % (len(r) + 4, r)
382 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700383 return ret
384
385
James E. Blair4886cc12012-07-18 15:39:41 -0700386class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
Monty Taylorbc758832013-06-17 17:22:42 -0400387 def __init__(self, upstream_root, *args):
388 super(FakeGerritTrigger, self).__init__(*args)
389 self.upstream_root = upstream_root
390
James E. Blair4886cc12012-07-18 15:39:41 -0700391 def getGitUrl(self, project):
Monty Taylorbc758832013-06-17 17:22:42 -0400392 return os.path.join(self.upstream_root, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700393
394
James E. Blair412e5582013-04-22 15:50:12 -0700395class FakeStatsd(threading.Thread):
396 def __init__(self):
397 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400398 self.daemon = True
James E. Blair412e5582013-04-22 15:50:12 -0700399 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
400 self.sock.bind(('', 0))
401 self.port = self.sock.getsockname()[1]
402 self.wake_read, self.wake_write = os.pipe()
403 self.stats = []
404
405 def run(self):
406 while True:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700407 poll = select.poll()
408 poll.register(self.sock, select.POLLIN)
409 poll.register(self.wake_read, select.POLLIN)
410 ret = poll.poll()
411 for (fd, event) in ret:
412 if fd == self.sock.fileno():
James E. Blair412e5582013-04-22 15:50:12 -0700413 data = self.sock.recvfrom(1024)
414 if not data:
415 return
416 self.stats.append(data[0])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700417 if fd == self.wake_read:
James E. Blair412e5582013-04-22 15:50:12 -0700418 return
419
420 def stop(self):
421 os.write(self.wake_write, '1\n')
422
423
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700424class FakeBuild(threading.Thread):
425 log = logging.getLogger("zuul.test")
426
427 def __init__(self, worker, job, number, node):
428 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400429 self.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700430 self.worker = worker
431 self.job = job
432 self.name = job.name.split(':')[1]
433 self.number = number
434 self.node = node
435 self.parameters = json.loads(job.arguments)
436 self.unique = self.parameters['ZUUL_UUID']
437 self.wait_condition = threading.Condition()
438 self.waiting = False
439 self.aborted = False
440 self.created = time.time()
441 self.description = ''
442
443 def release(self):
444 self.wait_condition.acquire()
445 self.wait_condition.notify()
446 self.waiting = False
447 self.log.debug("Build %s released" % self.unique)
448 self.wait_condition.release()
449
450 def isWaiting(self):
451 self.wait_condition.acquire()
452 if self.waiting:
453 ret = True
454 else:
455 ret = False
456 self.wait_condition.release()
457 return ret
458
459 def _wait(self):
460 self.wait_condition.acquire()
461 self.waiting = True
462 self.log.debug("Build %s waiting" % self.unique)
463 self.wait_condition.wait()
464 self.wait_condition.release()
465
466 def run(self):
467 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700468 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
469 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700470 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700471 'manager': self.worker.worker_id,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700472 }
473
474 self.job.sendWorkData(json.dumps(data))
475 self.job.sendWorkStatus(0, 100)
476
477 if self.worker.hold_jobs_in_build:
478 self._wait()
479 self.log.debug("Build %s continuing" % self.unique)
480
481 self.worker.lock.acquire()
482
483 result = 'SUCCESS'
484 if (('ZUUL_REF' in self.parameters) and
485 self.worker.shouldFailTest(self.name,
486 self.parameters['ZUUL_REF'])):
487 result = 'FAILURE'
488 if self.aborted:
489 result = 'ABORTED'
490
491 data = {'result': result}
492 changes = None
493 if 'ZUUL_CHANGE_IDS' in self.parameters:
494 changes = self.parameters['ZUUL_CHANGE_IDS']
495
496 self.worker.build_history.append(
497 BuildHistory(name=self.name, number=self.number,
498 result=result, changes=changes, node=self.node,
499 uuid=self.unique, description=self.description)
500 )
501
502 self.job.sendWorkComplete(json.dumps(data))
503 del self.worker.gearman_jobs[self.job.unique]
504 self.worker.running_builds.remove(self)
505 self.worker.lock.release()
506
507
508class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400509 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700510 super(FakeWorker, self).__init__(worker_id)
511 self.gearman_jobs = {}
512 self.build_history = []
513 self.running_builds = []
514 self.build_counter = 0
515 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400516 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700517
518 self.hold_jobs_in_build = False
519 self.lock = threading.Lock()
520 self.__work_thread = threading.Thread(target=self.work)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400521 self.__work_thread.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700522 self.__work_thread.start()
523
524 def handleJob(self, job):
525 parts = job.name.split(":")
526 cmd = parts[0]
527 name = parts[1]
528 if len(parts) > 2:
529 node = parts[2]
530 else:
531 node = None
532 if cmd == 'build':
533 self.handleBuild(job, name, node)
534 elif cmd == 'stop':
535 self.handleStop(job, name)
536 elif cmd == 'set_description':
537 self.handleSetDescription(job, name)
538
539 def handleBuild(self, job, name, node):
540 build = FakeBuild(self, job, self.build_counter, node)
541 job.build = build
542 self.gearman_jobs[job.unique] = job
543 self.build_counter += 1
544
545 self.running_builds.append(build)
546 build.start()
547
548 def handleStop(self, job, name):
549 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700550 parameters = json.loads(job.arguments)
551 name = parameters['name']
552 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700553 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700554 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700555 build.aborted = True
556 build.release()
557 job.sendWorkComplete()
558 return
559 job.sendWorkFail()
560
561 def handleSetDescription(self, job, name):
562 self.log.debug("handle set description")
563 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700564 name = parameters['name']
565 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700566 descr = parameters['html_description']
567 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700568 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700569 build.description = descr
570 job.sendWorkComplete()
571 return
572 for build in self.build_history:
James E. Blair3c483cf2013-06-04 16:30:43 -0700573 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700574 build.description = descr
575 job.sendWorkComplete()
576 return
577 job.sendWorkFail()
578
579 def work(self):
580 while self.running:
581 try:
582 job = self.getJob()
583 except gear.InterruptedError:
584 continue
585 try:
586 self.handleJob(job)
587 except:
588 self.log.exception("Worker exception:")
589
590 def addFailTest(self, name, change):
591 l = self.fail_tests.get(name, [])
592 l.append(change)
593 self.fail_tests[name] = l
594
595 def shouldFailTest(self, name, ref):
596 l = self.fail_tests.get(name, [])
597 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400598 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700599 return True
600 return False
601
602 def release(self, regex=None):
603 builds = self.running_builds[:]
604 self.log.debug("releasing build %s (%s)" % (regex,
605 len(self.running_builds)))
606 for build in builds:
607 if not regex or re.match(regex, build.name):
608 self.log.debug("releasing build %s" %
609 (build.parameters['ZUUL_UUID']))
610 build.release()
611 else:
612 self.log.debug("not releasing build %s" %
613 (build.parameters['ZUUL_UUID']))
614 self.log.debug("done releasing builds %s (%s)" %
615 (regex, len(self.running_builds)))
616
617
618class FakeGearmanServer(gear.Server):
619 def __init__(self):
620 self.hold_jobs_in_queue = False
621 super(FakeGearmanServer, self).__init__(0)
622
623 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700624 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
625 for job in queue:
626 if not hasattr(job, 'waiting'):
627 if job.name.startswith('build:'):
628 job.waiting = self.hold_jobs_in_queue
629 else:
630 job.waiting = False
631 if job.waiting:
632 continue
633 if job.name in connection.functions:
634 if not peek:
635 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400636 connection.related_jobs[job.handle] = job
637 job.worker_connection = connection
638 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700639 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700640 return None
641
642 def release(self, regex=None):
643 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700644 qlen = (len(self.high_queue) + len(self.normal_queue) +
645 len(self.low_queue))
646 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
647 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
648 queue = queue[:]
649 for job in queue:
650 cmd, name = job.name.split(':')
651 if cmd != 'build':
652 continue
653 if not regex or re.match(regex, name):
654 self.log.debug("releasing queued job %s" %
655 job.unique)
656 job.waiting = False
657 released = True
658 else:
659 self.log.debug("not releasing queued job %s" %
660 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700661 if released:
662 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700663 qlen = (len(self.high_queue) + len(self.normal_queue) +
664 len(self.low_queue))
665 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700666
667
Monty Taylorbc758832013-06-17 17:22:42 -0400668class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700669 log = logging.getLogger("zuul.test")
670
671 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400672 super(TestScheduler, self).setUp()
673 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
674 try:
675 test_timeout = int(test_timeout)
676 except ValueError:
677 # If timeout value is invalid do not set a timeout.
678 test_timeout = 0
679 if test_timeout > 0:
680 self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
681
682 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
683 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
684 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
685 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
686 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
687 os.environ.get('OS_STDERR_CAPTURE') == '1'):
688 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
689 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
690 self.useFixture(fixtures.NestedTempfile())
Monty Taylore6a6c402013-07-02 09:25:55 -0700691 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
692 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400693 self.useFixture(fixtures.FakeLogger(
694 level=logging.DEBUG,
695 format='%(asctime)s %(name)-32s '
696 '%(levelname)-8s %(message)s'))
697 tmp_root = tempfile.mkdtemp(dir=os.environ.get("ZUUL_TEST_ROOT",
698 '/tmp'))
Monty Taylorbc758832013-06-17 17:22:42 -0400699 self.test_root = os.path.join(tmp_root, "zuul-test")
700 self.upstream_root = os.path.join(self.test_root, "upstream")
701 self.git_root = os.path.join(self.test_root, "git")
702
703 CONFIG.set('zuul', 'git_dir', self.git_root)
704 if os.path.exists(self.test_root):
705 shutil.rmtree(self.test_root)
706 os.makedirs(self.test_root)
707 os.makedirs(self.upstream_root)
708 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700709
710 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400711 self.init_repo("org/project")
712 self.init_repo("org/project1")
713 self.init_repo("org/project2")
714 self.init_repo("org/project3")
715 self.init_repo("org/one-job-project")
716 self.init_repo("org/nonvoting-project")
717 self.init_repo("org/templated-project")
718 self.init_repo("org/node-project")
James E. Blair412e5582013-04-22 15:50:12 -0700719
720 self.statsd = FakeStatsd()
721 os.environ['STATSD_HOST'] = 'localhost'
722 os.environ['STATSD_PORT'] = str(self.statsd.port)
723 self.statsd.start()
724 # the statsd client object is configured in the statsd module import
725 reload(statsd)
726 reload(zuul.scheduler)
727
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700728 self.gearman_server = FakeGearmanServer()
729
730 self.config = ConfigParser.ConfigParser()
731 cfg = StringIO()
732 CONFIG.write(cfg)
733 cfg.seek(0)
734 self.config.readfp(cfg)
735 self.config.set('gearman', 'port', str(self.gearman_server.port))
736
Monty Taylorbc758832013-06-17 17:22:42 -0400737 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700738 self.worker.addServer('127.0.0.1', self.gearman_server.port)
739 self.gearman_server.worker = self.worker
740
James E. Blairb0fcae42012-07-17 11:12:10 -0700741 self.sched = zuul.scheduler.Scheduler()
742
James E. Blair8cc15a82012-08-01 11:17:57 -0700743 def URLOpenerFactory(*args, **kw):
744 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400745 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700746
James E. Blair8cc15a82012-08-01 11:17:57 -0700747 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700748 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700749
750 zuul.lib.gerrit.Gerrit = FakeGerrit
751
Monty Taylorbc758832013-06-17 17:22:42 -0400752 self.gerrit = FakeGerritTrigger(
753 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700754 self.gerrit.replication_timeout = 1.5
755 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700756 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400757 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700758
James E. Blair1843a552013-07-03 14:19:52 -0700759 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
760
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700761 self.sched.setLauncher(self.launcher)
James E. Blairb0fcae42012-07-17 11:12:10 -0700762 self.sched.setTrigger(self.gerrit)
763
764 self.sched.start()
765 self.sched.reconfigure(self.config)
766 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700767 self.webapp.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700768 self.launcher.gearman.waitForServer()
769 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400770 self.builds = self.worker.running_builds
771 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700772
James E. Blairfee8d652013-06-07 08:57:52 -0700773 self.addCleanup(self.assertFinalState)
774 self.addCleanup(self.shutdown)
775
776 def assertFinalState(self):
777 # Make sure that the change cache is cleared
778 assert len(self.sched.trigger._change_cache.keys()) == 0
779 self.assertEmptyQueues()
780
781 def shutdown(self):
782 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700783 self.launcher.stop()
784 self.worker.shutdown()
785 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700786 self.gerrit.stop()
787 self.sched.stop()
788 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700789 self.statsd.stop()
790 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700791 self.webapp.stop()
792 self.webapp.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700793 threads = threading.enumerate()
794 if len(threads) > 1:
795 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400796 super(TestScheduler, self).tearDown()
797
798 def init_repo(self, project):
799 parts = project.split('/')
800 path = os.path.join(self.upstream_root, *parts[:-1])
801 if not os.path.exists(path):
802 os.makedirs(path)
803 path = os.path.join(self.upstream_root, project)
804 repo = git.Repo.init(path)
805
806 repo.config_writer().set_value('user', 'email', 'user@example.com')
807 repo.config_writer().set_value('user', 'name', 'User Name')
808 repo.config_writer().write()
809
810 fn = os.path.join(path, 'README')
811 f = open(fn, 'w')
812 f.write("test\n")
813 f.close()
814 repo.index.add([fn])
815 repo.index.commit('initial commit')
816 master = repo.create_head('master')
817 repo.create_tag('init')
818
819 mp = repo.create_head('mp')
820 repo.head.reference = mp
821 f = open(fn, 'a')
822 f.write("test mp\n")
823 f.close()
824 repo.index.add([fn])
825 repo.index.commit('mp commit')
826
827 repo.head.reference = master
828 repo.head.reset(index=True, working_tree=True)
829 repo.git.clean('-x', '-f', '-d')
830
831 def ref_has_change(self, ref, change):
832 path = os.path.join(self.git_root, change.project)
833 repo = git.Repo(path)
834 for commit in repo.iter_commits(ref):
835 if commit.message.strip() == ('%s-1' % change.subject):
836 return True
837 return False
838
839 def job_has_changes(self, *args):
840 job = args[0]
841 commits = args[1:]
842 if isinstance(job, FakeBuild):
843 parameters = job.parameters
844 else:
845 parameters = json.loads(job.arguments)
846 project = parameters['ZUUL_PROJECT']
847 path = os.path.join(self.git_root, project)
848 repo = git.Repo(path)
849 ref = parameters['ZUUL_REF']
850 sha = parameters['ZUUL_COMMIT']
851 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
852 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
853 commit_messages = ['%s-1' % commit.subject for commit in commits]
854 for msg in commit_messages:
855 if msg not in repo_messages:
856 return False
857 if repo_shas[0] != sha:
858 return False
859 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700860
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700861 def registerJobs(self):
862 count = 0
863 for job in self.sched.jobs.keys():
864 self.worker.registerFunction('build:' + job)
865 count += 1
866 self.worker.registerFunction('stop:' + self.worker.worker_id)
867 count += 1
868
869 while len(self.gearman_server.functions) < count:
870 time.sleep(0)
871
872 def release(self, job):
873 if isinstance(job, FakeBuild):
874 job.release()
875 else:
876 job.waiting = False
877 self.log.debug("Queued job %s released" % job.unique)
878 self.gearman_server.wakeConnections()
879
880 def getParameter(self, job, name):
881 if isinstance(job, FakeBuild):
882 return job.parameters[name]
883 else:
884 parameters = json.loads(job.arguments)
885 return parameters[name]
886
887 def resetGearmanServer(self):
888 self.worker.setFunctions([])
889 while True:
890 done = True
891 for connection in self.gearman_server.active_connections:
892 if connection.functions:
893 done = False
894 if done:
895 break
896 time.sleep(0)
897 self.gearman_server.functions = set()
898
899 def haveAllBuildsReported(self):
900 # See if Zuul is waiting on a meta job to complete
901 if self.launcher.meta_jobs:
902 return False
903 # Find out if every build that the worker has completed has been
904 # reported back to Zuul. If it hasn't then that means a Gearman
905 # event is still in transit and the system is not stable.
906 for build in self.worker.build_history:
907 zbuild = self.launcher.builds.get(build.uuid)
908 if not zbuild:
909 # It has already been reported
910 continue
911 # It hasn't been reported yet.
912 return False
913 # Make sure that none of the worker connections are in GRAB_WAIT
914 for connection in self.worker.active_connections:
915 if connection.state == 'GRAB_WAIT':
916 return False
917 return True
918
919 def areAllBuildsWaiting(self):
920 ret = True
921
922 builds = self.launcher.builds.values()
923 for build in builds:
924 client_job = None
925 for conn in self.launcher.gearman.active_connections:
926 for j in conn.related_jobs.values():
927 if j.unique == build.uuid:
928 client_job = j
929 break
930 if not client_job:
931 self.log.debug("%s is not known to the gearman client" %
932 build)
933 ret = False
934 continue
935 if not client_job.handle:
936 self.log.debug("%s has no handle" % client_job)
937 ret = False
938 continue
939 server_job = self.gearman_server.jobs.get(client_job.handle)
940 if not server_job:
941 self.log.debug("%s is not known to the gearman server" %
942 client_job)
943 ret = False
944 continue
945 if not hasattr(server_job, 'waiting'):
946 self.log.debug("%s is being enqueued" % server_job)
947 ret = False
948 continue
949 if server_job.waiting:
950 continue
951 worker_job = self.worker.gearman_jobs.get(server_job.unique)
952 if worker_job:
953 if worker_job.build.isWaiting():
954 continue
955 else:
956 self.log.debug("%s is running" % worker_job)
957 ret = False
958 else:
959 self.log.debug("%s is unassigned" % server_job)
960 ret = False
961 return ret
962
James E. Blairb0fcae42012-07-17 11:12:10 -0700963 def waitUntilSettled(self):
964 self.log.debug("Waiting until settled...")
965 start = time.time()
966 while True:
967 if time.time() - start > 10:
968 print 'queue status:',
969 print self.sched.trigger_event_queue.empty(),
970 print self.sched.result_event_queue.empty(),
971 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700972 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -0700973 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700974 # Make sure no new events show up while we're checking
975 self.worker.lock.acquire()
976 # have all build states propogated to zuul?
977 if self.haveAllBuildsReported():
978 # Join ensures that the queue is empty _and_ events have been
979 # processed
980 self.fake_gerrit.event_queue.join()
981 self.sched.trigger_event_queue.join()
982 self.sched.result_event_queue.join()
983 if (self.sched.trigger_event_queue.empty() and
984 self.sched.result_event_queue.empty() and
985 self.fake_gerrit.event_queue.empty() and
986 self.areAllBuildsWaiting()):
987 self.worker.lock.release()
988 self.log.debug("...settled.")
989 return
990 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700991 self.sched.wake_event.wait(0.1)
992
James E. Blaird466dc42012-07-31 10:42:56 -0700993 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -0800994 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -0700995 return len(jobs)
996
James E. Blair4ca985f2013-05-30 12:27:43 -0700997 def getJobFromHistory(self, name):
998 history = self.worker.build_history
999 for job in history:
1000 if job.name == name:
1001 return job
1002 raise Exception("Unable to find job %s in history" % name)
1003
James E. Blaire0487072012-08-29 17:38:31 -07001004 def assertEmptyQueues(self):
1005 # Make sure there are no orphaned jobs
1006 for pipeline in self.sched.pipelines.values():
1007 for queue in pipeline.queues:
1008 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001009 print 'pipeline %s queue %s contents %s' % (
1010 pipeline.name, queue.name, queue.queue)
James E. Blaire0487072012-08-29 17:38:31 -07001011 assert len(queue.queue) == 0
1012 if len(queue.severed_heads) != 0:
1013 print 'heads', queue.severed_heads
1014 assert len(queue.severed_heads) == 0
1015
James E. Blair412e5582013-04-22 15:50:12 -07001016 def assertReportedStat(self, key, value=None):
1017 start = time.time()
1018 while time.time() < (start + 5):
1019 for stat in self.statsd.stats:
1020 k, v = stat.split(':')
1021 if key == k:
1022 if value is None:
1023 return
1024 if value == v:
1025 return
1026 time.sleep(0.1)
1027
1028 pprint.pprint(self.statsd.stats)
1029 raise Exception("Key %s not found in reported stats" % key)
1030
James E. Blairb0fcae42012-07-17 11:12:10 -07001031 def test_jobs_launched(self):
1032 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001033
James E. Blairb0fcae42012-07-17 11:12:10 -07001034 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001035 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001036 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1037 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001038 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1039 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1040 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blairb0fcae42012-07-17 11:12:10 -07001041 assert A.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -07001042 assert A.reported == 2
James E. Blairb0fcae42012-07-17 11:12:10 -07001043
James E. Blair412e5582013-04-22 15:50:12 -07001044 self.assertReportedStat('gerrit.event.comment-added', '1|c')
1045 self.assertReportedStat('zuul.pipeline.gate.current_changes', '1|g')
1046 self.assertReportedStat('zuul.job.project-merge')
1047 self.assertReportedStat('zuul.pipeline.gate.resident_time')
1048 self.assertReportedStat('zuul.pipeline.gate.total_changes', '1|c')
1049 self.assertReportedStat(
1050 'zuul.pipeline.gate.org.project.resident_time')
1051 self.assertReportedStat(
1052 'zuul.pipeline.gate.org.project.total_changes', '1|c')
1053
James E. Blair42f74822013-05-14 15:18:03 -07001054 def test_duplicate_pipelines(self):
1055 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001056
James E. Blair42f74822013-05-14 15:18:03 -07001057 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1058 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1059 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001060
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001061 assert len(self.history) == 2
1062 self.history[0].name == 'project-test1'
1063 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001064
1065 assert len(A.messages) == 2
1066 if 'dup1/project-test1' in A.messages[0]:
1067 assert 'dup1/project-test1' in A.messages[0]
1068 assert 'dup2/project-test1' not in A.messages[0]
1069 assert 'dup1/project-test1' not in A.messages[1]
1070 assert 'dup2/project-test1' in A.messages[1]
1071 else:
1072 assert 'dup1/project-test1' in A.messages[1]
1073 assert 'dup2/project-test1' not in A.messages[1]
1074 assert 'dup1/project-test1' not in A.messages[0]
1075 assert 'dup2/project-test1' in A.messages[0]
1076
James E. Blairb0fcae42012-07-17 11:12:10 -07001077 def test_parallel_changes(self):
1078 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001079
1080 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001081 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1082 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1083 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001084 A.addApproval('CRVW', 2)
1085 B.addApproval('CRVW', 2)
1086 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001087
1088 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1089 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1090 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1091
1092 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001093 assert len(self.builds) == 1
1094 assert self.builds[0].name == 'project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001095 assert self.job_has_changes(self.builds[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -07001096
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001097 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001098 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001099 assert len(self.builds) == 3
1100 assert self.builds[0].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001101 assert self.job_has_changes(self.builds[0], A)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001102 assert self.builds[1].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001103 assert self.job_has_changes(self.builds[1], A)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001104 assert self.builds[2].name == 'project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001105 assert self.job_has_changes(self.builds[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -07001106
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001107 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001108 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001109 assert len(self.builds) == 5
1110 assert self.builds[0].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001111 assert self.job_has_changes(self.builds[0], A)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001112 assert self.builds[1].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001113 assert self.job_has_changes(self.builds[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -07001114
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001115 assert self.builds[2].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001116 assert self.job_has_changes(self.builds[2], A, B)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001117 assert self.builds[3].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001118 assert self.job_has_changes(self.builds[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -07001119
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001120 assert self.builds[4].name == 'project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001121 assert self.job_has_changes(self.builds[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -07001122
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001123 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001124 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001125 assert len(self.builds) == 6
1126 assert self.builds[0].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001127 assert self.job_has_changes(self.builds[0], A)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001128 assert self.builds[1].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001129 assert self.job_has_changes(self.builds[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -07001130
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001131 assert self.builds[2].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001132 assert self.job_has_changes(self.builds[2], A, B)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001133 assert self.builds[3].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001134 assert self.job_has_changes(self.builds[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -07001135
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001136 assert self.builds[4].name == 'project-test1'
Monty Taylorbc758832013-06-17 17:22:42 -04001137 assert self.job_has_changes(self.builds[4], A, B, C)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001138 assert self.builds[5].name == 'project-test2'
Monty Taylorbc758832013-06-17 17:22:42 -04001139 assert self.job_has_changes(self.builds[5], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -07001140
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001141 self.worker.hold_jobs_in_build = False
1142 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001143 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001144 assert len(self.builds) == 0
James E. Blairb0fcae42012-07-17 11:12:10 -07001145
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001146 assert len(self.history) == 9
James E. Blairb0fcae42012-07-17 11:12:10 -07001147 assert A.data['status'] == 'MERGED'
1148 assert B.data['status'] == 'MERGED'
1149 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -07001150 assert A.reported == 2
1151 assert B.reported == 2
1152 assert C.reported == 2
James E. Blairb02a3bb2012-07-30 17:49:55 -07001153
1154 def test_failed_changes(self):
1155 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001156 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001157
James E. Blairb02a3bb2012-07-30 17:49:55 -07001158 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1159 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001160 A.addApproval('CRVW', 2)
1161 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001162
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001163 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001164
James E. Blaire2819012013-06-28 17:17:26 -04001165 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1166 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001167 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001168
1169 self.worker.release('.*-merge')
1170 self.waitUntilSettled()
1171
1172 self.worker.hold_jobs_in_build = False
1173 self.worker.release()
1174
1175 self.waitUntilSettled()
1176 # It's certain that the merge job for change 2 will run, but
1177 # the test1 and test2 jobs may or may not run.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001178 assert len(self.history) > 6
James E. Blairb02a3bb2012-07-30 17:49:55 -07001179 assert A.data['status'] == 'NEW'
1180 assert B.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -07001181 assert A.reported == 2
1182 assert B.reported == 2
James E. Blairb02a3bb2012-07-30 17:49:55 -07001183
1184 def test_independent_queues(self):
1185 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001186
1187 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001188 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001189 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1190 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001191 A.addApproval('CRVW', 2)
1192 B.addApproval('CRVW', 2)
1193 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001194
1195 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1196 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1197 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1198
James E. Blairb02a3bb2012-07-30 17:49:55 -07001199 self.waitUntilSettled()
1200
1201 # There should be one merge job at the head of each queue running
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001202 assert len(self.builds) == 2
1203 assert self.builds[0].name == 'project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001204 assert self.job_has_changes(self.builds[0], A)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001205 assert self.builds[1].name == 'project1-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001206 assert self.job_has_changes(self.builds[1], B)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001207
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001208 # Release the current merge builds
1209 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001210 self.waitUntilSettled()
1211 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001212 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001213 self.waitUntilSettled()
1214
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001215 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001216 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001217 assert len(self.builds) == 8
James E. Blairb02a3bb2012-07-30 17:49:55 -07001218
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001219 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001220 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001221 assert len(self.builds) == 0
James E. Blairb02a3bb2012-07-30 17:49:55 -07001222
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001223 assert len(self.history) == 11
James E. Blairb02a3bb2012-07-30 17:49:55 -07001224 assert A.data['status'] == 'MERGED'
1225 assert B.data['status'] == 'MERGED'
1226 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -07001227 assert A.reported == 2
1228 assert B.reported == 2
1229 assert C.reported == 2
1230
1231 def test_failed_change_at_head(self):
1232 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001233
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001234 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001235 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1236 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1237 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001238 A.addApproval('CRVW', 2)
1239 B.addApproval('CRVW', 2)
1240 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001241
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001242 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001243
1244 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1245 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1246 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1247
1248 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001249
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001250 assert len(self.builds) == 1
1251 assert self.builds[0].name == 'project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001252 assert self.job_has_changes(self.builds[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -07001253
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001254 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001255 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001256 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001257 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001258 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001259 self.waitUntilSettled()
1260
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001261 assert len(self.builds) == 6
1262 assert self.builds[0].name == 'project-test1'
1263 assert self.builds[1].name == 'project-test2'
1264 assert self.builds[2].name == 'project-test1'
1265 assert self.builds[3].name == 'project-test2'
1266 assert self.builds[4].name == 'project-test1'
1267 assert self.builds[5].name == 'project-test2'
James E. Blaird466dc42012-07-31 10:42:56 -07001268
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001269 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001270 self.waitUntilSettled()
1271
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001272 assert len(self.builds) == 2 # project-test2, project-merge for B
1273 assert self.countJobResults(self.history, 'ABORTED') == 4
James E. Blaird466dc42012-07-31 10:42:56 -07001274
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001275 self.worker.hold_jobs_in_build = False
1276 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001277 self.waitUntilSettled()
1278
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001279 assert len(self.builds) == 0
1280 assert len(self.history) == 15
James E. Blaird466dc42012-07-31 10:42:56 -07001281 assert A.data['status'] == 'NEW'
1282 assert B.data['status'] == 'MERGED'
1283 assert C.data['status'] == 'MERGED'
1284 assert A.reported == 2
1285 assert B.reported == 2
1286 assert C.reported == 2
1287
1288 def test_failed_change_at_head_with_queue(self):
1289 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001290
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001291 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001292 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1293 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1294 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001295 A.addApproval('CRVW', 2)
1296 B.addApproval('CRVW', 2)
1297 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001298
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001299 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001300
1301 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1302 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1303 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1304
1305 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001306 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001307 assert len(self.builds) == 0
James E. Blaird466dc42012-07-31 10:42:56 -07001308 assert len(queue) == 1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001309 assert queue[0].name == 'build:project-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001310 assert self.job_has_changes(queue[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -07001311
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001312 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001313 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001314 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001315 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001316 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001317 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001318 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001319
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001320 assert len(self.builds) == 0
James E. Blaird466dc42012-07-31 10:42:56 -07001321 assert len(queue) == 6
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001322 assert queue[0].name == 'build:project-test1'
1323 assert queue[1].name == 'build:project-test2'
1324 assert queue[2].name == 'build:project-test1'
1325 assert queue[3].name == 'build:project-test2'
1326 assert queue[4].name == 'build:project-test1'
1327 assert queue[5].name == 'build:project-test2'
James E. Blaird466dc42012-07-31 10:42:56 -07001328
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001329 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001330 self.waitUntilSettled()
1331
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001332 assert len(self.builds) == 0
James E. Blair701c5b42013-06-06 09:34:59 -07001333 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001334 assert len(queue) == 2 # project-test2, project-merge for B
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001335 assert self.countJobResults(self.history, 'ABORTED') == 0
James E. Blaird466dc42012-07-31 10:42:56 -07001336
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001337 self.gearman_server.hold_jobs_in_queue = False
1338 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001339 self.waitUntilSettled()
1340
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001341 assert len(self.builds) == 0
1342 assert len(self.history) == 11
James E. Blaird466dc42012-07-31 10:42:56 -07001343 assert A.data['status'] == 'NEW'
1344 assert B.data['status'] == 'MERGED'
1345 assert C.data['status'] == 'MERGED'
1346 assert A.reported == 2
1347 assert B.reported == 2
1348 assert C.reported == 2
James E. Blair8c803f82012-07-31 16:25:42 -07001349
1350 def test_patch_order(self):
1351 "Test that dependent patches are tested in the right order"
1352 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1353 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1354 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1355 A.addApproval('CRVW', 2)
1356 B.addApproval('CRVW', 2)
1357 C.addApproval('CRVW', 2)
1358
1359 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1360 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1361 M2.setMerged()
1362 M1.setMerged()
1363
1364 # C -> B -> A -> M1 -> M2
1365 # M2 is here to make sure it is never queried. If it is, it
1366 # means zuul is walking down the entire history of merged
1367 # changes.
1368
1369 C.setDependsOn(B, 1)
1370 B.setDependsOn(A, 1)
1371 A.setDependsOn(M1, 1)
1372 M1.setDependsOn(M2, 1)
1373
1374 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1375
1376 self.waitUntilSettled()
1377
1378 assert A.data['status'] == 'NEW'
1379 assert B.data['status'] == 'NEW'
1380 assert C.data['status'] == 'NEW'
1381
1382 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1383 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1384
1385 self.waitUntilSettled()
1386 assert M2.queried == 0
1387 assert A.data['status'] == 'MERGED'
1388 assert B.data['status'] == 'MERGED'
1389 assert C.data['status'] == 'MERGED'
1390 assert A.reported == 2
1391 assert B.reported == 2
1392 assert C.reported == 2
1393
1394 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001395 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001396 # TODO: move to test_gerrit (this is a unit test!)
1397 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4aea70c2012-07-26 14:23:24 -07001398 a = self.sched.trigger.getChange(1, 2)
1399 mgr = self.sched.pipelines['gate'].manager
1400 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001401
1402 A.addApproval('CRVW', 2)
James E. Blairfee8d652013-06-07 08:57:52 -07001403 a = self.sched.trigger.getChange(1, 2, refresh=True)
James E. Blair4aea70c2012-07-26 14:23:24 -07001404 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001405
1406 A.addApproval('APRV', 1)
James E. Blairfee8d652013-06-07 08:57:52 -07001407 a = self.sched.trigger.getChange(1, 2, refresh=True)
James E. Blair4aea70c2012-07-26 14:23:24 -07001408 assert self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blairfee8d652013-06-07 08:57:52 -07001409 self.sched.trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001410
1411 def test_build_configuration(self):
1412 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001413
1414 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001415 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1416 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1417 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1418 A.addApproval('CRVW', 2)
1419 B.addApproval('CRVW', 2)
1420 C.addApproval('CRVW', 2)
1421 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1422 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1423 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1424 self.waitUntilSettled()
1425
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001426 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001427 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001428 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001429 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001430 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001431 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001432 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001433 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1434 self.gearman_server.hold_jobs_in_queue = False
1435 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001436 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001437
Monty Taylorbc758832013-06-17 17:22:42 -04001438 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001439 repo = git.Repo(path)
1440 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1441 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001442 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
1443 assert repo_messages == correct_messages
James E. Blair973721f2012-08-15 10:19:43 -07001444
1445 def test_build_configuration_conflict(self):
1446 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001447
1448 self.gearman_server.hold_jobs_in_queue = True
James E. Blair973721f2012-08-15 10:19:43 -07001449 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1450 A.addPatchset(['conflict'])
1451 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1452 B.addPatchset(['conflict'])
1453 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1454 A.addApproval('CRVW', 2)
1455 B.addApproval('CRVW', 2)
1456 C.addApproval('CRVW', 2)
1457 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1458 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1459 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1460 self.waitUntilSettled()
1461
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001462 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001463 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001464 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001465 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001466 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001467 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001468 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001469 self.getParameter(queue[-1], 'ZUUL_REF')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001470 self.gearman_server.hold_jobs_in_queue = False
1471 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001472 self.waitUntilSettled()
1473
1474 assert A.data['status'] == 'MERGED'
1475 assert B.data['status'] == 'NEW'
1476 assert C.data['status'] == 'MERGED'
1477 assert A.reported == 2
1478 assert B.reported == 2
1479 assert C.reported == 2
James E. Blairdaabed22012-08-15 15:38:57 -07001480
1481 def test_post(self):
1482 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001483
Zhongyue Luo5d556072012-09-21 02:00:47 +09001484 e = {
1485 "type": "ref-updated",
1486 "submitter": {
1487 "name": "User Name",
1488 },
1489 "refUpdate": {
1490 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1491 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1492 "refName": "master",
1493 "project": "org/project",
1494 }
1495 }
James E. Blairdaabed22012-08-15 15:38:57 -07001496 self.fake_gerrit.addEvent(e)
1497 self.waitUntilSettled()
1498
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001499 job_names = [x.name for x in self.history]
1500 assert len(self.history) == 1
James E. Blairdaabed22012-08-15 15:38:57 -07001501 assert 'project-post' in job_names
James E. Blairc6294a52012-08-17 10:19:48 -07001502
1503 def test_build_configuration_branch(self):
1504 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001505
1506 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001507 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1508 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1509 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1510 A.addApproval('CRVW', 2)
1511 B.addApproval('CRVW', 2)
1512 C.addApproval('CRVW', 2)
1513 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1514 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1515 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1516 self.waitUntilSettled()
1517
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001518 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001519 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001520 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001521 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001522 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001523 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001524 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001525 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1526 self.gearman_server.hold_jobs_in_queue = False
1527 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001528 self.waitUntilSettled()
1529
Monty Taylorbc758832013-06-17 17:22:42 -04001530 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001531 repo = git.Repo(path)
1532 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1533 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001534 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
1535 assert repo_messages == correct_messages
1536
1537 def test_build_configuration_branch_interaction(self):
1538 "Test that switching between branches works"
1539 self.test_build_configuration()
1540 self.test_build_configuration_branch()
1541 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001542 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001543 repo = git.Repo(path)
1544 repo.heads.master.commit = repo.commit('init')
1545 self.test_build_configuration()
1546
1547 def test_build_configuration_multi_branch(self):
1548 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001549
1550 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001551 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1552 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1553 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1554 A.addApproval('CRVW', 2)
1555 B.addApproval('CRVW', 2)
1556 C.addApproval('CRVW', 2)
1557 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1558 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1559 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1560 self.waitUntilSettled()
1561
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001562 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001563 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001564 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001565 ref_mp = self.getParameter(queue[-1], 'ZUUL_REF')
1566 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001567 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001568 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001569 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001570 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001571 ref_master = self.getParameter(queue[-1], 'ZUUL_REF')
1572 self.gearman_server.hold_jobs_in_queue = False
1573 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001574 self.waitUntilSettled()
1575
Monty Taylorbc758832013-06-17 17:22:42 -04001576 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001577 repo = git.Repo(path)
1578
1579 repo_messages = [c.message.strip()
1580 for c in repo.iter_commits(ref_master)]
1581 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001582 correct_messages = ['initial commit', 'A-1', 'C-1']
1583 assert repo_messages == correct_messages
1584
1585 repo_messages = [c.message.strip()
1586 for c in repo.iter_commits(ref_mp)]
1587 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001588 correct_messages = ['initial commit', 'mp commit', 'B-1']
1589 assert repo_messages == correct_messages
James E. Blair7f71c802012-08-22 13:04:32 -07001590
1591 def test_one_job_project(self):
1592 "Test that queueing works with one job"
1593 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1594 'master', 'A')
1595 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1596 'master', 'B')
1597 A.addApproval('CRVW', 2)
1598 B.addApproval('CRVW', 2)
1599 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1600 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1601 self.waitUntilSettled()
1602
James E. Blair7f71c802012-08-22 13:04:32 -07001603 assert A.data['status'] == 'MERGED'
1604 assert A.reported == 2
1605 assert B.data['status'] == 'MERGED'
1606 assert B.reported == 2
James E. Blaircaec0c52012-08-22 14:52:22 -07001607
Antoine Musso80edd5a2013-02-13 15:37:53 +01001608 def test_job_from_templates_launched(self):
1609 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001610
Antoine Musso80edd5a2013-02-13 15:37:53 +01001611 A = self.fake_gerrit.addFakeChange(
1612 'org/templated-project', 'master', 'A')
1613 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1614 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001615
James E. Blair4ca985f2013-05-30 12:27:43 -07001616 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1617 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
Antoine Musso80edd5a2013-02-13 15:37:53 +01001618
James E. Blaircaec0c52012-08-22 14:52:22 -07001619 def test_dependent_changes_dequeue(self):
1620 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001621
James E. Blaircaec0c52012-08-22 14:52:22 -07001622 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1623 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1624 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1625 A.addApproval('CRVW', 2)
1626 B.addApproval('CRVW', 2)
1627 C.addApproval('CRVW', 2)
1628
1629 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1630 M1.setMerged()
1631
1632 # C -> B -> A -> M1
1633
1634 C.setDependsOn(B, 1)
1635 B.setDependsOn(A, 1)
1636 A.setDependsOn(M1, 1)
1637
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001638 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001639
1640 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1641 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1642 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1643
1644 self.waitUntilSettled()
1645
James E. Blairec590122012-08-22 15:19:31 -07001646 assert A.data['status'] == 'NEW'
1647 assert A.reported == 2
1648 assert B.data['status'] == 'NEW'
1649 assert B.reported == 2
1650 assert C.data['status'] == 'NEW'
1651 assert C.reported == 2
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001652 assert len(self.history) == 1
James E. Blairec590122012-08-22 15:19:31 -07001653
1654 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001655 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001656 # If it's dequeued more than once, we should see extra
1657 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001658
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001659 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001660 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1661 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1662 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1663 A.addApproval('CRVW', 2)
1664 B.addApproval('CRVW', 2)
1665 C.addApproval('CRVW', 2)
1666
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001667 self.worker.addFailTest('project1-test1', A)
1668 self.worker.addFailTest('project1-test2', A)
1669 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001670
1671 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1672 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1673 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1674
1675 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001676
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001677 assert len(self.builds) == 1
1678 assert self.builds[0].name == 'project1-merge'
Monty Taylorbc758832013-06-17 17:22:42 -04001679 assert self.job_has_changes(self.builds[0], A)
James E. Blairec590122012-08-22 15:19:31 -07001680
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001681 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001682 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001683 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001684 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001685 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001686 self.waitUntilSettled()
1687
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001688 assert len(self.builds) == 9
1689 assert self.builds[0].name == 'project1-test1'
1690 assert self.builds[1].name == 'project1-test2'
1691 assert self.builds[2].name == 'project1-project2-integration'
1692 assert self.builds[3].name == 'project1-test1'
1693 assert self.builds[4].name == 'project1-test2'
1694 assert self.builds[5].name == 'project1-project2-integration'
1695 assert self.builds[6].name == 'project1-test1'
1696 assert self.builds[7].name == 'project1-test2'
1697 assert self.builds[8].name == 'project1-project2-integration'
James E. Blairec590122012-08-22 15:19:31 -07001698
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001699 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001700 self.waitUntilSettled()
1701
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001702 assert len(self.builds) == 3 # test2, integration, merge for B
1703 assert self.countJobResults(self.history, 'ABORTED') == 6
James E. Blairec590122012-08-22 15:19:31 -07001704
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001705 self.worker.hold_jobs_in_build = False
1706 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001707 self.waitUntilSettled()
1708
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001709 assert len(self.builds) == 0
1710 assert len(self.history) == 20
James E. Blaircaec0c52012-08-22 14:52:22 -07001711
1712 assert A.data['status'] == 'NEW'
James E. Blairec590122012-08-22 15:19:31 -07001713 assert B.data['status'] == 'MERGED'
1714 assert C.data['status'] == 'MERGED'
1715 assert A.reported == 2
1716 assert B.reported == 2
1717 assert C.reported == 2
James E. Blair4ec821f2012-08-23 15:28:28 -07001718
1719 def test_nonvoting_job(self):
1720 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001721
James E. Blair4ec821f2012-08-23 15:28:28 -07001722 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1723 'master', 'A')
1724 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001725 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001726 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1727
1728 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001729
1730 assert A.data['status'] == 'MERGED'
1731 assert A.reported == 2
James E. Blair4ca985f2013-05-30 12:27:43 -07001732 assert (self.getJobFromHistory('nonvoting-project-merge').result ==
1733 'SUCCESS')
1734 assert (self.getJobFromHistory('nonvoting-project-test1').result ==
1735 'SUCCESS')
1736 assert (self.getJobFromHistory('nonvoting-project-test2').result ==
1737 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001738
1739 def test_check_queue_success(self):
1740 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001741
James E. Blaire0487072012-08-29 17:38:31 -07001742 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1743 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1744
1745 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001746
1747 assert A.data['status'] == 'NEW'
1748 assert A.reported == 1
James E. Blair4ca985f2013-05-30 12:27:43 -07001749 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1750 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1751 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blaire0487072012-08-29 17:38:31 -07001752
1753 def test_check_queue_failure(self):
1754 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001755
James E. Blaire0487072012-08-29 17:38:31 -07001756 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001757 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001758 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1759
1760 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001761
1762 assert A.data['status'] == 'NEW'
1763 assert A.reported == 1
James E. Blair4ca985f2013-05-30 12:27:43 -07001764 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1765 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1766 assert self.getJobFromHistory('project-test2').result == 'FAILURE'
James E. Blair127bc182012-08-28 15:55:15 -07001767
1768 def test_dependent_behind_dequeue(self):
1769 "test that dependent changes behind dequeued changes work"
1770 # This complicated test is a reproduction of a real life bug
1771 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001772
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001773 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001774 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1775 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1776 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1777 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1778 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1779 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1780 D.setDependsOn(C, 1)
1781 E.setDependsOn(D, 1)
1782 A.addApproval('CRVW', 2)
1783 B.addApproval('CRVW', 2)
1784 C.addApproval('CRVW', 2)
1785 D.addApproval('CRVW', 2)
1786 E.addApproval('CRVW', 2)
1787 F.addApproval('CRVW', 2)
1788
1789 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001790
1791 # Change object re-use in the gerrit trigger is hidden if
1792 # changes are added in quick succession; waiting makes it more
1793 # like real life.
1794 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1795 self.waitUntilSettled()
1796 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1797 self.waitUntilSettled()
1798
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001799 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001800 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001801 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001802 self.waitUntilSettled()
1803
1804 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1805 self.waitUntilSettled()
1806 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1807 self.waitUntilSettled()
1808 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1809 self.waitUntilSettled()
1810 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1811 self.waitUntilSettled()
1812
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001813 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001814 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001815 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001816 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001817 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001818 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001819 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001820 self.waitUntilSettled()
1821
1822 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001823
1824 # Grab pointers to the jobs we want to release before
1825 # releasing any, because list indexes may change as
1826 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001827 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001828 a.release()
1829 b.release()
1830 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001831 self.waitUntilSettled()
1832
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001833 self.worker.hold_jobs_in_build = False
1834 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07001835 self.waitUntilSettled()
1836
James E. Blair127bc182012-08-28 15:55:15 -07001837 assert A.data['status'] == 'NEW'
1838 assert B.data['status'] == 'MERGED'
1839 assert C.data['status'] == 'MERGED'
1840 assert D.data['status'] == 'MERGED'
1841 assert E.data['status'] == 'MERGED'
1842 assert F.data['status'] == 'MERGED'
1843
1844 assert A.reported == 2
1845 assert B.reported == 2
1846 assert C.reported == 2
1847 assert D.reported == 2
1848 assert E.reported == 2
1849 assert F.reported == 2
1850
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001851 assert self.countJobResults(self.history, 'ABORTED') == 15
1852 assert len(self.history) == 44
James E. Blair05fed602012-09-07 12:45:24 -07001853
1854 def test_merger_repack(self):
1855 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001856
James E. Blair05fed602012-09-07 12:45:24 -07001857 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1858 A.addApproval('CRVW', 2)
1859 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1860 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001861 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1862 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1863 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blair05fed602012-09-07 12:45:24 -07001864 assert A.data['status'] == 'MERGED'
1865 assert A.reported == 2
1866 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07001867 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001868
Monty Taylorbc758832013-06-17 17:22:42 -04001869 path = os.path.join(self.git_root, "org/project")
1870 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001871
1872 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1873 A.addApproval('CRVW', 2)
1874 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1875 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001876 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1877 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1878 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blair05fed602012-09-07 12:45:24 -07001879 assert A.data['status'] == 'MERGED'
1880 assert A.reported == 2
James E. Blair7ee88a22012-09-12 18:59:31 +02001881
James E. Blair4886f282012-11-15 09:27:33 -08001882 def test_merger_repack_large_change(self):
1883 "Test that the merger works with large changes after a repack"
1884 # https://bugs.launchpad.net/zuul/+bug/1078946
1885 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1886 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001887 path = os.path.join(self.upstream_root, "org/project1")
1888 print repack_repo(path)
1889 path = os.path.join(self.git_root, "org/project1")
1890 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001891
1892 A.addApproval('CRVW', 2)
1893 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1894 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001895 assert self.getJobFromHistory('project1-merge').result == 'SUCCESS'
1896 assert self.getJobFromHistory('project1-test1').result == 'SUCCESS'
1897 assert self.getJobFromHistory('project1-test2').result == 'SUCCESS'
James E. Blair4886f282012-11-15 09:27:33 -08001898 assert A.data['status'] == 'MERGED'
1899 assert A.reported == 2
James E. Blair4886f282012-11-15 09:27:33 -08001900
James E. Blair7ee88a22012-09-12 18:59:31 +02001901 def test_nonexistent_job(self):
1902 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001903 # Set to the state immediately after a restart
1904 self.resetGearmanServer()
1905 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02001906
1907 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1908 A.addApproval('CRVW', 2)
1909 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1910 # There may be a thread about to report a lost change
1911 while A.reported < 2:
1912 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001913 job_names = [x.name for x in self.history]
James E. Blair7ee88a22012-09-12 18:59:31 +02001914 assert not job_names
1915 assert A.data['status'] == 'NEW'
1916 assert A.reported == 2
1917 self.assertEmptyQueues()
1918
1919 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001920 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02001921 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1922 A.addApproval('CRVW', 2)
1923 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1924 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001925 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1926 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1927 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blair7ee88a22012-09-12 18:59:31 +02001928 assert A.data['status'] == 'MERGED'
1929 assert A.reported == 2
James E. Blairf62d4282012-12-31 17:01:50 -08001930
1931 def test_single_nonexistent_post_job(self):
1932 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08001933 e = {
1934 "type": "ref-updated",
1935 "submitter": {
1936 "name": "User Name",
1937 },
1938 "refUpdate": {
1939 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1940 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1941 "refName": "master",
1942 "project": "org/project",
1943 }
1944 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001945 # Set to the state immediately after a restart
1946 self.resetGearmanServer()
1947 self.launcher.negative_function_cache_ttl = 0
1948
James E. Blairf62d4282012-12-31 17:01:50 -08001949 self.fake_gerrit.addEvent(e)
1950 self.waitUntilSettled()
1951
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001952 assert len(self.history) == 0
James E. Blair2fa50962013-01-30 21:50:41 -08001953
1954 def test_new_patchset_dequeues_old(self):
1955 "Test that a new patchset causes the old to be dequeued"
1956 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001957 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001958 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1959 M.setMerged()
1960
1961 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1962 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1963 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1964 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1965 A.addApproval('CRVW', 2)
1966 B.addApproval('CRVW', 2)
1967 C.addApproval('CRVW', 2)
1968 D.addApproval('CRVW', 2)
1969
1970 C.setDependsOn(B, 1)
1971 B.setDependsOn(A, 1)
1972 A.setDependsOn(M, 1)
1973
1974 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1975 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1976 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1977 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1978 self.waitUntilSettled()
1979
1980 B.addPatchset()
1981 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1982 self.waitUntilSettled()
1983
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001984 self.worker.hold_jobs_in_build = False
1985 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001986 self.waitUntilSettled()
1987
James E. Blair2fa50962013-01-30 21:50:41 -08001988 assert A.data['status'] == 'MERGED'
1989 assert A.reported == 2
1990 assert B.data['status'] == 'NEW'
1991 assert B.reported == 2
1992 assert C.data['status'] == 'NEW'
1993 assert C.reported == 2
1994 assert D.data['status'] == 'MERGED'
1995 assert D.reported == 2
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001996 assert len(self.history) == 9 # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001997
1998 def test_new_patchset_dequeues_old_on_head(self):
1999 "Test that a new patchset causes the old to be dequeued (at head)"
2000 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002001 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002002 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2003 M.setMerged()
2004 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2005 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2006 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2007 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2008 A.addApproval('CRVW', 2)
2009 B.addApproval('CRVW', 2)
2010 C.addApproval('CRVW', 2)
2011 D.addApproval('CRVW', 2)
2012
2013 C.setDependsOn(B, 1)
2014 B.setDependsOn(A, 1)
2015 A.setDependsOn(M, 1)
2016
2017 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2018 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2019 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2020 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2021 self.waitUntilSettled()
2022
2023 A.addPatchset()
2024 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2025 self.waitUntilSettled()
2026
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002027 self.worker.hold_jobs_in_build = False
2028 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002029 self.waitUntilSettled()
2030
James E. Blair2fa50962013-01-30 21:50:41 -08002031 assert A.data['status'] == 'NEW'
2032 assert A.reported == 2
2033 assert B.data['status'] == 'NEW'
2034 assert B.reported == 2
2035 assert C.data['status'] == 'NEW'
2036 assert C.reported == 2
2037 assert D.data['status'] == 'MERGED'
2038 assert D.reported == 2
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002039 assert len(self.history) == 7
James E. Blair2fa50962013-01-30 21:50:41 -08002040
2041 def test_new_patchset_dequeues_old_without_dependents(self):
2042 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002043 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002044 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2045 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2046 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2047 A.addApproval('CRVW', 2)
2048 B.addApproval('CRVW', 2)
2049 C.addApproval('CRVW', 2)
2050
2051 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2052 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2053 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2054 self.waitUntilSettled()
2055
2056 B.addPatchset()
2057 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2058 self.waitUntilSettled()
2059
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002060 self.worker.hold_jobs_in_build = False
2061 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002062 self.waitUntilSettled()
2063
James E. Blair2fa50962013-01-30 21:50:41 -08002064 assert A.data['status'] == 'MERGED'
2065 assert A.reported == 2
2066 assert B.data['status'] == 'NEW'
2067 assert B.reported == 2
2068 assert C.data['status'] == 'MERGED'
2069 assert C.reported == 2
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002070 assert len(self.history) == 9
James E. Blair2fa50962013-01-30 21:50:41 -08002071
2072 def test_new_patchset_dequeues_old_independent_queue(self):
2073 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002074 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002075 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2076 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2077 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2078 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2079 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2080 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2081 self.waitUntilSettled()
2082
2083 B.addPatchset()
2084 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2085 self.waitUntilSettled()
2086
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002087 self.worker.hold_jobs_in_build = False
2088 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002089 self.waitUntilSettled()
2090
James E. Blair2fa50962013-01-30 21:50:41 -08002091 assert A.data['status'] == 'NEW'
2092 assert A.reported == 1
2093 assert B.data['status'] == 'NEW'
2094 assert B.reported == 1
2095 assert C.data['status'] == 'NEW'
2096 assert C.reported == 1
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002097 assert len(self.history) == 10
2098 assert self.countJobResults(self.history, 'ABORTED') == 1
James E. Blair7d0dedc2013-02-21 17:26:09 -08002099
2100 def test_zuul_refs(self):
2101 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002102 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002103 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2104 M1.setMerged()
2105 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2106 M2.setMerged()
2107
2108 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2109 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2110 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2111 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2112 A.addApproval('CRVW', 2)
2113 B.addApproval('CRVW', 2)
2114 C.addApproval('CRVW', 2)
2115 D.addApproval('CRVW', 2)
2116 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2117 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2118 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2119 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2120
2121 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002122 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002123 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002124 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002125 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002126 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002127 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002128 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002129 self.waitUntilSettled()
2130
James E. Blair7d0dedc2013-02-21 17:26:09 -08002131 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002132 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002133 if x.parameters['ZUUL_CHANGE'] == '3':
2134 a_zref = x.parameters['ZUUL_REF']
2135 if x.parameters['ZUUL_CHANGE'] == '4':
2136 b_zref = x.parameters['ZUUL_REF']
2137 if x.parameters['ZUUL_CHANGE'] == '5':
2138 c_zref = x.parameters['ZUUL_REF']
2139 if x.parameters['ZUUL_CHANGE'] == '6':
2140 d_zref = x.parameters['ZUUL_REF']
2141
2142 # There are... four... refs.
2143 assert a_zref is not None
2144 assert b_zref is not None
2145 assert c_zref is not None
2146 assert d_zref is not None
2147
2148 # And they should all be different
2149 refs = set([a_zref, b_zref, c_zref, d_zref])
2150 assert len(refs) == 4
2151
2152 # a ref should have a, not b, and should not be in project2
Monty Taylorbc758832013-06-17 17:22:42 -04002153 assert self.ref_has_change(a_zref, A)
2154 assert not self.ref_has_change(a_zref, B)
2155 assert not self.ref_has_change(a_zref, M2)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002156
2157 # b ref should have a and b, and should not be in project2
Monty Taylorbc758832013-06-17 17:22:42 -04002158 assert self.ref_has_change(b_zref, A)
2159 assert self.ref_has_change(b_zref, B)
2160 assert not self.ref_has_change(b_zref, M2)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002161
2162 # c ref should have a and b in 1, c in 2
Monty Taylorbc758832013-06-17 17:22:42 -04002163 assert self.ref_has_change(c_zref, A)
2164 assert self.ref_has_change(c_zref, B)
2165 assert self.ref_has_change(c_zref, C)
2166 assert not self.ref_has_change(c_zref, D)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002167
2168 # d ref should have a and b in 1, c and d in 2
Monty Taylorbc758832013-06-17 17:22:42 -04002169 assert self.ref_has_change(d_zref, A)
2170 assert self.ref_has_change(d_zref, B)
2171 assert self.ref_has_change(d_zref, C)
2172 assert self.ref_has_change(d_zref, D)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002173
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002174 self.worker.hold_jobs_in_build = False
2175 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002176 self.waitUntilSettled()
2177
2178 assert A.data['status'] == 'MERGED'
2179 assert A.reported == 2
2180 assert B.data['status'] == 'MERGED'
2181 assert B.reported == 2
2182 assert C.data['status'] == 'MERGED'
2183 assert C.reported == 2
2184 assert D.data['status'] == 'MERGED'
2185 assert D.reported == 2
James E. Blair70c71582013-03-06 08:50:50 -08002186
James E. Blair412e5582013-04-22 15:50:12 -07002187 def test_statsd(self):
2188 "Test each of the statsd methods used in the scheduler"
2189 import extras
2190 statsd = extras.try_import('statsd.statsd')
2191 statsd.incr('test-incr')
2192 statsd.timing('test-timing', 3)
2193 statsd.gauge('test-guage', 12)
2194 self.assertReportedStat('test-incr', '1|c')
2195 self.assertReportedStat('test-timing', '3|ms')
2196 self.assertReportedStat('test-guage', '12|g')
2197
James E. Blair70c71582013-03-06 08:50:50 -08002198 def test_file_jobs(self):
2199 "Test that file jobs run only when appropriate"
2200 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2201 A.addPatchset(['pip-requires'])
2202 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2203 A.addApproval('CRVW', 2)
2204 B.addApproval('CRVW', 2)
2205 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2206 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2207 self.waitUntilSettled()
2208
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002209 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002210 if x.name == 'project-testfile']
2211
2212 assert len(testfile_jobs) == 1
2213 assert testfile_jobs[0].changes == '1,2'
2214 assert A.data['status'] == 'MERGED'
2215 assert A.reported == 2
2216 assert B.data['status'] == 'MERGED'
2217 assert B.reported == 2
James E. Blair3c5e5b52013-04-26 11:17:03 -07002218
2219 def test_test_config(self):
2220 "Test that we can test the config"
2221 sched = zuul.scheduler.Scheduler()
2222 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002223
2224 def test_build_description(self):
2225 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002226 self.worker.registerFunction('set_description:' +
2227 self.worker.worker_id)
2228
2229 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2230 A.addApproval('CRVW', 2)
2231 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2232 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002233 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002234 self.log.debug("Description: %s" % desc)
2235 assert re.search("Branch.*master", desc)
2236 assert re.search("Pipeline.*gate", desc)
2237 assert re.search("project-merge.*SUCCESS", desc)
2238 assert re.search("project-test1.*SUCCESS", desc)
2239 assert re.search("project-test2.*SUCCESS", desc)
2240 assert re.search("Reported result.*SUCCESS", desc)
2241
James E. Blair1843a552013-07-03 14:19:52 -07002242 def test_json_status(self):
2243 "Test that we can retrieve JSON status info"
2244 self.worker.hold_jobs_in_build = True
2245 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2246 A.addApproval('CRVW', 2)
2247 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2248 self.waitUntilSettled()
2249
2250 port = self.webapp.server.socket.getsockname()[1]
2251
2252 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2253 data = f.read()
2254
2255 self.worker.hold_jobs_in_build = False
2256 self.worker.release()
2257 self.waitUntilSettled()
2258
2259 data = json.loads(data)
2260 status_jobs = set()
2261 for p in data['pipelines']:
2262 for q in p['change_queues']:
2263 for head in q['heads']:
2264 for change in head:
2265 assert change['id'] == '1,1'
2266 for job in change['jobs']:
2267 status_jobs.add(job['name'])
2268 assert 'project-merge' in status_jobs
2269 assert 'project-test1' in status_jobs
2270 assert 'project-test2' in status_jobs
2271
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002272 def test_node_label(self):
2273 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002274 self.worker.registerFunction('build:node-project-test1:debian')
2275
2276 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2277 A.addApproval('CRVW', 2)
2278 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2279 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002280
2281 assert self.getJobFromHistory('node-project-merge').node is None
2282 assert self.getJobFromHistory('node-project-test1').node == 'debian'
2283 assert self.getJobFromHistory('node-project-test2').node is None