blob: 9787ae1d7330913de53667bedbac2bcab85ec1c1 [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,
Paul Belangerec49b4c2013-07-20 20:32:20 -0400497 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700498
499 self.job.sendWorkData(json.dumps(data))
500 self.job.sendWorkStatus(0, 100)
501
502 if self.worker.hold_jobs_in_build:
503 self._wait()
504 self.log.debug("Build %s continuing" % self.unique)
505
506 self.worker.lock.acquire()
507
508 result = 'SUCCESS'
509 if (('ZUUL_REF' in self.parameters) and
510 self.worker.shouldFailTest(self.name,
511 self.parameters['ZUUL_REF'])):
512 result = 'FAILURE'
513 if self.aborted:
514 result = 'ABORTED'
515
James E. Blair4a28a882013-08-23 15:17:33 -0700516 if self.run_error:
517 work_fail = True
518 result = 'RUN_ERROR'
519 else:
520 data['result'] = result
521 work_fail = False
522
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700523 changes = None
524 if 'ZUUL_CHANGE_IDS' in self.parameters:
525 changes = self.parameters['ZUUL_CHANGE_IDS']
526
527 self.worker.build_history.append(
528 BuildHistory(name=self.name, number=self.number,
529 result=result, changes=changes, node=self.node,
James E. Blair64ed6f22013-07-10 14:07:23 -0700530 uuid=self.unique, description=self.description,
531 pipeline=self.parameters['ZUUL_PIPELINE'])
Paul Belangerec49b4c2013-07-20 20:32:20 -0400532 )
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700533
James E. Blair4a28a882013-08-23 15:17:33 -0700534 self.job.sendWorkData(json.dumps(data))
535 if work_fail:
536 self.job.sendWorkFail()
537 else:
538 self.job.sendWorkComplete(json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700539 del self.worker.gearman_jobs[self.job.unique]
540 self.worker.running_builds.remove(self)
541 self.worker.lock.release()
542
543
544class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400545 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700546 super(FakeWorker, self).__init__(worker_id)
547 self.gearman_jobs = {}
548 self.build_history = []
549 self.running_builds = []
550 self.build_counter = 0
551 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400552 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700553
554 self.hold_jobs_in_build = False
555 self.lock = threading.Lock()
556 self.__work_thread = threading.Thread(target=self.work)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400557 self.__work_thread.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700558 self.__work_thread.start()
559
560 def handleJob(self, job):
561 parts = job.name.split(":")
562 cmd = parts[0]
563 name = parts[1]
564 if len(parts) > 2:
565 node = parts[2]
566 else:
567 node = None
568 if cmd == 'build':
569 self.handleBuild(job, name, node)
570 elif cmd == 'stop':
571 self.handleStop(job, name)
572 elif cmd == 'set_description':
573 self.handleSetDescription(job, name)
574
575 def handleBuild(self, job, name, node):
576 build = FakeBuild(self, job, self.build_counter, node)
577 job.build = build
578 self.gearman_jobs[job.unique] = job
579 self.build_counter += 1
580
581 self.running_builds.append(build)
582 build.start()
583
584 def handleStop(self, job, name):
585 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700586 parameters = json.loads(job.arguments)
587 name = parameters['name']
588 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700589 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700590 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700591 build.aborted = True
592 build.release()
593 job.sendWorkComplete()
594 return
595 job.sendWorkFail()
596
597 def handleSetDescription(self, job, name):
598 self.log.debug("handle set description")
599 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700600 name = parameters['name']
601 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700602 descr = parameters['html_description']
603 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700604 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700605 build.description = descr
606 job.sendWorkComplete()
607 return
608 for build in self.build_history:
James E. Blair3c483cf2013-06-04 16:30:43 -0700609 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700610 build.description = descr
611 job.sendWorkComplete()
612 return
613 job.sendWorkFail()
614
615 def work(self):
616 while self.running:
617 try:
618 job = self.getJob()
619 except gear.InterruptedError:
620 continue
621 try:
622 self.handleJob(job)
623 except:
624 self.log.exception("Worker exception:")
625
626 def addFailTest(self, name, change):
627 l = self.fail_tests.get(name, [])
628 l.append(change)
629 self.fail_tests[name] = l
630
631 def shouldFailTest(self, name, ref):
632 l = self.fail_tests.get(name, [])
633 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400634 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700635 return True
636 return False
637
638 def release(self, regex=None):
639 builds = self.running_builds[:]
640 self.log.debug("releasing build %s (%s)" % (regex,
James E. Blair78e31b32013-07-09 09:11:34 -0700641 len(self.running_builds)))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700642 for build in builds:
643 if not regex or re.match(regex, build.name):
644 self.log.debug("releasing build %s" %
645 (build.parameters['ZUUL_UUID']))
646 build.release()
647 else:
648 self.log.debug("not releasing build %s" %
649 (build.parameters['ZUUL_UUID']))
650 self.log.debug("done releasing builds %s (%s)" %
651 (regex, len(self.running_builds)))
652
653
654class FakeGearmanServer(gear.Server):
655 def __init__(self):
656 self.hold_jobs_in_queue = False
657 super(FakeGearmanServer, self).__init__(0)
658
659 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700660 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
661 for job in queue:
662 if not hasattr(job, 'waiting'):
663 if job.name.startswith('build:'):
664 job.waiting = self.hold_jobs_in_queue
665 else:
666 job.waiting = False
667 if job.waiting:
668 continue
669 if job.name in connection.functions:
670 if not peek:
671 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400672 connection.related_jobs[job.handle] = job
673 job.worker_connection = connection
674 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700675 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700676 return None
677
678 def release(self, regex=None):
679 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700680 qlen = (len(self.high_queue) + len(self.normal_queue) +
681 len(self.low_queue))
682 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
James E. Blairdda6c912013-07-29 14:12:12 -0700683 for job in self.getQueue():
684 cmd, name = job.name.split(':')
685 if cmd != 'build':
686 continue
687 if not regex or re.match(regex, name):
688 self.log.debug("releasing queued job %s" %
689 job.unique)
690 job.waiting = False
691 released = True
692 else:
693 self.log.debug("not releasing queued job %s" %
694 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700695 if released:
696 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700697 qlen = (len(self.high_queue) + len(self.normal_queue) +
698 len(self.low_queue))
699 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700700
701
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000702class FakeSMTP(object):
703 log = logging.getLogger('zuul.FakeSMTP')
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000704
James E. Blairff80a2f2013-12-27 13:24:06 -0800705 def __init__(self, messages, server, port):
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000706 self.server = server
707 self.port = port
James E. Blairff80a2f2013-12-27 13:24:06 -0800708 self.messages = messages
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000709
710 def sendmail(self, from_email, to_email, msg):
711 self.log.info("Sending email from %s, to %s, with msg %s" % (
712 from_email, to_email, msg))
713
714 headers = msg.split('\n\n', 1)[0]
715 body = msg.split('\n\n', 1)[1]
716
James E. Blairff80a2f2013-12-27 13:24:06 -0800717 self.messages.append(dict(
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000718 from_email=from_email,
719 to_email=to_email,
720 msg=msg,
721 headers=headers,
722 body=body,
723 ))
724
725 return True
726
727 def quit(self):
728 return True
729
730
Monty Taylorbc758832013-06-17 17:22:42 -0400731class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700732 log = logging.getLogger("zuul.test")
733
734 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400735 super(TestScheduler, self).setUp()
736 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
737 try:
738 test_timeout = int(test_timeout)
739 except ValueError:
740 # If timeout value is invalid do not set a timeout.
741 test_timeout = 0
742 if test_timeout > 0:
James E. Blairff80a2f2013-12-27 13:24:06 -0800743 self.useFixture(fixtures.Timeout(test_timeout, gentle=False))
Monty Taylorbc758832013-06-17 17:22:42 -0400744
745 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
746 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
747 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
748 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
749 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
750 os.environ.get('OS_STDERR_CAPTURE') == '1'):
751 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
752 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Monty Taylore6a6c402013-07-02 09:25:55 -0700753 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
754 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400755 self.useFixture(fixtures.FakeLogger(
756 level=logging.DEBUG,
757 format='%(asctime)s %(name)-32s '
758 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700759 tmp_root = self.useFixture(fixtures.TempDir(
760 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400761 self.test_root = os.path.join(tmp_root, "zuul-test")
762 self.upstream_root = os.path.join(self.test_root, "upstream")
763 self.git_root = os.path.join(self.test_root, "git")
764
765 CONFIG.set('zuul', 'git_dir', self.git_root)
766 if os.path.exists(self.test_root):
767 shutil.rmtree(self.test_root)
768 os.makedirs(self.test_root)
769 os.makedirs(self.upstream_root)
770 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700771
772 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400773 self.init_repo("org/project")
774 self.init_repo("org/project1")
775 self.init_repo("org/project2")
776 self.init_repo("org/project3")
777 self.init_repo("org/one-job-project")
778 self.init_repo("org/nonvoting-project")
779 self.init_repo("org/templated-project")
James E. Blair3e98c022013-12-16 15:25:38 -0800780 self.init_repo("org/layered-project")
Monty Taylorbc758832013-06-17 17:22:42 -0400781 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700782 self.init_repo("org/conflict-project")
James E. Blair412e5582013-04-22 15:50:12 -0700783
784 self.statsd = FakeStatsd()
785 os.environ['STATSD_HOST'] = 'localhost'
786 os.environ['STATSD_PORT'] = str(self.statsd.port)
787 self.statsd.start()
788 # the statsd client object is configured in the statsd module import
789 reload(statsd)
790 reload(zuul.scheduler)
791
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700792 self.gearman_server = FakeGearmanServer()
793
794 self.config = ConfigParser.ConfigParser()
795 cfg = StringIO()
796 CONFIG.write(cfg)
797 cfg.seek(0)
798 self.config.readfp(cfg)
799 self.config.set('gearman', 'port', str(self.gearman_server.port))
800
Monty Taylorbc758832013-06-17 17:22:42 -0400801 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700802 self.worker.addServer('127.0.0.1', self.gearman_server.port)
803 self.gearman_server.worker = self.worker
804
James E. Blairb0fcae42012-07-17 11:12:10 -0700805 self.sched = zuul.scheduler.Scheduler()
806
James E. Blair8cc15a82012-08-01 11:17:57 -0700807 def URLOpenerFactory(*args, **kw):
808 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400809 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700810
James E. Blair8cc15a82012-08-01 11:17:57 -0700811 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700812 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700813
James E. Blairff80a2f2013-12-27 13:24:06 -0800814 self.smtp_messages = []
815
816 def FakeSMTPFactory(*args, **kw):
817 args = [self.smtp_messages] + list(args)
818 return FakeSMTP(*args, **kw)
819
James E. Blairb0fcae42012-07-17 11:12:10 -0700820 zuul.lib.gerrit.Gerrit = FakeGerrit
James E. Blairff80a2f2013-12-27 13:24:06 -0800821 self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTPFactory))
James E. Blairb0fcae42012-07-17 11:12:10 -0700822
Monty Taylorbc758832013-06-17 17:22:42 -0400823 self.gerrit = FakeGerritTrigger(
824 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700825 self.gerrit.replication_timeout = 1.5
826 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700827 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400828 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700829
James E. Blair1843a552013-07-03 14:19:52 -0700830 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
James E. Blairad28e912013-11-27 10:43:22 -0800831 self.rpc = zuul.rpclistener.RPCListener(self.config, self.sched)
James E. Blair1843a552013-07-03 14:19:52 -0700832
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700833 self.sched.setLauncher(self.launcher)
James E. Blair6c358e72013-07-29 17:06:47 -0700834 self.sched.registerTrigger(self.gerrit)
James E. Blair63bb0ef2013-07-29 17:14:51 -0700835 self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
836 self.sched.registerTrigger(self.timer)
James E. Blairb0fcae42012-07-17 11:12:10 -0700837
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000838 self.sched.registerReporter(
839 zuul.reporter.gerrit.Reporter(self.gerrit))
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000840 self.smtp_reporter = zuul.reporter.smtp.Reporter(
841 self.config.get('smtp', 'default_from'),
842 self.config.get('smtp', 'default_to'),
843 self.config.get('smtp', 'server'))
844 self.sched.registerReporter(self.smtp_reporter)
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000845
James E. Blairb0fcae42012-07-17 11:12:10 -0700846 self.sched.start()
847 self.sched.reconfigure(self.config)
848 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700849 self.webapp.start()
James E. Blairad28e912013-11-27 10:43:22 -0800850 self.rpc.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700851 self.launcher.gearman.waitForServer()
852 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400853 self.builds = self.worker.running_builds
854 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700855
James E. Blairfee8d652013-06-07 08:57:52 -0700856 self.addCleanup(self.assertFinalState)
857 self.addCleanup(self.shutdown)
858
859 def assertFinalState(self):
860 # Make sure that the change cache is cleared
James E. Blair6c358e72013-07-29 17:06:47 -0700861 self.assertEqual(len(self.gerrit._change_cache.keys()), 0)
Clark Boylan4ba48d92013-11-11 18:03:53 -0800862 # Make sure that git.Repo objects have been garbage collected.
863 repos = []
864 gc.collect()
865 for obj in gc.get_objects():
866 if isinstance(obj, git.Repo):
867 repos.append(obj)
868 self.assertEqual(len(repos), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700869 self.assertEmptyQueues()
870
871 def shutdown(self):
872 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700873 self.launcher.stop()
874 self.worker.shutdown()
875 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700876 self.gerrit.stop()
James E. Blair63bb0ef2013-07-29 17:14:51 -0700877 self.timer.stop()
James E. Blairb0fcae42012-07-17 11:12:10 -0700878 self.sched.stop()
879 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700880 self.statsd.stop()
881 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700882 self.webapp.stop()
883 self.webapp.join()
James E. Blairad28e912013-11-27 10:43:22 -0800884 self.rpc.stop()
885 self.rpc.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700886 threads = threading.enumerate()
887 if len(threads) > 1:
888 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400889 super(TestScheduler, self).tearDown()
890
891 def init_repo(self, project):
892 parts = project.split('/')
893 path = os.path.join(self.upstream_root, *parts[:-1])
894 if not os.path.exists(path):
895 os.makedirs(path)
896 path = os.path.join(self.upstream_root, project)
897 repo = git.Repo.init(path)
898
899 repo.config_writer().set_value('user', 'email', 'user@example.com')
900 repo.config_writer().set_value('user', 'name', 'User Name')
901 repo.config_writer().write()
902
903 fn = os.path.join(path, 'README')
904 f = open(fn, 'w')
905 f.write("test\n")
906 f.close()
907 repo.index.add([fn])
908 repo.index.commit('initial commit')
909 master = repo.create_head('master')
910 repo.create_tag('init')
911
912 mp = repo.create_head('mp')
913 repo.head.reference = mp
914 f = open(fn, 'a')
915 f.write("test mp\n")
916 f.close()
917 repo.index.add([fn])
918 repo.index.commit('mp commit')
919
920 repo.head.reference = master
921 repo.head.reset(index=True, working_tree=True)
922 repo.git.clean('-x', '-f', '-d')
923
924 def ref_has_change(self, ref, change):
925 path = os.path.join(self.git_root, change.project)
926 repo = git.Repo(path)
927 for commit in repo.iter_commits(ref):
928 if commit.message.strip() == ('%s-1' % change.subject):
929 return True
930 return False
931
932 def job_has_changes(self, *args):
933 job = args[0]
934 commits = args[1:]
935 if isinstance(job, FakeBuild):
936 parameters = job.parameters
937 else:
938 parameters = json.loads(job.arguments)
939 project = parameters['ZUUL_PROJECT']
940 path = os.path.join(self.git_root, project)
941 repo = git.Repo(path)
942 ref = parameters['ZUUL_REF']
943 sha = parameters['ZUUL_COMMIT']
944 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
945 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
946 commit_messages = ['%s-1' % commit.subject for commit in commits]
947 for msg in commit_messages:
948 if msg not in repo_messages:
949 return False
950 if repo_shas[0] != sha:
951 return False
952 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700953
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700954 def registerJobs(self):
955 count = 0
James E. Blaireff88162013-07-01 12:44:14 -0400956 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700957 self.worker.registerFunction('build:' + job)
958 count += 1
959 self.worker.registerFunction('stop:' + self.worker.worker_id)
960 count += 1
961
962 while len(self.gearman_server.functions) < count:
963 time.sleep(0)
964
965 def release(self, job):
966 if isinstance(job, FakeBuild):
967 job.release()
968 else:
969 job.waiting = False
970 self.log.debug("Queued job %s released" % job.unique)
971 self.gearman_server.wakeConnections()
972
973 def getParameter(self, job, name):
974 if isinstance(job, FakeBuild):
975 return job.parameters[name]
976 else:
977 parameters = json.loads(job.arguments)
978 return parameters[name]
979
980 def resetGearmanServer(self):
981 self.worker.setFunctions([])
982 while True:
983 done = True
984 for connection in self.gearman_server.active_connections:
James E. Blairad28e912013-11-27 10:43:22 -0800985 if (connection.functions and
986 connection.client_id != 'Zuul RPC Listener'):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700987 done = False
988 if done:
989 break
990 time.sleep(0)
991 self.gearman_server.functions = set()
James E. Blairad28e912013-11-27 10:43:22 -0800992 self.rpc.register()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700993
994 def haveAllBuildsReported(self):
995 # See if Zuul is waiting on a meta job to complete
996 if self.launcher.meta_jobs:
997 return False
998 # Find out if every build that the worker has completed has been
999 # reported back to Zuul. If it hasn't then that means a Gearman
1000 # event is still in transit and the system is not stable.
1001 for build in self.worker.build_history:
1002 zbuild = self.launcher.builds.get(build.uuid)
1003 if not zbuild:
1004 # It has already been reported
1005 continue
1006 # It hasn't been reported yet.
1007 return False
1008 # Make sure that none of the worker connections are in GRAB_WAIT
1009 for connection in self.worker.active_connections:
1010 if connection.state == 'GRAB_WAIT':
1011 return False
1012 return True
1013
1014 def areAllBuildsWaiting(self):
1015 ret = True
1016
1017 builds = self.launcher.builds.values()
1018 for build in builds:
1019 client_job = None
1020 for conn in self.launcher.gearman.active_connections:
1021 for j in conn.related_jobs.values():
1022 if j.unique == build.uuid:
1023 client_job = j
1024 break
1025 if not client_job:
1026 self.log.debug("%s is not known to the gearman client" %
1027 build)
1028 ret = False
1029 continue
1030 if not client_job.handle:
1031 self.log.debug("%s has no handle" % client_job)
1032 ret = False
1033 continue
1034 server_job = self.gearman_server.jobs.get(client_job.handle)
1035 if not server_job:
1036 self.log.debug("%s is not known to the gearman server" %
1037 client_job)
1038 ret = False
1039 continue
1040 if not hasattr(server_job, 'waiting'):
1041 self.log.debug("%s is being enqueued" % server_job)
1042 ret = False
1043 continue
1044 if server_job.waiting:
1045 continue
1046 worker_job = self.worker.gearman_jobs.get(server_job.unique)
1047 if worker_job:
1048 if worker_job.build.isWaiting():
1049 continue
1050 else:
1051 self.log.debug("%s is running" % worker_job)
1052 ret = False
1053 else:
1054 self.log.debug("%s is unassigned" % server_job)
1055 ret = False
1056 return ret
1057
James E. Blairb0fcae42012-07-17 11:12:10 -07001058 def waitUntilSettled(self):
1059 self.log.debug("Waiting until settled...")
1060 start = time.time()
1061 while True:
1062 if time.time() - start > 10:
1063 print 'queue status:',
1064 print self.sched.trigger_event_queue.empty(),
1065 print self.sched.result_event_queue.empty(),
1066 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001067 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -07001068 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001069 # Make sure no new events show up while we're checking
1070 self.worker.lock.acquire()
1071 # have all build states propogated to zuul?
1072 if self.haveAllBuildsReported():
1073 # Join ensures that the queue is empty _and_ events have been
1074 # processed
1075 self.fake_gerrit.event_queue.join()
1076 self.sched.trigger_event_queue.join()
1077 self.sched.result_event_queue.join()
1078 if (self.sched.trigger_event_queue.empty() and
1079 self.sched.result_event_queue.empty() and
1080 self.fake_gerrit.event_queue.empty() and
1081 self.areAllBuildsWaiting()):
1082 self.worker.lock.release()
1083 self.log.debug("...settled.")
1084 return
1085 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001086 self.sched.wake_event.wait(0.1)
1087
James E. Blaird466dc42012-07-31 10:42:56 -07001088 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -08001089 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -07001090 return len(jobs)
1091
James E. Blair4ca985f2013-05-30 12:27:43 -07001092 def getJobFromHistory(self, name):
1093 history = self.worker.build_history
1094 for job in history:
1095 if job.name == name:
1096 return job
1097 raise Exception("Unable to find job %s in history" % name)
1098
James E. Blaire0487072012-08-29 17:38:31 -07001099 def assertEmptyQueues(self):
1100 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001101 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001102 for queue in pipeline.queues:
1103 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001104 print 'pipeline %s queue %s contents %s' % (
1105 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001106 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001107
James E. Blair66eeebf2013-07-27 17:44:32 -07001108 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001109 start = time.time()
1110 while time.time() < (start + 5):
1111 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001112 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001113 k, v = stat.split(':')
1114 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001115 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001116 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001117 elif value:
1118 if value == v:
1119 return
1120 elif kind:
1121 if v.endswith('|' + kind):
1122 return
James E. Blair412e5582013-04-22 15:50:12 -07001123 time.sleep(0.1)
1124
1125 pprint.pprint(self.statsd.stats)
1126 raise Exception("Key %s not found in reported stats" % key)
1127
James E. Blairb0fcae42012-07-17 11:12:10 -07001128 def test_jobs_launched(self):
1129 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001130
James E. Blairb0fcae42012-07-17 11:12:10 -07001131 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001132 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001133 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1134 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001135 self.assertEqual(self.getJobFromHistory('project-merge').result,
1136 'SUCCESS')
1137 self.assertEqual(self.getJobFromHistory('project-test1').result,
1138 'SUCCESS')
1139 self.assertEqual(self.getJobFromHistory('project-test2').result,
1140 'SUCCESS')
1141 self.assertEqual(A.data['status'], 'MERGED')
1142 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001143
James E. Blair66eeebf2013-07-27 17:44:32 -07001144 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1145 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1146 value='1|g')
1147 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1148 kind='ms')
1149 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1150 value='1|c')
1151 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1152 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1153 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001154 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001155 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001156 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001157 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001158
James E. Blair3cb10702013-08-24 08:56:03 -07001159 def test_initial_pipeline_gauges(self):
1160 "Test that each pipeline reported its length on start"
1161 pipeline_names = self.sched.layout.pipelines.keys()
1162 self.assertNotEqual(len(pipeline_names), 0)
1163 for name in pipeline_names:
1164 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
1165 value='0|g')
1166
James E. Blair42f74822013-05-14 15:18:03 -07001167 def test_duplicate_pipelines(self):
1168 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001169
James E. Blair42f74822013-05-14 15:18:03 -07001170 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1171 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1172 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001173
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001174 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001175 self.history[0].name == 'project-test1'
1176 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001177
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001178 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001179 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001180 self.assertIn('dup1/project-test1', A.messages[0])
1181 self.assertNotIn('dup2/project-test1', A.messages[0])
1182 self.assertNotIn('dup1/project-test1', A.messages[1])
1183 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001184 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001185 self.assertIn('dup1/project-test1', A.messages[1])
1186 self.assertNotIn('dup2/project-test1', A.messages[1])
1187 self.assertNotIn('dup1/project-test1', A.messages[0])
1188 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001189
James E. Blairb0fcae42012-07-17 11:12:10 -07001190 def test_parallel_changes(self):
1191 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001192
1193 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001194 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1195 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1196 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001197 A.addApproval('CRVW', 2)
1198 B.addApproval('CRVW', 2)
1199 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001200
1201 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1202 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1203 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1204
1205 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001206 self.assertEqual(len(self.builds), 1)
1207 self.assertEqual(self.builds[0].name, 'project-merge')
1208 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001209
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001210 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001211 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001212 self.assertEqual(len(self.builds), 3)
1213 self.assertEqual(self.builds[0].name, 'project-test1')
1214 self.assertTrue(self.job_has_changes(self.builds[0], A))
1215 self.assertEqual(self.builds[1].name, 'project-test2')
1216 self.assertTrue(self.job_has_changes(self.builds[1], A))
1217 self.assertEqual(self.builds[2].name, 'project-merge')
1218 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001219
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001220 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001221 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001222 self.assertEqual(len(self.builds), 5)
1223 self.assertEqual(self.builds[0].name, 'project-test1')
1224 self.assertTrue(self.job_has_changes(self.builds[0], A))
1225 self.assertEqual(self.builds[1].name, 'project-test2')
1226 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001227
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001228 self.assertEqual(self.builds[2].name, 'project-test1')
1229 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1230 self.assertEqual(self.builds[3].name, 'project-test2')
1231 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001232
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001233 self.assertEqual(self.builds[4].name, 'project-merge')
1234 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001235
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001236 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001237 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001238 self.assertEqual(len(self.builds), 6)
1239 self.assertEqual(self.builds[0].name, 'project-test1')
1240 self.assertTrue(self.job_has_changes(self.builds[0], A))
1241 self.assertEqual(self.builds[1].name, 'project-test2')
1242 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001243
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001244 self.assertEqual(self.builds[2].name, 'project-test1')
1245 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1246 self.assertEqual(self.builds[3].name, 'project-test2')
1247 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001248
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001249 self.assertEqual(self.builds[4].name, 'project-test1')
1250 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1251 self.assertEqual(self.builds[5].name, 'project-test2')
1252 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001253
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001254 self.worker.hold_jobs_in_build = False
1255 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001256 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001257 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001258
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001259 self.assertEqual(len(self.history), 9)
1260 self.assertEqual(A.data['status'], 'MERGED')
1261 self.assertEqual(B.data['status'], 'MERGED')
1262 self.assertEqual(C.data['status'], 'MERGED')
1263 self.assertEqual(A.reported, 2)
1264 self.assertEqual(B.reported, 2)
1265 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001266
1267 def test_failed_changes(self):
1268 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001269 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001270
James E. Blairb02a3bb2012-07-30 17:49:55 -07001271 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1272 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001273 A.addApproval('CRVW', 2)
1274 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001275
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001276 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001277
James E. Blaire2819012013-06-28 17:17:26 -04001278 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1279 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001280 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001281
1282 self.worker.release('.*-merge')
1283 self.waitUntilSettled()
1284
1285 self.worker.hold_jobs_in_build = False
1286 self.worker.release()
1287
1288 self.waitUntilSettled()
1289 # It's certain that the merge job for change 2 will run, but
1290 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001291 self.assertTrue(len(self.history) > 6)
1292 self.assertEqual(A.data['status'], 'NEW')
1293 self.assertEqual(B.data['status'], 'MERGED')
1294 self.assertEqual(A.reported, 2)
1295 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001296
1297 def test_independent_queues(self):
1298 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001299
1300 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001301 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001302 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1303 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001304 A.addApproval('CRVW', 2)
1305 B.addApproval('CRVW', 2)
1306 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001307
1308 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1309 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1310 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1311
James E. Blairb02a3bb2012-07-30 17:49:55 -07001312 self.waitUntilSettled()
1313
1314 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001315 self.assertEqual(len(self.builds), 2)
1316 self.assertEqual(self.builds[0].name, 'project-merge')
1317 self.assertTrue(self.job_has_changes(self.builds[0], A))
1318 self.assertEqual(self.builds[1].name, 'project1-merge')
1319 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001320
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001321 # Release the current merge builds
1322 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001323 self.waitUntilSettled()
1324 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001325 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001326 self.waitUntilSettled()
1327
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001328 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001329 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001330 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001331
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001332 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001333 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001334 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001335
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001336 self.assertEqual(len(self.history), 11)
1337 self.assertEqual(A.data['status'], 'MERGED')
1338 self.assertEqual(B.data['status'], 'MERGED')
1339 self.assertEqual(C.data['status'], 'MERGED')
1340 self.assertEqual(A.reported, 2)
1341 self.assertEqual(B.reported, 2)
1342 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001343
1344 def test_failed_change_at_head(self):
1345 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001346
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001347 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001348 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1349 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1350 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001351 A.addApproval('CRVW', 2)
1352 B.addApproval('CRVW', 2)
1353 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001354
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001355 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001356
1357 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1358 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1359 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1360
1361 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001362
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001363 self.assertEqual(len(self.builds), 1)
1364 self.assertEqual(self.builds[0].name, 'project-merge')
1365 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001366
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001367 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001368 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001369 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001370 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001371 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001372 self.waitUntilSettled()
1373
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001374 self.assertEqual(len(self.builds), 6)
1375 self.assertEqual(self.builds[0].name, 'project-test1')
1376 self.assertEqual(self.builds[1].name, 'project-test2')
1377 self.assertEqual(self.builds[2].name, 'project-test1')
1378 self.assertEqual(self.builds[3].name, 'project-test2')
1379 self.assertEqual(self.builds[4].name, 'project-test1')
1380 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001381
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001382 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001383 self.waitUntilSettled()
1384
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001385 # project-test2, project-merge for B
1386 self.assertEqual(len(self.builds), 2)
1387 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001388
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001389 self.worker.hold_jobs_in_build = False
1390 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001391 self.waitUntilSettled()
1392
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001393 self.assertEqual(len(self.builds), 0)
1394 self.assertEqual(len(self.history), 15)
1395 self.assertEqual(A.data['status'], 'NEW')
1396 self.assertEqual(B.data['status'], 'MERGED')
1397 self.assertEqual(C.data['status'], 'MERGED')
1398 self.assertEqual(A.reported, 2)
1399 self.assertEqual(B.reported, 2)
1400 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001401
James E. Blair0aac4872013-08-23 14:02:38 -07001402 def test_failed_change_in_middle(self):
1403 "Test a failed change in the middle of the queue"
1404
1405 self.worker.hold_jobs_in_build = True
1406 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1407 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1408 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1409 A.addApproval('CRVW', 2)
1410 B.addApproval('CRVW', 2)
1411 C.addApproval('CRVW', 2)
1412
1413 self.worker.addFailTest('project-test1', B)
1414
1415 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1416 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1417 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1418
1419 self.waitUntilSettled()
1420
1421 self.worker.release('.*-merge')
1422 self.waitUntilSettled()
1423 self.worker.release('.*-merge')
1424 self.waitUntilSettled()
1425 self.worker.release('.*-merge')
1426 self.waitUntilSettled()
1427
1428 self.assertEqual(len(self.builds), 6)
1429 self.assertEqual(self.builds[0].name, 'project-test1')
1430 self.assertEqual(self.builds[1].name, 'project-test2')
1431 self.assertEqual(self.builds[2].name, 'project-test1')
1432 self.assertEqual(self.builds[3].name, 'project-test2')
1433 self.assertEqual(self.builds[4].name, 'project-test1')
1434 self.assertEqual(self.builds[5].name, 'project-test2')
1435
1436 self.release(self.builds[2])
1437 self.waitUntilSettled()
1438
James E. Blair972e3c72013-08-29 12:04:55 -07001439 # project-test1 and project-test2 for A
1440 # project-test2 for B
1441 # project-merge for C (without B)
1442 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -07001443 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1444
James E. Blair972e3c72013-08-29 12:04:55 -07001445 self.worker.release('.*-merge')
1446 self.waitUntilSettled()
1447
1448 # project-test1 and project-test2 for A
1449 # project-test2 for B
1450 # project-test1 and project-test2 for C
1451 self.assertEqual(len(self.builds), 5)
1452
James E. Blair0aac4872013-08-23 14:02:38 -07001453 items = self.sched.layout.pipelines['gate'].getAllItems()
1454 builds = items[0].current_build_set.getBuilds()
1455 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1456 self.assertEqual(self.countJobResults(builds, None), 2)
1457 builds = items[1].current_build_set.getBuilds()
1458 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1459 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1460 self.assertEqual(self.countJobResults(builds, None), 1)
1461 builds = items[2].current_build_set.getBuilds()
1462 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -07001463 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -07001464
1465 self.worker.hold_jobs_in_build = False
1466 self.worker.release()
1467 self.waitUntilSettled()
1468
1469 self.assertEqual(len(self.builds), 0)
1470 self.assertEqual(len(self.history), 12)
1471 self.assertEqual(A.data['status'], 'MERGED')
1472 self.assertEqual(B.data['status'], 'NEW')
1473 self.assertEqual(C.data['status'], 'MERGED')
1474 self.assertEqual(A.reported, 2)
1475 self.assertEqual(B.reported, 2)
1476 self.assertEqual(C.reported, 2)
1477
James E. Blaird466dc42012-07-31 10:42:56 -07001478 def test_failed_change_at_head_with_queue(self):
1479 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001480
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001481 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001482 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1483 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1484 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001485 A.addApproval('CRVW', 2)
1486 B.addApproval('CRVW', 2)
1487 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001488
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001489 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001490
1491 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1492 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1493 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1494
1495 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001496 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001497 self.assertEqual(len(self.builds), 0)
1498 self.assertEqual(len(queue), 1)
1499 self.assertEqual(queue[0].name, 'build:project-merge')
1500 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001501
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001502 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001503 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001504 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001505 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001506 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001507 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001508 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001509
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001510 self.assertEqual(len(self.builds), 0)
1511 self.assertEqual(len(queue), 6)
1512 self.assertEqual(queue[0].name, 'build:project-test1')
1513 self.assertEqual(queue[1].name, 'build:project-test2')
1514 self.assertEqual(queue[2].name, 'build:project-test1')
1515 self.assertEqual(queue[3].name, 'build:project-test2')
1516 self.assertEqual(queue[4].name, 'build:project-test1')
1517 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001518
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001519 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001520 self.waitUntilSettled()
1521
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001522 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001523 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001524 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1525 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001526
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001527 self.gearman_server.hold_jobs_in_queue = False
1528 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001529 self.waitUntilSettled()
1530
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001531 self.assertEqual(len(self.builds), 0)
1532 self.assertEqual(len(self.history), 11)
1533 self.assertEqual(A.data['status'], 'NEW')
1534 self.assertEqual(B.data['status'], 'MERGED')
1535 self.assertEqual(C.data['status'], 'MERGED')
1536 self.assertEqual(A.reported, 2)
1537 self.assertEqual(B.reported, 2)
1538 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001539
James E. Blairfef71632013-09-23 11:15:47 -07001540 def test_two_failed_changes_at_head(self):
1541 "Test that changes are reparented correctly if 2 fail at head"
1542
1543 self.worker.hold_jobs_in_build = True
1544 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1545 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1546 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1547 A.addApproval('CRVW', 2)
1548 B.addApproval('CRVW', 2)
1549 C.addApproval('CRVW', 2)
1550
1551 self.worker.addFailTest('project-test1', A)
1552 self.worker.addFailTest('project-test1', B)
1553
1554 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1555 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1556 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1557 self.waitUntilSettled()
1558
1559 self.worker.release('.*-merge')
1560 self.waitUntilSettled()
1561 self.worker.release('.*-merge')
1562 self.waitUntilSettled()
1563 self.worker.release('.*-merge')
1564 self.waitUntilSettled()
1565
1566 self.assertEqual(len(self.builds), 6)
1567 self.assertEqual(self.builds[0].name, 'project-test1')
1568 self.assertEqual(self.builds[1].name, 'project-test2')
1569 self.assertEqual(self.builds[2].name, 'project-test1')
1570 self.assertEqual(self.builds[3].name, 'project-test2')
1571 self.assertEqual(self.builds[4].name, 'project-test1')
1572 self.assertEqual(self.builds[5].name, 'project-test2')
1573
1574 self.assertTrue(self.job_has_changes(self.builds[0], A))
1575 self.assertTrue(self.job_has_changes(self.builds[2], A))
1576 self.assertTrue(self.job_has_changes(self.builds[2], B))
1577 self.assertTrue(self.job_has_changes(self.builds[4], A))
1578 self.assertTrue(self.job_has_changes(self.builds[4], B))
1579 self.assertTrue(self.job_has_changes(self.builds[4], C))
1580
1581 # Fail change B first
1582 self.release(self.builds[2])
1583 self.waitUntilSettled()
1584
1585 # restart of C after B failure
1586 self.worker.release('.*-merge')
1587 self.waitUntilSettled()
1588
1589 self.assertEqual(len(self.builds), 5)
1590 self.assertEqual(self.builds[0].name, 'project-test1')
1591 self.assertEqual(self.builds[1].name, 'project-test2')
1592 self.assertEqual(self.builds[2].name, 'project-test2')
1593 self.assertEqual(self.builds[3].name, 'project-test1')
1594 self.assertEqual(self.builds[4].name, 'project-test2')
1595
1596 self.assertTrue(self.job_has_changes(self.builds[1], A))
1597 self.assertTrue(self.job_has_changes(self.builds[2], A))
1598 self.assertTrue(self.job_has_changes(self.builds[2], B))
1599 self.assertTrue(self.job_has_changes(self.builds[4], A))
1600 self.assertFalse(self.job_has_changes(self.builds[4], B))
1601 self.assertTrue(self.job_has_changes(self.builds[4], C))
1602
1603 # Finish running all passing jobs for change A
1604 self.release(self.builds[1])
1605 self.waitUntilSettled()
1606 # Fail and report change A
1607 self.release(self.builds[0])
1608 self.waitUntilSettled()
1609
1610 # restart of B,C after A failure
1611 self.worker.release('.*-merge')
1612 self.waitUntilSettled()
1613 self.worker.release('.*-merge')
1614 self.waitUntilSettled()
1615
1616 self.assertEqual(len(self.builds), 4)
1617 self.assertEqual(self.builds[0].name, 'project-test1') # B
1618 self.assertEqual(self.builds[1].name, 'project-test2') # B
1619 self.assertEqual(self.builds[2].name, 'project-test1') # C
1620 self.assertEqual(self.builds[3].name, 'project-test2') # C
1621
1622 self.assertFalse(self.job_has_changes(self.builds[1], A))
1623 self.assertTrue(self.job_has_changes(self.builds[1], B))
1624 self.assertFalse(self.job_has_changes(self.builds[1], C))
1625
1626 self.assertFalse(self.job_has_changes(self.builds[2], A))
1627 # After A failed and B and C restarted, B should be back in
1628 # C's tests because it has not failed yet.
1629 self.assertTrue(self.job_has_changes(self.builds[2], B))
1630 self.assertTrue(self.job_has_changes(self.builds[2], C))
1631
1632 self.worker.hold_jobs_in_build = False
1633 self.worker.release()
1634 self.waitUntilSettled()
1635
1636 self.assertEqual(len(self.builds), 0)
1637 self.assertEqual(len(self.history), 21)
1638 self.assertEqual(A.data['status'], 'NEW')
1639 self.assertEqual(B.data['status'], 'NEW')
1640 self.assertEqual(C.data['status'], 'MERGED')
1641 self.assertEqual(A.reported, 2)
1642 self.assertEqual(B.reported, 2)
1643 self.assertEqual(C.reported, 2)
1644
James E. Blair8c803f82012-07-31 16:25:42 -07001645 def test_patch_order(self):
1646 "Test that dependent patches are tested in the right order"
1647 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1648 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1649 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1650 A.addApproval('CRVW', 2)
1651 B.addApproval('CRVW', 2)
1652 C.addApproval('CRVW', 2)
1653
1654 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1655 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1656 M2.setMerged()
1657 M1.setMerged()
1658
1659 # C -> B -> A -> M1 -> M2
1660 # M2 is here to make sure it is never queried. If it is, it
1661 # means zuul is walking down the entire history of merged
1662 # changes.
1663
1664 C.setDependsOn(B, 1)
1665 B.setDependsOn(A, 1)
1666 A.setDependsOn(M1, 1)
1667 M1.setDependsOn(M2, 1)
1668
1669 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1670
1671 self.waitUntilSettled()
1672
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001673 self.assertEqual(A.data['status'], 'NEW')
1674 self.assertEqual(B.data['status'], 'NEW')
1675 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001676
1677 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1678 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1679
1680 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001681 self.assertEqual(M2.queried, 0)
1682 self.assertEqual(A.data['status'], 'MERGED')
1683 self.assertEqual(B.data['status'], 'MERGED')
1684 self.assertEqual(C.data['status'], 'MERGED')
1685 self.assertEqual(A.reported, 2)
1686 self.assertEqual(B.reported, 2)
1687 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001688
James E. Blair0e933c52013-07-11 10:18:52 -07001689 def test_trigger_cache(self):
1690 "Test that the trigger cache operates correctly"
1691 self.worker.hold_jobs_in_build = True
1692
1693 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1694 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1695 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1696 A.addApproval('CRVW', 2)
1697 B.addApproval('CRVW', 2)
1698
1699 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1700 M1.setMerged()
1701
1702 B.setDependsOn(A, 1)
1703 A.setDependsOn(M1, 1)
1704
1705 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1706 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1707
1708 self.waitUntilSettled()
1709
1710 for build in self.builds:
1711 if build.parameters['ZUUL_PIPELINE'] == 'check':
1712 build.release()
1713 self.waitUntilSettled()
1714 for build in self.builds:
1715 if build.parameters['ZUUL_PIPELINE'] == 'check':
1716 build.release()
1717 self.waitUntilSettled()
1718
1719 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1720 self.waitUntilSettled()
1721
James E. Blair6c358e72013-07-29 17:06:47 -07001722 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001723 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001724 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001725
1726 self.worker.hold_jobs_in_build = False
1727 self.worker.release()
1728 self.waitUntilSettled()
1729
1730 self.assertEqual(A.data['status'], 'MERGED')
1731 self.assertEqual(B.data['status'], 'MERGED')
1732 self.assertEqual(A.queried, 2) # Initial and isMerged
1733 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1734
James E. Blair8c803f82012-07-31 16:25:42 -07001735 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001736 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001737 # TODO: move to test_gerrit (this is a unit test!)
1738 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001739 trigger = self.sched.layout.pipelines['gate'].trigger
1740 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001741 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001742 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001743
1744 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001745 a = trigger.getChange(1, 2, refresh=True)
1746 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001747
1748 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001749 a = trigger.getChange(1, 2, refresh=True)
1750 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1751 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001752
1753 def test_build_configuration(self):
1754 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001755
1756 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001757 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1758 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1759 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1760 A.addApproval('CRVW', 2)
1761 B.addApproval('CRVW', 2)
1762 C.addApproval('CRVW', 2)
1763 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1764 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1765 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1766 self.waitUntilSettled()
1767
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001768 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001769 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001770 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001771 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001772 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001773 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001774 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001775 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1776 self.gearman_server.hold_jobs_in_queue = False
1777 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001778 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001779
Monty Taylorbc758832013-06-17 17:22:42 -04001780 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001781 repo = git.Repo(path)
1782 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1783 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001784 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001785 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001786
1787 def test_build_configuration_conflict(self):
1788 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001789
1790 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001791 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1792 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001793 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001794 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1795 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001796 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001797 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1798 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001799 A.addApproval('CRVW', 2)
1800 B.addApproval('CRVW', 2)
1801 C.addApproval('CRVW', 2)
1802 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1803 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1804 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1805 self.waitUntilSettled()
1806
James E. Blair6736beb2013-07-11 15:18:15 -07001807 self.assertEqual(A.reported, 1)
1808 self.assertEqual(B.reported, 1)
1809 self.assertEqual(C.reported, 1)
1810
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001811 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001812 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001813 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001814 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001815 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001816 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001817
1818 self.assertEqual(len(self.history), 2) # A and C merge jobs
1819
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001820 self.gearman_server.hold_jobs_in_queue = False
1821 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001822 self.waitUntilSettled()
1823
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001824 self.assertEqual(A.data['status'], 'MERGED')
1825 self.assertEqual(B.data['status'], 'NEW')
1826 self.assertEqual(C.data['status'], 'MERGED')
1827 self.assertEqual(A.reported, 2)
1828 self.assertEqual(B.reported, 2)
1829 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001830 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001831
James E. Blairdaabed22012-08-15 15:38:57 -07001832 def test_post(self):
1833 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001834
Zhongyue Luo5d556072012-09-21 02:00:47 +09001835 e = {
1836 "type": "ref-updated",
1837 "submitter": {
1838 "name": "User Name",
1839 },
1840 "refUpdate": {
1841 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1842 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1843 "refName": "master",
1844 "project": "org/project",
1845 }
1846 }
James E. Blairdaabed22012-08-15 15:38:57 -07001847 self.fake_gerrit.addEvent(e)
1848 self.waitUntilSettled()
1849
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001850 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001851 self.assertEqual(len(self.history), 1)
1852 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001853
1854 def test_build_configuration_branch(self):
1855 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001856
1857 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001858 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1859 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1860 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1861 A.addApproval('CRVW', 2)
1862 B.addApproval('CRVW', 2)
1863 C.addApproval('CRVW', 2)
1864 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1865 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1866 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1867 self.waitUntilSettled()
1868
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001869 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001870 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001871 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001872 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001873 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001874 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001875 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001876 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1877 self.gearman_server.hold_jobs_in_queue = False
1878 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001879 self.waitUntilSettled()
1880
Monty Taylorbc758832013-06-17 17:22:42 -04001881 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001882 repo = git.Repo(path)
1883 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1884 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001885 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001886 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001887
1888 def test_build_configuration_branch_interaction(self):
1889 "Test that switching between branches works"
1890 self.test_build_configuration()
1891 self.test_build_configuration_branch()
1892 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001893 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001894 repo = git.Repo(path)
1895 repo.heads.master.commit = repo.commit('init')
1896 self.test_build_configuration()
1897
1898 def test_build_configuration_multi_branch(self):
1899 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001900
1901 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001902 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1903 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1904 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1905 A.addApproval('CRVW', 2)
1906 B.addApproval('CRVW', 2)
1907 C.addApproval('CRVW', 2)
1908 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1909 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1910 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1911 self.waitUntilSettled()
1912
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001913 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001914 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001915 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001916 job_B = None
1917 for job in queue:
1918 if 'project-merge' in job.name:
1919 job_B = job
1920 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001921 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001922 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001923 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001924 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001925 for job in queue:
1926 if 'project-merge' in job.name:
1927 job_C = job
1928 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairf750aa02013-07-15 14:11:24 -07001929 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001930 self.gearman_server.hold_jobs_in_queue = False
1931 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001932 self.waitUntilSettled()
1933
Monty Taylorbc758832013-06-17 17:22:42 -04001934 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001935 repo = git.Repo(path)
1936
1937 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001938 for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001939 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001940 correct_messages = ['initial commit', 'A-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001941 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001942
1943 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001944 for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07001945 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001946 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001947 self.assertEqual(repo_messages, correct_messages)
James E. Blair7f71c802012-08-22 13:04:32 -07001948
1949 def test_one_job_project(self):
1950 "Test that queueing works with one job"
1951 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1952 'master', 'A')
1953 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1954 'master', 'B')
1955 A.addApproval('CRVW', 2)
1956 B.addApproval('CRVW', 2)
1957 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1958 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1959 self.waitUntilSettled()
1960
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001961 self.assertEqual(A.data['status'], 'MERGED')
1962 self.assertEqual(A.reported, 2)
1963 self.assertEqual(B.data['status'], 'MERGED')
1964 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001965
Antoine Musso80edd5a2013-02-13 15:37:53 +01001966 def test_job_from_templates_launched(self):
1967 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001968
Antoine Musso80edd5a2013-02-13 15:37:53 +01001969 A = self.fake_gerrit.addFakeChange(
1970 'org/templated-project', 'master', 'A')
1971 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1972 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001973
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001974 self.assertEqual(self.getJobFromHistory('project-test1').result,
1975 'SUCCESS')
1976 self.assertEqual(self.getJobFromHistory('project-test2').result,
1977 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01001978
James E. Blair3e98c022013-12-16 15:25:38 -08001979 def test_layered_templates(self):
1980 "Test whether a job generated via a template can be launched"
1981
1982 A = self.fake_gerrit.addFakeChange(
1983 'org/layered-project', 'master', 'A')
1984 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1985 self.waitUntilSettled()
1986
1987 self.assertEqual(self.getJobFromHistory('project-test1').result,
1988 'SUCCESS')
1989 self.assertEqual(self.getJobFromHistory('project-test2').result,
1990 'SUCCESS')
James E. Blairaea6cf62013-12-16 15:38:12 -08001991 self.assertEqual(self.getJobFromHistory('layered-project-test3'
1992 ).result, 'SUCCESS')
1993 self.assertEqual(self.getJobFromHistory('layered-project-test4'
1994 ).result, 'SUCCESS')
1995 # test5 should run twice because two templates define it
James E. Blair3e98c022013-12-16 15:25:38 -08001996 test5_count = 0
1997 for job in self.worker.build_history:
James E. Blairaea6cf62013-12-16 15:38:12 -08001998 if job.name == 'layered-project-foo-test5':
James E. Blair3e98c022013-12-16 15:25:38 -08001999 test5_count += 1
2000 self.assertEqual(job.result, 'SUCCESS')
2001 self.assertEqual(test5_count, 2)
2002 self.assertEqual(self.getJobFromHistory('project-test6').result,
2003 'SUCCESS')
2004
James E. Blaircaec0c52012-08-22 14:52:22 -07002005 def test_dependent_changes_dequeue(self):
2006 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002007
James E. Blaircaec0c52012-08-22 14:52:22 -07002008 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2009 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2010 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2011 A.addApproval('CRVW', 2)
2012 B.addApproval('CRVW', 2)
2013 C.addApproval('CRVW', 2)
2014
2015 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
2016 M1.setMerged()
2017
2018 # C -> B -> A -> M1
2019
2020 C.setDependsOn(B, 1)
2021 B.setDependsOn(A, 1)
2022 A.setDependsOn(M1, 1)
2023
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002024 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07002025
2026 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2027 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2028 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2029
2030 self.waitUntilSettled()
2031
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002032 self.assertEqual(A.data['status'], 'NEW')
2033 self.assertEqual(A.reported, 2)
2034 self.assertEqual(B.data['status'], 'NEW')
2035 self.assertEqual(B.reported, 2)
2036 self.assertEqual(C.data['status'], 'NEW')
2037 self.assertEqual(C.reported, 2)
2038 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07002039
James E. Blair972e3c72013-08-29 12:04:55 -07002040 def test_failing_dependent_changes(self):
2041 "Test that failing dependent patches are taken out of stream"
2042 self.worker.hold_jobs_in_build = True
2043 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2044 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2045 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2046 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2047 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
2048 A.addApproval('CRVW', 2)
2049 B.addApproval('CRVW', 2)
2050 C.addApproval('CRVW', 2)
2051 D.addApproval('CRVW', 2)
2052 E.addApproval('CRVW', 2)
2053
2054 # E, D -> C -> B, A
2055
2056 D.setDependsOn(C, 1)
2057 C.setDependsOn(B, 1)
2058
2059 self.worker.addFailTest('project-test1', B)
2060
2061 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2062 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2063 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2064 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2065 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2066
2067 self.waitUntilSettled()
2068 self.worker.release('.*-merge')
2069 self.waitUntilSettled()
2070 self.worker.release('.*-merge')
2071 self.waitUntilSettled()
2072 self.worker.release('.*-merge')
2073 self.waitUntilSettled()
2074 self.worker.release('.*-merge')
2075 self.waitUntilSettled()
2076 self.worker.release('.*-merge')
2077 self.waitUntilSettled()
2078
2079 self.worker.hold_jobs_in_build = False
2080 for build in self.builds:
2081 if build.parameters['ZUUL_CHANGE'] != '1':
2082 build.release()
2083 self.waitUntilSettled()
2084
2085 self.worker.release()
2086 self.waitUntilSettled()
2087
2088 self.assertEqual(A.data['status'], 'MERGED')
2089 self.assertEqual(A.reported, 2)
2090 self.assertEqual(B.data['status'], 'NEW')
2091 self.assertEqual(B.reported, 2)
2092 self.assertEqual(C.data['status'], 'NEW')
2093 self.assertEqual(C.reported, 2)
2094 self.assertEqual(D.data['status'], 'NEW')
2095 self.assertEqual(D.reported, 2)
2096 self.assertEqual(E.data['status'], 'MERGED')
2097 self.assertEqual(E.reported, 2)
2098 self.assertEqual(len(self.history), 18)
2099
James E. Blairec590122012-08-22 15:19:31 -07002100 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08002101 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07002102 # If it's dequeued more than once, we should see extra
2103 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07002104
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002105 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07002106 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2107 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2108 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2109 A.addApproval('CRVW', 2)
2110 B.addApproval('CRVW', 2)
2111 C.addApproval('CRVW', 2)
2112
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002113 self.worker.addFailTest('project1-test1', A)
2114 self.worker.addFailTest('project1-test2', A)
2115 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07002116
2117 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2118 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2119 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2120
2121 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07002122
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002123 self.assertEqual(len(self.builds), 1)
2124 self.assertEqual(self.builds[0].name, 'project1-merge')
2125 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07002126
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002127 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002128 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002129 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002130 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002131 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002132 self.waitUntilSettled()
2133
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002134 self.assertEqual(len(self.builds), 9)
2135 self.assertEqual(self.builds[0].name, 'project1-test1')
2136 self.assertEqual(self.builds[1].name, 'project1-test2')
2137 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
2138 self.assertEqual(self.builds[3].name, 'project1-test1')
2139 self.assertEqual(self.builds[4].name, 'project1-test2')
2140 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
2141 self.assertEqual(self.builds[6].name, 'project1-test1')
2142 self.assertEqual(self.builds[7].name, 'project1-test2')
2143 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07002144
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002145 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07002146 self.waitUntilSettled()
2147
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002148 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
2149 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07002150
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002151 self.worker.hold_jobs_in_build = False
2152 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07002153 self.waitUntilSettled()
2154
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002155 self.assertEqual(len(self.builds), 0)
2156 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07002157
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002158 self.assertEqual(A.data['status'], 'NEW')
2159 self.assertEqual(B.data['status'], 'MERGED')
2160 self.assertEqual(C.data['status'], 'MERGED')
2161 self.assertEqual(A.reported, 2)
2162 self.assertEqual(B.reported, 2)
2163 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07002164
2165 def test_nonvoting_job(self):
2166 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002167
James E. Blair4ec821f2012-08-23 15:28:28 -07002168 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
2169 'master', 'A')
2170 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002171 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07002172 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2173
2174 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07002175
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002176 self.assertEqual(A.data['status'], 'MERGED')
2177 self.assertEqual(A.reported, 2)
2178 self.assertEqual(
2179 self.getJobFromHistory('nonvoting-project-merge').result,
2180 'SUCCESS')
2181 self.assertEqual(
2182 self.getJobFromHistory('nonvoting-project-test1').result,
2183 'SUCCESS')
2184 self.assertEqual(
2185 self.getJobFromHistory('nonvoting-project-test2').result,
2186 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07002187
2188 def test_check_queue_success(self):
2189 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002190
James E. Blaire0487072012-08-29 17:38:31 -07002191 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2192 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2193
2194 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002195
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002196 self.assertEqual(A.data['status'], 'NEW')
2197 self.assertEqual(A.reported, 1)
2198 self.assertEqual(self.getJobFromHistory('project-merge').result,
2199 'SUCCESS')
2200 self.assertEqual(self.getJobFromHistory('project-test1').result,
2201 'SUCCESS')
2202 self.assertEqual(self.getJobFromHistory('project-test2').result,
2203 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07002204
2205 def test_check_queue_failure(self):
2206 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002207
James E. Blaire0487072012-08-29 17:38:31 -07002208 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002209 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07002210 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2211
2212 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002213
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002214 self.assertEqual(A.data['status'], 'NEW')
2215 self.assertEqual(A.reported, 1)
2216 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07002217 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002218 self.assertEqual(self.getJobFromHistory('project-test1').result,
2219 'SUCCESS')
2220 self.assertEqual(self.getJobFromHistory('project-test2').result,
2221 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07002222
2223 def test_dependent_behind_dequeue(self):
2224 "test that dependent changes behind dequeued changes work"
2225 # This complicated test is a reproduction of a real life bug
2226 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07002227
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002228 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07002229 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2230 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2231 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2232 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2233 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
2234 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
2235 D.setDependsOn(C, 1)
2236 E.setDependsOn(D, 1)
2237 A.addApproval('CRVW', 2)
2238 B.addApproval('CRVW', 2)
2239 C.addApproval('CRVW', 2)
2240 D.addApproval('CRVW', 2)
2241 E.addApproval('CRVW', 2)
2242 F.addApproval('CRVW', 2)
2243
2244 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07002245
2246 # Change object re-use in the gerrit trigger is hidden if
2247 # changes are added in quick succession; waiting makes it more
2248 # like real life.
2249 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2250 self.waitUntilSettled()
2251 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2252 self.waitUntilSettled()
2253
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002254 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002255 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002256 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002257 self.waitUntilSettled()
2258
2259 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2260 self.waitUntilSettled()
2261 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2262 self.waitUntilSettled()
2263 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2264 self.waitUntilSettled()
2265 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
2266 self.waitUntilSettled()
2267
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002268 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002269 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002270 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002271 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002272 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002273 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002274 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002275 self.waitUntilSettled()
2276
2277 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002278
2279 # Grab pointers to the jobs we want to release before
2280 # releasing any, because list indexes may change as
2281 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002282 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002283 a.release()
2284 b.release()
2285 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002286 self.waitUntilSettled()
2287
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002288 self.worker.hold_jobs_in_build = False
2289 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002290 self.waitUntilSettled()
2291
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002292 self.assertEqual(A.data['status'], 'NEW')
2293 self.assertEqual(B.data['status'], 'MERGED')
2294 self.assertEqual(C.data['status'], 'MERGED')
2295 self.assertEqual(D.data['status'], 'MERGED')
2296 self.assertEqual(E.data['status'], 'MERGED')
2297 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002298
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002299 self.assertEqual(A.reported, 2)
2300 self.assertEqual(B.reported, 2)
2301 self.assertEqual(C.reported, 2)
2302 self.assertEqual(D.reported, 2)
2303 self.assertEqual(E.reported, 2)
2304 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002305
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002306 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2307 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002308
2309 def test_merger_repack(self):
2310 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002311
James E. Blair05fed602012-09-07 12:45:24 -07002312 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2313 A.addApproval('CRVW', 2)
2314 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2315 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002316 self.assertEqual(self.getJobFromHistory('project-merge').result,
2317 'SUCCESS')
2318 self.assertEqual(self.getJobFromHistory('project-test1').result,
2319 'SUCCESS')
2320 self.assertEqual(self.getJobFromHistory('project-test2').result,
2321 'SUCCESS')
2322 self.assertEqual(A.data['status'], 'MERGED')
2323 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07002324 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002325 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002326
Monty Taylorbc758832013-06-17 17:22:42 -04002327 path = os.path.join(self.git_root, "org/project")
2328 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002329
2330 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2331 A.addApproval('CRVW', 2)
2332 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2333 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002334 self.assertEqual(self.getJobFromHistory('project-merge').result,
2335 'SUCCESS')
2336 self.assertEqual(self.getJobFromHistory('project-test1').result,
2337 'SUCCESS')
2338 self.assertEqual(self.getJobFromHistory('project-test2').result,
2339 'SUCCESS')
2340 self.assertEqual(A.data['status'], 'MERGED')
2341 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002342
James E. Blair4886f282012-11-15 09:27:33 -08002343 def test_merger_repack_large_change(self):
2344 "Test that the merger works with large changes after a repack"
2345 # https://bugs.launchpad.net/zuul/+bug/1078946
2346 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2347 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002348 path = os.path.join(self.upstream_root, "org/project1")
2349 print repack_repo(path)
2350 path = os.path.join(self.git_root, "org/project1")
2351 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002352
2353 A.addApproval('CRVW', 2)
2354 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2355 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002356 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2357 'SUCCESS')
2358 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2359 'SUCCESS')
2360 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2361 'SUCCESS')
2362 self.assertEqual(A.data['status'], 'MERGED')
2363 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002364
James E. Blair7ee88a22012-09-12 18:59:31 +02002365 def test_nonexistent_job(self):
2366 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002367 # Set to the state immediately after a restart
2368 self.resetGearmanServer()
2369 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002370
2371 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2372 A.addApproval('CRVW', 2)
2373 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2374 # There may be a thread about to report a lost change
2375 while A.reported < 2:
2376 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002377 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002378 self.assertFalse(job_names)
2379 self.assertEqual(A.data['status'], 'NEW')
2380 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002381 self.assertEmptyQueues()
2382
2383 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002384 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002385 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2386 A.addApproval('CRVW', 2)
2387 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2388 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002389 self.assertEqual(self.getJobFromHistory('project-merge').result,
2390 'SUCCESS')
2391 self.assertEqual(self.getJobFromHistory('project-test1').result,
2392 'SUCCESS')
2393 self.assertEqual(self.getJobFromHistory('project-test2').result,
2394 'SUCCESS')
2395 self.assertEqual(A.data['status'], 'MERGED')
2396 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002397
2398 def test_single_nonexistent_post_job(self):
2399 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002400 e = {
2401 "type": "ref-updated",
2402 "submitter": {
2403 "name": "User Name",
2404 },
2405 "refUpdate": {
2406 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2407 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2408 "refName": "master",
2409 "project": "org/project",
2410 }
2411 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002412 # Set to the state immediately after a restart
2413 self.resetGearmanServer()
2414 self.launcher.negative_function_cache_ttl = 0
2415
James E. Blairf62d4282012-12-31 17:01:50 -08002416 self.fake_gerrit.addEvent(e)
2417 self.waitUntilSettled()
2418
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002419 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002420
2421 def test_new_patchset_dequeues_old(self):
2422 "Test that a new patchset causes the old to be dequeued"
2423 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002424 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002425 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2426 M.setMerged()
2427
2428 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2429 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2430 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2431 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2432 A.addApproval('CRVW', 2)
2433 B.addApproval('CRVW', 2)
2434 C.addApproval('CRVW', 2)
2435 D.addApproval('CRVW', 2)
2436
2437 C.setDependsOn(B, 1)
2438 B.setDependsOn(A, 1)
2439 A.setDependsOn(M, 1)
2440
2441 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2442 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2443 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2444 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2445 self.waitUntilSettled()
2446
2447 B.addPatchset()
2448 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2449 self.waitUntilSettled()
2450
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002451 self.worker.hold_jobs_in_build = False
2452 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002453 self.waitUntilSettled()
2454
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002455 self.assertEqual(A.data['status'], 'MERGED')
2456 self.assertEqual(A.reported, 2)
2457 self.assertEqual(B.data['status'], 'NEW')
2458 self.assertEqual(B.reported, 2)
2459 self.assertEqual(C.data['status'], 'NEW')
2460 self.assertEqual(C.reported, 2)
2461 self.assertEqual(D.data['status'], 'MERGED')
2462 self.assertEqual(D.reported, 2)
2463 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002464
Arx Cruzb1b010d2013-10-28 19:49:59 -02002465 def test_zuul_url_return(self):
2466 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
2467 self.assertTrue(self.sched.config.has_option('zuul', 'zuul_url'))
2468 self.worker.hold_jobs_in_build = True
2469
2470 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2471 A.addApproval('CRVW', 2)
2472 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2473 self.waitUntilSettled()
2474
2475 self.assertEqual(len(self.builds), 1)
2476 for build in self.builds:
2477 self.assertTrue('ZUUL_URL' in build.parameters)
2478
2479 self.worker.hold_jobs_in_build = False
2480 self.worker.release()
2481 self.waitUntilSettled()
2482
James E. Blair2fa50962013-01-30 21:50:41 -08002483 def test_new_patchset_dequeues_old_on_head(self):
2484 "Test that a new patchset causes the old to be dequeued (at head)"
2485 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002486 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002487 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2488 M.setMerged()
2489 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2490 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2491 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2492 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2493 A.addApproval('CRVW', 2)
2494 B.addApproval('CRVW', 2)
2495 C.addApproval('CRVW', 2)
2496 D.addApproval('CRVW', 2)
2497
2498 C.setDependsOn(B, 1)
2499 B.setDependsOn(A, 1)
2500 A.setDependsOn(M, 1)
2501
2502 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2503 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2504 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2505 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2506 self.waitUntilSettled()
2507
2508 A.addPatchset()
2509 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2510 self.waitUntilSettled()
2511
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002512 self.worker.hold_jobs_in_build = False
2513 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002514 self.waitUntilSettled()
2515
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002516 self.assertEqual(A.data['status'], 'NEW')
2517 self.assertEqual(A.reported, 2)
2518 self.assertEqual(B.data['status'], 'NEW')
2519 self.assertEqual(B.reported, 2)
2520 self.assertEqual(C.data['status'], 'NEW')
2521 self.assertEqual(C.reported, 2)
2522 self.assertEqual(D.data['status'], 'MERGED')
2523 self.assertEqual(D.reported, 2)
2524 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002525
2526 def test_new_patchset_dequeues_old_without_dependents(self):
2527 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002528 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002529 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2530 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2531 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2532 A.addApproval('CRVW', 2)
2533 B.addApproval('CRVW', 2)
2534 C.addApproval('CRVW', 2)
2535
2536 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2537 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2538 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2539 self.waitUntilSettled()
2540
2541 B.addPatchset()
2542 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2543 self.waitUntilSettled()
2544
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002545 self.worker.hold_jobs_in_build = False
2546 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002547 self.waitUntilSettled()
2548
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002549 self.assertEqual(A.data['status'], 'MERGED')
2550 self.assertEqual(A.reported, 2)
2551 self.assertEqual(B.data['status'], 'NEW')
2552 self.assertEqual(B.reported, 2)
2553 self.assertEqual(C.data['status'], 'MERGED')
2554 self.assertEqual(C.reported, 2)
2555 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002556
2557 def test_new_patchset_dequeues_old_independent_queue(self):
2558 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002559 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002560 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2561 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2562 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2563 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2564 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2565 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2566 self.waitUntilSettled()
2567
2568 B.addPatchset()
2569 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2570 self.waitUntilSettled()
2571
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002572 self.worker.hold_jobs_in_build = False
2573 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002574 self.waitUntilSettled()
2575
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002576 self.assertEqual(A.data['status'], 'NEW')
2577 self.assertEqual(A.reported, 1)
2578 self.assertEqual(B.data['status'], 'NEW')
2579 self.assertEqual(B.reported, 1)
2580 self.assertEqual(C.data['status'], 'NEW')
2581 self.assertEqual(C.reported, 1)
2582 self.assertEqual(len(self.history), 10)
2583 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002584
2585 def test_zuul_refs(self):
2586 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002587 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002588 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2589 M1.setMerged()
2590 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2591 M2.setMerged()
2592
2593 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2594 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2595 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2596 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2597 A.addApproval('CRVW', 2)
2598 B.addApproval('CRVW', 2)
2599 C.addApproval('CRVW', 2)
2600 D.addApproval('CRVW', 2)
2601 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2602 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2603 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2604 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2605
2606 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002607 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002608 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002609 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002610 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002611 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002612 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002613 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002614 self.waitUntilSettled()
2615
James E. Blair7d0dedc2013-02-21 17:26:09 -08002616 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002617 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002618 if x.parameters['ZUUL_CHANGE'] == '3':
2619 a_zref = x.parameters['ZUUL_REF']
2620 if x.parameters['ZUUL_CHANGE'] == '4':
2621 b_zref = x.parameters['ZUUL_REF']
2622 if x.parameters['ZUUL_CHANGE'] == '5':
2623 c_zref = x.parameters['ZUUL_REF']
2624 if x.parameters['ZUUL_CHANGE'] == '6':
2625 d_zref = x.parameters['ZUUL_REF']
2626
2627 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002628 self.assertIsNotNone(a_zref)
2629 self.assertIsNotNone(b_zref)
2630 self.assertIsNotNone(c_zref)
2631 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002632
2633 # And they should all be different
2634 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002635 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002636
2637 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002638 self.assertTrue(self.ref_has_change(a_zref, A))
2639 self.assertFalse(self.ref_has_change(a_zref, B))
2640 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002641
2642 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002643 self.assertTrue(self.ref_has_change(b_zref, A))
2644 self.assertTrue(self.ref_has_change(b_zref, B))
2645 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002646
2647 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002648 self.assertTrue(self.ref_has_change(c_zref, A))
2649 self.assertTrue(self.ref_has_change(c_zref, B))
2650 self.assertTrue(self.ref_has_change(c_zref, C))
2651 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002652
2653 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002654 self.assertTrue(self.ref_has_change(d_zref, A))
2655 self.assertTrue(self.ref_has_change(d_zref, B))
2656 self.assertTrue(self.ref_has_change(d_zref, C))
2657 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002658
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002659 self.worker.hold_jobs_in_build = False
2660 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002661 self.waitUntilSettled()
2662
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002663 self.assertEqual(A.data['status'], 'MERGED')
2664 self.assertEqual(A.reported, 2)
2665 self.assertEqual(B.data['status'], 'MERGED')
2666 self.assertEqual(B.reported, 2)
2667 self.assertEqual(C.data['status'], 'MERGED')
2668 self.assertEqual(C.reported, 2)
2669 self.assertEqual(D.data['status'], 'MERGED')
2670 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002671
James E. Blairc053d022014-01-22 14:57:33 -08002672 def test_required_approval_check_and_gate(self):
2673 "Test required-approval triggers both check and gate"
2674 self.config.set('zuul', 'layout_config',
2675 'tests/fixtures/layout-require-approval.yaml')
2676 self.sched.reconfigure(self.config)
2677 self.registerJobs()
2678
2679 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2680 A.addApproval('CRVW', 2)
2681 # Add a too-old +1
2682 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2683
2684 aprv = A.addApproval('APRV', 1)
2685 self.fake_gerrit.addEvent(aprv)
2686 self.waitUntilSettled()
2687 # Should have run a check job
2688 self.assertEqual(len(self.history), 1)
2689 self.assertEqual(self.history[0].name, 'project-check')
2690
2691 # Report the result of that check job (overrides previous vrfy)
2692 # Skynet alert: this should trigger a gate job now that
2693 # all reqs are met
2694 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2695 self.waitUntilSettled()
2696 self.assertEqual(len(self.history), 2)
2697 self.assertEqual(self.history[1].name, 'project-gate')
2698
2699 def test_required_approval_newer(self):
2700 "Test required-approval newer trigger parameter"
2701 self.config.set('zuul', 'layout_config',
2702 'tests/fixtures/layout-require-approval.yaml')
2703 self.sched.reconfigure(self.config)
2704 self.registerJobs()
2705
2706 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2707 A.addApproval('CRVW', 2)
2708 aprv = A.addApproval('APRV', 1)
2709 self.fake_gerrit.addEvent(aprv)
2710 self.waitUntilSettled()
2711 # No +1 from Jenkins so should not be enqueued
2712 self.assertEqual(len(self.history), 0)
2713
2714 # Add a too-old +1, should trigger check but not gate
2715 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2716 self.fake_gerrit.addEvent(aprv)
2717 self.waitUntilSettled()
2718 self.assertEqual(len(self.history), 1)
2719 self.assertEqual(self.history[0].name, 'project-check')
2720
2721 # Add a recent +1
2722 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2723 self.fake_gerrit.addEvent(aprv)
2724 self.waitUntilSettled()
2725 self.assertEqual(len(self.history), 2)
2726 self.assertEqual(self.history[1].name, 'project-gate')
2727
2728 def test_required_approval_older(self):
2729 "Test required-approval older trigger parameter"
2730 self.config.set('zuul', 'layout_config',
2731 'tests/fixtures/layout-require-approval.yaml')
2732 self.sched.reconfigure(self.config)
2733 self.registerJobs()
2734
2735 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2736 crvw = A.addApproval('CRVW', 2)
2737 self.fake_gerrit.addEvent(crvw)
2738 self.waitUntilSettled()
2739 # No +1 from Jenkins so should not be enqueued
2740 self.assertEqual(len(self.history), 0)
2741
2742 # Add an old +1 and trigger check with a comment
2743 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2744 self.fake_gerrit.addEvent(crvw)
2745 self.waitUntilSettled()
2746 self.assertEqual(len(self.history), 1)
2747 self.assertEqual(self.history[0].name, 'project-check')
2748
2749 # Add a recent +1 and make sure nothing changes
2750 A.addApproval('VRFY', 1)
2751 self.fake_gerrit.addEvent(crvw)
2752 self.waitUntilSettled()
2753 self.assertEqual(len(self.history), 1)
2754
2755 # The last thing we did was query a change then do nothing
2756 # with a pipeline, so it will be in the cache; clean it up so
2757 # it does not fail the test.
2758 for pipeline in self.sched.layout.pipelines.values():
2759 pipeline.trigger.maintainCache([])
2760
James E. Blair4a28a882013-08-23 15:17:33 -07002761 def test_rerun_on_error(self):
2762 "Test that if a worker fails to run a job, it is run again"
2763 self.worker.hold_jobs_in_build = True
2764 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2765 A.addApproval('CRVW', 2)
2766 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2767 self.waitUntilSettled()
2768
2769 self.builds[0].run_error = True
2770 self.worker.hold_jobs_in_build = False
2771 self.worker.release()
2772 self.waitUntilSettled()
2773 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2774 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2775
James E. Blair412e5582013-04-22 15:50:12 -07002776 def test_statsd(self):
2777 "Test each of the statsd methods used in the scheduler"
2778 import extras
2779 statsd = extras.try_import('statsd.statsd')
2780 statsd.incr('test-incr')
2781 statsd.timing('test-timing', 3)
2782 statsd.gauge('test-guage', 12)
2783 self.assertReportedStat('test-incr', '1|c')
2784 self.assertReportedStat('test-timing', '3|ms')
2785 self.assertReportedStat('test-guage', '12|g')
2786
James E. Blair70c71582013-03-06 08:50:50 -08002787 def test_file_jobs(self):
2788 "Test that file jobs run only when appropriate"
2789 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2790 A.addPatchset(['pip-requires'])
2791 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2792 A.addApproval('CRVW', 2)
2793 B.addApproval('CRVW', 2)
2794 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2795 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2796 self.waitUntilSettled()
2797
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002798 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002799 if x.name == 'project-testfile']
2800
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002801 self.assertEqual(len(testfile_jobs), 1)
2802 self.assertEqual(testfile_jobs[0].changes, '1,2')
2803 self.assertEqual(A.data['status'], 'MERGED')
2804 self.assertEqual(A.reported, 2)
2805 self.assertEqual(B.data['status'], 'MERGED')
2806 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002807
2808 def test_test_config(self):
2809 "Test that we can test the config"
2810 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002811 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002812 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002813 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002814
2815 def test_build_description(self):
2816 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002817 self.worker.registerFunction('set_description:' +
2818 self.worker.worker_id)
2819
2820 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2821 A.addApproval('CRVW', 2)
2822 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2823 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002824 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002825 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002826 self.assertTrue(re.search("Branch.*master", desc))
2827 self.assertTrue(re.search("Pipeline.*gate", desc))
2828 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2829 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2830 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2831 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002832
James E. Blair64ed6f22013-07-10 14:07:23 -07002833 def test_queue_precedence(self):
2834 "Test that queue precedence works"
2835
2836 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002837 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002838 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2839 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2840 A.addApproval('CRVW', 2)
2841 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2842
2843 self.waitUntilSettled()
2844 self.gearman_server.hold_jobs_in_queue = False
2845 self.gearman_server.release()
2846 self.waitUntilSettled()
2847
James E. Blair8de58bd2013-07-18 16:23:33 -07002848 # Run one build at a time to ensure non-race order:
2849 for x in range(6):
2850 self.release(self.builds[0])
2851 self.waitUntilSettled()
2852 self.worker.hold_jobs_in_build = False
2853 self.waitUntilSettled()
2854
James E. Blair64ed6f22013-07-10 14:07:23 -07002855 self.log.debug(self.history)
2856 self.assertEqual(self.history[0].pipeline, 'gate')
2857 self.assertEqual(self.history[1].pipeline, 'check')
2858 self.assertEqual(self.history[2].pipeline, 'gate')
2859 self.assertEqual(self.history[3].pipeline, 'gate')
2860 self.assertEqual(self.history[4].pipeline, 'check')
2861 self.assertEqual(self.history[5].pipeline, 'check')
2862
James E. Blair1843a552013-07-03 14:19:52 -07002863 def test_json_status(self):
2864 "Test that we can retrieve JSON status info"
2865 self.worker.hold_jobs_in_build = True
2866 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2867 A.addApproval('CRVW', 2)
2868 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2869 self.waitUntilSettled()
2870
2871 port = self.webapp.server.socket.getsockname()[1]
2872
2873 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2874 data = f.read()
2875
2876 self.worker.hold_jobs_in_build = False
2877 self.worker.release()
2878 self.waitUntilSettled()
2879
2880 data = json.loads(data)
2881 status_jobs = set()
2882 for p in data['pipelines']:
2883 for q in p['change_queues']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002884 if q['dependent']:
2885 self.assertEqual(q['window'], 20)
2886 else:
2887 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002888 for head in q['heads']:
2889 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002890 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002891 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002892 for job in change['jobs']:
2893 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002894 self.assertIn('project-merge', status_jobs)
2895 self.assertIn('project-test1', status_jobs)
2896 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002897
James E. Blairc3d428e2013-12-03 15:06:48 -08002898 def test_merging_queues(self):
2899 "Test that transitively-connected change queues are merged"
2900 self.config.set('zuul', 'layout_config',
2901 'tests/fixtures/layout-merge-queues.yaml')
2902 self.sched.reconfigure(self.config)
2903 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
2904
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002905 def test_node_label(self):
2906 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002907 self.worker.registerFunction('build:node-project-test1:debian')
2908
2909 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2910 A.addApproval('CRVW', 2)
2911 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2912 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002913
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002914 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2915 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2916 'debian')
2917 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07002918
2919 def test_live_reconfiguration(self):
2920 "Test that live reconfiguration works"
2921 self.worker.hold_jobs_in_build = True
2922 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2923 A.addApproval('CRVW', 2)
2924 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2925 self.waitUntilSettled()
2926
2927 self.sched.reconfigure(self.config)
2928
2929 self.worker.hold_jobs_in_build = False
2930 self.worker.release()
2931 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002932 self.assertEqual(self.getJobFromHistory('project-merge').result,
2933 'SUCCESS')
2934 self.assertEqual(self.getJobFromHistory('project-test1').result,
2935 'SUCCESS')
2936 self.assertEqual(self.getJobFromHistory('project-test2').result,
2937 'SUCCESS')
2938 self.assertEqual(A.data['status'], 'MERGED')
2939 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002940
James E. Blaire712d9f2013-07-31 11:40:11 -07002941 def test_live_reconfiguration_functions(self):
2942 "Test live reconfiguration with a custom function"
2943 self.worker.registerFunction('build:node-project-test1:debian')
2944 self.worker.registerFunction('build:node-project-test1:wheezy')
2945 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2946 A.addApproval('CRVW', 2)
2947 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2948 self.waitUntilSettled()
2949
2950 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2951 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2952 'debian')
2953 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2954
2955 self.config.set('zuul', 'layout_config',
2956 'tests/fixtures/layout-live-'
2957 'reconfiguration-functions.yaml')
2958 self.sched.reconfigure(self.config)
2959 self.worker.build_history = []
2960
2961 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
2962 B.addApproval('CRVW', 2)
2963 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2964 self.waitUntilSettled()
2965
2966 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2967 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2968 'wheezy')
2969 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2970
James E. Blair287c06d2013-07-24 10:39:30 -07002971 def test_delayed_repo_init(self):
2972 self.config.set('zuul', 'layout_config',
2973 'tests/fixtures/layout-delayed-repo-init.yaml')
2974 self.sched.reconfigure(self.config)
2975
2976 self.init_repo("org/new-project")
2977 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2978
2979 A.addApproval('CRVW', 2)
2980 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2981 self.waitUntilSettled()
2982 self.assertEqual(self.getJobFromHistory('project-merge').result,
2983 'SUCCESS')
2984 self.assertEqual(self.getJobFromHistory('project-test1').result,
2985 'SUCCESS')
2986 self.assertEqual(self.getJobFromHistory('project-test2').result,
2987 'SUCCESS')
2988 self.assertEqual(A.data['status'], 'MERGED')
2989 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002990
Clark Boylan6dbbc482013-10-18 10:57:31 -07002991 def test_repo_deleted(self):
2992 self.config.set('zuul', 'layout_config',
2993 'tests/fixtures/layout-repo-deleted.yaml')
2994 self.sched.reconfigure(self.config)
2995
2996 self.init_repo("org/delete-project")
2997 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2998
2999 A.addApproval('CRVW', 2)
3000 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3001 self.waitUntilSettled()
3002 self.assertEqual(self.getJobFromHistory('project-merge').result,
3003 'SUCCESS')
3004 self.assertEqual(self.getJobFromHistory('project-test1').result,
3005 'SUCCESS')
3006 self.assertEqual(self.getJobFromHistory('project-test2').result,
3007 'SUCCESS')
3008 self.assertEqual(A.data['status'], 'MERGED')
3009 self.assertEqual(A.reported, 2)
3010
3011 # Delete org/new-project zuul repo. Should be recloned.
3012 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
3013
3014 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
3015
3016 B.addApproval('CRVW', 2)
3017 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3018 self.waitUntilSettled()
3019 self.assertEqual(self.getJobFromHistory('project-merge').result,
3020 'SUCCESS')
3021 self.assertEqual(self.getJobFromHistory('project-test1').result,
3022 'SUCCESS')
3023 self.assertEqual(self.getJobFromHistory('project-test2').result,
3024 'SUCCESS')
3025 self.assertEqual(B.data['status'], 'MERGED')
3026 self.assertEqual(B.reported, 2)
3027
James E. Blair87650fa2014-01-08 11:43:23 +08003028 def test_push_urls(self):
3029 "Test that Zuul can push refs to multiple URLs"
3030 upstream_path = os.path.join(self.upstream_root, 'org/project')
3031 replica1 = os.path.join(self.upstream_root, 'replica1')
3032 replica2 = os.path.join(self.upstream_root, 'replica2')
3033
3034 self.config.add_section('replication')
3035 self.config.set('replication', 'url1', 'file://%s' % replica1)
3036 self.config.set('replication', 'url2', 'file://%s' % replica2)
3037 self.sched.reconfigure(self.config)
3038
3039 r1 = git.Repo.clone_from(upstream_path, replica1 + '/org/project.git')
3040 r2 = git.Repo.clone_from(upstream_path, replica2 + '/org/project.git')
3041
3042 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3043 A.addApproval('CRVW', 2)
3044 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3045 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
3046 B.addApproval('CRVW', 2)
3047 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3048 self.waitUntilSettled()
3049 count = 0
3050 for ref in r1.refs:
3051 if ref.path.startswith('refs/zuul'):
3052 count += 1
3053 self.assertEqual(count, 3)
3054
3055 count = 0
3056 for ref in r2.refs:
3057 if ref.path.startswith('refs/zuul'):
3058 count += 1
3059 self.assertEqual(count, 3)
3060
James E. Blair63bb0ef2013-07-29 17:14:51 -07003061 def test_timer(self):
3062 "Test that a periodic job is triggered"
3063 self.worker.hold_jobs_in_build = True
3064 self.config.set('zuul', 'layout_config',
3065 'tests/fixtures/layout-timer.yaml')
3066 self.sched.reconfigure(self.config)
3067 self.registerJobs()
3068
3069 start = time.time()
3070 failed = True
3071 while ((time.time() - start) < 30):
3072 if len(self.builds) == 2:
3073 failed = False
3074 break
3075 else:
3076 time.sleep(1)
3077
3078 if failed:
3079 raise Exception("Expected jobs never ran")
3080
3081 self.waitUntilSettled()
3082 port = self.webapp.server.socket.getsockname()[1]
3083
3084 f = urllib.urlopen("http://localhost:%s/status.json" % port)
3085 data = f.read()
3086
3087 self.worker.hold_jobs_in_build = False
3088 self.worker.release()
3089 self.waitUntilSettled()
3090
3091 self.assertEqual(self.getJobFromHistory(
3092 'project-bitrot-stable-old').result, 'SUCCESS')
3093 self.assertEqual(self.getJobFromHistory(
3094 'project-bitrot-stable-older').result, 'SUCCESS')
3095
3096 data = json.loads(data)
3097 status_jobs = set()
3098 for p in data['pipelines']:
3099 for q in p['change_queues']:
3100 for head in q['heads']:
3101 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07003102 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003103 for job in change['jobs']:
3104 status_jobs.add(job['name'])
3105 self.assertIn('project-bitrot-stable-old', status_jobs)
3106 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003107
3108 def test_check_smtp_pool(self):
3109 self.config.set('zuul', 'layout_config',
3110 'tests/fixtures/layout-smtp.yaml')
3111 self.sched.reconfigure(self.config)
3112
3113 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3114 self.waitUntilSettled()
3115
3116 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3117 self.waitUntilSettled()
3118
James E. Blairff80a2f2013-12-27 13:24:06 -08003119 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003120
3121 # A.messages only holds what FakeGerrit places in it. Thus we
3122 # work on the knowledge of what the first message should be as
3123 # it is only configured to go to SMTP.
3124
3125 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003126 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003127 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003128 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003129 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003130 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003131
3132 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003133 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003134 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003135 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003136 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003137 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003138
James E. Blaire5910202013-12-27 09:50:31 -08003139 def test_timer_smtp(self):
3140 "Test that a periodic job is triggered"
3141 self.config.set('zuul', 'layout_config',
3142 'tests/fixtures/layout-timer-smtp.yaml')
3143 self.sched.reconfigure(self.config)
3144 self.registerJobs()
3145
3146 start = time.time()
3147 failed = True
3148 while ((time.time() - start) < 30):
3149 if len(self.history) == 2:
3150 failed = False
3151 break
3152 else:
3153 time.sleep(1)
3154
3155 if failed:
3156 raise Exception("Expected jobs never ran")
3157
3158 self.waitUntilSettled()
3159
3160 self.assertEqual(self.getJobFromHistory(
3161 'project-bitrot-stable-old').result, 'SUCCESS')
3162 self.assertEqual(self.getJobFromHistory(
3163 'project-bitrot-stable-older').result, 'SUCCESS')
3164
James E. Blairff80a2f2013-12-27 13:24:06 -08003165 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003166
3167 # A.messages only holds what FakeGerrit places in it. Thus we
3168 # work on the knowledge of what the first message should be as
3169 # it is only configured to go to SMTP.
3170
3171 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003172 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003173 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003174 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003175 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003176 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003177
James E. Blairad28e912013-11-27 10:43:22 -08003178 def test_client_enqueue(self):
3179 "Test that the RPC client can enqueue a change"
3180 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3181 A.addApproval('CRVW', 2)
3182 A.addApproval('APRV', 1)
3183
3184 client = zuul.rpcclient.RPCClient('127.0.0.1',
3185 self.gearman_server.port)
3186 r = client.enqueue(pipeline='gate',
3187 project='org/project',
3188 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003189 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003190 self.waitUntilSettled()
3191 self.assertEqual(self.getJobFromHistory('project-merge').result,
3192 'SUCCESS')
3193 self.assertEqual(self.getJobFromHistory('project-test1').result,
3194 'SUCCESS')
3195 self.assertEqual(self.getJobFromHistory('project-test2').result,
3196 'SUCCESS')
3197 self.assertEqual(A.data['status'], 'MERGED')
3198 self.assertEqual(A.reported, 2)
3199 self.assertEqual(r, True)
3200
3201 def test_client_enqueue_negative(self):
3202 "Test that the RPC client returns errors"
3203 client = zuul.rpcclient.RPCClient('127.0.0.1',
3204 self.gearman_server.port)
3205 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3206 "Invalid project"):
3207 r = client.enqueue(pipeline='gate',
3208 project='project-does-not-exist',
3209 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003210 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003211 client.shutdown()
3212 self.assertEqual(r, False)
3213
3214 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3215 "Invalid pipeline"):
3216 r = client.enqueue(pipeline='pipeline-does-not-exist',
3217 project='org/project',
3218 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003219 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003220 client.shutdown()
3221 self.assertEqual(r, False)
3222
3223 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3224 "Invalid trigger"):
3225 r = client.enqueue(pipeline='gate',
3226 project='org/project',
3227 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003228 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003229 client.shutdown()
3230 self.assertEqual(r, False)
3231
3232 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3233 "Invalid change"):
3234 r = client.enqueue(pipeline='gate',
3235 project='org/project',
3236 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003237 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003238 client.shutdown()
3239 self.assertEqual(r, False)
3240
3241 self.waitUntilSettled()
3242 self.assertEqual(len(self.history), 0)
3243 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003244
3245 def test_client_promote(self):
3246 "Test that the RPC client can promote a change"
3247 self.worker.hold_jobs_in_build = True
3248 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3249 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3250 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3251 A.addApproval('CRVW', 2)
3252 B.addApproval('CRVW', 2)
3253 C.addApproval('CRVW', 2)
3254
3255 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3256 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3257 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3258
3259 self.waitUntilSettled()
3260
Sean Daguef39b9ca2014-01-10 21:34:35 -05003261 items = self.sched.layout.pipelines['gate'].getAllItems()
3262 enqueue_times = {}
3263 for item in items:
3264 enqueue_times[str(item.change)] = item.enqueue_time
3265
James E. Blair36658cf2013-12-06 17:53:48 -08003266 client = zuul.rpcclient.RPCClient('127.0.0.1',
3267 self.gearman_server.port)
3268 r = client.promote(pipeline='gate',
3269 change_ids=['2,1', '3,1'])
3270
Sean Daguef39b9ca2014-01-10 21:34:35 -05003271 # ensure that enqueue times are durable
3272 items = self.sched.layout.pipelines['gate'].getAllItems()
3273 for item in items:
3274 self.assertEqual(
3275 enqueue_times[str(item.change)], item.enqueue_time)
3276
James E. Blair36658cf2013-12-06 17:53:48 -08003277 self.worker.release('.*-merge')
3278 self.waitUntilSettled()
3279 self.worker.release('.*-merge')
3280 self.waitUntilSettled()
3281 self.worker.release('.*-merge')
3282 self.waitUntilSettled()
3283
3284 self.assertEqual(len(self.builds), 6)
3285 self.assertEqual(self.builds[0].name, 'project-test1')
3286 self.assertEqual(self.builds[1].name, 'project-test2')
3287 self.assertEqual(self.builds[2].name, 'project-test1')
3288 self.assertEqual(self.builds[3].name, 'project-test2')
3289 self.assertEqual(self.builds[4].name, 'project-test1')
3290 self.assertEqual(self.builds[5].name, 'project-test2')
3291
3292 self.assertTrue(self.job_has_changes(self.builds[0], B))
3293 self.assertFalse(self.job_has_changes(self.builds[0], A))
3294 self.assertFalse(self.job_has_changes(self.builds[0], C))
3295
3296 self.assertTrue(self.job_has_changes(self.builds[2], B))
3297 self.assertTrue(self.job_has_changes(self.builds[2], C))
3298 self.assertFalse(self.job_has_changes(self.builds[2], A))
3299
3300 self.assertTrue(self.job_has_changes(self.builds[4], B))
3301 self.assertTrue(self.job_has_changes(self.builds[4], C))
3302 self.assertTrue(self.job_has_changes(self.builds[4], A))
3303
3304 self.worker.release()
3305 self.waitUntilSettled()
3306
3307 self.assertEqual(A.data['status'], 'MERGED')
3308 self.assertEqual(A.reported, 2)
3309 self.assertEqual(B.data['status'], 'MERGED')
3310 self.assertEqual(B.reported, 2)
3311 self.assertEqual(C.data['status'], 'MERGED')
3312 self.assertEqual(C.reported, 2)
3313
3314 client.shutdown()
3315 self.assertEqual(r, True)
3316
3317 def test_client_promote_dependent(self):
3318 "Test that the RPC client can promote a dependent change"
3319 # C (depends on B) -> B -> A ; then promote C to get:
3320 # A -> C (depends on B) -> B
3321 self.worker.hold_jobs_in_build = True
3322 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3323 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3324 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3325
3326 C.setDependsOn(B, 1)
3327
3328 A.addApproval('CRVW', 2)
3329 B.addApproval('CRVW', 2)
3330 C.addApproval('CRVW', 2)
3331
3332 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3333 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3334 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3335
3336 self.waitUntilSettled()
3337
3338 client = zuul.rpcclient.RPCClient('127.0.0.1',
3339 self.gearman_server.port)
3340 r = client.promote(pipeline='gate',
3341 change_ids=['3,1'])
3342
3343 self.worker.release('.*-merge')
3344 self.waitUntilSettled()
3345 self.worker.release('.*-merge')
3346 self.waitUntilSettled()
3347 self.worker.release('.*-merge')
3348 self.waitUntilSettled()
3349
3350 self.assertEqual(len(self.builds), 6)
3351 self.assertEqual(self.builds[0].name, 'project-test1')
3352 self.assertEqual(self.builds[1].name, 'project-test2')
3353 self.assertEqual(self.builds[2].name, 'project-test1')
3354 self.assertEqual(self.builds[3].name, 'project-test2')
3355 self.assertEqual(self.builds[4].name, 'project-test1')
3356 self.assertEqual(self.builds[5].name, 'project-test2')
3357
3358 self.assertTrue(self.job_has_changes(self.builds[0], B))
3359 self.assertFalse(self.job_has_changes(self.builds[0], A))
3360 self.assertFalse(self.job_has_changes(self.builds[0], C))
3361
3362 self.assertTrue(self.job_has_changes(self.builds[2], B))
3363 self.assertTrue(self.job_has_changes(self.builds[2], C))
3364 self.assertFalse(self.job_has_changes(self.builds[2], A))
3365
3366 self.assertTrue(self.job_has_changes(self.builds[4], B))
3367 self.assertTrue(self.job_has_changes(self.builds[4], C))
3368 self.assertTrue(self.job_has_changes(self.builds[4], A))
3369
3370 self.worker.release()
3371 self.waitUntilSettled()
3372
3373 self.assertEqual(A.data['status'], 'MERGED')
3374 self.assertEqual(A.reported, 2)
3375 self.assertEqual(B.data['status'], 'MERGED')
3376 self.assertEqual(B.reported, 2)
3377 self.assertEqual(C.data['status'], 'MERGED')
3378 self.assertEqual(C.reported, 2)
3379
3380 client.shutdown()
3381 self.assertEqual(r, True)
3382
3383 def test_client_promote_negative(self):
3384 "Test that the RPC client returns errors for promotion"
3385 self.worker.hold_jobs_in_build = True
3386 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3387 A.addApproval('CRVW', 2)
3388 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3389 self.waitUntilSettled()
3390
3391 client = zuul.rpcclient.RPCClient('127.0.0.1',
3392 self.gearman_server.port)
3393
3394 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3395 r = client.promote(pipeline='nonexistent',
3396 change_ids=['2,1', '3,1'])
3397 client.shutdown()
3398 self.assertEqual(r, False)
3399
3400 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3401 r = client.promote(pipeline='gate',
3402 change_ids=['4,1'])
3403 client.shutdown()
3404 self.assertEqual(r, False)
3405
3406 self.worker.hold_jobs_in_build = False
3407 self.worker.release()
3408 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003409
3410 def test_queue_rate_limiting(self):
3411 "Test that DependentPipelines are rate limited with dep across window"
3412 self.config.set('zuul', 'layout_config',
3413 'tests/fixtures/layout-rate-limit.yaml')
3414 self.sched.reconfigure(self.config)
3415 self.worker.hold_jobs_in_build = True
3416 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3417 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3418 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3419
3420 C.setDependsOn(B, 1)
3421 self.worker.addFailTest('project-test1', A)
3422
3423 A.addApproval('CRVW', 2)
3424 B.addApproval('CRVW', 2)
3425 C.addApproval('CRVW', 2)
3426
3427 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3428 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3429 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3430 self.waitUntilSettled()
3431
3432 # Only A and B will have their merge jobs queued because
3433 # window is 2.
3434 self.assertEqual(len(self.builds), 2)
3435 self.assertEqual(self.builds[0].name, 'project-merge')
3436 self.assertEqual(self.builds[1].name, 'project-merge')
3437
3438 self.worker.release('.*-merge')
3439 self.waitUntilSettled()
3440 self.worker.release('.*-merge')
3441 self.waitUntilSettled()
3442
3443 # Only A and B will have their test jobs queued because
3444 # window is 2.
3445 self.assertEqual(len(self.builds), 4)
3446 self.assertEqual(self.builds[0].name, 'project-test1')
3447 self.assertEqual(self.builds[1].name, 'project-test2')
3448 self.assertEqual(self.builds[2].name, 'project-test1')
3449 self.assertEqual(self.builds[3].name, 'project-test2')
3450
3451 self.worker.release('project-.*')
3452 self.waitUntilSettled()
3453
3454 queue = self.sched.layout.pipelines['gate'].queues[0]
3455 # A failed so window is reduced by 1 to 1.
3456 self.assertEqual(queue.window, 1)
3457 self.assertEqual(queue.window_floor, 1)
3458 self.assertEqual(A.data['status'], 'NEW')
3459
3460 # Gate is reset and only B's merge job is queued because
3461 # window shrunk to 1.
3462 self.assertEqual(len(self.builds), 1)
3463 self.assertEqual(self.builds[0].name, 'project-merge')
3464
3465 self.worker.release('.*-merge')
3466 self.waitUntilSettled()
3467
3468 # Only B's test jobs are queued because window is still 1.
3469 self.assertEqual(len(self.builds), 2)
3470 self.assertEqual(self.builds[0].name, 'project-test1')
3471 self.assertEqual(self.builds[1].name, 'project-test2')
3472
3473 self.worker.release('project-.*')
3474 self.waitUntilSettled()
3475
3476 # B was successfully merged so window is increased to 2.
3477 self.assertEqual(queue.window, 2)
3478 self.assertEqual(queue.window_floor, 1)
3479 self.assertEqual(B.data['status'], 'MERGED')
3480
3481 # Only C is left and its merge job is queued.
3482 self.assertEqual(len(self.builds), 1)
3483 self.assertEqual(self.builds[0].name, 'project-merge')
3484
3485 self.worker.release('.*-merge')
3486 self.waitUntilSettled()
3487
3488 # After successful merge job the test jobs for C are queued.
3489 self.assertEqual(len(self.builds), 2)
3490 self.assertEqual(self.builds[0].name, 'project-test1')
3491 self.assertEqual(self.builds[1].name, 'project-test2')
3492
3493 self.worker.release('project-.*')
3494 self.waitUntilSettled()
3495
3496 # C successfully merged so window is bumped to 3.
3497 self.assertEqual(queue.window, 3)
3498 self.assertEqual(queue.window_floor, 1)
3499 self.assertEqual(C.data['status'], 'MERGED')
3500
3501 def test_queue_rate_limiting_dependent(self):
3502 "Test that DependentPipelines are rate limited with dep in window"
3503 self.config.set('zuul', 'layout_config',
3504 'tests/fixtures/layout-rate-limit.yaml')
3505 self.sched.reconfigure(self.config)
3506 self.worker.hold_jobs_in_build = True
3507 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3508 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3509 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3510
3511 B.setDependsOn(A, 1)
3512
3513 self.worker.addFailTest('project-test1', A)
3514
3515 A.addApproval('CRVW', 2)
3516 B.addApproval('CRVW', 2)
3517 C.addApproval('CRVW', 2)
3518
3519 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3520 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3521 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3522 self.waitUntilSettled()
3523
3524 # Only A and B will have their merge jobs queued because
3525 # window is 2.
3526 self.assertEqual(len(self.builds), 2)
3527 self.assertEqual(self.builds[0].name, 'project-merge')
3528 self.assertEqual(self.builds[1].name, 'project-merge')
3529
3530 self.worker.release('.*-merge')
3531 self.waitUntilSettled()
3532 self.worker.release('.*-merge')
3533 self.waitUntilSettled()
3534
3535 # Only A and B will have their test jobs queued because
3536 # window is 2.
3537 self.assertEqual(len(self.builds), 4)
3538 self.assertEqual(self.builds[0].name, 'project-test1')
3539 self.assertEqual(self.builds[1].name, 'project-test2')
3540 self.assertEqual(self.builds[2].name, 'project-test1')
3541 self.assertEqual(self.builds[3].name, 'project-test2')
3542
3543 self.worker.release('project-.*')
3544 self.waitUntilSettled()
3545
3546 queue = self.sched.layout.pipelines['gate'].queues[0]
3547 # A failed so window is reduced by 1 to 1.
3548 self.assertEqual(queue.window, 1)
3549 self.assertEqual(queue.window_floor, 1)
3550 self.assertEqual(A.data['status'], 'NEW')
3551 self.assertEqual(B.data['status'], 'NEW')
3552
3553 # Gate is reset and only C's merge job is queued because
3554 # window shrunk to 1 and A and B were dequeued.
3555 self.assertEqual(len(self.builds), 1)
3556 self.assertEqual(self.builds[0].name, 'project-merge')
3557
3558 self.worker.release('.*-merge')
3559 self.waitUntilSettled()
3560
3561 # Only C's test jobs are queued because window is still 1.
3562 self.assertEqual(len(self.builds), 2)
3563 self.assertEqual(self.builds[0].name, 'project-test1')
3564 self.assertEqual(self.builds[1].name, 'project-test2')
3565
3566 self.worker.release('project-.*')
3567 self.waitUntilSettled()
3568
3569 # C was successfully merged so window is increased to 2.
3570 self.assertEqual(queue.window, 2)
3571 self.assertEqual(queue.window_floor, 1)
3572 self.assertEqual(C.data['status'], 'MERGED')