blob: 7cfea1c4e4e5cc2ccfb218de858f3ae9506aaa2c [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
Joshua Hesketh36c3fa52014-01-22 11:40:52 +110033import swiftclient
Monty Taylorbc758832013-06-17 17:22:42 -040034import threading
35import time
James E. Blair1843a552013-07-03 14:19:52 -070036import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040037import urllib2
Monty Taylorbc758832013-06-17 17:22:42 -040038
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
tanline621a412014-02-12 11:16:36 +080042import six.moves.urllib.parse as urlparse
Monty Taylorbc758832013-06-17 17:22:42 -040043import statsd
44import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070045
James E. Blairb0fcae42012-07-17 11:12:10 -070046import zuul.scheduler
James E. Blair1843a552013-07-03 14:19:52 -070047import zuul.webapp
James E. Blairad28e912013-11-27 10:43:22 -080048import zuul.rpclistener
49import zuul.rpcclient
James E. Blair1f4c2bb2013-04-26 08:40:46 -070050import zuul.launcher.gearman
Joshua Hesketh36c3fa52014-01-22 11:40:52 +110051import zuul.lib.swift
James E. Blair4076e2b2014-01-28 12:42:20 -080052import zuul.merger.server
53import zuul.merger.client
Joshua Hesketh1879cf72013-08-19 14:13:15 +100054import zuul.reporter.gerrit
Joshua Hesketh5fea8672013-08-19 17:32:01 +100055import zuul.reporter.smtp
James E. Blairb0fcae42012-07-17 11:12:10 -070056import zuul.trigger.gerrit
James E. Blair63bb0ef2013-07-29 17:14:51 -070057import zuul.trigger.timer
James E. Blairb0fcae42012-07-17 11:12:10 -070058
59FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
60 'fixtures')
61CONFIG = ConfigParser.ConfigParser()
62CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
63
64CONFIG.set('zuul', 'layout_config',
65 os.path.join(FIXTURE_DIR, "layout.yaml"))
66
James E. Blair1f4c2bb2013-04-26 08:40:46 -070067logging.basicConfig(level=logging.DEBUG,
68 format='%(asctime)s %(name)-32s '
69 '%(levelname)-8s %(message)s')
James E. Blairb0fcae42012-07-17 11:12:10 -070070
71
Monty Taylorbc758832013-06-17 17:22:42 -040072def repack_repo(path):
James E. Blairac2c3242014-01-24 13:38:51 -080073 cmd = ['git', '--git-dir=%s/.git' % path, 'repack', '-afd']
74 output = subprocess.Popen(cmd, close_fds=True,
75 stdout=subprocess.PIPE,
76 stderr=subprocess.PIPE)
Monty Taylorbc758832013-06-17 17:22:42 -040077 out = output.communicate()
78 if output.returncode:
79 raise Exception("git repack returned %d" % output.returncode)
80 return out
81
82
James E. Blair8cc15a82012-08-01 11:17:57 -070083def random_sha1():
84 return hashlib.sha1(str(random.random())).hexdigest()
85
86
James E. Blair4886cc12012-07-18 15:39:41 -070087class ChangeReference(git.Reference):
88 _common_path_default = "refs/changes"
89 _points_to_commits_only = True
90
91
James E. Blairb0fcae42012-07-17 11:12:10 -070092class FakeChange(object):
James E. Blair8c803f82012-07-31 16:25:42 -070093 categories = {'APRV': ('Approved', -1, 1),
94 'CRVW': ('Code-Review', -2, 2),
95 'VRFY': ('Verified', -2, 2)}
James E. Blairb0fcae42012-07-17 11:12:10 -070096
Monty Taylorbc758832013-06-17 17:22:42 -040097 def __init__(self, gerrit, number, project, branch, subject,
98 status='NEW', upstream_root=None):
James E. Blair8cc15a82012-08-01 11:17:57 -070099 self.gerrit = gerrit
James E. Blaird466dc42012-07-31 10:42:56 -0700100 self.reported = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700101 self.queried = 0
James E. Blairb0fcae42012-07-17 11:12:10 -0700102 self.patchsets = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700103 self.number = number
104 self.project = project
105 self.branch = branch
106 self.subject = subject
107 self.latest_patchset = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700108 self.depends_on_change = None
109 self.needed_by_changes = []
James E. Blair127bc182012-08-28 15:55:15 -0700110 self.fail_merge = False
James E. Blair42f74822013-05-14 15:18:03 -0700111 self.messages = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700112 self.data = {
113 'branch': branch,
114 'comments': [],
115 'commitMessage': subject,
116 'createdOn': time.time(),
James E. Blair8cc15a82012-08-01 11:17:57 -0700117 'id': 'I' + random_sha1(),
James E. Blairb0fcae42012-07-17 11:12:10 -0700118 'lastUpdated': time.time(),
119 'number': str(number),
120 'open': True,
121 'owner': {'email': 'user@example.com',
122 'name': 'User Name',
123 'username': 'username'},
124 'patchSets': self.patchsets,
125 'project': project,
126 'status': status,
127 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700128 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700129 'url': 'https://hostname/%s' % number}
130
Monty Taylorbc758832013-06-17 17:22:42 -0400131 self.upstream_root = upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700132 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700133 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700134
Monty Taylorbc758832013-06-17 17:22:42 -0400135 def add_fake_change_to_repo(self, msg, fn, large):
136 path = os.path.join(self.upstream_root, self.project)
137 repo = git.Repo(path)
138 ref = ChangeReference.create(repo, '1/%s/%s' % (self.number,
139 self.latest_patchset),
140 'refs/tags/init')
141 repo.head.reference = ref
142 repo.head.reset(index=True, working_tree=True)
143 repo.git.clean('-x', '-f', '-d')
144
145 path = os.path.join(self.upstream_root, self.project)
146 if not large:
147 fn = os.path.join(path, fn)
148 f = open(fn, 'w')
149 f.write("test %s %s %s\n" %
150 (self.branch, self.number, self.latest_patchset))
151 f.close()
152 repo.index.add([fn])
153 else:
154 for fni in range(100):
155 fn = os.path.join(path, str(fni))
156 f = open(fn, 'w')
157 for ci in range(4096):
158 f.write(random.choice(string.printable))
159 f.close()
160 repo.index.add([fn])
161
James E. Blair287c06d2013-07-24 10:39:30 -0700162 r = repo.index.commit(msg)
163 repo.head.reference = 'master'
164 repo.head.reset(index=True, working_tree=True)
165 repo.git.clean('-x', '-f', '-d')
James E. Blairac2c3242014-01-24 13:38:51 -0800166 repo.heads['master'].checkout()
James E. Blair287c06d2013-07-24 10:39:30 -0700167 return r
Monty Taylorbc758832013-06-17 17:22:42 -0400168
James E. Blair70c71582013-03-06 08:50:50 -0800169 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700170 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700171 if files:
172 fn = files[0]
173 else:
174 fn = '%s-%s' % (self.branch, self.number)
175 msg = self.subject + '-' + str(self.latest_patchset)
Monty Taylorbc758832013-06-17 17:22:42 -0400176 c = self.add_fake_change_to_repo(msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800177 ps_files = [{'file': '/COMMIT_MSG',
178 'type': 'ADDED'},
179 {'file': 'README',
180 'type': 'MODIFIED'}]
181 for f in files:
182 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700183 d = {'approvals': [],
184 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800185 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700186 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700187 'ref': 'refs/changes/1/%s/%s' % (self.number,
188 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700189 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700190 'uploader': {'email': 'user@example.com',
191 'name': 'User name',
192 'username': 'user'}}
193 self.data['currentPatchSet'] = d
194 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700195 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700196
James E. Blaire0487072012-08-29 17:38:31 -0700197 def getPatchsetCreatedEvent(self, patchset):
198 event = {"type": "patchset-created",
199 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800200 "branch": self.branch,
201 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
202 "number": str(self.number),
203 "subject": self.subject,
204 "owner": {"name": "User Name"},
205 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700206 "patchSet": self.patchsets[patchset - 1],
207 "uploader": {"name": "User Name"}}
208 return event
209
James E. Blair42f74822013-05-14 15:18:03 -0700210 def getChangeRestoredEvent(self):
211 event = {"type": "change-restored",
212 "change": {"project": self.project,
213 "branch": self.branch,
214 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
215 "number": str(self.number),
216 "subject": self.subject,
217 "owner": {"name": "User Name"},
218 "url": "https://hostname/3"},
219 "restorer": {"name": "User Name"},
220 "reason": ""}
221 return event
222
James E. Blairc053d022014-01-22 14:57:33 -0800223 def addApproval(self, category, value, username='jenkins',
224 granted_on=None):
225 if not granted_on:
226 granted_on = time.time()
James E. Blair8c803f82012-07-31 16:25:42 -0700227 approval = {'description': self.categories[category][0],
228 'type': category,
James E. Blairc053d022014-01-22 14:57:33 -0800229 'value': str(value),
James E. Blair64ff4ef2014-01-24 13:50:23 -0800230 'by': {
231 'username': username,
232 'email': username + '@example.com',
233 },
James E. Blairc053d022014-01-22 14:57:33 -0800234 'grantedOn': int(granted_on)}
235 for i, x in enumerate(self.patchsets[-1]['approvals'][:]):
James E. Blair64ff4ef2014-01-24 13:50:23 -0800236 if x['by']['username'] == username and x['type'] == category:
James E. Blairc053d022014-01-22 14:57:33 -0800237 del self.patchsets[-1]['approvals'][i]
James E. Blair8c803f82012-07-31 16:25:42 -0700238 self.patchsets[-1]['approvals'].append(approval)
239 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700240 'author': {'email': 'user@example.com',
241 'name': 'User Name',
242 'username': 'username'},
243 'change': {'branch': self.branch,
244 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
245 'number': str(self.number),
246 'owner': {'email': 'user@example.com',
247 'name': 'User Name',
248 'username': 'username'},
249 'project': self.project,
250 'subject': self.subject,
251 'topic': 'master',
252 'url': 'https://hostname/459'},
253 'comment': '',
254 'patchSet': self.patchsets[-1],
255 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700256 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700257 return json.loads(json.dumps(event))
258
James E. Blair8c803f82012-07-31 16:25:42 -0700259 def getSubmitRecords(self):
260 status = {}
261 for cat in self.categories.keys():
262 status[cat] = 0
263
264 for a in self.patchsets[-1]['approvals']:
265 cur = status[a['type']]
266 cat_min, cat_max = self.categories[a['type']][1:]
267 new = int(a['value'])
268 if new == cat_min:
269 cur = new
270 elif abs(new) > abs(cur):
271 cur = new
272 status[a['type']] = cur
273
274 labels = []
275 ok = True
276 for typ, cat in self.categories.items():
277 cur = status[typ]
278 cat_min, cat_max = cat[1:]
279 if cur == cat_min:
280 value = 'REJECT'
281 ok = False
282 elif cur == cat_max:
283 value = 'OK'
284 else:
285 value = 'NEED'
286 ok = False
287 labels.append({'label': cat[0], 'status': value})
288 if ok:
289 return [{'status': 'OK'}]
290 return [{'status': 'NOT_READY',
291 'labels': labels}]
292
293 def setDependsOn(self, other, patchset):
294 self.depends_on_change = other
295 d = {'id': other.data['id'],
296 'number': other.data['number'],
297 'ref': other.patchsets[patchset - 1]['ref']
298 }
299 self.data['dependsOn'] = [d]
300
301 other.needed_by_changes.append(self)
302 needed = other.data.get('neededBy', [])
303 d = {'id': self.data['id'],
304 'number': self.data['number'],
305 'ref': self.patchsets[patchset - 1]['ref'],
306 'revision': self.patchsets[patchset - 1]['revision']
307 }
308 needed.append(d)
309 other.data['neededBy'] = needed
310
James E. Blairb0fcae42012-07-17 11:12:10 -0700311 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700312 self.queried += 1
313 d = self.data.get('dependsOn')
314 if d:
315 d = d[0]
316 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
317 d['isCurrentPatchSet'] = True
318 else:
319 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700320 return json.loads(json.dumps(self.data))
321
322 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800323 if (self.depends_on_change and
324 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700325 return
James E. Blair127bc182012-08-28 15:55:15 -0700326 if self.fail_merge:
327 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700328 self.data['status'] = 'MERGED'
329 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700330
Monty Taylorbc758832013-06-17 17:22:42 -0400331 path = os.path.join(self.upstream_root, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700332 repo = git.Repo(path)
333 repo.heads[self.branch].commit = \
334 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700335
James E. Blaird466dc42012-07-31 10:42:56 -0700336 def setReported(self):
337 self.reported += 1
338
James E. Blairb0fcae42012-07-17 11:12:10 -0700339
340class FakeGerrit(object):
341 def __init__(self, *args, **kw):
342 self.event_queue = Queue.Queue()
343 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
344 self.change_number = 0
345 self.changes = {}
346
347 def addFakeChange(self, project, branch, subject):
348 self.change_number += 1
Monty Taylorbc758832013-06-17 17:22:42 -0400349 c = FakeChange(self, self.change_number, project, branch, subject,
350 upstream_root=self.upstream_root)
James E. Blairb0fcae42012-07-17 11:12:10 -0700351 self.changes[self.change_number] = c
352 return c
353
354 def addEvent(self, data):
355 return self.event_queue.put(data)
356
357 def getEvent(self):
358 return self.event_queue.get()
359
360 def eventDone(self):
361 self.event_queue.task_done()
362
363 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700364 number, ps = changeid.split(',')
365 change = self.changes[int(number)]
James E. Blair42f74822013-05-14 15:18:03 -0700366 change.messages.append(message)
James E. Blairb0fcae42012-07-17 11:12:10 -0700367 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700368 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700369 if message:
370 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700371
372 def query(self, number):
James E. Blairad28e912013-11-27 10:43:22 -0800373 change = self.changes.get(int(number))
374 if change:
375 return change.query()
376 return {}
James E. Blairb0fcae42012-07-17 11:12:10 -0700377
378 def startWatching(self, *args, **kw):
379 pass
380
381
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700382class BuildHistory(object):
383 def __init__(self, **kw):
384 self.__dict__.update(kw)
James E. Blairb0fcae42012-07-17 11:12:10 -0700385
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700386 def __repr__(self):
387 return ("<Completed build, result: %s name: %s #%s changes: %s>" %
388 (self.result, self.name, self.number, self.changes))
James E. Blairb0fcae42012-07-17 11:12:10 -0700389
390
James E. Blair8cc15a82012-08-01 11:17:57 -0700391class FakeURLOpener(object):
Monty Taylorbc758832013-06-17 17:22:42 -0400392 def __init__(self, upstream_root, fake_gerrit, url):
393 self.upstream_root = upstream_root
James E. Blair8cc15a82012-08-01 11:17:57 -0700394 self.fake_gerrit = fake_gerrit
395 self.url = url
396
397 def read(self):
398 res = urlparse.urlparse(self.url)
399 path = res.path
400 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200401 ret = '001e# service=git-upload-pack\n'
402 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
403 'multi_ack thin-pack side-band side-band-64k ofs-delta '
404 'shallow no-progress include-tag multi_ack_detailed no-done\n')
Monty Taylorbc758832013-06-17 17:22:42 -0400405 path = os.path.join(self.upstream_root, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700406 repo = git.Repo(path)
407 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200408 r = ref.object.hexsha + ' ' + ref.path + '\n'
409 ret += '%04x%s' % (len(r) + 4, r)
410 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700411 return ret
412
413
James E. Blair4886cc12012-07-18 15:39:41 -0700414class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000415 name = 'gerrit'
416
Monty Taylorbc758832013-06-17 17:22:42 -0400417 def __init__(self, upstream_root, *args):
418 super(FakeGerritTrigger, self).__init__(*args)
419 self.upstream_root = upstream_root
420
James E. Blair4886cc12012-07-18 15:39:41 -0700421 def getGitUrl(self, project):
Monty Taylorbc758832013-06-17 17:22:42 -0400422 return os.path.join(self.upstream_root, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700423
424
James E. Blair412e5582013-04-22 15:50:12 -0700425class FakeStatsd(threading.Thread):
426 def __init__(self):
427 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400428 self.daemon = True
James E. Blair412e5582013-04-22 15:50:12 -0700429 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
430 self.sock.bind(('', 0))
431 self.port = self.sock.getsockname()[1]
432 self.wake_read, self.wake_write = os.pipe()
433 self.stats = []
434
435 def run(self):
436 while True:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700437 poll = select.poll()
438 poll.register(self.sock, select.POLLIN)
439 poll.register(self.wake_read, select.POLLIN)
440 ret = poll.poll()
441 for (fd, event) in ret:
442 if fd == self.sock.fileno():
James E. Blair412e5582013-04-22 15:50:12 -0700443 data = self.sock.recvfrom(1024)
444 if not data:
445 return
446 self.stats.append(data[0])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700447 if fd == self.wake_read:
James E. Blair412e5582013-04-22 15:50:12 -0700448 return
449
450 def stop(self):
451 os.write(self.wake_write, '1\n')
452
453
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700454class FakeBuild(threading.Thread):
455 log = logging.getLogger("zuul.test")
456
457 def __init__(self, worker, job, number, node):
458 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400459 self.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700460 self.worker = worker
461 self.job = job
462 self.name = job.name.split(':')[1]
463 self.number = number
464 self.node = node
465 self.parameters = json.loads(job.arguments)
466 self.unique = self.parameters['ZUUL_UUID']
467 self.wait_condition = threading.Condition()
468 self.waiting = False
469 self.aborted = False
470 self.created = time.time()
471 self.description = ''
James E. Blair4a28a882013-08-23 15:17:33 -0700472 self.run_error = False
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700473
474 def release(self):
475 self.wait_condition.acquire()
476 self.wait_condition.notify()
477 self.waiting = False
478 self.log.debug("Build %s released" % self.unique)
479 self.wait_condition.release()
480
481 def isWaiting(self):
482 self.wait_condition.acquire()
483 if self.waiting:
484 ret = True
485 else:
486 ret = False
487 self.wait_condition.release()
488 return ret
489
490 def _wait(self):
491 self.wait_condition.acquire()
492 self.waiting = True
493 self.log.debug("Build %s waiting" % self.unique)
494 self.wait_condition.wait()
495 self.wait_condition.release()
496
497 def run(self):
498 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700499 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
500 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700501 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700502 'manager': self.worker.worker_id,
Joshua Heskethba8776a2014-01-12 14:35:40 +0800503 'worker_name': 'My Worker',
504 'worker_hostname': 'localhost',
505 'worker_ips': ['127.0.0.1', '192.168.1.1'],
506 'worker_fqdn': 'zuul.example.org',
507 'worker_program': 'FakeBuilder',
508 'worker_version': 'v1.1',
509 'worker_extra': {'something': 'else'}
Paul Belangerec49b4c2013-07-20 20:32:20 -0400510 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700511
Joshua Heskethba8776a2014-01-12 14:35:40 +0800512 self.log.debug('Running build %s' % self.unique)
513
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700514 self.job.sendWorkData(json.dumps(data))
Joshua Heskethba8776a2014-01-12 14:35:40 +0800515 self.log.debug('Sent WorkData packet with %s' % json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700516 self.job.sendWorkStatus(0, 100)
517
518 if self.worker.hold_jobs_in_build:
Joshua Heskethba8776a2014-01-12 14:35:40 +0800519 self.log.debug('Holding build %s' % self.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700520 self._wait()
521 self.log.debug("Build %s continuing" % self.unique)
522
523 self.worker.lock.acquire()
524
525 result = 'SUCCESS'
526 if (('ZUUL_REF' in self.parameters) and
527 self.worker.shouldFailTest(self.name,
528 self.parameters['ZUUL_REF'])):
529 result = 'FAILURE'
530 if self.aborted:
531 result = 'ABORTED'
532
James E. Blair4a28a882013-08-23 15:17:33 -0700533 if self.run_error:
534 work_fail = True
535 result = 'RUN_ERROR'
536 else:
537 data['result'] = result
538 work_fail = False
539
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700540 changes = None
541 if 'ZUUL_CHANGE_IDS' in self.parameters:
542 changes = self.parameters['ZUUL_CHANGE_IDS']
543
544 self.worker.build_history.append(
545 BuildHistory(name=self.name, number=self.number,
546 result=result, changes=changes, node=self.node,
James E. Blair64ed6f22013-07-10 14:07:23 -0700547 uuid=self.unique, description=self.description,
548 pipeline=self.parameters['ZUUL_PIPELINE'])
Paul Belangerec49b4c2013-07-20 20:32:20 -0400549 )
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700550
James E. Blair4a28a882013-08-23 15:17:33 -0700551 self.job.sendWorkData(json.dumps(data))
552 if work_fail:
553 self.job.sendWorkFail()
554 else:
555 self.job.sendWorkComplete(json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700556 del self.worker.gearman_jobs[self.job.unique]
557 self.worker.running_builds.remove(self)
558 self.worker.lock.release()
559
560
561class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400562 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700563 super(FakeWorker, self).__init__(worker_id)
564 self.gearman_jobs = {}
565 self.build_history = []
566 self.running_builds = []
567 self.build_counter = 0
568 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400569 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700570
571 self.hold_jobs_in_build = False
572 self.lock = threading.Lock()
573 self.__work_thread = threading.Thread(target=self.work)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400574 self.__work_thread.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700575 self.__work_thread.start()
576
577 def handleJob(self, job):
578 parts = job.name.split(":")
579 cmd = parts[0]
580 name = parts[1]
581 if len(parts) > 2:
582 node = parts[2]
583 else:
584 node = None
585 if cmd == 'build':
586 self.handleBuild(job, name, node)
587 elif cmd == 'stop':
588 self.handleStop(job, name)
589 elif cmd == 'set_description':
590 self.handleSetDescription(job, name)
591
592 def handleBuild(self, job, name, node):
593 build = FakeBuild(self, job, self.build_counter, node)
594 job.build = build
595 self.gearman_jobs[job.unique] = job
596 self.build_counter += 1
597
598 self.running_builds.append(build)
599 build.start()
600
601 def handleStop(self, job, name):
602 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700603 parameters = json.loads(job.arguments)
604 name = parameters['name']
605 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700606 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700607 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700608 build.aborted = True
609 build.release()
610 job.sendWorkComplete()
611 return
612 job.sendWorkFail()
613
614 def handleSetDescription(self, job, name):
615 self.log.debug("handle set description")
616 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700617 name = parameters['name']
618 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700619 descr = parameters['html_description']
620 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700621 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700622 build.description = descr
623 job.sendWorkComplete()
624 return
625 for build in self.build_history:
James E. Blair3c483cf2013-06-04 16:30:43 -0700626 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700627 build.description = descr
628 job.sendWorkComplete()
629 return
630 job.sendWorkFail()
631
632 def work(self):
633 while self.running:
634 try:
635 job = self.getJob()
636 except gear.InterruptedError:
637 continue
638 try:
639 self.handleJob(job)
640 except:
641 self.log.exception("Worker exception:")
642
643 def addFailTest(self, name, change):
644 l = self.fail_tests.get(name, [])
645 l.append(change)
646 self.fail_tests[name] = l
647
648 def shouldFailTest(self, name, ref):
649 l = self.fail_tests.get(name, [])
650 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400651 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700652 return True
653 return False
654
655 def release(self, regex=None):
656 builds = self.running_builds[:]
657 self.log.debug("releasing build %s (%s)" % (regex,
James E. Blair78e31b32013-07-09 09:11:34 -0700658 len(self.running_builds)))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700659 for build in builds:
660 if not regex or re.match(regex, build.name):
661 self.log.debug("releasing build %s" %
662 (build.parameters['ZUUL_UUID']))
663 build.release()
664 else:
665 self.log.debug("not releasing build %s" %
666 (build.parameters['ZUUL_UUID']))
667 self.log.debug("done releasing builds %s (%s)" %
668 (regex, len(self.running_builds)))
669
670
671class FakeGearmanServer(gear.Server):
672 def __init__(self):
673 self.hold_jobs_in_queue = False
674 super(FakeGearmanServer, self).__init__(0)
675
676 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700677 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
678 for job in queue:
679 if not hasattr(job, 'waiting'):
680 if job.name.startswith('build:'):
681 job.waiting = self.hold_jobs_in_queue
682 else:
683 job.waiting = False
684 if job.waiting:
685 continue
686 if job.name in connection.functions:
687 if not peek:
688 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400689 connection.related_jobs[job.handle] = job
690 job.worker_connection = connection
691 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700692 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700693 return None
694
695 def release(self, regex=None):
696 released = False
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("releasing queued job %s (%s)" % (regex, qlen))
James E. Blairdda6c912013-07-29 14:12:12 -0700700 for job in self.getQueue():
701 cmd, name = job.name.split(':')
702 if cmd != 'build':
703 continue
704 if not regex or re.match(regex, name):
705 self.log.debug("releasing queued job %s" %
706 job.unique)
707 job.waiting = False
708 released = True
709 else:
710 self.log.debug("not releasing queued job %s" %
711 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700712 if released:
713 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700714 qlen = (len(self.high_queue) + len(self.normal_queue) +
715 len(self.low_queue))
716 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700717
718
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000719class FakeSMTP(object):
720 log = logging.getLogger('zuul.FakeSMTP')
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000721
James E. Blairff80a2f2013-12-27 13:24:06 -0800722 def __init__(self, messages, server, port):
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000723 self.server = server
724 self.port = port
James E. Blairff80a2f2013-12-27 13:24:06 -0800725 self.messages = messages
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000726
727 def sendmail(self, from_email, to_email, msg):
728 self.log.info("Sending email from %s, to %s, with msg %s" % (
729 from_email, to_email, msg))
730
731 headers = msg.split('\n\n', 1)[0]
732 body = msg.split('\n\n', 1)[1]
733
James E. Blairff80a2f2013-12-27 13:24:06 -0800734 self.messages.append(dict(
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000735 from_email=from_email,
736 to_email=to_email,
737 msg=msg,
738 headers=headers,
739 body=body,
740 ))
741
742 return True
743
744 def quit(self):
745 return True
746
747
Joshua Hesketh36c3fa52014-01-22 11:40:52 +1100748class FakeSwiftClientConnection(swiftclient.client.Connection):
749 def post_account(self, headers):
750 # Do nothing
751 pass
752
753 def get_auth(self):
754 # Returns endpoint and (unused) auth token
755 endpoint = os.path.join('https://storage.example.org', 'V1',
756 'AUTH_account')
757 return endpoint, ''
758
759
Monty Taylorbc758832013-06-17 17:22:42 -0400760class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700761 log = logging.getLogger("zuul.test")
762
763 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400764 super(TestScheduler, self).setUp()
765 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
766 try:
767 test_timeout = int(test_timeout)
768 except ValueError:
769 # If timeout value is invalid do not set a timeout.
770 test_timeout = 0
771 if test_timeout > 0:
James E. Blairff80a2f2013-12-27 13:24:06 -0800772 self.useFixture(fixtures.Timeout(test_timeout, gentle=False))
Monty Taylorbc758832013-06-17 17:22:42 -0400773
774 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
775 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
776 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
777 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
778 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
779 os.environ.get('OS_STDERR_CAPTURE') == '1'):
780 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
781 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Monty Taylore6a6c402013-07-02 09:25:55 -0700782 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
783 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400784 self.useFixture(fixtures.FakeLogger(
785 level=logging.DEBUG,
786 format='%(asctime)s %(name)-32s '
787 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700788 tmp_root = self.useFixture(fixtures.TempDir(
789 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400790 self.test_root = os.path.join(tmp_root, "zuul-test")
791 self.upstream_root = os.path.join(self.test_root, "upstream")
792 self.git_root = os.path.join(self.test_root, "git")
793
James E. Blair4076e2b2014-01-28 12:42:20 -0800794 CONFIG.set('merger', 'git_dir', self.git_root)
Monty Taylorbc758832013-06-17 17:22:42 -0400795 if os.path.exists(self.test_root):
796 shutil.rmtree(self.test_root)
797 os.makedirs(self.test_root)
798 os.makedirs(self.upstream_root)
799 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700800
801 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400802 self.init_repo("org/project")
803 self.init_repo("org/project1")
804 self.init_repo("org/project2")
805 self.init_repo("org/project3")
806 self.init_repo("org/one-job-project")
807 self.init_repo("org/nonvoting-project")
808 self.init_repo("org/templated-project")
James E. Blair3e98c022013-12-16 15:25:38 -0800809 self.init_repo("org/layered-project")
Monty Taylorbc758832013-06-17 17:22:42 -0400810 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700811 self.init_repo("org/conflict-project")
James E. Blair18c64442014-03-18 10:14:45 -0700812 self.init_repo("org/noop-project")
James E. Blair412e5582013-04-22 15:50:12 -0700813
814 self.statsd = FakeStatsd()
815 os.environ['STATSD_HOST'] = 'localhost'
816 os.environ['STATSD_PORT'] = str(self.statsd.port)
817 self.statsd.start()
818 # the statsd client object is configured in the statsd module import
819 reload(statsd)
820 reload(zuul.scheduler)
821
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700822 self.gearman_server = FakeGearmanServer()
823
824 self.config = ConfigParser.ConfigParser()
825 cfg = StringIO()
826 CONFIG.write(cfg)
827 cfg.seek(0)
828 self.config.readfp(cfg)
829 self.config.set('gearman', 'port', str(self.gearman_server.port))
830
Monty Taylorbc758832013-06-17 17:22:42 -0400831 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700832 self.worker.addServer('127.0.0.1', self.gearman_server.port)
833 self.gearman_server.worker = self.worker
834
James E. Blair4076e2b2014-01-28 12:42:20 -0800835 self.merge_server = zuul.merger.server.MergeServer(self.config)
836 self.merge_server.start()
837
James E. Blairb0fcae42012-07-17 11:12:10 -0700838 self.sched = zuul.scheduler.Scheduler()
839
Joshua Hesketh36c3fa52014-01-22 11:40:52 +1100840 self.useFixture(fixtures.MonkeyPatch('swiftclient.client.Connection',
841 FakeSwiftClientConnection))
842 self.swift = zuul.lib.swift.Swift(self.config)
843
James E. Blair8cc15a82012-08-01 11:17:57 -0700844 def URLOpenerFactory(*args, **kw):
845 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400846 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700847
James E. Blair8cc15a82012-08-01 11:17:57 -0700848 urllib2.urlopen = URLOpenerFactory
Joshua Hesketh36c3fa52014-01-22 11:40:52 +1100849
850 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched,
851 self.swift)
James E. Blair4076e2b2014-01-28 12:42:20 -0800852 self.merge_client = zuul.merger.client.MergeClient(
853 self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700854
James E. Blairff80a2f2013-12-27 13:24:06 -0800855 self.smtp_messages = []
856
857 def FakeSMTPFactory(*args, **kw):
858 args = [self.smtp_messages] + list(args)
859 return FakeSMTP(*args, **kw)
860
James E. Blairb0fcae42012-07-17 11:12:10 -0700861 zuul.lib.gerrit.Gerrit = FakeGerrit
James E. Blairff80a2f2013-12-27 13:24:06 -0800862 self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTPFactory))
James E. Blairb0fcae42012-07-17 11:12:10 -0700863
Monty Taylorbc758832013-06-17 17:22:42 -0400864 self.gerrit = FakeGerritTrigger(
865 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700866 self.gerrit.replication_timeout = 1.5
867 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700868 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400869 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700870
James E. Blair1843a552013-07-03 14:19:52 -0700871 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
James E. Blairad28e912013-11-27 10:43:22 -0800872 self.rpc = zuul.rpclistener.RPCListener(self.config, self.sched)
James E. Blair1843a552013-07-03 14:19:52 -0700873
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700874 self.sched.setLauncher(self.launcher)
James E. Blair4076e2b2014-01-28 12:42:20 -0800875 self.sched.setMerger(self.merge_client)
James E. Blair6c358e72013-07-29 17:06:47 -0700876 self.sched.registerTrigger(self.gerrit)
James E. Blair63bb0ef2013-07-29 17:14:51 -0700877 self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
878 self.sched.registerTrigger(self.timer)
James E. Blairb0fcae42012-07-17 11:12:10 -0700879
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000880 self.sched.registerReporter(
881 zuul.reporter.gerrit.Reporter(self.gerrit))
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000882 self.smtp_reporter = zuul.reporter.smtp.Reporter(
883 self.config.get('smtp', 'default_from'),
884 self.config.get('smtp', 'default_to'),
885 self.config.get('smtp', 'server'))
886 self.sched.registerReporter(self.smtp_reporter)
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000887
James E. Blairb0fcae42012-07-17 11:12:10 -0700888 self.sched.start()
889 self.sched.reconfigure(self.config)
890 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700891 self.webapp.start()
James E. Blairad28e912013-11-27 10:43:22 -0800892 self.rpc.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700893 self.launcher.gearman.waitForServer()
894 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400895 self.builds = self.worker.running_builds
896 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700897
James E. Blairfee8d652013-06-07 08:57:52 -0700898 self.addCleanup(self.assertFinalState)
899 self.addCleanup(self.shutdown)
900
901 def assertFinalState(self):
902 # Make sure that the change cache is cleared
James E. Blair6c358e72013-07-29 17:06:47 -0700903 self.assertEqual(len(self.gerrit._change_cache.keys()), 0)
Clark Boylan4ba48d92013-11-11 18:03:53 -0800904 # Make sure that git.Repo objects have been garbage collected.
905 repos = []
906 gc.collect()
907 for obj in gc.get_objects():
908 if isinstance(obj, git.Repo):
909 repos.append(obj)
910 self.assertEqual(len(repos), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700911 self.assertEmptyQueues()
912
913 def shutdown(self):
914 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700915 self.launcher.stop()
James E. Blair4076e2b2014-01-28 12:42:20 -0800916 self.merge_server.stop()
917 self.merge_server.join()
918 self.merge_client.stop()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700919 self.worker.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700920 self.gerrit.stop()
James E. Blair63bb0ef2013-07-29 17:14:51 -0700921 self.timer.stop()
James E. Blairb0fcae42012-07-17 11:12:10 -0700922 self.sched.stop()
923 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700924 self.statsd.stop()
925 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700926 self.webapp.stop()
927 self.webapp.join()
James E. Blairad28e912013-11-27 10:43:22 -0800928 self.rpc.stop()
929 self.rpc.join()
James E. Blair491b3172014-03-10 15:05:36 -0700930 self.gearman_server.shutdown()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700931 threads = threading.enumerate()
932 if len(threads) > 1:
933 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400934 super(TestScheduler, self).tearDown()
935
936 def init_repo(self, project):
937 parts = project.split('/')
938 path = os.path.join(self.upstream_root, *parts[:-1])
939 if not os.path.exists(path):
940 os.makedirs(path)
941 path = os.path.join(self.upstream_root, project)
942 repo = git.Repo.init(path)
943
944 repo.config_writer().set_value('user', 'email', 'user@example.com')
945 repo.config_writer().set_value('user', 'name', 'User Name')
946 repo.config_writer().write()
947
948 fn = os.path.join(path, 'README')
949 f = open(fn, 'w')
950 f.write("test\n")
951 f.close()
952 repo.index.add([fn])
953 repo.index.commit('initial commit')
954 master = repo.create_head('master')
955 repo.create_tag('init')
956
957 mp = repo.create_head('mp')
958 repo.head.reference = mp
959 f = open(fn, 'a')
960 f.write("test mp\n")
961 f.close()
962 repo.index.add([fn])
963 repo.index.commit('mp commit')
964
965 repo.head.reference = master
966 repo.head.reset(index=True, working_tree=True)
967 repo.git.clean('-x', '-f', '-d')
968
969 def ref_has_change(self, ref, change):
970 path = os.path.join(self.git_root, change.project)
971 repo = git.Repo(path)
972 for commit in repo.iter_commits(ref):
973 if commit.message.strip() == ('%s-1' % change.subject):
974 return True
975 return False
976
977 def job_has_changes(self, *args):
978 job = args[0]
979 commits = args[1:]
980 if isinstance(job, FakeBuild):
981 parameters = job.parameters
982 else:
983 parameters = json.loads(job.arguments)
984 project = parameters['ZUUL_PROJECT']
985 path = os.path.join(self.git_root, project)
986 repo = git.Repo(path)
987 ref = parameters['ZUUL_REF']
988 sha = parameters['ZUUL_COMMIT']
989 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
990 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
991 commit_messages = ['%s-1' % commit.subject for commit in commits]
James E. Blairac2c3242014-01-24 13:38:51 -0800992 self.log.debug("Checking if job %s has changes; commit_messages %s;"
993 " repo_messages %s; sha %s" % (job, commit_messages,
994 repo_messages, sha))
Monty Taylorbc758832013-06-17 17:22:42 -0400995 for msg in commit_messages:
996 if msg not in repo_messages:
James E. Blairac2c3242014-01-24 13:38:51 -0800997 self.log.debug(" messages do not match")
Monty Taylorbc758832013-06-17 17:22:42 -0400998 return False
999 if repo_shas[0] != sha:
James E. Blairac2c3242014-01-24 13:38:51 -08001000 self.log.debug(" sha does not match")
Monty Taylorbc758832013-06-17 17:22:42 -04001001 return False
James E. Blairac2c3242014-01-24 13:38:51 -08001002 self.log.debug(" OK")
Monty Taylorbc758832013-06-17 17:22:42 -04001003 return True
James E. Blairb0fcae42012-07-17 11:12:10 -07001004
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001005 def registerJobs(self):
1006 count = 0
James E. Blaireff88162013-07-01 12:44:14 -04001007 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001008 self.worker.registerFunction('build:' + job)
1009 count += 1
1010 self.worker.registerFunction('stop:' + self.worker.worker_id)
1011 count += 1
1012
1013 while len(self.gearman_server.functions) < count:
1014 time.sleep(0)
1015
1016 def release(self, job):
1017 if isinstance(job, FakeBuild):
1018 job.release()
1019 else:
1020 job.waiting = False
1021 self.log.debug("Queued job %s released" % job.unique)
1022 self.gearman_server.wakeConnections()
1023
1024 def getParameter(self, job, name):
1025 if isinstance(job, FakeBuild):
1026 return job.parameters[name]
1027 else:
1028 parameters = json.loads(job.arguments)
1029 return parameters[name]
1030
1031 def resetGearmanServer(self):
1032 self.worker.setFunctions([])
1033 while True:
1034 done = True
1035 for connection in self.gearman_server.active_connections:
James E. Blairad28e912013-11-27 10:43:22 -08001036 if (connection.functions and
James E. Blair4076e2b2014-01-28 12:42:20 -08001037 connection.client_id not in ['Zuul RPC Listener',
1038 'Zuul Merger']):
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001039 done = False
1040 if done:
1041 break
1042 time.sleep(0)
1043 self.gearman_server.functions = set()
James E. Blairad28e912013-11-27 10:43:22 -08001044 self.rpc.register()
James E. Blair4076e2b2014-01-28 12:42:20 -08001045 self.merge_server.register()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001046
1047 def haveAllBuildsReported(self):
1048 # See if Zuul is waiting on a meta job to complete
1049 if self.launcher.meta_jobs:
1050 return False
1051 # Find out if every build that the worker has completed has been
1052 # reported back to Zuul. If it hasn't then that means a Gearman
1053 # event is still in transit and the system is not stable.
1054 for build in self.worker.build_history:
1055 zbuild = self.launcher.builds.get(build.uuid)
1056 if not zbuild:
1057 # It has already been reported
1058 continue
1059 # It hasn't been reported yet.
1060 return False
1061 # Make sure that none of the worker connections are in GRAB_WAIT
1062 for connection in self.worker.active_connections:
1063 if connection.state == 'GRAB_WAIT':
1064 return False
1065 return True
1066
1067 def areAllBuildsWaiting(self):
1068 ret = True
1069
1070 builds = self.launcher.builds.values()
1071 for build in builds:
1072 client_job = None
1073 for conn in self.launcher.gearman.active_connections:
1074 for j in conn.related_jobs.values():
1075 if j.unique == build.uuid:
1076 client_job = j
1077 break
1078 if not client_job:
1079 self.log.debug("%s is not known to the gearman client" %
1080 build)
1081 ret = False
1082 continue
1083 if not client_job.handle:
1084 self.log.debug("%s has no handle" % client_job)
1085 ret = False
1086 continue
1087 server_job = self.gearman_server.jobs.get(client_job.handle)
1088 if not server_job:
1089 self.log.debug("%s is not known to the gearman server" %
1090 client_job)
1091 ret = False
1092 continue
1093 if not hasattr(server_job, 'waiting'):
1094 self.log.debug("%s is being enqueued" % server_job)
1095 ret = False
1096 continue
1097 if server_job.waiting:
1098 continue
1099 worker_job = self.worker.gearman_jobs.get(server_job.unique)
1100 if worker_job:
1101 if worker_job.build.isWaiting():
1102 continue
1103 else:
1104 self.log.debug("%s is running" % worker_job)
1105 ret = False
1106 else:
1107 self.log.debug("%s is unassigned" % server_job)
1108 ret = False
1109 return ret
1110
James E. Blairb0fcae42012-07-17 11:12:10 -07001111 def waitUntilSettled(self):
1112 self.log.debug("Waiting until settled...")
1113 start = time.time()
1114 while True:
1115 if time.time() - start > 10:
1116 print 'queue status:',
1117 print self.sched.trigger_event_queue.empty(),
1118 print self.sched.result_event_queue.empty(),
1119 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001120 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -07001121 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001122 # Make sure no new events show up while we're checking
1123 self.worker.lock.acquire()
1124 # have all build states propogated to zuul?
1125 if self.haveAllBuildsReported():
1126 # Join ensures that the queue is empty _and_ events have been
1127 # processed
1128 self.fake_gerrit.event_queue.join()
1129 self.sched.trigger_event_queue.join()
1130 self.sched.result_event_queue.join()
James E. Blaira84f0e42014-02-06 07:09:22 -08001131 self.sched.run_handler_lock.acquire()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001132 if (self.sched.trigger_event_queue.empty() and
1133 self.sched.result_event_queue.empty() and
1134 self.fake_gerrit.event_queue.empty() and
James E. Blair4076e2b2014-01-28 12:42:20 -08001135 not self.merge_client.build_sets and
James E. Blair9c2985a2014-03-10 10:59:05 -07001136 self.haveAllBuildsReported() and
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001137 self.areAllBuildsWaiting()):
James E. Blaira84f0e42014-02-06 07:09:22 -08001138 self.sched.run_handler_lock.release()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001139 self.worker.lock.release()
1140 self.log.debug("...settled.")
1141 return
James E. Blaira84f0e42014-02-06 07:09:22 -08001142 self.sched.run_handler_lock.release()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001143 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001144 self.sched.wake_event.wait(0.1)
1145
James E. Blaird466dc42012-07-31 10:42:56 -07001146 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -08001147 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -07001148 return len(jobs)
1149
James E. Blair4ca985f2013-05-30 12:27:43 -07001150 def getJobFromHistory(self, name):
1151 history = self.worker.build_history
1152 for job in history:
1153 if job.name == name:
1154 return job
1155 raise Exception("Unable to find job %s in history" % name)
1156
James E. Blaire0487072012-08-29 17:38:31 -07001157 def assertEmptyQueues(self):
1158 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001159 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001160 for queue in pipeline.queues:
1161 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001162 print 'pipeline %s queue %s contents %s' % (
1163 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001164 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001165
James E. Blair66eeebf2013-07-27 17:44:32 -07001166 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001167 start = time.time()
1168 while time.time() < (start + 5):
1169 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001170 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001171 k, v = stat.split(':')
1172 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001173 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001174 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001175 elif value:
1176 if value == v:
1177 return
1178 elif kind:
1179 if v.endswith('|' + kind):
1180 return
James E. Blair412e5582013-04-22 15:50:12 -07001181 time.sleep(0.1)
1182
1183 pprint.pprint(self.statsd.stats)
1184 raise Exception("Key %s not found in reported stats" % key)
1185
James E. Blairb0fcae42012-07-17 11:12:10 -07001186 def test_jobs_launched(self):
1187 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001188
James E. Blairb0fcae42012-07-17 11:12:10 -07001189 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001190 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001191 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1192 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001193 self.assertEqual(self.getJobFromHistory('project-merge').result,
1194 'SUCCESS')
1195 self.assertEqual(self.getJobFromHistory('project-test1').result,
1196 'SUCCESS')
1197 self.assertEqual(self.getJobFromHistory('project-test2').result,
1198 'SUCCESS')
1199 self.assertEqual(A.data['status'], 'MERGED')
1200 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001201
James E. Blair66eeebf2013-07-27 17:44:32 -07001202 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1203 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1204 value='1|g')
1205 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1206 kind='ms')
1207 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1208 value='1|c')
1209 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1210 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1211 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001212 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001213 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001214 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001215 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001216
James E. Blair3cb10702013-08-24 08:56:03 -07001217 def test_initial_pipeline_gauges(self):
1218 "Test that each pipeline reported its length on start"
1219 pipeline_names = self.sched.layout.pipelines.keys()
1220 self.assertNotEqual(len(pipeline_names), 0)
1221 for name in pipeline_names:
1222 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
1223 value='0|g')
1224
James E. Blair42f74822013-05-14 15:18:03 -07001225 def test_duplicate_pipelines(self):
1226 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001227
James E. Blair42f74822013-05-14 15:18:03 -07001228 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1229 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1230 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001231
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001232 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001233 self.history[0].name == 'project-test1'
1234 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001235
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001236 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001237 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001238 self.assertIn('dup1/project-test1', A.messages[0])
1239 self.assertNotIn('dup2/project-test1', A.messages[0])
1240 self.assertNotIn('dup1/project-test1', A.messages[1])
1241 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001242 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001243 self.assertIn('dup1/project-test1', A.messages[1])
1244 self.assertNotIn('dup2/project-test1', A.messages[1])
1245 self.assertNotIn('dup1/project-test1', A.messages[0])
1246 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001247
James E. Blairb0fcae42012-07-17 11:12:10 -07001248 def test_parallel_changes(self):
1249 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001250
1251 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001252 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1253 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1254 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001255 A.addApproval('CRVW', 2)
1256 B.addApproval('CRVW', 2)
1257 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001258
1259 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1260 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1261 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1262
1263 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001264 self.assertEqual(len(self.builds), 1)
1265 self.assertEqual(self.builds[0].name, 'project-merge')
1266 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001267
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001268 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001269 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001270 self.assertEqual(len(self.builds), 3)
1271 self.assertEqual(self.builds[0].name, 'project-test1')
1272 self.assertTrue(self.job_has_changes(self.builds[0], A))
1273 self.assertEqual(self.builds[1].name, 'project-test2')
1274 self.assertTrue(self.job_has_changes(self.builds[1], A))
1275 self.assertEqual(self.builds[2].name, 'project-merge')
1276 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001277
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001278 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001279 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001280 self.assertEqual(len(self.builds), 5)
1281 self.assertEqual(self.builds[0].name, 'project-test1')
1282 self.assertTrue(self.job_has_changes(self.builds[0], A))
1283 self.assertEqual(self.builds[1].name, 'project-test2')
1284 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001285
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001286 self.assertEqual(self.builds[2].name, 'project-test1')
1287 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1288 self.assertEqual(self.builds[3].name, 'project-test2')
1289 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001290
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001291 self.assertEqual(self.builds[4].name, 'project-merge')
1292 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001293
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001294 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001295 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001296 self.assertEqual(len(self.builds), 6)
1297 self.assertEqual(self.builds[0].name, 'project-test1')
1298 self.assertTrue(self.job_has_changes(self.builds[0], A))
1299 self.assertEqual(self.builds[1].name, 'project-test2')
1300 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001301
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001302 self.assertEqual(self.builds[2].name, 'project-test1')
1303 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1304 self.assertEqual(self.builds[3].name, 'project-test2')
1305 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001306
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001307 self.assertEqual(self.builds[4].name, 'project-test1')
1308 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1309 self.assertEqual(self.builds[5].name, 'project-test2')
1310 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001311
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001312 self.worker.hold_jobs_in_build = False
1313 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001314 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001315 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001316
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001317 self.assertEqual(len(self.history), 9)
1318 self.assertEqual(A.data['status'], 'MERGED')
1319 self.assertEqual(B.data['status'], 'MERGED')
1320 self.assertEqual(C.data['status'], 'MERGED')
1321 self.assertEqual(A.reported, 2)
1322 self.assertEqual(B.reported, 2)
1323 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001324
1325 def test_failed_changes(self):
1326 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001327 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001328
James E. Blairb02a3bb2012-07-30 17:49:55 -07001329 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1330 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001331 A.addApproval('CRVW', 2)
1332 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001333
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001334 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001335
James E. Blaire2819012013-06-28 17:17:26 -04001336 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1337 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001338 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001339
1340 self.worker.release('.*-merge')
1341 self.waitUntilSettled()
1342
1343 self.worker.hold_jobs_in_build = False
1344 self.worker.release()
1345
1346 self.waitUntilSettled()
1347 # It's certain that the merge job for change 2 will run, but
1348 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001349 self.assertTrue(len(self.history) > 6)
1350 self.assertEqual(A.data['status'], 'NEW')
1351 self.assertEqual(B.data['status'], 'MERGED')
1352 self.assertEqual(A.reported, 2)
1353 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001354
1355 def test_independent_queues(self):
1356 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001357
1358 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001359 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001360 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1361 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001362 A.addApproval('CRVW', 2)
1363 B.addApproval('CRVW', 2)
1364 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001365
1366 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1367 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1368 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1369
James E. Blairb02a3bb2012-07-30 17:49:55 -07001370 self.waitUntilSettled()
1371
1372 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001373 self.assertEqual(len(self.builds), 2)
1374 self.assertEqual(self.builds[0].name, 'project-merge')
1375 self.assertTrue(self.job_has_changes(self.builds[0], A))
1376 self.assertEqual(self.builds[1].name, 'project1-merge')
1377 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001378
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001379 # Release the current merge builds
1380 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001381 self.waitUntilSettled()
1382 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001383 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001384 self.waitUntilSettled()
1385
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001386 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001387 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001388 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001389
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001390 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001391 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001392 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001393
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001394 self.assertEqual(len(self.history), 11)
1395 self.assertEqual(A.data['status'], 'MERGED')
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
1402 def test_failed_change_at_head(self):
1403 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001404
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001405 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001406 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')
James E. Blair8c803f82012-07-31 16:25:42 -07001409 A.addApproval('CRVW', 2)
1410 B.addApproval('CRVW', 2)
1411 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001412
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001413 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001414
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()
James E. Blaird466dc42012-07-31 10:42:56 -07001420
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001421 self.assertEqual(len(self.builds), 1)
1422 self.assertEqual(self.builds[0].name, 'project-merge')
1423 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001424
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001425 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001426 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001427 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001428 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001429 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001430 self.waitUntilSettled()
1431
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001432 self.assertEqual(len(self.builds), 6)
1433 self.assertEqual(self.builds[0].name, 'project-test1')
1434 self.assertEqual(self.builds[1].name, 'project-test2')
1435 self.assertEqual(self.builds[2].name, 'project-test1')
1436 self.assertEqual(self.builds[3].name, 'project-test2')
1437 self.assertEqual(self.builds[4].name, 'project-test1')
1438 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001439
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001440 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001441 self.waitUntilSettled()
1442
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001443 # project-test2, project-merge for B
1444 self.assertEqual(len(self.builds), 2)
1445 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001446
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001447 self.worker.hold_jobs_in_build = False
1448 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001449 self.waitUntilSettled()
1450
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001451 self.assertEqual(len(self.builds), 0)
1452 self.assertEqual(len(self.history), 15)
1453 self.assertEqual(A.data['status'], 'NEW')
1454 self.assertEqual(B.data['status'], 'MERGED')
1455 self.assertEqual(C.data['status'], 'MERGED')
1456 self.assertEqual(A.reported, 2)
1457 self.assertEqual(B.reported, 2)
1458 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001459
James E. Blair0aac4872013-08-23 14:02:38 -07001460 def test_failed_change_in_middle(self):
1461 "Test a failed change in the middle of the queue"
1462
1463 self.worker.hold_jobs_in_build = True
1464 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1465 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1466 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1467 A.addApproval('CRVW', 2)
1468 B.addApproval('CRVW', 2)
1469 C.addApproval('CRVW', 2)
1470
1471 self.worker.addFailTest('project-test1', B)
1472
1473 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1474 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1475 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1476
1477 self.waitUntilSettled()
1478
1479 self.worker.release('.*-merge')
1480 self.waitUntilSettled()
1481 self.worker.release('.*-merge')
1482 self.waitUntilSettled()
1483 self.worker.release('.*-merge')
1484 self.waitUntilSettled()
1485
1486 self.assertEqual(len(self.builds), 6)
1487 self.assertEqual(self.builds[0].name, 'project-test1')
1488 self.assertEqual(self.builds[1].name, 'project-test2')
1489 self.assertEqual(self.builds[2].name, 'project-test1')
1490 self.assertEqual(self.builds[3].name, 'project-test2')
1491 self.assertEqual(self.builds[4].name, 'project-test1')
1492 self.assertEqual(self.builds[5].name, 'project-test2')
1493
1494 self.release(self.builds[2])
1495 self.waitUntilSettled()
1496
James E. Blair972e3c72013-08-29 12:04:55 -07001497 # project-test1 and project-test2 for A
1498 # project-test2 for B
1499 # project-merge for C (without B)
1500 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -07001501 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1502
James E. Blair972e3c72013-08-29 12:04:55 -07001503 self.worker.release('.*-merge')
1504 self.waitUntilSettled()
1505
1506 # project-test1 and project-test2 for A
1507 # project-test2 for B
1508 # project-test1 and project-test2 for C
1509 self.assertEqual(len(self.builds), 5)
1510
James E. Blair0aac4872013-08-23 14:02:38 -07001511 items = self.sched.layout.pipelines['gate'].getAllItems()
1512 builds = items[0].current_build_set.getBuilds()
1513 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1514 self.assertEqual(self.countJobResults(builds, None), 2)
1515 builds = items[1].current_build_set.getBuilds()
1516 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1517 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1518 self.assertEqual(self.countJobResults(builds, None), 1)
1519 builds = items[2].current_build_set.getBuilds()
1520 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -07001521 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -07001522
1523 self.worker.hold_jobs_in_build = False
1524 self.worker.release()
1525 self.waitUntilSettled()
1526
1527 self.assertEqual(len(self.builds), 0)
1528 self.assertEqual(len(self.history), 12)
1529 self.assertEqual(A.data['status'], 'MERGED')
1530 self.assertEqual(B.data['status'], 'NEW')
1531 self.assertEqual(C.data['status'], 'MERGED')
1532 self.assertEqual(A.reported, 2)
1533 self.assertEqual(B.reported, 2)
1534 self.assertEqual(C.reported, 2)
1535
James E. Blaird466dc42012-07-31 10:42:56 -07001536 def test_failed_change_at_head_with_queue(self):
1537 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001538
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001539 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001540 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1541 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1542 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001543 A.addApproval('CRVW', 2)
1544 B.addApproval('CRVW', 2)
1545 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001546
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001547 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001548
1549 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1550 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1551 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1552
1553 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001554 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001555 self.assertEqual(len(self.builds), 0)
1556 self.assertEqual(len(queue), 1)
1557 self.assertEqual(queue[0].name, 'build:project-merge')
1558 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001559
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001560 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001561 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001562 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001563 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001564 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001565 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001566 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001567
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001568 self.assertEqual(len(self.builds), 0)
1569 self.assertEqual(len(queue), 6)
1570 self.assertEqual(queue[0].name, 'build:project-test1')
1571 self.assertEqual(queue[1].name, 'build:project-test2')
1572 self.assertEqual(queue[2].name, 'build:project-test1')
1573 self.assertEqual(queue[3].name, 'build:project-test2')
1574 self.assertEqual(queue[4].name, 'build:project-test1')
1575 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001576
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001577 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001578 self.waitUntilSettled()
1579
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001580 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001581 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001582 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1583 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001584
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001585 self.gearman_server.hold_jobs_in_queue = False
1586 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001587 self.waitUntilSettled()
1588
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001589 self.assertEqual(len(self.builds), 0)
1590 self.assertEqual(len(self.history), 11)
1591 self.assertEqual(A.data['status'], 'NEW')
1592 self.assertEqual(B.data['status'], 'MERGED')
1593 self.assertEqual(C.data['status'], 'MERGED')
1594 self.assertEqual(A.reported, 2)
1595 self.assertEqual(B.reported, 2)
1596 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001597
James E. Blairfef71632013-09-23 11:15:47 -07001598 def test_two_failed_changes_at_head(self):
1599 "Test that changes are reparented correctly if 2 fail at head"
1600
1601 self.worker.hold_jobs_in_build = True
1602 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1603 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1604 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1605 A.addApproval('CRVW', 2)
1606 B.addApproval('CRVW', 2)
1607 C.addApproval('CRVW', 2)
1608
1609 self.worker.addFailTest('project-test1', A)
1610 self.worker.addFailTest('project-test1', B)
1611
1612 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1613 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1614 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1615 self.waitUntilSettled()
1616
1617 self.worker.release('.*-merge')
1618 self.waitUntilSettled()
1619 self.worker.release('.*-merge')
1620 self.waitUntilSettled()
1621 self.worker.release('.*-merge')
1622 self.waitUntilSettled()
1623
1624 self.assertEqual(len(self.builds), 6)
1625 self.assertEqual(self.builds[0].name, 'project-test1')
1626 self.assertEqual(self.builds[1].name, 'project-test2')
1627 self.assertEqual(self.builds[2].name, 'project-test1')
1628 self.assertEqual(self.builds[3].name, 'project-test2')
1629 self.assertEqual(self.builds[4].name, 'project-test1')
1630 self.assertEqual(self.builds[5].name, 'project-test2')
1631
1632 self.assertTrue(self.job_has_changes(self.builds[0], A))
1633 self.assertTrue(self.job_has_changes(self.builds[2], A))
1634 self.assertTrue(self.job_has_changes(self.builds[2], B))
1635 self.assertTrue(self.job_has_changes(self.builds[4], A))
1636 self.assertTrue(self.job_has_changes(self.builds[4], B))
1637 self.assertTrue(self.job_has_changes(self.builds[4], C))
1638
1639 # Fail change B first
1640 self.release(self.builds[2])
1641 self.waitUntilSettled()
1642
1643 # restart of C after B failure
1644 self.worker.release('.*-merge')
1645 self.waitUntilSettled()
1646
1647 self.assertEqual(len(self.builds), 5)
1648 self.assertEqual(self.builds[0].name, 'project-test1')
1649 self.assertEqual(self.builds[1].name, 'project-test2')
1650 self.assertEqual(self.builds[2].name, 'project-test2')
1651 self.assertEqual(self.builds[3].name, 'project-test1')
1652 self.assertEqual(self.builds[4].name, 'project-test2')
1653
1654 self.assertTrue(self.job_has_changes(self.builds[1], A))
1655 self.assertTrue(self.job_has_changes(self.builds[2], A))
1656 self.assertTrue(self.job_has_changes(self.builds[2], B))
1657 self.assertTrue(self.job_has_changes(self.builds[4], A))
1658 self.assertFalse(self.job_has_changes(self.builds[4], B))
1659 self.assertTrue(self.job_has_changes(self.builds[4], C))
1660
1661 # Finish running all passing jobs for change A
1662 self.release(self.builds[1])
1663 self.waitUntilSettled()
1664 # Fail and report change A
1665 self.release(self.builds[0])
1666 self.waitUntilSettled()
1667
1668 # restart of B,C after A failure
1669 self.worker.release('.*-merge')
1670 self.waitUntilSettled()
1671 self.worker.release('.*-merge')
1672 self.waitUntilSettled()
1673
1674 self.assertEqual(len(self.builds), 4)
1675 self.assertEqual(self.builds[0].name, 'project-test1') # B
1676 self.assertEqual(self.builds[1].name, 'project-test2') # B
1677 self.assertEqual(self.builds[2].name, 'project-test1') # C
1678 self.assertEqual(self.builds[3].name, 'project-test2') # C
1679
1680 self.assertFalse(self.job_has_changes(self.builds[1], A))
1681 self.assertTrue(self.job_has_changes(self.builds[1], B))
1682 self.assertFalse(self.job_has_changes(self.builds[1], C))
1683
1684 self.assertFalse(self.job_has_changes(self.builds[2], A))
1685 # After A failed and B and C restarted, B should be back in
1686 # C's tests because it has not failed yet.
1687 self.assertTrue(self.job_has_changes(self.builds[2], B))
1688 self.assertTrue(self.job_has_changes(self.builds[2], C))
1689
1690 self.worker.hold_jobs_in_build = False
1691 self.worker.release()
1692 self.waitUntilSettled()
1693
1694 self.assertEqual(len(self.builds), 0)
1695 self.assertEqual(len(self.history), 21)
1696 self.assertEqual(A.data['status'], 'NEW')
1697 self.assertEqual(B.data['status'], 'NEW')
1698 self.assertEqual(C.data['status'], 'MERGED')
1699 self.assertEqual(A.reported, 2)
1700 self.assertEqual(B.reported, 2)
1701 self.assertEqual(C.reported, 2)
1702
James E. Blair8c803f82012-07-31 16:25:42 -07001703 def test_patch_order(self):
1704 "Test that dependent patches are tested in the right order"
1705 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1706 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1707 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1708 A.addApproval('CRVW', 2)
1709 B.addApproval('CRVW', 2)
1710 C.addApproval('CRVW', 2)
1711
1712 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1713 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1714 M2.setMerged()
1715 M1.setMerged()
1716
1717 # C -> B -> A -> M1 -> M2
1718 # M2 is here to make sure it is never queried. If it is, it
1719 # means zuul is walking down the entire history of merged
1720 # changes.
1721
1722 C.setDependsOn(B, 1)
1723 B.setDependsOn(A, 1)
1724 A.setDependsOn(M1, 1)
1725 M1.setDependsOn(M2, 1)
1726
1727 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1728
1729 self.waitUntilSettled()
1730
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001731 self.assertEqual(A.data['status'], 'NEW')
1732 self.assertEqual(B.data['status'], 'NEW')
1733 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001734
1735 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1736 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1737
1738 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001739 self.assertEqual(M2.queried, 0)
1740 self.assertEqual(A.data['status'], 'MERGED')
1741 self.assertEqual(B.data['status'], 'MERGED')
1742 self.assertEqual(C.data['status'], 'MERGED')
1743 self.assertEqual(A.reported, 2)
1744 self.assertEqual(B.reported, 2)
1745 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001746
James E. Blair0e933c52013-07-11 10:18:52 -07001747 def test_trigger_cache(self):
1748 "Test that the trigger cache operates correctly"
1749 self.worker.hold_jobs_in_build = True
1750
1751 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1752 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1753 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1754 A.addApproval('CRVW', 2)
1755 B.addApproval('CRVW', 2)
1756
1757 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1758 M1.setMerged()
1759
1760 B.setDependsOn(A, 1)
1761 A.setDependsOn(M1, 1)
1762
1763 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1764 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1765
1766 self.waitUntilSettled()
1767
1768 for build in self.builds:
1769 if build.parameters['ZUUL_PIPELINE'] == 'check':
1770 build.release()
1771 self.waitUntilSettled()
1772 for build in self.builds:
1773 if build.parameters['ZUUL_PIPELINE'] == 'check':
1774 build.release()
1775 self.waitUntilSettled()
1776
1777 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1778 self.waitUntilSettled()
1779
James E. Blair6c358e72013-07-29 17:06:47 -07001780 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001781 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001782 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001783
1784 self.worker.hold_jobs_in_build = False
1785 self.worker.release()
1786 self.waitUntilSettled()
1787
1788 self.assertEqual(A.data['status'], 'MERGED')
1789 self.assertEqual(B.data['status'], 'MERGED')
1790 self.assertEqual(A.queried, 2) # Initial and isMerged
1791 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1792
James E. Blair8c803f82012-07-31 16:25:42 -07001793 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001794 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001795 # TODO: move to test_gerrit (this is a unit test!)
1796 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001797 trigger = self.sched.layout.pipelines['gate'].trigger
1798 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001799 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001800 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001801
1802 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001803 a = trigger.getChange(1, 2, refresh=True)
1804 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001805
1806 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001807 a = trigger.getChange(1, 2, refresh=True)
1808 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1809 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001810
1811 def test_build_configuration(self):
1812 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001813
1814 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001815 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1816 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1817 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1818 A.addApproval('CRVW', 2)
1819 B.addApproval('CRVW', 2)
1820 C.addApproval('CRVW', 2)
1821 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1822 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1823 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1824 self.waitUntilSettled()
1825
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001826 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001827 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001828 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001829 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001830 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001831 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001832 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001833 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1834 self.gearman_server.hold_jobs_in_queue = False
1835 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001836 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001837
Monty Taylorbc758832013-06-17 17:22:42 -04001838 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001839 repo = git.Repo(path)
1840 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1841 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001842 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001843 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001844
1845 def test_build_configuration_conflict(self):
1846 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001847
1848 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001849 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1850 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001851 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001852 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1853 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001854 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001855 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1856 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001857 A.addApproval('CRVW', 2)
1858 B.addApproval('CRVW', 2)
1859 C.addApproval('CRVW', 2)
1860 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1861 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1862 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1863 self.waitUntilSettled()
1864
James E. Blair6736beb2013-07-11 15:18:15 -07001865 self.assertEqual(A.reported, 1)
1866 self.assertEqual(B.reported, 1)
1867 self.assertEqual(C.reported, 1)
1868
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001869 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001870 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001871 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001872 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001873 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001874 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001875
1876 self.assertEqual(len(self.history), 2) # A and C merge jobs
1877
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001878 self.gearman_server.hold_jobs_in_queue = False
1879 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001880 self.waitUntilSettled()
1881
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001882 self.assertEqual(A.data['status'], 'MERGED')
1883 self.assertEqual(B.data['status'], 'NEW')
1884 self.assertEqual(C.data['status'], 'MERGED')
1885 self.assertEqual(A.reported, 2)
1886 self.assertEqual(B.reported, 2)
1887 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001888 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001889
James E. Blairdaabed22012-08-15 15:38:57 -07001890 def test_post(self):
1891 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001892
Zhongyue Luo5d556072012-09-21 02:00:47 +09001893 e = {
1894 "type": "ref-updated",
1895 "submitter": {
1896 "name": "User Name",
1897 },
1898 "refUpdate": {
1899 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1900 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1901 "refName": "master",
1902 "project": "org/project",
1903 }
1904 }
James E. Blairdaabed22012-08-15 15:38:57 -07001905 self.fake_gerrit.addEvent(e)
1906 self.waitUntilSettled()
1907
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001908 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001909 self.assertEqual(len(self.history), 1)
1910 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001911
1912 def test_build_configuration_branch(self):
1913 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001914
1915 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001916 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1917 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1918 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1919 A.addApproval('CRVW', 2)
1920 B.addApproval('CRVW', 2)
1921 C.addApproval('CRVW', 2)
1922 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1923 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1924 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1925 self.waitUntilSettled()
1926
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001927 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001928 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001929 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001930 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001931 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001932 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001933 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001934 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1935 self.gearman_server.hold_jobs_in_queue = False
1936 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001937 self.waitUntilSettled()
1938
Monty Taylorbc758832013-06-17 17:22:42 -04001939 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001940 repo = git.Repo(path)
1941 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1942 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001943 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001944 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001945
1946 def test_build_configuration_branch_interaction(self):
1947 "Test that switching between branches works"
1948 self.test_build_configuration()
1949 self.test_build_configuration_branch()
1950 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001951 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001952 repo = git.Repo(path)
1953 repo.heads.master.commit = repo.commit('init')
1954 self.test_build_configuration()
1955
1956 def test_build_configuration_multi_branch(self):
1957 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001958
1959 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001960 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1961 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1962 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1963 A.addApproval('CRVW', 2)
1964 B.addApproval('CRVW', 2)
1965 C.addApproval('CRVW', 2)
1966 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1967 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1968 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1969 self.waitUntilSettled()
James E. Blairbb1fe502014-03-04 10:15:06 -08001970 queue = self.gearman_server.getQueue()
1971 job_A = None
1972 for job in queue:
1973 if 'project-merge' in job.name:
1974 job_A = job
1975 ref_A = self.getParameter(job_A, 'ZUUL_REF')
1976 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
1977 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
1978 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
James E. Blairc6294a52012-08-17 10:19:48 -07001979
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001980 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001981 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001982 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001983 job_B = None
1984 for job in queue:
1985 if 'project-merge' in job.name:
1986 job_B = job
1987 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001988 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001989 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blairbb1fe502014-03-04 10:15:06 -08001990 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
1991
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001992 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001993 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001994 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001995 for job in queue:
1996 if 'project-merge' in job.name:
1997 job_C = job
1998 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001999 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07002000 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blairbb1fe502014-03-04 10:15:06 -08002001 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002002 self.gearman_server.hold_jobs_in_queue = False
2003 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07002004 self.waitUntilSettled()
2005
Monty Taylorbc758832013-06-17 17:22:42 -04002006 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07002007 repo = git.Repo(path)
2008
2009 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07002010 for c in repo.iter_commits(ref_C)]
James E. Blairbb1fe502014-03-04 10:15:06 -08002011 repo_shas = [c.hexsha for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07002012 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07002013 correct_messages = ['initial commit', 'A-1', 'C-1']
James E. Blairbb1fe502014-03-04 10:15:06 -08002014 # Ensure the right commits are in the history for this ref
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002015 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08002016 # Ensure ZUUL_REF -> ZUUL_COMMIT
2017 self.assertEqual(repo_shas[0], commit_C)
James E. Blairc6294a52012-08-17 10:19:48 -07002018
2019 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07002020 for c in repo.iter_commits(ref_B)]
James E. Blairbb1fe502014-03-04 10:15:06 -08002021 repo_shas = [c.hexsha for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07002022 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07002023 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002024 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08002025 self.assertEqual(repo_shas[0], commit_B)
2026
2027 repo_messages = [c.message.strip()
2028 for c in repo.iter_commits(ref_A)]
2029 repo_shas = [c.hexsha for c in repo.iter_commits(ref_A)]
2030 repo_messages.reverse()
2031 correct_messages = ['initial commit', 'A-1']
2032 self.assertEqual(repo_messages, correct_messages)
2033 self.assertEqual(repo_shas[0], commit_A)
2034
2035 self.assertNotEqual(ref_A, ref_B, ref_C)
2036 self.assertNotEqual(commit_A, commit_B, commit_C)
James E. Blair7f71c802012-08-22 13:04:32 -07002037
2038 def test_one_job_project(self):
2039 "Test that queueing works with one job"
2040 A = self.fake_gerrit.addFakeChange('org/one-job-project',
2041 'master', 'A')
2042 B = self.fake_gerrit.addFakeChange('org/one-job-project',
2043 'master', 'B')
2044 A.addApproval('CRVW', 2)
2045 B.addApproval('CRVW', 2)
2046 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2047 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2048 self.waitUntilSettled()
2049
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002050 self.assertEqual(A.data['status'], 'MERGED')
2051 self.assertEqual(A.reported, 2)
2052 self.assertEqual(B.data['status'], 'MERGED')
2053 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07002054
Antoine Musso80edd5a2013-02-13 15:37:53 +01002055 def test_job_from_templates_launched(self):
2056 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002057
Antoine Musso80edd5a2013-02-13 15:37:53 +01002058 A = self.fake_gerrit.addFakeChange(
2059 'org/templated-project', 'master', 'A')
2060 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2061 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01002062
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002063 self.assertEqual(self.getJobFromHistory('project-test1').result,
2064 'SUCCESS')
2065 self.assertEqual(self.getJobFromHistory('project-test2').result,
2066 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01002067
James E. Blair3e98c022013-12-16 15:25:38 -08002068 def test_layered_templates(self):
2069 "Test whether a job generated via a template can be launched"
2070
2071 A = self.fake_gerrit.addFakeChange(
2072 'org/layered-project', 'master', 'A')
2073 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2074 self.waitUntilSettled()
2075
2076 self.assertEqual(self.getJobFromHistory('project-test1').result,
2077 'SUCCESS')
2078 self.assertEqual(self.getJobFromHistory('project-test2').result,
2079 'SUCCESS')
James E. Blairaea6cf62013-12-16 15:38:12 -08002080 self.assertEqual(self.getJobFromHistory('layered-project-test3'
2081 ).result, 'SUCCESS')
2082 self.assertEqual(self.getJobFromHistory('layered-project-test4'
2083 ).result, 'SUCCESS')
James E. Blair12a92b12014-03-26 11:54:53 -07002084 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
2085 ).result, 'SUCCESS')
James E. Blair3e98c022013-12-16 15:25:38 -08002086 self.assertEqual(self.getJobFromHistory('project-test6').result,
2087 'SUCCESS')
2088
James E. Blaircaec0c52012-08-22 14:52:22 -07002089 def test_dependent_changes_dequeue(self):
2090 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002091
James E. Blaircaec0c52012-08-22 14:52:22 -07002092 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2093 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2094 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2095 A.addApproval('CRVW', 2)
2096 B.addApproval('CRVW', 2)
2097 C.addApproval('CRVW', 2)
2098
2099 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
2100 M1.setMerged()
2101
2102 # C -> B -> A -> M1
2103
2104 C.setDependsOn(B, 1)
2105 B.setDependsOn(A, 1)
2106 A.setDependsOn(M1, 1)
2107
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002108 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07002109
2110 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2111 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2112 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2113
2114 self.waitUntilSettled()
2115
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002116 self.assertEqual(A.data['status'], 'NEW')
2117 self.assertEqual(A.reported, 2)
2118 self.assertEqual(B.data['status'], 'NEW')
2119 self.assertEqual(B.reported, 2)
2120 self.assertEqual(C.data['status'], 'NEW')
2121 self.assertEqual(C.reported, 2)
2122 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07002123
James E. Blair972e3c72013-08-29 12:04:55 -07002124 def test_failing_dependent_changes(self):
2125 "Test that failing dependent patches are taken out of stream"
2126 self.worker.hold_jobs_in_build = True
2127 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2128 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2129 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2130 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2131 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
2132 A.addApproval('CRVW', 2)
2133 B.addApproval('CRVW', 2)
2134 C.addApproval('CRVW', 2)
2135 D.addApproval('CRVW', 2)
2136 E.addApproval('CRVW', 2)
2137
2138 # E, D -> C -> B, A
2139
2140 D.setDependsOn(C, 1)
2141 C.setDependsOn(B, 1)
2142
2143 self.worker.addFailTest('project-test1', B)
2144
2145 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2146 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2147 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2148 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2149 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2150
2151 self.waitUntilSettled()
2152 self.worker.release('.*-merge')
2153 self.waitUntilSettled()
2154 self.worker.release('.*-merge')
2155 self.waitUntilSettled()
2156 self.worker.release('.*-merge')
2157 self.waitUntilSettled()
2158 self.worker.release('.*-merge')
2159 self.waitUntilSettled()
2160 self.worker.release('.*-merge')
2161 self.waitUntilSettled()
2162
2163 self.worker.hold_jobs_in_build = False
2164 for build in self.builds:
2165 if build.parameters['ZUUL_CHANGE'] != '1':
2166 build.release()
2167 self.waitUntilSettled()
2168
2169 self.worker.release()
2170 self.waitUntilSettled()
2171
2172 self.assertEqual(A.data['status'], 'MERGED')
2173 self.assertEqual(A.reported, 2)
2174 self.assertEqual(B.data['status'], 'NEW')
2175 self.assertEqual(B.reported, 2)
2176 self.assertEqual(C.data['status'], 'NEW')
2177 self.assertEqual(C.reported, 2)
2178 self.assertEqual(D.data['status'], 'NEW')
2179 self.assertEqual(D.reported, 2)
2180 self.assertEqual(E.data['status'], 'MERGED')
2181 self.assertEqual(E.reported, 2)
2182 self.assertEqual(len(self.history), 18)
2183
James E. Blairec590122012-08-22 15:19:31 -07002184 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08002185 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07002186 # If it's dequeued more than once, we should see extra
2187 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07002188
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002189 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07002190 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2191 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2192 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2193 A.addApproval('CRVW', 2)
2194 B.addApproval('CRVW', 2)
2195 C.addApproval('CRVW', 2)
2196
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002197 self.worker.addFailTest('project1-test1', A)
2198 self.worker.addFailTest('project1-test2', A)
2199 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07002200
2201 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2202 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2203 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2204
2205 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07002206
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002207 self.assertEqual(len(self.builds), 1)
2208 self.assertEqual(self.builds[0].name, 'project1-merge')
2209 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07002210
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002211 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002212 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002213 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002214 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002215 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002216 self.waitUntilSettled()
2217
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002218 self.assertEqual(len(self.builds), 9)
2219 self.assertEqual(self.builds[0].name, 'project1-test1')
2220 self.assertEqual(self.builds[1].name, 'project1-test2')
2221 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
2222 self.assertEqual(self.builds[3].name, 'project1-test1')
2223 self.assertEqual(self.builds[4].name, 'project1-test2')
2224 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
2225 self.assertEqual(self.builds[6].name, 'project1-test1')
2226 self.assertEqual(self.builds[7].name, 'project1-test2')
2227 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07002228
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002229 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07002230 self.waitUntilSettled()
2231
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002232 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
2233 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07002234
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002235 self.worker.hold_jobs_in_build = False
2236 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07002237 self.waitUntilSettled()
2238
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002239 self.assertEqual(len(self.builds), 0)
2240 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07002241
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002242 self.assertEqual(A.data['status'], 'NEW')
2243 self.assertEqual(B.data['status'], 'MERGED')
2244 self.assertEqual(C.data['status'], 'MERGED')
2245 self.assertEqual(A.reported, 2)
2246 self.assertEqual(B.reported, 2)
2247 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07002248
2249 def test_nonvoting_job(self):
2250 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002251
James E. Blair4ec821f2012-08-23 15:28:28 -07002252 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
2253 'master', 'A')
2254 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002255 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07002256 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2257
2258 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07002259
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002260 self.assertEqual(A.data['status'], 'MERGED')
2261 self.assertEqual(A.reported, 2)
2262 self.assertEqual(
2263 self.getJobFromHistory('nonvoting-project-merge').result,
2264 'SUCCESS')
2265 self.assertEqual(
2266 self.getJobFromHistory('nonvoting-project-test1').result,
2267 'SUCCESS')
2268 self.assertEqual(
2269 self.getJobFromHistory('nonvoting-project-test2').result,
2270 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07002271
2272 def test_check_queue_success(self):
2273 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002274
James E. Blaire0487072012-08-29 17:38:31 -07002275 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2276 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2277
2278 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002279
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002280 self.assertEqual(A.data['status'], 'NEW')
2281 self.assertEqual(A.reported, 1)
2282 self.assertEqual(self.getJobFromHistory('project-merge').result,
2283 'SUCCESS')
2284 self.assertEqual(self.getJobFromHistory('project-test1').result,
2285 'SUCCESS')
2286 self.assertEqual(self.getJobFromHistory('project-test2').result,
2287 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07002288
2289 def test_check_queue_failure(self):
2290 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002291
James E. Blaire0487072012-08-29 17:38:31 -07002292 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002293 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07002294 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2295
2296 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002297
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002298 self.assertEqual(A.data['status'], 'NEW')
2299 self.assertEqual(A.reported, 1)
2300 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07002301 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002302 self.assertEqual(self.getJobFromHistory('project-test1').result,
2303 'SUCCESS')
2304 self.assertEqual(self.getJobFromHistory('project-test2').result,
2305 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07002306
2307 def test_dependent_behind_dequeue(self):
2308 "test that dependent changes behind dequeued changes work"
2309 # This complicated test is a reproduction of a real life bug
2310 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07002311
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002312 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07002313 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2314 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2315 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2316 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2317 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
2318 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
2319 D.setDependsOn(C, 1)
2320 E.setDependsOn(D, 1)
2321 A.addApproval('CRVW', 2)
2322 B.addApproval('CRVW', 2)
2323 C.addApproval('CRVW', 2)
2324 D.addApproval('CRVW', 2)
2325 E.addApproval('CRVW', 2)
2326 F.addApproval('CRVW', 2)
2327
2328 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07002329
2330 # Change object re-use in the gerrit trigger is hidden if
2331 # changes are added in quick succession; waiting makes it more
2332 # like real life.
2333 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2334 self.waitUntilSettled()
2335 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2336 self.waitUntilSettled()
2337
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002338 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002339 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002340 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002341 self.waitUntilSettled()
2342
2343 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2344 self.waitUntilSettled()
2345 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2346 self.waitUntilSettled()
2347 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2348 self.waitUntilSettled()
2349 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
2350 self.waitUntilSettled()
2351
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002352 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002353 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002354 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002355 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002356 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002357 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002358 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002359 self.waitUntilSettled()
2360
2361 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002362
2363 # Grab pointers to the jobs we want to release before
2364 # releasing any, because list indexes may change as
2365 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002366 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002367 a.release()
2368 b.release()
2369 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002370 self.waitUntilSettled()
2371
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002372 self.worker.hold_jobs_in_build = False
2373 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002374 self.waitUntilSettled()
2375
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002376 self.assertEqual(A.data['status'], 'NEW')
2377 self.assertEqual(B.data['status'], 'MERGED')
2378 self.assertEqual(C.data['status'], 'MERGED')
2379 self.assertEqual(D.data['status'], 'MERGED')
2380 self.assertEqual(E.data['status'], 'MERGED')
2381 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002382
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002383 self.assertEqual(A.reported, 2)
2384 self.assertEqual(B.reported, 2)
2385 self.assertEqual(C.reported, 2)
2386 self.assertEqual(D.reported, 2)
2387 self.assertEqual(E.reported, 2)
2388 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002389
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002390 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2391 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002392
2393 def test_merger_repack(self):
2394 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002395
James E. Blair05fed602012-09-07 12:45:24 -07002396 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2397 A.addApproval('CRVW', 2)
2398 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2399 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002400 self.assertEqual(self.getJobFromHistory('project-merge').result,
2401 'SUCCESS')
2402 self.assertEqual(self.getJobFromHistory('project-test1').result,
2403 'SUCCESS')
2404 self.assertEqual(self.getJobFromHistory('project-test2').result,
2405 'SUCCESS')
2406 self.assertEqual(A.data['status'], 'MERGED')
2407 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07002408 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002409 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002410
Monty Taylorbc758832013-06-17 17:22:42 -04002411 path = os.path.join(self.git_root, "org/project")
2412 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002413
2414 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2415 A.addApproval('CRVW', 2)
2416 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2417 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002418 self.assertEqual(self.getJobFromHistory('project-merge').result,
2419 'SUCCESS')
2420 self.assertEqual(self.getJobFromHistory('project-test1').result,
2421 'SUCCESS')
2422 self.assertEqual(self.getJobFromHistory('project-test2').result,
2423 'SUCCESS')
2424 self.assertEqual(A.data['status'], 'MERGED')
2425 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002426
James E. Blair4886f282012-11-15 09:27:33 -08002427 def test_merger_repack_large_change(self):
2428 "Test that the merger works with large changes after a repack"
2429 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08002430 # This test assumes the repo is already cloned; make sure it is
2431 url = self.sched.triggers['gerrit'].getGitUrl(
2432 self.sched.layout.projects['org/project1'])
James E. Blair4076e2b2014-01-28 12:42:20 -08002433 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08002434 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2435 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002436 path = os.path.join(self.upstream_root, "org/project1")
2437 print repack_repo(path)
2438 path = os.path.join(self.git_root, "org/project1")
2439 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002440
2441 A.addApproval('CRVW', 2)
2442 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2443 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002444 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2445 'SUCCESS')
2446 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2447 'SUCCESS')
2448 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2449 'SUCCESS')
2450 self.assertEqual(A.data['status'], 'MERGED')
2451 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002452
James E. Blair7ee88a22012-09-12 18:59:31 +02002453 def test_nonexistent_job(self):
2454 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002455 # Set to the state immediately after a restart
2456 self.resetGearmanServer()
2457 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002458
2459 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2460 A.addApproval('CRVW', 2)
2461 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2462 # There may be a thread about to report a lost change
2463 while A.reported < 2:
2464 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002465 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002466 self.assertFalse(job_names)
2467 self.assertEqual(A.data['status'], 'NEW')
2468 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002469 self.assertEmptyQueues()
2470
2471 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002472 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002473 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2474 A.addApproval('CRVW', 2)
2475 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2476 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002477 self.assertEqual(self.getJobFromHistory('project-merge').result,
2478 'SUCCESS')
2479 self.assertEqual(self.getJobFromHistory('project-test1').result,
2480 'SUCCESS')
2481 self.assertEqual(self.getJobFromHistory('project-test2').result,
2482 'SUCCESS')
2483 self.assertEqual(A.data['status'], 'MERGED')
2484 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002485
2486 def test_single_nonexistent_post_job(self):
2487 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002488 e = {
2489 "type": "ref-updated",
2490 "submitter": {
2491 "name": "User Name",
2492 },
2493 "refUpdate": {
2494 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2495 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2496 "refName": "master",
2497 "project": "org/project",
2498 }
2499 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002500 # Set to the state immediately after a restart
2501 self.resetGearmanServer()
2502 self.launcher.negative_function_cache_ttl = 0
2503
James E. Blairf62d4282012-12-31 17:01:50 -08002504 self.fake_gerrit.addEvent(e)
2505 self.waitUntilSettled()
2506
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002507 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002508
2509 def test_new_patchset_dequeues_old(self):
2510 "Test that a new patchset causes the old to be dequeued"
2511 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002512 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002513 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2514 M.setMerged()
2515
2516 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2517 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2518 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2519 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2520 A.addApproval('CRVW', 2)
2521 B.addApproval('CRVW', 2)
2522 C.addApproval('CRVW', 2)
2523 D.addApproval('CRVW', 2)
2524
2525 C.setDependsOn(B, 1)
2526 B.setDependsOn(A, 1)
2527 A.setDependsOn(M, 1)
2528
2529 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2530 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2531 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2532 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2533 self.waitUntilSettled()
2534
2535 B.addPatchset()
2536 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2537 self.waitUntilSettled()
2538
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002539 self.worker.hold_jobs_in_build = False
2540 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002541 self.waitUntilSettled()
2542
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002543 self.assertEqual(A.data['status'], 'MERGED')
2544 self.assertEqual(A.reported, 2)
2545 self.assertEqual(B.data['status'], 'NEW')
2546 self.assertEqual(B.reported, 2)
2547 self.assertEqual(C.data['status'], 'NEW')
2548 self.assertEqual(C.reported, 2)
2549 self.assertEqual(D.data['status'], 'MERGED')
2550 self.assertEqual(D.reported, 2)
2551 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002552
Arx Cruzb1b010d2013-10-28 19:49:59 -02002553 def test_zuul_url_return(self):
2554 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08002555 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Arx Cruzb1b010d2013-10-28 19:49:59 -02002556 self.worker.hold_jobs_in_build = True
2557
2558 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2559 A.addApproval('CRVW', 2)
2560 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2561 self.waitUntilSettled()
2562
2563 self.assertEqual(len(self.builds), 1)
2564 for build in self.builds:
2565 self.assertTrue('ZUUL_URL' in build.parameters)
2566
2567 self.worker.hold_jobs_in_build = False
2568 self.worker.release()
2569 self.waitUntilSettled()
2570
James E. Blair2fa50962013-01-30 21:50:41 -08002571 def test_new_patchset_dequeues_old_on_head(self):
2572 "Test that a new patchset causes the old to be dequeued (at head)"
2573 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002574 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002575 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2576 M.setMerged()
2577 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2578 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2579 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2580 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2581 A.addApproval('CRVW', 2)
2582 B.addApproval('CRVW', 2)
2583 C.addApproval('CRVW', 2)
2584 D.addApproval('CRVW', 2)
2585
2586 C.setDependsOn(B, 1)
2587 B.setDependsOn(A, 1)
2588 A.setDependsOn(M, 1)
2589
2590 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2591 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2592 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2593 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2594 self.waitUntilSettled()
2595
2596 A.addPatchset()
2597 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2598 self.waitUntilSettled()
2599
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002600 self.worker.hold_jobs_in_build = False
2601 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002602 self.waitUntilSettled()
2603
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002604 self.assertEqual(A.data['status'], 'NEW')
2605 self.assertEqual(A.reported, 2)
2606 self.assertEqual(B.data['status'], 'NEW')
2607 self.assertEqual(B.reported, 2)
2608 self.assertEqual(C.data['status'], 'NEW')
2609 self.assertEqual(C.reported, 2)
2610 self.assertEqual(D.data['status'], 'MERGED')
2611 self.assertEqual(D.reported, 2)
2612 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002613
2614 def test_new_patchset_dequeues_old_without_dependents(self):
2615 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002616 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002617 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2618 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2619 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2620 A.addApproval('CRVW', 2)
2621 B.addApproval('CRVW', 2)
2622 C.addApproval('CRVW', 2)
2623
2624 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2625 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2626 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2627 self.waitUntilSettled()
2628
2629 B.addPatchset()
2630 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2631 self.waitUntilSettled()
2632
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002633 self.worker.hold_jobs_in_build = False
2634 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002635 self.waitUntilSettled()
2636
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002637 self.assertEqual(A.data['status'], 'MERGED')
2638 self.assertEqual(A.reported, 2)
2639 self.assertEqual(B.data['status'], 'NEW')
2640 self.assertEqual(B.reported, 2)
2641 self.assertEqual(C.data['status'], 'MERGED')
2642 self.assertEqual(C.reported, 2)
2643 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002644
2645 def test_new_patchset_dequeues_old_independent_queue(self):
2646 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002647 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002648 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2649 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2650 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2651 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2652 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2653 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2654 self.waitUntilSettled()
2655
2656 B.addPatchset()
2657 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2658 self.waitUntilSettled()
2659
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002660 self.worker.hold_jobs_in_build = False
2661 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002662 self.waitUntilSettled()
2663
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002664 self.assertEqual(A.data['status'], 'NEW')
2665 self.assertEqual(A.reported, 1)
2666 self.assertEqual(B.data['status'], 'NEW')
2667 self.assertEqual(B.reported, 1)
2668 self.assertEqual(C.data['status'], 'NEW')
2669 self.assertEqual(C.reported, 1)
2670 self.assertEqual(len(self.history), 10)
2671 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002672
James E. Blair18c64442014-03-18 10:14:45 -07002673 def test_noop_job(self):
2674 "Test that the internal noop job works"
2675 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
2676 A.addApproval('CRVW', 2)
2677 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2678 self.waitUntilSettled()
2679
2680 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2681 self.assertTrue(self.sched._areAllBuildsComplete())
2682 self.assertEqual(len(self.history), 0)
2683 self.assertEqual(A.data['status'], 'MERGED')
2684 self.assertEqual(A.reported, 2)
2685
James E. Blair7d0dedc2013-02-21 17:26:09 -08002686 def test_zuul_refs(self):
2687 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002688 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002689 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2690 M1.setMerged()
2691 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2692 M2.setMerged()
2693
2694 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2695 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2696 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2697 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2698 A.addApproval('CRVW', 2)
2699 B.addApproval('CRVW', 2)
2700 C.addApproval('CRVW', 2)
2701 D.addApproval('CRVW', 2)
2702 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2703 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2704 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2705 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2706
2707 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002708 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002709 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002710 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002711 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002712 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002713 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002714 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002715 self.waitUntilSettled()
2716
James E. Blair7d0dedc2013-02-21 17:26:09 -08002717 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002718 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002719 if x.parameters['ZUUL_CHANGE'] == '3':
2720 a_zref = x.parameters['ZUUL_REF']
2721 if x.parameters['ZUUL_CHANGE'] == '4':
2722 b_zref = x.parameters['ZUUL_REF']
2723 if x.parameters['ZUUL_CHANGE'] == '5':
2724 c_zref = x.parameters['ZUUL_REF']
2725 if x.parameters['ZUUL_CHANGE'] == '6':
2726 d_zref = x.parameters['ZUUL_REF']
2727
2728 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002729 self.assertIsNotNone(a_zref)
2730 self.assertIsNotNone(b_zref)
2731 self.assertIsNotNone(c_zref)
2732 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002733
2734 # And they should all be different
2735 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002736 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002737
2738 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002739 self.assertTrue(self.ref_has_change(a_zref, A))
2740 self.assertFalse(self.ref_has_change(a_zref, B))
2741 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002742
2743 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002744 self.assertTrue(self.ref_has_change(b_zref, A))
2745 self.assertTrue(self.ref_has_change(b_zref, B))
2746 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002747
2748 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002749 self.assertTrue(self.ref_has_change(c_zref, A))
2750 self.assertTrue(self.ref_has_change(c_zref, B))
2751 self.assertTrue(self.ref_has_change(c_zref, C))
2752 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002753
2754 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002755 self.assertTrue(self.ref_has_change(d_zref, A))
2756 self.assertTrue(self.ref_has_change(d_zref, B))
2757 self.assertTrue(self.ref_has_change(d_zref, C))
2758 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002759
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002760 self.worker.hold_jobs_in_build = False
2761 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002762 self.waitUntilSettled()
2763
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002764 self.assertEqual(A.data['status'], 'MERGED')
2765 self.assertEqual(A.reported, 2)
2766 self.assertEqual(B.data['status'], 'MERGED')
2767 self.assertEqual(B.reported, 2)
2768 self.assertEqual(C.data['status'], 'MERGED')
2769 self.assertEqual(C.reported, 2)
2770 self.assertEqual(D.data['status'], 'MERGED')
2771 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002772
James E. Blairc053d022014-01-22 14:57:33 -08002773 def test_required_approval_check_and_gate(self):
2774 "Test required-approval triggers both check and gate"
2775 self.config.set('zuul', 'layout_config',
2776 'tests/fixtures/layout-require-approval.yaml')
2777 self.sched.reconfigure(self.config)
2778 self.registerJobs()
2779
2780 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2781 A.addApproval('CRVW', 2)
2782 # Add a too-old +1
2783 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2784
2785 aprv = A.addApproval('APRV', 1)
2786 self.fake_gerrit.addEvent(aprv)
2787 self.waitUntilSettled()
2788 # Should have run a check job
2789 self.assertEqual(len(self.history), 1)
2790 self.assertEqual(self.history[0].name, 'project-check')
2791
2792 # Report the result of that check job (overrides previous vrfy)
2793 # Skynet alert: this should trigger a gate job now that
2794 # all reqs are met
2795 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2796 self.waitUntilSettled()
2797 self.assertEqual(len(self.history), 2)
2798 self.assertEqual(self.history[1].name, 'project-gate')
2799
2800 def test_required_approval_newer(self):
2801 "Test required-approval newer trigger parameter"
2802 self.config.set('zuul', 'layout_config',
2803 'tests/fixtures/layout-require-approval.yaml')
2804 self.sched.reconfigure(self.config)
2805 self.registerJobs()
2806
2807 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2808 A.addApproval('CRVW', 2)
2809 aprv = A.addApproval('APRV', 1)
2810 self.fake_gerrit.addEvent(aprv)
2811 self.waitUntilSettled()
2812 # No +1 from Jenkins so should not be enqueued
2813 self.assertEqual(len(self.history), 0)
2814
2815 # Add a too-old +1, should trigger check but not gate
2816 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2817 self.fake_gerrit.addEvent(aprv)
2818 self.waitUntilSettled()
2819 self.assertEqual(len(self.history), 1)
2820 self.assertEqual(self.history[0].name, 'project-check')
2821
2822 # Add a recent +1
2823 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2824 self.fake_gerrit.addEvent(aprv)
2825 self.waitUntilSettled()
2826 self.assertEqual(len(self.history), 2)
2827 self.assertEqual(self.history[1].name, 'project-gate')
2828
2829 def test_required_approval_older(self):
2830 "Test required-approval older trigger parameter"
2831 self.config.set('zuul', 'layout_config',
2832 'tests/fixtures/layout-require-approval.yaml')
2833 self.sched.reconfigure(self.config)
2834 self.registerJobs()
2835
2836 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2837 crvw = A.addApproval('CRVW', 2)
2838 self.fake_gerrit.addEvent(crvw)
2839 self.waitUntilSettled()
2840 # No +1 from Jenkins so should not be enqueued
2841 self.assertEqual(len(self.history), 0)
2842
2843 # Add an old +1 and trigger check with a comment
2844 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2845 self.fake_gerrit.addEvent(crvw)
2846 self.waitUntilSettled()
2847 self.assertEqual(len(self.history), 1)
2848 self.assertEqual(self.history[0].name, 'project-check')
2849
2850 # Add a recent +1 and make sure nothing changes
2851 A.addApproval('VRFY', 1)
2852 self.fake_gerrit.addEvent(crvw)
2853 self.waitUntilSettled()
2854 self.assertEqual(len(self.history), 1)
2855
2856 # The last thing we did was query a change then do nothing
2857 # with a pipeline, so it will be in the cache; clean it up so
2858 # it does not fail the test.
2859 for pipeline in self.sched.layout.pipelines.values():
2860 pipeline.trigger.maintainCache([])
2861
James E. Blair4a28a882013-08-23 15:17:33 -07002862 def test_rerun_on_error(self):
2863 "Test that if a worker fails to run a job, it is run again"
2864 self.worker.hold_jobs_in_build = True
2865 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2866 A.addApproval('CRVW', 2)
2867 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2868 self.waitUntilSettled()
2869
2870 self.builds[0].run_error = True
2871 self.worker.hold_jobs_in_build = False
2872 self.worker.release()
2873 self.waitUntilSettled()
2874 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2875 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2876
James E. Blair412e5582013-04-22 15:50:12 -07002877 def test_statsd(self):
2878 "Test each of the statsd methods used in the scheduler"
2879 import extras
2880 statsd = extras.try_import('statsd.statsd')
2881 statsd.incr('test-incr')
2882 statsd.timing('test-timing', 3)
2883 statsd.gauge('test-guage', 12)
2884 self.assertReportedStat('test-incr', '1|c')
2885 self.assertReportedStat('test-timing', '3|ms')
2886 self.assertReportedStat('test-guage', '12|g')
2887
James E. Blairdad52252014-02-07 16:59:17 -08002888 def test_stuck_job_cleanup(self):
2889 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07002890 # This job won't be registered at startup because it is not in
2891 # the standard layout, but we need it to already be registerd
2892 # for when we reconfigure, as that is when Zuul will attempt
2893 # to run the new job.
2894 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002895 self.gearman_server.hold_jobs_in_queue = True
2896 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2897 A.addApproval('CRVW', 2)
2898 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2899 self.waitUntilSettled()
2900 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2901
2902 self.config.set('zuul', 'layout_config',
2903 'tests/fixtures/layout-no-jobs.yaml')
2904 self.sched.reconfigure(self.config)
2905 self.waitUntilSettled()
2906
James E. Blair18c64442014-03-18 10:14:45 -07002907 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002908 self.waitUntilSettled()
2909 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2910 self.assertTrue(self.sched._areAllBuildsComplete())
2911
2912 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002913 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002914 self.assertEqual(self.history[0].result, 'SUCCESS')
2915
James E. Blair70c71582013-03-06 08:50:50 -08002916 def test_file_jobs(self):
2917 "Test that file jobs run only when appropriate"
2918 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2919 A.addPatchset(['pip-requires'])
2920 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2921 A.addApproval('CRVW', 2)
2922 B.addApproval('CRVW', 2)
2923 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2924 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2925 self.waitUntilSettled()
2926
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002927 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002928 if x.name == 'project-testfile']
2929
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002930 self.assertEqual(len(testfile_jobs), 1)
2931 self.assertEqual(testfile_jobs[0].changes, '1,2')
2932 self.assertEqual(A.data['status'], 'MERGED')
2933 self.assertEqual(A.reported, 2)
2934 self.assertEqual(B.data['status'], 'MERGED')
2935 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002936
2937 def test_test_config(self):
2938 "Test that we can test the config"
2939 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002940 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002941 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002942 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002943
2944 def test_build_description(self):
2945 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002946 self.worker.registerFunction('set_description:' +
2947 self.worker.worker_id)
2948
2949 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2950 A.addApproval('CRVW', 2)
2951 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2952 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002953 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002954 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002955 self.assertTrue(re.search("Branch.*master", desc))
2956 self.assertTrue(re.search("Pipeline.*gate", desc))
2957 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2958 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2959 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2960 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002961
James E. Blair64ed6f22013-07-10 14:07:23 -07002962 def test_queue_precedence(self):
2963 "Test that queue precedence works"
2964
2965 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002966 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002967 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2968 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2969 A.addApproval('CRVW', 2)
2970 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2971
2972 self.waitUntilSettled()
2973 self.gearman_server.hold_jobs_in_queue = False
2974 self.gearman_server.release()
2975 self.waitUntilSettled()
2976
James E. Blair8de58bd2013-07-18 16:23:33 -07002977 # Run one build at a time to ensure non-race order:
2978 for x in range(6):
2979 self.release(self.builds[0])
2980 self.waitUntilSettled()
2981 self.worker.hold_jobs_in_build = False
2982 self.waitUntilSettled()
2983
James E. Blair64ed6f22013-07-10 14:07:23 -07002984 self.log.debug(self.history)
2985 self.assertEqual(self.history[0].pipeline, 'gate')
2986 self.assertEqual(self.history[1].pipeline, 'check')
2987 self.assertEqual(self.history[2].pipeline, 'gate')
2988 self.assertEqual(self.history[3].pipeline, 'gate')
2989 self.assertEqual(self.history[4].pipeline, 'check')
2990 self.assertEqual(self.history[5].pipeline, 'check')
2991
James E. Blair1843a552013-07-03 14:19:52 -07002992 def test_json_status(self):
2993 "Test that we can retrieve JSON status info"
2994 self.worker.hold_jobs_in_build = True
2995 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2996 A.addApproval('CRVW', 2)
2997 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2998 self.waitUntilSettled()
2999
3000 port = self.webapp.server.socket.getsockname()[1]
3001
3002 f = urllib.urlopen("http://localhost:%s/status.json" % port)
3003 data = f.read()
3004
3005 self.worker.hold_jobs_in_build = False
3006 self.worker.release()
3007 self.waitUntilSettled()
3008
3009 data = json.loads(data)
3010 status_jobs = set()
3011 for p in data['pipelines']:
3012 for q in p['change_queues']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08003013 if q['dependent']:
3014 self.assertEqual(q['window'], 20)
3015 else:
3016 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07003017 for head in q['heads']:
3018 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08003019 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003020 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07003021 for job in change['jobs']:
3022 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003023 self.assertIn('project-merge', status_jobs)
3024 self.assertIn('project-test1', status_jobs)
3025 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07003026
James E. Blairc3d428e2013-12-03 15:06:48 -08003027 def test_merging_queues(self):
3028 "Test that transitively-connected change queues are merged"
3029 self.config.set('zuul', 'layout_config',
3030 'tests/fixtures/layout-merge-queues.yaml')
3031 self.sched.reconfigure(self.config)
3032 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
3033
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003034 def test_node_label(self):
3035 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003036 self.worker.registerFunction('build:node-project-test1:debian')
3037
3038 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
3039 A.addApproval('CRVW', 2)
3040 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3041 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07003042
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003043 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3044 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3045 'debian')
3046 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07003047
3048 def test_live_reconfiguration(self):
3049 "Test that live reconfiguration works"
3050 self.worker.hold_jobs_in_build = True
3051 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3052 A.addApproval('CRVW', 2)
3053 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3054 self.waitUntilSettled()
3055
3056 self.sched.reconfigure(self.config)
3057
3058 self.worker.hold_jobs_in_build = False
3059 self.worker.release()
3060 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003061 self.assertEqual(self.getJobFromHistory('project-merge').result,
3062 'SUCCESS')
3063 self.assertEqual(self.getJobFromHistory('project-test1').result,
3064 'SUCCESS')
3065 self.assertEqual(self.getJobFromHistory('project-test2').result,
3066 'SUCCESS')
3067 self.assertEqual(A.data['status'], 'MERGED')
3068 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07003069
James E. Blaire712d9f2013-07-31 11:40:11 -07003070 def test_live_reconfiguration_functions(self):
3071 "Test live reconfiguration with a custom function"
3072 self.worker.registerFunction('build:node-project-test1:debian')
3073 self.worker.registerFunction('build:node-project-test1:wheezy')
3074 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
3075 A.addApproval('CRVW', 2)
3076 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3077 self.waitUntilSettled()
3078
3079 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3080 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3081 'debian')
3082 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
3083
3084 self.config.set('zuul', 'layout_config',
3085 'tests/fixtures/layout-live-'
3086 'reconfiguration-functions.yaml')
3087 self.sched.reconfigure(self.config)
3088 self.worker.build_history = []
3089
3090 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
3091 B.addApproval('CRVW', 2)
3092 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3093 self.waitUntilSettled()
3094
3095 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3096 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3097 'wheezy')
3098 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
3099
James E. Blair287c06d2013-07-24 10:39:30 -07003100 def test_delayed_repo_init(self):
3101 self.config.set('zuul', 'layout_config',
3102 'tests/fixtures/layout-delayed-repo-init.yaml')
3103 self.sched.reconfigure(self.config)
3104
3105 self.init_repo("org/new-project")
3106 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
3107
3108 A.addApproval('CRVW', 2)
3109 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3110 self.waitUntilSettled()
3111 self.assertEqual(self.getJobFromHistory('project-merge').result,
3112 'SUCCESS')
3113 self.assertEqual(self.getJobFromHistory('project-test1').result,
3114 'SUCCESS')
3115 self.assertEqual(self.getJobFromHistory('project-test2').result,
3116 'SUCCESS')
3117 self.assertEqual(A.data['status'], 'MERGED')
3118 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003119
Clark Boylan6dbbc482013-10-18 10:57:31 -07003120 def test_repo_deleted(self):
3121 self.config.set('zuul', 'layout_config',
3122 'tests/fixtures/layout-repo-deleted.yaml')
3123 self.sched.reconfigure(self.config)
3124
3125 self.init_repo("org/delete-project")
3126 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
3127
3128 A.addApproval('CRVW', 2)
3129 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3130 self.waitUntilSettled()
3131 self.assertEqual(self.getJobFromHistory('project-merge').result,
3132 'SUCCESS')
3133 self.assertEqual(self.getJobFromHistory('project-test1').result,
3134 'SUCCESS')
3135 self.assertEqual(self.getJobFromHistory('project-test2').result,
3136 'SUCCESS')
3137 self.assertEqual(A.data['status'], 'MERGED')
3138 self.assertEqual(A.reported, 2)
3139
3140 # Delete org/new-project zuul repo. Should be recloned.
3141 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
3142
3143 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
3144
3145 B.addApproval('CRVW', 2)
3146 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3147 self.waitUntilSettled()
3148 self.assertEqual(self.getJobFromHistory('project-merge').result,
3149 'SUCCESS')
3150 self.assertEqual(self.getJobFromHistory('project-test1').result,
3151 'SUCCESS')
3152 self.assertEqual(self.getJobFromHistory('project-test2').result,
3153 'SUCCESS')
3154 self.assertEqual(B.data['status'], 'MERGED')
3155 self.assertEqual(B.reported, 2)
3156
James E. Blair63bb0ef2013-07-29 17:14:51 -07003157 def test_timer(self):
3158 "Test that a periodic job is triggered"
3159 self.worker.hold_jobs_in_build = True
3160 self.config.set('zuul', 'layout_config',
3161 'tests/fixtures/layout-timer.yaml')
3162 self.sched.reconfigure(self.config)
3163 self.registerJobs()
3164
3165 start = time.time()
3166 failed = True
3167 while ((time.time() - start) < 30):
3168 if len(self.builds) == 2:
3169 failed = False
3170 break
3171 else:
3172 time.sleep(1)
3173
3174 if failed:
3175 raise Exception("Expected jobs never ran")
3176
3177 self.waitUntilSettled()
3178 port = self.webapp.server.socket.getsockname()[1]
3179
3180 f = urllib.urlopen("http://localhost:%s/status.json" % port)
3181 data = f.read()
3182
3183 self.worker.hold_jobs_in_build = False
3184 self.worker.release()
3185 self.waitUntilSettled()
3186
3187 self.assertEqual(self.getJobFromHistory(
3188 'project-bitrot-stable-old').result, 'SUCCESS')
3189 self.assertEqual(self.getJobFromHistory(
3190 'project-bitrot-stable-older').result, 'SUCCESS')
3191
3192 data = json.loads(data)
3193 status_jobs = set()
3194 for p in data['pipelines']:
3195 for q in p['change_queues']:
3196 for head in q['heads']:
3197 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07003198 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003199 for job in change['jobs']:
3200 status_jobs.add(job['name'])
3201 self.assertIn('project-bitrot-stable-old', status_jobs)
3202 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003203
James E. Blair4f6033c2014-03-27 15:49:09 -07003204 def test_idle(self):
3205 "Test that frequent periodic jobs work"
3206 self.worker.hold_jobs_in_build = True
3207 self.config.set('zuul', 'layout_config',
3208 'tests/fixtures/layout-idle.yaml')
3209 self.sched.reconfigure(self.config)
3210 self.registerJobs()
3211
3212 # The pipeline triggers every second, so we should have seen
3213 # several by now.
3214 time.sleep(5)
3215 self.waitUntilSettled()
3216 self.assertEqual(len(self.builds), 2)
3217 self.worker.release('.*')
3218 self.waitUntilSettled()
3219 self.assertEqual(len(self.builds), 0)
3220 self.assertEqual(len(self.history), 2)
3221
3222 time.sleep(5)
3223 self.waitUntilSettled()
3224 self.assertEqual(len(self.builds), 2)
3225 self.assertEqual(len(self.history), 2)
3226 self.worker.release('.*')
3227 self.waitUntilSettled()
3228 self.assertEqual(len(self.builds), 0)
3229 self.assertEqual(len(self.history), 4)
3230
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003231 def test_check_smtp_pool(self):
3232 self.config.set('zuul', 'layout_config',
3233 'tests/fixtures/layout-smtp.yaml')
3234 self.sched.reconfigure(self.config)
3235
3236 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3237 self.waitUntilSettled()
3238
3239 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3240 self.waitUntilSettled()
3241
James E. Blairff80a2f2013-12-27 13:24:06 -08003242 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003243
3244 # A.messages only holds what FakeGerrit places in it. Thus we
3245 # work on the knowledge of what the first message should be as
3246 # it is only configured to go to SMTP.
3247
3248 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003249 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003250 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003251 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003252 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003253 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003254
3255 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003256 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003257 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003258 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003259 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003260 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003261
James E. Blaire5910202013-12-27 09:50:31 -08003262 def test_timer_smtp(self):
3263 "Test that a periodic job is triggered"
3264 self.config.set('zuul', 'layout_config',
3265 'tests/fixtures/layout-timer-smtp.yaml')
3266 self.sched.reconfigure(self.config)
3267 self.registerJobs()
3268
3269 start = time.time()
3270 failed = True
3271 while ((time.time() - start) < 30):
3272 if len(self.history) == 2:
3273 failed = False
3274 break
3275 else:
3276 time.sleep(1)
3277
3278 if failed:
3279 raise Exception("Expected jobs never ran")
3280
3281 self.waitUntilSettled()
3282
3283 self.assertEqual(self.getJobFromHistory(
3284 'project-bitrot-stable-old').result, 'SUCCESS')
3285 self.assertEqual(self.getJobFromHistory(
3286 'project-bitrot-stable-older').result, 'SUCCESS')
3287
James E. Blairff80a2f2013-12-27 13:24:06 -08003288 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003289
3290 # A.messages only holds what FakeGerrit places in it. Thus we
3291 # work on the knowledge of what the first message should be as
3292 # it is only configured to go to SMTP.
3293
3294 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003295 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003296 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003297 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003298 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003299 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003300
James E. Blairad28e912013-11-27 10:43:22 -08003301 def test_client_enqueue(self):
3302 "Test that the RPC client can enqueue a change"
3303 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3304 A.addApproval('CRVW', 2)
3305 A.addApproval('APRV', 1)
3306
3307 client = zuul.rpcclient.RPCClient('127.0.0.1',
3308 self.gearman_server.port)
3309 r = client.enqueue(pipeline='gate',
3310 project='org/project',
3311 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003312 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003313 self.waitUntilSettled()
3314 self.assertEqual(self.getJobFromHistory('project-merge').result,
3315 'SUCCESS')
3316 self.assertEqual(self.getJobFromHistory('project-test1').result,
3317 'SUCCESS')
3318 self.assertEqual(self.getJobFromHistory('project-test2').result,
3319 'SUCCESS')
3320 self.assertEqual(A.data['status'], 'MERGED')
3321 self.assertEqual(A.reported, 2)
3322 self.assertEqual(r, True)
3323
3324 def test_client_enqueue_negative(self):
3325 "Test that the RPC client returns errors"
3326 client = zuul.rpcclient.RPCClient('127.0.0.1',
3327 self.gearman_server.port)
3328 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3329 "Invalid project"):
3330 r = client.enqueue(pipeline='gate',
3331 project='project-does-not-exist',
3332 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003333 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003334 client.shutdown()
3335 self.assertEqual(r, False)
3336
3337 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3338 "Invalid pipeline"):
3339 r = client.enqueue(pipeline='pipeline-does-not-exist',
3340 project='org/project',
3341 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003342 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003343 client.shutdown()
3344 self.assertEqual(r, False)
3345
3346 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3347 "Invalid trigger"):
3348 r = client.enqueue(pipeline='gate',
3349 project='org/project',
3350 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003351 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003352 client.shutdown()
3353 self.assertEqual(r, False)
3354
3355 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3356 "Invalid change"):
3357 r = client.enqueue(pipeline='gate',
3358 project='org/project',
3359 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003360 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003361 client.shutdown()
3362 self.assertEqual(r, False)
3363
3364 self.waitUntilSettled()
3365 self.assertEqual(len(self.history), 0)
3366 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003367
3368 def test_client_promote(self):
3369 "Test that the RPC client can promote a change"
3370 self.worker.hold_jobs_in_build = True
3371 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3372 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3373 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3374 A.addApproval('CRVW', 2)
3375 B.addApproval('CRVW', 2)
3376 C.addApproval('CRVW', 2)
3377
3378 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3379 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3380 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3381
3382 self.waitUntilSettled()
3383
Sean Daguef39b9ca2014-01-10 21:34:35 -05003384 items = self.sched.layout.pipelines['gate'].getAllItems()
3385 enqueue_times = {}
3386 for item in items:
3387 enqueue_times[str(item.change)] = item.enqueue_time
3388
James E. Blair36658cf2013-12-06 17:53:48 -08003389 client = zuul.rpcclient.RPCClient('127.0.0.1',
3390 self.gearman_server.port)
3391 r = client.promote(pipeline='gate',
3392 change_ids=['2,1', '3,1'])
3393
Sean Daguef39b9ca2014-01-10 21:34:35 -05003394 # ensure that enqueue times are durable
3395 items = self.sched.layout.pipelines['gate'].getAllItems()
3396 for item in items:
3397 self.assertEqual(
3398 enqueue_times[str(item.change)], item.enqueue_time)
3399
James E. Blair78acec92014-02-06 07:11:32 -08003400 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003401 self.worker.release('.*-merge')
3402 self.waitUntilSettled()
3403 self.worker.release('.*-merge')
3404 self.waitUntilSettled()
3405 self.worker.release('.*-merge')
3406 self.waitUntilSettled()
3407
3408 self.assertEqual(len(self.builds), 6)
3409 self.assertEqual(self.builds[0].name, 'project-test1')
3410 self.assertEqual(self.builds[1].name, 'project-test2')
3411 self.assertEqual(self.builds[2].name, 'project-test1')
3412 self.assertEqual(self.builds[3].name, 'project-test2')
3413 self.assertEqual(self.builds[4].name, 'project-test1')
3414 self.assertEqual(self.builds[5].name, 'project-test2')
3415
3416 self.assertTrue(self.job_has_changes(self.builds[0], B))
3417 self.assertFalse(self.job_has_changes(self.builds[0], A))
3418 self.assertFalse(self.job_has_changes(self.builds[0], C))
3419
3420 self.assertTrue(self.job_has_changes(self.builds[2], B))
3421 self.assertTrue(self.job_has_changes(self.builds[2], C))
3422 self.assertFalse(self.job_has_changes(self.builds[2], A))
3423
3424 self.assertTrue(self.job_has_changes(self.builds[4], B))
3425 self.assertTrue(self.job_has_changes(self.builds[4], C))
3426 self.assertTrue(self.job_has_changes(self.builds[4], A))
3427
3428 self.worker.release()
3429 self.waitUntilSettled()
3430
3431 self.assertEqual(A.data['status'], 'MERGED')
3432 self.assertEqual(A.reported, 2)
3433 self.assertEqual(B.data['status'], 'MERGED')
3434 self.assertEqual(B.reported, 2)
3435 self.assertEqual(C.data['status'], 'MERGED')
3436 self.assertEqual(C.reported, 2)
3437
3438 client.shutdown()
3439 self.assertEqual(r, True)
3440
3441 def test_client_promote_dependent(self):
3442 "Test that the RPC client can promote a dependent change"
3443 # C (depends on B) -> B -> A ; then promote C to get:
3444 # A -> C (depends on B) -> B
3445 self.worker.hold_jobs_in_build = True
3446 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3447 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3448 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3449
3450 C.setDependsOn(B, 1)
3451
3452 A.addApproval('CRVW', 2)
3453 B.addApproval('CRVW', 2)
3454 C.addApproval('CRVW', 2)
3455
3456 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3457 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3458 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3459
3460 self.waitUntilSettled()
3461
3462 client = zuul.rpcclient.RPCClient('127.0.0.1',
3463 self.gearman_server.port)
3464 r = client.promote(pipeline='gate',
3465 change_ids=['3,1'])
3466
James E. Blair78acec92014-02-06 07:11:32 -08003467 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003468 self.worker.release('.*-merge')
3469 self.waitUntilSettled()
3470 self.worker.release('.*-merge')
3471 self.waitUntilSettled()
3472 self.worker.release('.*-merge')
3473 self.waitUntilSettled()
3474
3475 self.assertEqual(len(self.builds), 6)
3476 self.assertEqual(self.builds[0].name, 'project-test1')
3477 self.assertEqual(self.builds[1].name, 'project-test2')
3478 self.assertEqual(self.builds[2].name, 'project-test1')
3479 self.assertEqual(self.builds[3].name, 'project-test2')
3480 self.assertEqual(self.builds[4].name, 'project-test1')
3481 self.assertEqual(self.builds[5].name, 'project-test2')
3482
3483 self.assertTrue(self.job_has_changes(self.builds[0], B))
3484 self.assertFalse(self.job_has_changes(self.builds[0], A))
3485 self.assertFalse(self.job_has_changes(self.builds[0], C))
3486
3487 self.assertTrue(self.job_has_changes(self.builds[2], B))
3488 self.assertTrue(self.job_has_changes(self.builds[2], C))
3489 self.assertFalse(self.job_has_changes(self.builds[2], A))
3490
3491 self.assertTrue(self.job_has_changes(self.builds[4], B))
3492 self.assertTrue(self.job_has_changes(self.builds[4], C))
3493 self.assertTrue(self.job_has_changes(self.builds[4], A))
3494
3495 self.worker.release()
3496 self.waitUntilSettled()
3497
3498 self.assertEqual(A.data['status'], 'MERGED')
3499 self.assertEqual(A.reported, 2)
3500 self.assertEqual(B.data['status'], 'MERGED')
3501 self.assertEqual(B.reported, 2)
3502 self.assertEqual(C.data['status'], 'MERGED')
3503 self.assertEqual(C.reported, 2)
3504
3505 client.shutdown()
3506 self.assertEqual(r, True)
3507
3508 def test_client_promote_negative(self):
3509 "Test that the RPC client returns errors for promotion"
3510 self.worker.hold_jobs_in_build = True
3511 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3512 A.addApproval('CRVW', 2)
3513 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3514 self.waitUntilSettled()
3515
3516 client = zuul.rpcclient.RPCClient('127.0.0.1',
3517 self.gearman_server.port)
3518
3519 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3520 r = client.promote(pipeline='nonexistent',
3521 change_ids=['2,1', '3,1'])
3522 client.shutdown()
3523 self.assertEqual(r, False)
3524
3525 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3526 r = client.promote(pipeline='gate',
3527 change_ids=['4,1'])
3528 client.shutdown()
3529 self.assertEqual(r, False)
3530
3531 self.worker.hold_jobs_in_build = False
3532 self.worker.release()
3533 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003534
3535 def test_queue_rate_limiting(self):
3536 "Test that DependentPipelines are rate limited with dep across window"
3537 self.config.set('zuul', 'layout_config',
3538 'tests/fixtures/layout-rate-limit.yaml')
3539 self.sched.reconfigure(self.config)
3540 self.worker.hold_jobs_in_build = True
3541 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3542 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3543 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3544
3545 C.setDependsOn(B, 1)
3546 self.worker.addFailTest('project-test1', A)
3547
3548 A.addApproval('CRVW', 2)
3549 B.addApproval('CRVW', 2)
3550 C.addApproval('CRVW', 2)
3551
3552 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3553 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3554 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3555 self.waitUntilSettled()
3556
3557 # Only A and B will have their merge jobs queued because
3558 # window is 2.
3559 self.assertEqual(len(self.builds), 2)
3560 self.assertEqual(self.builds[0].name, 'project-merge')
3561 self.assertEqual(self.builds[1].name, 'project-merge')
3562
3563 self.worker.release('.*-merge')
3564 self.waitUntilSettled()
3565 self.worker.release('.*-merge')
3566 self.waitUntilSettled()
3567
3568 # Only A and B will have their test jobs queued because
3569 # window is 2.
3570 self.assertEqual(len(self.builds), 4)
3571 self.assertEqual(self.builds[0].name, 'project-test1')
3572 self.assertEqual(self.builds[1].name, 'project-test2')
3573 self.assertEqual(self.builds[2].name, 'project-test1')
3574 self.assertEqual(self.builds[3].name, 'project-test2')
3575
3576 self.worker.release('project-.*')
3577 self.waitUntilSettled()
3578
3579 queue = self.sched.layout.pipelines['gate'].queues[0]
3580 # A failed so window is reduced by 1 to 1.
3581 self.assertEqual(queue.window, 1)
3582 self.assertEqual(queue.window_floor, 1)
3583 self.assertEqual(A.data['status'], 'NEW')
3584
3585 # Gate is reset and only B's merge job is queued because
3586 # window shrunk to 1.
3587 self.assertEqual(len(self.builds), 1)
3588 self.assertEqual(self.builds[0].name, 'project-merge')
3589
3590 self.worker.release('.*-merge')
3591 self.waitUntilSettled()
3592
3593 # Only B's test jobs are queued because window is still 1.
3594 self.assertEqual(len(self.builds), 2)
3595 self.assertEqual(self.builds[0].name, 'project-test1')
3596 self.assertEqual(self.builds[1].name, 'project-test2')
3597
3598 self.worker.release('project-.*')
3599 self.waitUntilSettled()
3600
3601 # B was successfully merged so window is increased to 2.
3602 self.assertEqual(queue.window, 2)
3603 self.assertEqual(queue.window_floor, 1)
3604 self.assertEqual(B.data['status'], 'MERGED')
3605
3606 # Only C is left and its merge job is queued.
3607 self.assertEqual(len(self.builds), 1)
3608 self.assertEqual(self.builds[0].name, 'project-merge')
3609
3610 self.worker.release('.*-merge')
3611 self.waitUntilSettled()
3612
3613 # After successful merge job the test jobs for C are queued.
3614 self.assertEqual(len(self.builds), 2)
3615 self.assertEqual(self.builds[0].name, 'project-test1')
3616 self.assertEqual(self.builds[1].name, 'project-test2')
3617
3618 self.worker.release('project-.*')
3619 self.waitUntilSettled()
3620
3621 # C successfully merged so window is bumped to 3.
3622 self.assertEqual(queue.window, 3)
3623 self.assertEqual(queue.window_floor, 1)
3624 self.assertEqual(C.data['status'], 'MERGED')
3625
3626 def test_queue_rate_limiting_dependent(self):
3627 "Test that DependentPipelines are rate limited with dep in window"
3628 self.config.set('zuul', 'layout_config',
3629 'tests/fixtures/layout-rate-limit.yaml')
3630 self.sched.reconfigure(self.config)
3631 self.worker.hold_jobs_in_build = True
3632 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3633 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3634 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3635
3636 B.setDependsOn(A, 1)
3637
3638 self.worker.addFailTest('project-test1', A)
3639
3640 A.addApproval('CRVW', 2)
3641 B.addApproval('CRVW', 2)
3642 C.addApproval('CRVW', 2)
3643
3644 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3645 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3646 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3647 self.waitUntilSettled()
3648
3649 # Only A and B will have their merge jobs queued because
3650 # window is 2.
3651 self.assertEqual(len(self.builds), 2)
3652 self.assertEqual(self.builds[0].name, 'project-merge')
3653 self.assertEqual(self.builds[1].name, 'project-merge')
3654
3655 self.worker.release('.*-merge')
3656 self.waitUntilSettled()
3657 self.worker.release('.*-merge')
3658 self.waitUntilSettled()
3659
3660 # Only A and B will have their test jobs queued because
3661 # window is 2.
3662 self.assertEqual(len(self.builds), 4)
3663 self.assertEqual(self.builds[0].name, 'project-test1')
3664 self.assertEqual(self.builds[1].name, 'project-test2')
3665 self.assertEqual(self.builds[2].name, 'project-test1')
3666 self.assertEqual(self.builds[3].name, 'project-test2')
3667
3668 self.worker.release('project-.*')
3669 self.waitUntilSettled()
3670
3671 queue = self.sched.layout.pipelines['gate'].queues[0]
3672 # A failed so window is reduced by 1 to 1.
3673 self.assertEqual(queue.window, 1)
3674 self.assertEqual(queue.window_floor, 1)
3675 self.assertEqual(A.data['status'], 'NEW')
3676 self.assertEqual(B.data['status'], 'NEW')
3677
3678 # Gate is reset and only C's merge job is queued because
3679 # window shrunk to 1 and A and B were dequeued.
3680 self.assertEqual(len(self.builds), 1)
3681 self.assertEqual(self.builds[0].name, 'project-merge')
3682
3683 self.worker.release('.*-merge')
3684 self.waitUntilSettled()
3685
3686 # Only C's test jobs are queued because window is still 1.
3687 self.assertEqual(len(self.builds), 2)
3688 self.assertEqual(self.builds[0].name, 'project-test1')
3689 self.assertEqual(self.builds[1].name, 'project-test2')
3690
3691 self.worker.release('project-.*')
3692 self.waitUntilSettled()
3693
3694 # C was successfully merged so window is increased to 2.
3695 self.assertEqual(queue.window, 2)
3696 self.assertEqual(queue.window_floor, 1)
3697 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003698
3699 def test_worker_update_metadata(self):
3700 "Test if a worker can send back metadata about itself"
3701 self.worker.hold_jobs_in_build = True
3702
3703 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3704 A.addApproval('CRVW', 2)
3705 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3706 self.waitUntilSettled()
3707
3708 self.assertEqual(len(self.launcher.builds), 1)
3709
3710 self.log.debug('Current builds:')
3711 self.log.debug(self.launcher.builds)
3712
3713 start = time.time()
3714 while True:
3715 if time.time() - start > 10:
3716 raise Exception("Timeout waiting for gearman server to report "
3717 + "back to the client")
3718 build = self.launcher.builds.values()[0]
3719 if build.worker.name == "My Worker":
3720 break
3721 else:
3722 time.sleep(0)
3723
3724 self.log.debug(build)
3725 self.assertEqual("My Worker", build.worker.name)
3726 self.assertEqual("localhost", build.worker.hostname)
3727 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3728 self.assertEqual("zuul.example.org", build.worker.fqdn)
3729 self.assertEqual("FakeBuilder", build.worker.program)
3730 self.assertEqual("v1.1", build.worker.version)
3731 self.assertEqual({'something': 'else'}, build.worker.extra)
3732
3733 self.worker.hold_jobs_in_build = False
3734 self.worker.release()
3735 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003736
3737 def test_footer_message(self):
3738 "Test a pipeline's footer message is correctly added to the report."
3739 self.config.set('zuul', 'layout_config',
3740 'tests/fixtures/layout-footer-message.yaml')
3741 self.sched.reconfigure(self.config)
3742 self.registerJobs()
3743
3744 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3745 A.addApproval('CRVW', 2)
3746 self.worker.addFailTest('test1', A)
3747 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3748 self.waitUntilSettled()
3749
3750 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3751 B.addApproval('CRVW', 2)
3752 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3753 self.waitUntilSettled()
3754
3755 self.assertEqual(2, len(self.smtp_messages))
3756
3757 failure_body = """\
3758Build failed. For information on how to proceed, see \
3759http://wiki.example.org/Test_Failures
3760
3761- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3762- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3763
3764For CI problems and help debugging, contact ci@example.org"""
3765
3766 success_body = """\
3767Build succeeded.
3768
3769- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3770- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3771
3772For CI problems and help debugging, contact ci@example.org"""
3773
3774 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3775 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11003776
3777 def test_merge_failure_reporters(self):
3778 """Check that the config is set up correctly"""
3779
3780 self.config.set('zuul', 'layout_config',
3781 'tests/fixtures/layout-merge-failure.yaml')
3782 self.sched.reconfigure(self.config)
3783 self.registerJobs()
3784
3785 self.assertEqual(
3786 "Merge Failed.\n\nThis change was unable to be automatically "
3787 "merged with the current state of the repository. Please rebase "
3788 "your change and upload a new patchset.",
3789 self.sched.layout.pipelines['check'].merge_failure_message)
3790 self.assertEqual(
3791 "The merge failed! For more information...",
3792 self.sched.layout.pipelines['gate'].merge_failure_message)
3793
3794 self.assertEqual(
3795 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3796 self.assertEqual(
3797 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3798
3799 self.assertTrue(isinstance(
3800 self.sched.layout.pipelines['check'].merge_failure_actions[0].
3801 reporter, zuul.reporter.gerrit.Reporter))
3802
3803 self.assertTrue(
3804 (
3805 isinstance(self.sched.layout.pipelines['gate'].
3806 merge_failure_actions[0].reporter,
3807 zuul.reporter.smtp.Reporter) and
3808 isinstance(self.sched.layout.pipelines['gate'].
3809 merge_failure_actions[1].reporter,
3810 zuul.reporter.gerrit.Reporter)
3811 ) or (
3812 isinstance(self.sched.layout.pipelines['gate'].
3813 merge_failure_actions[0].reporter,
3814 zuul.reporter.gerrit.Reporter) and
3815 isinstance(self.sched.layout.pipelines['gate'].
3816 merge_failure_actions[1].reporter,
3817 zuul.reporter.smtp.Reporter)
3818 )
3819 )
3820
3821 def test_merge_failure_reports(self):
3822 """Check that when a change fails to merge the correct message is sent
3823 to the correct reporter"""
3824 self.config.set('zuul', 'layout_config',
3825 'tests/fixtures/layout-merge-failure.yaml')
3826 self.sched.reconfigure(self.config)
3827 self.registerJobs()
3828
3829 # Check a test failure isn't reported to SMTP
3830 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3831 A.addApproval('CRVW', 2)
3832 self.worker.addFailTest('project-test1', A)
3833 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3834 self.waitUntilSettled()
3835
3836 self.assertEqual(3, len(self.history)) # 3 jobs
3837 self.assertEqual(0, len(self.smtp_messages))
3838
3839 # Check a merge failure is reported to SMTP
3840 # B should be merged, but C will conflict with B
3841 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3842 B.addPatchset(['conflict'])
3843 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3844 C.addPatchset(['conflict'])
3845 B.addApproval('CRVW', 2)
3846 C.addApproval('CRVW', 2)
3847 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3848 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3849 self.waitUntilSettled()
3850
3851 self.assertEqual(6, len(self.history)) # A and B jobs
3852 self.assertEqual(1, len(self.smtp_messages))
3853 self.assertEqual('The merge failed! For more information...',
3854 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003855
3856 def test_swift_instructions(self):
3857 "Test that the correct swift instructions are sent to the workers"
3858 self.config.set('zuul', 'layout_config',
3859 'tests/fixtures/layout-swift.yaml')
3860 self.sched.reconfigure(self.config)
3861 self.registerJobs()
3862
3863 self.worker.hold_jobs_in_build = True
3864 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3865
3866 A.addApproval('CRVW', 2)
3867 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3868 self.waitUntilSettled()
3869
3870 self.assertEqual(
3871 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
3872 "gate/test-merge/",
3873 self.builds[0].parameters['SWIFT_logs_URL'][:-32])
3874 self.assertEqual(5,
3875 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
3876 split('\n')))
3877 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
3878
3879 self.assertEqual(
3880 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
3881 "gate/test-test/",
3882 self.builds[1].parameters['SWIFT_logs_URL'][:-32])
3883 self.assertEqual(5,
3884 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
3885 split('\n')))
3886 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
3887
3888 self.assertEqual(
3889 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
3890 "gate/test-test/",
3891 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-32])
3892 self.assertEqual(5,
3893 len(self.builds[1].
3894 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
3895 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
3896
3897 self.worker.hold_jobs_in_build = False
3898 self.worker.release()
3899 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003900
3901 def test_client_get_running_jobs(self):
3902 "Test that the RPC client can get a list of running jobs"
3903 self.worker.hold_jobs_in_build = True
3904 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3905 A.addApproval('CRVW', 2)
3906 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3907 self.waitUntilSettled()
3908
3909 client = zuul.rpcclient.RPCClient('127.0.0.1',
3910 self.gearman_server.port)
3911
3912 # Wait for gearman server to send the initial workData back to zuul
3913 start = time.time()
3914 while True:
3915 if time.time() - start > 10:
3916 raise Exception("Timeout waiting for gearman server to report "
3917 + "back to the client")
3918 build = self.launcher.builds.values()[0]
3919 if build.worker.name == "My Worker":
3920 break
3921 else:
3922 time.sleep(0)
3923
3924 running_items = client.get_running_jobs()
3925
3926 self.assertEqual(1, len(running_items))
3927 running_item = running_items[0]
3928 self.assertEqual([], running_item['failing_reasons'])
3929 self.assertEqual([], running_item['items_behind'])
3930 self.assertEqual('https://hostname/1', running_item['url'])
3931 self.assertEqual(None, running_item['item_ahead'])
3932 self.assertEqual('org/project', running_item['project'])
3933 self.assertEqual(None, running_item['remaining_time'])
3934 self.assertEqual(True, running_item['active'])
3935 self.assertEqual('1,1', running_item['id'])
3936
3937 self.assertEqual(3, len(running_item['jobs']))
3938 for job in running_item['jobs']:
3939 if job['name'] == 'project-merge':
3940 self.assertEqual('project-merge', job['name'])
3941 self.assertEqual('gate', job['pipeline'])
3942 self.assertEqual(False, job['retry'])
3943 self.assertEqual(13, len(job['parameters']))
3944 self.assertEqual('https://server/job/project-merge/0/',
3945 job['url'])
3946 self.assertEqual(7, len(job['worker']))
3947 self.assertEqual(False, job['canceled'])
3948 self.assertEqual(True, job['voting'])
3949 self.assertEqual(None, job['result'])
3950 self.assertEqual('gate', job['pipeline'])
3951 break
3952
3953 self.worker.hold_jobs_in_build = False
3954 self.worker.release()
3955 self.waitUntilSettled()
3956
3957 running_items = client.get_running_jobs()
3958 self.assertEqual(0, len(running_items))