blob: 94d2aa36aa02d13874aab2b2d99c28e5394a1d52 [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')
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000693
James E. Blairff80a2f2013-12-27 13:24:06 -0800694 def __init__(self, messages, server, port):
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000695 self.server = server
696 self.port = port
James E. Blairff80a2f2013-12-27 13:24:06 -0800697 self.messages = messages
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000698
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
James E. Blairff80a2f2013-12-27 13:24:06 -0800706 self.messages.append(dict(
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000707 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:
James E. Blairff80a2f2013-12-27 13:24:06 -0800732 self.useFixture(fixtures.Timeout(test_timeout, gentle=False))
Monty Taylorbc758832013-06-17 17:22:42 -0400733
734 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
735 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
736 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
737 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
738 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
739 os.environ.get('OS_STDERR_CAPTURE') == '1'):
740 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
741 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Monty Taylore6a6c402013-07-02 09:25:55 -0700742 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
743 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400744 self.useFixture(fixtures.FakeLogger(
745 level=logging.DEBUG,
746 format='%(asctime)s %(name)-32s '
747 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700748 tmp_root = self.useFixture(fixtures.TempDir(
749 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400750 self.test_root = os.path.join(tmp_root, "zuul-test")
751 self.upstream_root = os.path.join(self.test_root, "upstream")
752 self.git_root = os.path.join(self.test_root, "git")
753
754 CONFIG.set('zuul', 'git_dir', self.git_root)
755 if os.path.exists(self.test_root):
756 shutil.rmtree(self.test_root)
757 os.makedirs(self.test_root)
758 os.makedirs(self.upstream_root)
759 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700760
761 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400762 self.init_repo("org/project")
763 self.init_repo("org/project1")
764 self.init_repo("org/project2")
765 self.init_repo("org/project3")
766 self.init_repo("org/one-job-project")
767 self.init_repo("org/nonvoting-project")
768 self.init_repo("org/templated-project")
James E. Blair3e98c022013-12-16 15:25:38 -0800769 self.init_repo("org/layered-project")
Monty Taylorbc758832013-06-17 17:22:42 -0400770 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700771 self.init_repo("org/conflict-project")
James E. Blair412e5582013-04-22 15:50:12 -0700772
773 self.statsd = FakeStatsd()
774 os.environ['STATSD_HOST'] = 'localhost'
775 os.environ['STATSD_PORT'] = str(self.statsd.port)
776 self.statsd.start()
777 # the statsd client object is configured in the statsd module import
778 reload(statsd)
779 reload(zuul.scheduler)
780
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700781 self.gearman_server = FakeGearmanServer()
782
783 self.config = ConfigParser.ConfigParser()
784 cfg = StringIO()
785 CONFIG.write(cfg)
786 cfg.seek(0)
787 self.config.readfp(cfg)
788 self.config.set('gearman', 'port', str(self.gearman_server.port))
789
Monty Taylorbc758832013-06-17 17:22:42 -0400790 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700791 self.worker.addServer('127.0.0.1', self.gearman_server.port)
792 self.gearman_server.worker = self.worker
793
James E. Blairb0fcae42012-07-17 11:12:10 -0700794 self.sched = zuul.scheduler.Scheduler()
795
James E. Blair8cc15a82012-08-01 11:17:57 -0700796 def URLOpenerFactory(*args, **kw):
797 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400798 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700799
James E. Blair8cc15a82012-08-01 11:17:57 -0700800 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700801 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700802
James E. Blairff80a2f2013-12-27 13:24:06 -0800803 self.smtp_messages = []
804
805 def FakeSMTPFactory(*args, **kw):
806 args = [self.smtp_messages] + list(args)
807 return FakeSMTP(*args, **kw)
808
James E. Blairb0fcae42012-07-17 11:12:10 -0700809 zuul.lib.gerrit.Gerrit = FakeGerrit
James E. Blairff80a2f2013-12-27 13:24:06 -0800810 self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTPFactory))
James E. Blairb0fcae42012-07-17 11:12:10 -0700811
Monty Taylorbc758832013-06-17 17:22:42 -0400812 self.gerrit = FakeGerritTrigger(
813 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700814 self.gerrit.replication_timeout = 1.5
815 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700816 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400817 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700818
James E. Blair1843a552013-07-03 14:19:52 -0700819 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
James E. Blairad28e912013-11-27 10:43:22 -0800820 self.rpc = zuul.rpclistener.RPCListener(self.config, self.sched)
James E. Blair1843a552013-07-03 14:19:52 -0700821
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700822 self.sched.setLauncher(self.launcher)
James E. Blair6c358e72013-07-29 17:06:47 -0700823 self.sched.registerTrigger(self.gerrit)
James E. Blair63bb0ef2013-07-29 17:14:51 -0700824 self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
825 self.sched.registerTrigger(self.timer)
James E. Blairb0fcae42012-07-17 11:12:10 -0700826
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000827 self.sched.registerReporter(
828 zuul.reporter.gerrit.Reporter(self.gerrit))
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000829 self.smtp_reporter = zuul.reporter.smtp.Reporter(
830 self.config.get('smtp', 'default_from'),
831 self.config.get('smtp', 'default_to'),
832 self.config.get('smtp', 'server'))
833 self.sched.registerReporter(self.smtp_reporter)
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000834
James E. Blairb0fcae42012-07-17 11:12:10 -0700835 self.sched.start()
836 self.sched.reconfigure(self.config)
837 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700838 self.webapp.start()
James E. Blairad28e912013-11-27 10:43:22 -0800839 self.rpc.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700840 self.launcher.gearman.waitForServer()
841 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400842 self.builds = self.worker.running_builds
843 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700844
James E. Blairfee8d652013-06-07 08:57:52 -0700845 self.addCleanup(self.assertFinalState)
846 self.addCleanup(self.shutdown)
847
848 def assertFinalState(self):
849 # Make sure that the change cache is cleared
James E. Blair6c358e72013-07-29 17:06:47 -0700850 self.assertEqual(len(self.gerrit._change_cache.keys()), 0)
Clark Boylan4ba48d92013-11-11 18:03:53 -0800851 # Make sure that git.Repo objects have been garbage collected.
852 repos = []
853 gc.collect()
854 for obj in gc.get_objects():
855 if isinstance(obj, git.Repo):
856 repos.append(obj)
857 self.assertEqual(len(repos), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700858 self.assertEmptyQueues()
859
860 def shutdown(self):
861 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700862 self.launcher.stop()
863 self.worker.shutdown()
864 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700865 self.gerrit.stop()
James E. Blair63bb0ef2013-07-29 17:14:51 -0700866 self.timer.stop()
James E. Blairb0fcae42012-07-17 11:12:10 -0700867 self.sched.stop()
868 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700869 self.statsd.stop()
870 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700871 self.webapp.stop()
872 self.webapp.join()
James E. Blairad28e912013-11-27 10:43:22 -0800873 self.rpc.stop()
874 self.rpc.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700875 threads = threading.enumerate()
876 if len(threads) > 1:
877 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400878 super(TestScheduler, self).tearDown()
879
880 def init_repo(self, project):
881 parts = project.split('/')
882 path = os.path.join(self.upstream_root, *parts[:-1])
883 if not os.path.exists(path):
884 os.makedirs(path)
885 path = os.path.join(self.upstream_root, project)
886 repo = git.Repo.init(path)
887
888 repo.config_writer().set_value('user', 'email', 'user@example.com')
889 repo.config_writer().set_value('user', 'name', 'User Name')
890 repo.config_writer().write()
891
892 fn = os.path.join(path, 'README')
893 f = open(fn, 'w')
894 f.write("test\n")
895 f.close()
896 repo.index.add([fn])
897 repo.index.commit('initial commit')
898 master = repo.create_head('master')
899 repo.create_tag('init')
900
901 mp = repo.create_head('mp')
902 repo.head.reference = mp
903 f = open(fn, 'a')
904 f.write("test mp\n")
905 f.close()
906 repo.index.add([fn])
907 repo.index.commit('mp commit')
908
909 repo.head.reference = master
910 repo.head.reset(index=True, working_tree=True)
911 repo.git.clean('-x', '-f', '-d')
912
913 def ref_has_change(self, ref, change):
914 path = os.path.join(self.git_root, change.project)
915 repo = git.Repo(path)
916 for commit in repo.iter_commits(ref):
917 if commit.message.strip() == ('%s-1' % change.subject):
918 return True
919 return False
920
921 def job_has_changes(self, *args):
922 job = args[0]
923 commits = args[1:]
924 if isinstance(job, FakeBuild):
925 parameters = job.parameters
926 else:
927 parameters = json.loads(job.arguments)
928 project = parameters['ZUUL_PROJECT']
929 path = os.path.join(self.git_root, project)
930 repo = git.Repo(path)
931 ref = parameters['ZUUL_REF']
932 sha = parameters['ZUUL_COMMIT']
933 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
934 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
935 commit_messages = ['%s-1' % commit.subject for commit in commits]
936 for msg in commit_messages:
937 if msg not in repo_messages:
938 return False
939 if repo_shas[0] != sha:
940 return False
941 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700942
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700943 def registerJobs(self):
944 count = 0
James E. Blaireff88162013-07-01 12:44:14 -0400945 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700946 self.worker.registerFunction('build:' + job)
947 count += 1
948 self.worker.registerFunction('stop:' + self.worker.worker_id)
949 count += 1
950
951 while len(self.gearman_server.functions) < count:
952 time.sleep(0)
953
954 def release(self, job):
955 if isinstance(job, FakeBuild):
956 job.release()
957 else:
958 job.waiting = False
959 self.log.debug("Queued job %s released" % job.unique)
960 self.gearman_server.wakeConnections()
961
962 def getParameter(self, job, name):
963 if isinstance(job, FakeBuild):
964 return job.parameters[name]
965 else:
966 parameters = json.loads(job.arguments)
967 return parameters[name]
968
969 def resetGearmanServer(self):
970 self.worker.setFunctions([])
971 while True:
972 done = True
973 for connection in self.gearman_server.active_connections:
James E. Blairad28e912013-11-27 10:43:22 -0800974 if (connection.functions and
975 connection.client_id != 'Zuul RPC Listener'):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700976 done = False
977 if done:
978 break
979 time.sleep(0)
980 self.gearman_server.functions = set()
James E. Blairad28e912013-11-27 10:43:22 -0800981 self.rpc.register()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700982
983 def haveAllBuildsReported(self):
984 # See if Zuul is waiting on a meta job to complete
985 if self.launcher.meta_jobs:
986 return False
987 # Find out if every build that the worker has completed has been
988 # reported back to Zuul. If it hasn't then that means a Gearman
989 # event is still in transit and the system is not stable.
990 for build in self.worker.build_history:
991 zbuild = self.launcher.builds.get(build.uuid)
992 if not zbuild:
993 # It has already been reported
994 continue
995 # It hasn't been reported yet.
996 return False
997 # Make sure that none of the worker connections are in GRAB_WAIT
998 for connection in self.worker.active_connections:
999 if connection.state == 'GRAB_WAIT':
1000 return False
1001 return True
1002
1003 def areAllBuildsWaiting(self):
1004 ret = True
1005
1006 builds = self.launcher.builds.values()
1007 for build in builds:
1008 client_job = None
1009 for conn in self.launcher.gearman.active_connections:
1010 for j in conn.related_jobs.values():
1011 if j.unique == build.uuid:
1012 client_job = j
1013 break
1014 if not client_job:
1015 self.log.debug("%s is not known to the gearman client" %
1016 build)
1017 ret = False
1018 continue
1019 if not client_job.handle:
1020 self.log.debug("%s has no handle" % client_job)
1021 ret = False
1022 continue
1023 server_job = self.gearman_server.jobs.get(client_job.handle)
1024 if not server_job:
1025 self.log.debug("%s is not known to the gearman server" %
1026 client_job)
1027 ret = False
1028 continue
1029 if not hasattr(server_job, 'waiting'):
1030 self.log.debug("%s is being enqueued" % server_job)
1031 ret = False
1032 continue
1033 if server_job.waiting:
1034 continue
1035 worker_job = self.worker.gearman_jobs.get(server_job.unique)
1036 if worker_job:
1037 if worker_job.build.isWaiting():
1038 continue
1039 else:
1040 self.log.debug("%s is running" % worker_job)
1041 ret = False
1042 else:
1043 self.log.debug("%s is unassigned" % server_job)
1044 ret = False
1045 return ret
1046
James E. Blairb0fcae42012-07-17 11:12:10 -07001047 def waitUntilSettled(self):
1048 self.log.debug("Waiting until settled...")
1049 start = time.time()
1050 while True:
1051 if time.time() - start > 10:
1052 print 'queue status:',
1053 print self.sched.trigger_event_queue.empty(),
1054 print self.sched.result_event_queue.empty(),
1055 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001056 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -07001057 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001058 # Make sure no new events show up while we're checking
1059 self.worker.lock.acquire()
1060 # have all build states propogated to zuul?
1061 if self.haveAllBuildsReported():
1062 # Join ensures that the queue is empty _and_ events have been
1063 # processed
1064 self.fake_gerrit.event_queue.join()
1065 self.sched.trigger_event_queue.join()
1066 self.sched.result_event_queue.join()
1067 if (self.sched.trigger_event_queue.empty() and
1068 self.sched.result_event_queue.empty() and
1069 self.fake_gerrit.event_queue.empty() and
1070 self.areAllBuildsWaiting()):
1071 self.worker.lock.release()
1072 self.log.debug("...settled.")
1073 return
1074 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001075 self.sched.wake_event.wait(0.1)
1076
James E. Blaird466dc42012-07-31 10:42:56 -07001077 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -08001078 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -07001079 return len(jobs)
1080
James E. Blair4ca985f2013-05-30 12:27:43 -07001081 def getJobFromHistory(self, name):
1082 history = self.worker.build_history
1083 for job in history:
1084 if job.name == name:
1085 return job
1086 raise Exception("Unable to find job %s in history" % name)
1087
James E. Blaire0487072012-08-29 17:38:31 -07001088 def assertEmptyQueues(self):
1089 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001090 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001091 for queue in pipeline.queues:
1092 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001093 print 'pipeline %s queue %s contents %s' % (
1094 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001095 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001096
James E. Blair66eeebf2013-07-27 17:44:32 -07001097 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001098 start = time.time()
1099 while time.time() < (start + 5):
1100 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001101 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001102 k, v = stat.split(':')
1103 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001104 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001105 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001106 elif value:
1107 if value == v:
1108 return
1109 elif kind:
1110 if v.endswith('|' + kind):
1111 return
James E. Blair412e5582013-04-22 15:50:12 -07001112 time.sleep(0.1)
1113
1114 pprint.pprint(self.statsd.stats)
1115 raise Exception("Key %s not found in reported stats" % key)
1116
James E. Blairb0fcae42012-07-17 11:12:10 -07001117 def test_jobs_launched(self):
1118 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001119
James E. Blairb0fcae42012-07-17 11:12:10 -07001120 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001121 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001122 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1123 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001124 self.assertEqual(self.getJobFromHistory('project-merge').result,
1125 'SUCCESS')
1126 self.assertEqual(self.getJobFromHistory('project-test1').result,
1127 'SUCCESS')
1128 self.assertEqual(self.getJobFromHistory('project-test2').result,
1129 'SUCCESS')
1130 self.assertEqual(A.data['status'], 'MERGED')
1131 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001132
James E. Blair66eeebf2013-07-27 17:44:32 -07001133 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1134 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1135 value='1|g')
1136 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1137 kind='ms')
1138 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1139 value='1|c')
1140 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1141 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1142 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001143 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001144 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001145 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001146 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001147
James E. Blair3cb10702013-08-24 08:56:03 -07001148 def test_initial_pipeline_gauges(self):
1149 "Test that each pipeline reported its length on start"
1150 pipeline_names = self.sched.layout.pipelines.keys()
1151 self.assertNotEqual(len(pipeline_names), 0)
1152 for name in pipeline_names:
1153 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
1154 value='0|g')
1155
James E. Blair42f74822013-05-14 15:18:03 -07001156 def test_duplicate_pipelines(self):
1157 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001158
James E. Blair42f74822013-05-14 15:18:03 -07001159 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1160 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1161 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001162
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001163 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001164 self.history[0].name == 'project-test1'
1165 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001166
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001167 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001168 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001169 self.assertIn('dup1/project-test1', A.messages[0])
1170 self.assertNotIn('dup2/project-test1', A.messages[0])
1171 self.assertNotIn('dup1/project-test1', A.messages[1])
1172 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001173 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001174 self.assertIn('dup1/project-test1', A.messages[1])
1175 self.assertNotIn('dup2/project-test1', A.messages[1])
1176 self.assertNotIn('dup1/project-test1', A.messages[0])
1177 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001178
James E. Blairb0fcae42012-07-17 11:12:10 -07001179 def test_parallel_changes(self):
1180 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001181
1182 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001183 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1184 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1185 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001186 A.addApproval('CRVW', 2)
1187 B.addApproval('CRVW', 2)
1188 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001189
1190 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1191 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1192 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1193
1194 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001195 self.assertEqual(len(self.builds), 1)
1196 self.assertEqual(self.builds[0].name, 'project-merge')
1197 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001198
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001199 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001200 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001201 self.assertEqual(len(self.builds), 3)
1202 self.assertEqual(self.builds[0].name, 'project-test1')
1203 self.assertTrue(self.job_has_changes(self.builds[0], A))
1204 self.assertEqual(self.builds[1].name, 'project-test2')
1205 self.assertTrue(self.job_has_changes(self.builds[1], A))
1206 self.assertEqual(self.builds[2].name, 'project-merge')
1207 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001208
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001209 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001210 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001211 self.assertEqual(len(self.builds), 5)
1212 self.assertEqual(self.builds[0].name, 'project-test1')
1213 self.assertTrue(self.job_has_changes(self.builds[0], A))
1214 self.assertEqual(self.builds[1].name, 'project-test2')
1215 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001216
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001217 self.assertEqual(self.builds[2].name, 'project-test1')
1218 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1219 self.assertEqual(self.builds[3].name, 'project-test2')
1220 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001221
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001222 self.assertEqual(self.builds[4].name, 'project-merge')
1223 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001224
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001225 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001226 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001227 self.assertEqual(len(self.builds), 6)
1228 self.assertEqual(self.builds[0].name, 'project-test1')
1229 self.assertTrue(self.job_has_changes(self.builds[0], A))
1230 self.assertEqual(self.builds[1].name, 'project-test2')
1231 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001232
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001233 self.assertEqual(self.builds[2].name, 'project-test1')
1234 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1235 self.assertEqual(self.builds[3].name, 'project-test2')
1236 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001237
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001238 self.assertEqual(self.builds[4].name, 'project-test1')
1239 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1240 self.assertEqual(self.builds[5].name, 'project-test2')
1241 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001242
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001243 self.worker.hold_jobs_in_build = False
1244 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001245 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001246 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001247
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001248 self.assertEqual(len(self.history), 9)
1249 self.assertEqual(A.data['status'], 'MERGED')
1250 self.assertEqual(B.data['status'], 'MERGED')
1251 self.assertEqual(C.data['status'], 'MERGED')
1252 self.assertEqual(A.reported, 2)
1253 self.assertEqual(B.reported, 2)
1254 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001255
1256 def test_failed_changes(self):
1257 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001258 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001259
James E. Blairb02a3bb2012-07-30 17:49:55 -07001260 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1261 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001262 A.addApproval('CRVW', 2)
1263 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001264
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001265 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001266
James E. Blaire2819012013-06-28 17:17:26 -04001267 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1268 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001269 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001270
1271 self.worker.release('.*-merge')
1272 self.waitUntilSettled()
1273
1274 self.worker.hold_jobs_in_build = False
1275 self.worker.release()
1276
1277 self.waitUntilSettled()
1278 # It's certain that the merge job for change 2 will run, but
1279 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001280 self.assertTrue(len(self.history) > 6)
1281 self.assertEqual(A.data['status'], 'NEW')
1282 self.assertEqual(B.data['status'], 'MERGED')
1283 self.assertEqual(A.reported, 2)
1284 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001285
1286 def test_independent_queues(self):
1287 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001288
1289 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001290 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001291 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1292 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001293 A.addApproval('CRVW', 2)
1294 B.addApproval('CRVW', 2)
1295 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001296
1297 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1298 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1299 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1300
James E. Blairb02a3bb2012-07-30 17:49:55 -07001301 self.waitUntilSettled()
1302
1303 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001304 self.assertEqual(len(self.builds), 2)
1305 self.assertEqual(self.builds[0].name, 'project-merge')
1306 self.assertTrue(self.job_has_changes(self.builds[0], A))
1307 self.assertEqual(self.builds[1].name, 'project1-merge')
1308 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001309
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001310 # Release the current merge builds
1311 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001312 self.waitUntilSettled()
1313 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001314 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001315 self.waitUntilSettled()
1316
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001317 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001318 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001319 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001320
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001321 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001322 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001323 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001324
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001325 self.assertEqual(len(self.history), 11)
1326 self.assertEqual(A.data['status'], 'MERGED')
1327 self.assertEqual(B.data['status'], 'MERGED')
1328 self.assertEqual(C.data['status'], 'MERGED')
1329 self.assertEqual(A.reported, 2)
1330 self.assertEqual(B.reported, 2)
1331 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001332
1333 def test_failed_change_at_head(self):
1334 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001335
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001336 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001337 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1338 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1339 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001340 A.addApproval('CRVW', 2)
1341 B.addApproval('CRVW', 2)
1342 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001343
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001344 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001345
1346 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1347 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1348 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1349
1350 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001351
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001352 self.assertEqual(len(self.builds), 1)
1353 self.assertEqual(self.builds[0].name, 'project-merge')
1354 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001355
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001356 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001357 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001358 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001359 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001360 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001361 self.waitUntilSettled()
1362
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001363 self.assertEqual(len(self.builds), 6)
1364 self.assertEqual(self.builds[0].name, 'project-test1')
1365 self.assertEqual(self.builds[1].name, 'project-test2')
1366 self.assertEqual(self.builds[2].name, 'project-test1')
1367 self.assertEqual(self.builds[3].name, 'project-test2')
1368 self.assertEqual(self.builds[4].name, 'project-test1')
1369 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001370
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001371 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001372 self.waitUntilSettled()
1373
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001374 # project-test2, project-merge for B
1375 self.assertEqual(len(self.builds), 2)
1376 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001377
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001378 self.worker.hold_jobs_in_build = False
1379 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001380 self.waitUntilSettled()
1381
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001382 self.assertEqual(len(self.builds), 0)
1383 self.assertEqual(len(self.history), 15)
1384 self.assertEqual(A.data['status'], 'NEW')
1385 self.assertEqual(B.data['status'], 'MERGED')
1386 self.assertEqual(C.data['status'], 'MERGED')
1387 self.assertEqual(A.reported, 2)
1388 self.assertEqual(B.reported, 2)
1389 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001390
James E. Blair0aac4872013-08-23 14:02:38 -07001391 def test_failed_change_in_middle(self):
1392 "Test a failed change in the middle of the queue"
1393
1394 self.worker.hold_jobs_in_build = True
1395 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1396 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1397 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1398 A.addApproval('CRVW', 2)
1399 B.addApproval('CRVW', 2)
1400 C.addApproval('CRVW', 2)
1401
1402 self.worker.addFailTest('project-test1', B)
1403
1404 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1405 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1406 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1407
1408 self.waitUntilSettled()
1409
1410 self.worker.release('.*-merge')
1411 self.waitUntilSettled()
1412 self.worker.release('.*-merge')
1413 self.waitUntilSettled()
1414 self.worker.release('.*-merge')
1415 self.waitUntilSettled()
1416
1417 self.assertEqual(len(self.builds), 6)
1418 self.assertEqual(self.builds[0].name, 'project-test1')
1419 self.assertEqual(self.builds[1].name, 'project-test2')
1420 self.assertEqual(self.builds[2].name, 'project-test1')
1421 self.assertEqual(self.builds[3].name, 'project-test2')
1422 self.assertEqual(self.builds[4].name, 'project-test1')
1423 self.assertEqual(self.builds[5].name, 'project-test2')
1424
1425 self.release(self.builds[2])
1426 self.waitUntilSettled()
1427
James E. Blair972e3c72013-08-29 12:04:55 -07001428 # project-test1 and project-test2 for A
1429 # project-test2 for B
1430 # project-merge for C (without B)
1431 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -07001432 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1433
James E. Blair972e3c72013-08-29 12:04:55 -07001434 self.worker.release('.*-merge')
1435 self.waitUntilSettled()
1436
1437 # project-test1 and project-test2 for A
1438 # project-test2 for B
1439 # project-test1 and project-test2 for C
1440 self.assertEqual(len(self.builds), 5)
1441
James E. Blair0aac4872013-08-23 14:02:38 -07001442 items = self.sched.layout.pipelines['gate'].getAllItems()
1443 builds = items[0].current_build_set.getBuilds()
1444 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1445 self.assertEqual(self.countJobResults(builds, None), 2)
1446 builds = items[1].current_build_set.getBuilds()
1447 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1448 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1449 self.assertEqual(self.countJobResults(builds, None), 1)
1450 builds = items[2].current_build_set.getBuilds()
1451 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -07001452 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -07001453
1454 self.worker.hold_jobs_in_build = False
1455 self.worker.release()
1456 self.waitUntilSettled()
1457
1458 self.assertEqual(len(self.builds), 0)
1459 self.assertEqual(len(self.history), 12)
1460 self.assertEqual(A.data['status'], 'MERGED')
1461 self.assertEqual(B.data['status'], 'NEW')
1462 self.assertEqual(C.data['status'], 'MERGED')
1463 self.assertEqual(A.reported, 2)
1464 self.assertEqual(B.reported, 2)
1465 self.assertEqual(C.reported, 2)
1466
James E. Blaird466dc42012-07-31 10:42:56 -07001467 def test_failed_change_at_head_with_queue(self):
1468 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001469
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001470 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001471 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1472 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1473 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001474 A.addApproval('CRVW', 2)
1475 B.addApproval('CRVW', 2)
1476 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001477
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001478 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001479
1480 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1481 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1482 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1483
1484 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001485 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001486 self.assertEqual(len(self.builds), 0)
1487 self.assertEqual(len(queue), 1)
1488 self.assertEqual(queue[0].name, 'build:project-merge')
1489 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001490
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001491 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001492 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001493 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001494 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001495 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001496 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001497 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001498
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001499 self.assertEqual(len(self.builds), 0)
1500 self.assertEqual(len(queue), 6)
1501 self.assertEqual(queue[0].name, 'build:project-test1')
1502 self.assertEqual(queue[1].name, 'build:project-test2')
1503 self.assertEqual(queue[2].name, 'build:project-test1')
1504 self.assertEqual(queue[3].name, 'build:project-test2')
1505 self.assertEqual(queue[4].name, 'build:project-test1')
1506 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001507
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001508 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001509 self.waitUntilSettled()
1510
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001511 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001512 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001513 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1514 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001515
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001516 self.gearman_server.hold_jobs_in_queue = False
1517 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001518 self.waitUntilSettled()
1519
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001520 self.assertEqual(len(self.builds), 0)
1521 self.assertEqual(len(self.history), 11)
1522 self.assertEqual(A.data['status'], 'NEW')
1523 self.assertEqual(B.data['status'], 'MERGED')
1524 self.assertEqual(C.data['status'], 'MERGED')
1525 self.assertEqual(A.reported, 2)
1526 self.assertEqual(B.reported, 2)
1527 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001528
James E. Blairfef71632013-09-23 11:15:47 -07001529 def test_two_failed_changes_at_head(self):
1530 "Test that changes are reparented correctly if 2 fail at head"
1531
1532 self.worker.hold_jobs_in_build = True
1533 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1534 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1535 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1536 A.addApproval('CRVW', 2)
1537 B.addApproval('CRVW', 2)
1538 C.addApproval('CRVW', 2)
1539
1540 self.worker.addFailTest('project-test1', A)
1541 self.worker.addFailTest('project-test1', B)
1542
1543 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1544 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1545 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1546 self.waitUntilSettled()
1547
1548 self.worker.release('.*-merge')
1549 self.waitUntilSettled()
1550 self.worker.release('.*-merge')
1551 self.waitUntilSettled()
1552 self.worker.release('.*-merge')
1553 self.waitUntilSettled()
1554
1555 self.assertEqual(len(self.builds), 6)
1556 self.assertEqual(self.builds[0].name, 'project-test1')
1557 self.assertEqual(self.builds[1].name, 'project-test2')
1558 self.assertEqual(self.builds[2].name, 'project-test1')
1559 self.assertEqual(self.builds[3].name, 'project-test2')
1560 self.assertEqual(self.builds[4].name, 'project-test1')
1561 self.assertEqual(self.builds[5].name, 'project-test2')
1562
1563 self.assertTrue(self.job_has_changes(self.builds[0], A))
1564 self.assertTrue(self.job_has_changes(self.builds[2], A))
1565 self.assertTrue(self.job_has_changes(self.builds[2], B))
1566 self.assertTrue(self.job_has_changes(self.builds[4], A))
1567 self.assertTrue(self.job_has_changes(self.builds[4], B))
1568 self.assertTrue(self.job_has_changes(self.builds[4], C))
1569
1570 # Fail change B first
1571 self.release(self.builds[2])
1572 self.waitUntilSettled()
1573
1574 # restart of C after B failure
1575 self.worker.release('.*-merge')
1576 self.waitUntilSettled()
1577
1578 self.assertEqual(len(self.builds), 5)
1579 self.assertEqual(self.builds[0].name, 'project-test1')
1580 self.assertEqual(self.builds[1].name, 'project-test2')
1581 self.assertEqual(self.builds[2].name, 'project-test2')
1582 self.assertEqual(self.builds[3].name, 'project-test1')
1583 self.assertEqual(self.builds[4].name, 'project-test2')
1584
1585 self.assertTrue(self.job_has_changes(self.builds[1], A))
1586 self.assertTrue(self.job_has_changes(self.builds[2], A))
1587 self.assertTrue(self.job_has_changes(self.builds[2], B))
1588 self.assertTrue(self.job_has_changes(self.builds[4], A))
1589 self.assertFalse(self.job_has_changes(self.builds[4], B))
1590 self.assertTrue(self.job_has_changes(self.builds[4], C))
1591
1592 # Finish running all passing jobs for change A
1593 self.release(self.builds[1])
1594 self.waitUntilSettled()
1595 # Fail and report change A
1596 self.release(self.builds[0])
1597 self.waitUntilSettled()
1598
1599 # restart of B,C after A failure
1600 self.worker.release('.*-merge')
1601 self.waitUntilSettled()
1602 self.worker.release('.*-merge')
1603 self.waitUntilSettled()
1604
1605 self.assertEqual(len(self.builds), 4)
1606 self.assertEqual(self.builds[0].name, 'project-test1') # B
1607 self.assertEqual(self.builds[1].name, 'project-test2') # B
1608 self.assertEqual(self.builds[2].name, 'project-test1') # C
1609 self.assertEqual(self.builds[3].name, 'project-test2') # C
1610
1611 self.assertFalse(self.job_has_changes(self.builds[1], A))
1612 self.assertTrue(self.job_has_changes(self.builds[1], B))
1613 self.assertFalse(self.job_has_changes(self.builds[1], C))
1614
1615 self.assertFalse(self.job_has_changes(self.builds[2], A))
1616 # After A failed and B and C restarted, B should be back in
1617 # C's tests because it has not failed yet.
1618 self.assertTrue(self.job_has_changes(self.builds[2], B))
1619 self.assertTrue(self.job_has_changes(self.builds[2], C))
1620
1621 self.worker.hold_jobs_in_build = False
1622 self.worker.release()
1623 self.waitUntilSettled()
1624
1625 self.assertEqual(len(self.builds), 0)
1626 self.assertEqual(len(self.history), 21)
1627 self.assertEqual(A.data['status'], 'NEW')
1628 self.assertEqual(B.data['status'], 'NEW')
1629 self.assertEqual(C.data['status'], 'MERGED')
1630 self.assertEqual(A.reported, 2)
1631 self.assertEqual(B.reported, 2)
1632 self.assertEqual(C.reported, 2)
1633
James E. Blair8c803f82012-07-31 16:25:42 -07001634 def test_patch_order(self):
1635 "Test that dependent patches are tested in the right order"
1636 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1637 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1638 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1639 A.addApproval('CRVW', 2)
1640 B.addApproval('CRVW', 2)
1641 C.addApproval('CRVW', 2)
1642
1643 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1644 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1645 M2.setMerged()
1646 M1.setMerged()
1647
1648 # C -> B -> A -> M1 -> M2
1649 # M2 is here to make sure it is never queried. If it is, it
1650 # means zuul is walking down the entire history of merged
1651 # changes.
1652
1653 C.setDependsOn(B, 1)
1654 B.setDependsOn(A, 1)
1655 A.setDependsOn(M1, 1)
1656 M1.setDependsOn(M2, 1)
1657
1658 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1659
1660 self.waitUntilSettled()
1661
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001662 self.assertEqual(A.data['status'], 'NEW')
1663 self.assertEqual(B.data['status'], 'NEW')
1664 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001665
1666 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1667 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1668
1669 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001670 self.assertEqual(M2.queried, 0)
1671 self.assertEqual(A.data['status'], 'MERGED')
1672 self.assertEqual(B.data['status'], 'MERGED')
1673 self.assertEqual(C.data['status'], 'MERGED')
1674 self.assertEqual(A.reported, 2)
1675 self.assertEqual(B.reported, 2)
1676 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001677
James E. Blair0e933c52013-07-11 10:18:52 -07001678 def test_trigger_cache(self):
1679 "Test that the trigger cache operates correctly"
1680 self.worker.hold_jobs_in_build = True
1681
1682 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1683 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1684 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1685 A.addApproval('CRVW', 2)
1686 B.addApproval('CRVW', 2)
1687
1688 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1689 M1.setMerged()
1690
1691 B.setDependsOn(A, 1)
1692 A.setDependsOn(M1, 1)
1693
1694 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1695 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1696
1697 self.waitUntilSettled()
1698
1699 for build in self.builds:
1700 if build.parameters['ZUUL_PIPELINE'] == 'check':
1701 build.release()
1702 self.waitUntilSettled()
1703 for build in self.builds:
1704 if build.parameters['ZUUL_PIPELINE'] == 'check':
1705 build.release()
1706 self.waitUntilSettled()
1707
1708 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1709 self.waitUntilSettled()
1710
James E. Blair6c358e72013-07-29 17:06:47 -07001711 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001712 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001713 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001714
1715 self.worker.hold_jobs_in_build = False
1716 self.worker.release()
1717 self.waitUntilSettled()
1718
1719 self.assertEqual(A.data['status'], 'MERGED')
1720 self.assertEqual(B.data['status'], 'MERGED')
1721 self.assertEqual(A.queried, 2) # Initial and isMerged
1722 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1723
James E. Blair8c803f82012-07-31 16:25:42 -07001724 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001725 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001726 # TODO: move to test_gerrit (this is a unit test!)
1727 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001728 trigger = self.sched.layout.pipelines['gate'].trigger
1729 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001730 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001731 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001732
1733 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001734 a = trigger.getChange(1, 2, refresh=True)
1735 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001736
1737 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001738 a = trigger.getChange(1, 2, refresh=True)
1739 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1740 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001741
1742 def test_build_configuration(self):
1743 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001744
1745 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001746 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1747 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1748 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1749 A.addApproval('CRVW', 2)
1750 B.addApproval('CRVW', 2)
1751 C.addApproval('CRVW', 2)
1752 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1753 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1754 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1755 self.waitUntilSettled()
1756
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001757 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001758 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001759 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001760 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001761 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001762 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001763 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001764 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1765 self.gearman_server.hold_jobs_in_queue = False
1766 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001767 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001768
Monty Taylorbc758832013-06-17 17:22:42 -04001769 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001770 repo = git.Repo(path)
1771 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1772 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001773 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001774 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001775
1776 def test_build_configuration_conflict(self):
1777 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001778
1779 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001780 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1781 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001782 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001783 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1784 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001785 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001786 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1787 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001788 A.addApproval('CRVW', 2)
1789 B.addApproval('CRVW', 2)
1790 C.addApproval('CRVW', 2)
1791 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1792 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1793 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1794 self.waitUntilSettled()
1795
James E. Blair6736beb2013-07-11 15:18:15 -07001796 self.assertEqual(A.reported, 1)
1797 self.assertEqual(B.reported, 1)
1798 self.assertEqual(C.reported, 1)
1799
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001800 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001801 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001802 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001803 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001804 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001805 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001806
1807 self.assertEqual(len(self.history), 2) # A and C merge jobs
1808
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001809 self.gearman_server.hold_jobs_in_queue = False
1810 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001811 self.waitUntilSettled()
1812
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001813 self.assertEqual(A.data['status'], 'MERGED')
1814 self.assertEqual(B.data['status'], 'NEW')
1815 self.assertEqual(C.data['status'], 'MERGED')
1816 self.assertEqual(A.reported, 2)
1817 self.assertEqual(B.reported, 2)
1818 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001819 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001820
James E. Blairdaabed22012-08-15 15:38:57 -07001821 def test_post(self):
1822 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001823
Zhongyue Luo5d556072012-09-21 02:00:47 +09001824 e = {
1825 "type": "ref-updated",
1826 "submitter": {
1827 "name": "User Name",
1828 },
1829 "refUpdate": {
1830 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1831 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1832 "refName": "master",
1833 "project": "org/project",
1834 }
1835 }
James E. Blairdaabed22012-08-15 15:38:57 -07001836 self.fake_gerrit.addEvent(e)
1837 self.waitUntilSettled()
1838
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001839 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001840 self.assertEqual(len(self.history), 1)
1841 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001842
1843 def test_build_configuration_branch(self):
1844 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001845
1846 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001847 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1848 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1849 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1850 A.addApproval('CRVW', 2)
1851 B.addApproval('CRVW', 2)
1852 C.addApproval('CRVW', 2)
1853 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1854 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1855 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1856 self.waitUntilSettled()
1857
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001858 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001859 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001860 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001861 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001862 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001863 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001864 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001865 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1866 self.gearman_server.hold_jobs_in_queue = False
1867 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001868 self.waitUntilSettled()
1869
Monty Taylorbc758832013-06-17 17:22:42 -04001870 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001871 repo = git.Repo(path)
1872 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1873 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001874 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001875 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001876
1877 def test_build_configuration_branch_interaction(self):
1878 "Test that switching between branches works"
1879 self.test_build_configuration()
1880 self.test_build_configuration_branch()
1881 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001882 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001883 repo = git.Repo(path)
1884 repo.heads.master.commit = repo.commit('init')
1885 self.test_build_configuration()
1886
1887 def test_build_configuration_multi_branch(self):
1888 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001889
1890 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001891 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1892 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1893 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1894 A.addApproval('CRVW', 2)
1895 B.addApproval('CRVW', 2)
1896 C.addApproval('CRVW', 2)
1897 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1898 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1899 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1900 self.waitUntilSettled()
1901
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001902 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001903 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001904 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001905 job_B = None
1906 for job in queue:
1907 if 'project-merge' in job.name:
1908 job_B = job
1909 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001910 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001911 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001912 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001913 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001914 for job in queue:
1915 if 'project-merge' in job.name:
1916 job_C = job
1917 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001918 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001919 self.gearman_server.hold_jobs_in_queue = False
1920 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001921 self.waitUntilSettled()
1922
Monty Taylorbc758832013-06-17 17:22:42 -04001923 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001924 repo = git.Repo(path)
1925
1926 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001927 for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001928 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001929 correct_messages = ['initial commit', 'A-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001930 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001931
1932 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001933 for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001934 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001935 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001936 self.assertEqual(repo_messages, correct_messages)
James E. Blair7f71c802012-08-22 13:04:32 -07001937
1938 def test_one_job_project(self):
1939 "Test that queueing works with one job"
1940 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1941 'master', 'A')
1942 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1943 'master', 'B')
1944 A.addApproval('CRVW', 2)
1945 B.addApproval('CRVW', 2)
1946 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1947 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1948 self.waitUntilSettled()
1949
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001950 self.assertEqual(A.data['status'], 'MERGED')
1951 self.assertEqual(A.reported, 2)
1952 self.assertEqual(B.data['status'], 'MERGED')
1953 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001954
Antoine Musso80edd5a2013-02-13 15:37:53 +01001955 def test_job_from_templates_launched(self):
1956 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001957
Antoine Musso80edd5a2013-02-13 15:37:53 +01001958 A = self.fake_gerrit.addFakeChange(
1959 'org/templated-project', 'master', 'A')
1960 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1961 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001962
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001963 self.assertEqual(self.getJobFromHistory('project-test1').result,
1964 'SUCCESS')
1965 self.assertEqual(self.getJobFromHistory('project-test2').result,
1966 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001967
James E. Blair3e98c022013-12-16 15:25:38 -08001968 def test_layered_templates(self):
1969 "Test whether a job generated via a template can be launched"
1970
1971 A = self.fake_gerrit.addFakeChange(
1972 'org/layered-project', 'master', 'A')
1973 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1974 self.waitUntilSettled()
1975
1976 self.assertEqual(self.getJobFromHistory('project-test1').result,
1977 'SUCCESS')
1978 self.assertEqual(self.getJobFromHistory('project-test2').result,
1979 'SUCCESS')
James E. Blairaea6cf62013-12-16 15:38:12 -08001980 self.assertEqual(self.getJobFromHistory('layered-project-test3'
1981 ).result, 'SUCCESS')
1982 self.assertEqual(self.getJobFromHistory('layered-project-test4'
1983 ).result, 'SUCCESS')
1984 # test5 should run twice because two templates define it
James E. Blair3e98c022013-12-16 15:25:38 -08001985 test5_count = 0
1986 for job in self.worker.build_history:
James E. Blairaea6cf62013-12-16 15:38:12 -08001987 if job.name == 'layered-project-foo-test5':
James E. Blair3e98c022013-12-16 15:25:38 -08001988 test5_count += 1
1989 self.assertEqual(job.result, 'SUCCESS')
1990 self.assertEqual(test5_count, 2)
1991 self.assertEqual(self.getJobFromHistory('project-test6').result,
1992 'SUCCESS')
1993
James E. Blaircaec0c52012-08-22 14:52:22 -07001994 def test_dependent_changes_dequeue(self):
1995 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001996
James E. Blaircaec0c52012-08-22 14:52:22 -07001997 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1998 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1999 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2000 A.addApproval('CRVW', 2)
2001 B.addApproval('CRVW', 2)
2002 C.addApproval('CRVW', 2)
2003
2004 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
2005 M1.setMerged()
2006
2007 # C -> B -> A -> M1
2008
2009 C.setDependsOn(B, 1)
2010 B.setDependsOn(A, 1)
2011 A.setDependsOn(M1, 1)
2012
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002013 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07002014
2015 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2016 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2017 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2018
2019 self.waitUntilSettled()
2020
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002021 self.assertEqual(A.data['status'], 'NEW')
2022 self.assertEqual(A.reported, 2)
2023 self.assertEqual(B.data['status'], 'NEW')
2024 self.assertEqual(B.reported, 2)
2025 self.assertEqual(C.data['status'], 'NEW')
2026 self.assertEqual(C.reported, 2)
2027 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07002028
James E. Blair972e3c72013-08-29 12:04:55 -07002029 def test_failing_dependent_changes(self):
2030 "Test that failing dependent patches are taken out of stream"
2031 self.worker.hold_jobs_in_build = True
2032 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2033 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2034 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2035 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2036 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
2037 A.addApproval('CRVW', 2)
2038 B.addApproval('CRVW', 2)
2039 C.addApproval('CRVW', 2)
2040 D.addApproval('CRVW', 2)
2041 E.addApproval('CRVW', 2)
2042
2043 # E, D -> C -> B, A
2044
2045 D.setDependsOn(C, 1)
2046 C.setDependsOn(B, 1)
2047
2048 self.worker.addFailTest('project-test1', B)
2049
2050 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2051 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2052 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2053 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2054 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2055
2056 self.waitUntilSettled()
2057 self.worker.release('.*-merge')
2058 self.waitUntilSettled()
2059 self.worker.release('.*-merge')
2060 self.waitUntilSettled()
2061 self.worker.release('.*-merge')
2062 self.waitUntilSettled()
2063 self.worker.release('.*-merge')
2064 self.waitUntilSettled()
2065 self.worker.release('.*-merge')
2066 self.waitUntilSettled()
2067
2068 self.worker.hold_jobs_in_build = False
2069 for build in self.builds:
2070 if build.parameters['ZUUL_CHANGE'] != '1':
2071 build.release()
2072 self.waitUntilSettled()
2073
2074 self.worker.release()
2075 self.waitUntilSettled()
2076
2077 self.assertEqual(A.data['status'], 'MERGED')
2078 self.assertEqual(A.reported, 2)
2079 self.assertEqual(B.data['status'], 'NEW')
2080 self.assertEqual(B.reported, 2)
2081 self.assertEqual(C.data['status'], 'NEW')
2082 self.assertEqual(C.reported, 2)
2083 self.assertEqual(D.data['status'], 'NEW')
2084 self.assertEqual(D.reported, 2)
2085 self.assertEqual(E.data['status'], 'MERGED')
2086 self.assertEqual(E.reported, 2)
2087 self.assertEqual(len(self.history), 18)
2088
James E. Blairec590122012-08-22 15:19:31 -07002089 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08002090 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07002091 # If it's dequeued more than once, we should see extra
2092 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07002093
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002094 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07002095 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2096 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2097 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2098 A.addApproval('CRVW', 2)
2099 B.addApproval('CRVW', 2)
2100 C.addApproval('CRVW', 2)
2101
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002102 self.worker.addFailTest('project1-test1', A)
2103 self.worker.addFailTest('project1-test2', A)
2104 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07002105
2106 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2107 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2108 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2109
2110 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07002111
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002112 self.assertEqual(len(self.builds), 1)
2113 self.assertEqual(self.builds[0].name, 'project1-merge')
2114 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07002115
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002116 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002117 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002118 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002119 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002120 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002121 self.waitUntilSettled()
2122
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002123 self.assertEqual(len(self.builds), 9)
2124 self.assertEqual(self.builds[0].name, 'project1-test1')
2125 self.assertEqual(self.builds[1].name, 'project1-test2')
2126 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
2127 self.assertEqual(self.builds[3].name, 'project1-test1')
2128 self.assertEqual(self.builds[4].name, 'project1-test2')
2129 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
2130 self.assertEqual(self.builds[6].name, 'project1-test1')
2131 self.assertEqual(self.builds[7].name, 'project1-test2')
2132 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07002133
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002134 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07002135 self.waitUntilSettled()
2136
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002137 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
2138 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07002139
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002140 self.worker.hold_jobs_in_build = False
2141 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07002142 self.waitUntilSettled()
2143
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002144 self.assertEqual(len(self.builds), 0)
2145 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07002146
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002147 self.assertEqual(A.data['status'], 'NEW')
2148 self.assertEqual(B.data['status'], 'MERGED')
2149 self.assertEqual(C.data['status'], 'MERGED')
2150 self.assertEqual(A.reported, 2)
2151 self.assertEqual(B.reported, 2)
2152 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07002153
2154 def test_nonvoting_job(self):
2155 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002156
James E. Blair4ec821f2012-08-23 15:28:28 -07002157 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
2158 'master', 'A')
2159 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002160 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07002161 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2162
2163 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07002164
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002165 self.assertEqual(A.data['status'], 'MERGED')
2166 self.assertEqual(A.reported, 2)
2167 self.assertEqual(
2168 self.getJobFromHistory('nonvoting-project-merge').result,
2169 'SUCCESS')
2170 self.assertEqual(
2171 self.getJobFromHistory('nonvoting-project-test1').result,
2172 'SUCCESS')
2173 self.assertEqual(
2174 self.getJobFromHistory('nonvoting-project-test2').result,
2175 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07002176
2177 def test_check_queue_success(self):
2178 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002179
James E. Blaire0487072012-08-29 17:38:31 -07002180 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2181 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2182
2183 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002184
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002185 self.assertEqual(A.data['status'], 'NEW')
2186 self.assertEqual(A.reported, 1)
2187 self.assertEqual(self.getJobFromHistory('project-merge').result,
2188 'SUCCESS')
2189 self.assertEqual(self.getJobFromHistory('project-test1').result,
2190 'SUCCESS')
2191 self.assertEqual(self.getJobFromHistory('project-test2').result,
2192 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07002193
2194 def test_check_queue_failure(self):
2195 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002196
James E. Blaire0487072012-08-29 17:38:31 -07002197 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002198 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07002199 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2200
2201 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002202
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002203 self.assertEqual(A.data['status'], 'NEW')
2204 self.assertEqual(A.reported, 1)
2205 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07002206 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002207 self.assertEqual(self.getJobFromHistory('project-test1').result,
2208 'SUCCESS')
2209 self.assertEqual(self.getJobFromHistory('project-test2').result,
2210 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07002211
2212 def test_dependent_behind_dequeue(self):
2213 "test that dependent changes behind dequeued changes work"
2214 # This complicated test is a reproduction of a real life bug
2215 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07002216
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002217 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07002218 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2219 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2220 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2221 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2222 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
2223 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
2224 D.setDependsOn(C, 1)
2225 E.setDependsOn(D, 1)
2226 A.addApproval('CRVW', 2)
2227 B.addApproval('CRVW', 2)
2228 C.addApproval('CRVW', 2)
2229 D.addApproval('CRVW', 2)
2230 E.addApproval('CRVW', 2)
2231 F.addApproval('CRVW', 2)
2232
2233 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07002234
2235 # Change object re-use in the gerrit trigger is hidden if
2236 # changes are added in quick succession; waiting makes it more
2237 # like real life.
2238 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2239 self.waitUntilSettled()
2240 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2241 self.waitUntilSettled()
2242
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002243 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002244 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002245 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002246 self.waitUntilSettled()
2247
2248 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2249 self.waitUntilSettled()
2250 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2251 self.waitUntilSettled()
2252 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2253 self.waitUntilSettled()
2254 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
2255 self.waitUntilSettled()
2256
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002257 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002258 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002259 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002260 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002261 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002262 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002263 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002264 self.waitUntilSettled()
2265
2266 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002267
2268 # Grab pointers to the jobs we want to release before
2269 # releasing any, because list indexes may change as
2270 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002271 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002272 a.release()
2273 b.release()
2274 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002275 self.waitUntilSettled()
2276
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002277 self.worker.hold_jobs_in_build = False
2278 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002279 self.waitUntilSettled()
2280
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002281 self.assertEqual(A.data['status'], 'NEW')
2282 self.assertEqual(B.data['status'], 'MERGED')
2283 self.assertEqual(C.data['status'], 'MERGED')
2284 self.assertEqual(D.data['status'], 'MERGED')
2285 self.assertEqual(E.data['status'], 'MERGED')
2286 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002287
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002288 self.assertEqual(A.reported, 2)
2289 self.assertEqual(B.reported, 2)
2290 self.assertEqual(C.reported, 2)
2291 self.assertEqual(D.reported, 2)
2292 self.assertEqual(E.reported, 2)
2293 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002294
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002295 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2296 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002297
2298 def test_merger_repack(self):
2299 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002300
James E. Blair05fed602012-09-07 12:45:24 -07002301 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2302 A.addApproval('CRVW', 2)
2303 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2304 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002305 self.assertEqual(self.getJobFromHistory('project-merge').result,
2306 'SUCCESS')
2307 self.assertEqual(self.getJobFromHistory('project-test1').result,
2308 'SUCCESS')
2309 self.assertEqual(self.getJobFromHistory('project-test2').result,
2310 'SUCCESS')
2311 self.assertEqual(A.data['status'], 'MERGED')
2312 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07002313 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002314 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002315
Monty Taylorbc758832013-06-17 17:22:42 -04002316 path = os.path.join(self.git_root, "org/project")
2317 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002318
2319 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2320 A.addApproval('CRVW', 2)
2321 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2322 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002323 self.assertEqual(self.getJobFromHistory('project-merge').result,
2324 'SUCCESS')
2325 self.assertEqual(self.getJobFromHistory('project-test1').result,
2326 'SUCCESS')
2327 self.assertEqual(self.getJobFromHistory('project-test2').result,
2328 'SUCCESS')
2329 self.assertEqual(A.data['status'], 'MERGED')
2330 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002331
James E. Blair4886f282012-11-15 09:27:33 -08002332 def test_merger_repack_large_change(self):
2333 "Test that the merger works with large changes after a repack"
2334 # https://bugs.launchpad.net/zuul/+bug/1078946
2335 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2336 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002337 path = os.path.join(self.upstream_root, "org/project1")
2338 print repack_repo(path)
2339 path = os.path.join(self.git_root, "org/project1")
2340 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002341
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('project1-merge').result,
2346 'SUCCESS')
2347 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2348 'SUCCESS')
2349 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2350 'SUCCESS')
2351 self.assertEqual(A.data['status'], 'MERGED')
2352 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002353
James E. Blair7ee88a22012-09-12 18:59:31 +02002354 def test_nonexistent_job(self):
2355 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002356 # Set to the state immediately after a restart
2357 self.resetGearmanServer()
2358 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002359
2360 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2361 A.addApproval('CRVW', 2)
2362 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2363 # There may be a thread about to report a lost change
2364 while A.reported < 2:
2365 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002366 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002367 self.assertFalse(job_names)
2368 self.assertEqual(A.data['status'], 'NEW')
2369 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002370 self.assertEmptyQueues()
2371
2372 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002373 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002374 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2375 A.addApproval('CRVW', 2)
2376 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2377 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002378 self.assertEqual(self.getJobFromHistory('project-merge').result,
2379 'SUCCESS')
2380 self.assertEqual(self.getJobFromHistory('project-test1').result,
2381 'SUCCESS')
2382 self.assertEqual(self.getJobFromHistory('project-test2').result,
2383 'SUCCESS')
2384 self.assertEqual(A.data['status'], 'MERGED')
2385 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002386
2387 def test_single_nonexistent_post_job(self):
2388 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002389 e = {
2390 "type": "ref-updated",
2391 "submitter": {
2392 "name": "User Name",
2393 },
2394 "refUpdate": {
2395 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2396 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2397 "refName": "master",
2398 "project": "org/project",
2399 }
2400 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002401 # Set to the state immediately after a restart
2402 self.resetGearmanServer()
2403 self.launcher.negative_function_cache_ttl = 0
2404
James E. Blairf62d4282012-12-31 17:01:50 -08002405 self.fake_gerrit.addEvent(e)
2406 self.waitUntilSettled()
2407
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002408 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002409
2410 def test_new_patchset_dequeues_old(self):
2411 "Test that a new patchset causes the old to be dequeued"
2412 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002413 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002414 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2415 M.setMerged()
2416
2417 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2418 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2419 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2420 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2421 A.addApproval('CRVW', 2)
2422 B.addApproval('CRVW', 2)
2423 C.addApproval('CRVW', 2)
2424 D.addApproval('CRVW', 2)
2425
2426 C.setDependsOn(B, 1)
2427 B.setDependsOn(A, 1)
2428 A.setDependsOn(M, 1)
2429
2430 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2431 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2432 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2433 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2434 self.waitUntilSettled()
2435
2436 B.addPatchset()
2437 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2438 self.waitUntilSettled()
2439
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002440 self.worker.hold_jobs_in_build = False
2441 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002442 self.waitUntilSettled()
2443
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002444 self.assertEqual(A.data['status'], 'MERGED')
2445 self.assertEqual(A.reported, 2)
2446 self.assertEqual(B.data['status'], 'NEW')
2447 self.assertEqual(B.reported, 2)
2448 self.assertEqual(C.data['status'], 'NEW')
2449 self.assertEqual(C.reported, 2)
2450 self.assertEqual(D.data['status'], 'MERGED')
2451 self.assertEqual(D.reported, 2)
2452 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002453
Arx Cruzb1b010d2013-10-28 19:49:59 -02002454 def test_zuul_url_return(self):
2455 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
2456 self.assertTrue(self.sched.config.has_option('zuul', 'zuul_url'))
2457 self.worker.hold_jobs_in_build = True
2458
2459 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2460 A.addApproval('CRVW', 2)
2461 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2462 self.waitUntilSettled()
2463
2464 self.assertEqual(len(self.builds), 1)
2465 for build in self.builds:
2466 self.assertTrue('ZUUL_URL' in build.parameters)
2467
2468 self.worker.hold_jobs_in_build = False
2469 self.worker.release()
2470 self.waitUntilSettled()
2471
James E. Blair2fa50962013-01-30 21:50:41 -08002472 def test_new_patchset_dequeues_old_on_head(self):
2473 "Test that a new patchset causes the old to be dequeued (at head)"
2474 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002475 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002476 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2477 M.setMerged()
2478 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2479 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2480 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2481 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2482 A.addApproval('CRVW', 2)
2483 B.addApproval('CRVW', 2)
2484 C.addApproval('CRVW', 2)
2485 D.addApproval('CRVW', 2)
2486
2487 C.setDependsOn(B, 1)
2488 B.setDependsOn(A, 1)
2489 A.setDependsOn(M, 1)
2490
2491 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2492 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2493 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2494 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2495 self.waitUntilSettled()
2496
2497 A.addPatchset()
2498 self.fake_gerrit.addEvent(A.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'], 'NEW')
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'], 'NEW')
2510 self.assertEqual(C.reported, 2)
2511 self.assertEqual(D.data['status'], 'MERGED')
2512 self.assertEqual(D.reported, 2)
2513 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002514
2515 def test_new_patchset_dequeues_old_without_dependents(self):
2516 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002517 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002518 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2519 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2520 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2521 A.addApproval('CRVW', 2)
2522 B.addApproval('CRVW', 2)
2523 C.addApproval('CRVW', 2)
2524
2525 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2526 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2527 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2528 self.waitUntilSettled()
2529
2530 B.addPatchset()
2531 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2532 self.waitUntilSettled()
2533
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002534 self.worker.hold_jobs_in_build = False
2535 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002536 self.waitUntilSettled()
2537
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002538 self.assertEqual(A.data['status'], 'MERGED')
2539 self.assertEqual(A.reported, 2)
2540 self.assertEqual(B.data['status'], 'NEW')
2541 self.assertEqual(B.reported, 2)
2542 self.assertEqual(C.data['status'], 'MERGED')
2543 self.assertEqual(C.reported, 2)
2544 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002545
2546 def test_new_patchset_dequeues_old_independent_queue(self):
2547 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002548 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002549 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2550 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2551 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2552 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2553 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2554 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2555 self.waitUntilSettled()
2556
2557 B.addPatchset()
2558 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2559 self.waitUntilSettled()
2560
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002561 self.worker.hold_jobs_in_build = False
2562 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002563 self.waitUntilSettled()
2564
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002565 self.assertEqual(A.data['status'], 'NEW')
2566 self.assertEqual(A.reported, 1)
2567 self.assertEqual(B.data['status'], 'NEW')
2568 self.assertEqual(B.reported, 1)
2569 self.assertEqual(C.data['status'], 'NEW')
2570 self.assertEqual(C.reported, 1)
2571 self.assertEqual(len(self.history), 10)
2572 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002573
2574 def test_zuul_refs(self):
2575 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002576 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002577 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2578 M1.setMerged()
2579 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2580 M2.setMerged()
2581
2582 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2583 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2584 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2585 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2586 A.addApproval('CRVW', 2)
2587 B.addApproval('CRVW', 2)
2588 C.addApproval('CRVW', 2)
2589 D.addApproval('CRVW', 2)
2590 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2591 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2592 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2593 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2594
2595 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002596 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002597 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002598 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002599 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002600 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002601 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002602 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002603 self.waitUntilSettled()
2604
James E. Blair7d0dedc2013-02-21 17:26:09 -08002605 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002606 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002607 if x.parameters['ZUUL_CHANGE'] == '3':
2608 a_zref = x.parameters['ZUUL_REF']
2609 if x.parameters['ZUUL_CHANGE'] == '4':
2610 b_zref = x.parameters['ZUUL_REF']
2611 if x.parameters['ZUUL_CHANGE'] == '5':
2612 c_zref = x.parameters['ZUUL_REF']
2613 if x.parameters['ZUUL_CHANGE'] == '6':
2614 d_zref = x.parameters['ZUUL_REF']
2615
2616 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002617 self.assertIsNotNone(a_zref)
2618 self.assertIsNotNone(b_zref)
2619 self.assertIsNotNone(c_zref)
2620 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002621
2622 # And they should all be different
2623 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002624 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002625
2626 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002627 self.assertTrue(self.ref_has_change(a_zref, A))
2628 self.assertFalse(self.ref_has_change(a_zref, B))
2629 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002630
2631 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002632 self.assertTrue(self.ref_has_change(b_zref, A))
2633 self.assertTrue(self.ref_has_change(b_zref, B))
2634 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002635
2636 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002637 self.assertTrue(self.ref_has_change(c_zref, A))
2638 self.assertTrue(self.ref_has_change(c_zref, B))
2639 self.assertTrue(self.ref_has_change(c_zref, C))
2640 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002641
2642 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002643 self.assertTrue(self.ref_has_change(d_zref, A))
2644 self.assertTrue(self.ref_has_change(d_zref, B))
2645 self.assertTrue(self.ref_has_change(d_zref, C))
2646 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002647
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002648 self.worker.hold_jobs_in_build = False
2649 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002650 self.waitUntilSettled()
2651
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002652 self.assertEqual(A.data['status'], 'MERGED')
2653 self.assertEqual(A.reported, 2)
2654 self.assertEqual(B.data['status'], 'MERGED')
2655 self.assertEqual(B.reported, 2)
2656 self.assertEqual(C.data['status'], 'MERGED')
2657 self.assertEqual(C.reported, 2)
2658 self.assertEqual(D.data['status'], 'MERGED')
2659 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002660
James E. Blair4a28a882013-08-23 15:17:33 -07002661 def test_rerun_on_error(self):
2662 "Test that if a worker fails to run a job, it is run again"
2663 self.worker.hold_jobs_in_build = True
2664 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2665 A.addApproval('CRVW', 2)
2666 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2667 self.waitUntilSettled()
2668
2669 self.builds[0].run_error = True
2670 self.worker.hold_jobs_in_build = False
2671 self.worker.release()
2672 self.waitUntilSettled()
2673 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2674 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2675
James E. Blair412e5582013-04-22 15:50:12 -07002676 def test_statsd(self):
2677 "Test each of the statsd methods used in the scheduler"
2678 import extras
2679 statsd = extras.try_import('statsd.statsd')
2680 statsd.incr('test-incr')
2681 statsd.timing('test-timing', 3)
2682 statsd.gauge('test-guage', 12)
2683 self.assertReportedStat('test-incr', '1|c')
2684 self.assertReportedStat('test-timing', '3|ms')
2685 self.assertReportedStat('test-guage', '12|g')
2686
James E. Blair70c71582013-03-06 08:50:50 -08002687 def test_file_jobs(self):
2688 "Test that file jobs run only when appropriate"
2689 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2690 A.addPatchset(['pip-requires'])
2691 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2692 A.addApproval('CRVW', 2)
2693 B.addApproval('CRVW', 2)
2694 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2695 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2696 self.waitUntilSettled()
2697
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002698 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002699 if x.name == 'project-testfile']
2700
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002701 self.assertEqual(len(testfile_jobs), 1)
2702 self.assertEqual(testfile_jobs[0].changes, '1,2')
2703 self.assertEqual(A.data['status'], 'MERGED')
2704 self.assertEqual(A.reported, 2)
2705 self.assertEqual(B.data['status'], 'MERGED')
2706 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002707
2708 def test_test_config(self):
2709 "Test that we can test the config"
2710 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002711 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002712 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002713 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002714
2715 def test_build_description(self):
2716 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002717 self.worker.registerFunction('set_description:' +
2718 self.worker.worker_id)
2719
2720 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2721 A.addApproval('CRVW', 2)
2722 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2723 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002724 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002725 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002726 self.assertTrue(re.search("Branch.*master", desc))
2727 self.assertTrue(re.search("Pipeline.*gate", desc))
2728 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2729 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2730 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2731 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002732
James E. Blair64ed6f22013-07-10 14:07:23 -07002733 def test_queue_precedence(self):
2734 "Test that queue precedence works"
2735
2736 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002737 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002738 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2739 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2740 A.addApproval('CRVW', 2)
2741 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2742
2743 self.waitUntilSettled()
2744 self.gearman_server.hold_jobs_in_queue = False
2745 self.gearman_server.release()
2746 self.waitUntilSettled()
2747
James E. Blair8de58bd2013-07-18 16:23:33 -07002748 # Run one build at a time to ensure non-race order:
2749 for x in range(6):
2750 self.release(self.builds[0])
2751 self.waitUntilSettled()
2752 self.worker.hold_jobs_in_build = False
2753 self.waitUntilSettled()
2754
James E. Blair64ed6f22013-07-10 14:07:23 -07002755 self.log.debug(self.history)
2756 self.assertEqual(self.history[0].pipeline, 'gate')
2757 self.assertEqual(self.history[1].pipeline, 'check')
2758 self.assertEqual(self.history[2].pipeline, 'gate')
2759 self.assertEqual(self.history[3].pipeline, 'gate')
2760 self.assertEqual(self.history[4].pipeline, 'check')
2761 self.assertEqual(self.history[5].pipeline, 'check')
2762
James E. Blair1843a552013-07-03 14:19:52 -07002763 def test_json_status(self):
2764 "Test that we can retrieve JSON status info"
2765 self.worker.hold_jobs_in_build = True
2766 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2767 A.addApproval('CRVW', 2)
2768 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2769 self.waitUntilSettled()
2770
2771 port = self.webapp.server.socket.getsockname()[1]
2772
2773 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2774 data = f.read()
2775
2776 self.worker.hold_jobs_in_build = False
2777 self.worker.release()
2778 self.waitUntilSettled()
2779
2780 data = json.loads(data)
2781 status_jobs = set()
2782 for p in data['pipelines']:
2783 for q in p['change_queues']:
2784 for head in q['heads']:
2785 for change in head:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002786 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002787 for job in change['jobs']:
2788 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002789 self.assertIn('project-merge', status_jobs)
2790 self.assertIn('project-test1', status_jobs)
2791 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002792
James E. Blairc3d428e2013-12-03 15:06:48 -08002793 def test_merging_queues(self):
2794 "Test that transitively-connected change queues are merged"
2795 self.config.set('zuul', 'layout_config',
2796 'tests/fixtures/layout-merge-queues.yaml')
2797 self.sched.reconfigure(self.config)
2798 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
2799
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002800 def test_node_label(self):
2801 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002802 self.worker.registerFunction('build:node-project-test1:debian')
2803
2804 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2805 A.addApproval('CRVW', 2)
2806 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2807 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002808
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002809 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2810 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2811 'debian')
2812 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002813
2814 def test_live_reconfiguration(self):
2815 "Test that live reconfiguration works"
2816 self.worker.hold_jobs_in_build = True
2817 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2818 A.addApproval('CRVW', 2)
2819 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2820 self.waitUntilSettled()
2821
2822 self.sched.reconfigure(self.config)
2823
2824 self.worker.hold_jobs_in_build = False
2825 self.worker.release()
2826 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002827 self.assertEqual(self.getJobFromHistory('project-merge').result,
2828 'SUCCESS')
2829 self.assertEqual(self.getJobFromHistory('project-test1').result,
2830 'SUCCESS')
2831 self.assertEqual(self.getJobFromHistory('project-test2').result,
2832 'SUCCESS')
2833 self.assertEqual(A.data['status'], 'MERGED')
2834 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002835
James E. Blaire712d9f2013-07-31 11:40:11 -07002836 def test_live_reconfiguration_functions(self):
2837 "Test live reconfiguration with a custom function"
2838 self.worker.registerFunction('build:node-project-test1:debian')
2839 self.worker.registerFunction('build:node-project-test1:wheezy')
2840 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2841 A.addApproval('CRVW', 2)
2842 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2843 self.waitUntilSettled()
2844
2845 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2846 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2847 'debian')
2848 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2849
2850 self.config.set('zuul', 'layout_config',
2851 'tests/fixtures/layout-live-'
2852 'reconfiguration-functions.yaml')
2853 self.sched.reconfigure(self.config)
2854 self.worker.build_history = []
2855
2856 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
2857 B.addApproval('CRVW', 2)
2858 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2859 self.waitUntilSettled()
2860
2861 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2862 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2863 'wheezy')
2864 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2865
James E. Blair287c06d2013-07-24 10:39:30 -07002866 def test_delayed_repo_init(self):
2867 self.config.set('zuul', 'layout_config',
2868 'tests/fixtures/layout-delayed-repo-init.yaml')
2869 self.sched.reconfigure(self.config)
2870
2871 self.init_repo("org/new-project")
2872 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2873
2874 A.addApproval('CRVW', 2)
2875 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2876 self.waitUntilSettled()
2877 self.assertEqual(self.getJobFromHistory('project-merge').result,
2878 'SUCCESS')
2879 self.assertEqual(self.getJobFromHistory('project-test1').result,
2880 'SUCCESS')
2881 self.assertEqual(self.getJobFromHistory('project-test2').result,
2882 'SUCCESS')
2883 self.assertEqual(A.data['status'], 'MERGED')
2884 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002885
Clark Boylan6dbbc482013-10-18 10:57:31 -07002886 def test_repo_deleted(self):
2887 self.config.set('zuul', 'layout_config',
2888 'tests/fixtures/layout-repo-deleted.yaml')
2889 self.sched.reconfigure(self.config)
2890
2891 self.init_repo("org/delete-project")
2892 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2893
2894 A.addApproval('CRVW', 2)
2895 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2896 self.waitUntilSettled()
2897 self.assertEqual(self.getJobFromHistory('project-merge').result,
2898 'SUCCESS')
2899 self.assertEqual(self.getJobFromHistory('project-test1').result,
2900 'SUCCESS')
2901 self.assertEqual(self.getJobFromHistory('project-test2').result,
2902 'SUCCESS')
2903 self.assertEqual(A.data['status'], 'MERGED')
2904 self.assertEqual(A.reported, 2)
2905
2906 # Delete org/new-project zuul repo. Should be recloned.
2907 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
2908
2909 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2910
2911 B.addApproval('CRVW', 2)
2912 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2913 self.waitUntilSettled()
2914 self.assertEqual(self.getJobFromHistory('project-merge').result,
2915 'SUCCESS')
2916 self.assertEqual(self.getJobFromHistory('project-test1').result,
2917 'SUCCESS')
2918 self.assertEqual(self.getJobFromHistory('project-test2').result,
2919 'SUCCESS')
2920 self.assertEqual(B.data['status'], 'MERGED')
2921 self.assertEqual(B.reported, 2)
2922
James E. Blair63bb0ef2013-07-29 17:14:51 -07002923 def test_timer(self):
2924 "Test that a periodic job is triggered"
2925 self.worker.hold_jobs_in_build = True
2926 self.config.set('zuul', 'layout_config',
2927 'tests/fixtures/layout-timer.yaml')
2928 self.sched.reconfigure(self.config)
2929 self.registerJobs()
2930
2931 start = time.time()
2932 failed = True
2933 while ((time.time() - start) < 30):
2934 if len(self.builds) == 2:
2935 failed = False
2936 break
2937 else:
2938 time.sleep(1)
2939
2940 if failed:
2941 raise Exception("Expected jobs never ran")
2942
2943 self.waitUntilSettled()
2944 port = self.webapp.server.socket.getsockname()[1]
2945
2946 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2947 data = f.read()
2948
2949 self.worker.hold_jobs_in_build = False
2950 self.worker.release()
2951 self.waitUntilSettled()
2952
2953 self.assertEqual(self.getJobFromHistory(
2954 'project-bitrot-stable-old').result, 'SUCCESS')
2955 self.assertEqual(self.getJobFromHistory(
2956 'project-bitrot-stable-older').result, 'SUCCESS')
2957
2958 data = json.loads(data)
2959 status_jobs = set()
2960 for p in data['pipelines']:
2961 for q in p['change_queues']:
2962 for head in q['heads']:
2963 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002964 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002965 for job in change['jobs']:
2966 status_jobs.add(job['name'])
2967 self.assertIn('project-bitrot-stable-old', status_jobs)
2968 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002969
2970 def test_check_smtp_pool(self):
2971 self.config.set('zuul', 'layout_config',
2972 'tests/fixtures/layout-smtp.yaml')
2973 self.sched.reconfigure(self.config)
2974
2975 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2976 self.waitUntilSettled()
2977
2978 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2979 self.waitUntilSettled()
2980
James E. Blairff80a2f2013-12-27 13:24:06 -08002981 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002982
2983 # A.messages only holds what FakeGerrit places in it. Thus we
2984 # work on the knowledge of what the first message should be as
2985 # it is only configured to go to SMTP.
2986
2987 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002988 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002989 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002990 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002991 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002992 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002993
2994 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002995 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002996 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002997 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002998 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002999 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003000
James E. Blaire5910202013-12-27 09:50:31 -08003001 def test_timer_smtp(self):
3002 "Test that a periodic job is triggered"
3003 self.config.set('zuul', 'layout_config',
3004 'tests/fixtures/layout-timer-smtp.yaml')
3005 self.sched.reconfigure(self.config)
3006 self.registerJobs()
3007
3008 start = time.time()
3009 failed = True
3010 while ((time.time() - start) < 30):
3011 if len(self.history) == 2:
3012 failed = False
3013 break
3014 else:
3015 time.sleep(1)
3016
3017 if failed:
3018 raise Exception("Expected jobs never ran")
3019
3020 self.waitUntilSettled()
3021
3022 self.assertEqual(self.getJobFromHistory(
3023 'project-bitrot-stable-old').result, 'SUCCESS')
3024 self.assertEqual(self.getJobFromHistory(
3025 'project-bitrot-stable-older').result, 'SUCCESS')
3026
James E. Blairff80a2f2013-12-27 13:24:06 -08003027 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003028
3029 # A.messages only holds what FakeGerrit places in it. Thus we
3030 # work on the knowledge of what the first message should be as
3031 # it is only configured to go to SMTP.
3032
3033 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003034 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003035 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003036 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003037 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003038 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003039
James E. Blairad28e912013-11-27 10:43:22 -08003040 def test_client_enqueue(self):
3041 "Test that the RPC client can enqueue a change"
3042 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3043 A.addApproval('CRVW', 2)
3044 A.addApproval('APRV', 1)
3045
3046 client = zuul.rpcclient.RPCClient('127.0.0.1',
3047 self.gearman_server.port)
3048 r = client.enqueue(pipeline='gate',
3049 project='org/project',
3050 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003051 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003052 self.waitUntilSettled()
3053 self.assertEqual(self.getJobFromHistory('project-merge').result,
3054 'SUCCESS')
3055 self.assertEqual(self.getJobFromHistory('project-test1').result,
3056 'SUCCESS')
3057 self.assertEqual(self.getJobFromHistory('project-test2').result,
3058 'SUCCESS')
3059 self.assertEqual(A.data['status'], 'MERGED')
3060 self.assertEqual(A.reported, 2)
3061 self.assertEqual(r, True)
3062
3063 def test_client_enqueue_negative(self):
3064 "Test that the RPC client returns errors"
3065 client = zuul.rpcclient.RPCClient('127.0.0.1',
3066 self.gearman_server.port)
3067 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3068 "Invalid project"):
3069 r = client.enqueue(pipeline='gate',
3070 project='project-does-not-exist',
3071 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003072 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003073 client.shutdown()
3074 self.assertEqual(r, False)
3075
3076 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3077 "Invalid pipeline"):
3078 r = client.enqueue(pipeline='pipeline-does-not-exist',
3079 project='org/project',
3080 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003081 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003082 client.shutdown()
3083 self.assertEqual(r, False)
3084
3085 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3086 "Invalid trigger"):
3087 r = client.enqueue(pipeline='gate',
3088 project='org/project',
3089 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003090 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003091 client.shutdown()
3092 self.assertEqual(r, False)
3093
3094 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3095 "Invalid change"):
3096 r = client.enqueue(pipeline='gate',
3097 project='org/project',
3098 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003099 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003100 client.shutdown()
3101 self.assertEqual(r, False)
3102
3103 self.waitUntilSettled()
3104 self.assertEqual(len(self.history), 0)
3105 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003106
3107 def test_client_promote(self):
3108 "Test that the RPC client can promote a change"
3109 self.worker.hold_jobs_in_build = True
3110 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3111 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3112 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3113 A.addApproval('CRVW', 2)
3114 B.addApproval('CRVW', 2)
3115 C.addApproval('CRVW', 2)
3116
3117 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3118 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3119 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3120
3121 self.waitUntilSettled()
3122
3123 client = zuul.rpcclient.RPCClient('127.0.0.1',
3124 self.gearman_server.port)
3125 r = client.promote(pipeline='gate',
3126 change_ids=['2,1', '3,1'])
3127
3128 self.worker.release('.*-merge')
3129 self.waitUntilSettled()
3130 self.worker.release('.*-merge')
3131 self.waitUntilSettled()
3132 self.worker.release('.*-merge')
3133 self.waitUntilSettled()
3134
3135 self.assertEqual(len(self.builds), 6)
3136 self.assertEqual(self.builds[0].name, 'project-test1')
3137 self.assertEqual(self.builds[1].name, 'project-test2')
3138 self.assertEqual(self.builds[2].name, 'project-test1')
3139 self.assertEqual(self.builds[3].name, 'project-test2')
3140 self.assertEqual(self.builds[4].name, 'project-test1')
3141 self.assertEqual(self.builds[5].name, 'project-test2')
3142
3143 self.assertTrue(self.job_has_changes(self.builds[0], B))
3144 self.assertFalse(self.job_has_changes(self.builds[0], A))
3145 self.assertFalse(self.job_has_changes(self.builds[0], C))
3146
3147 self.assertTrue(self.job_has_changes(self.builds[2], B))
3148 self.assertTrue(self.job_has_changes(self.builds[2], C))
3149 self.assertFalse(self.job_has_changes(self.builds[2], A))
3150
3151 self.assertTrue(self.job_has_changes(self.builds[4], B))
3152 self.assertTrue(self.job_has_changes(self.builds[4], C))
3153 self.assertTrue(self.job_has_changes(self.builds[4], A))
3154
3155 self.worker.release()
3156 self.waitUntilSettled()
3157
3158 self.assertEqual(A.data['status'], 'MERGED')
3159 self.assertEqual(A.reported, 2)
3160 self.assertEqual(B.data['status'], 'MERGED')
3161 self.assertEqual(B.reported, 2)
3162 self.assertEqual(C.data['status'], 'MERGED')
3163 self.assertEqual(C.reported, 2)
3164
3165 client.shutdown()
3166 self.assertEqual(r, True)
3167
3168 def test_client_promote_dependent(self):
3169 "Test that the RPC client can promote a dependent change"
3170 # C (depends on B) -> B -> A ; then promote C to get:
3171 # A -> C (depends on B) -> B
3172 self.worker.hold_jobs_in_build = True
3173 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3174 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3175 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3176
3177 C.setDependsOn(B, 1)
3178
3179 A.addApproval('CRVW', 2)
3180 B.addApproval('CRVW', 2)
3181 C.addApproval('CRVW', 2)
3182
3183 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3184 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3185 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3186
3187 self.waitUntilSettled()
3188
3189 client = zuul.rpcclient.RPCClient('127.0.0.1',
3190 self.gearman_server.port)
3191 r = client.promote(pipeline='gate',
3192 change_ids=['3,1'])
3193
3194 self.worker.release('.*-merge')
3195 self.waitUntilSettled()
3196 self.worker.release('.*-merge')
3197 self.waitUntilSettled()
3198 self.worker.release('.*-merge')
3199 self.waitUntilSettled()
3200
3201 self.assertEqual(len(self.builds), 6)
3202 self.assertEqual(self.builds[0].name, 'project-test1')
3203 self.assertEqual(self.builds[1].name, 'project-test2')
3204 self.assertEqual(self.builds[2].name, 'project-test1')
3205 self.assertEqual(self.builds[3].name, 'project-test2')
3206 self.assertEqual(self.builds[4].name, 'project-test1')
3207 self.assertEqual(self.builds[5].name, 'project-test2')
3208
3209 self.assertTrue(self.job_has_changes(self.builds[0], B))
3210 self.assertFalse(self.job_has_changes(self.builds[0], A))
3211 self.assertFalse(self.job_has_changes(self.builds[0], C))
3212
3213 self.assertTrue(self.job_has_changes(self.builds[2], B))
3214 self.assertTrue(self.job_has_changes(self.builds[2], C))
3215 self.assertFalse(self.job_has_changes(self.builds[2], A))
3216
3217 self.assertTrue(self.job_has_changes(self.builds[4], B))
3218 self.assertTrue(self.job_has_changes(self.builds[4], C))
3219 self.assertTrue(self.job_has_changes(self.builds[4], A))
3220
3221 self.worker.release()
3222 self.waitUntilSettled()
3223
3224 self.assertEqual(A.data['status'], 'MERGED')
3225 self.assertEqual(A.reported, 2)
3226 self.assertEqual(B.data['status'], 'MERGED')
3227 self.assertEqual(B.reported, 2)
3228 self.assertEqual(C.data['status'], 'MERGED')
3229 self.assertEqual(C.reported, 2)
3230
3231 client.shutdown()
3232 self.assertEqual(r, True)
3233
3234 def test_client_promote_negative(self):
3235 "Test that the RPC client returns errors for promotion"
3236 self.worker.hold_jobs_in_build = True
3237 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3238 A.addApproval('CRVW', 2)
3239 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3240 self.waitUntilSettled()
3241
3242 client = zuul.rpcclient.RPCClient('127.0.0.1',
3243 self.gearman_server.port)
3244
3245 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3246 r = client.promote(pipeline='nonexistent',
3247 change_ids=['2,1', '3,1'])
3248 client.shutdown()
3249 self.assertEqual(r, False)
3250
3251 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3252 r = client.promote(pipeline='gate',
3253 change_ids=['4,1'])
3254 client.shutdown()
3255 self.assertEqual(r, False)
3256
3257 self.worker.hold_jobs_in_build = False
3258 self.worker.release()
3259 self.waitUntilSettled()