blob: baedf97c45ac7631c79a55733bf437023b84a8b7 [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. Blairc053d022014-01-22 14:57:33 -0800217 def addApproval(self, category, value, username='jenkins',
218 granted_on=None):
219 if not granted_on:
220 granted_on = time.time()
James E. Blair8c803f82012-07-31 16:25:42 -0700221 approval = {'description': self.categories[category][0],
222 'type': category,
James E. Blairc053d022014-01-22 14:57:33 -0800223 'value': str(value),
James E. Blair64ff4ef2014-01-24 13:50:23 -0800224 'by': {
225 'username': username,
226 'email': username + '@example.com',
227 },
James E. Blairc053d022014-01-22 14:57:33 -0800228 'grantedOn': int(granted_on)}
229 for i, x in enumerate(self.patchsets[-1]['approvals'][:]):
James E. Blair64ff4ef2014-01-24 13:50:23 -0800230 if x['by']['username'] == username and x['type'] == category:
James E. Blairc053d022014-01-22 14:57:33 -0800231 del self.patchsets[-1]['approvals'][i]
James E. Blair8c803f82012-07-31 16:25:42 -0700232 self.patchsets[-1]['approvals'].append(approval)
233 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700234 'author': {'email': 'user@example.com',
235 'name': 'User Name',
236 'username': 'username'},
237 'change': {'branch': self.branch,
238 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
239 'number': str(self.number),
240 'owner': {'email': 'user@example.com',
241 'name': 'User Name',
242 'username': 'username'},
243 'project': self.project,
244 'subject': self.subject,
245 'topic': 'master',
246 'url': 'https://hostname/459'},
247 'comment': '',
248 'patchSet': self.patchsets[-1],
249 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700250 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700251 return json.loads(json.dumps(event))
252
James E. Blair8c803f82012-07-31 16:25:42 -0700253 def getSubmitRecords(self):
254 status = {}
255 for cat in self.categories.keys():
256 status[cat] = 0
257
258 for a in self.patchsets[-1]['approvals']:
259 cur = status[a['type']]
260 cat_min, cat_max = self.categories[a['type']][1:]
261 new = int(a['value'])
262 if new == cat_min:
263 cur = new
264 elif abs(new) > abs(cur):
265 cur = new
266 status[a['type']] = cur
267
268 labels = []
269 ok = True
270 for typ, cat in self.categories.items():
271 cur = status[typ]
272 cat_min, cat_max = cat[1:]
273 if cur == cat_min:
274 value = 'REJECT'
275 ok = False
276 elif cur == cat_max:
277 value = 'OK'
278 else:
279 value = 'NEED'
280 ok = False
281 labels.append({'label': cat[0], 'status': value})
282 if ok:
283 return [{'status': 'OK'}]
284 return [{'status': 'NOT_READY',
285 'labels': labels}]
286
287 def setDependsOn(self, other, patchset):
288 self.depends_on_change = other
289 d = {'id': other.data['id'],
290 'number': other.data['number'],
291 'ref': other.patchsets[patchset - 1]['ref']
292 }
293 self.data['dependsOn'] = [d]
294
295 other.needed_by_changes.append(self)
296 needed = other.data.get('neededBy', [])
297 d = {'id': self.data['id'],
298 'number': self.data['number'],
299 'ref': self.patchsets[patchset - 1]['ref'],
300 'revision': self.patchsets[patchset - 1]['revision']
301 }
302 needed.append(d)
303 other.data['neededBy'] = needed
304
James E. Blairb0fcae42012-07-17 11:12:10 -0700305 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700306 self.queried += 1
307 d = self.data.get('dependsOn')
308 if d:
309 d = d[0]
310 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
311 d['isCurrentPatchSet'] = True
312 else:
313 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700314 return json.loads(json.dumps(self.data))
315
316 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800317 if (self.depends_on_change and
318 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700319 return
James E. Blair127bc182012-08-28 15:55:15 -0700320 if self.fail_merge:
321 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700322 self.data['status'] = 'MERGED'
323 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700324
Monty Taylorbc758832013-06-17 17:22:42 -0400325 path = os.path.join(self.upstream_root, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700326 repo = git.Repo(path)
327 repo.heads[self.branch].commit = \
328 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700329
James E. Blaird466dc42012-07-31 10:42:56 -0700330 def setReported(self):
331 self.reported += 1
332
James E. Blairb0fcae42012-07-17 11:12:10 -0700333
334class FakeGerrit(object):
335 def __init__(self, *args, **kw):
336 self.event_queue = Queue.Queue()
337 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
338 self.change_number = 0
339 self.changes = {}
340
341 def addFakeChange(self, project, branch, subject):
342 self.change_number += 1
Monty Taylorbc758832013-06-17 17:22:42 -0400343 c = FakeChange(self, self.change_number, project, branch, subject,
344 upstream_root=self.upstream_root)
James E. Blairb0fcae42012-07-17 11:12:10 -0700345 self.changes[self.change_number] = c
346 return c
347
348 def addEvent(self, data):
349 return self.event_queue.put(data)
350
351 def getEvent(self):
352 return self.event_queue.get()
353
354 def eventDone(self):
355 self.event_queue.task_done()
356
357 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700358 number, ps = changeid.split(',')
359 change = self.changes[int(number)]
James E. Blair42f74822013-05-14 15:18:03 -0700360 change.messages.append(message)
James E. Blairb0fcae42012-07-17 11:12:10 -0700361 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700362 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700363 if message:
364 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700365
366 def query(self, number):
James E. Blairad28e912013-11-27 10:43:22 -0800367 change = self.changes.get(int(number))
368 if change:
369 return change.query()
370 return {}
James E. Blairb0fcae42012-07-17 11:12:10 -0700371
372 def startWatching(self, *args, **kw):
373 pass
374
375
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700376class BuildHistory(object):
377 def __init__(self, **kw):
378 self.__dict__.update(kw)
James E. Blairb0fcae42012-07-17 11:12:10 -0700379
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700380 def __repr__(self):
381 return ("<Completed build, result: %s name: %s #%s changes: %s>" %
382 (self.result, self.name, self.number, self.changes))
James E. Blairb0fcae42012-07-17 11:12:10 -0700383
384
James E. Blair8cc15a82012-08-01 11:17:57 -0700385class FakeURLOpener(object):
Monty Taylorbc758832013-06-17 17:22:42 -0400386 def __init__(self, upstream_root, fake_gerrit, url):
387 self.upstream_root = upstream_root
James E. Blair8cc15a82012-08-01 11:17:57 -0700388 self.fake_gerrit = fake_gerrit
389 self.url = url
390
391 def read(self):
392 res = urlparse.urlparse(self.url)
393 path = res.path
394 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200395 ret = '001e# service=git-upload-pack\n'
396 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
397 'multi_ack thin-pack side-band side-band-64k ofs-delta '
398 'shallow no-progress include-tag multi_ack_detailed no-done\n')
Monty Taylorbc758832013-06-17 17:22:42 -0400399 path = os.path.join(self.upstream_root, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700400 repo = git.Repo(path)
401 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200402 r = ref.object.hexsha + ' ' + ref.path + '\n'
403 ret += '%04x%s' % (len(r) + 4, r)
404 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700405 return ret
406
407
James E. Blair4886cc12012-07-18 15:39:41 -0700408class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000409 name = 'gerrit'
410
Monty Taylorbc758832013-06-17 17:22:42 -0400411 def __init__(self, upstream_root, *args):
412 super(FakeGerritTrigger, self).__init__(*args)
413 self.upstream_root = upstream_root
414
James E. Blair4886cc12012-07-18 15:39:41 -0700415 def getGitUrl(self, project):
Monty Taylorbc758832013-06-17 17:22:42 -0400416 return os.path.join(self.upstream_root, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700417
418
James E. Blair412e5582013-04-22 15:50:12 -0700419class FakeStatsd(threading.Thread):
420 def __init__(self):
421 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400422 self.daemon = True
James E. Blair412e5582013-04-22 15:50:12 -0700423 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
424 self.sock.bind(('', 0))
425 self.port = self.sock.getsockname()[1]
426 self.wake_read, self.wake_write = os.pipe()
427 self.stats = []
428
429 def run(self):
430 while True:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700431 poll = select.poll()
432 poll.register(self.sock, select.POLLIN)
433 poll.register(self.wake_read, select.POLLIN)
434 ret = poll.poll()
435 for (fd, event) in ret:
436 if fd == self.sock.fileno():
James E. Blair412e5582013-04-22 15:50:12 -0700437 data = self.sock.recvfrom(1024)
438 if not data:
439 return
440 self.stats.append(data[0])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700441 if fd == self.wake_read:
James E. Blair412e5582013-04-22 15:50:12 -0700442 return
443
444 def stop(self):
445 os.write(self.wake_write, '1\n')
446
447
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700448class FakeBuild(threading.Thread):
449 log = logging.getLogger("zuul.test")
450
451 def __init__(self, worker, job, number, node):
452 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400453 self.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700454 self.worker = worker
455 self.job = job
456 self.name = job.name.split(':')[1]
457 self.number = number
458 self.node = node
459 self.parameters = json.loads(job.arguments)
460 self.unique = self.parameters['ZUUL_UUID']
461 self.wait_condition = threading.Condition()
462 self.waiting = False
463 self.aborted = False
464 self.created = time.time()
465 self.description = ''
James E. Blair4a28a882013-08-23 15:17:33 -0700466 self.run_error = False
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700467
468 def release(self):
469 self.wait_condition.acquire()
470 self.wait_condition.notify()
471 self.waiting = False
472 self.log.debug("Build %s released" % self.unique)
473 self.wait_condition.release()
474
475 def isWaiting(self):
476 self.wait_condition.acquire()
477 if self.waiting:
478 ret = True
479 else:
480 ret = False
481 self.wait_condition.release()
482 return ret
483
484 def _wait(self):
485 self.wait_condition.acquire()
486 self.waiting = True
487 self.log.debug("Build %s waiting" % self.unique)
488 self.wait_condition.wait()
489 self.wait_condition.release()
490
491 def run(self):
492 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700493 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
494 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700495 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700496 'manager': self.worker.worker_id,
Joshua Heskethba8776a2014-01-12 14:35:40 +0800497 'worker_name': 'My Worker',
498 'worker_hostname': 'localhost',
499 'worker_ips': ['127.0.0.1', '192.168.1.1'],
500 'worker_fqdn': 'zuul.example.org',
501 'worker_program': 'FakeBuilder',
502 'worker_version': 'v1.1',
503 'worker_extra': {'something': 'else'}
Paul Belangerec49b4c2013-07-20 20:32:20 -0400504 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700505
Joshua Heskethba8776a2014-01-12 14:35:40 +0800506 self.log.debug('Running build %s' % self.unique)
507
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700508 self.job.sendWorkData(json.dumps(data))
Joshua Heskethba8776a2014-01-12 14:35:40 +0800509 self.log.debug('Sent WorkData packet with %s' % json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700510 self.job.sendWorkStatus(0, 100)
511
512 if self.worker.hold_jobs_in_build:
Joshua Heskethba8776a2014-01-12 14:35:40 +0800513 self.log.debug('Holding build %s' % self.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700514 self._wait()
515 self.log.debug("Build %s continuing" % self.unique)
516
517 self.worker.lock.acquire()
518
519 result = 'SUCCESS'
520 if (('ZUUL_REF' in self.parameters) and
521 self.worker.shouldFailTest(self.name,
522 self.parameters['ZUUL_REF'])):
523 result = 'FAILURE'
524 if self.aborted:
525 result = 'ABORTED'
526
James E. Blair4a28a882013-08-23 15:17:33 -0700527 if self.run_error:
528 work_fail = True
529 result = 'RUN_ERROR'
530 else:
531 data['result'] = result
532 work_fail = False
533
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700534 changes = None
535 if 'ZUUL_CHANGE_IDS' in self.parameters:
536 changes = self.parameters['ZUUL_CHANGE_IDS']
537
538 self.worker.build_history.append(
539 BuildHistory(name=self.name, number=self.number,
540 result=result, changes=changes, node=self.node,
James E. Blair64ed6f22013-07-10 14:07:23 -0700541 uuid=self.unique, description=self.description,
542 pipeline=self.parameters['ZUUL_PIPELINE'])
Paul Belangerec49b4c2013-07-20 20:32:20 -0400543 )
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700544
James E. Blair4a28a882013-08-23 15:17:33 -0700545 self.job.sendWorkData(json.dumps(data))
546 if work_fail:
547 self.job.sendWorkFail()
548 else:
549 self.job.sendWorkComplete(json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700550 del self.worker.gearman_jobs[self.job.unique]
551 self.worker.running_builds.remove(self)
552 self.worker.lock.release()
553
554
555class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400556 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700557 super(FakeWorker, self).__init__(worker_id)
558 self.gearman_jobs = {}
559 self.build_history = []
560 self.running_builds = []
561 self.build_counter = 0
562 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400563 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700564
565 self.hold_jobs_in_build = False
566 self.lock = threading.Lock()
567 self.__work_thread = threading.Thread(target=self.work)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400568 self.__work_thread.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700569 self.__work_thread.start()
570
571 def handleJob(self, job):
572 parts = job.name.split(":")
573 cmd = parts[0]
574 name = parts[1]
575 if len(parts) > 2:
576 node = parts[2]
577 else:
578 node = None
579 if cmd == 'build':
580 self.handleBuild(job, name, node)
581 elif cmd == 'stop':
582 self.handleStop(job, name)
583 elif cmd == 'set_description':
584 self.handleSetDescription(job, name)
585
586 def handleBuild(self, job, name, node):
587 build = FakeBuild(self, job, self.build_counter, node)
588 job.build = build
589 self.gearman_jobs[job.unique] = job
590 self.build_counter += 1
591
592 self.running_builds.append(build)
593 build.start()
594
595 def handleStop(self, job, name):
596 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700597 parameters = json.loads(job.arguments)
598 name = parameters['name']
599 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700600 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700601 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700602 build.aborted = True
603 build.release()
604 job.sendWorkComplete()
605 return
606 job.sendWorkFail()
607
608 def handleSetDescription(self, job, name):
609 self.log.debug("handle set description")
610 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700611 name = parameters['name']
612 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700613 descr = parameters['html_description']
614 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700615 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700616 build.description = descr
617 job.sendWorkComplete()
618 return
619 for build in self.build_history:
James E. Blair3c483cf2013-06-04 16:30:43 -0700620 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700621 build.description = descr
622 job.sendWorkComplete()
623 return
624 job.sendWorkFail()
625
626 def work(self):
627 while self.running:
628 try:
629 job = self.getJob()
630 except gear.InterruptedError:
631 continue
632 try:
633 self.handleJob(job)
634 except:
635 self.log.exception("Worker exception:")
636
637 def addFailTest(self, name, change):
638 l = self.fail_tests.get(name, [])
639 l.append(change)
640 self.fail_tests[name] = l
641
642 def shouldFailTest(self, name, ref):
643 l = self.fail_tests.get(name, [])
644 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400645 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700646 return True
647 return False
648
649 def release(self, regex=None):
650 builds = self.running_builds[:]
651 self.log.debug("releasing build %s (%s)" % (regex,
James E. Blair78e31b32013-07-09 09:11:34 -0700652 len(self.running_builds)))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700653 for build in builds:
654 if not regex or re.match(regex, build.name):
655 self.log.debug("releasing build %s" %
656 (build.parameters['ZUUL_UUID']))
657 build.release()
658 else:
659 self.log.debug("not releasing build %s" %
660 (build.parameters['ZUUL_UUID']))
661 self.log.debug("done releasing builds %s (%s)" %
662 (regex, len(self.running_builds)))
663
664
665class FakeGearmanServer(gear.Server):
666 def __init__(self):
667 self.hold_jobs_in_queue = False
668 super(FakeGearmanServer, self).__init__(0)
669
670 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700671 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
672 for job in queue:
673 if not hasattr(job, 'waiting'):
674 if job.name.startswith('build:'):
675 job.waiting = self.hold_jobs_in_queue
676 else:
677 job.waiting = False
678 if job.waiting:
679 continue
680 if job.name in connection.functions:
681 if not peek:
682 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400683 connection.related_jobs[job.handle] = job
684 job.worker_connection = connection
685 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700686 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700687 return None
688
689 def release(self, regex=None):
690 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700691 qlen = (len(self.high_queue) + len(self.normal_queue) +
692 len(self.low_queue))
693 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
James E. Blairdda6c912013-07-29 14:12:12 -0700694 for job in self.getQueue():
695 cmd, name = job.name.split(':')
696 if cmd != 'build':
697 continue
698 if not regex or re.match(regex, name):
699 self.log.debug("releasing queued job %s" %
700 job.unique)
701 job.waiting = False
702 released = True
703 else:
704 self.log.debug("not releasing queued job %s" %
705 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700706 if released:
707 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700708 qlen = (len(self.high_queue) + len(self.normal_queue) +
709 len(self.low_queue))
710 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700711
712
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000713class FakeSMTP(object):
714 log = logging.getLogger('zuul.FakeSMTP')
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000715
James E. Blairff80a2f2013-12-27 13:24:06 -0800716 def __init__(self, messages, server, port):
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000717 self.server = server
718 self.port = port
James E. Blairff80a2f2013-12-27 13:24:06 -0800719 self.messages = messages
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000720
721 def sendmail(self, from_email, to_email, msg):
722 self.log.info("Sending email from %s, to %s, with msg %s" % (
723 from_email, to_email, msg))
724
725 headers = msg.split('\n\n', 1)[0]
726 body = msg.split('\n\n', 1)[1]
727
James E. Blairff80a2f2013-12-27 13:24:06 -0800728 self.messages.append(dict(
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000729 from_email=from_email,
730 to_email=to_email,
731 msg=msg,
732 headers=headers,
733 body=body,
734 ))
735
736 return True
737
738 def quit(self):
739 return True
740
741
Monty Taylorbc758832013-06-17 17:22:42 -0400742class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700743 log = logging.getLogger("zuul.test")
744
745 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400746 super(TestScheduler, self).setUp()
747 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
748 try:
749 test_timeout = int(test_timeout)
750 except ValueError:
751 # If timeout value is invalid do not set a timeout.
752 test_timeout = 0
753 if test_timeout > 0:
James E. Blairff80a2f2013-12-27 13:24:06 -0800754 self.useFixture(fixtures.Timeout(test_timeout, gentle=False))
Monty Taylorbc758832013-06-17 17:22:42 -0400755
756 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
757 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
758 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
759 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
760 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
761 os.environ.get('OS_STDERR_CAPTURE') == '1'):
762 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
763 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Monty Taylore6a6c402013-07-02 09:25:55 -0700764 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
765 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400766 self.useFixture(fixtures.FakeLogger(
767 level=logging.DEBUG,
768 format='%(asctime)s %(name)-32s '
769 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700770 tmp_root = self.useFixture(fixtures.TempDir(
771 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400772 self.test_root = os.path.join(tmp_root, "zuul-test")
773 self.upstream_root = os.path.join(self.test_root, "upstream")
774 self.git_root = os.path.join(self.test_root, "git")
775
776 CONFIG.set('zuul', 'git_dir', self.git_root)
777 if os.path.exists(self.test_root):
778 shutil.rmtree(self.test_root)
779 os.makedirs(self.test_root)
780 os.makedirs(self.upstream_root)
781 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700782
783 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400784 self.init_repo("org/project")
785 self.init_repo("org/project1")
786 self.init_repo("org/project2")
787 self.init_repo("org/project3")
788 self.init_repo("org/one-job-project")
789 self.init_repo("org/nonvoting-project")
790 self.init_repo("org/templated-project")
James E. Blair3e98c022013-12-16 15:25:38 -0800791 self.init_repo("org/layered-project")
Monty Taylorbc758832013-06-17 17:22:42 -0400792 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700793 self.init_repo("org/conflict-project")
James E. Blair412e5582013-04-22 15:50:12 -0700794
795 self.statsd = FakeStatsd()
796 os.environ['STATSD_HOST'] = 'localhost'
797 os.environ['STATSD_PORT'] = str(self.statsd.port)
798 self.statsd.start()
799 # the statsd client object is configured in the statsd module import
800 reload(statsd)
801 reload(zuul.scheduler)
802
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700803 self.gearman_server = FakeGearmanServer()
804
805 self.config = ConfigParser.ConfigParser()
806 cfg = StringIO()
807 CONFIG.write(cfg)
808 cfg.seek(0)
809 self.config.readfp(cfg)
810 self.config.set('gearman', 'port', str(self.gearman_server.port))
811
Monty Taylorbc758832013-06-17 17:22:42 -0400812 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700813 self.worker.addServer('127.0.0.1', self.gearman_server.port)
814 self.gearman_server.worker = self.worker
815
James E. Blairb0fcae42012-07-17 11:12:10 -0700816 self.sched = zuul.scheduler.Scheduler()
817
James E. Blair8cc15a82012-08-01 11:17:57 -0700818 def URLOpenerFactory(*args, **kw):
819 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400820 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700821
James E. Blair8cc15a82012-08-01 11:17:57 -0700822 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700823 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700824
James E. Blairff80a2f2013-12-27 13:24:06 -0800825 self.smtp_messages = []
826
827 def FakeSMTPFactory(*args, **kw):
828 args = [self.smtp_messages] + list(args)
829 return FakeSMTP(*args, **kw)
830
James E. Blairb0fcae42012-07-17 11:12:10 -0700831 zuul.lib.gerrit.Gerrit = FakeGerrit
James E. Blairff80a2f2013-12-27 13:24:06 -0800832 self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTPFactory))
James E. Blairb0fcae42012-07-17 11:12:10 -0700833
Monty Taylorbc758832013-06-17 17:22:42 -0400834 self.gerrit = FakeGerritTrigger(
835 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700836 self.gerrit.replication_timeout = 1.5
837 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700838 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400839 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700840
James E. Blair1843a552013-07-03 14:19:52 -0700841 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
James E. Blairad28e912013-11-27 10:43:22 -0800842 self.rpc = zuul.rpclistener.RPCListener(self.config, self.sched)
James E. Blair1843a552013-07-03 14:19:52 -0700843
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700844 self.sched.setLauncher(self.launcher)
James E. Blair6c358e72013-07-29 17:06:47 -0700845 self.sched.registerTrigger(self.gerrit)
James E. Blair63bb0ef2013-07-29 17:14:51 -0700846 self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
847 self.sched.registerTrigger(self.timer)
James E. Blairb0fcae42012-07-17 11:12:10 -0700848
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000849 self.sched.registerReporter(
850 zuul.reporter.gerrit.Reporter(self.gerrit))
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000851 self.smtp_reporter = zuul.reporter.smtp.Reporter(
852 self.config.get('smtp', 'default_from'),
853 self.config.get('smtp', 'default_to'),
854 self.config.get('smtp', 'server'))
855 self.sched.registerReporter(self.smtp_reporter)
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000856
James E. Blairb0fcae42012-07-17 11:12:10 -0700857 self.sched.start()
858 self.sched.reconfigure(self.config)
859 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700860 self.webapp.start()
James E. Blairad28e912013-11-27 10:43:22 -0800861 self.rpc.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700862 self.launcher.gearman.waitForServer()
863 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400864 self.builds = self.worker.running_builds
865 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700866
James E. Blairfee8d652013-06-07 08:57:52 -0700867 self.addCleanup(self.assertFinalState)
868 self.addCleanup(self.shutdown)
869
870 def assertFinalState(self):
871 # Make sure that the change cache is cleared
James E. Blair6c358e72013-07-29 17:06:47 -0700872 self.assertEqual(len(self.gerrit._change_cache.keys()), 0)
Clark Boylan4ba48d92013-11-11 18:03:53 -0800873 # Make sure that git.Repo objects have been garbage collected.
874 repos = []
875 gc.collect()
876 for obj in gc.get_objects():
877 if isinstance(obj, git.Repo):
878 repos.append(obj)
879 self.assertEqual(len(repos), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700880 self.assertEmptyQueues()
881
882 def shutdown(self):
883 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700884 self.launcher.stop()
885 self.worker.shutdown()
886 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700887 self.gerrit.stop()
James E. Blair63bb0ef2013-07-29 17:14:51 -0700888 self.timer.stop()
James E. Blairb0fcae42012-07-17 11:12:10 -0700889 self.sched.stop()
890 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700891 self.statsd.stop()
892 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700893 self.webapp.stop()
894 self.webapp.join()
James E. Blairad28e912013-11-27 10:43:22 -0800895 self.rpc.stop()
896 self.rpc.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700897 threads = threading.enumerate()
898 if len(threads) > 1:
899 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400900 super(TestScheduler, self).tearDown()
901
902 def init_repo(self, project):
903 parts = project.split('/')
904 path = os.path.join(self.upstream_root, *parts[:-1])
905 if not os.path.exists(path):
906 os.makedirs(path)
907 path = os.path.join(self.upstream_root, project)
908 repo = git.Repo.init(path)
909
910 repo.config_writer().set_value('user', 'email', 'user@example.com')
911 repo.config_writer().set_value('user', 'name', 'User Name')
912 repo.config_writer().write()
913
914 fn = os.path.join(path, 'README')
915 f = open(fn, 'w')
916 f.write("test\n")
917 f.close()
918 repo.index.add([fn])
919 repo.index.commit('initial commit')
920 master = repo.create_head('master')
921 repo.create_tag('init')
922
923 mp = repo.create_head('mp')
924 repo.head.reference = mp
925 f = open(fn, 'a')
926 f.write("test mp\n")
927 f.close()
928 repo.index.add([fn])
929 repo.index.commit('mp commit')
930
931 repo.head.reference = master
932 repo.head.reset(index=True, working_tree=True)
933 repo.git.clean('-x', '-f', '-d')
934
935 def ref_has_change(self, ref, change):
936 path = os.path.join(self.git_root, change.project)
937 repo = git.Repo(path)
938 for commit in repo.iter_commits(ref):
939 if commit.message.strip() == ('%s-1' % change.subject):
940 return True
941 return False
942
943 def job_has_changes(self, *args):
944 job = args[0]
945 commits = args[1:]
946 if isinstance(job, FakeBuild):
947 parameters = job.parameters
948 else:
949 parameters = json.loads(job.arguments)
950 project = parameters['ZUUL_PROJECT']
951 path = os.path.join(self.git_root, project)
952 repo = git.Repo(path)
953 ref = parameters['ZUUL_REF']
954 sha = parameters['ZUUL_COMMIT']
955 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
956 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
957 commit_messages = ['%s-1' % commit.subject for commit in commits]
958 for msg in commit_messages:
959 if msg not in repo_messages:
960 return False
961 if repo_shas[0] != sha:
962 return False
963 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700964
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700965 def registerJobs(self):
966 count = 0
James E. Blaireff88162013-07-01 12:44:14 -0400967 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700968 self.worker.registerFunction('build:' + job)
969 count += 1
970 self.worker.registerFunction('stop:' + self.worker.worker_id)
971 count += 1
972
973 while len(self.gearman_server.functions) < count:
974 time.sleep(0)
975
976 def release(self, job):
977 if isinstance(job, FakeBuild):
978 job.release()
979 else:
980 job.waiting = False
981 self.log.debug("Queued job %s released" % job.unique)
982 self.gearman_server.wakeConnections()
983
984 def getParameter(self, job, name):
985 if isinstance(job, FakeBuild):
986 return job.parameters[name]
987 else:
988 parameters = json.loads(job.arguments)
989 return parameters[name]
990
991 def resetGearmanServer(self):
992 self.worker.setFunctions([])
993 while True:
994 done = True
995 for connection in self.gearman_server.active_connections:
James E. Blairad28e912013-11-27 10:43:22 -0800996 if (connection.functions and
997 connection.client_id != 'Zuul RPC Listener'):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700998 done = False
999 if done:
1000 break
1001 time.sleep(0)
1002 self.gearman_server.functions = set()
James E. Blairad28e912013-11-27 10:43:22 -08001003 self.rpc.register()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001004
1005 def haveAllBuildsReported(self):
1006 # See if Zuul is waiting on a meta job to complete
1007 if self.launcher.meta_jobs:
1008 return False
1009 # Find out if every build that the worker has completed has been
1010 # reported back to Zuul. If it hasn't then that means a Gearman
1011 # event is still in transit and the system is not stable.
1012 for build in self.worker.build_history:
1013 zbuild = self.launcher.builds.get(build.uuid)
1014 if not zbuild:
1015 # It has already been reported
1016 continue
1017 # It hasn't been reported yet.
1018 return False
1019 # Make sure that none of the worker connections are in GRAB_WAIT
1020 for connection in self.worker.active_connections:
1021 if connection.state == 'GRAB_WAIT':
1022 return False
1023 return True
1024
1025 def areAllBuildsWaiting(self):
1026 ret = True
1027
1028 builds = self.launcher.builds.values()
1029 for build in builds:
1030 client_job = None
1031 for conn in self.launcher.gearman.active_connections:
1032 for j in conn.related_jobs.values():
1033 if j.unique == build.uuid:
1034 client_job = j
1035 break
1036 if not client_job:
1037 self.log.debug("%s is not known to the gearman client" %
1038 build)
1039 ret = False
1040 continue
1041 if not client_job.handle:
1042 self.log.debug("%s has no handle" % client_job)
1043 ret = False
1044 continue
1045 server_job = self.gearman_server.jobs.get(client_job.handle)
1046 if not server_job:
1047 self.log.debug("%s is not known to the gearman server" %
1048 client_job)
1049 ret = False
1050 continue
1051 if not hasattr(server_job, 'waiting'):
1052 self.log.debug("%s is being enqueued" % server_job)
1053 ret = False
1054 continue
1055 if server_job.waiting:
1056 continue
1057 worker_job = self.worker.gearman_jobs.get(server_job.unique)
1058 if worker_job:
1059 if worker_job.build.isWaiting():
1060 continue
1061 else:
1062 self.log.debug("%s is running" % worker_job)
1063 ret = False
1064 else:
1065 self.log.debug("%s is unassigned" % server_job)
1066 ret = False
1067 return ret
1068
James E. Blairb0fcae42012-07-17 11:12:10 -07001069 def waitUntilSettled(self):
1070 self.log.debug("Waiting until settled...")
1071 start = time.time()
1072 while True:
1073 if time.time() - start > 10:
1074 print 'queue status:',
1075 print self.sched.trigger_event_queue.empty(),
1076 print self.sched.result_event_queue.empty(),
1077 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001078 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -07001079 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001080 # Make sure no new events show up while we're checking
1081 self.worker.lock.acquire()
1082 # have all build states propogated to zuul?
1083 if self.haveAllBuildsReported():
1084 # Join ensures that the queue is empty _and_ events have been
1085 # processed
1086 self.fake_gerrit.event_queue.join()
1087 self.sched.trigger_event_queue.join()
1088 self.sched.result_event_queue.join()
1089 if (self.sched.trigger_event_queue.empty() and
1090 self.sched.result_event_queue.empty() and
1091 self.fake_gerrit.event_queue.empty() and
1092 self.areAllBuildsWaiting()):
1093 self.worker.lock.release()
1094 self.log.debug("...settled.")
1095 return
1096 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001097 self.sched.wake_event.wait(0.1)
1098
James E. Blaird466dc42012-07-31 10:42:56 -07001099 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -08001100 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -07001101 return len(jobs)
1102
James E. Blair4ca985f2013-05-30 12:27:43 -07001103 def getJobFromHistory(self, name):
1104 history = self.worker.build_history
1105 for job in history:
1106 if job.name == name:
1107 return job
1108 raise Exception("Unable to find job %s in history" % name)
1109
James E. Blaire0487072012-08-29 17:38:31 -07001110 def assertEmptyQueues(self):
1111 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001112 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001113 for queue in pipeline.queues:
1114 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001115 print 'pipeline %s queue %s contents %s' % (
1116 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001117 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001118
James E. Blair66eeebf2013-07-27 17:44:32 -07001119 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001120 start = time.time()
1121 while time.time() < (start + 5):
1122 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001123 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001124 k, v = stat.split(':')
1125 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001126 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001127 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001128 elif value:
1129 if value == v:
1130 return
1131 elif kind:
1132 if v.endswith('|' + kind):
1133 return
James E. Blair412e5582013-04-22 15:50:12 -07001134 time.sleep(0.1)
1135
1136 pprint.pprint(self.statsd.stats)
1137 raise Exception("Key %s not found in reported stats" % key)
1138
James E. Blairb0fcae42012-07-17 11:12:10 -07001139 def test_jobs_launched(self):
1140 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001141
James E. Blairb0fcae42012-07-17 11:12:10 -07001142 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001143 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001144 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1145 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001146 self.assertEqual(self.getJobFromHistory('project-merge').result,
1147 'SUCCESS')
1148 self.assertEqual(self.getJobFromHistory('project-test1').result,
1149 'SUCCESS')
1150 self.assertEqual(self.getJobFromHistory('project-test2').result,
1151 'SUCCESS')
1152 self.assertEqual(A.data['status'], 'MERGED')
1153 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001154
James E. Blair66eeebf2013-07-27 17:44:32 -07001155 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1156 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1157 value='1|g')
1158 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1159 kind='ms')
1160 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1161 value='1|c')
1162 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1163 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1164 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001165 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001166 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001167 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001168 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001169
James E. Blair3cb10702013-08-24 08:56:03 -07001170 def test_initial_pipeline_gauges(self):
1171 "Test that each pipeline reported its length on start"
1172 pipeline_names = self.sched.layout.pipelines.keys()
1173 self.assertNotEqual(len(pipeline_names), 0)
1174 for name in pipeline_names:
1175 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
1176 value='0|g')
1177
James E. Blair42f74822013-05-14 15:18:03 -07001178 def test_duplicate_pipelines(self):
1179 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001180
James E. Blair42f74822013-05-14 15:18:03 -07001181 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1182 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1183 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001184
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001185 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001186 self.history[0].name == 'project-test1'
1187 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001188
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001189 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001190 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001191 self.assertIn('dup1/project-test1', A.messages[0])
1192 self.assertNotIn('dup2/project-test1', A.messages[0])
1193 self.assertNotIn('dup1/project-test1', A.messages[1])
1194 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001195 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001196 self.assertIn('dup1/project-test1', A.messages[1])
1197 self.assertNotIn('dup2/project-test1', A.messages[1])
1198 self.assertNotIn('dup1/project-test1', A.messages[0])
1199 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001200
James E. Blairb0fcae42012-07-17 11:12:10 -07001201 def test_parallel_changes(self):
1202 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001203
1204 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001205 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1206 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1207 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001208 A.addApproval('CRVW', 2)
1209 B.addApproval('CRVW', 2)
1210 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001211
1212 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1213 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1214 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1215
1216 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001217 self.assertEqual(len(self.builds), 1)
1218 self.assertEqual(self.builds[0].name, 'project-merge')
1219 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001220
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001221 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001222 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001223 self.assertEqual(len(self.builds), 3)
1224 self.assertEqual(self.builds[0].name, 'project-test1')
1225 self.assertTrue(self.job_has_changes(self.builds[0], A))
1226 self.assertEqual(self.builds[1].name, 'project-test2')
1227 self.assertTrue(self.job_has_changes(self.builds[1], A))
1228 self.assertEqual(self.builds[2].name, 'project-merge')
1229 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001230
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001231 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001232 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001233 self.assertEqual(len(self.builds), 5)
1234 self.assertEqual(self.builds[0].name, 'project-test1')
1235 self.assertTrue(self.job_has_changes(self.builds[0], A))
1236 self.assertEqual(self.builds[1].name, 'project-test2')
1237 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001238
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001239 self.assertEqual(self.builds[2].name, 'project-test1')
1240 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1241 self.assertEqual(self.builds[3].name, 'project-test2')
1242 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001243
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001244 self.assertEqual(self.builds[4].name, 'project-merge')
1245 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001246
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001247 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001248 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001249 self.assertEqual(len(self.builds), 6)
1250 self.assertEqual(self.builds[0].name, 'project-test1')
1251 self.assertTrue(self.job_has_changes(self.builds[0], A))
1252 self.assertEqual(self.builds[1].name, 'project-test2')
1253 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001254
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001255 self.assertEqual(self.builds[2].name, 'project-test1')
1256 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1257 self.assertEqual(self.builds[3].name, 'project-test2')
1258 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001259
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001260 self.assertEqual(self.builds[4].name, 'project-test1')
1261 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1262 self.assertEqual(self.builds[5].name, 'project-test2')
1263 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001264
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001265 self.worker.hold_jobs_in_build = False
1266 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001267 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001268 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001269
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001270 self.assertEqual(len(self.history), 9)
1271 self.assertEqual(A.data['status'], 'MERGED')
1272 self.assertEqual(B.data['status'], 'MERGED')
1273 self.assertEqual(C.data['status'], 'MERGED')
1274 self.assertEqual(A.reported, 2)
1275 self.assertEqual(B.reported, 2)
1276 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001277
1278 def test_failed_changes(self):
1279 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001280 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001281
James E. Blairb02a3bb2012-07-30 17:49:55 -07001282 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1283 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001284 A.addApproval('CRVW', 2)
1285 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001286
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001287 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001288
James E. Blaire2819012013-06-28 17:17:26 -04001289 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1290 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001291 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001292
1293 self.worker.release('.*-merge')
1294 self.waitUntilSettled()
1295
1296 self.worker.hold_jobs_in_build = False
1297 self.worker.release()
1298
1299 self.waitUntilSettled()
1300 # It's certain that the merge job for change 2 will run, but
1301 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001302 self.assertTrue(len(self.history) > 6)
1303 self.assertEqual(A.data['status'], 'NEW')
1304 self.assertEqual(B.data['status'], 'MERGED')
1305 self.assertEqual(A.reported, 2)
1306 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001307
1308 def test_independent_queues(self):
1309 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001310
1311 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001312 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001313 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1314 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001315 A.addApproval('CRVW', 2)
1316 B.addApproval('CRVW', 2)
1317 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001318
1319 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1320 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1321 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1322
James E. Blairb02a3bb2012-07-30 17:49:55 -07001323 self.waitUntilSettled()
1324
1325 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001326 self.assertEqual(len(self.builds), 2)
1327 self.assertEqual(self.builds[0].name, 'project-merge')
1328 self.assertTrue(self.job_has_changes(self.builds[0], A))
1329 self.assertEqual(self.builds[1].name, 'project1-merge')
1330 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001331
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001332 # Release the current merge builds
1333 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001334 self.waitUntilSettled()
1335 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001336 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001337 self.waitUntilSettled()
1338
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001339 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001340 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001341 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001342
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001343 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001344 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001345 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001346
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001347 self.assertEqual(len(self.history), 11)
1348 self.assertEqual(A.data['status'], 'MERGED')
1349 self.assertEqual(B.data['status'], 'MERGED')
1350 self.assertEqual(C.data['status'], 'MERGED')
1351 self.assertEqual(A.reported, 2)
1352 self.assertEqual(B.reported, 2)
1353 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001354
1355 def test_failed_change_at_head(self):
1356 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001357
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001358 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001359 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1360 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1361 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001362 A.addApproval('CRVW', 2)
1363 B.addApproval('CRVW', 2)
1364 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001365
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001366 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001367
1368 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1369 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1370 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1371
1372 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001373
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001374 self.assertEqual(len(self.builds), 1)
1375 self.assertEqual(self.builds[0].name, 'project-merge')
1376 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001377
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001378 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001379 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001380 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001381 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001382 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001383 self.waitUntilSettled()
1384
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001385 self.assertEqual(len(self.builds), 6)
1386 self.assertEqual(self.builds[0].name, 'project-test1')
1387 self.assertEqual(self.builds[1].name, 'project-test2')
1388 self.assertEqual(self.builds[2].name, 'project-test1')
1389 self.assertEqual(self.builds[3].name, 'project-test2')
1390 self.assertEqual(self.builds[4].name, 'project-test1')
1391 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001392
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001393 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001394 self.waitUntilSettled()
1395
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001396 # project-test2, project-merge for B
1397 self.assertEqual(len(self.builds), 2)
1398 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001399
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001400 self.worker.hold_jobs_in_build = False
1401 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001402 self.waitUntilSettled()
1403
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001404 self.assertEqual(len(self.builds), 0)
1405 self.assertEqual(len(self.history), 15)
1406 self.assertEqual(A.data['status'], 'NEW')
1407 self.assertEqual(B.data['status'], 'MERGED')
1408 self.assertEqual(C.data['status'], 'MERGED')
1409 self.assertEqual(A.reported, 2)
1410 self.assertEqual(B.reported, 2)
1411 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001412
James E. Blair0aac4872013-08-23 14:02:38 -07001413 def test_failed_change_in_middle(self):
1414 "Test a failed change in the middle of the queue"
1415
1416 self.worker.hold_jobs_in_build = True
1417 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1418 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1419 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1420 A.addApproval('CRVW', 2)
1421 B.addApproval('CRVW', 2)
1422 C.addApproval('CRVW', 2)
1423
1424 self.worker.addFailTest('project-test1', B)
1425
1426 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1427 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1428 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1429
1430 self.waitUntilSettled()
1431
1432 self.worker.release('.*-merge')
1433 self.waitUntilSettled()
1434 self.worker.release('.*-merge')
1435 self.waitUntilSettled()
1436 self.worker.release('.*-merge')
1437 self.waitUntilSettled()
1438
1439 self.assertEqual(len(self.builds), 6)
1440 self.assertEqual(self.builds[0].name, 'project-test1')
1441 self.assertEqual(self.builds[1].name, 'project-test2')
1442 self.assertEqual(self.builds[2].name, 'project-test1')
1443 self.assertEqual(self.builds[3].name, 'project-test2')
1444 self.assertEqual(self.builds[4].name, 'project-test1')
1445 self.assertEqual(self.builds[5].name, 'project-test2')
1446
1447 self.release(self.builds[2])
1448 self.waitUntilSettled()
1449
James E. Blair972e3c72013-08-29 12:04:55 -07001450 # project-test1 and project-test2 for A
1451 # project-test2 for B
1452 # project-merge for C (without B)
1453 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -07001454 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1455
James E. Blair972e3c72013-08-29 12:04:55 -07001456 self.worker.release('.*-merge')
1457 self.waitUntilSettled()
1458
1459 # project-test1 and project-test2 for A
1460 # project-test2 for B
1461 # project-test1 and project-test2 for C
1462 self.assertEqual(len(self.builds), 5)
1463
James E. Blair0aac4872013-08-23 14:02:38 -07001464 items = self.sched.layout.pipelines['gate'].getAllItems()
1465 builds = items[0].current_build_set.getBuilds()
1466 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1467 self.assertEqual(self.countJobResults(builds, None), 2)
1468 builds = items[1].current_build_set.getBuilds()
1469 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1470 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1471 self.assertEqual(self.countJobResults(builds, None), 1)
1472 builds = items[2].current_build_set.getBuilds()
1473 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -07001474 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -07001475
1476 self.worker.hold_jobs_in_build = False
1477 self.worker.release()
1478 self.waitUntilSettled()
1479
1480 self.assertEqual(len(self.builds), 0)
1481 self.assertEqual(len(self.history), 12)
1482 self.assertEqual(A.data['status'], 'MERGED')
1483 self.assertEqual(B.data['status'], 'NEW')
1484 self.assertEqual(C.data['status'], 'MERGED')
1485 self.assertEqual(A.reported, 2)
1486 self.assertEqual(B.reported, 2)
1487 self.assertEqual(C.reported, 2)
1488
James E. Blaird466dc42012-07-31 10:42:56 -07001489 def test_failed_change_at_head_with_queue(self):
1490 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001491
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001492 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001493 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1494 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1495 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001496 A.addApproval('CRVW', 2)
1497 B.addApproval('CRVW', 2)
1498 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001499
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001500 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001501
1502 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1503 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1504 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1505
1506 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001507 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001508 self.assertEqual(len(self.builds), 0)
1509 self.assertEqual(len(queue), 1)
1510 self.assertEqual(queue[0].name, 'build:project-merge')
1511 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001512
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001513 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001514 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001515 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001516 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001517 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001518 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001519 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001520
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001521 self.assertEqual(len(self.builds), 0)
1522 self.assertEqual(len(queue), 6)
1523 self.assertEqual(queue[0].name, 'build:project-test1')
1524 self.assertEqual(queue[1].name, 'build:project-test2')
1525 self.assertEqual(queue[2].name, 'build:project-test1')
1526 self.assertEqual(queue[3].name, 'build:project-test2')
1527 self.assertEqual(queue[4].name, 'build:project-test1')
1528 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001529
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001530 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001531 self.waitUntilSettled()
1532
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001533 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001534 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001535 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1536 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001537
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001538 self.gearman_server.hold_jobs_in_queue = False
1539 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001540 self.waitUntilSettled()
1541
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001542 self.assertEqual(len(self.builds), 0)
1543 self.assertEqual(len(self.history), 11)
1544 self.assertEqual(A.data['status'], 'NEW')
1545 self.assertEqual(B.data['status'], 'MERGED')
1546 self.assertEqual(C.data['status'], 'MERGED')
1547 self.assertEqual(A.reported, 2)
1548 self.assertEqual(B.reported, 2)
1549 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001550
James E. Blairfef71632013-09-23 11:15:47 -07001551 def test_two_failed_changes_at_head(self):
1552 "Test that changes are reparented correctly if 2 fail at head"
1553
1554 self.worker.hold_jobs_in_build = True
1555 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1556 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1557 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1558 A.addApproval('CRVW', 2)
1559 B.addApproval('CRVW', 2)
1560 C.addApproval('CRVW', 2)
1561
1562 self.worker.addFailTest('project-test1', A)
1563 self.worker.addFailTest('project-test1', B)
1564
1565 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1566 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1567 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1568 self.waitUntilSettled()
1569
1570 self.worker.release('.*-merge')
1571 self.waitUntilSettled()
1572 self.worker.release('.*-merge')
1573 self.waitUntilSettled()
1574 self.worker.release('.*-merge')
1575 self.waitUntilSettled()
1576
1577 self.assertEqual(len(self.builds), 6)
1578 self.assertEqual(self.builds[0].name, 'project-test1')
1579 self.assertEqual(self.builds[1].name, 'project-test2')
1580 self.assertEqual(self.builds[2].name, 'project-test1')
1581 self.assertEqual(self.builds[3].name, 'project-test2')
1582 self.assertEqual(self.builds[4].name, 'project-test1')
1583 self.assertEqual(self.builds[5].name, 'project-test2')
1584
1585 self.assertTrue(self.job_has_changes(self.builds[0], 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.assertTrue(self.job_has_changes(self.builds[4], B))
1590 self.assertTrue(self.job_has_changes(self.builds[4], C))
1591
1592 # Fail change B first
1593 self.release(self.builds[2])
1594 self.waitUntilSettled()
1595
1596 # restart of C after B failure
1597 self.worker.release('.*-merge')
1598 self.waitUntilSettled()
1599
1600 self.assertEqual(len(self.builds), 5)
1601 self.assertEqual(self.builds[0].name, 'project-test1')
1602 self.assertEqual(self.builds[1].name, 'project-test2')
1603 self.assertEqual(self.builds[2].name, 'project-test2')
1604 self.assertEqual(self.builds[3].name, 'project-test1')
1605 self.assertEqual(self.builds[4].name, 'project-test2')
1606
1607 self.assertTrue(self.job_has_changes(self.builds[1], A))
1608 self.assertTrue(self.job_has_changes(self.builds[2], A))
1609 self.assertTrue(self.job_has_changes(self.builds[2], B))
1610 self.assertTrue(self.job_has_changes(self.builds[4], A))
1611 self.assertFalse(self.job_has_changes(self.builds[4], B))
1612 self.assertTrue(self.job_has_changes(self.builds[4], C))
1613
1614 # Finish running all passing jobs for change A
1615 self.release(self.builds[1])
1616 self.waitUntilSettled()
1617 # Fail and report change A
1618 self.release(self.builds[0])
1619 self.waitUntilSettled()
1620
1621 # restart of B,C after A failure
1622 self.worker.release('.*-merge')
1623 self.waitUntilSettled()
1624 self.worker.release('.*-merge')
1625 self.waitUntilSettled()
1626
1627 self.assertEqual(len(self.builds), 4)
1628 self.assertEqual(self.builds[0].name, 'project-test1') # B
1629 self.assertEqual(self.builds[1].name, 'project-test2') # B
1630 self.assertEqual(self.builds[2].name, 'project-test1') # C
1631 self.assertEqual(self.builds[3].name, 'project-test2') # C
1632
1633 self.assertFalse(self.job_has_changes(self.builds[1], A))
1634 self.assertTrue(self.job_has_changes(self.builds[1], B))
1635 self.assertFalse(self.job_has_changes(self.builds[1], C))
1636
1637 self.assertFalse(self.job_has_changes(self.builds[2], A))
1638 # After A failed and B and C restarted, B should be back in
1639 # C's tests because it has not failed yet.
1640 self.assertTrue(self.job_has_changes(self.builds[2], B))
1641 self.assertTrue(self.job_has_changes(self.builds[2], C))
1642
1643 self.worker.hold_jobs_in_build = False
1644 self.worker.release()
1645 self.waitUntilSettled()
1646
1647 self.assertEqual(len(self.builds), 0)
1648 self.assertEqual(len(self.history), 21)
1649 self.assertEqual(A.data['status'], 'NEW')
1650 self.assertEqual(B.data['status'], 'NEW')
1651 self.assertEqual(C.data['status'], 'MERGED')
1652 self.assertEqual(A.reported, 2)
1653 self.assertEqual(B.reported, 2)
1654 self.assertEqual(C.reported, 2)
1655
James E. Blair8c803f82012-07-31 16:25:42 -07001656 def test_patch_order(self):
1657 "Test that dependent patches are tested in the right order"
1658 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1659 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1660 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1661 A.addApproval('CRVW', 2)
1662 B.addApproval('CRVW', 2)
1663 C.addApproval('CRVW', 2)
1664
1665 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1666 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1667 M2.setMerged()
1668 M1.setMerged()
1669
1670 # C -> B -> A -> M1 -> M2
1671 # M2 is here to make sure it is never queried. If it is, it
1672 # means zuul is walking down the entire history of merged
1673 # changes.
1674
1675 C.setDependsOn(B, 1)
1676 B.setDependsOn(A, 1)
1677 A.setDependsOn(M1, 1)
1678 M1.setDependsOn(M2, 1)
1679
1680 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1681
1682 self.waitUntilSettled()
1683
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001684 self.assertEqual(A.data['status'], 'NEW')
1685 self.assertEqual(B.data['status'], 'NEW')
1686 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001687
1688 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1689 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1690
1691 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001692 self.assertEqual(M2.queried, 0)
1693 self.assertEqual(A.data['status'], 'MERGED')
1694 self.assertEqual(B.data['status'], 'MERGED')
1695 self.assertEqual(C.data['status'], 'MERGED')
1696 self.assertEqual(A.reported, 2)
1697 self.assertEqual(B.reported, 2)
1698 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001699
James E. Blair0e933c52013-07-11 10:18:52 -07001700 def test_trigger_cache(self):
1701 "Test that the trigger cache operates correctly"
1702 self.worker.hold_jobs_in_build = True
1703
1704 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1705 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1706 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1707 A.addApproval('CRVW', 2)
1708 B.addApproval('CRVW', 2)
1709
1710 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1711 M1.setMerged()
1712
1713 B.setDependsOn(A, 1)
1714 A.setDependsOn(M1, 1)
1715
1716 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1717 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1718
1719 self.waitUntilSettled()
1720
1721 for build in self.builds:
1722 if build.parameters['ZUUL_PIPELINE'] == 'check':
1723 build.release()
1724 self.waitUntilSettled()
1725 for build in self.builds:
1726 if build.parameters['ZUUL_PIPELINE'] == 'check':
1727 build.release()
1728 self.waitUntilSettled()
1729
1730 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1731 self.waitUntilSettled()
1732
James E. Blair6c358e72013-07-29 17:06:47 -07001733 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001734 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001735 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001736
1737 self.worker.hold_jobs_in_build = False
1738 self.worker.release()
1739 self.waitUntilSettled()
1740
1741 self.assertEqual(A.data['status'], 'MERGED')
1742 self.assertEqual(B.data['status'], 'MERGED')
1743 self.assertEqual(A.queried, 2) # Initial and isMerged
1744 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1745
James E. Blair8c803f82012-07-31 16:25:42 -07001746 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001747 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001748 # TODO: move to test_gerrit (this is a unit test!)
1749 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001750 trigger = self.sched.layout.pipelines['gate'].trigger
1751 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001752 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001753 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001754
1755 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001756 a = trigger.getChange(1, 2, refresh=True)
1757 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001758
1759 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001760 a = trigger.getChange(1, 2, refresh=True)
1761 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1762 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001763
1764 def test_build_configuration(self):
1765 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001766
1767 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001768 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1769 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1770 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1771 A.addApproval('CRVW', 2)
1772 B.addApproval('CRVW', 2)
1773 C.addApproval('CRVW', 2)
1774 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1775 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1776 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1777 self.waitUntilSettled()
1778
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001779 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001780 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001781 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001782 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001783 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001784 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001785 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001786 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1787 self.gearman_server.hold_jobs_in_queue = False
1788 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001789 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001790
Monty Taylorbc758832013-06-17 17:22:42 -04001791 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001792 repo = git.Repo(path)
1793 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1794 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001795 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001796 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001797
1798 def test_build_configuration_conflict(self):
1799 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001800
1801 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001802 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1803 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001804 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001805 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1806 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001807 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001808 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1809 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001810 A.addApproval('CRVW', 2)
1811 B.addApproval('CRVW', 2)
1812 C.addApproval('CRVW', 2)
1813 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1814 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1815 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1816 self.waitUntilSettled()
1817
James E. Blair6736beb2013-07-11 15:18:15 -07001818 self.assertEqual(A.reported, 1)
1819 self.assertEqual(B.reported, 1)
1820 self.assertEqual(C.reported, 1)
1821
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001822 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001823 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001824 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001825 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001826 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001827 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001828
1829 self.assertEqual(len(self.history), 2) # A and C merge jobs
1830
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001831 self.gearman_server.hold_jobs_in_queue = False
1832 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001833 self.waitUntilSettled()
1834
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001835 self.assertEqual(A.data['status'], 'MERGED')
1836 self.assertEqual(B.data['status'], 'NEW')
1837 self.assertEqual(C.data['status'], 'MERGED')
1838 self.assertEqual(A.reported, 2)
1839 self.assertEqual(B.reported, 2)
1840 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001841 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001842
James E. Blairdaabed22012-08-15 15:38:57 -07001843 def test_post(self):
1844 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001845
Zhongyue Luo5d556072012-09-21 02:00:47 +09001846 e = {
1847 "type": "ref-updated",
1848 "submitter": {
1849 "name": "User Name",
1850 },
1851 "refUpdate": {
1852 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1853 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1854 "refName": "master",
1855 "project": "org/project",
1856 }
1857 }
James E. Blairdaabed22012-08-15 15:38:57 -07001858 self.fake_gerrit.addEvent(e)
1859 self.waitUntilSettled()
1860
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001861 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001862 self.assertEqual(len(self.history), 1)
1863 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001864
1865 def test_build_configuration_branch(self):
1866 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001867
1868 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001869 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1870 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1871 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1872 A.addApproval('CRVW', 2)
1873 B.addApproval('CRVW', 2)
1874 C.addApproval('CRVW', 2)
1875 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1876 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1877 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1878 self.waitUntilSettled()
1879
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001880 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001881 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001882 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001883 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001884 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001885 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001886 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001887 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1888 self.gearman_server.hold_jobs_in_queue = False
1889 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001890 self.waitUntilSettled()
1891
Monty Taylorbc758832013-06-17 17:22:42 -04001892 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001893 repo = git.Repo(path)
1894 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1895 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001896 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001897 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001898
1899 def test_build_configuration_branch_interaction(self):
1900 "Test that switching between branches works"
1901 self.test_build_configuration()
1902 self.test_build_configuration_branch()
1903 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001904 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001905 repo = git.Repo(path)
1906 repo.heads.master.commit = repo.commit('init')
1907 self.test_build_configuration()
1908
1909 def test_build_configuration_multi_branch(self):
1910 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001911
1912 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001913 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1914 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1915 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1916 A.addApproval('CRVW', 2)
1917 B.addApproval('CRVW', 2)
1918 C.addApproval('CRVW', 2)
1919 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1920 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1921 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1922 self.waitUntilSettled()
1923
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001924 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001925 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001926 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001927 job_B = None
1928 for job in queue:
1929 if 'project-merge' in job.name:
1930 job_B = job
1931 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001932 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001933 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001934 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001935 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001936 for job in queue:
1937 if 'project-merge' in job.name:
1938 job_C = job
1939 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001940 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001941 self.gearman_server.hold_jobs_in_queue = False
1942 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001943 self.waitUntilSettled()
1944
Monty Taylorbc758832013-06-17 17:22:42 -04001945 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001946 repo = git.Repo(path)
1947
1948 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001949 for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001950 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001951 correct_messages = ['initial commit', 'A-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001952 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001953
1954 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001955 for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001956 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001957 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001958 self.assertEqual(repo_messages, correct_messages)
James E. Blair7f71c802012-08-22 13:04:32 -07001959
1960 def test_one_job_project(self):
1961 "Test that queueing works with one job"
1962 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1963 'master', 'A')
1964 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1965 'master', 'B')
1966 A.addApproval('CRVW', 2)
1967 B.addApproval('CRVW', 2)
1968 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1969 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1970 self.waitUntilSettled()
1971
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001972 self.assertEqual(A.data['status'], 'MERGED')
1973 self.assertEqual(A.reported, 2)
1974 self.assertEqual(B.data['status'], 'MERGED')
1975 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001976
Antoine Musso80edd5a2013-02-13 15:37:53 +01001977 def test_job_from_templates_launched(self):
1978 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001979
Antoine Musso80edd5a2013-02-13 15:37:53 +01001980 A = self.fake_gerrit.addFakeChange(
1981 'org/templated-project', 'master', 'A')
1982 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1983 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001984
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001985 self.assertEqual(self.getJobFromHistory('project-test1').result,
1986 'SUCCESS')
1987 self.assertEqual(self.getJobFromHistory('project-test2').result,
1988 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001989
James E. Blair3e98c022013-12-16 15:25:38 -08001990 def test_layered_templates(self):
1991 "Test whether a job generated via a template can be launched"
1992
1993 A = self.fake_gerrit.addFakeChange(
1994 'org/layered-project', 'master', 'A')
1995 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1996 self.waitUntilSettled()
1997
1998 self.assertEqual(self.getJobFromHistory('project-test1').result,
1999 'SUCCESS')
2000 self.assertEqual(self.getJobFromHistory('project-test2').result,
2001 'SUCCESS')
James E. Blairaea6cf62013-12-16 15:38:12 -08002002 self.assertEqual(self.getJobFromHistory('layered-project-test3'
2003 ).result, 'SUCCESS')
2004 self.assertEqual(self.getJobFromHistory('layered-project-test4'
2005 ).result, 'SUCCESS')
2006 # test5 should run twice because two templates define it
James E. Blair3e98c022013-12-16 15:25:38 -08002007 test5_count = 0
2008 for job in self.worker.build_history:
James E. Blairaea6cf62013-12-16 15:38:12 -08002009 if job.name == 'layered-project-foo-test5':
James E. Blair3e98c022013-12-16 15:25:38 -08002010 test5_count += 1
2011 self.assertEqual(job.result, 'SUCCESS')
2012 self.assertEqual(test5_count, 2)
2013 self.assertEqual(self.getJobFromHistory('project-test6').result,
2014 'SUCCESS')
2015
James E. Blaircaec0c52012-08-22 14:52:22 -07002016 def test_dependent_changes_dequeue(self):
2017 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002018
James E. Blaircaec0c52012-08-22 14:52:22 -07002019 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2020 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2021 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2022 A.addApproval('CRVW', 2)
2023 B.addApproval('CRVW', 2)
2024 C.addApproval('CRVW', 2)
2025
2026 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
2027 M1.setMerged()
2028
2029 # C -> B -> A -> M1
2030
2031 C.setDependsOn(B, 1)
2032 B.setDependsOn(A, 1)
2033 A.setDependsOn(M1, 1)
2034
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002035 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07002036
2037 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2038 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2039 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2040
2041 self.waitUntilSettled()
2042
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002043 self.assertEqual(A.data['status'], 'NEW')
2044 self.assertEqual(A.reported, 2)
2045 self.assertEqual(B.data['status'], 'NEW')
2046 self.assertEqual(B.reported, 2)
2047 self.assertEqual(C.data['status'], 'NEW')
2048 self.assertEqual(C.reported, 2)
2049 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07002050
James E. Blair972e3c72013-08-29 12:04:55 -07002051 def test_failing_dependent_changes(self):
2052 "Test that failing dependent patches are taken out of stream"
2053 self.worker.hold_jobs_in_build = True
2054 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2055 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2056 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2057 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2058 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
2059 A.addApproval('CRVW', 2)
2060 B.addApproval('CRVW', 2)
2061 C.addApproval('CRVW', 2)
2062 D.addApproval('CRVW', 2)
2063 E.addApproval('CRVW', 2)
2064
2065 # E, D -> C -> B, A
2066
2067 D.setDependsOn(C, 1)
2068 C.setDependsOn(B, 1)
2069
2070 self.worker.addFailTest('project-test1', B)
2071
2072 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2073 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2074 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2075 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2076 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2077
2078 self.waitUntilSettled()
2079 self.worker.release('.*-merge')
2080 self.waitUntilSettled()
2081 self.worker.release('.*-merge')
2082 self.waitUntilSettled()
2083 self.worker.release('.*-merge')
2084 self.waitUntilSettled()
2085 self.worker.release('.*-merge')
2086 self.waitUntilSettled()
2087 self.worker.release('.*-merge')
2088 self.waitUntilSettled()
2089
2090 self.worker.hold_jobs_in_build = False
2091 for build in self.builds:
2092 if build.parameters['ZUUL_CHANGE'] != '1':
2093 build.release()
2094 self.waitUntilSettled()
2095
2096 self.worker.release()
2097 self.waitUntilSettled()
2098
2099 self.assertEqual(A.data['status'], 'MERGED')
2100 self.assertEqual(A.reported, 2)
2101 self.assertEqual(B.data['status'], 'NEW')
2102 self.assertEqual(B.reported, 2)
2103 self.assertEqual(C.data['status'], 'NEW')
2104 self.assertEqual(C.reported, 2)
2105 self.assertEqual(D.data['status'], 'NEW')
2106 self.assertEqual(D.reported, 2)
2107 self.assertEqual(E.data['status'], 'MERGED')
2108 self.assertEqual(E.reported, 2)
2109 self.assertEqual(len(self.history), 18)
2110
James E. Blairec590122012-08-22 15:19:31 -07002111 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08002112 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07002113 # If it's dequeued more than once, we should see extra
2114 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07002115
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002116 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07002117 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2118 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2119 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2120 A.addApproval('CRVW', 2)
2121 B.addApproval('CRVW', 2)
2122 C.addApproval('CRVW', 2)
2123
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002124 self.worker.addFailTest('project1-test1', A)
2125 self.worker.addFailTest('project1-test2', A)
2126 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07002127
2128 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2129 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2130 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2131
2132 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07002133
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002134 self.assertEqual(len(self.builds), 1)
2135 self.assertEqual(self.builds[0].name, 'project1-merge')
2136 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07002137
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002138 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002139 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002140 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002141 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002142 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002143 self.waitUntilSettled()
2144
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002145 self.assertEqual(len(self.builds), 9)
2146 self.assertEqual(self.builds[0].name, 'project1-test1')
2147 self.assertEqual(self.builds[1].name, 'project1-test2')
2148 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
2149 self.assertEqual(self.builds[3].name, 'project1-test1')
2150 self.assertEqual(self.builds[4].name, 'project1-test2')
2151 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
2152 self.assertEqual(self.builds[6].name, 'project1-test1')
2153 self.assertEqual(self.builds[7].name, 'project1-test2')
2154 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07002155
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002156 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07002157 self.waitUntilSettled()
2158
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002159 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
2160 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07002161
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002162 self.worker.hold_jobs_in_build = False
2163 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07002164 self.waitUntilSettled()
2165
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002166 self.assertEqual(len(self.builds), 0)
2167 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07002168
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002169 self.assertEqual(A.data['status'], 'NEW')
2170 self.assertEqual(B.data['status'], 'MERGED')
2171 self.assertEqual(C.data['status'], 'MERGED')
2172 self.assertEqual(A.reported, 2)
2173 self.assertEqual(B.reported, 2)
2174 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07002175
2176 def test_nonvoting_job(self):
2177 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002178
James E. Blair4ec821f2012-08-23 15:28:28 -07002179 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
2180 'master', 'A')
2181 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002182 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07002183 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2184
2185 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07002186
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002187 self.assertEqual(A.data['status'], 'MERGED')
2188 self.assertEqual(A.reported, 2)
2189 self.assertEqual(
2190 self.getJobFromHistory('nonvoting-project-merge').result,
2191 'SUCCESS')
2192 self.assertEqual(
2193 self.getJobFromHistory('nonvoting-project-test1').result,
2194 'SUCCESS')
2195 self.assertEqual(
2196 self.getJobFromHistory('nonvoting-project-test2').result,
2197 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07002198
2199 def test_check_queue_success(self):
2200 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002201
James E. Blaire0487072012-08-29 17:38:31 -07002202 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2203 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2204
2205 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002206
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002207 self.assertEqual(A.data['status'], 'NEW')
2208 self.assertEqual(A.reported, 1)
2209 self.assertEqual(self.getJobFromHistory('project-merge').result,
2210 'SUCCESS')
2211 self.assertEqual(self.getJobFromHistory('project-test1').result,
2212 'SUCCESS')
2213 self.assertEqual(self.getJobFromHistory('project-test2').result,
2214 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07002215
2216 def test_check_queue_failure(self):
2217 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002218
James E. Blaire0487072012-08-29 17:38:31 -07002219 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002220 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07002221 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2222
2223 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002224
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002225 self.assertEqual(A.data['status'], 'NEW')
2226 self.assertEqual(A.reported, 1)
2227 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07002228 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002229 self.assertEqual(self.getJobFromHistory('project-test1').result,
2230 'SUCCESS')
2231 self.assertEqual(self.getJobFromHistory('project-test2').result,
2232 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07002233
2234 def test_dependent_behind_dequeue(self):
2235 "test that dependent changes behind dequeued changes work"
2236 # This complicated test is a reproduction of a real life bug
2237 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07002238
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002239 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07002240 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2241 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2242 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2243 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2244 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
2245 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
2246 D.setDependsOn(C, 1)
2247 E.setDependsOn(D, 1)
2248 A.addApproval('CRVW', 2)
2249 B.addApproval('CRVW', 2)
2250 C.addApproval('CRVW', 2)
2251 D.addApproval('CRVW', 2)
2252 E.addApproval('CRVW', 2)
2253 F.addApproval('CRVW', 2)
2254
2255 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07002256
2257 # Change object re-use in the gerrit trigger is hidden if
2258 # changes are added in quick succession; waiting makes it more
2259 # like real life.
2260 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2261 self.waitUntilSettled()
2262 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2263 self.waitUntilSettled()
2264
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002265 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002266 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002267 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002268 self.waitUntilSettled()
2269
2270 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2271 self.waitUntilSettled()
2272 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2273 self.waitUntilSettled()
2274 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2275 self.waitUntilSettled()
2276 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
2277 self.waitUntilSettled()
2278
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002279 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002280 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002281 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002282 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002283 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002284 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002285 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002286 self.waitUntilSettled()
2287
2288 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002289
2290 # Grab pointers to the jobs we want to release before
2291 # releasing any, because list indexes may change as
2292 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002293 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002294 a.release()
2295 b.release()
2296 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002297 self.waitUntilSettled()
2298
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002299 self.worker.hold_jobs_in_build = False
2300 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002301 self.waitUntilSettled()
2302
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002303 self.assertEqual(A.data['status'], 'NEW')
2304 self.assertEqual(B.data['status'], 'MERGED')
2305 self.assertEqual(C.data['status'], 'MERGED')
2306 self.assertEqual(D.data['status'], 'MERGED')
2307 self.assertEqual(E.data['status'], 'MERGED')
2308 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002309
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002310 self.assertEqual(A.reported, 2)
2311 self.assertEqual(B.reported, 2)
2312 self.assertEqual(C.reported, 2)
2313 self.assertEqual(D.reported, 2)
2314 self.assertEqual(E.reported, 2)
2315 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002316
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002317 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2318 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002319
2320 def test_merger_repack(self):
2321 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002322
James E. Blair05fed602012-09-07 12:45:24 -07002323 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2324 A.addApproval('CRVW', 2)
2325 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2326 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002327 self.assertEqual(self.getJobFromHistory('project-merge').result,
2328 'SUCCESS')
2329 self.assertEqual(self.getJobFromHistory('project-test1').result,
2330 'SUCCESS')
2331 self.assertEqual(self.getJobFromHistory('project-test2').result,
2332 'SUCCESS')
2333 self.assertEqual(A.data['status'], 'MERGED')
2334 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07002335 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002336 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002337
Monty Taylorbc758832013-06-17 17:22:42 -04002338 path = os.path.join(self.git_root, "org/project")
2339 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002340
2341 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2342 A.addApproval('CRVW', 2)
2343 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2344 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002345 self.assertEqual(self.getJobFromHistory('project-merge').result,
2346 'SUCCESS')
2347 self.assertEqual(self.getJobFromHistory('project-test1').result,
2348 'SUCCESS')
2349 self.assertEqual(self.getJobFromHistory('project-test2').result,
2350 'SUCCESS')
2351 self.assertEqual(A.data['status'], 'MERGED')
2352 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002353
James E. Blair4886f282012-11-15 09:27:33 -08002354 def test_merger_repack_large_change(self):
2355 "Test that the merger works with large changes after a repack"
2356 # https://bugs.launchpad.net/zuul/+bug/1078946
2357 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2358 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002359 path = os.path.join(self.upstream_root, "org/project1")
2360 print repack_repo(path)
2361 path = os.path.join(self.git_root, "org/project1")
2362 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002363
2364 A.addApproval('CRVW', 2)
2365 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2366 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002367 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2368 'SUCCESS')
2369 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2370 'SUCCESS')
2371 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2372 'SUCCESS')
2373 self.assertEqual(A.data['status'], 'MERGED')
2374 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002375
James E. Blair7ee88a22012-09-12 18:59:31 +02002376 def test_nonexistent_job(self):
2377 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002378 # Set to the state immediately after a restart
2379 self.resetGearmanServer()
2380 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002381
2382 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2383 A.addApproval('CRVW', 2)
2384 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2385 # There may be a thread about to report a lost change
2386 while A.reported < 2:
2387 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002388 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002389 self.assertFalse(job_names)
2390 self.assertEqual(A.data['status'], 'NEW')
2391 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002392 self.assertEmptyQueues()
2393
2394 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002395 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002396 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2397 A.addApproval('CRVW', 2)
2398 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2399 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002400 self.assertEqual(self.getJobFromHistory('project-merge').result,
2401 'SUCCESS')
2402 self.assertEqual(self.getJobFromHistory('project-test1').result,
2403 'SUCCESS')
2404 self.assertEqual(self.getJobFromHistory('project-test2').result,
2405 'SUCCESS')
2406 self.assertEqual(A.data['status'], 'MERGED')
2407 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002408
2409 def test_single_nonexistent_post_job(self):
2410 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002411 e = {
2412 "type": "ref-updated",
2413 "submitter": {
2414 "name": "User Name",
2415 },
2416 "refUpdate": {
2417 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2418 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2419 "refName": "master",
2420 "project": "org/project",
2421 }
2422 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002423 # Set to the state immediately after a restart
2424 self.resetGearmanServer()
2425 self.launcher.negative_function_cache_ttl = 0
2426
James E. Blairf62d4282012-12-31 17:01:50 -08002427 self.fake_gerrit.addEvent(e)
2428 self.waitUntilSettled()
2429
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002430 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002431
2432 def test_new_patchset_dequeues_old(self):
2433 "Test that a new patchset causes the old to be dequeued"
2434 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002435 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002436 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2437 M.setMerged()
2438
2439 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2440 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2441 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2442 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2443 A.addApproval('CRVW', 2)
2444 B.addApproval('CRVW', 2)
2445 C.addApproval('CRVW', 2)
2446 D.addApproval('CRVW', 2)
2447
2448 C.setDependsOn(B, 1)
2449 B.setDependsOn(A, 1)
2450 A.setDependsOn(M, 1)
2451
2452 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2453 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2454 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2455 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2456 self.waitUntilSettled()
2457
2458 B.addPatchset()
2459 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2460 self.waitUntilSettled()
2461
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002462 self.worker.hold_jobs_in_build = False
2463 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002464 self.waitUntilSettled()
2465
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002466 self.assertEqual(A.data['status'], 'MERGED')
2467 self.assertEqual(A.reported, 2)
2468 self.assertEqual(B.data['status'], 'NEW')
2469 self.assertEqual(B.reported, 2)
2470 self.assertEqual(C.data['status'], 'NEW')
2471 self.assertEqual(C.reported, 2)
2472 self.assertEqual(D.data['status'], 'MERGED')
2473 self.assertEqual(D.reported, 2)
2474 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002475
Arx Cruzb1b010d2013-10-28 19:49:59 -02002476 def test_zuul_url_return(self):
2477 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
2478 self.assertTrue(self.sched.config.has_option('zuul', 'zuul_url'))
2479 self.worker.hold_jobs_in_build = True
2480
2481 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2482 A.addApproval('CRVW', 2)
2483 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2484 self.waitUntilSettled()
2485
2486 self.assertEqual(len(self.builds), 1)
2487 for build in self.builds:
2488 self.assertTrue('ZUUL_URL' in build.parameters)
2489
2490 self.worker.hold_jobs_in_build = False
2491 self.worker.release()
2492 self.waitUntilSettled()
2493
James E. Blair2fa50962013-01-30 21:50:41 -08002494 def test_new_patchset_dequeues_old_on_head(self):
2495 "Test that a new patchset causes the old to be dequeued (at head)"
2496 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002497 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002498 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2499 M.setMerged()
2500 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2501 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2502 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2503 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2504 A.addApproval('CRVW', 2)
2505 B.addApproval('CRVW', 2)
2506 C.addApproval('CRVW', 2)
2507 D.addApproval('CRVW', 2)
2508
2509 C.setDependsOn(B, 1)
2510 B.setDependsOn(A, 1)
2511 A.setDependsOn(M, 1)
2512
2513 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2514 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2515 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2516 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2517 self.waitUntilSettled()
2518
2519 A.addPatchset()
2520 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2521 self.waitUntilSettled()
2522
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002523 self.worker.hold_jobs_in_build = False
2524 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002525 self.waitUntilSettled()
2526
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002527 self.assertEqual(A.data['status'], 'NEW')
2528 self.assertEqual(A.reported, 2)
2529 self.assertEqual(B.data['status'], 'NEW')
2530 self.assertEqual(B.reported, 2)
2531 self.assertEqual(C.data['status'], 'NEW')
2532 self.assertEqual(C.reported, 2)
2533 self.assertEqual(D.data['status'], 'MERGED')
2534 self.assertEqual(D.reported, 2)
2535 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002536
2537 def test_new_patchset_dequeues_old_without_dependents(self):
2538 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002539 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002540 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2541 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2542 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2543 A.addApproval('CRVW', 2)
2544 B.addApproval('CRVW', 2)
2545 C.addApproval('CRVW', 2)
2546
2547 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2548 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2549 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2550 self.waitUntilSettled()
2551
2552 B.addPatchset()
2553 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2554 self.waitUntilSettled()
2555
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002556 self.worker.hold_jobs_in_build = False
2557 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002558 self.waitUntilSettled()
2559
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002560 self.assertEqual(A.data['status'], 'MERGED')
2561 self.assertEqual(A.reported, 2)
2562 self.assertEqual(B.data['status'], 'NEW')
2563 self.assertEqual(B.reported, 2)
2564 self.assertEqual(C.data['status'], 'MERGED')
2565 self.assertEqual(C.reported, 2)
2566 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002567
2568 def test_new_patchset_dequeues_old_independent_queue(self):
2569 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002570 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002571 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2572 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2573 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2574 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2575 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2576 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2577 self.waitUntilSettled()
2578
2579 B.addPatchset()
2580 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2581 self.waitUntilSettled()
2582
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002583 self.worker.hold_jobs_in_build = False
2584 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002585 self.waitUntilSettled()
2586
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002587 self.assertEqual(A.data['status'], 'NEW')
2588 self.assertEqual(A.reported, 1)
2589 self.assertEqual(B.data['status'], 'NEW')
2590 self.assertEqual(B.reported, 1)
2591 self.assertEqual(C.data['status'], 'NEW')
2592 self.assertEqual(C.reported, 1)
2593 self.assertEqual(len(self.history), 10)
2594 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002595
2596 def test_zuul_refs(self):
2597 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002598 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002599 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2600 M1.setMerged()
2601 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2602 M2.setMerged()
2603
2604 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2605 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2606 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2607 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2608 A.addApproval('CRVW', 2)
2609 B.addApproval('CRVW', 2)
2610 C.addApproval('CRVW', 2)
2611 D.addApproval('CRVW', 2)
2612 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2613 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2614 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2615 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2616
2617 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002618 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002619 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002620 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002621 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002622 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002623 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002624 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002625 self.waitUntilSettled()
2626
James E. Blair7d0dedc2013-02-21 17:26:09 -08002627 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002628 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002629 if x.parameters['ZUUL_CHANGE'] == '3':
2630 a_zref = x.parameters['ZUUL_REF']
2631 if x.parameters['ZUUL_CHANGE'] == '4':
2632 b_zref = x.parameters['ZUUL_REF']
2633 if x.parameters['ZUUL_CHANGE'] == '5':
2634 c_zref = x.parameters['ZUUL_REF']
2635 if x.parameters['ZUUL_CHANGE'] == '6':
2636 d_zref = x.parameters['ZUUL_REF']
2637
2638 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002639 self.assertIsNotNone(a_zref)
2640 self.assertIsNotNone(b_zref)
2641 self.assertIsNotNone(c_zref)
2642 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002643
2644 # And they should all be different
2645 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002646 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002647
2648 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002649 self.assertTrue(self.ref_has_change(a_zref, A))
2650 self.assertFalse(self.ref_has_change(a_zref, B))
2651 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002652
2653 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002654 self.assertTrue(self.ref_has_change(b_zref, A))
2655 self.assertTrue(self.ref_has_change(b_zref, B))
2656 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002657
2658 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002659 self.assertTrue(self.ref_has_change(c_zref, A))
2660 self.assertTrue(self.ref_has_change(c_zref, B))
2661 self.assertTrue(self.ref_has_change(c_zref, C))
2662 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002663
2664 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002665 self.assertTrue(self.ref_has_change(d_zref, A))
2666 self.assertTrue(self.ref_has_change(d_zref, B))
2667 self.assertTrue(self.ref_has_change(d_zref, C))
2668 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002669
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002670 self.worker.hold_jobs_in_build = False
2671 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002672 self.waitUntilSettled()
2673
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002674 self.assertEqual(A.data['status'], 'MERGED')
2675 self.assertEqual(A.reported, 2)
2676 self.assertEqual(B.data['status'], 'MERGED')
2677 self.assertEqual(B.reported, 2)
2678 self.assertEqual(C.data['status'], 'MERGED')
2679 self.assertEqual(C.reported, 2)
2680 self.assertEqual(D.data['status'], 'MERGED')
2681 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002682
James E. Blairc053d022014-01-22 14:57:33 -08002683 def test_required_approval_check_and_gate(self):
2684 "Test required-approval triggers both check and gate"
2685 self.config.set('zuul', 'layout_config',
2686 'tests/fixtures/layout-require-approval.yaml')
2687 self.sched.reconfigure(self.config)
2688 self.registerJobs()
2689
2690 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2691 A.addApproval('CRVW', 2)
2692 # Add a too-old +1
2693 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2694
2695 aprv = A.addApproval('APRV', 1)
2696 self.fake_gerrit.addEvent(aprv)
2697 self.waitUntilSettled()
2698 # Should have run a check job
2699 self.assertEqual(len(self.history), 1)
2700 self.assertEqual(self.history[0].name, 'project-check')
2701
2702 # Report the result of that check job (overrides previous vrfy)
2703 # Skynet alert: this should trigger a gate job now that
2704 # all reqs are met
2705 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2706 self.waitUntilSettled()
2707 self.assertEqual(len(self.history), 2)
2708 self.assertEqual(self.history[1].name, 'project-gate')
2709
2710 def test_required_approval_newer(self):
2711 "Test required-approval newer trigger parameter"
2712 self.config.set('zuul', 'layout_config',
2713 'tests/fixtures/layout-require-approval.yaml')
2714 self.sched.reconfigure(self.config)
2715 self.registerJobs()
2716
2717 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2718 A.addApproval('CRVW', 2)
2719 aprv = A.addApproval('APRV', 1)
2720 self.fake_gerrit.addEvent(aprv)
2721 self.waitUntilSettled()
2722 # No +1 from Jenkins so should not be enqueued
2723 self.assertEqual(len(self.history), 0)
2724
2725 # Add a too-old +1, should trigger check but not gate
2726 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2727 self.fake_gerrit.addEvent(aprv)
2728 self.waitUntilSettled()
2729 self.assertEqual(len(self.history), 1)
2730 self.assertEqual(self.history[0].name, 'project-check')
2731
2732 # Add a recent +1
2733 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2734 self.fake_gerrit.addEvent(aprv)
2735 self.waitUntilSettled()
2736 self.assertEqual(len(self.history), 2)
2737 self.assertEqual(self.history[1].name, 'project-gate')
2738
2739 def test_required_approval_older(self):
2740 "Test required-approval older trigger parameter"
2741 self.config.set('zuul', 'layout_config',
2742 'tests/fixtures/layout-require-approval.yaml')
2743 self.sched.reconfigure(self.config)
2744 self.registerJobs()
2745
2746 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2747 crvw = A.addApproval('CRVW', 2)
2748 self.fake_gerrit.addEvent(crvw)
2749 self.waitUntilSettled()
2750 # No +1 from Jenkins so should not be enqueued
2751 self.assertEqual(len(self.history), 0)
2752
2753 # Add an old +1 and trigger check with a comment
2754 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2755 self.fake_gerrit.addEvent(crvw)
2756 self.waitUntilSettled()
2757 self.assertEqual(len(self.history), 1)
2758 self.assertEqual(self.history[0].name, 'project-check')
2759
2760 # Add a recent +1 and make sure nothing changes
2761 A.addApproval('VRFY', 1)
2762 self.fake_gerrit.addEvent(crvw)
2763 self.waitUntilSettled()
2764 self.assertEqual(len(self.history), 1)
2765
2766 # The last thing we did was query a change then do nothing
2767 # with a pipeline, so it will be in the cache; clean it up so
2768 # it does not fail the test.
2769 for pipeline in self.sched.layout.pipelines.values():
2770 pipeline.trigger.maintainCache([])
2771
James E. Blair4a28a882013-08-23 15:17:33 -07002772 def test_rerun_on_error(self):
2773 "Test that if a worker fails to run a job, it is run again"
2774 self.worker.hold_jobs_in_build = True
2775 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2776 A.addApproval('CRVW', 2)
2777 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2778 self.waitUntilSettled()
2779
2780 self.builds[0].run_error = True
2781 self.worker.hold_jobs_in_build = False
2782 self.worker.release()
2783 self.waitUntilSettled()
2784 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2785 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2786
James E. Blair412e5582013-04-22 15:50:12 -07002787 def test_statsd(self):
2788 "Test each of the statsd methods used in the scheduler"
2789 import extras
2790 statsd = extras.try_import('statsd.statsd')
2791 statsd.incr('test-incr')
2792 statsd.timing('test-timing', 3)
2793 statsd.gauge('test-guage', 12)
2794 self.assertReportedStat('test-incr', '1|c')
2795 self.assertReportedStat('test-timing', '3|ms')
2796 self.assertReportedStat('test-guage', '12|g')
2797
James E. Blair70c71582013-03-06 08:50:50 -08002798 def test_file_jobs(self):
2799 "Test that file jobs run only when appropriate"
2800 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2801 A.addPatchset(['pip-requires'])
2802 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2803 A.addApproval('CRVW', 2)
2804 B.addApproval('CRVW', 2)
2805 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2806 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2807 self.waitUntilSettled()
2808
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002809 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002810 if x.name == 'project-testfile']
2811
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002812 self.assertEqual(len(testfile_jobs), 1)
2813 self.assertEqual(testfile_jobs[0].changes, '1,2')
2814 self.assertEqual(A.data['status'], 'MERGED')
2815 self.assertEqual(A.reported, 2)
2816 self.assertEqual(B.data['status'], 'MERGED')
2817 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002818
2819 def test_test_config(self):
2820 "Test that we can test the config"
2821 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002822 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002823 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002824 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002825
2826 def test_build_description(self):
2827 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002828 self.worker.registerFunction('set_description:' +
2829 self.worker.worker_id)
2830
2831 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2832 A.addApproval('CRVW', 2)
2833 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2834 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002835 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002836 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002837 self.assertTrue(re.search("Branch.*master", desc))
2838 self.assertTrue(re.search("Pipeline.*gate", desc))
2839 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2840 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2841 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2842 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002843
James E. Blair64ed6f22013-07-10 14:07:23 -07002844 def test_queue_precedence(self):
2845 "Test that queue precedence works"
2846
2847 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002848 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002849 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2850 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2851 A.addApproval('CRVW', 2)
2852 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2853
2854 self.waitUntilSettled()
2855 self.gearman_server.hold_jobs_in_queue = False
2856 self.gearman_server.release()
2857 self.waitUntilSettled()
2858
James E. Blair8de58bd2013-07-18 16:23:33 -07002859 # Run one build at a time to ensure non-race order:
2860 for x in range(6):
2861 self.release(self.builds[0])
2862 self.waitUntilSettled()
2863 self.worker.hold_jobs_in_build = False
2864 self.waitUntilSettled()
2865
James E. Blair64ed6f22013-07-10 14:07:23 -07002866 self.log.debug(self.history)
2867 self.assertEqual(self.history[0].pipeline, 'gate')
2868 self.assertEqual(self.history[1].pipeline, 'check')
2869 self.assertEqual(self.history[2].pipeline, 'gate')
2870 self.assertEqual(self.history[3].pipeline, 'gate')
2871 self.assertEqual(self.history[4].pipeline, 'check')
2872 self.assertEqual(self.history[5].pipeline, 'check')
2873
James E. Blair1843a552013-07-03 14:19:52 -07002874 def test_json_status(self):
2875 "Test that we can retrieve JSON status info"
2876 self.worker.hold_jobs_in_build = True
2877 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2878 A.addApproval('CRVW', 2)
2879 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2880 self.waitUntilSettled()
2881
2882 port = self.webapp.server.socket.getsockname()[1]
2883
2884 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2885 data = f.read()
2886
2887 self.worker.hold_jobs_in_build = False
2888 self.worker.release()
2889 self.waitUntilSettled()
2890
2891 data = json.loads(data)
2892 status_jobs = set()
2893 for p in data['pipelines']:
2894 for q in p['change_queues']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002895 if q['dependent']:
2896 self.assertEqual(q['window'], 20)
2897 else:
2898 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002899 for head in q['heads']:
2900 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002901 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002902 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002903 for job in change['jobs']:
2904 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002905 self.assertIn('project-merge', status_jobs)
2906 self.assertIn('project-test1', status_jobs)
2907 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002908
James E. Blairc3d428e2013-12-03 15:06:48 -08002909 def test_merging_queues(self):
2910 "Test that transitively-connected change queues are merged"
2911 self.config.set('zuul', 'layout_config',
2912 'tests/fixtures/layout-merge-queues.yaml')
2913 self.sched.reconfigure(self.config)
2914 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
2915
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002916 def test_node_label(self):
2917 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002918 self.worker.registerFunction('build:node-project-test1:debian')
2919
2920 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2921 A.addApproval('CRVW', 2)
2922 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2923 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002924
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002925 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2926 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2927 'debian')
2928 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002929
2930 def test_live_reconfiguration(self):
2931 "Test that live reconfiguration works"
2932 self.worker.hold_jobs_in_build = True
2933 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2934 A.addApproval('CRVW', 2)
2935 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2936 self.waitUntilSettled()
2937
2938 self.sched.reconfigure(self.config)
2939
2940 self.worker.hold_jobs_in_build = False
2941 self.worker.release()
2942 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002943 self.assertEqual(self.getJobFromHistory('project-merge').result,
2944 'SUCCESS')
2945 self.assertEqual(self.getJobFromHistory('project-test1').result,
2946 'SUCCESS')
2947 self.assertEqual(self.getJobFromHistory('project-test2').result,
2948 'SUCCESS')
2949 self.assertEqual(A.data['status'], 'MERGED')
2950 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002951
James E. Blaire712d9f2013-07-31 11:40:11 -07002952 def test_live_reconfiguration_functions(self):
2953 "Test live reconfiguration with a custom function"
2954 self.worker.registerFunction('build:node-project-test1:debian')
2955 self.worker.registerFunction('build:node-project-test1:wheezy')
2956 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2957 A.addApproval('CRVW', 2)
2958 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2959 self.waitUntilSettled()
2960
2961 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2962 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2963 'debian')
2964 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2965
2966 self.config.set('zuul', 'layout_config',
2967 'tests/fixtures/layout-live-'
2968 'reconfiguration-functions.yaml')
2969 self.sched.reconfigure(self.config)
2970 self.worker.build_history = []
2971
2972 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
2973 B.addApproval('CRVW', 2)
2974 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2975 self.waitUntilSettled()
2976
2977 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2978 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2979 'wheezy')
2980 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2981
James E. Blair287c06d2013-07-24 10:39:30 -07002982 def test_delayed_repo_init(self):
2983 self.config.set('zuul', 'layout_config',
2984 'tests/fixtures/layout-delayed-repo-init.yaml')
2985 self.sched.reconfigure(self.config)
2986
2987 self.init_repo("org/new-project")
2988 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2989
2990 A.addApproval('CRVW', 2)
2991 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2992 self.waitUntilSettled()
2993 self.assertEqual(self.getJobFromHistory('project-merge').result,
2994 'SUCCESS')
2995 self.assertEqual(self.getJobFromHistory('project-test1').result,
2996 'SUCCESS')
2997 self.assertEqual(self.getJobFromHistory('project-test2').result,
2998 'SUCCESS')
2999 self.assertEqual(A.data['status'], 'MERGED')
3000 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003001
Clark Boylan6dbbc482013-10-18 10:57:31 -07003002 def test_repo_deleted(self):
3003 self.config.set('zuul', 'layout_config',
3004 'tests/fixtures/layout-repo-deleted.yaml')
3005 self.sched.reconfigure(self.config)
3006
3007 self.init_repo("org/delete-project")
3008 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
3009
3010 A.addApproval('CRVW', 2)
3011 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3012 self.waitUntilSettled()
3013 self.assertEqual(self.getJobFromHistory('project-merge').result,
3014 'SUCCESS')
3015 self.assertEqual(self.getJobFromHistory('project-test1').result,
3016 'SUCCESS')
3017 self.assertEqual(self.getJobFromHistory('project-test2').result,
3018 'SUCCESS')
3019 self.assertEqual(A.data['status'], 'MERGED')
3020 self.assertEqual(A.reported, 2)
3021
3022 # Delete org/new-project zuul repo. Should be recloned.
3023 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
3024
3025 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
3026
3027 B.addApproval('CRVW', 2)
3028 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3029 self.waitUntilSettled()
3030 self.assertEqual(self.getJobFromHistory('project-merge').result,
3031 'SUCCESS')
3032 self.assertEqual(self.getJobFromHistory('project-test1').result,
3033 'SUCCESS')
3034 self.assertEqual(self.getJobFromHistory('project-test2').result,
3035 'SUCCESS')
3036 self.assertEqual(B.data['status'], 'MERGED')
3037 self.assertEqual(B.reported, 2)
3038
James E. Blair87650fa2014-01-08 11:43:23 +08003039 def test_push_urls(self):
3040 "Test that Zuul can push refs to multiple URLs"
3041 upstream_path = os.path.join(self.upstream_root, 'org/project')
3042 replica1 = os.path.join(self.upstream_root, 'replica1')
3043 replica2 = os.path.join(self.upstream_root, 'replica2')
3044
3045 self.config.add_section('replication')
3046 self.config.set('replication', 'url1', 'file://%s' % replica1)
3047 self.config.set('replication', 'url2', 'file://%s' % replica2)
3048 self.sched.reconfigure(self.config)
3049
3050 r1 = git.Repo.clone_from(upstream_path, replica1 + '/org/project.git')
3051 r2 = git.Repo.clone_from(upstream_path, replica2 + '/org/project.git')
3052
3053 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3054 A.addApproval('CRVW', 2)
3055 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3056 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
3057 B.addApproval('CRVW', 2)
3058 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3059 self.waitUntilSettled()
3060 count = 0
3061 for ref in r1.refs:
3062 if ref.path.startswith('refs/zuul'):
3063 count += 1
3064 self.assertEqual(count, 3)
3065
3066 count = 0
3067 for ref in r2.refs:
3068 if ref.path.startswith('refs/zuul'):
3069 count += 1
3070 self.assertEqual(count, 3)
3071
James E. Blair63bb0ef2013-07-29 17:14:51 -07003072 def test_timer(self):
3073 "Test that a periodic job is triggered"
3074 self.worker.hold_jobs_in_build = True
3075 self.config.set('zuul', 'layout_config',
3076 'tests/fixtures/layout-timer.yaml')
3077 self.sched.reconfigure(self.config)
3078 self.registerJobs()
3079
3080 start = time.time()
3081 failed = True
3082 while ((time.time() - start) < 30):
3083 if len(self.builds) == 2:
3084 failed = False
3085 break
3086 else:
3087 time.sleep(1)
3088
3089 if failed:
3090 raise Exception("Expected jobs never ran")
3091
3092 self.waitUntilSettled()
3093 port = self.webapp.server.socket.getsockname()[1]
3094
3095 f = urllib.urlopen("http://localhost:%s/status.json" % port)
3096 data = f.read()
3097
3098 self.worker.hold_jobs_in_build = False
3099 self.worker.release()
3100 self.waitUntilSettled()
3101
3102 self.assertEqual(self.getJobFromHistory(
3103 'project-bitrot-stable-old').result, 'SUCCESS')
3104 self.assertEqual(self.getJobFromHistory(
3105 'project-bitrot-stable-older').result, 'SUCCESS')
3106
3107 data = json.loads(data)
3108 status_jobs = set()
3109 for p in data['pipelines']:
3110 for q in p['change_queues']:
3111 for head in q['heads']:
3112 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07003113 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003114 for job in change['jobs']:
3115 status_jobs.add(job['name'])
3116 self.assertIn('project-bitrot-stable-old', status_jobs)
3117 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003118
3119 def test_check_smtp_pool(self):
3120 self.config.set('zuul', 'layout_config',
3121 'tests/fixtures/layout-smtp.yaml')
3122 self.sched.reconfigure(self.config)
3123
3124 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3125 self.waitUntilSettled()
3126
3127 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3128 self.waitUntilSettled()
3129
James E. Blairff80a2f2013-12-27 13:24:06 -08003130 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003131
3132 # A.messages only holds what FakeGerrit places in it. Thus we
3133 # work on the knowledge of what the first message should be as
3134 # it is only configured to go to SMTP.
3135
3136 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003137 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003138 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003139 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003140 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003141 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003142
3143 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003144 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003145 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003146 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003147 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003148 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003149
James E. Blaire5910202013-12-27 09:50:31 -08003150 def test_timer_smtp(self):
3151 "Test that a periodic job is triggered"
3152 self.config.set('zuul', 'layout_config',
3153 'tests/fixtures/layout-timer-smtp.yaml')
3154 self.sched.reconfigure(self.config)
3155 self.registerJobs()
3156
3157 start = time.time()
3158 failed = True
3159 while ((time.time() - start) < 30):
3160 if len(self.history) == 2:
3161 failed = False
3162 break
3163 else:
3164 time.sleep(1)
3165
3166 if failed:
3167 raise Exception("Expected jobs never ran")
3168
3169 self.waitUntilSettled()
3170
3171 self.assertEqual(self.getJobFromHistory(
3172 'project-bitrot-stable-old').result, 'SUCCESS')
3173 self.assertEqual(self.getJobFromHistory(
3174 'project-bitrot-stable-older').result, 'SUCCESS')
3175
James E. Blairff80a2f2013-12-27 13:24:06 -08003176 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003177
3178 # A.messages only holds what FakeGerrit places in it. Thus we
3179 # work on the knowledge of what the first message should be as
3180 # it is only configured to go to SMTP.
3181
3182 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003183 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003184 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003185 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003186 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003187 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003188
James E. Blairad28e912013-11-27 10:43:22 -08003189 def test_client_enqueue(self):
3190 "Test that the RPC client can enqueue a change"
3191 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3192 A.addApproval('CRVW', 2)
3193 A.addApproval('APRV', 1)
3194
3195 client = zuul.rpcclient.RPCClient('127.0.0.1',
3196 self.gearman_server.port)
3197 r = client.enqueue(pipeline='gate',
3198 project='org/project',
3199 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003200 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003201 self.waitUntilSettled()
3202 self.assertEqual(self.getJobFromHistory('project-merge').result,
3203 'SUCCESS')
3204 self.assertEqual(self.getJobFromHistory('project-test1').result,
3205 'SUCCESS')
3206 self.assertEqual(self.getJobFromHistory('project-test2').result,
3207 'SUCCESS')
3208 self.assertEqual(A.data['status'], 'MERGED')
3209 self.assertEqual(A.reported, 2)
3210 self.assertEqual(r, True)
3211
3212 def test_client_enqueue_negative(self):
3213 "Test that the RPC client returns errors"
3214 client = zuul.rpcclient.RPCClient('127.0.0.1',
3215 self.gearman_server.port)
3216 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3217 "Invalid project"):
3218 r = client.enqueue(pipeline='gate',
3219 project='project-does-not-exist',
3220 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003221 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003222 client.shutdown()
3223 self.assertEqual(r, False)
3224
3225 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3226 "Invalid pipeline"):
3227 r = client.enqueue(pipeline='pipeline-does-not-exist',
3228 project='org/project',
3229 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003230 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003231 client.shutdown()
3232 self.assertEqual(r, False)
3233
3234 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3235 "Invalid trigger"):
3236 r = client.enqueue(pipeline='gate',
3237 project='org/project',
3238 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003239 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003240 client.shutdown()
3241 self.assertEqual(r, False)
3242
3243 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3244 "Invalid change"):
3245 r = client.enqueue(pipeline='gate',
3246 project='org/project',
3247 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003248 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003249 client.shutdown()
3250 self.assertEqual(r, False)
3251
3252 self.waitUntilSettled()
3253 self.assertEqual(len(self.history), 0)
3254 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003255
3256 def test_client_promote(self):
3257 "Test that the RPC client can promote a change"
3258 self.worker.hold_jobs_in_build = True
3259 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3260 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3261 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3262 A.addApproval('CRVW', 2)
3263 B.addApproval('CRVW', 2)
3264 C.addApproval('CRVW', 2)
3265
3266 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3267 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3268 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3269
3270 self.waitUntilSettled()
3271
Sean Daguef39b9ca2014-01-10 21:34:35 -05003272 items = self.sched.layout.pipelines['gate'].getAllItems()
3273 enqueue_times = {}
3274 for item in items:
3275 enqueue_times[str(item.change)] = item.enqueue_time
3276
James E. Blair36658cf2013-12-06 17:53:48 -08003277 client = zuul.rpcclient.RPCClient('127.0.0.1',
3278 self.gearman_server.port)
3279 r = client.promote(pipeline='gate',
3280 change_ids=['2,1', '3,1'])
3281
Sean Daguef39b9ca2014-01-10 21:34:35 -05003282 # ensure that enqueue times are durable
3283 items = self.sched.layout.pipelines['gate'].getAllItems()
3284 for item in items:
3285 self.assertEqual(
3286 enqueue_times[str(item.change)], item.enqueue_time)
3287
James E. Blair36658cf2013-12-06 17:53:48 -08003288 self.worker.release('.*-merge')
3289 self.waitUntilSettled()
3290 self.worker.release('.*-merge')
3291 self.waitUntilSettled()
3292 self.worker.release('.*-merge')
3293 self.waitUntilSettled()
3294
3295 self.assertEqual(len(self.builds), 6)
3296 self.assertEqual(self.builds[0].name, 'project-test1')
3297 self.assertEqual(self.builds[1].name, 'project-test2')
3298 self.assertEqual(self.builds[2].name, 'project-test1')
3299 self.assertEqual(self.builds[3].name, 'project-test2')
3300 self.assertEqual(self.builds[4].name, 'project-test1')
3301 self.assertEqual(self.builds[5].name, 'project-test2')
3302
3303 self.assertTrue(self.job_has_changes(self.builds[0], B))
3304 self.assertFalse(self.job_has_changes(self.builds[0], A))
3305 self.assertFalse(self.job_has_changes(self.builds[0], C))
3306
3307 self.assertTrue(self.job_has_changes(self.builds[2], B))
3308 self.assertTrue(self.job_has_changes(self.builds[2], C))
3309 self.assertFalse(self.job_has_changes(self.builds[2], A))
3310
3311 self.assertTrue(self.job_has_changes(self.builds[4], B))
3312 self.assertTrue(self.job_has_changes(self.builds[4], C))
3313 self.assertTrue(self.job_has_changes(self.builds[4], A))
3314
3315 self.worker.release()
3316 self.waitUntilSettled()
3317
3318 self.assertEqual(A.data['status'], 'MERGED')
3319 self.assertEqual(A.reported, 2)
3320 self.assertEqual(B.data['status'], 'MERGED')
3321 self.assertEqual(B.reported, 2)
3322 self.assertEqual(C.data['status'], 'MERGED')
3323 self.assertEqual(C.reported, 2)
3324
3325 client.shutdown()
3326 self.assertEqual(r, True)
3327
3328 def test_client_promote_dependent(self):
3329 "Test that the RPC client can promote a dependent change"
3330 # C (depends on B) -> B -> A ; then promote C to get:
3331 # A -> C (depends on B) -> B
3332 self.worker.hold_jobs_in_build = True
3333 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3334 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3335 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3336
3337 C.setDependsOn(B, 1)
3338
3339 A.addApproval('CRVW', 2)
3340 B.addApproval('CRVW', 2)
3341 C.addApproval('CRVW', 2)
3342
3343 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3344 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3345 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3346
3347 self.waitUntilSettled()
3348
3349 client = zuul.rpcclient.RPCClient('127.0.0.1',
3350 self.gearman_server.port)
3351 r = client.promote(pipeline='gate',
3352 change_ids=['3,1'])
3353
3354 self.worker.release('.*-merge')
3355 self.waitUntilSettled()
3356 self.worker.release('.*-merge')
3357 self.waitUntilSettled()
3358 self.worker.release('.*-merge')
3359 self.waitUntilSettled()
3360
3361 self.assertEqual(len(self.builds), 6)
3362 self.assertEqual(self.builds[0].name, 'project-test1')
3363 self.assertEqual(self.builds[1].name, 'project-test2')
3364 self.assertEqual(self.builds[2].name, 'project-test1')
3365 self.assertEqual(self.builds[3].name, 'project-test2')
3366 self.assertEqual(self.builds[4].name, 'project-test1')
3367 self.assertEqual(self.builds[5].name, 'project-test2')
3368
3369 self.assertTrue(self.job_has_changes(self.builds[0], B))
3370 self.assertFalse(self.job_has_changes(self.builds[0], A))
3371 self.assertFalse(self.job_has_changes(self.builds[0], C))
3372
3373 self.assertTrue(self.job_has_changes(self.builds[2], B))
3374 self.assertTrue(self.job_has_changes(self.builds[2], C))
3375 self.assertFalse(self.job_has_changes(self.builds[2], A))
3376
3377 self.assertTrue(self.job_has_changes(self.builds[4], B))
3378 self.assertTrue(self.job_has_changes(self.builds[4], C))
3379 self.assertTrue(self.job_has_changes(self.builds[4], A))
3380
3381 self.worker.release()
3382 self.waitUntilSettled()
3383
3384 self.assertEqual(A.data['status'], 'MERGED')
3385 self.assertEqual(A.reported, 2)
3386 self.assertEqual(B.data['status'], 'MERGED')
3387 self.assertEqual(B.reported, 2)
3388 self.assertEqual(C.data['status'], 'MERGED')
3389 self.assertEqual(C.reported, 2)
3390
3391 client.shutdown()
3392 self.assertEqual(r, True)
3393
3394 def test_client_promote_negative(self):
3395 "Test that the RPC client returns errors for promotion"
3396 self.worker.hold_jobs_in_build = True
3397 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3398 A.addApproval('CRVW', 2)
3399 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3400 self.waitUntilSettled()
3401
3402 client = zuul.rpcclient.RPCClient('127.0.0.1',
3403 self.gearman_server.port)
3404
3405 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3406 r = client.promote(pipeline='nonexistent',
3407 change_ids=['2,1', '3,1'])
3408 client.shutdown()
3409 self.assertEqual(r, False)
3410
3411 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3412 r = client.promote(pipeline='gate',
3413 change_ids=['4,1'])
3414 client.shutdown()
3415 self.assertEqual(r, False)
3416
3417 self.worker.hold_jobs_in_build = False
3418 self.worker.release()
3419 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003420
3421 def test_queue_rate_limiting(self):
3422 "Test that DependentPipelines are rate limited with dep across window"
3423 self.config.set('zuul', 'layout_config',
3424 'tests/fixtures/layout-rate-limit.yaml')
3425 self.sched.reconfigure(self.config)
3426 self.worker.hold_jobs_in_build = True
3427 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3428 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3429 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3430
3431 C.setDependsOn(B, 1)
3432 self.worker.addFailTest('project-test1', A)
3433
3434 A.addApproval('CRVW', 2)
3435 B.addApproval('CRVW', 2)
3436 C.addApproval('CRVW', 2)
3437
3438 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3439 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3440 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3441 self.waitUntilSettled()
3442
3443 # Only A and B will have their merge jobs queued because
3444 # window is 2.
3445 self.assertEqual(len(self.builds), 2)
3446 self.assertEqual(self.builds[0].name, 'project-merge')
3447 self.assertEqual(self.builds[1].name, 'project-merge')
3448
3449 self.worker.release('.*-merge')
3450 self.waitUntilSettled()
3451 self.worker.release('.*-merge')
3452 self.waitUntilSettled()
3453
3454 # Only A and B will have their test jobs queued because
3455 # window is 2.
3456 self.assertEqual(len(self.builds), 4)
3457 self.assertEqual(self.builds[0].name, 'project-test1')
3458 self.assertEqual(self.builds[1].name, 'project-test2')
3459 self.assertEqual(self.builds[2].name, 'project-test1')
3460 self.assertEqual(self.builds[3].name, 'project-test2')
3461
3462 self.worker.release('project-.*')
3463 self.waitUntilSettled()
3464
3465 queue = self.sched.layout.pipelines['gate'].queues[0]
3466 # A failed so window is reduced by 1 to 1.
3467 self.assertEqual(queue.window, 1)
3468 self.assertEqual(queue.window_floor, 1)
3469 self.assertEqual(A.data['status'], 'NEW')
3470
3471 # Gate is reset and only B's merge job is queued because
3472 # window shrunk to 1.
3473 self.assertEqual(len(self.builds), 1)
3474 self.assertEqual(self.builds[0].name, 'project-merge')
3475
3476 self.worker.release('.*-merge')
3477 self.waitUntilSettled()
3478
3479 # Only B's test jobs are queued because window is still 1.
3480 self.assertEqual(len(self.builds), 2)
3481 self.assertEqual(self.builds[0].name, 'project-test1')
3482 self.assertEqual(self.builds[1].name, 'project-test2')
3483
3484 self.worker.release('project-.*')
3485 self.waitUntilSettled()
3486
3487 # B was successfully merged so window is increased to 2.
3488 self.assertEqual(queue.window, 2)
3489 self.assertEqual(queue.window_floor, 1)
3490 self.assertEqual(B.data['status'], 'MERGED')
3491
3492 # Only C is left and its merge job is queued.
3493 self.assertEqual(len(self.builds), 1)
3494 self.assertEqual(self.builds[0].name, 'project-merge')
3495
3496 self.worker.release('.*-merge')
3497 self.waitUntilSettled()
3498
3499 # After successful merge job the test jobs for C are queued.
3500 self.assertEqual(len(self.builds), 2)
3501 self.assertEqual(self.builds[0].name, 'project-test1')
3502 self.assertEqual(self.builds[1].name, 'project-test2')
3503
3504 self.worker.release('project-.*')
3505 self.waitUntilSettled()
3506
3507 # C successfully merged so window is bumped to 3.
3508 self.assertEqual(queue.window, 3)
3509 self.assertEqual(queue.window_floor, 1)
3510 self.assertEqual(C.data['status'], 'MERGED')
3511
3512 def test_queue_rate_limiting_dependent(self):
3513 "Test that DependentPipelines are rate limited with dep in window"
3514 self.config.set('zuul', 'layout_config',
3515 'tests/fixtures/layout-rate-limit.yaml')
3516 self.sched.reconfigure(self.config)
3517 self.worker.hold_jobs_in_build = True
3518 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3519 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3520 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3521
3522 B.setDependsOn(A, 1)
3523
3524 self.worker.addFailTest('project-test1', A)
3525
3526 A.addApproval('CRVW', 2)
3527 B.addApproval('CRVW', 2)
3528 C.addApproval('CRVW', 2)
3529
3530 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3531 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3532 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3533 self.waitUntilSettled()
3534
3535 # Only A and B will have their merge jobs queued because
3536 # window is 2.
3537 self.assertEqual(len(self.builds), 2)
3538 self.assertEqual(self.builds[0].name, 'project-merge')
3539 self.assertEqual(self.builds[1].name, 'project-merge')
3540
3541 self.worker.release('.*-merge')
3542 self.waitUntilSettled()
3543 self.worker.release('.*-merge')
3544 self.waitUntilSettled()
3545
3546 # Only A and B will have their test jobs queued because
3547 # window is 2.
3548 self.assertEqual(len(self.builds), 4)
3549 self.assertEqual(self.builds[0].name, 'project-test1')
3550 self.assertEqual(self.builds[1].name, 'project-test2')
3551 self.assertEqual(self.builds[2].name, 'project-test1')
3552 self.assertEqual(self.builds[3].name, 'project-test2')
3553
3554 self.worker.release('project-.*')
3555 self.waitUntilSettled()
3556
3557 queue = self.sched.layout.pipelines['gate'].queues[0]
3558 # A failed so window is reduced by 1 to 1.
3559 self.assertEqual(queue.window, 1)
3560 self.assertEqual(queue.window_floor, 1)
3561 self.assertEqual(A.data['status'], 'NEW')
3562 self.assertEqual(B.data['status'], 'NEW')
3563
3564 # Gate is reset and only C's merge job is queued because
3565 # window shrunk to 1 and A and B were dequeued.
3566 self.assertEqual(len(self.builds), 1)
3567 self.assertEqual(self.builds[0].name, 'project-merge')
3568
3569 self.worker.release('.*-merge')
3570 self.waitUntilSettled()
3571
3572 # Only C's test jobs are queued because window is still 1.
3573 self.assertEqual(len(self.builds), 2)
3574 self.assertEqual(self.builds[0].name, 'project-test1')
3575 self.assertEqual(self.builds[1].name, 'project-test2')
3576
3577 self.worker.release('project-.*')
3578 self.waitUntilSettled()
3579
3580 # C was successfully merged so window is increased to 2.
3581 self.assertEqual(queue.window, 2)
3582 self.assertEqual(queue.window_floor, 1)
3583 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003584
3585 def test_worker_update_metadata(self):
3586 "Test if a worker can send back metadata about itself"
3587 self.worker.hold_jobs_in_build = True
3588
3589 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3590 A.addApproval('CRVW', 2)
3591 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3592 self.waitUntilSettled()
3593
3594 self.assertEqual(len(self.launcher.builds), 1)
3595
3596 self.log.debug('Current builds:')
3597 self.log.debug(self.launcher.builds)
3598
3599 start = time.time()
3600 while True:
3601 if time.time() - start > 10:
3602 raise Exception("Timeout waiting for gearman server to report "
3603 + "back to the client")
3604 build = self.launcher.builds.values()[0]
3605 if build.worker.name == "My Worker":
3606 break
3607 else:
3608 time.sleep(0)
3609
3610 self.log.debug(build)
3611 self.assertEqual("My Worker", build.worker.name)
3612 self.assertEqual("localhost", build.worker.hostname)
3613 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3614 self.assertEqual("zuul.example.org", build.worker.fqdn)
3615 self.assertEqual("FakeBuilder", build.worker.program)
3616 self.assertEqual("v1.1", build.worker.version)
3617 self.assertEqual({'something': 'else'}, build.worker.extra)
3618
3619 self.worker.hold_jobs_in_build = False
3620 self.worker.release()
3621 self.waitUntilSettled()