blob: c15d70c9b7ae3e861e99313fadedae748aa82a2f [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")
769 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700770 self.init_repo("org/conflict-project")
James E. Blair412e5582013-04-22 15:50:12 -0700771
772 self.statsd = FakeStatsd()
773 os.environ['STATSD_HOST'] = 'localhost'
774 os.environ['STATSD_PORT'] = str(self.statsd.port)
775 self.statsd.start()
776 # the statsd client object is configured in the statsd module import
777 reload(statsd)
778 reload(zuul.scheduler)
779
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700780 self.gearman_server = FakeGearmanServer()
781
782 self.config = ConfigParser.ConfigParser()
783 cfg = StringIO()
784 CONFIG.write(cfg)
785 cfg.seek(0)
786 self.config.readfp(cfg)
787 self.config.set('gearman', 'port', str(self.gearman_server.port))
788
Monty Taylorbc758832013-06-17 17:22:42 -0400789 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700790 self.worker.addServer('127.0.0.1', self.gearman_server.port)
791 self.gearman_server.worker = self.worker
792
James E. Blairb0fcae42012-07-17 11:12:10 -0700793 self.sched = zuul.scheduler.Scheduler()
794
James E. Blair8cc15a82012-08-01 11:17:57 -0700795 def URLOpenerFactory(*args, **kw):
796 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400797 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700798
James E. Blair8cc15a82012-08-01 11:17:57 -0700799 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700800 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700801
802 zuul.lib.gerrit.Gerrit = FakeGerrit
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000803 self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTP))
James E. Blairb0fcae42012-07-17 11:12:10 -0700804
Monty Taylorbc758832013-06-17 17:22:42 -0400805 self.gerrit = FakeGerritTrigger(
806 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700807 self.gerrit.replication_timeout = 1.5
808 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700809 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400810 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700811
James E. Blair1843a552013-07-03 14:19:52 -0700812 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
James E. Blairad28e912013-11-27 10:43:22 -0800813 self.rpc = zuul.rpclistener.RPCListener(self.config, self.sched)
James E. Blair1843a552013-07-03 14:19:52 -0700814
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700815 self.sched.setLauncher(self.launcher)
James E. Blair6c358e72013-07-29 17:06:47 -0700816 self.sched.registerTrigger(self.gerrit)
James E. Blair63bb0ef2013-07-29 17:14:51 -0700817 self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
818 self.sched.registerTrigger(self.timer)
James E. Blairb0fcae42012-07-17 11:12:10 -0700819
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000820 self.sched.registerReporter(
821 zuul.reporter.gerrit.Reporter(self.gerrit))
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000822 self.smtp_reporter = zuul.reporter.smtp.Reporter(
823 self.config.get('smtp', 'default_from'),
824 self.config.get('smtp', 'default_to'),
825 self.config.get('smtp', 'server'))
826 self.sched.registerReporter(self.smtp_reporter)
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000827
James E. Blairb0fcae42012-07-17 11:12:10 -0700828 self.sched.start()
829 self.sched.reconfigure(self.config)
830 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700831 self.webapp.start()
James E. Blairad28e912013-11-27 10:43:22 -0800832 self.rpc.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700833 self.launcher.gearman.waitForServer()
834 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400835 self.builds = self.worker.running_builds
836 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700837
James E. Blairfee8d652013-06-07 08:57:52 -0700838 self.addCleanup(self.assertFinalState)
839 self.addCleanup(self.shutdown)
840
841 def assertFinalState(self):
842 # Make sure that the change cache is cleared
James E. Blair6c358e72013-07-29 17:06:47 -0700843 self.assertEqual(len(self.gerrit._change_cache.keys()), 0)
Clark Boylan4ba48d92013-11-11 18:03:53 -0800844 # Make sure that git.Repo objects have been garbage collected.
845 repos = []
846 gc.collect()
847 for obj in gc.get_objects():
848 if isinstance(obj, git.Repo):
849 repos.append(obj)
850 self.assertEqual(len(repos), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700851 self.assertEmptyQueues()
852
853 def shutdown(self):
854 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700855 self.launcher.stop()
856 self.worker.shutdown()
857 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700858 self.gerrit.stop()
James E. Blair63bb0ef2013-07-29 17:14:51 -0700859 self.timer.stop()
James E. Blairb0fcae42012-07-17 11:12:10 -0700860 self.sched.stop()
861 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700862 self.statsd.stop()
863 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700864 self.webapp.stop()
865 self.webapp.join()
James E. Blairad28e912013-11-27 10:43:22 -0800866 self.rpc.stop()
867 self.rpc.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700868 threads = threading.enumerate()
869 if len(threads) > 1:
870 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400871 super(TestScheduler, self).tearDown()
872
873 def init_repo(self, project):
874 parts = project.split('/')
875 path = os.path.join(self.upstream_root, *parts[:-1])
876 if not os.path.exists(path):
877 os.makedirs(path)
878 path = os.path.join(self.upstream_root, project)
879 repo = git.Repo.init(path)
880
881 repo.config_writer().set_value('user', 'email', 'user@example.com')
882 repo.config_writer().set_value('user', 'name', 'User Name')
883 repo.config_writer().write()
884
885 fn = os.path.join(path, 'README')
886 f = open(fn, 'w')
887 f.write("test\n")
888 f.close()
889 repo.index.add([fn])
890 repo.index.commit('initial commit')
891 master = repo.create_head('master')
892 repo.create_tag('init')
893
894 mp = repo.create_head('mp')
895 repo.head.reference = mp
896 f = open(fn, 'a')
897 f.write("test mp\n")
898 f.close()
899 repo.index.add([fn])
900 repo.index.commit('mp commit')
901
902 repo.head.reference = master
903 repo.head.reset(index=True, working_tree=True)
904 repo.git.clean('-x', '-f', '-d')
905
906 def ref_has_change(self, ref, change):
907 path = os.path.join(self.git_root, change.project)
908 repo = git.Repo(path)
909 for commit in repo.iter_commits(ref):
910 if commit.message.strip() == ('%s-1' % change.subject):
911 return True
912 return False
913
914 def job_has_changes(self, *args):
915 job = args[0]
916 commits = args[1:]
917 if isinstance(job, FakeBuild):
918 parameters = job.parameters
919 else:
920 parameters = json.loads(job.arguments)
921 project = parameters['ZUUL_PROJECT']
922 path = os.path.join(self.git_root, project)
923 repo = git.Repo(path)
924 ref = parameters['ZUUL_REF']
925 sha = parameters['ZUUL_COMMIT']
926 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
927 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
928 commit_messages = ['%s-1' % commit.subject for commit in commits]
929 for msg in commit_messages:
930 if msg not in repo_messages:
931 return False
932 if repo_shas[0] != sha:
933 return False
934 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700935
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700936 def registerJobs(self):
937 count = 0
James E. Blaireff88162013-07-01 12:44:14 -0400938 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700939 self.worker.registerFunction('build:' + job)
940 count += 1
941 self.worker.registerFunction('stop:' + self.worker.worker_id)
942 count += 1
943
944 while len(self.gearman_server.functions) < count:
945 time.sleep(0)
946
947 def release(self, job):
948 if isinstance(job, FakeBuild):
949 job.release()
950 else:
951 job.waiting = False
952 self.log.debug("Queued job %s released" % job.unique)
953 self.gearman_server.wakeConnections()
954
955 def getParameter(self, job, name):
956 if isinstance(job, FakeBuild):
957 return job.parameters[name]
958 else:
959 parameters = json.loads(job.arguments)
960 return parameters[name]
961
962 def resetGearmanServer(self):
963 self.worker.setFunctions([])
964 while True:
965 done = True
966 for connection in self.gearman_server.active_connections:
James E. Blairad28e912013-11-27 10:43:22 -0800967 if (connection.functions and
968 connection.client_id != 'Zuul RPC Listener'):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700969 done = False
970 if done:
971 break
972 time.sleep(0)
973 self.gearman_server.functions = set()
James E. Blairad28e912013-11-27 10:43:22 -0800974 self.rpc.register()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700975
976 def haveAllBuildsReported(self):
977 # See if Zuul is waiting on a meta job to complete
978 if self.launcher.meta_jobs:
979 return False
980 # Find out if every build that the worker has completed has been
981 # reported back to Zuul. If it hasn't then that means a Gearman
982 # event is still in transit and the system is not stable.
983 for build in self.worker.build_history:
984 zbuild = self.launcher.builds.get(build.uuid)
985 if not zbuild:
986 # It has already been reported
987 continue
988 # It hasn't been reported yet.
989 return False
990 # Make sure that none of the worker connections are in GRAB_WAIT
991 for connection in self.worker.active_connections:
992 if connection.state == 'GRAB_WAIT':
993 return False
994 return True
995
996 def areAllBuildsWaiting(self):
997 ret = True
998
999 builds = self.launcher.builds.values()
1000 for build in builds:
1001 client_job = None
1002 for conn in self.launcher.gearman.active_connections:
1003 for j in conn.related_jobs.values():
1004 if j.unique == build.uuid:
1005 client_job = j
1006 break
1007 if not client_job:
1008 self.log.debug("%s is not known to the gearman client" %
1009 build)
1010 ret = False
1011 continue
1012 if not client_job.handle:
1013 self.log.debug("%s has no handle" % client_job)
1014 ret = False
1015 continue
1016 server_job = self.gearman_server.jobs.get(client_job.handle)
1017 if not server_job:
1018 self.log.debug("%s is not known to the gearman server" %
1019 client_job)
1020 ret = False
1021 continue
1022 if not hasattr(server_job, 'waiting'):
1023 self.log.debug("%s is being enqueued" % server_job)
1024 ret = False
1025 continue
1026 if server_job.waiting:
1027 continue
1028 worker_job = self.worker.gearman_jobs.get(server_job.unique)
1029 if worker_job:
1030 if worker_job.build.isWaiting():
1031 continue
1032 else:
1033 self.log.debug("%s is running" % worker_job)
1034 ret = False
1035 else:
1036 self.log.debug("%s is unassigned" % server_job)
1037 ret = False
1038 return ret
1039
James E. Blairb0fcae42012-07-17 11:12:10 -07001040 def waitUntilSettled(self):
1041 self.log.debug("Waiting until settled...")
1042 start = time.time()
1043 while True:
1044 if time.time() - start > 10:
1045 print 'queue status:',
1046 print self.sched.trigger_event_queue.empty(),
1047 print self.sched.result_event_queue.empty(),
1048 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001049 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -07001050 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001051 # Make sure no new events show up while we're checking
1052 self.worker.lock.acquire()
1053 # have all build states propogated to zuul?
1054 if self.haveAllBuildsReported():
1055 # Join ensures that the queue is empty _and_ events have been
1056 # processed
1057 self.fake_gerrit.event_queue.join()
1058 self.sched.trigger_event_queue.join()
1059 self.sched.result_event_queue.join()
1060 if (self.sched.trigger_event_queue.empty() and
1061 self.sched.result_event_queue.empty() and
1062 self.fake_gerrit.event_queue.empty() and
1063 self.areAllBuildsWaiting()):
1064 self.worker.lock.release()
1065 self.log.debug("...settled.")
1066 return
1067 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001068 self.sched.wake_event.wait(0.1)
1069
James E. Blaird466dc42012-07-31 10:42:56 -07001070 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -08001071 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -07001072 return len(jobs)
1073
James E. Blair4ca985f2013-05-30 12:27:43 -07001074 def getJobFromHistory(self, name):
1075 history = self.worker.build_history
1076 for job in history:
1077 if job.name == name:
1078 return job
1079 raise Exception("Unable to find job %s in history" % name)
1080
James E. Blaire0487072012-08-29 17:38:31 -07001081 def assertEmptyQueues(self):
1082 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001083 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001084 for queue in pipeline.queues:
1085 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001086 print 'pipeline %s queue %s contents %s' % (
1087 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001088 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001089
James E. Blair66eeebf2013-07-27 17:44:32 -07001090 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001091 start = time.time()
1092 while time.time() < (start + 5):
1093 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001094 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001095 k, v = stat.split(':')
1096 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001097 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001098 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001099 elif value:
1100 if value == v:
1101 return
1102 elif kind:
1103 if v.endswith('|' + kind):
1104 return
James E. Blair412e5582013-04-22 15:50:12 -07001105 time.sleep(0.1)
1106
1107 pprint.pprint(self.statsd.stats)
1108 raise Exception("Key %s not found in reported stats" % key)
1109
James E. Blairb0fcae42012-07-17 11:12:10 -07001110 def test_jobs_launched(self):
1111 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001112
James E. Blairb0fcae42012-07-17 11:12:10 -07001113 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001114 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001115 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1116 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001117 self.assertEqual(self.getJobFromHistory('project-merge').result,
1118 'SUCCESS')
1119 self.assertEqual(self.getJobFromHistory('project-test1').result,
1120 'SUCCESS')
1121 self.assertEqual(self.getJobFromHistory('project-test2').result,
1122 'SUCCESS')
1123 self.assertEqual(A.data['status'], 'MERGED')
1124 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001125
James E. Blair66eeebf2013-07-27 17:44:32 -07001126 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1127 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1128 value='1|g')
1129 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1130 kind='ms')
1131 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1132 value='1|c')
1133 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1134 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1135 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001136 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001137 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001138 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001139 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001140
James E. Blair3cb10702013-08-24 08:56:03 -07001141 def test_initial_pipeline_gauges(self):
1142 "Test that each pipeline reported its length on start"
1143 pipeline_names = self.sched.layout.pipelines.keys()
1144 self.assertNotEqual(len(pipeline_names), 0)
1145 for name in pipeline_names:
1146 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
1147 value='0|g')
1148
James E. Blair42f74822013-05-14 15:18:03 -07001149 def test_duplicate_pipelines(self):
1150 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001151
James E. Blair42f74822013-05-14 15:18:03 -07001152 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1153 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1154 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001155
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001156 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001157 self.history[0].name == 'project-test1'
1158 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001159
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001160 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001161 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001162 self.assertIn('dup1/project-test1', A.messages[0])
1163 self.assertNotIn('dup2/project-test1', A.messages[0])
1164 self.assertNotIn('dup1/project-test1', A.messages[1])
1165 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001166 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001167 self.assertIn('dup1/project-test1', A.messages[1])
1168 self.assertNotIn('dup2/project-test1', A.messages[1])
1169 self.assertNotIn('dup1/project-test1', A.messages[0])
1170 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001171
James E. Blairb0fcae42012-07-17 11:12:10 -07001172 def test_parallel_changes(self):
1173 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001174
1175 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001176 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1177 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1178 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001179 A.addApproval('CRVW', 2)
1180 B.addApproval('CRVW', 2)
1181 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001182
1183 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1184 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1185 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1186
1187 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001188 self.assertEqual(len(self.builds), 1)
1189 self.assertEqual(self.builds[0].name, 'project-merge')
1190 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001191
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001192 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001193 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001194 self.assertEqual(len(self.builds), 3)
1195 self.assertEqual(self.builds[0].name, 'project-test1')
1196 self.assertTrue(self.job_has_changes(self.builds[0], A))
1197 self.assertEqual(self.builds[1].name, 'project-test2')
1198 self.assertTrue(self.job_has_changes(self.builds[1], A))
1199 self.assertEqual(self.builds[2].name, 'project-merge')
1200 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001201
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001202 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001203 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001204 self.assertEqual(len(self.builds), 5)
1205 self.assertEqual(self.builds[0].name, 'project-test1')
1206 self.assertTrue(self.job_has_changes(self.builds[0], A))
1207 self.assertEqual(self.builds[1].name, 'project-test2')
1208 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001209
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001210 self.assertEqual(self.builds[2].name, 'project-test1')
1211 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1212 self.assertEqual(self.builds[3].name, 'project-test2')
1213 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001214
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001215 self.assertEqual(self.builds[4].name, 'project-merge')
1216 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001217
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001218 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001219 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001220 self.assertEqual(len(self.builds), 6)
1221 self.assertEqual(self.builds[0].name, 'project-test1')
1222 self.assertTrue(self.job_has_changes(self.builds[0], A))
1223 self.assertEqual(self.builds[1].name, 'project-test2')
1224 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001225
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001226 self.assertEqual(self.builds[2].name, 'project-test1')
1227 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1228 self.assertEqual(self.builds[3].name, 'project-test2')
1229 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001230
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001231 self.assertEqual(self.builds[4].name, 'project-test1')
1232 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1233 self.assertEqual(self.builds[5].name, 'project-test2')
1234 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001235
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001236 self.worker.hold_jobs_in_build = False
1237 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001238 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001239 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001240
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001241 self.assertEqual(len(self.history), 9)
1242 self.assertEqual(A.data['status'], 'MERGED')
1243 self.assertEqual(B.data['status'], 'MERGED')
1244 self.assertEqual(C.data['status'], 'MERGED')
1245 self.assertEqual(A.reported, 2)
1246 self.assertEqual(B.reported, 2)
1247 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001248
1249 def test_failed_changes(self):
1250 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001251 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001252
James E. Blairb02a3bb2012-07-30 17:49:55 -07001253 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1254 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001255 A.addApproval('CRVW', 2)
1256 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001257
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001258 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001259
James E. Blaire2819012013-06-28 17:17:26 -04001260 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1261 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001262 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001263
1264 self.worker.release('.*-merge')
1265 self.waitUntilSettled()
1266
1267 self.worker.hold_jobs_in_build = False
1268 self.worker.release()
1269
1270 self.waitUntilSettled()
1271 # It's certain that the merge job for change 2 will run, but
1272 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001273 self.assertTrue(len(self.history) > 6)
1274 self.assertEqual(A.data['status'], 'NEW')
1275 self.assertEqual(B.data['status'], 'MERGED')
1276 self.assertEqual(A.reported, 2)
1277 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001278
1279 def test_independent_queues(self):
1280 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001281
1282 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001283 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001284 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1285 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001286 A.addApproval('CRVW', 2)
1287 B.addApproval('CRVW', 2)
1288 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001289
1290 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1291 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1292 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1293
James E. Blairb02a3bb2012-07-30 17:49:55 -07001294 self.waitUntilSettled()
1295
1296 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001297 self.assertEqual(len(self.builds), 2)
1298 self.assertEqual(self.builds[0].name, 'project-merge')
1299 self.assertTrue(self.job_has_changes(self.builds[0], A))
1300 self.assertEqual(self.builds[1].name, 'project1-merge')
1301 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001302
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001303 # Release the current merge builds
1304 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001305 self.waitUntilSettled()
1306 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001307 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001308 self.waitUntilSettled()
1309
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001310 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001311 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001312 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001313
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001314 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001315 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001316 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001317
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001318 self.assertEqual(len(self.history), 11)
1319 self.assertEqual(A.data['status'], 'MERGED')
1320 self.assertEqual(B.data['status'], 'MERGED')
1321 self.assertEqual(C.data['status'], 'MERGED')
1322 self.assertEqual(A.reported, 2)
1323 self.assertEqual(B.reported, 2)
1324 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001325
1326 def test_failed_change_at_head(self):
1327 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001328
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001329 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001330 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1331 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1332 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001333 A.addApproval('CRVW', 2)
1334 B.addApproval('CRVW', 2)
1335 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001336
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001337 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001338
1339 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1340 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1341 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1342
1343 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001344
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001345 self.assertEqual(len(self.builds), 1)
1346 self.assertEqual(self.builds[0].name, 'project-merge')
1347 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001348
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001349 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001350 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001351 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001352 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001353 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001354 self.waitUntilSettled()
1355
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001356 self.assertEqual(len(self.builds), 6)
1357 self.assertEqual(self.builds[0].name, 'project-test1')
1358 self.assertEqual(self.builds[1].name, 'project-test2')
1359 self.assertEqual(self.builds[2].name, 'project-test1')
1360 self.assertEqual(self.builds[3].name, 'project-test2')
1361 self.assertEqual(self.builds[4].name, 'project-test1')
1362 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001363
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001364 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001365 self.waitUntilSettled()
1366
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001367 # project-test2, project-merge for B
1368 self.assertEqual(len(self.builds), 2)
1369 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001370
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001371 self.worker.hold_jobs_in_build = False
1372 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001373 self.waitUntilSettled()
1374
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001375 self.assertEqual(len(self.builds), 0)
1376 self.assertEqual(len(self.history), 15)
1377 self.assertEqual(A.data['status'], 'NEW')
1378 self.assertEqual(B.data['status'], 'MERGED')
1379 self.assertEqual(C.data['status'], 'MERGED')
1380 self.assertEqual(A.reported, 2)
1381 self.assertEqual(B.reported, 2)
1382 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001383
James E. Blair0aac4872013-08-23 14:02:38 -07001384 def test_failed_change_in_middle(self):
1385 "Test a failed change in the middle of the queue"
1386
1387 self.worker.hold_jobs_in_build = True
1388 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1389 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1390 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1391 A.addApproval('CRVW', 2)
1392 B.addApproval('CRVW', 2)
1393 C.addApproval('CRVW', 2)
1394
1395 self.worker.addFailTest('project-test1', B)
1396
1397 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1398 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1399 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1400
1401 self.waitUntilSettled()
1402
1403 self.worker.release('.*-merge')
1404 self.waitUntilSettled()
1405 self.worker.release('.*-merge')
1406 self.waitUntilSettled()
1407 self.worker.release('.*-merge')
1408 self.waitUntilSettled()
1409
1410 self.assertEqual(len(self.builds), 6)
1411 self.assertEqual(self.builds[0].name, 'project-test1')
1412 self.assertEqual(self.builds[1].name, 'project-test2')
1413 self.assertEqual(self.builds[2].name, 'project-test1')
1414 self.assertEqual(self.builds[3].name, 'project-test2')
1415 self.assertEqual(self.builds[4].name, 'project-test1')
1416 self.assertEqual(self.builds[5].name, 'project-test2')
1417
1418 self.release(self.builds[2])
1419 self.waitUntilSettled()
1420
James E. Blair972e3c72013-08-29 12:04:55 -07001421 # project-test1 and project-test2 for A
1422 # project-test2 for B
1423 # project-merge for C (without B)
1424 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -07001425 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1426
James E. Blair972e3c72013-08-29 12:04:55 -07001427 self.worker.release('.*-merge')
1428 self.waitUntilSettled()
1429
1430 # project-test1 and project-test2 for A
1431 # project-test2 for B
1432 # project-test1 and project-test2 for C
1433 self.assertEqual(len(self.builds), 5)
1434
James E. Blair0aac4872013-08-23 14:02:38 -07001435 items = self.sched.layout.pipelines['gate'].getAllItems()
1436 builds = items[0].current_build_set.getBuilds()
1437 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1438 self.assertEqual(self.countJobResults(builds, None), 2)
1439 builds = items[1].current_build_set.getBuilds()
1440 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1441 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1442 self.assertEqual(self.countJobResults(builds, None), 1)
1443 builds = items[2].current_build_set.getBuilds()
1444 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -07001445 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -07001446
1447 self.worker.hold_jobs_in_build = False
1448 self.worker.release()
1449 self.waitUntilSettled()
1450
1451 self.assertEqual(len(self.builds), 0)
1452 self.assertEqual(len(self.history), 12)
1453 self.assertEqual(A.data['status'], 'MERGED')
1454 self.assertEqual(B.data['status'], 'NEW')
1455 self.assertEqual(C.data['status'], 'MERGED')
1456 self.assertEqual(A.reported, 2)
1457 self.assertEqual(B.reported, 2)
1458 self.assertEqual(C.reported, 2)
1459
James E. Blaird466dc42012-07-31 10:42:56 -07001460 def test_failed_change_at_head_with_queue(self):
1461 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001462
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001463 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001464 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1465 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1466 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001467 A.addApproval('CRVW', 2)
1468 B.addApproval('CRVW', 2)
1469 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001470
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001471 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001472
1473 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1474 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1475 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1476
1477 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001478 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001479 self.assertEqual(len(self.builds), 0)
1480 self.assertEqual(len(queue), 1)
1481 self.assertEqual(queue[0].name, 'build:project-merge')
1482 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001483
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001484 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001485 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001486 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001487 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001488 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001489 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001490 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001491
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001492 self.assertEqual(len(self.builds), 0)
1493 self.assertEqual(len(queue), 6)
1494 self.assertEqual(queue[0].name, 'build:project-test1')
1495 self.assertEqual(queue[1].name, 'build:project-test2')
1496 self.assertEqual(queue[2].name, 'build:project-test1')
1497 self.assertEqual(queue[3].name, 'build:project-test2')
1498 self.assertEqual(queue[4].name, 'build:project-test1')
1499 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001500
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001501 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001502 self.waitUntilSettled()
1503
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001504 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001505 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001506 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1507 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001508
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001509 self.gearman_server.hold_jobs_in_queue = False
1510 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001511 self.waitUntilSettled()
1512
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001513 self.assertEqual(len(self.builds), 0)
1514 self.assertEqual(len(self.history), 11)
1515 self.assertEqual(A.data['status'], 'NEW')
1516 self.assertEqual(B.data['status'], 'MERGED')
1517 self.assertEqual(C.data['status'], 'MERGED')
1518 self.assertEqual(A.reported, 2)
1519 self.assertEqual(B.reported, 2)
1520 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001521
James E. Blairfef71632013-09-23 11:15:47 -07001522 def test_two_failed_changes_at_head(self):
1523 "Test that changes are reparented correctly if 2 fail at head"
1524
1525 self.worker.hold_jobs_in_build = True
1526 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1527 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1528 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1529 A.addApproval('CRVW', 2)
1530 B.addApproval('CRVW', 2)
1531 C.addApproval('CRVW', 2)
1532
1533 self.worker.addFailTest('project-test1', A)
1534 self.worker.addFailTest('project-test1', B)
1535
1536 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1537 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1538 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1539 self.waitUntilSettled()
1540
1541 self.worker.release('.*-merge')
1542 self.waitUntilSettled()
1543 self.worker.release('.*-merge')
1544 self.waitUntilSettled()
1545 self.worker.release('.*-merge')
1546 self.waitUntilSettled()
1547
1548 self.assertEqual(len(self.builds), 6)
1549 self.assertEqual(self.builds[0].name, 'project-test1')
1550 self.assertEqual(self.builds[1].name, 'project-test2')
1551 self.assertEqual(self.builds[2].name, 'project-test1')
1552 self.assertEqual(self.builds[3].name, 'project-test2')
1553 self.assertEqual(self.builds[4].name, 'project-test1')
1554 self.assertEqual(self.builds[5].name, 'project-test2')
1555
1556 self.assertTrue(self.job_has_changes(self.builds[0], A))
1557 self.assertTrue(self.job_has_changes(self.builds[2], A))
1558 self.assertTrue(self.job_has_changes(self.builds[2], B))
1559 self.assertTrue(self.job_has_changes(self.builds[4], A))
1560 self.assertTrue(self.job_has_changes(self.builds[4], B))
1561 self.assertTrue(self.job_has_changes(self.builds[4], C))
1562
1563 # Fail change B first
1564 self.release(self.builds[2])
1565 self.waitUntilSettled()
1566
1567 # restart of C after B failure
1568 self.worker.release('.*-merge')
1569 self.waitUntilSettled()
1570
1571 self.assertEqual(len(self.builds), 5)
1572 self.assertEqual(self.builds[0].name, 'project-test1')
1573 self.assertEqual(self.builds[1].name, 'project-test2')
1574 self.assertEqual(self.builds[2].name, 'project-test2')
1575 self.assertEqual(self.builds[3].name, 'project-test1')
1576 self.assertEqual(self.builds[4].name, 'project-test2')
1577
1578 self.assertTrue(self.job_has_changes(self.builds[1], A))
1579 self.assertTrue(self.job_has_changes(self.builds[2], A))
1580 self.assertTrue(self.job_has_changes(self.builds[2], B))
1581 self.assertTrue(self.job_has_changes(self.builds[4], A))
1582 self.assertFalse(self.job_has_changes(self.builds[4], B))
1583 self.assertTrue(self.job_has_changes(self.builds[4], C))
1584
1585 # Finish running all passing jobs for change A
1586 self.release(self.builds[1])
1587 self.waitUntilSettled()
1588 # Fail and report change A
1589 self.release(self.builds[0])
1590 self.waitUntilSettled()
1591
1592 # restart of B,C after A failure
1593 self.worker.release('.*-merge')
1594 self.waitUntilSettled()
1595 self.worker.release('.*-merge')
1596 self.waitUntilSettled()
1597
1598 self.assertEqual(len(self.builds), 4)
1599 self.assertEqual(self.builds[0].name, 'project-test1') # B
1600 self.assertEqual(self.builds[1].name, 'project-test2') # B
1601 self.assertEqual(self.builds[2].name, 'project-test1') # C
1602 self.assertEqual(self.builds[3].name, 'project-test2') # C
1603
1604 self.assertFalse(self.job_has_changes(self.builds[1], A))
1605 self.assertTrue(self.job_has_changes(self.builds[1], B))
1606 self.assertFalse(self.job_has_changes(self.builds[1], C))
1607
1608 self.assertFalse(self.job_has_changes(self.builds[2], A))
1609 # After A failed and B and C restarted, B should be back in
1610 # C's tests because it has not failed yet.
1611 self.assertTrue(self.job_has_changes(self.builds[2], B))
1612 self.assertTrue(self.job_has_changes(self.builds[2], C))
1613
1614 self.worker.hold_jobs_in_build = False
1615 self.worker.release()
1616 self.waitUntilSettled()
1617
1618 self.assertEqual(len(self.builds), 0)
1619 self.assertEqual(len(self.history), 21)
1620 self.assertEqual(A.data['status'], 'NEW')
1621 self.assertEqual(B.data['status'], 'NEW')
1622 self.assertEqual(C.data['status'], 'MERGED')
1623 self.assertEqual(A.reported, 2)
1624 self.assertEqual(B.reported, 2)
1625 self.assertEqual(C.reported, 2)
1626
James E. Blair8c803f82012-07-31 16:25:42 -07001627 def test_patch_order(self):
1628 "Test that dependent patches are tested in the right order"
1629 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1630 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1631 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1632 A.addApproval('CRVW', 2)
1633 B.addApproval('CRVW', 2)
1634 C.addApproval('CRVW', 2)
1635
1636 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1637 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1638 M2.setMerged()
1639 M1.setMerged()
1640
1641 # C -> B -> A -> M1 -> M2
1642 # M2 is here to make sure it is never queried. If it is, it
1643 # means zuul is walking down the entire history of merged
1644 # changes.
1645
1646 C.setDependsOn(B, 1)
1647 B.setDependsOn(A, 1)
1648 A.setDependsOn(M1, 1)
1649 M1.setDependsOn(M2, 1)
1650
1651 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1652
1653 self.waitUntilSettled()
1654
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001655 self.assertEqual(A.data['status'], 'NEW')
1656 self.assertEqual(B.data['status'], 'NEW')
1657 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001658
1659 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1660 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1661
1662 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001663 self.assertEqual(M2.queried, 0)
1664 self.assertEqual(A.data['status'], 'MERGED')
1665 self.assertEqual(B.data['status'], 'MERGED')
1666 self.assertEqual(C.data['status'], 'MERGED')
1667 self.assertEqual(A.reported, 2)
1668 self.assertEqual(B.reported, 2)
1669 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001670
James E. Blair0e933c52013-07-11 10:18:52 -07001671 def test_trigger_cache(self):
1672 "Test that the trigger cache operates correctly"
1673 self.worker.hold_jobs_in_build = True
1674
1675 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1676 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1677 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1678 A.addApproval('CRVW', 2)
1679 B.addApproval('CRVW', 2)
1680
1681 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1682 M1.setMerged()
1683
1684 B.setDependsOn(A, 1)
1685 A.setDependsOn(M1, 1)
1686
1687 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1688 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1689
1690 self.waitUntilSettled()
1691
1692 for build in self.builds:
1693 if build.parameters['ZUUL_PIPELINE'] == 'check':
1694 build.release()
1695 self.waitUntilSettled()
1696 for build in self.builds:
1697 if build.parameters['ZUUL_PIPELINE'] == 'check':
1698 build.release()
1699 self.waitUntilSettled()
1700
1701 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1702 self.waitUntilSettled()
1703
James E. Blair6c358e72013-07-29 17:06:47 -07001704 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001705 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001706 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001707
1708 self.worker.hold_jobs_in_build = False
1709 self.worker.release()
1710 self.waitUntilSettled()
1711
1712 self.assertEqual(A.data['status'], 'MERGED')
1713 self.assertEqual(B.data['status'], 'MERGED')
1714 self.assertEqual(A.queried, 2) # Initial and isMerged
1715 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1716
James E. Blair8c803f82012-07-31 16:25:42 -07001717 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001718 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001719 # TODO: move to test_gerrit (this is a unit test!)
1720 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001721 trigger = self.sched.layout.pipelines['gate'].trigger
1722 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001723 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001724 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001725
1726 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001727 a = trigger.getChange(1, 2, refresh=True)
1728 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001729
1730 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001731 a = trigger.getChange(1, 2, refresh=True)
1732 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1733 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001734
1735 def test_build_configuration(self):
1736 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001737
1738 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001739 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1740 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1741 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1742 A.addApproval('CRVW', 2)
1743 B.addApproval('CRVW', 2)
1744 C.addApproval('CRVW', 2)
1745 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1746 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1747 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1748 self.waitUntilSettled()
1749
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001750 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001751 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001752 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001753 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001754 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001755 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001756 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001757 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1758 self.gearman_server.hold_jobs_in_queue = False
1759 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001760 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001761
Monty Taylorbc758832013-06-17 17:22:42 -04001762 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001763 repo = git.Repo(path)
1764 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1765 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001766 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001767 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001768
1769 def test_build_configuration_conflict(self):
1770 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001771
1772 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001773 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1774 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001775 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001776 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1777 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001778 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001779 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1780 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001781 A.addApproval('CRVW', 2)
1782 B.addApproval('CRVW', 2)
1783 C.addApproval('CRVW', 2)
1784 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1785 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1786 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1787 self.waitUntilSettled()
1788
James E. Blair6736beb2013-07-11 15:18:15 -07001789 self.assertEqual(A.reported, 1)
1790 self.assertEqual(B.reported, 1)
1791 self.assertEqual(C.reported, 1)
1792
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001793 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001794 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001795 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001796 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001797 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001798 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001799
1800 self.assertEqual(len(self.history), 2) # A and C merge jobs
1801
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001802 self.gearman_server.hold_jobs_in_queue = False
1803 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001804 self.waitUntilSettled()
1805
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001806 self.assertEqual(A.data['status'], 'MERGED')
1807 self.assertEqual(B.data['status'], 'NEW')
1808 self.assertEqual(C.data['status'], 'MERGED')
1809 self.assertEqual(A.reported, 2)
1810 self.assertEqual(B.reported, 2)
1811 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001812 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001813
James E. Blairdaabed22012-08-15 15:38:57 -07001814 def test_post(self):
1815 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001816
Zhongyue Luo5d556072012-09-21 02:00:47 +09001817 e = {
1818 "type": "ref-updated",
1819 "submitter": {
1820 "name": "User Name",
1821 },
1822 "refUpdate": {
1823 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1824 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1825 "refName": "master",
1826 "project": "org/project",
1827 }
1828 }
James E. Blairdaabed22012-08-15 15:38:57 -07001829 self.fake_gerrit.addEvent(e)
1830 self.waitUntilSettled()
1831
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001832 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001833 self.assertEqual(len(self.history), 1)
1834 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001835
1836 def test_build_configuration_branch(self):
1837 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001838
1839 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001840 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1841 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1842 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1843 A.addApproval('CRVW', 2)
1844 B.addApproval('CRVW', 2)
1845 C.addApproval('CRVW', 2)
1846 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1847 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1848 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1849 self.waitUntilSettled()
1850
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001851 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001852 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001853 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001854 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001855 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001856 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001857 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001858 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1859 self.gearman_server.hold_jobs_in_queue = False
1860 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001861 self.waitUntilSettled()
1862
Monty Taylorbc758832013-06-17 17:22:42 -04001863 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001864 repo = git.Repo(path)
1865 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1866 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001867 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001868 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001869
1870 def test_build_configuration_branch_interaction(self):
1871 "Test that switching between branches works"
1872 self.test_build_configuration()
1873 self.test_build_configuration_branch()
1874 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001875 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001876 repo = git.Repo(path)
1877 repo.heads.master.commit = repo.commit('init')
1878 self.test_build_configuration()
1879
1880 def test_build_configuration_multi_branch(self):
1881 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001882
1883 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001884 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1885 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1886 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1887 A.addApproval('CRVW', 2)
1888 B.addApproval('CRVW', 2)
1889 C.addApproval('CRVW', 2)
1890 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1891 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1892 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1893 self.waitUntilSettled()
1894
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001895 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001896 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001897 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001898 job_B = None
1899 for job in queue:
1900 if 'project-merge' in job.name:
1901 job_B = job
1902 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001903 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001904 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001905 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001906 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001907 for job in queue:
1908 if 'project-merge' in job.name:
1909 job_C = job
1910 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001911 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001912 self.gearman_server.hold_jobs_in_queue = False
1913 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001914 self.waitUntilSettled()
1915
Monty Taylorbc758832013-06-17 17:22:42 -04001916 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001917 repo = git.Repo(path)
1918
1919 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001920 for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001921 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001922 correct_messages = ['initial commit', 'A-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001923 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001924
1925 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001926 for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001927 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001928 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001929 self.assertEqual(repo_messages, correct_messages)
James E. Blair7f71c802012-08-22 13:04:32 -07001930
1931 def test_one_job_project(self):
1932 "Test that queueing works with one job"
1933 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1934 'master', 'A')
1935 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1936 'master', 'B')
1937 A.addApproval('CRVW', 2)
1938 B.addApproval('CRVW', 2)
1939 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1940 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1941 self.waitUntilSettled()
1942
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001943 self.assertEqual(A.data['status'], 'MERGED')
1944 self.assertEqual(A.reported, 2)
1945 self.assertEqual(B.data['status'], 'MERGED')
1946 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001947
Antoine Musso80edd5a2013-02-13 15:37:53 +01001948 def test_job_from_templates_launched(self):
1949 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001950
Antoine Musso80edd5a2013-02-13 15:37:53 +01001951 A = self.fake_gerrit.addFakeChange(
1952 'org/templated-project', 'master', 'A')
1953 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1954 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001955
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001956 self.assertEqual(self.getJobFromHistory('project-test1').result,
1957 'SUCCESS')
1958 self.assertEqual(self.getJobFromHistory('project-test2').result,
1959 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001960
James E. Blaircaec0c52012-08-22 14:52:22 -07001961 def test_dependent_changes_dequeue(self):
1962 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001963
James E. Blaircaec0c52012-08-22 14:52:22 -07001964 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1965 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1966 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1967 A.addApproval('CRVW', 2)
1968 B.addApproval('CRVW', 2)
1969 C.addApproval('CRVW', 2)
1970
1971 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1972 M1.setMerged()
1973
1974 # C -> B -> A -> M1
1975
1976 C.setDependsOn(B, 1)
1977 B.setDependsOn(A, 1)
1978 A.setDependsOn(M1, 1)
1979
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001980 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001981
1982 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1983 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1984 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1985
1986 self.waitUntilSettled()
1987
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001988 self.assertEqual(A.data['status'], 'NEW')
1989 self.assertEqual(A.reported, 2)
1990 self.assertEqual(B.data['status'], 'NEW')
1991 self.assertEqual(B.reported, 2)
1992 self.assertEqual(C.data['status'], 'NEW')
1993 self.assertEqual(C.reported, 2)
1994 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001995
James E. Blair972e3c72013-08-29 12:04:55 -07001996 def test_failing_dependent_changes(self):
1997 "Test that failing dependent patches are taken out of stream"
1998 self.worker.hold_jobs_in_build = True
1999 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2000 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2001 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2002 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2003 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
2004 A.addApproval('CRVW', 2)
2005 B.addApproval('CRVW', 2)
2006 C.addApproval('CRVW', 2)
2007 D.addApproval('CRVW', 2)
2008 E.addApproval('CRVW', 2)
2009
2010 # E, D -> C -> B, A
2011
2012 D.setDependsOn(C, 1)
2013 C.setDependsOn(B, 1)
2014
2015 self.worker.addFailTest('project-test1', B)
2016
2017 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2018 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2019 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2020 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2021 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2022
2023 self.waitUntilSettled()
2024 self.worker.release('.*-merge')
2025 self.waitUntilSettled()
2026 self.worker.release('.*-merge')
2027 self.waitUntilSettled()
2028 self.worker.release('.*-merge')
2029 self.waitUntilSettled()
2030 self.worker.release('.*-merge')
2031 self.waitUntilSettled()
2032 self.worker.release('.*-merge')
2033 self.waitUntilSettled()
2034
2035 self.worker.hold_jobs_in_build = False
2036 for build in self.builds:
2037 if build.parameters['ZUUL_CHANGE'] != '1':
2038 build.release()
2039 self.waitUntilSettled()
2040
2041 self.worker.release()
2042 self.waitUntilSettled()
2043
2044 self.assertEqual(A.data['status'], 'MERGED')
2045 self.assertEqual(A.reported, 2)
2046 self.assertEqual(B.data['status'], 'NEW')
2047 self.assertEqual(B.reported, 2)
2048 self.assertEqual(C.data['status'], 'NEW')
2049 self.assertEqual(C.reported, 2)
2050 self.assertEqual(D.data['status'], 'NEW')
2051 self.assertEqual(D.reported, 2)
2052 self.assertEqual(E.data['status'], 'MERGED')
2053 self.assertEqual(E.reported, 2)
2054 self.assertEqual(len(self.history), 18)
2055
James E. Blairec590122012-08-22 15:19:31 -07002056 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08002057 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07002058 # If it's dequeued more than once, we should see extra
2059 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07002060
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002061 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07002062 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2063 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2064 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2065 A.addApproval('CRVW', 2)
2066 B.addApproval('CRVW', 2)
2067 C.addApproval('CRVW', 2)
2068
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002069 self.worker.addFailTest('project1-test1', A)
2070 self.worker.addFailTest('project1-test2', A)
2071 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07002072
2073 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2074 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2075 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2076
2077 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07002078
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002079 self.assertEqual(len(self.builds), 1)
2080 self.assertEqual(self.builds[0].name, 'project1-merge')
2081 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07002082
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002083 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002084 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002085 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002086 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002087 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002088 self.waitUntilSettled()
2089
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002090 self.assertEqual(len(self.builds), 9)
2091 self.assertEqual(self.builds[0].name, 'project1-test1')
2092 self.assertEqual(self.builds[1].name, 'project1-test2')
2093 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
2094 self.assertEqual(self.builds[3].name, 'project1-test1')
2095 self.assertEqual(self.builds[4].name, 'project1-test2')
2096 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
2097 self.assertEqual(self.builds[6].name, 'project1-test1')
2098 self.assertEqual(self.builds[7].name, 'project1-test2')
2099 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07002100
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002101 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07002102 self.waitUntilSettled()
2103
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002104 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
2105 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07002106
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002107 self.worker.hold_jobs_in_build = False
2108 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07002109 self.waitUntilSettled()
2110
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002111 self.assertEqual(len(self.builds), 0)
2112 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07002113
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002114 self.assertEqual(A.data['status'], 'NEW')
2115 self.assertEqual(B.data['status'], 'MERGED')
2116 self.assertEqual(C.data['status'], 'MERGED')
2117 self.assertEqual(A.reported, 2)
2118 self.assertEqual(B.reported, 2)
2119 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07002120
2121 def test_nonvoting_job(self):
2122 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002123
James E. Blair4ec821f2012-08-23 15:28:28 -07002124 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
2125 'master', 'A')
2126 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002127 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07002128 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2129
2130 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07002131
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002132 self.assertEqual(A.data['status'], 'MERGED')
2133 self.assertEqual(A.reported, 2)
2134 self.assertEqual(
2135 self.getJobFromHistory('nonvoting-project-merge').result,
2136 'SUCCESS')
2137 self.assertEqual(
2138 self.getJobFromHistory('nonvoting-project-test1').result,
2139 'SUCCESS')
2140 self.assertEqual(
2141 self.getJobFromHistory('nonvoting-project-test2').result,
2142 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07002143
2144 def test_check_queue_success(self):
2145 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002146
James E. Blaire0487072012-08-29 17:38:31 -07002147 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2148 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2149
2150 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002151
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002152 self.assertEqual(A.data['status'], 'NEW')
2153 self.assertEqual(A.reported, 1)
2154 self.assertEqual(self.getJobFromHistory('project-merge').result,
2155 'SUCCESS')
2156 self.assertEqual(self.getJobFromHistory('project-test1').result,
2157 'SUCCESS')
2158 self.assertEqual(self.getJobFromHistory('project-test2').result,
2159 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07002160
2161 def test_check_queue_failure(self):
2162 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002163
James E. Blaire0487072012-08-29 17:38:31 -07002164 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002165 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07002166 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2167
2168 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002169
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002170 self.assertEqual(A.data['status'], 'NEW')
2171 self.assertEqual(A.reported, 1)
2172 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07002173 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002174 self.assertEqual(self.getJobFromHistory('project-test1').result,
2175 'SUCCESS')
2176 self.assertEqual(self.getJobFromHistory('project-test2').result,
2177 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07002178
2179 def test_dependent_behind_dequeue(self):
2180 "test that dependent changes behind dequeued changes work"
2181 # This complicated test is a reproduction of a real life bug
2182 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07002183
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002184 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07002185 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2186 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2187 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2188 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2189 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
2190 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
2191 D.setDependsOn(C, 1)
2192 E.setDependsOn(D, 1)
2193 A.addApproval('CRVW', 2)
2194 B.addApproval('CRVW', 2)
2195 C.addApproval('CRVW', 2)
2196 D.addApproval('CRVW', 2)
2197 E.addApproval('CRVW', 2)
2198 F.addApproval('CRVW', 2)
2199
2200 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07002201
2202 # Change object re-use in the gerrit trigger is hidden if
2203 # changes are added in quick succession; waiting makes it more
2204 # like real life.
2205 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2206 self.waitUntilSettled()
2207 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2208 self.waitUntilSettled()
2209
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002210 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002211 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002212 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002213 self.waitUntilSettled()
2214
2215 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2216 self.waitUntilSettled()
2217 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2218 self.waitUntilSettled()
2219 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2220 self.waitUntilSettled()
2221 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
2222 self.waitUntilSettled()
2223
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002224 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002225 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002226 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002227 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002228 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002229 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002230 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002231 self.waitUntilSettled()
2232
2233 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002234
2235 # Grab pointers to the jobs we want to release before
2236 # releasing any, because list indexes may change as
2237 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002238 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002239 a.release()
2240 b.release()
2241 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002242 self.waitUntilSettled()
2243
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002244 self.worker.hold_jobs_in_build = False
2245 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002246 self.waitUntilSettled()
2247
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002248 self.assertEqual(A.data['status'], 'NEW')
2249 self.assertEqual(B.data['status'], 'MERGED')
2250 self.assertEqual(C.data['status'], 'MERGED')
2251 self.assertEqual(D.data['status'], 'MERGED')
2252 self.assertEqual(E.data['status'], 'MERGED')
2253 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002254
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002255 self.assertEqual(A.reported, 2)
2256 self.assertEqual(B.reported, 2)
2257 self.assertEqual(C.reported, 2)
2258 self.assertEqual(D.reported, 2)
2259 self.assertEqual(E.reported, 2)
2260 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002261
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002262 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2263 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002264
2265 def test_merger_repack(self):
2266 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002267
James E. Blair05fed602012-09-07 12:45:24 -07002268 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2269 A.addApproval('CRVW', 2)
2270 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2271 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002272 self.assertEqual(self.getJobFromHistory('project-merge').result,
2273 'SUCCESS')
2274 self.assertEqual(self.getJobFromHistory('project-test1').result,
2275 'SUCCESS')
2276 self.assertEqual(self.getJobFromHistory('project-test2').result,
2277 'SUCCESS')
2278 self.assertEqual(A.data['status'], 'MERGED')
2279 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07002280 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002281 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002282
Monty Taylorbc758832013-06-17 17:22:42 -04002283 path = os.path.join(self.git_root, "org/project")
2284 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002285
2286 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2287 A.addApproval('CRVW', 2)
2288 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2289 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002290 self.assertEqual(self.getJobFromHistory('project-merge').result,
2291 'SUCCESS')
2292 self.assertEqual(self.getJobFromHistory('project-test1').result,
2293 'SUCCESS')
2294 self.assertEqual(self.getJobFromHistory('project-test2').result,
2295 'SUCCESS')
2296 self.assertEqual(A.data['status'], 'MERGED')
2297 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002298
James E. Blair4886f282012-11-15 09:27:33 -08002299 def test_merger_repack_large_change(self):
2300 "Test that the merger works with large changes after a repack"
2301 # https://bugs.launchpad.net/zuul/+bug/1078946
2302 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2303 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002304 path = os.path.join(self.upstream_root, "org/project1")
2305 print repack_repo(path)
2306 path = os.path.join(self.git_root, "org/project1")
2307 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002308
2309 A.addApproval('CRVW', 2)
2310 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2311 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002312 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2313 'SUCCESS')
2314 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2315 'SUCCESS')
2316 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2317 'SUCCESS')
2318 self.assertEqual(A.data['status'], 'MERGED')
2319 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002320
James E. Blair7ee88a22012-09-12 18:59:31 +02002321 def test_nonexistent_job(self):
2322 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002323 # Set to the state immediately after a restart
2324 self.resetGearmanServer()
2325 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002326
2327 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2328 A.addApproval('CRVW', 2)
2329 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2330 # There may be a thread about to report a lost change
2331 while A.reported < 2:
2332 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002333 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002334 self.assertFalse(job_names)
2335 self.assertEqual(A.data['status'], 'NEW')
2336 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002337 self.assertEmptyQueues()
2338
2339 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002340 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002341 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2342 A.addApproval('CRVW', 2)
2343 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2344 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002345 self.assertEqual(self.getJobFromHistory('project-merge').result,
2346 'SUCCESS')
2347 self.assertEqual(self.getJobFromHistory('project-test1').result,
2348 'SUCCESS')
2349 self.assertEqual(self.getJobFromHistory('project-test2').result,
2350 'SUCCESS')
2351 self.assertEqual(A.data['status'], 'MERGED')
2352 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002353
2354 def test_single_nonexistent_post_job(self):
2355 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002356 e = {
2357 "type": "ref-updated",
2358 "submitter": {
2359 "name": "User Name",
2360 },
2361 "refUpdate": {
2362 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2363 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2364 "refName": "master",
2365 "project": "org/project",
2366 }
2367 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002368 # Set to the state immediately after a restart
2369 self.resetGearmanServer()
2370 self.launcher.negative_function_cache_ttl = 0
2371
James E. Blairf62d4282012-12-31 17:01:50 -08002372 self.fake_gerrit.addEvent(e)
2373 self.waitUntilSettled()
2374
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002375 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002376
2377 def test_new_patchset_dequeues_old(self):
2378 "Test that a new patchset causes the old to be dequeued"
2379 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002380 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002381 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2382 M.setMerged()
2383
2384 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2385 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2386 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2387 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2388 A.addApproval('CRVW', 2)
2389 B.addApproval('CRVW', 2)
2390 C.addApproval('CRVW', 2)
2391 D.addApproval('CRVW', 2)
2392
2393 C.setDependsOn(B, 1)
2394 B.setDependsOn(A, 1)
2395 A.setDependsOn(M, 1)
2396
2397 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2398 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2399 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2400 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2401 self.waitUntilSettled()
2402
2403 B.addPatchset()
2404 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2405 self.waitUntilSettled()
2406
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002407 self.worker.hold_jobs_in_build = False
2408 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002409 self.waitUntilSettled()
2410
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002411 self.assertEqual(A.data['status'], 'MERGED')
2412 self.assertEqual(A.reported, 2)
2413 self.assertEqual(B.data['status'], 'NEW')
2414 self.assertEqual(B.reported, 2)
2415 self.assertEqual(C.data['status'], 'NEW')
2416 self.assertEqual(C.reported, 2)
2417 self.assertEqual(D.data['status'], 'MERGED')
2418 self.assertEqual(D.reported, 2)
2419 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002420
Arx Cruzb1b010d2013-10-28 19:49:59 -02002421 def test_zuul_url_return(self):
2422 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
2423 self.assertTrue(self.sched.config.has_option('zuul', 'zuul_url'))
2424 self.worker.hold_jobs_in_build = True
2425
2426 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2427 A.addApproval('CRVW', 2)
2428 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2429 self.waitUntilSettled()
2430
2431 self.assertEqual(len(self.builds), 1)
2432 for build in self.builds:
2433 self.assertTrue('ZUUL_URL' in build.parameters)
2434
2435 self.worker.hold_jobs_in_build = False
2436 self.worker.release()
2437 self.waitUntilSettled()
2438
James E. Blair2fa50962013-01-30 21:50:41 -08002439 def test_new_patchset_dequeues_old_on_head(self):
2440 "Test that a new patchset causes the old to be dequeued (at head)"
2441 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002442 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002443 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2444 M.setMerged()
2445 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2446 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2447 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2448 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2449 A.addApproval('CRVW', 2)
2450 B.addApproval('CRVW', 2)
2451 C.addApproval('CRVW', 2)
2452 D.addApproval('CRVW', 2)
2453
2454 C.setDependsOn(B, 1)
2455 B.setDependsOn(A, 1)
2456 A.setDependsOn(M, 1)
2457
2458 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2459 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2460 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2461 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2462 self.waitUntilSettled()
2463
2464 A.addPatchset()
2465 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2466 self.waitUntilSettled()
2467
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002468 self.worker.hold_jobs_in_build = False
2469 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002470 self.waitUntilSettled()
2471
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002472 self.assertEqual(A.data['status'], 'NEW')
2473 self.assertEqual(A.reported, 2)
2474 self.assertEqual(B.data['status'], 'NEW')
2475 self.assertEqual(B.reported, 2)
2476 self.assertEqual(C.data['status'], 'NEW')
2477 self.assertEqual(C.reported, 2)
2478 self.assertEqual(D.data['status'], 'MERGED')
2479 self.assertEqual(D.reported, 2)
2480 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002481
2482 def test_new_patchset_dequeues_old_without_dependents(self):
2483 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002484 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002485 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2486 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2487 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2488 A.addApproval('CRVW', 2)
2489 B.addApproval('CRVW', 2)
2490 C.addApproval('CRVW', 2)
2491
2492 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2493 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2494 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2495 self.waitUntilSettled()
2496
2497 B.addPatchset()
2498 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2499 self.waitUntilSettled()
2500
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002501 self.worker.hold_jobs_in_build = False
2502 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002503 self.waitUntilSettled()
2504
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002505 self.assertEqual(A.data['status'], 'MERGED')
2506 self.assertEqual(A.reported, 2)
2507 self.assertEqual(B.data['status'], 'NEW')
2508 self.assertEqual(B.reported, 2)
2509 self.assertEqual(C.data['status'], 'MERGED')
2510 self.assertEqual(C.reported, 2)
2511 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002512
2513 def test_new_patchset_dequeues_old_independent_queue(self):
2514 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002515 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002516 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2517 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2518 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2519 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2520 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2521 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(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'], 'NEW')
2533 self.assertEqual(A.reported, 1)
2534 self.assertEqual(B.data['status'], 'NEW')
2535 self.assertEqual(B.reported, 1)
2536 self.assertEqual(C.data['status'], 'NEW')
2537 self.assertEqual(C.reported, 1)
2538 self.assertEqual(len(self.history), 10)
2539 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002540
2541 def test_zuul_refs(self):
2542 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002543 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002544 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2545 M1.setMerged()
2546 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2547 M2.setMerged()
2548
2549 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2550 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2551 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2552 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2553 A.addApproval('CRVW', 2)
2554 B.addApproval('CRVW', 2)
2555 C.addApproval('CRVW', 2)
2556 D.addApproval('CRVW', 2)
2557 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2558 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2559 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2560 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2561
2562 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002563 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002564 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002565 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002566 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002567 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002568 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002569 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002570 self.waitUntilSettled()
2571
James E. Blair7d0dedc2013-02-21 17:26:09 -08002572 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002573 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002574 if x.parameters['ZUUL_CHANGE'] == '3':
2575 a_zref = x.parameters['ZUUL_REF']
2576 if x.parameters['ZUUL_CHANGE'] == '4':
2577 b_zref = x.parameters['ZUUL_REF']
2578 if x.parameters['ZUUL_CHANGE'] == '5':
2579 c_zref = x.parameters['ZUUL_REF']
2580 if x.parameters['ZUUL_CHANGE'] == '6':
2581 d_zref = x.parameters['ZUUL_REF']
2582
2583 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002584 self.assertIsNotNone(a_zref)
2585 self.assertIsNotNone(b_zref)
2586 self.assertIsNotNone(c_zref)
2587 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002588
2589 # And they should all be different
2590 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002591 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002592
2593 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002594 self.assertTrue(self.ref_has_change(a_zref, A))
2595 self.assertFalse(self.ref_has_change(a_zref, B))
2596 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002597
2598 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002599 self.assertTrue(self.ref_has_change(b_zref, A))
2600 self.assertTrue(self.ref_has_change(b_zref, B))
2601 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002602
2603 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002604 self.assertTrue(self.ref_has_change(c_zref, A))
2605 self.assertTrue(self.ref_has_change(c_zref, B))
2606 self.assertTrue(self.ref_has_change(c_zref, C))
2607 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002608
2609 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002610 self.assertTrue(self.ref_has_change(d_zref, A))
2611 self.assertTrue(self.ref_has_change(d_zref, B))
2612 self.assertTrue(self.ref_has_change(d_zref, C))
2613 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002614
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002615 self.worker.hold_jobs_in_build = False
2616 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002617 self.waitUntilSettled()
2618
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002619 self.assertEqual(A.data['status'], 'MERGED')
2620 self.assertEqual(A.reported, 2)
2621 self.assertEqual(B.data['status'], 'MERGED')
2622 self.assertEqual(B.reported, 2)
2623 self.assertEqual(C.data['status'], 'MERGED')
2624 self.assertEqual(C.reported, 2)
2625 self.assertEqual(D.data['status'], 'MERGED')
2626 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002627
James E. Blair4a28a882013-08-23 15:17:33 -07002628 def test_rerun_on_error(self):
2629 "Test that if a worker fails to run a job, it is run again"
2630 self.worker.hold_jobs_in_build = True
2631 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2632 A.addApproval('CRVW', 2)
2633 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2634 self.waitUntilSettled()
2635
2636 self.builds[0].run_error = True
2637 self.worker.hold_jobs_in_build = False
2638 self.worker.release()
2639 self.waitUntilSettled()
2640 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2641 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2642
James E. Blair412e5582013-04-22 15:50:12 -07002643 def test_statsd(self):
2644 "Test each of the statsd methods used in the scheduler"
2645 import extras
2646 statsd = extras.try_import('statsd.statsd')
2647 statsd.incr('test-incr')
2648 statsd.timing('test-timing', 3)
2649 statsd.gauge('test-guage', 12)
2650 self.assertReportedStat('test-incr', '1|c')
2651 self.assertReportedStat('test-timing', '3|ms')
2652 self.assertReportedStat('test-guage', '12|g')
2653
James E. Blair70c71582013-03-06 08:50:50 -08002654 def test_file_jobs(self):
2655 "Test that file jobs run only when appropriate"
2656 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2657 A.addPatchset(['pip-requires'])
2658 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2659 A.addApproval('CRVW', 2)
2660 B.addApproval('CRVW', 2)
2661 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2662 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2663 self.waitUntilSettled()
2664
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002665 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002666 if x.name == 'project-testfile']
2667
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002668 self.assertEqual(len(testfile_jobs), 1)
2669 self.assertEqual(testfile_jobs[0].changes, '1,2')
2670 self.assertEqual(A.data['status'], 'MERGED')
2671 self.assertEqual(A.reported, 2)
2672 self.assertEqual(B.data['status'], 'MERGED')
2673 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002674
2675 def test_test_config(self):
2676 "Test that we can test the config"
2677 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002678 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002679 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002680 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002681
2682 def test_build_description(self):
2683 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002684 self.worker.registerFunction('set_description:' +
2685 self.worker.worker_id)
2686
2687 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2688 A.addApproval('CRVW', 2)
2689 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2690 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002691 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002692 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002693 self.assertTrue(re.search("Branch.*master", desc))
2694 self.assertTrue(re.search("Pipeline.*gate", desc))
2695 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2696 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2697 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2698 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002699
James E. Blair64ed6f22013-07-10 14:07:23 -07002700 def test_queue_precedence(self):
2701 "Test that queue precedence works"
2702
2703 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002704 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002705 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2706 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2707 A.addApproval('CRVW', 2)
2708 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2709
2710 self.waitUntilSettled()
2711 self.gearman_server.hold_jobs_in_queue = False
2712 self.gearman_server.release()
2713 self.waitUntilSettled()
2714
James E. Blair8de58bd2013-07-18 16:23:33 -07002715 # Run one build at a time to ensure non-race order:
2716 for x in range(6):
2717 self.release(self.builds[0])
2718 self.waitUntilSettled()
2719 self.worker.hold_jobs_in_build = False
2720 self.waitUntilSettled()
2721
James E. Blair64ed6f22013-07-10 14:07:23 -07002722 self.log.debug(self.history)
2723 self.assertEqual(self.history[0].pipeline, 'gate')
2724 self.assertEqual(self.history[1].pipeline, 'check')
2725 self.assertEqual(self.history[2].pipeline, 'gate')
2726 self.assertEqual(self.history[3].pipeline, 'gate')
2727 self.assertEqual(self.history[4].pipeline, 'check')
2728 self.assertEqual(self.history[5].pipeline, 'check')
2729
James E. Blair1843a552013-07-03 14:19:52 -07002730 def test_json_status(self):
2731 "Test that we can retrieve JSON status info"
2732 self.worker.hold_jobs_in_build = True
2733 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2734 A.addApproval('CRVW', 2)
2735 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2736 self.waitUntilSettled()
2737
2738 port = self.webapp.server.socket.getsockname()[1]
2739
2740 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2741 data = f.read()
2742
2743 self.worker.hold_jobs_in_build = False
2744 self.worker.release()
2745 self.waitUntilSettled()
2746
2747 data = json.loads(data)
2748 status_jobs = set()
2749 for p in data['pipelines']:
2750 for q in p['change_queues']:
2751 for head in q['heads']:
2752 for change in head:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002753 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002754 for job in change['jobs']:
2755 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002756 self.assertIn('project-merge', status_jobs)
2757 self.assertIn('project-test1', status_jobs)
2758 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002759
James E. Blairc3d428e2013-12-03 15:06:48 -08002760 def test_merging_queues(self):
2761 "Test that transitively-connected change queues are merged"
2762 self.config.set('zuul', 'layout_config',
2763 'tests/fixtures/layout-merge-queues.yaml')
2764 self.sched.reconfigure(self.config)
2765 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
2766
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002767 def test_node_label(self):
2768 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002769 self.worker.registerFunction('build:node-project-test1:debian')
2770
2771 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2772 A.addApproval('CRVW', 2)
2773 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2774 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002775
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002776 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2777 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2778 'debian')
2779 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002780
2781 def test_live_reconfiguration(self):
2782 "Test that live reconfiguration works"
2783 self.worker.hold_jobs_in_build = True
2784 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2785 A.addApproval('CRVW', 2)
2786 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2787 self.waitUntilSettled()
2788
2789 self.sched.reconfigure(self.config)
2790
2791 self.worker.hold_jobs_in_build = False
2792 self.worker.release()
2793 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002794 self.assertEqual(self.getJobFromHistory('project-merge').result,
2795 'SUCCESS')
2796 self.assertEqual(self.getJobFromHistory('project-test1').result,
2797 'SUCCESS')
2798 self.assertEqual(self.getJobFromHistory('project-test2').result,
2799 'SUCCESS')
2800 self.assertEqual(A.data['status'], 'MERGED')
2801 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002802
James E. Blaire712d9f2013-07-31 11:40:11 -07002803 def test_live_reconfiguration_functions(self):
2804 "Test live reconfiguration with a custom function"
2805 self.worker.registerFunction('build:node-project-test1:debian')
2806 self.worker.registerFunction('build:node-project-test1:wheezy')
2807 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2808 A.addApproval('CRVW', 2)
2809 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2810 self.waitUntilSettled()
2811
2812 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2813 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2814 'debian')
2815 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2816
2817 self.config.set('zuul', 'layout_config',
2818 'tests/fixtures/layout-live-'
2819 'reconfiguration-functions.yaml')
2820 self.sched.reconfigure(self.config)
2821 self.worker.build_history = []
2822
2823 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
2824 B.addApproval('CRVW', 2)
2825 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2826 self.waitUntilSettled()
2827
2828 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2829 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2830 'wheezy')
2831 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2832
James E. Blair287c06d2013-07-24 10:39:30 -07002833 def test_delayed_repo_init(self):
2834 self.config.set('zuul', 'layout_config',
2835 'tests/fixtures/layout-delayed-repo-init.yaml')
2836 self.sched.reconfigure(self.config)
2837
2838 self.init_repo("org/new-project")
2839 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2840
2841 A.addApproval('CRVW', 2)
2842 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2843 self.waitUntilSettled()
2844 self.assertEqual(self.getJobFromHistory('project-merge').result,
2845 'SUCCESS')
2846 self.assertEqual(self.getJobFromHistory('project-test1').result,
2847 'SUCCESS')
2848 self.assertEqual(self.getJobFromHistory('project-test2').result,
2849 'SUCCESS')
2850 self.assertEqual(A.data['status'], 'MERGED')
2851 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002852
Clark Boylan6dbbc482013-10-18 10:57:31 -07002853 def test_repo_deleted(self):
2854 self.config.set('zuul', 'layout_config',
2855 'tests/fixtures/layout-repo-deleted.yaml')
2856 self.sched.reconfigure(self.config)
2857
2858 self.init_repo("org/delete-project")
2859 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2860
2861 A.addApproval('CRVW', 2)
2862 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2863 self.waitUntilSettled()
2864 self.assertEqual(self.getJobFromHistory('project-merge').result,
2865 'SUCCESS')
2866 self.assertEqual(self.getJobFromHistory('project-test1').result,
2867 'SUCCESS')
2868 self.assertEqual(self.getJobFromHistory('project-test2').result,
2869 'SUCCESS')
2870 self.assertEqual(A.data['status'], 'MERGED')
2871 self.assertEqual(A.reported, 2)
2872
2873 # Delete org/new-project zuul repo. Should be recloned.
2874 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
2875
2876 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2877
2878 B.addApproval('CRVW', 2)
2879 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2880 self.waitUntilSettled()
2881 self.assertEqual(self.getJobFromHistory('project-merge').result,
2882 'SUCCESS')
2883 self.assertEqual(self.getJobFromHistory('project-test1').result,
2884 'SUCCESS')
2885 self.assertEqual(self.getJobFromHistory('project-test2').result,
2886 'SUCCESS')
2887 self.assertEqual(B.data['status'], 'MERGED')
2888 self.assertEqual(B.reported, 2)
2889
James E. Blair63bb0ef2013-07-29 17:14:51 -07002890 def test_timer(self):
2891 "Test that a periodic job is triggered"
2892 self.worker.hold_jobs_in_build = True
2893 self.config.set('zuul', 'layout_config',
2894 'tests/fixtures/layout-timer.yaml')
2895 self.sched.reconfigure(self.config)
2896 self.registerJobs()
2897
2898 start = time.time()
2899 failed = True
2900 while ((time.time() - start) < 30):
2901 if len(self.builds) == 2:
2902 failed = False
2903 break
2904 else:
2905 time.sleep(1)
2906
2907 if failed:
2908 raise Exception("Expected jobs never ran")
2909
2910 self.waitUntilSettled()
2911 port = self.webapp.server.socket.getsockname()[1]
2912
2913 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2914 data = f.read()
2915
2916 self.worker.hold_jobs_in_build = False
2917 self.worker.release()
2918 self.waitUntilSettled()
2919
2920 self.assertEqual(self.getJobFromHistory(
2921 'project-bitrot-stable-old').result, 'SUCCESS')
2922 self.assertEqual(self.getJobFromHistory(
2923 'project-bitrot-stable-older').result, 'SUCCESS')
2924
2925 data = json.loads(data)
2926 status_jobs = set()
2927 for p in data['pipelines']:
2928 for q in p['change_queues']:
2929 for head in q['heads']:
2930 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002931 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002932 for job in change['jobs']:
2933 status_jobs.add(job['name'])
2934 self.assertIn('project-bitrot-stable-old', status_jobs)
2935 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002936
2937 def test_check_smtp_pool(self):
2938 self.config.set('zuul', 'layout_config',
2939 'tests/fixtures/layout-smtp.yaml')
2940 self.sched.reconfigure(self.config)
2941
2942 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2943 self.waitUntilSettled()
2944
2945 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2946 self.waitUntilSettled()
2947
2948 self.assertEqual(len(FakeSMTP.messages), 2)
2949
2950 # A.messages only holds what FakeGerrit places in it. Thus we
2951 # work on the knowledge of what the first message should be as
2952 # it is only configured to go to SMTP.
2953
2954 self.assertEqual('zuul@example.com',
2955 FakeSMTP.messages[0]['from_email'])
2956 self.assertEqual(['you@example.com'],
2957 FakeSMTP.messages[0]['to_email'])
2958 self.assertEqual('Starting check jobs.',
2959 FakeSMTP.messages[0]['body'])
2960
2961 self.assertEqual('zuul_from@example.com',
2962 FakeSMTP.messages[1]['from_email'])
2963 self.assertEqual(['alternative_me@example.com'],
2964 FakeSMTP.messages[1]['to_email'])
2965 self.assertEqual(A.messages[0],
2966 FakeSMTP.messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002967
2968 def test_client_enqueue(self):
2969 "Test that the RPC client can enqueue a change"
2970 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2971 A.addApproval('CRVW', 2)
2972 A.addApproval('APRV', 1)
2973
2974 client = zuul.rpcclient.RPCClient('127.0.0.1',
2975 self.gearman_server.port)
2976 r = client.enqueue(pipeline='gate',
2977 project='org/project',
2978 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002979 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002980 self.waitUntilSettled()
2981 self.assertEqual(self.getJobFromHistory('project-merge').result,
2982 'SUCCESS')
2983 self.assertEqual(self.getJobFromHistory('project-test1').result,
2984 'SUCCESS')
2985 self.assertEqual(self.getJobFromHistory('project-test2').result,
2986 'SUCCESS')
2987 self.assertEqual(A.data['status'], 'MERGED')
2988 self.assertEqual(A.reported, 2)
2989 self.assertEqual(r, True)
2990
2991 def test_client_enqueue_negative(self):
2992 "Test that the RPC client returns errors"
2993 client = zuul.rpcclient.RPCClient('127.0.0.1',
2994 self.gearman_server.port)
2995 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2996 "Invalid project"):
2997 r = client.enqueue(pipeline='gate',
2998 project='project-does-not-exist',
2999 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003000 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003001 client.shutdown()
3002 self.assertEqual(r, False)
3003
3004 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3005 "Invalid pipeline"):
3006 r = client.enqueue(pipeline='pipeline-does-not-exist',
3007 project='org/project',
3008 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003009 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003010 client.shutdown()
3011 self.assertEqual(r, False)
3012
3013 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3014 "Invalid trigger"):
3015 r = client.enqueue(pipeline='gate',
3016 project='org/project',
3017 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003018 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003019 client.shutdown()
3020 self.assertEqual(r, False)
3021
3022 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3023 "Invalid change"):
3024 r = client.enqueue(pipeline='gate',
3025 project='org/project',
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 self.waitUntilSettled()
3032 self.assertEqual(len(self.history), 0)
3033 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003034
3035 def test_client_promote(self):
3036 "Test that the RPC client can promote a change"
3037 self.worker.hold_jobs_in_build = True
3038 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3039 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3040 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3041 A.addApproval('CRVW', 2)
3042 B.addApproval('CRVW', 2)
3043 C.addApproval('CRVW', 2)
3044
3045 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3046 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3047 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3048
3049 self.waitUntilSettled()
3050
3051 client = zuul.rpcclient.RPCClient('127.0.0.1',
3052 self.gearman_server.port)
3053 r = client.promote(pipeline='gate',
3054 change_ids=['2,1', '3,1'])
3055
3056 self.worker.release('.*-merge')
3057 self.waitUntilSettled()
3058 self.worker.release('.*-merge')
3059 self.waitUntilSettled()
3060 self.worker.release('.*-merge')
3061 self.waitUntilSettled()
3062
3063 self.assertEqual(len(self.builds), 6)
3064 self.assertEqual(self.builds[0].name, 'project-test1')
3065 self.assertEqual(self.builds[1].name, 'project-test2')
3066 self.assertEqual(self.builds[2].name, 'project-test1')
3067 self.assertEqual(self.builds[3].name, 'project-test2')
3068 self.assertEqual(self.builds[4].name, 'project-test1')
3069 self.assertEqual(self.builds[5].name, 'project-test2')
3070
3071 self.assertTrue(self.job_has_changes(self.builds[0], B))
3072 self.assertFalse(self.job_has_changes(self.builds[0], A))
3073 self.assertFalse(self.job_has_changes(self.builds[0], C))
3074
3075 self.assertTrue(self.job_has_changes(self.builds[2], B))
3076 self.assertTrue(self.job_has_changes(self.builds[2], C))
3077 self.assertFalse(self.job_has_changes(self.builds[2], A))
3078
3079 self.assertTrue(self.job_has_changes(self.builds[4], B))
3080 self.assertTrue(self.job_has_changes(self.builds[4], C))
3081 self.assertTrue(self.job_has_changes(self.builds[4], A))
3082
3083 self.worker.release()
3084 self.waitUntilSettled()
3085
3086 self.assertEqual(A.data['status'], 'MERGED')
3087 self.assertEqual(A.reported, 2)
3088 self.assertEqual(B.data['status'], 'MERGED')
3089 self.assertEqual(B.reported, 2)
3090 self.assertEqual(C.data['status'], 'MERGED')
3091 self.assertEqual(C.reported, 2)
3092
3093 client.shutdown()
3094 self.assertEqual(r, True)
3095
3096 def test_client_promote_dependent(self):
3097 "Test that the RPC client can promote a dependent change"
3098 # C (depends on B) -> B -> A ; then promote C to get:
3099 # A -> C (depends on B) -> B
3100 self.worker.hold_jobs_in_build = True
3101 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3102 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3103 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3104
3105 C.setDependsOn(B, 1)
3106
3107 A.addApproval('CRVW', 2)
3108 B.addApproval('CRVW', 2)
3109 C.addApproval('CRVW', 2)
3110
3111 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3112 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3113 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3114
3115 self.waitUntilSettled()
3116
3117 client = zuul.rpcclient.RPCClient('127.0.0.1',
3118 self.gearman_server.port)
3119 r = client.promote(pipeline='gate',
3120 change_ids=['3,1'])
3121
3122 self.worker.release('.*-merge')
3123 self.waitUntilSettled()
3124 self.worker.release('.*-merge')
3125 self.waitUntilSettled()
3126 self.worker.release('.*-merge')
3127 self.waitUntilSettled()
3128
3129 self.assertEqual(len(self.builds), 6)
3130 self.assertEqual(self.builds[0].name, 'project-test1')
3131 self.assertEqual(self.builds[1].name, 'project-test2')
3132 self.assertEqual(self.builds[2].name, 'project-test1')
3133 self.assertEqual(self.builds[3].name, 'project-test2')
3134 self.assertEqual(self.builds[4].name, 'project-test1')
3135 self.assertEqual(self.builds[5].name, 'project-test2')
3136
3137 self.assertTrue(self.job_has_changes(self.builds[0], B))
3138 self.assertFalse(self.job_has_changes(self.builds[0], A))
3139 self.assertFalse(self.job_has_changes(self.builds[0], C))
3140
3141 self.assertTrue(self.job_has_changes(self.builds[2], B))
3142 self.assertTrue(self.job_has_changes(self.builds[2], C))
3143 self.assertFalse(self.job_has_changes(self.builds[2], A))
3144
3145 self.assertTrue(self.job_has_changes(self.builds[4], B))
3146 self.assertTrue(self.job_has_changes(self.builds[4], C))
3147 self.assertTrue(self.job_has_changes(self.builds[4], A))
3148
3149 self.worker.release()
3150 self.waitUntilSettled()
3151
3152 self.assertEqual(A.data['status'], 'MERGED')
3153 self.assertEqual(A.reported, 2)
3154 self.assertEqual(B.data['status'], 'MERGED')
3155 self.assertEqual(B.reported, 2)
3156 self.assertEqual(C.data['status'], 'MERGED')
3157 self.assertEqual(C.reported, 2)
3158
3159 client.shutdown()
3160 self.assertEqual(r, True)
3161
3162 def test_client_promote_negative(self):
3163 "Test that the RPC client returns errors for promotion"
3164 self.worker.hold_jobs_in_build = True
3165 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3166 A.addApproval('CRVW', 2)
3167 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3168 self.waitUntilSettled()
3169
3170 client = zuul.rpcclient.RPCClient('127.0.0.1',
3171 self.gearman_server.port)
3172
3173 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3174 r = client.promote(pipeline='nonexistent',
3175 change_ids=['2,1', '3,1'])
3176 client.shutdown()
3177 self.assertEqual(r, False)
3178
3179 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3180 r = client.promote(pipeline='gate',
3181 change_ids=['4,1'])
3182 client.shutdown()
3183 self.assertEqual(r, False)
3184
3185 self.worker.hold_jobs_in_build = False
3186 self.worker.release()
3187 self.waitUntilSettled()