blob: 8fb84195b574b4bee21956aca8ec2608ed8d4434 [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
Clark Boylan4ba48d92013-11-11 18:03:53 -080019import gc
James E. Blair8cc15a82012-08-01 11:17:57 -070020import hashlib
James E. Blairb0fcae42012-07-17 11:12:10 -070021import json
Monty Taylorbc758832013-06-17 17:22:42 -040022import logging
23import os
James E. Blairb0fcae42012-07-17 11:12:10 -070024import pprint
Monty Taylorbc758832013-06-17 17:22:42 -040025import Queue
26import random
James E. Blairb0fcae42012-07-17 11:12:10 -070027import re
James E. Blair412e5582013-04-22 15:50:12 -070028import select
James E. Blair4886cc12012-07-18 15:39:41 -070029import shutil
James E. Blair412e5582013-04-22 15:50:12 -070030import socket
James E. Blair4886f282012-11-15 09:27:33 -080031import string
Monty Taylorbc758832013-06-17 17:22:42 -040032import subprocess
Monty Taylorbc758832013-06-17 17:22:42 -040033import 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. Blairad28e912013-11-27 10:43:22 -080047import zuul.rpclistener
48import zuul.rpcclient
James E. Blair1f4c2bb2013-04-26 08:40:46 -070049import zuul.launcher.gearman
Joshua Hesketh1879cf72013-08-19 14:13:15 +100050import zuul.reporter.gerrit
Joshua Hesketh5fea8672013-08-19 17:32:01 +100051import zuul.reporter.smtp
James E. Blairb0fcae42012-07-17 11:12:10 -070052import zuul.trigger.gerrit
James E. Blair63bb0ef2013-07-29 17:14:51 -070053import zuul.trigger.timer
James E. Blairb0fcae42012-07-17 11:12:10 -070054
55FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
56 'fixtures')
57CONFIG = ConfigParser.ConfigParser()
58CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
59
60CONFIG.set('zuul', 'layout_config',
61 os.path.join(FIXTURE_DIR, "layout.yaml"))
62
James E. Blair1f4c2bb2013-04-26 08:40:46 -070063logging.basicConfig(level=logging.DEBUG,
64 format='%(asctime)s %(name)-32s '
65 '%(levelname)-8s %(message)s')
James E. Blairb0fcae42012-07-17 11:12:10 -070066
67
Monty Taylorbc758832013-06-17 17:22:42 -040068def repack_repo(path):
69 output = subprocess.Popen(
70 ['git', '--git-dir=%s/.git' % path, 'repack', '-afd'],
71 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
72 out = output.communicate()
73 if output.returncode:
74 raise Exception("git repack returned %d" % output.returncode)
75 return out
76
77
James E. Blair8cc15a82012-08-01 11:17:57 -070078def random_sha1():
79 return hashlib.sha1(str(random.random())).hexdigest()
80
81
James E. Blair4886cc12012-07-18 15:39:41 -070082class ChangeReference(git.Reference):
83 _common_path_default = "refs/changes"
84 _points_to_commits_only = True
85
86
James E. Blairb0fcae42012-07-17 11:12:10 -070087class FakeChange(object):
James E. Blair8c803f82012-07-31 16:25:42 -070088 categories = {'APRV': ('Approved', -1, 1),
89 'CRVW': ('Code-Review', -2, 2),
90 'VRFY': ('Verified', -2, 2)}
James E. Blairb0fcae42012-07-17 11:12:10 -070091
Monty Taylorbc758832013-06-17 17:22:42 -040092 def __init__(self, gerrit, number, project, branch, subject,
93 status='NEW', upstream_root=None):
James E. Blair8cc15a82012-08-01 11:17:57 -070094 self.gerrit = gerrit
James E. Blaird466dc42012-07-31 10:42:56 -070095 self.reported = 0
James E. Blair8c803f82012-07-31 16:25:42 -070096 self.queried = 0
James E. Blairb0fcae42012-07-17 11:12:10 -070097 self.patchsets = []
James E. Blairb0fcae42012-07-17 11:12:10 -070098 self.number = number
99 self.project = project
100 self.branch = branch
101 self.subject = subject
102 self.latest_patchset = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700103 self.depends_on_change = None
104 self.needed_by_changes = []
James E. Blair127bc182012-08-28 15:55:15 -0700105 self.fail_merge = False
James E. Blair42f74822013-05-14 15:18:03 -0700106 self.messages = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700107 self.data = {
108 'branch': branch,
109 'comments': [],
110 'commitMessage': subject,
111 'createdOn': time.time(),
James E. Blair8cc15a82012-08-01 11:17:57 -0700112 'id': 'I' + random_sha1(),
James E. Blairb0fcae42012-07-17 11:12:10 -0700113 'lastUpdated': time.time(),
114 'number': str(number),
115 'open': True,
116 'owner': {'email': 'user@example.com',
117 'name': 'User Name',
118 'username': 'username'},
119 'patchSets': self.patchsets,
120 'project': project,
121 'status': status,
122 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700123 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700124 'url': 'https://hostname/%s' % number}
125
Monty Taylorbc758832013-06-17 17:22:42 -0400126 self.upstream_root = upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700127 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700128 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700129
Monty Taylorbc758832013-06-17 17:22:42 -0400130 def add_fake_change_to_repo(self, msg, fn, large):
131 path = os.path.join(self.upstream_root, self.project)
132 repo = git.Repo(path)
133 ref = ChangeReference.create(repo, '1/%s/%s' % (self.number,
134 self.latest_patchset),
135 'refs/tags/init')
136 repo.head.reference = ref
137 repo.head.reset(index=True, working_tree=True)
138 repo.git.clean('-x', '-f', '-d')
139
140 path = os.path.join(self.upstream_root, self.project)
141 if not large:
142 fn = os.path.join(path, fn)
143 f = open(fn, 'w')
144 f.write("test %s %s %s\n" %
145 (self.branch, self.number, self.latest_patchset))
146 f.close()
147 repo.index.add([fn])
148 else:
149 for fni in range(100):
150 fn = os.path.join(path, str(fni))
151 f = open(fn, 'w')
152 for ci in range(4096):
153 f.write(random.choice(string.printable))
154 f.close()
155 repo.index.add([fn])
156
James E. Blair287c06d2013-07-24 10:39:30 -0700157 r = repo.index.commit(msg)
158 repo.head.reference = 'master'
159 repo.head.reset(index=True, working_tree=True)
160 repo.git.clean('-x', '-f', '-d')
161 return r
Monty Taylorbc758832013-06-17 17:22:42 -0400162
James E. Blair70c71582013-03-06 08:50:50 -0800163 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700164 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700165 if files:
166 fn = files[0]
167 else:
168 fn = '%s-%s' % (self.branch, self.number)
169 msg = self.subject + '-' + str(self.latest_patchset)
Monty Taylorbc758832013-06-17 17:22:42 -0400170 c = self.add_fake_change_to_repo(msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800171 ps_files = [{'file': '/COMMIT_MSG',
172 'type': 'ADDED'},
173 {'file': 'README',
174 'type': 'MODIFIED'}]
175 for f in files:
176 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700177 d = {'approvals': [],
178 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800179 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700180 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700181 'ref': 'refs/changes/1/%s/%s' % (self.number,
182 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700183 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700184 'uploader': {'email': 'user@example.com',
185 'name': 'User name',
186 'username': 'user'}}
187 self.data['currentPatchSet'] = d
188 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700189 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700190
James E. Blaire0487072012-08-29 17:38:31 -0700191 def getPatchsetCreatedEvent(self, patchset):
192 event = {"type": "patchset-created",
193 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800194 "branch": self.branch,
195 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
196 "number": str(self.number),
197 "subject": self.subject,
198 "owner": {"name": "User Name"},
199 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700200 "patchSet": self.patchsets[patchset - 1],
201 "uploader": {"name": "User Name"}}
202 return event
203
James E. Blair42f74822013-05-14 15:18:03 -0700204 def getChangeRestoredEvent(self):
205 event = {"type": "change-restored",
206 "change": {"project": self.project,
207 "branch": self.branch,
208 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
209 "number": str(self.number),
210 "subject": self.subject,
211 "owner": {"name": "User Name"},
212 "url": "https://hostname/3"},
213 "restorer": {"name": "User Name"},
214 "reason": ""}
215 return event
216
James E. Blairb0fcae42012-07-17 11:12:10 -0700217 def addApproval(self, category, value):
James E. Blair8c803f82012-07-31 16:25:42 -0700218 approval = {'description': self.categories[category][0],
219 'type': category,
220 'value': str(value)}
221 self.patchsets[-1]['approvals'].append(approval)
222 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700223 'author': {'email': 'user@example.com',
224 'name': 'User Name',
225 'username': 'username'},
226 'change': {'branch': self.branch,
227 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
228 'number': str(self.number),
229 'owner': {'email': 'user@example.com',
230 'name': 'User Name',
231 'username': 'username'},
232 'project': self.project,
233 'subject': self.subject,
234 'topic': 'master',
235 'url': 'https://hostname/459'},
236 'comment': '',
237 'patchSet': self.patchsets[-1],
238 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700239 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700240 return json.loads(json.dumps(event))
241
James E. Blair8c803f82012-07-31 16:25:42 -0700242 def getSubmitRecords(self):
243 status = {}
244 for cat in self.categories.keys():
245 status[cat] = 0
246
247 for a in self.patchsets[-1]['approvals']:
248 cur = status[a['type']]
249 cat_min, cat_max = self.categories[a['type']][1:]
250 new = int(a['value'])
251 if new == cat_min:
252 cur = new
253 elif abs(new) > abs(cur):
254 cur = new
255 status[a['type']] = cur
256
257 labels = []
258 ok = True
259 for typ, cat in self.categories.items():
260 cur = status[typ]
261 cat_min, cat_max = cat[1:]
262 if cur == cat_min:
263 value = 'REJECT'
264 ok = False
265 elif cur == cat_max:
266 value = 'OK'
267 else:
268 value = 'NEED'
269 ok = False
270 labels.append({'label': cat[0], 'status': value})
271 if ok:
272 return [{'status': 'OK'}]
273 return [{'status': 'NOT_READY',
274 'labels': labels}]
275
276 def setDependsOn(self, other, patchset):
277 self.depends_on_change = other
278 d = {'id': other.data['id'],
279 'number': other.data['number'],
280 'ref': other.patchsets[patchset - 1]['ref']
281 }
282 self.data['dependsOn'] = [d]
283
284 other.needed_by_changes.append(self)
285 needed = other.data.get('neededBy', [])
286 d = {'id': self.data['id'],
287 'number': self.data['number'],
288 'ref': self.patchsets[patchset - 1]['ref'],
289 'revision': self.patchsets[patchset - 1]['revision']
290 }
291 needed.append(d)
292 other.data['neededBy'] = needed
293
James E. Blairb0fcae42012-07-17 11:12:10 -0700294 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700295 self.queried += 1
296 d = self.data.get('dependsOn')
297 if d:
298 d = d[0]
299 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
300 d['isCurrentPatchSet'] = True
301 else:
302 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700303 return json.loads(json.dumps(self.data))
304
305 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800306 if (self.depends_on_change and
307 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700308 return
James E. Blair127bc182012-08-28 15:55:15 -0700309 if self.fail_merge:
310 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700311 self.data['status'] = 'MERGED'
312 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700313
Monty Taylorbc758832013-06-17 17:22:42 -0400314 path = os.path.join(self.upstream_root, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700315 repo = git.Repo(path)
316 repo.heads[self.branch].commit = \
317 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700318
James E. Blaird466dc42012-07-31 10:42:56 -0700319 def setReported(self):
320 self.reported += 1
321
James E. Blairb0fcae42012-07-17 11:12:10 -0700322
323class FakeGerrit(object):
324 def __init__(self, *args, **kw):
325 self.event_queue = Queue.Queue()
326 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
327 self.change_number = 0
328 self.changes = {}
329
330 def addFakeChange(self, project, branch, subject):
331 self.change_number += 1
Monty Taylorbc758832013-06-17 17:22:42 -0400332 c = FakeChange(self, self.change_number, project, branch, subject,
333 upstream_root=self.upstream_root)
James E. Blairb0fcae42012-07-17 11:12:10 -0700334 self.changes[self.change_number] = c
335 return c
336
337 def addEvent(self, data):
338 return self.event_queue.put(data)
339
340 def getEvent(self):
341 return self.event_queue.get()
342
343 def eventDone(self):
344 self.event_queue.task_done()
345
346 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700347 number, ps = changeid.split(',')
348 change = self.changes[int(number)]
James E. Blair42f74822013-05-14 15:18:03 -0700349 change.messages.append(message)
James E. Blairb0fcae42012-07-17 11:12:10 -0700350 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700351 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700352 if message:
353 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700354
355 def query(self, number):
James E. Blairad28e912013-11-27 10:43:22 -0800356 change = self.changes.get(int(number))
357 if change:
358 return change.query()
359 return {}
James E. Blairb0fcae42012-07-17 11:12:10 -0700360
361 def startWatching(self, *args, **kw):
362 pass
363
364
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700365class BuildHistory(object):
366 def __init__(self, **kw):
367 self.__dict__.update(kw)
James E. Blairb0fcae42012-07-17 11:12:10 -0700368
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700369 def __repr__(self):
370 return ("<Completed build, result: %s name: %s #%s changes: %s>" %
371 (self.result, self.name, self.number, self.changes))
James E. Blairb0fcae42012-07-17 11:12:10 -0700372
373
James E. Blair8cc15a82012-08-01 11:17:57 -0700374class FakeURLOpener(object):
Monty Taylorbc758832013-06-17 17:22:42 -0400375 def __init__(self, upstream_root, fake_gerrit, url):
376 self.upstream_root = upstream_root
James E. Blair8cc15a82012-08-01 11:17:57 -0700377 self.fake_gerrit = fake_gerrit
378 self.url = url
379
380 def read(self):
381 res = urlparse.urlparse(self.url)
382 path = res.path
383 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200384 ret = '001e# service=git-upload-pack\n'
385 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
386 'multi_ack thin-pack side-band side-band-64k ofs-delta '
387 'shallow no-progress include-tag multi_ack_detailed no-done\n')
Monty Taylorbc758832013-06-17 17:22:42 -0400388 path = os.path.join(self.upstream_root, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700389 repo = git.Repo(path)
390 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200391 r = ref.object.hexsha + ' ' + ref.path + '\n'
392 ret += '%04x%s' % (len(r) + 4, r)
393 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700394 return ret
395
396
James E. Blair4886cc12012-07-18 15:39:41 -0700397class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000398 name = 'gerrit'
399
Monty Taylorbc758832013-06-17 17:22:42 -0400400 def __init__(self, upstream_root, *args):
401 super(FakeGerritTrigger, self).__init__(*args)
402 self.upstream_root = upstream_root
403
James E. Blair4886cc12012-07-18 15:39:41 -0700404 def getGitUrl(self, project):
Monty Taylorbc758832013-06-17 17:22:42 -0400405 return os.path.join(self.upstream_root, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700406
407
James E. Blair412e5582013-04-22 15:50:12 -0700408class FakeStatsd(threading.Thread):
409 def __init__(self):
410 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400411 self.daemon = True
James E. Blair412e5582013-04-22 15:50:12 -0700412 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
413 self.sock.bind(('', 0))
414 self.port = self.sock.getsockname()[1]
415 self.wake_read, self.wake_write = os.pipe()
416 self.stats = []
417
418 def run(self):
419 while True:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700420 poll = select.poll()
421 poll.register(self.sock, select.POLLIN)
422 poll.register(self.wake_read, select.POLLIN)
423 ret = poll.poll()
424 for (fd, event) in ret:
425 if fd == self.sock.fileno():
James E. Blair412e5582013-04-22 15:50:12 -0700426 data = self.sock.recvfrom(1024)
427 if not data:
428 return
429 self.stats.append(data[0])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700430 if fd == self.wake_read:
James E. Blair412e5582013-04-22 15:50:12 -0700431 return
432
433 def stop(self):
434 os.write(self.wake_write, '1\n')
435
436
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700437class FakeBuild(threading.Thread):
438 log = logging.getLogger("zuul.test")
439
440 def __init__(self, worker, job, number, node):
441 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400442 self.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700443 self.worker = worker
444 self.job = job
445 self.name = job.name.split(':')[1]
446 self.number = number
447 self.node = node
448 self.parameters = json.loads(job.arguments)
449 self.unique = self.parameters['ZUUL_UUID']
450 self.wait_condition = threading.Condition()
451 self.waiting = False
452 self.aborted = False
453 self.created = time.time()
454 self.description = ''
James E. Blair4a28a882013-08-23 15:17:33 -0700455 self.run_error = False
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700456
457 def release(self):
458 self.wait_condition.acquire()
459 self.wait_condition.notify()
460 self.waiting = False
461 self.log.debug("Build %s released" % self.unique)
462 self.wait_condition.release()
463
464 def isWaiting(self):
465 self.wait_condition.acquire()
466 if self.waiting:
467 ret = True
468 else:
469 ret = False
470 self.wait_condition.release()
471 return ret
472
473 def _wait(self):
474 self.wait_condition.acquire()
475 self.waiting = True
476 self.log.debug("Build %s waiting" % self.unique)
477 self.wait_condition.wait()
478 self.wait_condition.release()
479
480 def run(self):
481 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700482 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
483 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700484 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700485 'manager': self.worker.worker_id,
Paul Belangerec49b4c2013-07-20 20:32:20 -0400486 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700487
488 self.job.sendWorkData(json.dumps(data))
489 self.job.sendWorkStatus(0, 100)
490
491 if self.worker.hold_jobs_in_build:
492 self._wait()
493 self.log.debug("Build %s continuing" % self.unique)
494
495 self.worker.lock.acquire()
496
497 result = 'SUCCESS'
498 if (('ZUUL_REF' in self.parameters) and
499 self.worker.shouldFailTest(self.name,
500 self.parameters['ZUUL_REF'])):
501 result = 'FAILURE'
502 if self.aborted:
503 result = 'ABORTED'
504
James E. Blair4a28a882013-08-23 15:17:33 -0700505 if self.run_error:
506 work_fail = True
507 result = 'RUN_ERROR'
508 else:
509 data['result'] = result
510 work_fail = False
511
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700512 changes = None
513 if 'ZUUL_CHANGE_IDS' in self.parameters:
514 changes = self.parameters['ZUUL_CHANGE_IDS']
515
516 self.worker.build_history.append(
517 BuildHistory(name=self.name, number=self.number,
518 result=result, changes=changes, node=self.node,
James E. Blair64ed6f22013-07-10 14:07:23 -0700519 uuid=self.unique, description=self.description,
520 pipeline=self.parameters['ZUUL_PIPELINE'])
Paul Belangerec49b4c2013-07-20 20:32:20 -0400521 )
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700522
James E. Blair4a28a882013-08-23 15:17:33 -0700523 self.job.sendWorkData(json.dumps(data))
524 if work_fail:
525 self.job.sendWorkFail()
526 else:
527 self.job.sendWorkComplete(json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700528 del self.worker.gearman_jobs[self.job.unique]
529 self.worker.running_builds.remove(self)
530 self.worker.lock.release()
531
532
533class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400534 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700535 super(FakeWorker, self).__init__(worker_id)
536 self.gearman_jobs = {}
537 self.build_history = []
538 self.running_builds = []
539 self.build_counter = 0
540 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400541 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700542
543 self.hold_jobs_in_build = False
544 self.lock = threading.Lock()
545 self.__work_thread = threading.Thread(target=self.work)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400546 self.__work_thread.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700547 self.__work_thread.start()
548
549 def handleJob(self, job):
550 parts = job.name.split(":")
551 cmd = parts[0]
552 name = parts[1]
553 if len(parts) > 2:
554 node = parts[2]
555 else:
556 node = None
557 if cmd == 'build':
558 self.handleBuild(job, name, node)
559 elif cmd == 'stop':
560 self.handleStop(job, name)
561 elif cmd == 'set_description':
562 self.handleSetDescription(job, name)
563
564 def handleBuild(self, job, name, node):
565 build = FakeBuild(self, job, self.build_counter, node)
566 job.build = build
567 self.gearman_jobs[job.unique] = job
568 self.build_counter += 1
569
570 self.running_builds.append(build)
571 build.start()
572
573 def handleStop(self, job, name):
574 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700575 parameters = json.loads(job.arguments)
576 name = parameters['name']
577 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700578 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700579 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700580 build.aborted = True
581 build.release()
582 job.sendWorkComplete()
583 return
584 job.sendWorkFail()
585
586 def handleSetDescription(self, job, name):
587 self.log.debug("handle set description")
588 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700589 name = parameters['name']
590 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700591 descr = parameters['html_description']
592 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700593 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700594 build.description = descr
595 job.sendWorkComplete()
596 return
597 for build in self.build_history:
James E. Blair3c483cf2013-06-04 16:30:43 -0700598 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700599 build.description = descr
600 job.sendWorkComplete()
601 return
602 job.sendWorkFail()
603
604 def work(self):
605 while self.running:
606 try:
607 job = self.getJob()
608 except gear.InterruptedError:
609 continue
610 try:
611 self.handleJob(job)
612 except:
613 self.log.exception("Worker exception:")
614
615 def addFailTest(self, name, change):
616 l = self.fail_tests.get(name, [])
617 l.append(change)
618 self.fail_tests[name] = l
619
620 def shouldFailTest(self, name, ref):
621 l = self.fail_tests.get(name, [])
622 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400623 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700624 return True
625 return False
626
627 def release(self, regex=None):
628 builds = self.running_builds[:]
629 self.log.debug("releasing build %s (%s)" % (regex,
James E. Blair78e31b32013-07-09 09:11:34 -0700630 len(self.running_builds)))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700631 for build in builds:
632 if not regex or re.match(regex, build.name):
633 self.log.debug("releasing build %s" %
634 (build.parameters['ZUUL_UUID']))
635 build.release()
636 else:
637 self.log.debug("not releasing build %s" %
638 (build.parameters['ZUUL_UUID']))
639 self.log.debug("done releasing builds %s (%s)" %
640 (regex, len(self.running_builds)))
641
642
643class FakeGearmanServer(gear.Server):
644 def __init__(self):
645 self.hold_jobs_in_queue = False
646 super(FakeGearmanServer, self).__init__(0)
647
648 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700649 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
650 for job in queue:
651 if not hasattr(job, 'waiting'):
652 if job.name.startswith('build:'):
653 job.waiting = self.hold_jobs_in_queue
654 else:
655 job.waiting = False
656 if job.waiting:
657 continue
658 if job.name in connection.functions:
659 if not peek:
660 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400661 connection.related_jobs[job.handle] = job
662 job.worker_connection = connection
663 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700664 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700665 return None
666
667 def release(self, regex=None):
668 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700669 qlen = (len(self.high_queue) + len(self.normal_queue) +
670 len(self.low_queue))
671 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
James E. Blairdda6c912013-07-29 14:12:12 -0700672 for job in self.getQueue():
673 cmd, name = job.name.split(':')
674 if cmd != 'build':
675 continue
676 if not regex or re.match(regex, name):
677 self.log.debug("releasing queued job %s" %
678 job.unique)
679 job.waiting = False
680 released = True
681 else:
682 self.log.debug("not releasing queued job %s" %
683 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700684 if released:
685 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700686 qlen = (len(self.high_queue) + len(self.normal_queue) +
687 len(self.low_queue))
688 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700689
690
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000691class FakeSMTP(object):
692 log = logging.getLogger('zuul.FakeSMTP')
693 messages = []
694
695 def __init__(self, server, port):
696 self.server = server
697 self.port = port
698
699 def sendmail(self, from_email, to_email, msg):
700 self.log.info("Sending email from %s, to %s, with msg %s" % (
701 from_email, to_email, msg))
702
703 headers = msg.split('\n\n', 1)[0]
704 body = msg.split('\n\n', 1)[1]
705
706 FakeSMTP.messages.append(dict(
707 from_email=from_email,
708 to_email=to_email,
709 msg=msg,
710 headers=headers,
711 body=body,
712 ))
713
714 return True
715
716 def quit(self):
717 return True
718
719
Monty Taylorbc758832013-06-17 17:22:42 -0400720class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700721 log = logging.getLogger("zuul.test")
722
723 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400724 super(TestScheduler, self).setUp()
725 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
726 try:
727 test_timeout = int(test_timeout)
728 except ValueError:
729 # If timeout value is invalid do not set a timeout.
730 test_timeout = 0
731 if test_timeout > 0:
732 self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
733
734 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
735 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
736 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
737 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
738 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
739 os.environ.get('OS_STDERR_CAPTURE') == '1'):
740 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
741 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Monty Taylore6a6c402013-07-02 09:25:55 -0700742 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
743 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400744 self.useFixture(fixtures.FakeLogger(
745 level=logging.DEBUG,
746 format='%(asctime)s %(name)-32s '
747 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700748 tmp_root = self.useFixture(fixtures.TempDir(
749 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400750 self.test_root = os.path.join(tmp_root, "zuul-test")
751 self.upstream_root = os.path.join(self.test_root, "upstream")
752 self.git_root = os.path.join(self.test_root, "git")
753
754 CONFIG.set('zuul', 'git_dir', self.git_root)
755 if os.path.exists(self.test_root):
756 shutil.rmtree(self.test_root)
757 os.makedirs(self.test_root)
758 os.makedirs(self.upstream_root)
759 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700760
761 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400762 self.init_repo("org/project")
763 self.init_repo("org/project1")
764 self.init_repo("org/project2")
765 self.init_repo("org/project3")
766 self.init_repo("org/one-job-project")
767 self.init_repo("org/nonvoting-project")
768 self.init_repo("org/templated-project")
James E. Blair3e98c022013-12-16 15:25:38 -0800769 self.init_repo("org/layered-project")
Monty Taylorbc758832013-06-17 17:22:42 -0400770 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700771 self.init_repo("org/conflict-project")
James E. Blair412e5582013-04-22 15:50:12 -0700772
773 self.statsd = FakeStatsd()
774 os.environ['STATSD_HOST'] = 'localhost'
775 os.environ['STATSD_PORT'] = str(self.statsd.port)
776 self.statsd.start()
777 # the statsd client object is configured in the statsd module import
778 reload(statsd)
779 reload(zuul.scheduler)
780
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700781 self.gearman_server = FakeGearmanServer()
782
783 self.config = ConfigParser.ConfigParser()
784 cfg = StringIO()
785 CONFIG.write(cfg)
786 cfg.seek(0)
787 self.config.readfp(cfg)
788 self.config.set('gearman', 'port', str(self.gearman_server.port))
789
Monty Taylorbc758832013-06-17 17:22:42 -0400790 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700791 self.worker.addServer('127.0.0.1', self.gearman_server.port)
792 self.gearman_server.worker = self.worker
793
James E. Blairb0fcae42012-07-17 11:12:10 -0700794 self.sched = zuul.scheduler.Scheduler()
795
James E. Blair8cc15a82012-08-01 11:17:57 -0700796 def URLOpenerFactory(*args, **kw):
797 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400798 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700799
James E. Blair8cc15a82012-08-01 11:17:57 -0700800 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700801 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700802
803 zuul.lib.gerrit.Gerrit = FakeGerrit
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000804 self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTP))
James E. Blairb0fcae42012-07-17 11:12:10 -0700805
Monty Taylorbc758832013-06-17 17:22:42 -0400806 self.gerrit = FakeGerritTrigger(
807 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700808 self.gerrit.replication_timeout = 1.5
809 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700810 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400811 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700812
James E. Blair1843a552013-07-03 14:19:52 -0700813 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
James E. Blairad28e912013-11-27 10:43:22 -0800814 self.rpc = zuul.rpclistener.RPCListener(self.config, self.sched)
James E. Blair1843a552013-07-03 14:19:52 -0700815
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700816 self.sched.setLauncher(self.launcher)
James E. Blair6c358e72013-07-29 17:06:47 -0700817 self.sched.registerTrigger(self.gerrit)
James E. Blair63bb0ef2013-07-29 17:14:51 -0700818 self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
819 self.sched.registerTrigger(self.timer)
James E. Blairb0fcae42012-07-17 11:12:10 -0700820
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000821 self.sched.registerReporter(
822 zuul.reporter.gerrit.Reporter(self.gerrit))
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000823 self.smtp_reporter = zuul.reporter.smtp.Reporter(
824 self.config.get('smtp', 'default_from'),
825 self.config.get('smtp', 'default_to'),
826 self.config.get('smtp', 'server'))
827 self.sched.registerReporter(self.smtp_reporter)
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000828
James E. Blairb0fcae42012-07-17 11:12:10 -0700829 self.sched.start()
830 self.sched.reconfigure(self.config)
831 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700832 self.webapp.start()
James E. Blairad28e912013-11-27 10:43:22 -0800833 self.rpc.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700834 self.launcher.gearman.waitForServer()
835 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400836 self.builds = self.worker.running_builds
837 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700838
James E. Blairfee8d652013-06-07 08:57:52 -0700839 self.addCleanup(self.assertFinalState)
840 self.addCleanup(self.shutdown)
841
842 def assertFinalState(self):
843 # Make sure that the change cache is cleared
James E. Blair6c358e72013-07-29 17:06:47 -0700844 self.assertEqual(len(self.gerrit._change_cache.keys()), 0)
Clark Boylan4ba48d92013-11-11 18:03:53 -0800845 # Make sure that git.Repo objects have been garbage collected.
846 repos = []
847 gc.collect()
848 for obj in gc.get_objects():
849 if isinstance(obj, git.Repo):
850 repos.append(obj)
851 self.assertEqual(len(repos), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700852 self.assertEmptyQueues()
853
854 def shutdown(self):
855 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700856 self.launcher.stop()
857 self.worker.shutdown()
858 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700859 self.gerrit.stop()
James E. Blair63bb0ef2013-07-29 17:14:51 -0700860 self.timer.stop()
James E. Blairb0fcae42012-07-17 11:12:10 -0700861 self.sched.stop()
862 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700863 self.statsd.stop()
864 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700865 self.webapp.stop()
866 self.webapp.join()
James E. Blairad28e912013-11-27 10:43:22 -0800867 self.rpc.stop()
868 self.rpc.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700869 threads = threading.enumerate()
870 if len(threads) > 1:
871 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400872 super(TestScheduler, self).tearDown()
873
874 def init_repo(self, project):
875 parts = project.split('/')
876 path = os.path.join(self.upstream_root, *parts[:-1])
877 if not os.path.exists(path):
878 os.makedirs(path)
879 path = os.path.join(self.upstream_root, project)
880 repo = git.Repo.init(path)
881
882 repo.config_writer().set_value('user', 'email', 'user@example.com')
883 repo.config_writer().set_value('user', 'name', 'User Name')
884 repo.config_writer().write()
885
886 fn = os.path.join(path, 'README')
887 f = open(fn, 'w')
888 f.write("test\n")
889 f.close()
890 repo.index.add([fn])
891 repo.index.commit('initial commit')
892 master = repo.create_head('master')
893 repo.create_tag('init')
894
895 mp = repo.create_head('mp')
896 repo.head.reference = mp
897 f = open(fn, 'a')
898 f.write("test mp\n")
899 f.close()
900 repo.index.add([fn])
901 repo.index.commit('mp commit')
902
903 repo.head.reference = master
904 repo.head.reset(index=True, working_tree=True)
905 repo.git.clean('-x', '-f', '-d')
906
907 def ref_has_change(self, ref, change):
908 path = os.path.join(self.git_root, change.project)
909 repo = git.Repo(path)
910 for commit in repo.iter_commits(ref):
911 if commit.message.strip() == ('%s-1' % change.subject):
912 return True
913 return False
914
915 def job_has_changes(self, *args):
916 job = args[0]
917 commits = args[1:]
918 if isinstance(job, FakeBuild):
919 parameters = job.parameters
920 else:
921 parameters = json.loads(job.arguments)
922 project = parameters['ZUUL_PROJECT']
923 path = os.path.join(self.git_root, project)
924 repo = git.Repo(path)
925 ref = parameters['ZUUL_REF']
926 sha = parameters['ZUUL_COMMIT']
927 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
928 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
929 commit_messages = ['%s-1' % commit.subject for commit in commits]
930 for msg in commit_messages:
931 if msg not in repo_messages:
932 return False
933 if repo_shas[0] != sha:
934 return False
935 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700936
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700937 def registerJobs(self):
938 count = 0
James E. Blaireff88162013-07-01 12:44:14 -0400939 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700940 self.worker.registerFunction('build:' + job)
941 count += 1
942 self.worker.registerFunction('stop:' + self.worker.worker_id)
943 count += 1
944
945 while len(self.gearman_server.functions) < count:
946 time.sleep(0)
947
948 def release(self, job):
949 if isinstance(job, FakeBuild):
950 job.release()
951 else:
952 job.waiting = False
953 self.log.debug("Queued job %s released" % job.unique)
954 self.gearman_server.wakeConnections()
955
956 def getParameter(self, job, name):
957 if isinstance(job, FakeBuild):
958 return job.parameters[name]
959 else:
960 parameters = json.loads(job.arguments)
961 return parameters[name]
962
963 def resetGearmanServer(self):
964 self.worker.setFunctions([])
965 while True:
966 done = True
967 for connection in self.gearman_server.active_connections:
James E. Blairad28e912013-11-27 10:43:22 -0800968 if (connection.functions and
969 connection.client_id != 'Zuul RPC Listener'):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700970 done = False
971 if done:
972 break
973 time.sleep(0)
974 self.gearman_server.functions = set()
James E. Blairad28e912013-11-27 10:43:22 -0800975 self.rpc.register()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700976
977 def haveAllBuildsReported(self):
978 # See if Zuul is waiting on a meta job to complete
979 if self.launcher.meta_jobs:
980 return False
981 # Find out if every build that the worker has completed has been
982 # reported back to Zuul. If it hasn't then that means a Gearman
983 # event is still in transit and the system is not stable.
984 for build in self.worker.build_history:
985 zbuild = self.launcher.builds.get(build.uuid)
986 if not zbuild:
987 # It has already been reported
988 continue
989 # It hasn't been reported yet.
990 return False
991 # Make sure that none of the worker connections are in GRAB_WAIT
992 for connection in self.worker.active_connections:
993 if connection.state == 'GRAB_WAIT':
994 return False
995 return True
996
997 def areAllBuildsWaiting(self):
998 ret = True
999
1000 builds = self.launcher.builds.values()
1001 for build in builds:
1002 client_job = None
1003 for conn in self.launcher.gearman.active_connections:
1004 for j in conn.related_jobs.values():
1005 if j.unique == build.uuid:
1006 client_job = j
1007 break
1008 if not client_job:
1009 self.log.debug("%s is not known to the gearman client" %
1010 build)
1011 ret = False
1012 continue
1013 if not client_job.handle:
1014 self.log.debug("%s has no handle" % client_job)
1015 ret = False
1016 continue
1017 server_job = self.gearman_server.jobs.get(client_job.handle)
1018 if not server_job:
1019 self.log.debug("%s is not known to the gearman server" %
1020 client_job)
1021 ret = False
1022 continue
1023 if not hasattr(server_job, 'waiting'):
1024 self.log.debug("%s is being enqueued" % server_job)
1025 ret = False
1026 continue
1027 if server_job.waiting:
1028 continue
1029 worker_job = self.worker.gearman_jobs.get(server_job.unique)
1030 if worker_job:
1031 if worker_job.build.isWaiting():
1032 continue
1033 else:
1034 self.log.debug("%s is running" % worker_job)
1035 ret = False
1036 else:
1037 self.log.debug("%s is unassigned" % server_job)
1038 ret = False
1039 return ret
1040
James E. Blairb0fcae42012-07-17 11:12:10 -07001041 def waitUntilSettled(self):
1042 self.log.debug("Waiting until settled...")
1043 start = time.time()
1044 while True:
1045 if time.time() - start > 10:
1046 print 'queue status:',
1047 print self.sched.trigger_event_queue.empty(),
1048 print self.sched.result_event_queue.empty(),
1049 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001050 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -07001051 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001052 # Make sure no new events show up while we're checking
1053 self.worker.lock.acquire()
1054 # have all build states propogated to zuul?
1055 if self.haveAllBuildsReported():
1056 # Join ensures that the queue is empty _and_ events have been
1057 # processed
1058 self.fake_gerrit.event_queue.join()
1059 self.sched.trigger_event_queue.join()
1060 self.sched.result_event_queue.join()
1061 if (self.sched.trigger_event_queue.empty() and
1062 self.sched.result_event_queue.empty() and
1063 self.fake_gerrit.event_queue.empty() and
1064 self.areAllBuildsWaiting()):
1065 self.worker.lock.release()
1066 self.log.debug("...settled.")
1067 return
1068 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001069 self.sched.wake_event.wait(0.1)
1070
James E. Blaird466dc42012-07-31 10:42:56 -07001071 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -08001072 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -07001073 return len(jobs)
1074
James E. Blair4ca985f2013-05-30 12:27:43 -07001075 def getJobFromHistory(self, name):
1076 history = self.worker.build_history
1077 for job in history:
1078 if job.name == name:
1079 return job
1080 raise Exception("Unable to find job %s in history" % name)
1081
James E. Blaire0487072012-08-29 17:38:31 -07001082 def assertEmptyQueues(self):
1083 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001084 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001085 for queue in pipeline.queues:
1086 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001087 print 'pipeline %s queue %s contents %s' % (
1088 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001089 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001090
James E. Blair66eeebf2013-07-27 17:44:32 -07001091 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001092 start = time.time()
1093 while time.time() < (start + 5):
1094 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001095 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001096 k, v = stat.split(':')
1097 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001098 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001099 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001100 elif value:
1101 if value == v:
1102 return
1103 elif kind:
1104 if v.endswith('|' + kind):
1105 return
James E. Blair412e5582013-04-22 15:50:12 -07001106 time.sleep(0.1)
1107
1108 pprint.pprint(self.statsd.stats)
1109 raise Exception("Key %s not found in reported stats" % key)
1110
James E. Blairb0fcae42012-07-17 11:12:10 -07001111 def test_jobs_launched(self):
1112 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001113
James E. Blairb0fcae42012-07-17 11:12:10 -07001114 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001115 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001116 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1117 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001118 self.assertEqual(self.getJobFromHistory('project-merge').result,
1119 'SUCCESS')
1120 self.assertEqual(self.getJobFromHistory('project-test1').result,
1121 'SUCCESS')
1122 self.assertEqual(self.getJobFromHistory('project-test2').result,
1123 'SUCCESS')
1124 self.assertEqual(A.data['status'], 'MERGED')
1125 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001126
James E. Blair66eeebf2013-07-27 17:44:32 -07001127 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1128 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1129 value='1|g')
1130 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1131 kind='ms')
1132 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1133 value='1|c')
1134 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1135 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1136 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001137 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001138 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001139 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001140 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001141
James E. Blair3cb10702013-08-24 08:56:03 -07001142 def test_initial_pipeline_gauges(self):
1143 "Test that each pipeline reported its length on start"
1144 pipeline_names = self.sched.layout.pipelines.keys()
1145 self.assertNotEqual(len(pipeline_names), 0)
1146 for name in pipeline_names:
1147 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
1148 value='0|g')
1149
James E. Blair42f74822013-05-14 15:18:03 -07001150 def test_duplicate_pipelines(self):
1151 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001152
James E. Blair42f74822013-05-14 15:18:03 -07001153 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1154 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1155 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001156
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001157 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001158 self.history[0].name == 'project-test1'
1159 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001160
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001161 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001162 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001163 self.assertIn('dup1/project-test1', A.messages[0])
1164 self.assertNotIn('dup2/project-test1', A.messages[0])
1165 self.assertNotIn('dup1/project-test1', A.messages[1])
1166 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001167 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001168 self.assertIn('dup1/project-test1', A.messages[1])
1169 self.assertNotIn('dup2/project-test1', A.messages[1])
1170 self.assertNotIn('dup1/project-test1', A.messages[0])
1171 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001172
James E. Blairb0fcae42012-07-17 11:12:10 -07001173 def test_parallel_changes(self):
1174 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001175
1176 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001177 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1178 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1179 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001180 A.addApproval('CRVW', 2)
1181 B.addApproval('CRVW', 2)
1182 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001183
1184 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1185 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1186 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1187
1188 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001189 self.assertEqual(len(self.builds), 1)
1190 self.assertEqual(self.builds[0].name, 'project-merge')
1191 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001192
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001193 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001194 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001195 self.assertEqual(len(self.builds), 3)
1196 self.assertEqual(self.builds[0].name, 'project-test1')
1197 self.assertTrue(self.job_has_changes(self.builds[0], A))
1198 self.assertEqual(self.builds[1].name, 'project-test2')
1199 self.assertTrue(self.job_has_changes(self.builds[1], A))
1200 self.assertEqual(self.builds[2].name, 'project-merge')
1201 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001202
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001203 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001204 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001205 self.assertEqual(len(self.builds), 5)
1206 self.assertEqual(self.builds[0].name, 'project-test1')
1207 self.assertTrue(self.job_has_changes(self.builds[0], A))
1208 self.assertEqual(self.builds[1].name, 'project-test2')
1209 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001210
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001211 self.assertEqual(self.builds[2].name, 'project-test1')
1212 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1213 self.assertEqual(self.builds[3].name, 'project-test2')
1214 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001215
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001216 self.assertEqual(self.builds[4].name, 'project-merge')
1217 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001218
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001219 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001220 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001221 self.assertEqual(len(self.builds), 6)
1222 self.assertEqual(self.builds[0].name, 'project-test1')
1223 self.assertTrue(self.job_has_changes(self.builds[0], A))
1224 self.assertEqual(self.builds[1].name, 'project-test2')
1225 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001226
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001227 self.assertEqual(self.builds[2].name, 'project-test1')
1228 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1229 self.assertEqual(self.builds[3].name, 'project-test2')
1230 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001231
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001232 self.assertEqual(self.builds[4].name, 'project-test1')
1233 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1234 self.assertEqual(self.builds[5].name, 'project-test2')
1235 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001236
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001237 self.worker.hold_jobs_in_build = False
1238 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001239 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001240 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001241
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001242 self.assertEqual(len(self.history), 9)
1243 self.assertEqual(A.data['status'], 'MERGED')
1244 self.assertEqual(B.data['status'], 'MERGED')
1245 self.assertEqual(C.data['status'], 'MERGED')
1246 self.assertEqual(A.reported, 2)
1247 self.assertEqual(B.reported, 2)
1248 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001249
1250 def test_failed_changes(self):
1251 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001252 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001253
James E. Blairb02a3bb2012-07-30 17:49:55 -07001254 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1255 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001256 A.addApproval('CRVW', 2)
1257 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001258
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001259 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001260
James E. Blaire2819012013-06-28 17:17:26 -04001261 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1262 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001263 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001264
1265 self.worker.release('.*-merge')
1266 self.waitUntilSettled()
1267
1268 self.worker.hold_jobs_in_build = False
1269 self.worker.release()
1270
1271 self.waitUntilSettled()
1272 # It's certain that the merge job for change 2 will run, but
1273 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001274 self.assertTrue(len(self.history) > 6)
1275 self.assertEqual(A.data['status'], 'NEW')
1276 self.assertEqual(B.data['status'], 'MERGED')
1277 self.assertEqual(A.reported, 2)
1278 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001279
1280 def test_independent_queues(self):
1281 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001282
1283 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001284 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001285 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1286 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001287 A.addApproval('CRVW', 2)
1288 B.addApproval('CRVW', 2)
1289 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001290
1291 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1292 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1293 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1294
James E. Blairb02a3bb2012-07-30 17:49:55 -07001295 self.waitUntilSettled()
1296
1297 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001298 self.assertEqual(len(self.builds), 2)
1299 self.assertEqual(self.builds[0].name, 'project-merge')
1300 self.assertTrue(self.job_has_changes(self.builds[0], A))
1301 self.assertEqual(self.builds[1].name, 'project1-merge')
1302 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001303
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001304 # Release the current merge builds
1305 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001306 self.waitUntilSettled()
1307 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001308 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001309 self.waitUntilSettled()
1310
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001311 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001312 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001313 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001314
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001315 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001316 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001317 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001318
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001319 self.assertEqual(len(self.history), 11)
1320 self.assertEqual(A.data['status'], 'MERGED')
1321 self.assertEqual(B.data['status'], 'MERGED')
1322 self.assertEqual(C.data['status'], 'MERGED')
1323 self.assertEqual(A.reported, 2)
1324 self.assertEqual(B.reported, 2)
1325 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001326
1327 def test_failed_change_at_head(self):
1328 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001329
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001330 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001331 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1332 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1333 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001334 A.addApproval('CRVW', 2)
1335 B.addApproval('CRVW', 2)
1336 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001337
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001338 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001339
1340 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1341 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1342 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1343
1344 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001345
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001346 self.assertEqual(len(self.builds), 1)
1347 self.assertEqual(self.builds[0].name, 'project-merge')
1348 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001349
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001350 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001351 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001352 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001353 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001354 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001355 self.waitUntilSettled()
1356
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001357 self.assertEqual(len(self.builds), 6)
1358 self.assertEqual(self.builds[0].name, 'project-test1')
1359 self.assertEqual(self.builds[1].name, 'project-test2')
1360 self.assertEqual(self.builds[2].name, 'project-test1')
1361 self.assertEqual(self.builds[3].name, 'project-test2')
1362 self.assertEqual(self.builds[4].name, 'project-test1')
1363 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001364
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001365 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001366 self.waitUntilSettled()
1367
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001368 # project-test2, project-merge for B
1369 self.assertEqual(len(self.builds), 2)
1370 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001371
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001372 self.worker.hold_jobs_in_build = False
1373 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001374 self.waitUntilSettled()
1375
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001376 self.assertEqual(len(self.builds), 0)
1377 self.assertEqual(len(self.history), 15)
1378 self.assertEqual(A.data['status'], 'NEW')
1379 self.assertEqual(B.data['status'], 'MERGED')
1380 self.assertEqual(C.data['status'], 'MERGED')
1381 self.assertEqual(A.reported, 2)
1382 self.assertEqual(B.reported, 2)
1383 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001384
James E. Blair0aac4872013-08-23 14:02:38 -07001385 def test_failed_change_in_middle(self):
1386 "Test a failed change in the middle of the queue"
1387
1388 self.worker.hold_jobs_in_build = True
1389 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1390 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1391 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1392 A.addApproval('CRVW', 2)
1393 B.addApproval('CRVW', 2)
1394 C.addApproval('CRVW', 2)
1395
1396 self.worker.addFailTest('project-test1', B)
1397
1398 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1399 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1400 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1401
1402 self.waitUntilSettled()
1403
1404 self.worker.release('.*-merge')
1405 self.waitUntilSettled()
1406 self.worker.release('.*-merge')
1407 self.waitUntilSettled()
1408 self.worker.release('.*-merge')
1409 self.waitUntilSettled()
1410
1411 self.assertEqual(len(self.builds), 6)
1412 self.assertEqual(self.builds[0].name, 'project-test1')
1413 self.assertEqual(self.builds[1].name, 'project-test2')
1414 self.assertEqual(self.builds[2].name, 'project-test1')
1415 self.assertEqual(self.builds[3].name, 'project-test2')
1416 self.assertEqual(self.builds[4].name, 'project-test1')
1417 self.assertEqual(self.builds[5].name, 'project-test2')
1418
1419 self.release(self.builds[2])
1420 self.waitUntilSettled()
1421
James E. Blair972e3c72013-08-29 12:04:55 -07001422 # project-test1 and project-test2 for A
1423 # project-test2 for B
1424 # project-merge for C (without B)
1425 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -07001426 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1427
James E. Blair972e3c72013-08-29 12:04:55 -07001428 self.worker.release('.*-merge')
1429 self.waitUntilSettled()
1430
1431 # project-test1 and project-test2 for A
1432 # project-test2 for B
1433 # project-test1 and project-test2 for C
1434 self.assertEqual(len(self.builds), 5)
1435
James E. Blair0aac4872013-08-23 14:02:38 -07001436 items = self.sched.layout.pipelines['gate'].getAllItems()
1437 builds = items[0].current_build_set.getBuilds()
1438 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1439 self.assertEqual(self.countJobResults(builds, None), 2)
1440 builds = items[1].current_build_set.getBuilds()
1441 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1442 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1443 self.assertEqual(self.countJobResults(builds, None), 1)
1444 builds = items[2].current_build_set.getBuilds()
1445 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -07001446 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -07001447
1448 self.worker.hold_jobs_in_build = False
1449 self.worker.release()
1450 self.waitUntilSettled()
1451
1452 self.assertEqual(len(self.builds), 0)
1453 self.assertEqual(len(self.history), 12)
1454 self.assertEqual(A.data['status'], 'MERGED')
1455 self.assertEqual(B.data['status'], 'NEW')
1456 self.assertEqual(C.data['status'], 'MERGED')
1457 self.assertEqual(A.reported, 2)
1458 self.assertEqual(B.reported, 2)
1459 self.assertEqual(C.reported, 2)
1460
James E. Blaird466dc42012-07-31 10:42:56 -07001461 def test_failed_change_at_head_with_queue(self):
1462 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001463
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001464 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001465 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1466 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1467 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001468 A.addApproval('CRVW', 2)
1469 B.addApproval('CRVW', 2)
1470 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001471
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001472 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001473
1474 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1475 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1476 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1477
1478 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001479 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001480 self.assertEqual(len(self.builds), 0)
1481 self.assertEqual(len(queue), 1)
1482 self.assertEqual(queue[0].name, 'build:project-merge')
1483 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001484
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001485 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001486 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001487 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001488 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001489 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001490 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001491 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001492
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001493 self.assertEqual(len(self.builds), 0)
1494 self.assertEqual(len(queue), 6)
1495 self.assertEqual(queue[0].name, 'build:project-test1')
1496 self.assertEqual(queue[1].name, 'build:project-test2')
1497 self.assertEqual(queue[2].name, 'build:project-test1')
1498 self.assertEqual(queue[3].name, 'build:project-test2')
1499 self.assertEqual(queue[4].name, 'build:project-test1')
1500 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001501
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001502 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001503 self.waitUntilSettled()
1504
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001505 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001506 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001507 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1508 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001509
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001510 self.gearman_server.hold_jobs_in_queue = False
1511 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001512 self.waitUntilSettled()
1513
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001514 self.assertEqual(len(self.builds), 0)
1515 self.assertEqual(len(self.history), 11)
1516 self.assertEqual(A.data['status'], 'NEW')
1517 self.assertEqual(B.data['status'], 'MERGED')
1518 self.assertEqual(C.data['status'], 'MERGED')
1519 self.assertEqual(A.reported, 2)
1520 self.assertEqual(B.reported, 2)
1521 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001522
James E. Blairfef71632013-09-23 11:15:47 -07001523 def test_two_failed_changes_at_head(self):
1524 "Test that changes are reparented correctly if 2 fail at head"
1525
1526 self.worker.hold_jobs_in_build = True
1527 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1528 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1529 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1530 A.addApproval('CRVW', 2)
1531 B.addApproval('CRVW', 2)
1532 C.addApproval('CRVW', 2)
1533
1534 self.worker.addFailTest('project-test1', A)
1535 self.worker.addFailTest('project-test1', B)
1536
1537 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1538 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1539 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1540 self.waitUntilSettled()
1541
1542 self.worker.release('.*-merge')
1543 self.waitUntilSettled()
1544 self.worker.release('.*-merge')
1545 self.waitUntilSettled()
1546 self.worker.release('.*-merge')
1547 self.waitUntilSettled()
1548
1549 self.assertEqual(len(self.builds), 6)
1550 self.assertEqual(self.builds[0].name, 'project-test1')
1551 self.assertEqual(self.builds[1].name, 'project-test2')
1552 self.assertEqual(self.builds[2].name, 'project-test1')
1553 self.assertEqual(self.builds[3].name, 'project-test2')
1554 self.assertEqual(self.builds[4].name, 'project-test1')
1555 self.assertEqual(self.builds[5].name, 'project-test2')
1556
1557 self.assertTrue(self.job_has_changes(self.builds[0], A))
1558 self.assertTrue(self.job_has_changes(self.builds[2], A))
1559 self.assertTrue(self.job_has_changes(self.builds[2], B))
1560 self.assertTrue(self.job_has_changes(self.builds[4], A))
1561 self.assertTrue(self.job_has_changes(self.builds[4], B))
1562 self.assertTrue(self.job_has_changes(self.builds[4], C))
1563
1564 # Fail change B first
1565 self.release(self.builds[2])
1566 self.waitUntilSettled()
1567
1568 # restart of C after B failure
1569 self.worker.release('.*-merge')
1570 self.waitUntilSettled()
1571
1572 self.assertEqual(len(self.builds), 5)
1573 self.assertEqual(self.builds[0].name, 'project-test1')
1574 self.assertEqual(self.builds[1].name, 'project-test2')
1575 self.assertEqual(self.builds[2].name, 'project-test2')
1576 self.assertEqual(self.builds[3].name, 'project-test1')
1577 self.assertEqual(self.builds[4].name, 'project-test2')
1578
1579 self.assertTrue(self.job_has_changes(self.builds[1], A))
1580 self.assertTrue(self.job_has_changes(self.builds[2], A))
1581 self.assertTrue(self.job_has_changes(self.builds[2], B))
1582 self.assertTrue(self.job_has_changes(self.builds[4], A))
1583 self.assertFalse(self.job_has_changes(self.builds[4], B))
1584 self.assertTrue(self.job_has_changes(self.builds[4], C))
1585
1586 # Finish running all passing jobs for change A
1587 self.release(self.builds[1])
1588 self.waitUntilSettled()
1589 # Fail and report change A
1590 self.release(self.builds[0])
1591 self.waitUntilSettled()
1592
1593 # restart of B,C after A failure
1594 self.worker.release('.*-merge')
1595 self.waitUntilSettled()
1596 self.worker.release('.*-merge')
1597 self.waitUntilSettled()
1598
1599 self.assertEqual(len(self.builds), 4)
1600 self.assertEqual(self.builds[0].name, 'project-test1') # B
1601 self.assertEqual(self.builds[1].name, 'project-test2') # B
1602 self.assertEqual(self.builds[2].name, 'project-test1') # C
1603 self.assertEqual(self.builds[3].name, 'project-test2') # C
1604
1605 self.assertFalse(self.job_has_changes(self.builds[1], A))
1606 self.assertTrue(self.job_has_changes(self.builds[1], B))
1607 self.assertFalse(self.job_has_changes(self.builds[1], C))
1608
1609 self.assertFalse(self.job_has_changes(self.builds[2], A))
1610 # After A failed and B and C restarted, B should be back in
1611 # C's tests because it has not failed yet.
1612 self.assertTrue(self.job_has_changes(self.builds[2], B))
1613 self.assertTrue(self.job_has_changes(self.builds[2], C))
1614
1615 self.worker.hold_jobs_in_build = False
1616 self.worker.release()
1617 self.waitUntilSettled()
1618
1619 self.assertEqual(len(self.builds), 0)
1620 self.assertEqual(len(self.history), 21)
1621 self.assertEqual(A.data['status'], 'NEW')
1622 self.assertEqual(B.data['status'], 'NEW')
1623 self.assertEqual(C.data['status'], 'MERGED')
1624 self.assertEqual(A.reported, 2)
1625 self.assertEqual(B.reported, 2)
1626 self.assertEqual(C.reported, 2)
1627
James E. Blair8c803f82012-07-31 16:25:42 -07001628 def test_patch_order(self):
1629 "Test that dependent patches are tested in the right order"
1630 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1631 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1632 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1633 A.addApproval('CRVW', 2)
1634 B.addApproval('CRVW', 2)
1635 C.addApproval('CRVW', 2)
1636
1637 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1638 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1639 M2.setMerged()
1640 M1.setMerged()
1641
1642 # C -> B -> A -> M1 -> M2
1643 # M2 is here to make sure it is never queried. If it is, it
1644 # means zuul is walking down the entire history of merged
1645 # changes.
1646
1647 C.setDependsOn(B, 1)
1648 B.setDependsOn(A, 1)
1649 A.setDependsOn(M1, 1)
1650 M1.setDependsOn(M2, 1)
1651
1652 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1653
1654 self.waitUntilSettled()
1655
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001656 self.assertEqual(A.data['status'], 'NEW')
1657 self.assertEqual(B.data['status'], 'NEW')
1658 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001659
1660 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1661 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1662
1663 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001664 self.assertEqual(M2.queried, 0)
1665 self.assertEqual(A.data['status'], 'MERGED')
1666 self.assertEqual(B.data['status'], 'MERGED')
1667 self.assertEqual(C.data['status'], 'MERGED')
1668 self.assertEqual(A.reported, 2)
1669 self.assertEqual(B.reported, 2)
1670 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001671
James E. Blair0e933c52013-07-11 10:18:52 -07001672 def test_trigger_cache(self):
1673 "Test that the trigger cache operates correctly"
1674 self.worker.hold_jobs_in_build = True
1675
1676 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1677 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1678 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1679 A.addApproval('CRVW', 2)
1680 B.addApproval('CRVW', 2)
1681
1682 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1683 M1.setMerged()
1684
1685 B.setDependsOn(A, 1)
1686 A.setDependsOn(M1, 1)
1687
1688 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1689 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1690
1691 self.waitUntilSettled()
1692
1693 for build in self.builds:
1694 if build.parameters['ZUUL_PIPELINE'] == 'check':
1695 build.release()
1696 self.waitUntilSettled()
1697 for build in self.builds:
1698 if build.parameters['ZUUL_PIPELINE'] == 'check':
1699 build.release()
1700 self.waitUntilSettled()
1701
1702 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1703 self.waitUntilSettled()
1704
James E. Blair6c358e72013-07-29 17:06:47 -07001705 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001706 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001707 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001708
1709 self.worker.hold_jobs_in_build = False
1710 self.worker.release()
1711 self.waitUntilSettled()
1712
1713 self.assertEqual(A.data['status'], 'MERGED')
1714 self.assertEqual(B.data['status'], 'MERGED')
1715 self.assertEqual(A.queried, 2) # Initial and isMerged
1716 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1717
James E. Blair8c803f82012-07-31 16:25:42 -07001718 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001719 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001720 # TODO: move to test_gerrit (this is a unit test!)
1721 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001722 trigger = self.sched.layout.pipelines['gate'].trigger
1723 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001724 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001725 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001726
1727 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001728 a = trigger.getChange(1, 2, refresh=True)
1729 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001730
1731 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001732 a = trigger.getChange(1, 2, refresh=True)
1733 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1734 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001735
1736 def test_build_configuration(self):
1737 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001738
1739 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001740 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1741 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1742 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1743 A.addApproval('CRVW', 2)
1744 B.addApproval('CRVW', 2)
1745 C.addApproval('CRVW', 2)
1746 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1747 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1748 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1749 self.waitUntilSettled()
1750
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001751 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001752 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001753 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001754 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001755 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001756 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001757 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001758 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1759 self.gearman_server.hold_jobs_in_queue = False
1760 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001761 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001762
Monty Taylorbc758832013-06-17 17:22:42 -04001763 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001764 repo = git.Repo(path)
1765 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1766 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001767 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001768 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001769
1770 def test_build_configuration_conflict(self):
1771 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001772
1773 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001774 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1775 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001776 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001777 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1778 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001779 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001780 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1781 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001782 A.addApproval('CRVW', 2)
1783 B.addApproval('CRVW', 2)
1784 C.addApproval('CRVW', 2)
1785 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1786 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1787 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1788 self.waitUntilSettled()
1789
James E. Blair6736beb2013-07-11 15:18:15 -07001790 self.assertEqual(A.reported, 1)
1791 self.assertEqual(B.reported, 1)
1792 self.assertEqual(C.reported, 1)
1793
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001794 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001795 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001796 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001797 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001798 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001799 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001800
1801 self.assertEqual(len(self.history), 2) # A and C merge jobs
1802
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001803 self.gearman_server.hold_jobs_in_queue = False
1804 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001805 self.waitUntilSettled()
1806
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001807 self.assertEqual(A.data['status'], 'MERGED')
1808 self.assertEqual(B.data['status'], 'NEW')
1809 self.assertEqual(C.data['status'], 'MERGED')
1810 self.assertEqual(A.reported, 2)
1811 self.assertEqual(B.reported, 2)
1812 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001813 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001814
James E. Blairdaabed22012-08-15 15:38:57 -07001815 def test_post(self):
1816 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001817
Zhongyue Luo5d556072012-09-21 02:00:47 +09001818 e = {
1819 "type": "ref-updated",
1820 "submitter": {
1821 "name": "User Name",
1822 },
1823 "refUpdate": {
1824 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1825 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1826 "refName": "master",
1827 "project": "org/project",
1828 }
1829 }
James E. Blairdaabed22012-08-15 15:38:57 -07001830 self.fake_gerrit.addEvent(e)
1831 self.waitUntilSettled()
1832
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001833 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001834 self.assertEqual(len(self.history), 1)
1835 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001836
1837 def test_build_configuration_branch(self):
1838 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001839
1840 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001841 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1842 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1843 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1844 A.addApproval('CRVW', 2)
1845 B.addApproval('CRVW', 2)
1846 C.addApproval('CRVW', 2)
1847 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1848 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1849 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1850 self.waitUntilSettled()
1851
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001852 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001853 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001854 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001855 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001856 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001857 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001858 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001859 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1860 self.gearman_server.hold_jobs_in_queue = False
1861 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001862 self.waitUntilSettled()
1863
Monty Taylorbc758832013-06-17 17:22:42 -04001864 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001865 repo = git.Repo(path)
1866 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1867 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001868 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001869 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001870
1871 def test_build_configuration_branch_interaction(self):
1872 "Test that switching between branches works"
1873 self.test_build_configuration()
1874 self.test_build_configuration_branch()
1875 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001876 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001877 repo = git.Repo(path)
1878 repo.heads.master.commit = repo.commit('init')
1879 self.test_build_configuration()
1880
1881 def test_build_configuration_multi_branch(self):
1882 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001883
1884 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001885 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1886 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1887 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1888 A.addApproval('CRVW', 2)
1889 B.addApproval('CRVW', 2)
1890 C.addApproval('CRVW', 2)
1891 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1892 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1893 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1894 self.waitUntilSettled()
1895
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001896 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001897 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001898 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001899 job_B = None
1900 for job in queue:
1901 if 'project-merge' in job.name:
1902 job_B = job
1903 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001904 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001905 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001906 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001907 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001908 for job in queue:
1909 if 'project-merge' in job.name:
1910 job_C = job
1911 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001912 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001913 self.gearman_server.hold_jobs_in_queue = False
1914 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001915 self.waitUntilSettled()
1916
Monty Taylorbc758832013-06-17 17:22:42 -04001917 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001918 repo = git.Repo(path)
1919
1920 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001921 for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001922 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001923 correct_messages = ['initial commit', 'A-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001924 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001925
1926 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001927 for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001928 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001929 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001930 self.assertEqual(repo_messages, correct_messages)
James E. Blair7f71c802012-08-22 13:04:32 -07001931
1932 def test_one_job_project(self):
1933 "Test that queueing works with one job"
1934 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1935 'master', 'A')
1936 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1937 'master', 'B')
1938 A.addApproval('CRVW', 2)
1939 B.addApproval('CRVW', 2)
1940 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1941 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1942 self.waitUntilSettled()
1943
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001944 self.assertEqual(A.data['status'], 'MERGED')
1945 self.assertEqual(A.reported, 2)
1946 self.assertEqual(B.data['status'], 'MERGED')
1947 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001948
Antoine Musso80edd5a2013-02-13 15:37:53 +01001949 def test_job_from_templates_launched(self):
1950 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001951
Antoine Musso80edd5a2013-02-13 15:37:53 +01001952 A = self.fake_gerrit.addFakeChange(
1953 'org/templated-project', 'master', 'A')
1954 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1955 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001956
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001957 self.assertEqual(self.getJobFromHistory('project-test1').result,
1958 'SUCCESS')
1959 self.assertEqual(self.getJobFromHistory('project-test2').result,
1960 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001961
James E. Blair3e98c022013-12-16 15:25:38 -08001962 def test_layered_templates(self):
1963 "Test whether a job generated via a template can be launched"
1964
1965 A = self.fake_gerrit.addFakeChange(
1966 'org/layered-project', 'master', 'A')
1967 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1968 self.waitUntilSettled()
1969
1970 self.assertEqual(self.getJobFromHistory('project-test1').result,
1971 'SUCCESS')
1972 self.assertEqual(self.getJobFromHistory('project-test2').result,
1973 'SUCCESS')
1974 self.assertEqual(self.getJobFromHistory('project-test3').result,
1975 'SUCCESS')
1976 self.assertEqual(self.getJobFromHistory('project-test4').result,
1977 'SUCCESS')
1978 # project-test5 should run twice because two templates define it
1979 test5_count = 0
1980 for job in self.worker.build_history:
1981 if job.name == 'project-test5':
1982 test5_count += 1
1983 self.assertEqual(job.result, 'SUCCESS')
1984 self.assertEqual(test5_count, 2)
1985 self.assertEqual(self.getJobFromHistory('project-test6').result,
1986 'SUCCESS')
1987
James E. Blaircaec0c52012-08-22 14:52:22 -07001988 def test_dependent_changes_dequeue(self):
1989 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001990
James E. Blaircaec0c52012-08-22 14:52:22 -07001991 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1992 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1993 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1994 A.addApproval('CRVW', 2)
1995 B.addApproval('CRVW', 2)
1996 C.addApproval('CRVW', 2)
1997
1998 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1999 M1.setMerged()
2000
2001 # C -> B -> A -> M1
2002
2003 C.setDependsOn(B, 1)
2004 B.setDependsOn(A, 1)
2005 A.setDependsOn(M1, 1)
2006
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002007 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07002008
2009 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2010 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2011 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2012
2013 self.waitUntilSettled()
2014
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002015 self.assertEqual(A.data['status'], 'NEW')
2016 self.assertEqual(A.reported, 2)
2017 self.assertEqual(B.data['status'], 'NEW')
2018 self.assertEqual(B.reported, 2)
2019 self.assertEqual(C.data['status'], 'NEW')
2020 self.assertEqual(C.reported, 2)
2021 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07002022
James E. Blair972e3c72013-08-29 12:04:55 -07002023 def test_failing_dependent_changes(self):
2024 "Test that failing dependent patches are taken out of stream"
2025 self.worker.hold_jobs_in_build = True
2026 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2027 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2028 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2029 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2030 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
2031 A.addApproval('CRVW', 2)
2032 B.addApproval('CRVW', 2)
2033 C.addApproval('CRVW', 2)
2034 D.addApproval('CRVW', 2)
2035 E.addApproval('CRVW', 2)
2036
2037 # E, D -> C -> B, A
2038
2039 D.setDependsOn(C, 1)
2040 C.setDependsOn(B, 1)
2041
2042 self.worker.addFailTest('project-test1', B)
2043
2044 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2045 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2046 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2047 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2048 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2049
2050 self.waitUntilSettled()
2051 self.worker.release('.*-merge')
2052 self.waitUntilSettled()
2053 self.worker.release('.*-merge')
2054 self.waitUntilSettled()
2055 self.worker.release('.*-merge')
2056 self.waitUntilSettled()
2057 self.worker.release('.*-merge')
2058 self.waitUntilSettled()
2059 self.worker.release('.*-merge')
2060 self.waitUntilSettled()
2061
2062 self.worker.hold_jobs_in_build = False
2063 for build in self.builds:
2064 if build.parameters['ZUUL_CHANGE'] != '1':
2065 build.release()
2066 self.waitUntilSettled()
2067
2068 self.worker.release()
2069 self.waitUntilSettled()
2070
2071 self.assertEqual(A.data['status'], 'MERGED')
2072 self.assertEqual(A.reported, 2)
2073 self.assertEqual(B.data['status'], 'NEW')
2074 self.assertEqual(B.reported, 2)
2075 self.assertEqual(C.data['status'], 'NEW')
2076 self.assertEqual(C.reported, 2)
2077 self.assertEqual(D.data['status'], 'NEW')
2078 self.assertEqual(D.reported, 2)
2079 self.assertEqual(E.data['status'], 'MERGED')
2080 self.assertEqual(E.reported, 2)
2081 self.assertEqual(len(self.history), 18)
2082
James E. Blairec590122012-08-22 15:19:31 -07002083 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08002084 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07002085 # If it's dequeued more than once, we should see extra
2086 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07002087
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002088 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07002089 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2090 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2091 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2092 A.addApproval('CRVW', 2)
2093 B.addApproval('CRVW', 2)
2094 C.addApproval('CRVW', 2)
2095
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002096 self.worker.addFailTest('project1-test1', A)
2097 self.worker.addFailTest('project1-test2', A)
2098 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07002099
2100 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2101 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2102 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2103
2104 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07002105
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002106 self.assertEqual(len(self.builds), 1)
2107 self.assertEqual(self.builds[0].name, 'project1-merge')
2108 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07002109
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002110 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002111 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002112 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002113 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002114 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002115 self.waitUntilSettled()
2116
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002117 self.assertEqual(len(self.builds), 9)
2118 self.assertEqual(self.builds[0].name, 'project1-test1')
2119 self.assertEqual(self.builds[1].name, 'project1-test2')
2120 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
2121 self.assertEqual(self.builds[3].name, 'project1-test1')
2122 self.assertEqual(self.builds[4].name, 'project1-test2')
2123 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
2124 self.assertEqual(self.builds[6].name, 'project1-test1')
2125 self.assertEqual(self.builds[7].name, 'project1-test2')
2126 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07002127
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002128 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07002129 self.waitUntilSettled()
2130
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002131 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
2132 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07002133
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002134 self.worker.hold_jobs_in_build = False
2135 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07002136 self.waitUntilSettled()
2137
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002138 self.assertEqual(len(self.builds), 0)
2139 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07002140
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002141 self.assertEqual(A.data['status'], 'NEW')
2142 self.assertEqual(B.data['status'], 'MERGED')
2143 self.assertEqual(C.data['status'], 'MERGED')
2144 self.assertEqual(A.reported, 2)
2145 self.assertEqual(B.reported, 2)
2146 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07002147
2148 def test_nonvoting_job(self):
2149 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002150
James E. Blair4ec821f2012-08-23 15:28:28 -07002151 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
2152 'master', 'A')
2153 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002154 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07002155 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2156
2157 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07002158
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002159 self.assertEqual(A.data['status'], 'MERGED')
2160 self.assertEqual(A.reported, 2)
2161 self.assertEqual(
2162 self.getJobFromHistory('nonvoting-project-merge').result,
2163 'SUCCESS')
2164 self.assertEqual(
2165 self.getJobFromHistory('nonvoting-project-test1').result,
2166 'SUCCESS')
2167 self.assertEqual(
2168 self.getJobFromHistory('nonvoting-project-test2').result,
2169 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07002170
2171 def test_check_queue_success(self):
2172 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002173
James E. Blaire0487072012-08-29 17:38:31 -07002174 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2175 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2176
2177 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002178
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002179 self.assertEqual(A.data['status'], 'NEW')
2180 self.assertEqual(A.reported, 1)
2181 self.assertEqual(self.getJobFromHistory('project-merge').result,
2182 'SUCCESS')
2183 self.assertEqual(self.getJobFromHistory('project-test1').result,
2184 'SUCCESS')
2185 self.assertEqual(self.getJobFromHistory('project-test2').result,
2186 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07002187
2188 def test_check_queue_failure(self):
2189 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002190
James E. Blaire0487072012-08-29 17:38:31 -07002191 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002192 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07002193 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2194
2195 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002196
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002197 self.assertEqual(A.data['status'], 'NEW')
2198 self.assertEqual(A.reported, 1)
2199 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07002200 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002201 self.assertEqual(self.getJobFromHistory('project-test1').result,
2202 'SUCCESS')
2203 self.assertEqual(self.getJobFromHistory('project-test2').result,
2204 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07002205
2206 def test_dependent_behind_dequeue(self):
2207 "test that dependent changes behind dequeued changes work"
2208 # This complicated test is a reproduction of a real life bug
2209 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07002210
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002211 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07002212 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2213 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2214 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2215 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2216 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
2217 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
2218 D.setDependsOn(C, 1)
2219 E.setDependsOn(D, 1)
2220 A.addApproval('CRVW', 2)
2221 B.addApproval('CRVW', 2)
2222 C.addApproval('CRVW', 2)
2223 D.addApproval('CRVW', 2)
2224 E.addApproval('CRVW', 2)
2225 F.addApproval('CRVW', 2)
2226
2227 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07002228
2229 # Change object re-use in the gerrit trigger is hidden if
2230 # changes are added in quick succession; waiting makes it more
2231 # like real life.
2232 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2233 self.waitUntilSettled()
2234 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2235 self.waitUntilSettled()
2236
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002237 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002238 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002239 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002240 self.waitUntilSettled()
2241
2242 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2243 self.waitUntilSettled()
2244 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2245 self.waitUntilSettled()
2246 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2247 self.waitUntilSettled()
2248 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
2249 self.waitUntilSettled()
2250
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002251 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002252 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002253 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002254 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002255 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002256 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002257 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002258 self.waitUntilSettled()
2259
2260 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002261
2262 # Grab pointers to the jobs we want to release before
2263 # releasing any, because list indexes may change as
2264 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002265 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002266 a.release()
2267 b.release()
2268 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002269 self.waitUntilSettled()
2270
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002271 self.worker.hold_jobs_in_build = False
2272 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002273 self.waitUntilSettled()
2274
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002275 self.assertEqual(A.data['status'], 'NEW')
2276 self.assertEqual(B.data['status'], 'MERGED')
2277 self.assertEqual(C.data['status'], 'MERGED')
2278 self.assertEqual(D.data['status'], 'MERGED')
2279 self.assertEqual(E.data['status'], 'MERGED')
2280 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002281
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002282 self.assertEqual(A.reported, 2)
2283 self.assertEqual(B.reported, 2)
2284 self.assertEqual(C.reported, 2)
2285 self.assertEqual(D.reported, 2)
2286 self.assertEqual(E.reported, 2)
2287 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002288
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002289 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2290 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002291
2292 def test_merger_repack(self):
2293 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002294
James E. Blair05fed602012-09-07 12:45:24 -07002295 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2296 A.addApproval('CRVW', 2)
2297 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2298 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002299 self.assertEqual(self.getJobFromHistory('project-merge').result,
2300 'SUCCESS')
2301 self.assertEqual(self.getJobFromHistory('project-test1').result,
2302 'SUCCESS')
2303 self.assertEqual(self.getJobFromHistory('project-test2').result,
2304 'SUCCESS')
2305 self.assertEqual(A.data['status'], 'MERGED')
2306 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07002307 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002308 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002309
Monty Taylorbc758832013-06-17 17:22:42 -04002310 path = os.path.join(self.git_root, "org/project")
2311 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002312
2313 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2314 A.addApproval('CRVW', 2)
2315 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2316 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002317 self.assertEqual(self.getJobFromHistory('project-merge').result,
2318 'SUCCESS')
2319 self.assertEqual(self.getJobFromHistory('project-test1').result,
2320 'SUCCESS')
2321 self.assertEqual(self.getJobFromHistory('project-test2').result,
2322 'SUCCESS')
2323 self.assertEqual(A.data['status'], 'MERGED')
2324 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002325
James E. Blair4886f282012-11-15 09:27:33 -08002326 def test_merger_repack_large_change(self):
2327 "Test that the merger works with large changes after a repack"
2328 # https://bugs.launchpad.net/zuul/+bug/1078946
2329 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2330 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002331 path = os.path.join(self.upstream_root, "org/project1")
2332 print repack_repo(path)
2333 path = os.path.join(self.git_root, "org/project1")
2334 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002335
2336 A.addApproval('CRVW', 2)
2337 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2338 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002339 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2340 'SUCCESS')
2341 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2342 'SUCCESS')
2343 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2344 'SUCCESS')
2345 self.assertEqual(A.data['status'], 'MERGED')
2346 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002347
James E. Blair7ee88a22012-09-12 18:59:31 +02002348 def test_nonexistent_job(self):
2349 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002350 # Set to the state immediately after a restart
2351 self.resetGearmanServer()
2352 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002353
2354 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2355 A.addApproval('CRVW', 2)
2356 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2357 # There may be a thread about to report a lost change
2358 while A.reported < 2:
2359 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002360 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002361 self.assertFalse(job_names)
2362 self.assertEqual(A.data['status'], 'NEW')
2363 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002364 self.assertEmptyQueues()
2365
2366 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002367 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002368 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2369 A.addApproval('CRVW', 2)
2370 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2371 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002372 self.assertEqual(self.getJobFromHistory('project-merge').result,
2373 'SUCCESS')
2374 self.assertEqual(self.getJobFromHistory('project-test1').result,
2375 'SUCCESS')
2376 self.assertEqual(self.getJobFromHistory('project-test2').result,
2377 'SUCCESS')
2378 self.assertEqual(A.data['status'], 'MERGED')
2379 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002380
2381 def test_single_nonexistent_post_job(self):
2382 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002383 e = {
2384 "type": "ref-updated",
2385 "submitter": {
2386 "name": "User Name",
2387 },
2388 "refUpdate": {
2389 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2390 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2391 "refName": "master",
2392 "project": "org/project",
2393 }
2394 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002395 # Set to the state immediately after a restart
2396 self.resetGearmanServer()
2397 self.launcher.negative_function_cache_ttl = 0
2398
James E. Blairf62d4282012-12-31 17:01:50 -08002399 self.fake_gerrit.addEvent(e)
2400 self.waitUntilSettled()
2401
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002402 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002403
2404 def test_new_patchset_dequeues_old(self):
2405 "Test that a new patchset causes the old to be dequeued"
2406 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002407 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002408 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2409 M.setMerged()
2410
2411 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2412 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2413 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2414 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2415 A.addApproval('CRVW', 2)
2416 B.addApproval('CRVW', 2)
2417 C.addApproval('CRVW', 2)
2418 D.addApproval('CRVW', 2)
2419
2420 C.setDependsOn(B, 1)
2421 B.setDependsOn(A, 1)
2422 A.setDependsOn(M, 1)
2423
2424 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2425 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2426 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2427 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2428 self.waitUntilSettled()
2429
2430 B.addPatchset()
2431 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2432 self.waitUntilSettled()
2433
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002434 self.worker.hold_jobs_in_build = False
2435 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002436 self.waitUntilSettled()
2437
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002438 self.assertEqual(A.data['status'], 'MERGED')
2439 self.assertEqual(A.reported, 2)
2440 self.assertEqual(B.data['status'], 'NEW')
2441 self.assertEqual(B.reported, 2)
2442 self.assertEqual(C.data['status'], 'NEW')
2443 self.assertEqual(C.reported, 2)
2444 self.assertEqual(D.data['status'], 'MERGED')
2445 self.assertEqual(D.reported, 2)
2446 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002447
Arx Cruzb1b010d2013-10-28 19:49:59 -02002448 def test_zuul_url_return(self):
2449 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
2450 self.assertTrue(self.sched.config.has_option('zuul', 'zuul_url'))
2451 self.worker.hold_jobs_in_build = True
2452
2453 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2454 A.addApproval('CRVW', 2)
2455 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2456 self.waitUntilSettled()
2457
2458 self.assertEqual(len(self.builds), 1)
2459 for build in self.builds:
2460 self.assertTrue('ZUUL_URL' in build.parameters)
2461
2462 self.worker.hold_jobs_in_build = False
2463 self.worker.release()
2464 self.waitUntilSettled()
2465
James E. Blair2fa50962013-01-30 21:50:41 -08002466 def test_new_patchset_dequeues_old_on_head(self):
2467 "Test that a new patchset causes the old to be dequeued (at head)"
2468 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002469 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002470 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2471 M.setMerged()
2472 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2473 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2474 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2475 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2476 A.addApproval('CRVW', 2)
2477 B.addApproval('CRVW', 2)
2478 C.addApproval('CRVW', 2)
2479 D.addApproval('CRVW', 2)
2480
2481 C.setDependsOn(B, 1)
2482 B.setDependsOn(A, 1)
2483 A.setDependsOn(M, 1)
2484
2485 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2486 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2487 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2488 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2489 self.waitUntilSettled()
2490
2491 A.addPatchset()
2492 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2493 self.waitUntilSettled()
2494
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002495 self.worker.hold_jobs_in_build = False
2496 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002497 self.waitUntilSettled()
2498
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002499 self.assertEqual(A.data['status'], 'NEW')
2500 self.assertEqual(A.reported, 2)
2501 self.assertEqual(B.data['status'], 'NEW')
2502 self.assertEqual(B.reported, 2)
2503 self.assertEqual(C.data['status'], 'NEW')
2504 self.assertEqual(C.reported, 2)
2505 self.assertEqual(D.data['status'], 'MERGED')
2506 self.assertEqual(D.reported, 2)
2507 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002508
2509 def test_new_patchset_dequeues_old_without_dependents(self):
2510 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002511 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002512 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2513 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2514 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2515 A.addApproval('CRVW', 2)
2516 B.addApproval('CRVW', 2)
2517 C.addApproval('CRVW', 2)
2518
2519 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2520 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2521 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2522 self.waitUntilSettled()
2523
2524 B.addPatchset()
2525 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2526 self.waitUntilSettled()
2527
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002528 self.worker.hold_jobs_in_build = False
2529 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002530 self.waitUntilSettled()
2531
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002532 self.assertEqual(A.data['status'], 'MERGED')
2533 self.assertEqual(A.reported, 2)
2534 self.assertEqual(B.data['status'], 'NEW')
2535 self.assertEqual(B.reported, 2)
2536 self.assertEqual(C.data['status'], 'MERGED')
2537 self.assertEqual(C.reported, 2)
2538 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002539
2540 def test_new_patchset_dequeues_old_independent_queue(self):
2541 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002542 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002543 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2544 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2545 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2546 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2547 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2548 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2549 self.waitUntilSettled()
2550
2551 B.addPatchset()
2552 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2553 self.waitUntilSettled()
2554
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002555 self.worker.hold_jobs_in_build = False
2556 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002557 self.waitUntilSettled()
2558
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002559 self.assertEqual(A.data['status'], 'NEW')
2560 self.assertEqual(A.reported, 1)
2561 self.assertEqual(B.data['status'], 'NEW')
2562 self.assertEqual(B.reported, 1)
2563 self.assertEqual(C.data['status'], 'NEW')
2564 self.assertEqual(C.reported, 1)
2565 self.assertEqual(len(self.history), 10)
2566 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002567
2568 def test_zuul_refs(self):
2569 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002570 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002571 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2572 M1.setMerged()
2573 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2574 M2.setMerged()
2575
2576 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2577 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2578 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2579 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2580 A.addApproval('CRVW', 2)
2581 B.addApproval('CRVW', 2)
2582 C.addApproval('CRVW', 2)
2583 D.addApproval('CRVW', 2)
2584 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2585 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2586 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2587 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2588
2589 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002590 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002591 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002592 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002593 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002594 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002595 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002596 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002597 self.waitUntilSettled()
2598
James E. Blair7d0dedc2013-02-21 17:26:09 -08002599 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002600 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002601 if x.parameters['ZUUL_CHANGE'] == '3':
2602 a_zref = x.parameters['ZUUL_REF']
2603 if x.parameters['ZUUL_CHANGE'] == '4':
2604 b_zref = x.parameters['ZUUL_REF']
2605 if x.parameters['ZUUL_CHANGE'] == '5':
2606 c_zref = x.parameters['ZUUL_REF']
2607 if x.parameters['ZUUL_CHANGE'] == '6':
2608 d_zref = x.parameters['ZUUL_REF']
2609
2610 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002611 self.assertIsNotNone(a_zref)
2612 self.assertIsNotNone(b_zref)
2613 self.assertIsNotNone(c_zref)
2614 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002615
2616 # And they should all be different
2617 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002618 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002619
2620 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002621 self.assertTrue(self.ref_has_change(a_zref, A))
2622 self.assertFalse(self.ref_has_change(a_zref, B))
2623 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002624
2625 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002626 self.assertTrue(self.ref_has_change(b_zref, A))
2627 self.assertTrue(self.ref_has_change(b_zref, B))
2628 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002629
2630 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002631 self.assertTrue(self.ref_has_change(c_zref, A))
2632 self.assertTrue(self.ref_has_change(c_zref, B))
2633 self.assertTrue(self.ref_has_change(c_zref, C))
2634 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002635
2636 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002637 self.assertTrue(self.ref_has_change(d_zref, A))
2638 self.assertTrue(self.ref_has_change(d_zref, B))
2639 self.assertTrue(self.ref_has_change(d_zref, C))
2640 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002641
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002642 self.worker.hold_jobs_in_build = False
2643 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002644 self.waitUntilSettled()
2645
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002646 self.assertEqual(A.data['status'], 'MERGED')
2647 self.assertEqual(A.reported, 2)
2648 self.assertEqual(B.data['status'], 'MERGED')
2649 self.assertEqual(B.reported, 2)
2650 self.assertEqual(C.data['status'], 'MERGED')
2651 self.assertEqual(C.reported, 2)
2652 self.assertEqual(D.data['status'], 'MERGED')
2653 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002654
James E. Blair4a28a882013-08-23 15:17:33 -07002655 def test_rerun_on_error(self):
2656 "Test that if a worker fails to run a job, it is run again"
2657 self.worker.hold_jobs_in_build = True
2658 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2659 A.addApproval('CRVW', 2)
2660 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2661 self.waitUntilSettled()
2662
2663 self.builds[0].run_error = True
2664 self.worker.hold_jobs_in_build = False
2665 self.worker.release()
2666 self.waitUntilSettled()
2667 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2668 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2669
James E. Blair412e5582013-04-22 15:50:12 -07002670 def test_statsd(self):
2671 "Test each of the statsd methods used in the scheduler"
2672 import extras
2673 statsd = extras.try_import('statsd.statsd')
2674 statsd.incr('test-incr')
2675 statsd.timing('test-timing', 3)
2676 statsd.gauge('test-guage', 12)
2677 self.assertReportedStat('test-incr', '1|c')
2678 self.assertReportedStat('test-timing', '3|ms')
2679 self.assertReportedStat('test-guage', '12|g')
2680
James E. Blair70c71582013-03-06 08:50:50 -08002681 def test_file_jobs(self):
2682 "Test that file jobs run only when appropriate"
2683 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2684 A.addPatchset(['pip-requires'])
2685 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2686 A.addApproval('CRVW', 2)
2687 B.addApproval('CRVW', 2)
2688 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2689 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2690 self.waitUntilSettled()
2691
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002692 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002693 if x.name == 'project-testfile']
2694
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002695 self.assertEqual(len(testfile_jobs), 1)
2696 self.assertEqual(testfile_jobs[0].changes, '1,2')
2697 self.assertEqual(A.data['status'], 'MERGED')
2698 self.assertEqual(A.reported, 2)
2699 self.assertEqual(B.data['status'], 'MERGED')
2700 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002701
2702 def test_test_config(self):
2703 "Test that we can test the config"
2704 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002705 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002706 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002707 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002708
2709 def test_build_description(self):
2710 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002711 self.worker.registerFunction('set_description:' +
2712 self.worker.worker_id)
2713
2714 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2715 A.addApproval('CRVW', 2)
2716 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2717 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002718 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002719 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002720 self.assertTrue(re.search("Branch.*master", desc))
2721 self.assertTrue(re.search("Pipeline.*gate", desc))
2722 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2723 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2724 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2725 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002726
James E. Blair64ed6f22013-07-10 14:07:23 -07002727 def test_queue_precedence(self):
2728 "Test that queue precedence works"
2729
2730 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002731 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002732 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2733 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2734 A.addApproval('CRVW', 2)
2735 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2736
2737 self.waitUntilSettled()
2738 self.gearman_server.hold_jobs_in_queue = False
2739 self.gearman_server.release()
2740 self.waitUntilSettled()
2741
James E. Blair8de58bd2013-07-18 16:23:33 -07002742 # Run one build at a time to ensure non-race order:
2743 for x in range(6):
2744 self.release(self.builds[0])
2745 self.waitUntilSettled()
2746 self.worker.hold_jobs_in_build = False
2747 self.waitUntilSettled()
2748
James E. Blair64ed6f22013-07-10 14:07:23 -07002749 self.log.debug(self.history)
2750 self.assertEqual(self.history[0].pipeline, 'gate')
2751 self.assertEqual(self.history[1].pipeline, 'check')
2752 self.assertEqual(self.history[2].pipeline, 'gate')
2753 self.assertEqual(self.history[3].pipeline, 'gate')
2754 self.assertEqual(self.history[4].pipeline, 'check')
2755 self.assertEqual(self.history[5].pipeline, 'check')
2756
James E. Blair1843a552013-07-03 14:19:52 -07002757 def test_json_status(self):
2758 "Test that we can retrieve JSON status info"
2759 self.worker.hold_jobs_in_build = True
2760 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2761 A.addApproval('CRVW', 2)
2762 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2763 self.waitUntilSettled()
2764
2765 port = self.webapp.server.socket.getsockname()[1]
2766
2767 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2768 data = f.read()
2769
2770 self.worker.hold_jobs_in_build = False
2771 self.worker.release()
2772 self.waitUntilSettled()
2773
2774 data = json.loads(data)
2775 status_jobs = set()
2776 for p in data['pipelines']:
2777 for q in p['change_queues']:
2778 for head in q['heads']:
2779 for change in head:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002780 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002781 for job in change['jobs']:
2782 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002783 self.assertIn('project-merge', status_jobs)
2784 self.assertIn('project-test1', status_jobs)
2785 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002786
James E. Blairc3d428e2013-12-03 15:06:48 -08002787 def test_merging_queues(self):
2788 "Test that transitively-connected change queues are merged"
2789 self.config.set('zuul', 'layout_config',
2790 'tests/fixtures/layout-merge-queues.yaml')
2791 self.sched.reconfigure(self.config)
2792 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
2793
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002794 def test_node_label(self):
2795 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002796 self.worker.registerFunction('build:node-project-test1:debian')
2797
2798 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2799 A.addApproval('CRVW', 2)
2800 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2801 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002802
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002803 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2804 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2805 'debian')
2806 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002807
2808 def test_live_reconfiguration(self):
2809 "Test that live reconfiguration works"
2810 self.worker.hold_jobs_in_build = True
2811 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2812 A.addApproval('CRVW', 2)
2813 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2814 self.waitUntilSettled()
2815
2816 self.sched.reconfigure(self.config)
2817
2818 self.worker.hold_jobs_in_build = False
2819 self.worker.release()
2820 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002821 self.assertEqual(self.getJobFromHistory('project-merge').result,
2822 'SUCCESS')
2823 self.assertEqual(self.getJobFromHistory('project-test1').result,
2824 'SUCCESS')
2825 self.assertEqual(self.getJobFromHistory('project-test2').result,
2826 'SUCCESS')
2827 self.assertEqual(A.data['status'], 'MERGED')
2828 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002829
James E. Blaire712d9f2013-07-31 11:40:11 -07002830 def test_live_reconfiguration_functions(self):
2831 "Test live reconfiguration with a custom function"
2832 self.worker.registerFunction('build:node-project-test1:debian')
2833 self.worker.registerFunction('build:node-project-test1:wheezy')
2834 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2835 A.addApproval('CRVW', 2)
2836 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2837 self.waitUntilSettled()
2838
2839 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2840 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2841 'debian')
2842 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2843
2844 self.config.set('zuul', 'layout_config',
2845 'tests/fixtures/layout-live-'
2846 'reconfiguration-functions.yaml')
2847 self.sched.reconfigure(self.config)
2848 self.worker.build_history = []
2849
2850 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
2851 B.addApproval('CRVW', 2)
2852 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2853 self.waitUntilSettled()
2854
2855 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2856 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2857 'wheezy')
2858 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2859
James E. Blair287c06d2013-07-24 10:39:30 -07002860 def test_delayed_repo_init(self):
2861 self.config.set('zuul', 'layout_config',
2862 'tests/fixtures/layout-delayed-repo-init.yaml')
2863 self.sched.reconfigure(self.config)
2864
2865 self.init_repo("org/new-project")
2866 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2867
2868 A.addApproval('CRVW', 2)
2869 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2870 self.waitUntilSettled()
2871 self.assertEqual(self.getJobFromHistory('project-merge').result,
2872 'SUCCESS')
2873 self.assertEqual(self.getJobFromHistory('project-test1').result,
2874 'SUCCESS')
2875 self.assertEqual(self.getJobFromHistory('project-test2').result,
2876 'SUCCESS')
2877 self.assertEqual(A.data['status'], 'MERGED')
2878 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002879
Clark Boylan6dbbc482013-10-18 10:57:31 -07002880 def test_repo_deleted(self):
2881 self.config.set('zuul', 'layout_config',
2882 'tests/fixtures/layout-repo-deleted.yaml')
2883 self.sched.reconfigure(self.config)
2884
2885 self.init_repo("org/delete-project")
2886 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2887
2888 A.addApproval('CRVW', 2)
2889 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2890 self.waitUntilSettled()
2891 self.assertEqual(self.getJobFromHistory('project-merge').result,
2892 'SUCCESS')
2893 self.assertEqual(self.getJobFromHistory('project-test1').result,
2894 'SUCCESS')
2895 self.assertEqual(self.getJobFromHistory('project-test2').result,
2896 'SUCCESS')
2897 self.assertEqual(A.data['status'], 'MERGED')
2898 self.assertEqual(A.reported, 2)
2899
2900 # Delete org/new-project zuul repo. Should be recloned.
2901 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
2902
2903 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2904
2905 B.addApproval('CRVW', 2)
2906 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2907 self.waitUntilSettled()
2908 self.assertEqual(self.getJobFromHistory('project-merge').result,
2909 'SUCCESS')
2910 self.assertEqual(self.getJobFromHistory('project-test1').result,
2911 'SUCCESS')
2912 self.assertEqual(self.getJobFromHistory('project-test2').result,
2913 'SUCCESS')
2914 self.assertEqual(B.data['status'], 'MERGED')
2915 self.assertEqual(B.reported, 2)
2916
James E. Blair63bb0ef2013-07-29 17:14:51 -07002917 def test_timer(self):
2918 "Test that a periodic job is triggered"
2919 self.worker.hold_jobs_in_build = True
2920 self.config.set('zuul', 'layout_config',
2921 'tests/fixtures/layout-timer.yaml')
2922 self.sched.reconfigure(self.config)
2923 self.registerJobs()
2924
2925 start = time.time()
2926 failed = True
2927 while ((time.time() - start) < 30):
2928 if len(self.builds) == 2:
2929 failed = False
2930 break
2931 else:
2932 time.sleep(1)
2933
2934 if failed:
2935 raise Exception("Expected jobs never ran")
2936
2937 self.waitUntilSettled()
2938 port = self.webapp.server.socket.getsockname()[1]
2939
2940 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2941 data = f.read()
2942
2943 self.worker.hold_jobs_in_build = False
2944 self.worker.release()
2945 self.waitUntilSettled()
2946
2947 self.assertEqual(self.getJobFromHistory(
2948 'project-bitrot-stable-old').result, 'SUCCESS')
2949 self.assertEqual(self.getJobFromHistory(
2950 'project-bitrot-stable-older').result, 'SUCCESS')
2951
2952 data = json.loads(data)
2953 status_jobs = set()
2954 for p in data['pipelines']:
2955 for q in p['change_queues']:
2956 for head in q['heads']:
2957 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002958 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002959 for job in change['jobs']:
2960 status_jobs.add(job['name'])
2961 self.assertIn('project-bitrot-stable-old', status_jobs)
2962 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002963
2964 def test_check_smtp_pool(self):
2965 self.config.set('zuul', 'layout_config',
2966 'tests/fixtures/layout-smtp.yaml')
2967 self.sched.reconfigure(self.config)
2968
2969 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2970 self.waitUntilSettled()
2971
2972 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2973 self.waitUntilSettled()
2974
2975 self.assertEqual(len(FakeSMTP.messages), 2)
2976
2977 # A.messages only holds what FakeGerrit places in it. Thus we
2978 # work on the knowledge of what the first message should be as
2979 # it is only configured to go to SMTP.
2980
2981 self.assertEqual('zuul@example.com',
2982 FakeSMTP.messages[0]['from_email'])
2983 self.assertEqual(['you@example.com'],
2984 FakeSMTP.messages[0]['to_email'])
2985 self.assertEqual('Starting check jobs.',
2986 FakeSMTP.messages[0]['body'])
2987
2988 self.assertEqual('zuul_from@example.com',
2989 FakeSMTP.messages[1]['from_email'])
2990 self.assertEqual(['alternative_me@example.com'],
2991 FakeSMTP.messages[1]['to_email'])
2992 self.assertEqual(A.messages[0],
2993 FakeSMTP.messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002994
2995 def test_client_enqueue(self):
2996 "Test that the RPC client can enqueue a change"
2997 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2998 A.addApproval('CRVW', 2)
2999 A.addApproval('APRV', 1)
3000
3001 client = zuul.rpcclient.RPCClient('127.0.0.1',
3002 self.gearman_server.port)
3003 r = client.enqueue(pipeline='gate',
3004 project='org/project',
3005 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003006 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003007 self.waitUntilSettled()
3008 self.assertEqual(self.getJobFromHistory('project-merge').result,
3009 'SUCCESS')
3010 self.assertEqual(self.getJobFromHistory('project-test1').result,
3011 'SUCCESS')
3012 self.assertEqual(self.getJobFromHistory('project-test2').result,
3013 'SUCCESS')
3014 self.assertEqual(A.data['status'], 'MERGED')
3015 self.assertEqual(A.reported, 2)
3016 self.assertEqual(r, True)
3017
3018 def test_client_enqueue_negative(self):
3019 "Test that the RPC client returns errors"
3020 client = zuul.rpcclient.RPCClient('127.0.0.1',
3021 self.gearman_server.port)
3022 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3023 "Invalid project"):
3024 r = client.enqueue(pipeline='gate',
3025 project='project-does-not-exist',
3026 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003027 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003028 client.shutdown()
3029 self.assertEqual(r, False)
3030
3031 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3032 "Invalid pipeline"):
3033 r = client.enqueue(pipeline='pipeline-does-not-exist',
3034 project='org/project',
3035 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003036 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003037 client.shutdown()
3038 self.assertEqual(r, False)
3039
3040 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3041 "Invalid trigger"):
3042 r = client.enqueue(pipeline='gate',
3043 project='org/project',
3044 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003045 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003046 client.shutdown()
3047 self.assertEqual(r, False)
3048
3049 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3050 "Invalid change"):
3051 r = client.enqueue(pipeline='gate',
3052 project='org/project',
3053 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003054 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003055 client.shutdown()
3056 self.assertEqual(r, False)
3057
3058 self.waitUntilSettled()
3059 self.assertEqual(len(self.history), 0)
3060 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003061
3062 def test_client_promote(self):
3063 "Test that the RPC client can promote a change"
3064 self.worker.hold_jobs_in_build = True
3065 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3066 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3067 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3068 A.addApproval('CRVW', 2)
3069 B.addApproval('CRVW', 2)
3070 C.addApproval('CRVW', 2)
3071
3072 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3073 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3074 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3075
3076 self.waitUntilSettled()
3077
3078 client = zuul.rpcclient.RPCClient('127.0.0.1',
3079 self.gearman_server.port)
3080 r = client.promote(pipeline='gate',
3081 change_ids=['2,1', '3,1'])
3082
3083 self.worker.release('.*-merge')
3084 self.waitUntilSettled()
3085 self.worker.release('.*-merge')
3086 self.waitUntilSettled()
3087 self.worker.release('.*-merge')
3088 self.waitUntilSettled()
3089
3090 self.assertEqual(len(self.builds), 6)
3091 self.assertEqual(self.builds[0].name, 'project-test1')
3092 self.assertEqual(self.builds[1].name, 'project-test2')
3093 self.assertEqual(self.builds[2].name, 'project-test1')
3094 self.assertEqual(self.builds[3].name, 'project-test2')
3095 self.assertEqual(self.builds[4].name, 'project-test1')
3096 self.assertEqual(self.builds[5].name, 'project-test2')
3097
3098 self.assertTrue(self.job_has_changes(self.builds[0], B))
3099 self.assertFalse(self.job_has_changes(self.builds[0], A))
3100 self.assertFalse(self.job_has_changes(self.builds[0], C))
3101
3102 self.assertTrue(self.job_has_changes(self.builds[2], B))
3103 self.assertTrue(self.job_has_changes(self.builds[2], C))
3104 self.assertFalse(self.job_has_changes(self.builds[2], A))
3105
3106 self.assertTrue(self.job_has_changes(self.builds[4], B))
3107 self.assertTrue(self.job_has_changes(self.builds[4], C))
3108 self.assertTrue(self.job_has_changes(self.builds[4], A))
3109
3110 self.worker.release()
3111 self.waitUntilSettled()
3112
3113 self.assertEqual(A.data['status'], 'MERGED')
3114 self.assertEqual(A.reported, 2)
3115 self.assertEqual(B.data['status'], 'MERGED')
3116 self.assertEqual(B.reported, 2)
3117 self.assertEqual(C.data['status'], 'MERGED')
3118 self.assertEqual(C.reported, 2)
3119
3120 client.shutdown()
3121 self.assertEqual(r, True)
3122
3123 def test_client_promote_dependent(self):
3124 "Test that the RPC client can promote a dependent change"
3125 # C (depends on B) -> B -> A ; then promote C to get:
3126 # A -> C (depends on B) -> B
3127 self.worker.hold_jobs_in_build = True
3128 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3129 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3130 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3131
3132 C.setDependsOn(B, 1)
3133
3134 A.addApproval('CRVW', 2)
3135 B.addApproval('CRVW', 2)
3136 C.addApproval('CRVW', 2)
3137
3138 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3139 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3140 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3141
3142 self.waitUntilSettled()
3143
3144 client = zuul.rpcclient.RPCClient('127.0.0.1',
3145 self.gearman_server.port)
3146 r = client.promote(pipeline='gate',
3147 change_ids=['3,1'])
3148
3149 self.worker.release('.*-merge')
3150 self.waitUntilSettled()
3151 self.worker.release('.*-merge')
3152 self.waitUntilSettled()
3153 self.worker.release('.*-merge')
3154 self.waitUntilSettled()
3155
3156 self.assertEqual(len(self.builds), 6)
3157 self.assertEqual(self.builds[0].name, 'project-test1')
3158 self.assertEqual(self.builds[1].name, 'project-test2')
3159 self.assertEqual(self.builds[2].name, 'project-test1')
3160 self.assertEqual(self.builds[3].name, 'project-test2')
3161 self.assertEqual(self.builds[4].name, 'project-test1')
3162 self.assertEqual(self.builds[5].name, 'project-test2')
3163
3164 self.assertTrue(self.job_has_changes(self.builds[0], B))
3165 self.assertFalse(self.job_has_changes(self.builds[0], A))
3166 self.assertFalse(self.job_has_changes(self.builds[0], C))
3167
3168 self.assertTrue(self.job_has_changes(self.builds[2], B))
3169 self.assertTrue(self.job_has_changes(self.builds[2], C))
3170 self.assertFalse(self.job_has_changes(self.builds[2], A))
3171
3172 self.assertTrue(self.job_has_changes(self.builds[4], B))
3173 self.assertTrue(self.job_has_changes(self.builds[4], C))
3174 self.assertTrue(self.job_has_changes(self.builds[4], A))
3175
3176 self.worker.release()
3177 self.waitUntilSettled()
3178
3179 self.assertEqual(A.data['status'], 'MERGED')
3180 self.assertEqual(A.reported, 2)
3181 self.assertEqual(B.data['status'], 'MERGED')
3182 self.assertEqual(B.reported, 2)
3183 self.assertEqual(C.data['status'], 'MERGED')
3184 self.assertEqual(C.reported, 2)
3185
3186 client.shutdown()
3187 self.assertEqual(r, True)
3188
3189 def test_client_promote_negative(self):
3190 "Test that the RPC client returns errors for promotion"
3191 self.worker.hold_jobs_in_build = True
3192 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3193 A.addApproval('CRVW', 2)
3194 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3195 self.waitUntilSettled()
3196
3197 client = zuul.rpcclient.RPCClient('127.0.0.1',
3198 self.gearman_server.port)
3199
3200 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3201 r = client.promote(pipeline='nonexistent',
3202 change_ids=['2,1', '3,1'])
3203 client.shutdown()
3204 self.assertEqual(r, False)
3205
3206 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3207 r = client.promote(pipeline='gate',
3208 change_ids=['4,1'])
3209 client.shutdown()
3210 self.assertEqual(r, False)
3211
3212 self.worker.hold_jobs_in_build = False
3213 self.worker.release()
3214 self.waitUntilSettled()