blob: 1d7be4a6d8ff0c2d217493d70bfa1590afacb430 [file] [log] [blame]
James E. Blairb0fcae42012-07-17 11:12:10 -07001#!/usr/bin/env python
2
3# Copyright 2012 Hewlett-Packard Development Company, L.P.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
James E. Blairb0fcae42012-07-17 11:12:10 -070017import ConfigParser
Monty Taylorbc758832013-06-17 17:22:42 -040018from cStringIO import StringIO
Clark Boylan4ba48d92013-11-11 18:03:53 -080019import gc
James E. Blair8cc15a82012-08-01 11:17:57 -070020import hashlib
James E. Blairb0fcae42012-07-17 11:12:10 -070021import json
Monty Taylorbc758832013-06-17 17:22:42 -040022import logging
23import os
James E. Blairb0fcae42012-07-17 11:12:10 -070024import pprint
Monty Taylorbc758832013-06-17 17:22:42 -040025import Queue
26import random
James E. Blairb0fcae42012-07-17 11:12:10 -070027import re
James E. Blair412e5582013-04-22 15:50:12 -070028import select
James E. Blair4886cc12012-07-18 15:39:41 -070029import shutil
James E. Blair412e5582013-04-22 15:50:12 -070030import socket
James E. Blair4886f282012-11-15 09:27:33 -080031import string
Monty Taylorbc758832013-06-17 17:22:42 -040032import subprocess
Monty Taylorbc758832013-06-17 17:22:42 -040033import threading
34import time
James E. Blair1843a552013-07-03 14:19:52 -070035import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040036import urllib2
Monty Taylorbc758832013-06-17 17:22:42 -040037
James E. Blair4886cc12012-07-18 15:39:41 -070038import git
James E. Blair1f4c2bb2013-04-26 08:40:46 -070039import gear
Monty Taylorbc758832013-06-17 17:22:42 -040040import fixtures
tanline621a412014-02-12 11:16:36 +080041import six.moves.urllib.parse as urlparse
Monty Taylorbc758832013-06-17 17:22:42 -040042import statsd
43import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070044
James E. Blairb0fcae42012-07-17 11:12:10 -070045import zuul.scheduler
James E. Blair1843a552013-07-03 14:19:52 -070046import zuul.webapp
James E. Blairad28e912013-11-27 10:43:22 -080047import zuul.rpclistener
48import zuul.rpcclient
James E. Blair1f4c2bb2013-04-26 08:40:46 -070049import zuul.launcher.gearman
James E. Blair4076e2b2014-01-28 12:42:20 -080050import zuul.merger.server
51import zuul.merger.client
Joshua Hesketh1879cf72013-08-19 14:13:15 +100052import zuul.reporter.gerrit
Joshua Hesketh5fea8672013-08-19 17:32:01 +100053import zuul.reporter.smtp
James E. Blairb0fcae42012-07-17 11:12:10 -070054import zuul.trigger.gerrit
James E. Blair63bb0ef2013-07-29 17:14:51 -070055import zuul.trigger.timer
James E. Blairb0fcae42012-07-17 11:12:10 -070056
57FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
58 'fixtures')
59CONFIG = ConfigParser.ConfigParser()
60CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
61
62CONFIG.set('zuul', 'layout_config',
63 os.path.join(FIXTURE_DIR, "layout.yaml"))
64
James E. Blair1f4c2bb2013-04-26 08:40:46 -070065logging.basicConfig(level=logging.DEBUG,
66 format='%(asctime)s %(name)-32s '
67 '%(levelname)-8s %(message)s')
James E. Blairb0fcae42012-07-17 11:12:10 -070068
69
Monty Taylorbc758832013-06-17 17:22:42 -040070def repack_repo(path):
James E. Blairac2c3242014-01-24 13:38:51 -080071 cmd = ['git', '--git-dir=%s/.git' % path, 'repack', '-afd']
72 output = subprocess.Popen(cmd, close_fds=True,
73 stdout=subprocess.PIPE,
74 stderr=subprocess.PIPE)
Monty Taylorbc758832013-06-17 17:22:42 -040075 out = output.communicate()
76 if output.returncode:
77 raise Exception("git repack returned %d" % output.returncode)
78 return out
79
80
James E. Blair8cc15a82012-08-01 11:17:57 -070081def random_sha1():
82 return hashlib.sha1(str(random.random())).hexdigest()
83
84
James E. Blair4886cc12012-07-18 15:39:41 -070085class ChangeReference(git.Reference):
86 _common_path_default = "refs/changes"
87 _points_to_commits_only = True
88
89
James E. Blairb0fcae42012-07-17 11:12:10 -070090class FakeChange(object):
James E. Blair8c803f82012-07-31 16:25:42 -070091 categories = {'APRV': ('Approved', -1, 1),
92 'CRVW': ('Code-Review', -2, 2),
93 'VRFY': ('Verified', -2, 2)}
James E. Blairb0fcae42012-07-17 11:12:10 -070094
Monty Taylorbc758832013-06-17 17:22:42 -040095 def __init__(self, gerrit, number, project, branch, subject,
96 status='NEW', upstream_root=None):
James E. Blair8cc15a82012-08-01 11:17:57 -070097 self.gerrit = gerrit
James E. Blaird466dc42012-07-31 10:42:56 -070098 self.reported = 0
James E. Blair8c803f82012-07-31 16:25:42 -070099 self.queried = 0
James E. Blairb0fcae42012-07-17 11:12:10 -0700100 self.patchsets = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700101 self.number = number
102 self.project = project
103 self.branch = branch
104 self.subject = subject
105 self.latest_patchset = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700106 self.depends_on_change = None
107 self.needed_by_changes = []
James E. Blair127bc182012-08-28 15:55:15 -0700108 self.fail_merge = False
James E. Blair42f74822013-05-14 15:18:03 -0700109 self.messages = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700110 self.data = {
111 'branch': branch,
112 'comments': [],
113 'commitMessage': subject,
114 'createdOn': time.time(),
James E. Blair8cc15a82012-08-01 11:17:57 -0700115 'id': 'I' + random_sha1(),
James E. Blairb0fcae42012-07-17 11:12:10 -0700116 'lastUpdated': time.time(),
117 'number': str(number),
118 'open': True,
119 'owner': {'email': 'user@example.com',
120 'name': 'User Name',
121 'username': 'username'},
122 'patchSets': self.patchsets,
123 'project': project,
124 'status': status,
125 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700126 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700127 'url': 'https://hostname/%s' % number}
128
Monty Taylorbc758832013-06-17 17:22:42 -0400129 self.upstream_root = upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700130 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700131 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700132
Monty Taylorbc758832013-06-17 17:22:42 -0400133 def add_fake_change_to_repo(self, msg, fn, large):
134 path = os.path.join(self.upstream_root, self.project)
135 repo = git.Repo(path)
136 ref = ChangeReference.create(repo, '1/%s/%s' % (self.number,
137 self.latest_patchset),
138 'refs/tags/init')
139 repo.head.reference = ref
140 repo.head.reset(index=True, working_tree=True)
141 repo.git.clean('-x', '-f', '-d')
142
143 path = os.path.join(self.upstream_root, self.project)
144 if not large:
145 fn = os.path.join(path, fn)
146 f = open(fn, 'w')
147 f.write("test %s %s %s\n" %
148 (self.branch, self.number, self.latest_patchset))
149 f.close()
150 repo.index.add([fn])
151 else:
152 for fni in range(100):
153 fn = os.path.join(path, str(fni))
154 f = open(fn, 'w')
155 for ci in range(4096):
156 f.write(random.choice(string.printable))
157 f.close()
158 repo.index.add([fn])
159
James E. Blair287c06d2013-07-24 10:39:30 -0700160 r = repo.index.commit(msg)
161 repo.head.reference = 'master'
162 repo.head.reset(index=True, working_tree=True)
163 repo.git.clean('-x', '-f', '-d')
James E. Blairac2c3242014-01-24 13:38:51 -0800164 repo.heads['master'].checkout()
James E. Blair287c06d2013-07-24 10:39:30 -0700165 return r
Monty Taylorbc758832013-06-17 17:22:42 -0400166
James E. Blair70c71582013-03-06 08:50:50 -0800167 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700168 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700169 if files:
170 fn = files[0]
171 else:
172 fn = '%s-%s' % (self.branch, self.number)
173 msg = self.subject + '-' + str(self.latest_patchset)
Monty Taylorbc758832013-06-17 17:22:42 -0400174 c = self.add_fake_change_to_repo(msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800175 ps_files = [{'file': '/COMMIT_MSG',
176 'type': 'ADDED'},
177 {'file': 'README',
178 'type': 'MODIFIED'}]
179 for f in files:
180 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700181 d = {'approvals': [],
182 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800183 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700184 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700185 'ref': 'refs/changes/1/%s/%s' % (self.number,
186 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700187 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700188 'uploader': {'email': 'user@example.com',
189 'name': 'User name',
190 'username': 'user'}}
191 self.data['currentPatchSet'] = d
192 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700193 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700194
James E. Blaire0487072012-08-29 17:38:31 -0700195 def getPatchsetCreatedEvent(self, patchset):
196 event = {"type": "patchset-created",
197 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800198 "branch": self.branch,
199 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
200 "number": str(self.number),
201 "subject": self.subject,
202 "owner": {"name": "User Name"},
203 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700204 "patchSet": self.patchsets[patchset - 1],
205 "uploader": {"name": "User Name"}}
206 return event
207
James E. Blair42f74822013-05-14 15:18:03 -0700208 def getChangeRestoredEvent(self):
209 event = {"type": "change-restored",
210 "change": {"project": self.project,
211 "branch": self.branch,
212 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
213 "number": str(self.number),
214 "subject": self.subject,
215 "owner": {"name": "User Name"},
216 "url": "https://hostname/3"},
217 "restorer": {"name": "User Name"},
218 "reason": ""}
219 return event
220
James E. Blairc053d022014-01-22 14:57:33 -0800221 def addApproval(self, category, value, username='jenkins',
222 granted_on=None):
223 if not granted_on:
224 granted_on = time.time()
James E. Blair8c803f82012-07-31 16:25:42 -0700225 approval = {'description': self.categories[category][0],
226 'type': category,
James E. Blairc053d022014-01-22 14:57:33 -0800227 'value': str(value),
James E. Blair64ff4ef2014-01-24 13:50:23 -0800228 'by': {
229 'username': username,
230 'email': username + '@example.com',
231 },
James E. Blairc053d022014-01-22 14:57:33 -0800232 'grantedOn': int(granted_on)}
233 for i, x in enumerate(self.patchsets[-1]['approvals'][:]):
James E. Blair64ff4ef2014-01-24 13:50:23 -0800234 if x['by']['username'] == username and x['type'] == category:
James E. Blairc053d022014-01-22 14:57:33 -0800235 del self.patchsets[-1]['approvals'][i]
James E. Blair8c803f82012-07-31 16:25:42 -0700236 self.patchsets[-1]['approvals'].append(approval)
237 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700238 'author': {'email': 'user@example.com',
239 'name': 'User Name',
240 'username': 'username'},
241 'change': {'branch': self.branch,
242 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
243 'number': str(self.number),
244 'owner': {'email': 'user@example.com',
245 'name': 'User Name',
246 'username': 'username'},
247 'project': self.project,
248 'subject': self.subject,
249 'topic': 'master',
250 'url': 'https://hostname/459'},
251 'comment': '',
252 'patchSet': self.patchsets[-1],
253 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700254 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700255 return json.loads(json.dumps(event))
256
James E. Blair8c803f82012-07-31 16:25:42 -0700257 def getSubmitRecords(self):
258 status = {}
259 for cat in self.categories.keys():
260 status[cat] = 0
261
262 for a in self.patchsets[-1]['approvals']:
263 cur = status[a['type']]
264 cat_min, cat_max = self.categories[a['type']][1:]
265 new = int(a['value'])
266 if new == cat_min:
267 cur = new
268 elif abs(new) > abs(cur):
269 cur = new
270 status[a['type']] = cur
271
272 labels = []
273 ok = True
274 for typ, cat in self.categories.items():
275 cur = status[typ]
276 cat_min, cat_max = cat[1:]
277 if cur == cat_min:
278 value = 'REJECT'
279 ok = False
280 elif cur == cat_max:
281 value = 'OK'
282 else:
283 value = 'NEED'
284 ok = False
285 labels.append({'label': cat[0], 'status': value})
286 if ok:
287 return [{'status': 'OK'}]
288 return [{'status': 'NOT_READY',
289 'labels': labels}]
290
291 def setDependsOn(self, other, patchset):
292 self.depends_on_change = other
293 d = {'id': other.data['id'],
294 'number': other.data['number'],
295 'ref': other.patchsets[patchset - 1]['ref']
296 }
297 self.data['dependsOn'] = [d]
298
299 other.needed_by_changes.append(self)
300 needed = other.data.get('neededBy', [])
301 d = {'id': self.data['id'],
302 'number': self.data['number'],
303 'ref': self.patchsets[patchset - 1]['ref'],
304 'revision': self.patchsets[patchset - 1]['revision']
305 }
306 needed.append(d)
307 other.data['neededBy'] = needed
308
James E. Blairb0fcae42012-07-17 11:12:10 -0700309 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700310 self.queried += 1
311 d = self.data.get('dependsOn')
312 if d:
313 d = d[0]
314 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
315 d['isCurrentPatchSet'] = True
316 else:
317 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700318 return json.loads(json.dumps(self.data))
319
320 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800321 if (self.depends_on_change and
322 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700323 return
James E. Blair127bc182012-08-28 15:55:15 -0700324 if self.fail_merge:
325 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700326 self.data['status'] = 'MERGED'
327 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700328
Monty Taylorbc758832013-06-17 17:22:42 -0400329 path = os.path.join(self.upstream_root, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700330 repo = git.Repo(path)
331 repo.heads[self.branch].commit = \
332 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700333
James E. Blaird466dc42012-07-31 10:42:56 -0700334 def setReported(self):
335 self.reported += 1
336
James E. Blairb0fcae42012-07-17 11:12:10 -0700337
338class FakeGerrit(object):
339 def __init__(self, *args, **kw):
340 self.event_queue = Queue.Queue()
341 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
342 self.change_number = 0
343 self.changes = {}
344
345 def addFakeChange(self, project, branch, subject):
346 self.change_number += 1
Monty Taylorbc758832013-06-17 17:22:42 -0400347 c = FakeChange(self, self.change_number, project, branch, subject,
348 upstream_root=self.upstream_root)
James E. Blairb0fcae42012-07-17 11:12:10 -0700349 self.changes[self.change_number] = c
350 return c
351
352 def addEvent(self, data):
353 return self.event_queue.put(data)
354
355 def getEvent(self):
356 return self.event_queue.get()
357
358 def eventDone(self):
359 self.event_queue.task_done()
360
361 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700362 number, ps = changeid.split(',')
363 change = self.changes[int(number)]
James E. Blair42f74822013-05-14 15:18:03 -0700364 change.messages.append(message)
James E. Blairb0fcae42012-07-17 11:12:10 -0700365 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700366 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700367 if message:
368 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700369
370 def query(self, number):
James E. Blairad28e912013-11-27 10:43:22 -0800371 change = self.changes.get(int(number))
372 if change:
373 return change.query()
374 return {}
James E. Blairb0fcae42012-07-17 11:12:10 -0700375
376 def startWatching(self, *args, **kw):
377 pass
378
379
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700380class BuildHistory(object):
381 def __init__(self, **kw):
382 self.__dict__.update(kw)
James E. Blairb0fcae42012-07-17 11:12:10 -0700383
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700384 def __repr__(self):
385 return ("<Completed build, result: %s name: %s #%s changes: %s>" %
386 (self.result, self.name, self.number, self.changes))
James E. Blairb0fcae42012-07-17 11:12:10 -0700387
388
James E. Blair8cc15a82012-08-01 11:17:57 -0700389class FakeURLOpener(object):
Monty Taylorbc758832013-06-17 17:22:42 -0400390 def __init__(self, upstream_root, fake_gerrit, url):
391 self.upstream_root = upstream_root
James E. Blair8cc15a82012-08-01 11:17:57 -0700392 self.fake_gerrit = fake_gerrit
393 self.url = url
394
395 def read(self):
396 res = urlparse.urlparse(self.url)
397 path = res.path
398 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200399 ret = '001e# service=git-upload-pack\n'
400 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
401 'multi_ack thin-pack side-band side-band-64k ofs-delta '
402 'shallow no-progress include-tag multi_ack_detailed no-done\n')
Monty Taylorbc758832013-06-17 17:22:42 -0400403 path = os.path.join(self.upstream_root, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700404 repo = git.Repo(path)
405 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200406 r = ref.object.hexsha + ' ' + ref.path + '\n'
407 ret += '%04x%s' % (len(r) + 4, r)
408 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700409 return ret
410
411
James E. Blair4886cc12012-07-18 15:39:41 -0700412class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000413 name = 'gerrit'
414
Monty Taylorbc758832013-06-17 17:22:42 -0400415 def __init__(self, upstream_root, *args):
416 super(FakeGerritTrigger, self).__init__(*args)
417 self.upstream_root = upstream_root
418
James E. Blair4886cc12012-07-18 15:39:41 -0700419 def getGitUrl(self, project):
Monty Taylorbc758832013-06-17 17:22:42 -0400420 return os.path.join(self.upstream_root, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700421
422
James E. Blair412e5582013-04-22 15:50:12 -0700423class FakeStatsd(threading.Thread):
424 def __init__(self):
425 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400426 self.daemon = True
James E. Blair412e5582013-04-22 15:50:12 -0700427 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
428 self.sock.bind(('', 0))
429 self.port = self.sock.getsockname()[1]
430 self.wake_read, self.wake_write = os.pipe()
431 self.stats = []
432
433 def run(self):
434 while True:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700435 poll = select.poll()
436 poll.register(self.sock, select.POLLIN)
437 poll.register(self.wake_read, select.POLLIN)
438 ret = poll.poll()
439 for (fd, event) in ret:
440 if fd == self.sock.fileno():
James E. Blair412e5582013-04-22 15:50:12 -0700441 data = self.sock.recvfrom(1024)
442 if not data:
443 return
444 self.stats.append(data[0])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700445 if fd == self.wake_read:
James E. Blair412e5582013-04-22 15:50:12 -0700446 return
447
448 def stop(self):
449 os.write(self.wake_write, '1\n')
450
451
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700452class FakeBuild(threading.Thread):
453 log = logging.getLogger("zuul.test")
454
455 def __init__(self, worker, job, number, node):
456 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400457 self.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700458 self.worker = worker
459 self.job = job
460 self.name = job.name.split(':')[1]
461 self.number = number
462 self.node = node
463 self.parameters = json.loads(job.arguments)
464 self.unique = self.parameters['ZUUL_UUID']
465 self.wait_condition = threading.Condition()
466 self.waiting = False
467 self.aborted = False
468 self.created = time.time()
469 self.description = ''
James E. Blair4a28a882013-08-23 15:17:33 -0700470 self.run_error = False
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700471
472 def release(self):
473 self.wait_condition.acquire()
474 self.wait_condition.notify()
475 self.waiting = False
476 self.log.debug("Build %s released" % self.unique)
477 self.wait_condition.release()
478
479 def isWaiting(self):
480 self.wait_condition.acquire()
481 if self.waiting:
482 ret = True
483 else:
484 ret = False
485 self.wait_condition.release()
486 return ret
487
488 def _wait(self):
489 self.wait_condition.acquire()
490 self.waiting = True
491 self.log.debug("Build %s waiting" % self.unique)
492 self.wait_condition.wait()
493 self.wait_condition.release()
494
495 def run(self):
496 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700497 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
498 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700499 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700500 'manager': self.worker.worker_id,
Joshua Heskethba8776a2014-01-12 14:35:40 +0800501 'worker_name': 'My Worker',
502 'worker_hostname': 'localhost',
503 'worker_ips': ['127.0.0.1', '192.168.1.1'],
504 'worker_fqdn': 'zuul.example.org',
505 'worker_program': 'FakeBuilder',
506 'worker_version': 'v1.1',
507 'worker_extra': {'something': 'else'}
Paul Belangerec49b4c2013-07-20 20:32:20 -0400508 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700509
Joshua Heskethba8776a2014-01-12 14:35:40 +0800510 self.log.debug('Running build %s' % self.unique)
511
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700512 self.job.sendWorkData(json.dumps(data))
Joshua Heskethba8776a2014-01-12 14:35:40 +0800513 self.log.debug('Sent WorkData packet with %s' % json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700514 self.job.sendWorkStatus(0, 100)
515
516 if self.worker.hold_jobs_in_build:
Joshua Heskethba8776a2014-01-12 14:35:40 +0800517 self.log.debug('Holding build %s' % self.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700518 self._wait()
519 self.log.debug("Build %s continuing" % self.unique)
520
521 self.worker.lock.acquire()
522
523 result = 'SUCCESS'
524 if (('ZUUL_REF' in self.parameters) and
525 self.worker.shouldFailTest(self.name,
526 self.parameters['ZUUL_REF'])):
527 result = 'FAILURE'
528 if self.aborted:
529 result = 'ABORTED'
530
James E. Blair4a28a882013-08-23 15:17:33 -0700531 if self.run_error:
532 work_fail = True
533 result = 'RUN_ERROR'
534 else:
535 data['result'] = result
536 work_fail = False
537
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700538 changes = None
539 if 'ZUUL_CHANGE_IDS' in self.parameters:
540 changes = self.parameters['ZUUL_CHANGE_IDS']
541
542 self.worker.build_history.append(
543 BuildHistory(name=self.name, number=self.number,
544 result=result, changes=changes, node=self.node,
James E. Blair64ed6f22013-07-10 14:07:23 -0700545 uuid=self.unique, description=self.description,
546 pipeline=self.parameters['ZUUL_PIPELINE'])
Paul Belangerec49b4c2013-07-20 20:32:20 -0400547 )
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700548
James E. Blair4a28a882013-08-23 15:17:33 -0700549 self.job.sendWorkData(json.dumps(data))
550 if work_fail:
551 self.job.sendWorkFail()
552 else:
553 self.job.sendWorkComplete(json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700554 del self.worker.gearman_jobs[self.job.unique]
555 self.worker.running_builds.remove(self)
556 self.worker.lock.release()
557
558
559class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400560 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700561 super(FakeWorker, self).__init__(worker_id)
562 self.gearman_jobs = {}
563 self.build_history = []
564 self.running_builds = []
565 self.build_counter = 0
566 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400567 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700568
569 self.hold_jobs_in_build = False
570 self.lock = threading.Lock()
571 self.__work_thread = threading.Thread(target=self.work)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400572 self.__work_thread.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700573 self.__work_thread.start()
574
575 def handleJob(self, job):
576 parts = job.name.split(":")
577 cmd = parts[0]
578 name = parts[1]
579 if len(parts) > 2:
580 node = parts[2]
581 else:
582 node = None
583 if cmd == 'build':
584 self.handleBuild(job, name, node)
585 elif cmd == 'stop':
586 self.handleStop(job, name)
587 elif cmd == 'set_description':
588 self.handleSetDescription(job, name)
589
590 def handleBuild(self, job, name, node):
591 build = FakeBuild(self, job, self.build_counter, node)
592 job.build = build
593 self.gearman_jobs[job.unique] = job
594 self.build_counter += 1
595
596 self.running_builds.append(build)
597 build.start()
598
599 def handleStop(self, job, name):
600 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700601 parameters = json.loads(job.arguments)
602 name = parameters['name']
603 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700604 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700605 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700606 build.aborted = True
607 build.release()
608 job.sendWorkComplete()
609 return
610 job.sendWorkFail()
611
612 def handleSetDescription(self, job, name):
613 self.log.debug("handle set description")
614 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700615 name = parameters['name']
616 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700617 descr = parameters['html_description']
618 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700619 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700620 build.description = descr
621 job.sendWorkComplete()
622 return
623 for build in self.build_history:
James E. Blair3c483cf2013-06-04 16:30:43 -0700624 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700625 build.description = descr
626 job.sendWorkComplete()
627 return
628 job.sendWorkFail()
629
630 def work(self):
631 while self.running:
632 try:
633 job = self.getJob()
634 except gear.InterruptedError:
635 continue
636 try:
637 self.handleJob(job)
638 except:
639 self.log.exception("Worker exception:")
640
641 def addFailTest(self, name, change):
642 l = self.fail_tests.get(name, [])
643 l.append(change)
644 self.fail_tests[name] = l
645
646 def shouldFailTest(self, name, ref):
647 l = self.fail_tests.get(name, [])
648 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400649 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700650 return True
651 return False
652
653 def release(self, regex=None):
654 builds = self.running_builds[:]
655 self.log.debug("releasing build %s (%s)" % (regex,
James E. Blair78e31b32013-07-09 09:11:34 -0700656 len(self.running_builds)))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700657 for build in builds:
658 if not regex or re.match(regex, build.name):
659 self.log.debug("releasing build %s" %
660 (build.parameters['ZUUL_UUID']))
661 build.release()
662 else:
663 self.log.debug("not releasing build %s" %
664 (build.parameters['ZUUL_UUID']))
665 self.log.debug("done releasing builds %s (%s)" %
666 (regex, len(self.running_builds)))
667
668
669class FakeGearmanServer(gear.Server):
670 def __init__(self):
671 self.hold_jobs_in_queue = False
672 super(FakeGearmanServer, self).__init__(0)
673
674 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700675 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
676 for job in queue:
677 if not hasattr(job, 'waiting'):
678 if job.name.startswith('build:'):
679 job.waiting = self.hold_jobs_in_queue
680 else:
681 job.waiting = False
682 if job.waiting:
683 continue
684 if job.name in connection.functions:
685 if not peek:
686 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400687 connection.related_jobs[job.handle] = job
688 job.worker_connection = connection
689 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700690 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700691 return None
692
693 def release(self, regex=None):
694 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700695 qlen = (len(self.high_queue) + len(self.normal_queue) +
696 len(self.low_queue))
697 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
James E. Blairdda6c912013-07-29 14:12:12 -0700698 for job in self.getQueue():
699 cmd, name = job.name.split(':')
700 if cmd != 'build':
701 continue
702 if not regex or re.match(regex, name):
703 self.log.debug("releasing queued job %s" %
704 job.unique)
705 job.waiting = False
706 released = True
707 else:
708 self.log.debug("not releasing queued job %s" %
709 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700710 if released:
711 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700712 qlen = (len(self.high_queue) + len(self.normal_queue) +
713 len(self.low_queue))
714 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700715
716
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000717class FakeSMTP(object):
718 log = logging.getLogger('zuul.FakeSMTP')
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000719
James E. Blairff80a2f2013-12-27 13:24:06 -0800720 def __init__(self, messages, server, port):
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000721 self.server = server
722 self.port = port
James E. Blairff80a2f2013-12-27 13:24:06 -0800723 self.messages = messages
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000724
725 def sendmail(self, from_email, to_email, msg):
726 self.log.info("Sending email from %s, to %s, with msg %s" % (
727 from_email, to_email, msg))
728
729 headers = msg.split('\n\n', 1)[0]
730 body = msg.split('\n\n', 1)[1]
731
James E. Blairff80a2f2013-12-27 13:24:06 -0800732 self.messages.append(dict(
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000733 from_email=from_email,
734 to_email=to_email,
735 msg=msg,
736 headers=headers,
737 body=body,
738 ))
739
740 return True
741
742 def quit(self):
743 return True
744
745
Monty Taylorbc758832013-06-17 17:22:42 -0400746class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700747 log = logging.getLogger("zuul.test")
748
749 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400750 super(TestScheduler, self).setUp()
751 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
752 try:
753 test_timeout = int(test_timeout)
754 except ValueError:
755 # If timeout value is invalid do not set a timeout.
756 test_timeout = 0
757 if test_timeout > 0:
James E. Blairff80a2f2013-12-27 13:24:06 -0800758 self.useFixture(fixtures.Timeout(test_timeout, gentle=False))
Monty Taylorbc758832013-06-17 17:22:42 -0400759
760 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
761 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
762 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
763 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
764 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
765 os.environ.get('OS_STDERR_CAPTURE') == '1'):
766 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
767 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Monty Taylore6a6c402013-07-02 09:25:55 -0700768 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
769 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400770 self.useFixture(fixtures.FakeLogger(
771 level=logging.DEBUG,
772 format='%(asctime)s %(name)-32s '
773 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700774 tmp_root = self.useFixture(fixtures.TempDir(
775 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400776 self.test_root = os.path.join(tmp_root, "zuul-test")
777 self.upstream_root = os.path.join(self.test_root, "upstream")
778 self.git_root = os.path.join(self.test_root, "git")
779
James E. Blair4076e2b2014-01-28 12:42:20 -0800780 CONFIG.set('merger', 'git_dir', self.git_root)
Monty Taylorbc758832013-06-17 17:22:42 -0400781 if os.path.exists(self.test_root):
782 shutil.rmtree(self.test_root)
783 os.makedirs(self.test_root)
784 os.makedirs(self.upstream_root)
785 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700786
787 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400788 self.init_repo("org/project")
789 self.init_repo("org/project1")
790 self.init_repo("org/project2")
791 self.init_repo("org/project3")
792 self.init_repo("org/one-job-project")
793 self.init_repo("org/nonvoting-project")
794 self.init_repo("org/templated-project")
James E. Blair3e98c022013-12-16 15:25:38 -0800795 self.init_repo("org/layered-project")
Monty Taylorbc758832013-06-17 17:22:42 -0400796 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700797 self.init_repo("org/conflict-project")
James E. Blair412e5582013-04-22 15:50:12 -0700798
799 self.statsd = FakeStatsd()
800 os.environ['STATSD_HOST'] = 'localhost'
801 os.environ['STATSD_PORT'] = str(self.statsd.port)
802 self.statsd.start()
803 # the statsd client object is configured in the statsd module import
804 reload(statsd)
805 reload(zuul.scheduler)
806
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700807 self.gearman_server = FakeGearmanServer()
808
809 self.config = ConfigParser.ConfigParser()
810 cfg = StringIO()
811 CONFIG.write(cfg)
812 cfg.seek(0)
813 self.config.readfp(cfg)
814 self.config.set('gearman', 'port', str(self.gearman_server.port))
815
Monty Taylorbc758832013-06-17 17:22:42 -0400816 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700817 self.worker.addServer('127.0.0.1', self.gearman_server.port)
818 self.gearman_server.worker = self.worker
819
James E. Blair4076e2b2014-01-28 12:42:20 -0800820 self.merge_server = zuul.merger.server.MergeServer(self.config)
821 self.merge_server.start()
822
James E. Blairb0fcae42012-07-17 11:12:10 -0700823 self.sched = zuul.scheduler.Scheduler()
824
James E. Blair8cc15a82012-08-01 11:17:57 -0700825 def URLOpenerFactory(*args, **kw):
826 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400827 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700828
James E. Blair8cc15a82012-08-01 11:17:57 -0700829 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700830 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blair4076e2b2014-01-28 12:42:20 -0800831 self.merge_client = zuul.merger.client.MergeClient(
832 self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700833
James E. Blairff80a2f2013-12-27 13:24:06 -0800834 self.smtp_messages = []
835
836 def FakeSMTPFactory(*args, **kw):
837 args = [self.smtp_messages] + list(args)
838 return FakeSMTP(*args, **kw)
839
James E. Blairb0fcae42012-07-17 11:12:10 -0700840 zuul.lib.gerrit.Gerrit = FakeGerrit
James E. Blairff80a2f2013-12-27 13:24:06 -0800841 self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTPFactory))
James E. Blairb0fcae42012-07-17 11:12:10 -0700842
Monty Taylorbc758832013-06-17 17:22:42 -0400843 self.gerrit = FakeGerritTrigger(
844 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700845 self.gerrit.replication_timeout = 1.5
846 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700847 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400848 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700849
James E. Blair1843a552013-07-03 14:19:52 -0700850 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
James E. Blairad28e912013-11-27 10:43:22 -0800851 self.rpc = zuul.rpclistener.RPCListener(self.config, self.sched)
James E. Blair1843a552013-07-03 14:19:52 -0700852
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700853 self.sched.setLauncher(self.launcher)
James E. Blair4076e2b2014-01-28 12:42:20 -0800854 self.sched.setMerger(self.merge_client)
James E. Blair6c358e72013-07-29 17:06:47 -0700855 self.sched.registerTrigger(self.gerrit)
James E. Blair63bb0ef2013-07-29 17:14:51 -0700856 self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
857 self.sched.registerTrigger(self.timer)
James E. Blairb0fcae42012-07-17 11:12:10 -0700858
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000859 self.sched.registerReporter(
860 zuul.reporter.gerrit.Reporter(self.gerrit))
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000861 self.smtp_reporter = zuul.reporter.smtp.Reporter(
862 self.config.get('smtp', 'default_from'),
863 self.config.get('smtp', 'default_to'),
864 self.config.get('smtp', 'server'))
865 self.sched.registerReporter(self.smtp_reporter)
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000866
James E. Blairb0fcae42012-07-17 11:12:10 -0700867 self.sched.start()
868 self.sched.reconfigure(self.config)
869 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700870 self.webapp.start()
James E. Blairad28e912013-11-27 10:43:22 -0800871 self.rpc.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700872 self.launcher.gearman.waitForServer()
873 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400874 self.builds = self.worker.running_builds
875 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700876
James E. Blairfee8d652013-06-07 08:57:52 -0700877 self.addCleanup(self.assertFinalState)
878 self.addCleanup(self.shutdown)
879
880 def assertFinalState(self):
881 # Make sure that the change cache is cleared
James E. Blair6c358e72013-07-29 17:06:47 -0700882 self.assertEqual(len(self.gerrit._change_cache.keys()), 0)
Clark Boylan4ba48d92013-11-11 18:03:53 -0800883 # Make sure that git.Repo objects have been garbage collected.
884 repos = []
885 gc.collect()
886 for obj in gc.get_objects():
887 if isinstance(obj, git.Repo):
888 repos.append(obj)
889 self.assertEqual(len(repos), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700890 self.assertEmptyQueues()
891
892 def shutdown(self):
893 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700894 self.launcher.stop()
James E. Blair4076e2b2014-01-28 12:42:20 -0800895 self.merge_server.stop()
896 self.merge_server.join()
897 self.merge_client.stop()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700898 self.worker.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700899 self.gerrit.stop()
James E. Blair63bb0ef2013-07-29 17:14:51 -0700900 self.timer.stop()
James E. Blairb0fcae42012-07-17 11:12:10 -0700901 self.sched.stop()
902 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700903 self.statsd.stop()
904 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700905 self.webapp.stop()
906 self.webapp.join()
James E. Blairad28e912013-11-27 10:43:22 -0800907 self.rpc.stop()
908 self.rpc.join()
James E. Blair491b3172014-03-10 15:05:36 -0700909 self.gearman_server.shutdown()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700910 threads = threading.enumerate()
911 if len(threads) > 1:
912 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400913 super(TestScheduler, self).tearDown()
914
915 def init_repo(self, project):
916 parts = project.split('/')
917 path = os.path.join(self.upstream_root, *parts[:-1])
918 if not os.path.exists(path):
919 os.makedirs(path)
920 path = os.path.join(self.upstream_root, project)
921 repo = git.Repo.init(path)
922
923 repo.config_writer().set_value('user', 'email', 'user@example.com')
924 repo.config_writer().set_value('user', 'name', 'User Name')
925 repo.config_writer().write()
926
927 fn = os.path.join(path, 'README')
928 f = open(fn, 'w')
929 f.write("test\n")
930 f.close()
931 repo.index.add([fn])
932 repo.index.commit('initial commit')
933 master = repo.create_head('master')
934 repo.create_tag('init')
935
936 mp = repo.create_head('mp')
937 repo.head.reference = mp
938 f = open(fn, 'a')
939 f.write("test mp\n")
940 f.close()
941 repo.index.add([fn])
942 repo.index.commit('mp commit')
943
944 repo.head.reference = master
945 repo.head.reset(index=True, working_tree=True)
946 repo.git.clean('-x', '-f', '-d')
947
948 def ref_has_change(self, ref, change):
949 path = os.path.join(self.git_root, change.project)
950 repo = git.Repo(path)
951 for commit in repo.iter_commits(ref):
952 if commit.message.strip() == ('%s-1' % change.subject):
953 return True
954 return False
955
956 def job_has_changes(self, *args):
957 job = args[0]
958 commits = args[1:]
959 if isinstance(job, FakeBuild):
960 parameters = job.parameters
961 else:
962 parameters = json.loads(job.arguments)
963 project = parameters['ZUUL_PROJECT']
964 path = os.path.join(self.git_root, project)
965 repo = git.Repo(path)
966 ref = parameters['ZUUL_REF']
967 sha = parameters['ZUUL_COMMIT']
968 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
969 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
970 commit_messages = ['%s-1' % commit.subject for commit in commits]
James E. Blairac2c3242014-01-24 13:38:51 -0800971 self.log.debug("Checking if job %s has changes; commit_messages %s;"
972 " repo_messages %s; sha %s" % (job, commit_messages,
973 repo_messages, sha))
Monty Taylorbc758832013-06-17 17:22:42 -0400974 for msg in commit_messages:
975 if msg not in repo_messages:
James E. Blairac2c3242014-01-24 13:38:51 -0800976 self.log.debug(" messages do not match")
Monty Taylorbc758832013-06-17 17:22:42 -0400977 return False
978 if repo_shas[0] != sha:
James E. Blairac2c3242014-01-24 13:38:51 -0800979 self.log.debug(" sha does not match")
Monty Taylorbc758832013-06-17 17:22:42 -0400980 return False
James E. Blairac2c3242014-01-24 13:38:51 -0800981 self.log.debug(" OK")
Monty Taylorbc758832013-06-17 17:22:42 -0400982 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700983
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700984 def registerJobs(self):
985 count = 0
James E. Blaireff88162013-07-01 12:44:14 -0400986 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700987 self.worker.registerFunction('build:' + job)
988 count += 1
989 self.worker.registerFunction('stop:' + self.worker.worker_id)
990 count += 1
991
992 while len(self.gearman_server.functions) < count:
993 time.sleep(0)
994
995 def release(self, job):
996 if isinstance(job, FakeBuild):
997 job.release()
998 else:
999 job.waiting = False
1000 self.log.debug("Queued job %s released" % job.unique)
1001 self.gearman_server.wakeConnections()
1002
1003 def getParameter(self, job, name):
1004 if isinstance(job, FakeBuild):
1005 return job.parameters[name]
1006 else:
1007 parameters = json.loads(job.arguments)
1008 return parameters[name]
1009
1010 def resetGearmanServer(self):
1011 self.worker.setFunctions([])
1012 while True:
1013 done = True
1014 for connection in self.gearman_server.active_connections:
James E. Blairad28e912013-11-27 10:43:22 -08001015 if (connection.functions and
James E. Blair4076e2b2014-01-28 12:42:20 -08001016 connection.client_id not in ['Zuul RPC Listener',
1017 'Zuul Merger']):
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001018 done = False
1019 if done:
1020 break
1021 time.sleep(0)
1022 self.gearman_server.functions = set()
James E. Blairad28e912013-11-27 10:43:22 -08001023 self.rpc.register()
James E. Blair4076e2b2014-01-28 12:42:20 -08001024 self.merge_server.register()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001025
1026 def haveAllBuildsReported(self):
1027 # See if Zuul is waiting on a meta job to complete
1028 if self.launcher.meta_jobs:
1029 return False
1030 # Find out if every build that the worker has completed has been
1031 # reported back to Zuul. If it hasn't then that means a Gearman
1032 # event is still in transit and the system is not stable.
1033 for build in self.worker.build_history:
1034 zbuild = self.launcher.builds.get(build.uuid)
1035 if not zbuild:
1036 # It has already been reported
1037 continue
1038 # It hasn't been reported yet.
1039 return False
1040 # Make sure that none of the worker connections are in GRAB_WAIT
1041 for connection in self.worker.active_connections:
1042 if connection.state == 'GRAB_WAIT':
1043 return False
1044 return True
1045
1046 def areAllBuildsWaiting(self):
1047 ret = True
1048
1049 builds = self.launcher.builds.values()
1050 for build in builds:
1051 client_job = None
1052 for conn in self.launcher.gearman.active_connections:
1053 for j in conn.related_jobs.values():
1054 if j.unique == build.uuid:
1055 client_job = j
1056 break
1057 if not client_job:
1058 self.log.debug("%s is not known to the gearman client" %
1059 build)
1060 ret = False
1061 continue
1062 if not client_job.handle:
1063 self.log.debug("%s has no handle" % client_job)
1064 ret = False
1065 continue
1066 server_job = self.gearman_server.jobs.get(client_job.handle)
1067 if not server_job:
1068 self.log.debug("%s is not known to the gearman server" %
1069 client_job)
1070 ret = False
1071 continue
1072 if not hasattr(server_job, 'waiting'):
1073 self.log.debug("%s is being enqueued" % server_job)
1074 ret = False
1075 continue
1076 if server_job.waiting:
1077 continue
1078 worker_job = self.worker.gearman_jobs.get(server_job.unique)
1079 if worker_job:
1080 if worker_job.build.isWaiting():
1081 continue
1082 else:
1083 self.log.debug("%s is running" % worker_job)
1084 ret = False
1085 else:
1086 self.log.debug("%s is unassigned" % server_job)
1087 ret = False
1088 return ret
1089
James E. Blairb0fcae42012-07-17 11:12:10 -07001090 def waitUntilSettled(self):
1091 self.log.debug("Waiting until settled...")
1092 start = time.time()
1093 while True:
1094 if time.time() - start > 10:
1095 print 'queue status:',
1096 print self.sched.trigger_event_queue.empty(),
1097 print self.sched.result_event_queue.empty(),
1098 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001099 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -07001100 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001101 # Make sure no new events show up while we're checking
1102 self.worker.lock.acquire()
1103 # have all build states propogated to zuul?
1104 if self.haveAllBuildsReported():
1105 # Join ensures that the queue is empty _and_ events have been
1106 # processed
1107 self.fake_gerrit.event_queue.join()
1108 self.sched.trigger_event_queue.join()
1109 self.sched.result_event_queue.join()
James E. Blaira84f0e42014-02-06 07:09:22 -08001110 self.sched.run_handler_lock.acquire()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001111 if (self.sched.trigger_event_queue.empty() and
1112 self.sched.result_event_queue.empty() and
1113 self.fake_gerrit.event_queue.empty() and
James E. Blair4076e2b2014-01-28 12:42:20 -08001114 not self.merge_client.build_sets and
James E. Blair9c2985a2014-03-10 10:59:05 -07001115 self.haveAllBuildsReported() and
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001116 self.areAllBuildsWaiting()):
James E. Blaira84f0e42014-02-06 07:09:22 -08001117 self.sched.run_handler_lock.release()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001118 self.worker.lock.release()
1119 self.log.debug("...settled.")
1120 return
James E. Blaira84f0e42014-02-06 07:09:22 -08001121 self.sched.run_handler_lock.release()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001122 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001123 self.sched.wake_event.wait(0.1)
1124
James E. Blaird466dc42012-07-31 10:42:56 -07001125 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -08001126 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -07001127 return len(jobs)
1128
James E. Blair4ca985f2013-05-30 12:27:43 -07001129 def getJobFromHistory(self, name):
1130 history = self.worker.build_history
1131 for job in history:
1132 if job.name == name:
1133 return job
1134 raise Exception("Unable to find job %s in history" % name)
1135
James E. Blaire0487072012-08-29 17:38:31 -07001136 def assertEmptyQueues(self):
1137 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001138 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001139 for queue in pipeline.queues:
1140 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001141 print 'pipeline %s queue %s contents %s' % (
1142 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001143 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001144
James E. Blair66eeebf2013-07-27 17:44:32 -07001145 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001146 start = time.time()
1147 while time.time() < (start + 5):
1148 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001149 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001150 k, v = stat.split(':')
1151 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001152 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001153 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001154 elif value:
1155 if value == v:
1156 return
1157 elif kind:
1158 if v.endswith('|' + kind):
1159 return
James E. Blair412e5582013-04-22 15:50:12 -07001160 time.sleep(0.1)
1161
1162 pprint.pprint(self.statsd.stats)
1163 raise Exception("Key %s not found in reported stats" % key)
1164
James E. Blairb0fcae42012-07-17 11:12:10 -07001165 def test_jobs_launched(self):
1166 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001167
James E. Blairb0fcae42012-07-17 11:12:10 -07001168 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001169 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001170 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1171 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001172 self.assertEqual(self.getJobFromHistory('project-merge').result,
1173 'SUCCESS')
1174 self.assertEqual(self.getJobFromHistory('project-test1').result,
1175 'SUCCESS')
1176 self.assertEqual(self.getJobFromHistory('project-test2').result,
1177 'SUCCESS')
1178 self.assertEqual(A.data['status'], 'MERGED')
1179 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001180
James E. Blair66eeebf2013-07-27 17:44:32 -07001181 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1182 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1183 value='1|g')
1184 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1185 kind='ms')
1186 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1187 value='1|c')
1188 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1189 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1190 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001191 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001192 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001193 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001194 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001195
James E. Blair3cb10702013-08-24 08:56:03 -07001196 def test_initial_pipeline_gauges(self):
1197 "Test that each pipeline reported its length on start"
1198 pipeline_names = self.sched.layout.pipelines.keys()
1199 self.assertNotEqual(len(pipeline_names), 0)
1200 for name in pipeline_names:
1201 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
1202 value='0|g')
1203
James E. Blair42f74822013-05-14 15:18:03 -07001204 def test_duplicate_pipelines(self):
1205 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001206
James E. Blair42f74822013-05-14 15:18:03 -07001207 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1208 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1209 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001210
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001211 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001212 self.history[0].name == 'project-test1'
1213 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001214
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001215 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001216 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001217 self.assertIn('dup1/project-test1', A.messages[0])
1218 self.assertNotIn('dup2/project-test1', A.messages[0])
1219 self.assertNotIn('dup1/project-test1', A.messages[1])
1220 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001221 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001222 self.assertIn('dup1/project-test1', A.messages[1])
1223 self.assertNotIn('dup2/project-test1', A.messages[1])
1224 self.assertNotIn('dup1/project-test1', A.messages[0])
1225 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001226
James E. Blairb0fcae42012-07-17 11:12:10 -07001227 def test_parallel_changes(self):
1228 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001229
1230 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001231 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1232 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1233 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001234 A.addApproval('CRVW', 2)
1235 B.addApproval('CRVW', 2)
1236 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001237
1238 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1239 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1240 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1241
1242 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001243 self.assertEqual(len(self.builds), 1)
1244 self.assertEqual(self.builds[0].name, 'project-merge')
1245 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001246
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001247 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001248 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001249 self.assertEqual(len(self.builds), 3)
1250 self.assertEqual(self.builds[0].name, 'project-test1')
1251 self.assertTrue(self.job_has_changes(self.builds[0], A))
1252 self.assertEqual(self.builds[1].name, 'project-test2')
1253 self.assertTrue(self.job_has_changes(self.builds[1], A))
1254 self.assertEqual(self.builds[2].name, 'project-merge')
1255 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001256
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001257 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001258 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001259 self.assertEqual(len(self.builds), 5)
1260 self.assertEqual(self.builds[0].name, 'project-test1')
1261 self.assertTrue(self.job_has_changes(self.builds[0], A))
1262 self.assertEqual(self.builds[1].name, 'project-test2')
1263 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001264
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001265 self.assertEqual(self.builds[2].name, 'project-test1')
1266 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1267 self.assertEqual(self.builds[3].name, 'project-test2')
1268 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001269
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001270 self.assertEqual(self.builds[4].name, 'project-merge')
1271 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001272
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001273 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001274 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001275 self.assertEqual(len(self.builds), 6)
1276 self.assertEqual(self.builds[0].name, 'project-test1')
1277 self.assertTrue(self.job_has_changes(self.builds[0], A))
1278 self.assertEqual(self.builds[1].name, 'project-test2')
1279 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001280
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001281 self.assertEqual(self.builds[2].name, 'project-test1')
1282 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1283 self.assertEqual(self.builds[3].name, 'project-test2')
1284 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001285
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001286 self.assertEqual(self.builds[4].name, 'project-test1')
1287 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1288 self.assertEqual(self.builds[5].name, 'project-test2')
1289 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001290
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001291 self.worker.hold_jobs_in_build = False
1292 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001293 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001294 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001295
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001296 self.assertEqual(len(self.history), 9)
1297 self.assertEqual(A.data['status'], 'MERGED')
1298 self.assertEqual(B.data['status'], 'MERGED')
1299 self.assertEqual(C.data['status'], 'MERGED')
1300 self.assertEqual(A.reported, 2)
1301 self.assertEqual(B.reported, 2)
1302 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001303
1304 def test_failed_changes(self):
1305 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001306 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001307
James E. Blairb02a3bb2012-07-30 17:49:55 -07001308 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1309 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001310 A.addApproval('CRVW', 2)
1311 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001312
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001313 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001314
James E. Blaire2819012013-06-28 17:17:26 -04001315 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1316 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001317 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001318
1319 self.worker.release('.*-merge')
1320 self.waitUntilSettled()
1321
1322 self.worker.hold_jobs_in_build = False
1323 self.worker.release()
1324
1325 self.waitUntilSettled()
1326 # It's certain that the merge job for change 2 will run, but
1327 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001328 self.assertTrue(len(self.history) > 6)
1329 self.assertEqual(A.data['status'], 'NEW')
1330 self.assertEqual(B.data['status'], 'MERGED')
1331 self.assertEqual(A.reported, 2)
1332 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001333
1334 def test_independent_queues(self):
1335 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001336
1337 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001338 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001339 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1340 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001341 A.addApproval('CRVW', 2)
1342 B.addApproval('CRVW', 2)
1343 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001344
1345 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1346 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1347 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1348
James E. Blairb02a3bb2012-07-30 17:49:55 -07001349 self.waitUntilSettled()
1350
1351 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001352 self.assertEqual(len(self.builds), 2)
1353 self.assertEqual(self.builds[0].name, 'project-merge')
1354 self.assertTrue(self.job_has_changes(self.builds[0], A))
1355 self.assertEqual(self.builds[1].name, 'project1-merge')
1356 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001357
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001358 # Release the current merge builds
1359 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001360 self.waitUntilSettled()
1361 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001362 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001363 self.waitUntilSettled()
1364
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001365 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001366 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001367 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001368
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001369 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001370 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001371 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001372
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001373 self.assertEqual(len(self.history), 11)
1374 self.assertEqual(A.data['status'], 'MERGED')
1375 self.assertEqual(B.data['status'], 'MERGED')
1376 self.assertEqual(C.data['status'], 'MERGED')
1377 self.assertEqual(A.reported, 2)
1378 self.assertEqual(B.reported, 2)
1379 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001380
1381 def test_failed_change_at_head(self):
1382 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001383
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001384 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001385 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1386 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1387 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001388 A.addApproval('CRVW', 2)
1389 B.addApproval('CRVW', 2)
1390 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001391
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001392 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001393
1394 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1395 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1396 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1397
1398 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001399
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001400 self.assertEqual(len(self.builds), 1)
1401 self.assertEqual(self.builds[0].name, 'project-merge')
1402 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001403
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001404 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001405 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001406 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001407 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001408 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001409 self.waitUntilSettled()
1410
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001411 self.assertEqual(len(self.builds), 6)
1412 self.assertEqual(self.builds[0].name, 'project-test1')
1413 self.assertEqual(self.builds[1].name, 'project-test2')
1414 self.assertEqual(self.builds[2].name, 'project-test1')
1415 self.assertEqual(self.builds[3].name, 'project-test2')
1416 self.assertEqual(self.builds[4].name, 'project-test1')
1417 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001418
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001419 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001420 self.waitUntilSettled()
1421
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001422 # project-test2, project-merge for B
1423 self.assertEqual(len(self.builds), 2)
1424 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001425
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001426 self.worker.hold_jobs_in_build = False
1427 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001428 self.waitUntilSettled()
1429
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001430 self.assertEqual(len(self.builds), 0)
1431 self.assertEqual(len(self.history), 15)
1432 self.assertEqual(A.data['status'], 'NEW')
1433 self.assertEqual(B.data['status'], 'MERGED')
1434 self.assertEqual(C.data['status'], 'MERGED')
1435 self.assertEqual(A.reported, 2)
1436 self.assertEqual(B.reported, 2)
1437 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001438
James E. Blair0aac4872013-08-23 14:02:38 -07001439 def test_failed_change_in_middle(self):
1440 "Test a failed change in the middle of the queue"
1441
1442 self.worker.hold_jobs_in_build = True
1443 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1444 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1445 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1446 A.addApproval('CRVW', 2)
1447 B.addApproval('CRVW', 2)
1448 C.addApproval('CRVW', 2)
1449
1450 self.worker.addFailTest('project-test1', B)
1451
1452 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1453 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1454 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1455
1456 self.waitUntilSettled()
1457
1458 self.worker.release('.*-merge')
1459 self.waitUntilSettled()
1460 self.worker.release('.*-merge')
1461 self.waitUntilSettled()
1462 self.worker.release('.*-merge')
1463 self.waitUntilSettled()
1464
1465 self.assertEqual(len(self.builds), 6)
1466 self.assertEqual(self.builds[0].name, 'project-test1')
1467 self.assertEqual(self.builds[1].name, 'project-test2')
1468 self.assertEqual(self.builds[2].name, 'project-test1')
1469 self.assertEqual(self.builds[3].name, 'project-test2')
1470 self.assertEqual(self.builds[4].name, 'project-test1')
1471 self.assertEqual(self.builds[5].name, 'project-test2')
1472
1473 self.release(self.builds[2])
1474 self.waitUntilSettled()
1475
James E. Blair972e3c72013-08-29 12:04:55 -07001476 # project-test1 and project-test2 for A
1477 # project-test2 for B
1478 # project-merge for C (without B)
1479 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -07001480 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1481
James E. Blair972e3c72013-08-29 12:04:55 -07001482 self.worker.release('.*-merge')
1483 self.waitUntilSettled()
1484
1485 # project-test1 and project-test2 for A
1486 # project-test2 for B
1487 # project-test1 and project-test2 for C
1488 self.assertEqual(len(self.builds), 5)
1489
James E. Blair0aac4872013-08-23 14:02:38 -07001490 items = self.sched.layout.pipelines['gate'].getAllItems()
1491 builds = items[0].current_build_set.getBuilds()
1492 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1493 self.assertEqual(self.countJobResults(builds, None), 2)
1494 builds = items[1].current_build_set.getBuilds()
1495 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1496 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1497 self.assertEqual(self.countJobResults(builds, None), 1)
1498 builds = items[2].current_build_set.getBuilds()
1499 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -07001500 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -07001501
1502 self.worker.hold_jobs_in_build = False
1503 self.worker.release()
1504 self.waitUntilSettled()
1505
1506 self.assertEqual(len(self.builds), 0)
1507 self.assertEqual(len(self.history), 12)
1508 self.assertEqual(A.data['status'], 'MERGED')
1509 self.assertEqual(B.data['status'], 'NEW')
1510 self.assertEqual(C.data['status'], 'MERGED')
1511 self.assertEqual(A.reported, 2)
1512 self.assertEqual(B.reported, 2)
1513 self.assertEqual(C.reported, 2)
1514
James E. Blaird466dc42012-07-31 10:42:56 -07001515 def test_failed_change_at_head_with_queue(self):
1516 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001517
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001518 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001519 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1520 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1521 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001522 A.addApproval('CRVW', 2)
1523 B.addApproval('CRVW', 2)
1524 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001525
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001526 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001527
1528 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1529 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1530 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1531
1532 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001533 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001534 self.assertEqual(len(self.builds), 0)
1535 self.assertEqual(len(queue), 1)
1536 self.assertEqual(queue[0].name, 'build:project-merge')
1537 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001538
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001539 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001540 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001541 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001542 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001543 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001544 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001545 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001546
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001547 self.assertEqual(len(self.builds), 0)
1548 self.assertEqual(len(queue), 6)
1549 self.assertEqual(queue[0].name, 'build:project-test1')
1550 self.assertEqual(queue[1].name, 'build:project-test2')
1551 self.assertEqual(queue[2].name, 'build:project-test1')
1552 self.assertEqual(queue[3].name, 'build:project-test2')
1553 self.assertEqual(queue[4].name, 'build:project-test1')
1554 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001555
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001556 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001557 self.waitUntilSettled()
1558
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001559 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001560 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001561 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1562 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001563
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001564 self.gearman_server.hold_jobs_in_queue = False
1565 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001566 self.waitUntilSettled()
1567
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001568 self.assertEqual(len(self.builds), 0)
1569 self.assertEqual(len(self.history), 11)
1570 self.assertEqual(A.data['status'], 'NEW')
1571 self.assertEqual(B.data['status'], 'MERGED')
1572 self.assertEqual(C.data['status'], 'MERGED')
1573 self.assertEqual(A.reported, 2)
1574 self.assertEqual(B.reported, 2)
1575 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001576
James E. Blairfef71632013-09-23 11:15:47 -07001577 def test_two_failed_changes_at_head(self):
1578 "Test that changes are reparented correctly if 2 fail at head"
1579
1580 self.worker.hold_jobs_in_build = True
1581 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1582 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1583 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1584 A.addApproval('CRVW', 2)
1585 B.addApproval('CRVW', 2)
1586 C.addApproval('CRVW', 2)
1587
1588 self.worker.addFailTest('project-test1', A)
1589 self.worker.addFailTest('project-test1', B)
1590
1591 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1592 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1593 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1594 self.waitUntilSettled()
1595
1596 self.worker.release('.*-merge')
1597 self.waitUntilSettled()
1598 self.worker.release('.*-merge')
1599 self.waitUntilSettled()
1600 self.worker.release('.*-merge')
1601 self.waitUntilSettled()
1602
1603 self.assertEqual(len(self.builds), 6)
1604 self.assertEqual(self.builds[0].name, 'project-test1')
1605 self.assertEqual(self.builds[1].name, 'project-test2')
1606 self.assertEqual(self.builds[2].name, 'project-test1')
1607 self.assertEqual(self.builds[3].name, 'project-test2')
1608 self.assertEqual(self.builds[4].name, 'project-test1')
1609 self.assertEqual(self.builds[5].name, 'project-test2')
1610
1611 self.assertTrue(self.job_has_changes(self.builds[0], A))
1612 self.assertTrue(self.job_has_changes(self.builds[2], A))
1613 self.assertTrue(self.job_has_changes(self.builds[2], B))
1614 self.assertTrue(self.job_has_changes(self.builds[4], A))
1615 self.assertTrue(self.job_has_changes(self.builds[4], B))
1616 self.assertTrue(self.job_has_changes(self.builds[4], C))
1617
1618 # Fail change B first
1619 self.release(self.builds[2])
1620 self.waitUntilSettled()
1621
1622 # restart of C after B failure
1623 self.worker.release('.*-merge')
1624 self.waitUntilSettled()
1625
1626 self.assertEqual(len(self.builds), 5)
1627 self.assertEqual(self.builds[0].name, 'project-test1')
1628 self.assertEqual(self.builds[1].name, 'project-test2')
1629 self.assertEqual(self.builds[2].name, 'project-test2')
1630 self.assertEqual(self.builds[3].name, 'project-test1')
1631 self.assertEqual(self.builds[4].name, 'project-test2')
1632
1633 self.assertTrue(self.job_has_changes(self.builds[1], A))
1634 self.assertTrue(self.job_has_changes(self.builds[2], A))
1635 self.assertTrue(self.job_has_changes(self.builds[2], B))
1636 self.assertTrue(self.job_has_changes(self.builds[4], A))
1637 self.assertFalse(self.job_has_changes(self.builds[4], B))
1638 self.assertTrue(self.job_has_changes(self.builds[4], C))
1639
1640 # Finish running all passing jobs for change A
1641 self.release(self.builds[1])
1642 self.waitUntilSettled()
1643 # Fail and report change A
1644 self.release(self.builds[0])
1645 self.waitUntilSettled()
1646
1647 # restart of B,C after A failure
1648 self.worker.release('.*-merge')
1649 self.waitUntilSettled()
1650 self.worker.release('.*-merge')
1651 self.waitUntilSettled()
1652
1653 self.assertEqual(len(self.builds), 4)
1654 self.assertEqual(self.builds[0].name, 'project-test1') # B
1655 self.assertEqual(self.builds[1].name, 'project-test2') # B
1656 self.assertEqual(self.builds[2].name, 'project-test1') # C
1657 self.assertEqual(self.builds[3].name, 'project-test2') # C
1658
1659 self.assertFalse(self.job_has_changes(self.builds[1], A))
1660 self.assertTrue(self.job_has_changes(self.builds[1], B))
1661 self.assertFalse(self.job_has_changes(self.builds[1], C))
1662
1663 self.assertFalse(self.job_has_changes(self.builds[2], A))
1664 # After A failed and B and C restarted, B should be back in
1665 # C's tests because it has not failed yet.
1666 self.assertTrue(self.job_has_changes(self.builds[2], B))
1667 self.assertTrue(self.job_has_changes(self.builds[2], C))
1668
1669 self.worker.hold_jobs_in_build = False
1670 self.worker.release()
1671 self.waitUntilSettled()
1672
1673 self.assertEqual(len(self.builds), 0)
1674 self.assertEqual(len(self.history), 21)
1675 self.assertEqual(A.data['status'], 'NEW')
1676 self.assertEqual(B.data['status'], 'NEW')
1677 self.assertEqual(C.data['status'], 'MERGED')
1678 self.assertEqual(A.reported, 2)
1679 self.assertEqual(B.reported, 2)
1680 self.assertEqual(C.reported, 2)
1681
James E. Blair8c803f82012-07-31 16:25:42 -07001682 def test_patch_order(self):
1683 "Test that dependent patches are tested in the right order"
1684 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1685 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1686 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1687 A.addApproval('CRVW', 2)
1688 B.addApproval('CRVW', 2)
1689 C.addApproval('CRVW', 2)
1690
1691 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1692 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1693 M2.setMerged()
1694 M1.setMerged()
1695
1696 # C -> B -> A -> M1 -> M2
1697 # M2 is here to make sure it is never queried. If it is, it
1698 # means zuul is walking down the entire history of merged
1699 # changes.
1700
1701 C.setDependsOn(B, 1)
1702 B.setDependsOn(A, 1)
1703 A.setDependsOn(M1, 1)
1704 M1.setDependsOn(M2, 1)
1705
1706 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1707
1708 self.waitUntilSettled()
1709
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001710 self.assertEqual(A.data['status'], 'NEW')
1711 self.assertEqual(B.data['status'], 'NEW')
1712 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001713
1714 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1715 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1716
1717 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001718 self.assertEqual(M2.queried, 0)
1719 self.assertEqual(A.data['status'], 'MERGED')
1720 self.assertEqual(B.data['status'], 'MERGED')
1721 self.assertEqual(C.data['status'], 'MERGED')
1722 self.assertEqual(A.reported, 2)
1723 self.assertEqual(B.reported, 2)
1724 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001725
James E. Blair0e933c52013-07-11 10:18:52 -07001726 def test_trigger_cache(self):
1727 "Test that the trigger cache operates correctly"
1728 self.worker.hold_jobs_in_build = True
1729
1730 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1731 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1732 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1733 A.addApproval('CRVW', 2)
1734 B.addApproval('CRVW', 2)
1735
1736 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1737 M1.setMerged()
1738
1739 B.setDependsOn(A, 1)
1740 A.setDependsOn(M1, 1)
1741
1742 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1743 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1744
1745 self.waitUntilSettled()
1746
1747 for build in self.builds:
1748 if build.parameters['ZUUL_PIPELINE'] == 'check':
1749 build.release()
1750 self.waitUntilSettled()
1751 for build in self.builds:
1752 if build.parameters['ZUUL_PIPELINE'] == 'check':
1753 build.release()
1754 self.waitUntilSettled()
1755
1756 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1757 self.waitUntilSettled()
1758
James E. Blair6c358e72013-07-29 17:06:47 -07001759 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001760 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001761 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001762
1763 self.worker.hold_jobs_in_build = False
1764 self.worker.release()
1765 self.waitUntilSettled()
1766
1767 self.assertEqual(A.data['status'], 'MERGED')
1768 self.assertEqual(B.data['status'], 'MERGED')
1769 self.assertEqual(A.queried, 2) # Initial and isMerged
1770 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1771
James E. Blair8c803f82012-07-31 16:25:42 -07001772 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001773 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001774 # TODO: move to test_gerrit (this is a unit test!)
1775 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001776 trigger = self.sched.layout.pipelines['gate'].trigger
1777 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001778 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001779 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001780
1781 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001782 a = trigger.getChange(1, 2, refresh=True)
1783 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001784
1785 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001786 a = trigger.getChange(1, 2, refresh=True)
1787 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1788 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001789
1790 def test_build_configuration(self):
1791 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001792
1793 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001794 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1795 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1796 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1797 A.addApproval('CRVW', 2)
1798 B.addApproval('CRVW', 2)
1799 C.addApproval('CRVW', 2)
1800 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1801 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1802 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1803 self.waitUntilSettled()
1804
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001805 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001806 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001807 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001808 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001809 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001810 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001811 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001812 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1813 self.gearman_server.hold_jobs_in_queue = False
1814 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001815 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001816
Monty Taylorbc758832013-06-17 17:22:42 -04001817 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001818 repo = git.Repo(path)
1819 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1820 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001821 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001822 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001823
1824 def test_build_configuration_conflict(self):
1825 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001826
1827 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001828 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1829 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001830 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001831 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1832 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001833 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001834 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1835 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001836 A.addApproval('CRVW', 2)
1837 B.addApproval('CRVW', 2)
1838 C.addApproval('CRVW', 2)
1839 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1840 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1841 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1842 self.waitUntilSettled()
1843
James E. Blair6736beb2013-07-11 15:18:15 -07001844 self.assertEqual(A.reported, 1)
1845 self.assertEqual(B.reported, 1)
1846 self.assertEqual(C.reported, 1)
1847
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001848 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001849 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001850 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001851 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001852 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001853 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001854
1855 self.assertEqual(len(self.history), 2) # A and C merge jobs
1856
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001857 self.gearman_server.hold_jobs_in_queue = False
1858 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001859 self.waitUntilSettled()
1860
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001861 self.assertEqual(A.data['status'], 'MERGED')
1862 self.assertEqual(B.data['status'], 'NEW')
1863 self.assertEqual(C.data['status'], 'MERGED')
1864 self.assertEqual(A.reported, 2)
1865 self.assertEqual(B.reported, 2)
1866 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001867 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001868
James E. Blairdaabed22012-08-15 15:38:57 -07001869 def test_post(self):
1870 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001871
Zhongyue Luo5d556072012-09-21 02:00:47 +09001872 e = {
1873 "type": "ref-updated",
1874 "submitter": {
1875 "name": "User Name",
1876 },
1877 "refUpdate": {
1878 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1879 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1880 "refName": "master",
1881 "project": "org/project",
1882 }
1883 }
James E. Blairdaabed22012-08-15 15:38:57 -07001884 self.fake_gerrit.addEvent(e)
1885 self.waitUntilSettled()
1886
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001887 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001888 self.assertEqual(len(self.history), 1)
1889 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001890
1891 def test_build_configuration_branch(self):
1892 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001893
1894 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001895 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1896 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1897 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1898 A.addApproval('CRVW', 2)
1899 B.addApproval('CRVW', 2)
1900 C.addApproval('CRVW', 2)
1901 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1902 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1903 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1904 self.waitUntilSettled()
1905
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001906 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001907 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001908 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001909 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001910 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001911 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001912 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001913 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1914 self.gearman_server.hold_jobs_in_queue = False
1915 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001916 self.waitUntilSettled()
1917
Monty Taylorbc758832013-06-17 17:22:42 -04001918 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001919 repo = git.Repo(path)
1920 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1921 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001922 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001923 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001924
1925 def test_build_configuration_branch_interaction(self):
1926 "Test that switching between branches works"
1927 self.test_build_configuration()
1928 self.test_build_configuration_branch()
1929 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001930 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001931 repo = git.Repo(path)
1932 repo.heads.master.commit = repo.commit('init')
1933 self.test_build_configuration()
1934
1935 def test_build_configuration_multi_branch(self):
1936 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001937
1938 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001939 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1940 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1941 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1942 A.addApproval('CRVW', 2)
1943 B.addApproval('CRVW', 2)
1944 C.addApproval('CRVW', 2)
1945 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1946 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1947 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1948 self.waitUntilSettled()
James E. Blairbb1fe502014-03-04 10:15:06 -08001949 queue = self.gearman_server.getQueue()
1950 job_A = None
1951 for job in queue:
1952 if 'project-merge' in job.name:
1953 job_A = job
1954 ref_A = self.getParameter(job_A, 'ZUUL_REF')
1955 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
1956 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
1957 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
James E. Blairc6294a52012-08-17 10:19:48 -07001958
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001959 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001960 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001961 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001962 job_B = None
1963 for job in queue:
1964 if 'project-merge' in job.name:
1965 job_B = job
1966 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001967 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001968 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blairbb1fe502014-03-04 10:15:06 -08001969 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
1970
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001971 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001972 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001973 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001974 for job in queue:
1975 if 'project-merge' in job.name:
1976 job_C = job
1977 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001978 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001979 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blairbb1fe502014-03-04 10:15:06 -08001980 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001981 self.gearman_server.hold_jobs_in_queue = False
1982 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001983 self.waitUntilSettled()
1984
Monty Taylorbc758832013-06-17 17:22:42 -04001985 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001986 repo = git.Repo(path)
1987
1988 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001989 for c in repo.iter_commits(ref_C)]
James E. Blairbb1fe502014-03-04 10:15:06 -08001990 repo_shas = [c.hexsha for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001991 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001992 correct_messages = ['initial commit', 'A-1', 'C-1']
James E. Blairbb1fe502014-03-04 10:15:06 -08001993 # Ensure the right commits are in the history for this ref
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001994 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08001995 # Ensure ZUUL_REF -> ZUUL_COMMIT
1996 self.assertEqual(repo_shas[0], commit_C)
James E. Blairc6294a52012-08-17 10:19:48 -07001997
1998 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001999 for c in repo.iter_commits(ref_B)]
James E. Blairbb1fe502014-03-04 10:15:06 -08002000 repo_shas = [c.hexsha for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07002001 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07002002 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002003 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08002004 self.assertEqual(repo_shas[0], commit_B)
2005
2006 repo_messages = [c.message.strip()
2007 for c in repo.iter_commits(ref_A)]
2008 repo_shas = [c.hexsha for c in repo.iter_commits(ref_A)]
2009 repo_messages.reverse()
2010 correct_messages = ['initial commit', 'A-1']
2011 self.assertEqual(repo_messages, correct_messages)
2012 self.assertEqual(repo_shas[0], commit_A)
2013
2014 self.assertNotEqual(ref_A, ref_B, ref_C)
2015 self.assertNotEqual(commit_A, commit_B, commit_C)
James E. Blair7f71c802012-08-22 13:04:32 -07002016
2017 def test_one_job_project(self):
2018 "Test that queueing works with one job"
2019 A = self.fake_gerrit.addFakeChange('org/one-job-project',
2020 'master', 'A')
2021 B = self.fake_gerrit.addFakeChange('org/one-job-project',
2022 'master', 'B')
2023 A.addApproval('CRVW', 2)
2024 B.addApproval('CRVW', 2)
2025 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2026 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2027 self.waitUntilSettled()
2028
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002029 self.assertEqual(A.data['status'], 'MERGED')
2030 self.assertEqual(A.reported, 2)
2031 self.assertEqual(B.data['status'], 'MERGED')
2032 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07002033
Antoine Musso80edd5a2013-02-13 15:37:53 +01002034 def test_job_from_templates_launched(self):
2035 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002036
Antoine Musso80edd5a2013-02-13 15:37:53 +01002037 A = self.fake_gerrit.addFakeChange(
2038 'org/templated-project', 'master', 'A')
2039 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2040 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01002041
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002042 self.assertEqual(self.getJobFromHistory('project-test1').result,
2043 'SUCCESS')
2044 self.assertEqual(self.getJobFromHistory('project-test2').result,
2045 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01002046
James E. Blair3e98c022013-12-16 15:25:38 -08002047 def test_layered_templates(self):
2048 "Test whether a job generated via a template can be launched"
2049
2050 A = self.fake_gerrit.addFakeChange(
2051 'org/layered-project', 'master', 'A')
2052 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2053 self.waitUntilSettled()
2054
2055 self.assertEqual(self.getJobFromHistory('project-test1').result,
2056 'SUCCESS')
2057 self.assertEqual(self.getJobFromHistory('project-test2').result,
2058 'SUCCESS')
James E. Blairaea6cf62013-12-16 15:38:12 -08002059 self.assertEqual(self.getJobFromHistory('layered-project-test3'
2060 ).result, 'SUCCESS')
2061 self.assertEqual(self.getJobFromHistory('layered-project-test4'
2062 ).result, 'SUCCESS')
2063 # test5 should run twice because two templates define it
James E. Blair3e98c022013-12-16 15:25:38 -08002064 test5_count = 0
2065 for job in self.worker.build_history:
James E. Blairaea6cf62013-12-16 15:38:12 -08002066 if job.name == 'layered-project-foo-test5':
James E. Blair3e98c022013-12-16 15:25:38 -08002067 test5_count += 1
2068 self.assertEqual(job.result, 'SUCCESS')
2069 self.assertEqual(test5_count, 2)
2070 self.assertEqual(self.getJobFromHistory('project-test6').result,
2071 'SUCCESS')
2072
James E. Blaircaec0c52012-08-22 14:52:22 -07002073 def test_dependent_changes_dequeue(self):
2074 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002075
James E. Blaircaec0c52012-08-22 14:52:22 -07002076 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2077 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2078 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2079 A.addApproval('CRVW', 2)
2080 B.addApproval('CRVW', 2)
2081 C.addApproval('CRVW', 2)
2082
2083 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
2084 M1.setMerged()
2085
2086 # C -> B -> A -> M1
2087
2088 C.setDependsOn(B, 1)
2089 B.setDependsOn(A, 1)
2090 A.setDependsOn(M1, 1)
2091
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002092 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07002093
2094 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2095 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2096 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2097
2098 self.waitUntilSettled()
2099
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002100 self.assertEqual(A.data['status'], 'NEW')
2101 self.assertEqual(A.reported, 2)
2102 self.assertEqual(B.data['status'], 'NEW')
2103 self.assertEqual(B.reported, 2)
2104 self.assertEqual(C.data['status'], 'NEW')
2105 self.assertEqual(C.reported, 2)
2106 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07002107
James E. Blair972e3c72013-08-29 12:04:55 -07002108 def test_failing_dependent_changes(self):
2109 "Test that failing dependent patches are taken out of stream"
2110 self.worker.hold_jobs_in_build = True
2111 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2112 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2113 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2114 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2115 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
2116 A.addApproval('CRVW', 2)
2117 B.addApproval('CRVW', 2)
2118 C.addApproval('CRVW', 2)
2119 D.addApproval('CRVW', 2)
2120 E.addApproval('CRVW', 2)
2121
2122 # E, D -> C -> B, A
2123
2124 D.setDependsOn(C, 1)
2125 C.setDependsOn(B, 1)
2126
2127 self.worker.addFailTest('project-test1', B)
2128
2129 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2130 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2131 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2132 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2133 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2134
2135 self.waitUntilSettled()
2136 self.worker.release('.*-merge')
2137 self.waitUntilSettled()
2138 self.worker.release('.*-merge')
2139 self.waitUntilSettled()
2140 self.worker.release('.*-merge')
2141 self.waitUntilSettled()
2142 self.worker.release('.*-merge')
2143 self.waitUntilSettled()
2144 self.worker.release('.*-merge')
2145 self.waitUntilSettled()
2146
2147 self.worker.hold_jobs_in_build = False
2148 for build in self.builds:
2149 if build.parameters['ZUUL_CHANGE'] != '1':
2150 build.release()
2151 self.waitUntilSettled()
2152
2153 self.worker.release()
2154 self.waitUntilSettled()
2155
2156 self.assertEqual(A.data['status'], 'MERGED')
2157 self.assertEqual(A.reported, 2)
2158 self.assertEqual(B.data['status'], 'NEW')
2159 self.assertEqual(B.reported, 2)
2160 self.assertEqual(C.data['status'], 'NEW')
2161 self.assertEqual(C.reported, 2)
2162 self.assertEqual(D.data['status'], 'NEW')
2163 self.assertEqual(D.reported, 2)
2164 self.assertEqual(E.data['status'], 'MERGED')
2165 self.assertEqual(E.reported, 2)
2166 self.assertEqual(len(self.history), 18)
2167
James E. Blairec590122012-08-22 15:19:31 -07002168 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08002169 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07002170 # If it's dequeued more than once, we should see extra
2171 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07002172
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002173 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07002174 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2175 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2176 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2177 A.addApproval('CRVW', 2)
2178 B.addApproval('CRVW', 2)
2179 C.addApproval('CRVW', 2)
2180
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002181 self.worker.addFailTest('project1-test1', A)
2182 self.worker.addFailTest('project1-test2', A)
2183 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07002184
2185 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2186 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2187 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2188
2189 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07002190
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002191 self.assertEqual(len(self.builds), 1)
2192 self.assertEqual(self.builds[0].name, 'project1-merge')
2193 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07002194
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002195 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002196 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002197 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002198 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002199 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002200 self.waitUntilSettled()
2201
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002202 self.assertEqual(len(self.builds), 9)
2203 self.assertEqual(self.builds[0].name, 'project1-test1')
2204 self.assertEqual(self.builds[1].name, 'project1-test2')
2205 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
2206 self.assertEqual(self.builds[3].name, 'project1-test1')
2207 self.assertEqual(self.builds[4].name, 'project1-test2')
2208 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
2209 self.assertEqual(self.builds[6].name, 'project1-test1')
2210 self.assertEqual(self.builds[7].name, 'project1-test2')
2211 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07002212
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002213 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07002214 self.waitUntilSettled()
2215
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002216 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
2217 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07002218
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002219 self.worker.hold_jobs_in_build = False
2220 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07002221 self.waitUntilSettled()
2222
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002223 self.assertEqual(len(self.builds), 0)
2224 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07002225
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002226 self.assertEqual(A.data['status'], 'NEW')
2227 self.assertEqual(B.data['status'], 'MERGED')
2228 self.assertEqual(C.data['status'], 'MERGED')
2229 self.assertEqual(A.reported, 2)
2230 self.assertEqual(B.reported, 2)
2231 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07002232
2233 def test_nonvoting_job(self):
2234 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002235
James E. Blair4ec821f2012-08-23 15:28:28 -07002236 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
2237 'master', 'A')
2238 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002239 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07002240 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2241
2242 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07002243
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002244 self.assertEqual(A.data['status'], 'MERGED')
2245 self.assertEqual(A.reported, 2)
2246 self.assertEqual(
2247 self.getJobFromHistory('nonvoting-project-merge').result,
2248 'SUCCESS')
2249 self.assertEqual(
2250 self.getJobFromHistory('nonvoting-project-test1').result,
2251 'SUCCESS')
2252 self.assertEqual(
2253 self.getJobFromHistory('nonvoting-project-test2').result,
2254 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07002255
2256 def test_check_queue_success(self):
2257 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002258
James E. Blaire0487072012-08-29 17:38:31 -07002259 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2260 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2261
2262 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002263
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002264 self.assertEqual(A.data['status'], 'NEW')
2265 self.assertEqual(A.reported, 1)
2266 self.assertEqual(self.getJobFromHistory('project-merge').result,
2267 'SUCCESS')
2268 self.assertEqual(self.getJobFromHistory('project-test1').result,
2269 'SUCCESS')
2270 self.assertEqual(self.getJobFromHistory('project-test2').result,
2271 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07002272
2273 def test_check_queue_failure(self):
2274 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002275
James E. Blaire0487072012-08-29 17:38:31 -07002276 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002277 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07002278 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2279
2280 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002281
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002282 self.assertEqual(A.data['status'], 'NEW')
2283 self.assertEqual(A.reported, 1)
2284 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07002285 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002286 self.assertEqual(self.getJobFromHistory('project-test1').result,
2287 'SUCCESS')
2288 self.assertEqual(self.getJobFromHistory('project-test2').result,
2289 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07002290
2291 def test_dependent_behind_dequeue(self):
2292 "test that dependent changes behind dequeued changes work"
2293 # This complicated test is a reproduction of a real life bug
2294 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07002295
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002296 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07002297 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2298 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2299 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2300 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2301 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
2302 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
2303 D.setDependsOn(C, 1)
2304 E.setDependsOn(D, 1)
2305 A.addApproval('CRVW', 2)
2306 B.addApproval('CRVW', 2)
2307 C.addApproval('CRVW', 2)
2308 D.addApproval('CRVW', 2)
2309 E.addApproval('CRVW', 2)
2310 F.addApproval('CRVW', 2)
2311
2312 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07002313
2314 # Change object re-use in the gerrit trigger is hidden if
2315 # changes are added in quick succession; waiting makes it more
2316 # like real life.
2317 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2318 self.waitUntilSettled()
2319 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2320 self.waitUntilSettled()
2321
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002322 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002323 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002324 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002325 self.waitUntilSettled()
2326
2327 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2328 self.waitUntilSettled()
2329 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2330 self.waitUntilSettled()
2331 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2332 self.waitUntilSettled()
2333 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
2334 self.waitUntilSettled()
2335
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002336 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002337 self.waitUntilSettled()
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()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002342 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002343 self.waitUntilSettled()
2344
2345 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002346
2347 # Grab pointers to the jobs we want to release before
2348 # releasing any, because list indexes may change as
2349 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002350 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002351 a.release()
2352 b.release()
2353 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002354 self.waitUntilSettled()
2355
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002356 self.worker.hold_jobs_in_build = False
2357 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002358 self.waitUntilSettled()
2359
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002360 self.assertEqual(A.data['status'], 'NEW')
2361 self.assertEqual(B.data['status'], 'MERGED')
2362 self.assertEqual(C.data['status'], 'MERGED')
2363 self.assertEqual(D.data['status'], 'MERGED')
2364 self.assertEqual(E.data['status'], 'MERGED')
2365 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002366
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002367 self.assertEqual(A.reported, 2)
2368 self.assertEqual(B.reported, 2)
2369 self.assertEqual(C.reported, 2)
2370 self.assertEqual(D.reported, 2)
2371 self.assertEqual(E.reported, 2)
2372 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002373
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002374 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2375 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002376
2377 def test_merger_repack(self):
2378 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002379
James E. Blair05fed602012-09-07 12:45:24 -07002380 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2381 A.addApproval('CRVW', 2)
2382 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2383 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002384 self.assertEqual(self.getJobFromHistory('project-merge').result,
2385 'SUCCESS')
2386 self.assertEqual(self.getJobFromHistory('project-test1').result,
2387 'SUCCESS')
2388 self.assertEqual(self.getJobFromHistory('project-test2').result,
2389 'SUCCESS')
2390 self.assertEqual(A.data['status'], 'MERGED')
2391 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07002392 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002393 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002394
Monty Taylorbc758832013-06-17 17:22:42 -04002395 path = os.path.join(self.git_root, "org/project")
2396 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002397
2398 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2399 A.addApproval('CRVW', 2)
2400 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2401 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002402 self.assertEqual(self.getJobFromHistory('project-merge').result,
2403 'SUCCESS')
2404 self.assertEqual(self.getJobFromHistory('project-test1').result,
2405 'SUCCESS')
2406 self.assertEqual(self.getJobFromHistory('project-test2').result,
2407 'SUCCESS')
2408 self.assertEqual(A.data['status'], 'MERGED')
2409 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002410
James E. Blair4886f282012-11-15 09:27:33 -08002411 def test_merger_repack_large_change(self):
2412 "Test that the merger works with large changes after a repack"
2413 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08002414 # This test assumes the repo is already cloned; make sure it is
2415 url = self.sched.triggers['gerrit'].getGitUrl(
2416 self.sched.layout.projects['org/project1'])
James E. Blair4076e2b2014-01-28 12:42:20 -08002417 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08002418 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2419 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002420 path = os.path.join(self.upstream_root, "org/project1")
2421 print repack_repo(path)
2422 path = os.path.join(self.git_root, "org/project1")
2423 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002424
2425 A.addApproval('CRVW', 2)
2426 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2427 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002428 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2429 'SUCCESS')
2430 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2431 'SUCCESS')
2432 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2433 'SUCCESS')
2434 self.assertEqual(A.data['status'], 'MERGED')
2435 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002436
James E. Blair7ee88a22012-09-12 18:59:31 +02002437 def test_nonexistent_job(self):
2438 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002439 # Set to the state immediately after a restart
2440 self.resetGearmanServer()
2441 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002442
2443 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2444 A.addApproval('CRVW', 2)
2445 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2446 # There may be a thread about to report a lost change
2447 while A.reported < 2:
2448 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002449 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002450 self.assertFalse(job_names)
2451 self.assertEqual(A.data['status'], 'NEW')
2452 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002453 self.assertEmptyQueues()
2454
2455 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002456 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002457 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2458 A.addApproval('CRVW', 2)
2459 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2460 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002461 self.assertEqual(self.getJobFromHistory('project-merge').result,
2462 'SUCCESS')
2463 self.assertEqual(self.getJobFromHistory('project-test1').result,
2464 'SUCCESS')
2465 self.assertEqual(self.getJobFromHistory('project-test2').result,
2466 'SUCCESS')
2467 self.assertEqual(A.data['status'], 'MERGED')
2468 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002469
2470 def test_single_nonexistent_post_job(self):
2471 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002472 e = {
2473 "type": "ref-updated",
2474 "submitter": {
2475 "name": "User Name",
2476 },
2477 "refUpdate": {
2478 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2479 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2480 "refName": "master",
2481 "project": "org/project",
2482 }
2483 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002484 # Set to the state immediately after a restart
2485 self.resetGearmanServer()
2486 self.launcher.negative_function_cache_ttl = 0
2487
James E. Blairf62d4282012-12-31 17:01:50 -08002488 self.fake_gerrit.addEvent(e)
2489 self.waitUntilSettled()
2490
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002491 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002492
2493 def test_new_patchset_dequeues_old(self):
2494 "Test that a new patchset causes the old to be dequeued"
2495 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002496 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002497 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2498 M.setMerged()
2499
2500 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2501 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2502 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2503 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2504 A.addApproval('CRVW', 2)
2505 B.addApproval('CRVW', 2)
2506 C.addApproval('CRVW', 2)
2507 D.addApproval('CRVW', 2)
2508
2509 C.setDependsOn(B, 1)
2510 B.setDependsOn(A, 1)
2511 A.setDependsOn(M, 1)
2512
2513 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2514 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2515 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2516 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2517 self.waitUntilSettled()
2518
2519 B.addPatchset()
2520 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2521 self.waitUntilSettled()
2522
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002523 self.worker.hold_jobs_in_build = False
2524 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002525 self.waitUntilSettled()
2526
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002527 self.assertEqual(A.data['status'], 'MERGED')
2528 self.assertEqual(A.reported, 2)
2529 self.assertEqual(B.data['status'], 'NEW')
2530 self.assertEqual(B.reported, 2)
2531 self.assertEqual(C.data['status'], 'NEW')
2532 self.assertEqual(C.reported, 2)
2533 self.assertEqual(D.data['status'], 'MERGED')
2534 self.assertEqual(D.reported, 2)
2535 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002536
Arx Cruzb1b010d2013-10-28 19:49:59 -02002537 def test_zuul_url_return(self):
2538 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08002539 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Arx Cruzb1b010d2013-10-28 19:49:59 -02002540 self.worker.hold_jobs_in_build = True
2541
2542 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2543 A.addApproval('CRVW', 2)
2544 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2545 self.waitUntilSettled()
2546
2547 self.assertEqual(len(self.builds), 1)
2548 for build in self.builds:
2549 self.assertTrue('ZUUL_URL' in build.parameters)
2550
2551 self.worker.hold_jobs_in_build = False
2552 self.worker.release()
2553 self.waitUntilSettled()
2554
James E. Blair2fa50962013-01-30 21:50:41 -08002555 def test_new_patchset_dequeues_old_on_head(self):
2556 "Test that a new patchset causes the old to be dequeued (at head)"
2557 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002558 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002559 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2560 M.setMerged()
2561 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2562 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2563 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2564 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2565 A.addApproval('CRVW', 2)
2566 B.addApproval('CRVW', 2)
2567 C.addApproval('CRVW', 2)
2568 D.addApproval('CRVW', 2)
2569
2570 C.setDependsOn(B, 1)
2571 B.setDependsOn(A, 1)
2572 A.setDependsOn(M, 1)
2573
2574 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2575 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2576 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2577 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2578 self.waitUntilSettled()
2579
2580 A.addPatchset()
2581 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2582 self.waitUntilSettled()
2583
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002584 self.worker.hold_jobs_in_build = False
2585 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002586 self.waitUntilSettled()
2587
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002588 self.assertEqual(A.data['status'], 'NEW')
2589 self.assertEqual(A.reported, 2)
2590 self.assertEqual(B.data['status'], 'NEW')
2591 self.assertEqual(B.reported, 2)
2592 self.assertEqual(C.data['status'], 'NEW')
2593 self.assertEqual(C.reported, 2)
2594 self.assertEqual(D.data['status'], 'MERGED')
2595 self.assertEqual(D.reported, 2)
2596 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002597
2598 def test_new_patchset_dequeues_old_without_dependents(self):
2599 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002600 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002601 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2602 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2603 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2604 A.addApproval('CRVW', 2)
2605 B.addApproval('CRVW', 2)
2606 C.addApproval('CRVW', 2)
2607
2608 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2609 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2610 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2611 self.waitUntilSettled()
2612
2613 B.addPatchset()
2614 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2615 self.waitUntilSettled()
2616
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002617 self.worker.hold_jobs_in_build = False
2618 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002619 self.waitUntilSettled()
2620
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002621 self.assertEqual(A.data['status'], 'MERGED')
2622 self.assertEqual(A.reported, 2)
2623 self.assertEqual(B.data['status'], 'NEW')
2624 self.assertEqual(B.reported, 2)
2625 self.assertEqual(C.data['status'], 'MERGED')
2626 self.assertEqual(C.reported, 2)
2627 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002628
2629 def test_new_patchset_dequeues_old_independent_queue(self):
2630 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002631 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002632 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2633 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2634 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2635 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2636 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2637 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2638 self.waitUntilSettled()
2639
2640 B.addPatchset()
2641 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2642 self.waitUntilSettled()
2643
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002644 self.worker.hold_jobs_in_build = False
2645 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002646 self.waitUntilSettled()
2647
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002648 self.assertEqual(A.data['status'], 'NEW')
2649 self.assertEqual(A.reported, 1)
2650 self.assertEqual(B.data['status'], 'NEW')
2651 self.assertEqual(B.reported, 1)
2652 self.assertEqual(C.data['status'], 'NEW')
2653 self.assertEqual(C.reported, 1)
2654 self.assertEqual(len(self.history), 10)
2655 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002656
2657 def test_zuul_refs(self):
2658 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002659 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002660 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2661 M1.setMerged()
2662 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2663 M2.setMerged()
2664
2665 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2666 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2667 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2668 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2669 A.addApproval('CRVW', 2)
2670 B.addApproval('CRVW', 2)
2671 C.addApproval('CRVW', 2)
2672 D.addApproval('CRVW', 2)
2673 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2674 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2675 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2676 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2677
2678 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002679 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002680 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002681 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002682 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002683 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002684 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002685 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002686 self.waitUntilSettled()
2687
James E. Blair7d0dedc2013-02-21 17:26:09 -08002688 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002689 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002690 if x.parameters['ZUUL_CHANGE'] == '3':
2691 a_zref = x.parameters['ZUUL_REF']
2692 if x.parameters['ZUUL_CHANGE'] == '4':
2693 b_zref = x.parameters['ZUUL_REF']
2694 if x.parameters['ZUUL_CHANGE'] == '5':
2695 c_zref = x.parameters['ZUUL_REF']
2696 if x.parameters['ZUUL_CHANGE'] == '6':
2697 d_zref = x.parameters['ZUUL_REF']
2698
2699 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002700 self.assertIsNotNone(a_zref)
2701 self.assertIsNotNone(b_zref)
2702 self.assertIsNotNone(c_zref)
2703 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002704
2705 # And they should all be different
2706 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002707 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002708
2709 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002710 self.assertTrue(self.ref_has_change(a_zref, A))
2711 self.assertFalse(self.ref_has_change(a_zref, B))
2712 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002713
2714 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002715 self.assertTrue(self.ref_has_change(b_zref, A))
2716 self.assertTrue(self.ref_has_change(b_zref, B))
2717 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002718
2719 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002720 self.assertTrue(self.ref_has_change(c_zref, A))
2721 self.assertTrue(self.ref_has_change(c_zref, B))
2722 self.assertTrue(self.ref_has_change(c_zref, C))
2723 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002724
2725 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002726 self.assertTrue(self.ref_has_change(d_zref, A))
2727 self.assertTrue(self.ref_has_change(d_zref, B))
2728 self.assertTrue(self.ref_has_change(d_zref, C))
2729 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002730
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002731 self.worker.hold_jobs_in_build = False
2732 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002733 self.waitUntilSettled()
2734
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002735 self.assertEqual(A.data['status'], 'MERGED')
2736 self.assertEqual(A.reported, 2)
2737 self.assertEqual(B.data['status'], 'MERGED')
2738 self.assertEqual(B.reported, 2)
2739 self.assertEqual(C.data['status'], 'MERGED')
2740 self.assertEqual(C.reported, 2)
2741 self.assertEqual(D.data['status'], 'MERGED')
2742 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002743
James E. Blairc053d022014-01-22 14:57:33 -08002744 def test_required_approval_check_and_gate(self):
2745 "Test required-approval triggers both check and gate"
2746 self.config.set('zuul', 'layout_config',
2747 'tests/fixtures/layout-require-approval.yaml')
2748 self.sched.reconfigure(self.config)
2749 self.registerJobs()
2750
2751 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2752 A.addApproval('CRVW', 2)
2753 # Add a too-old +1
2754 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2755
2756 aprv = A.addApproval('APRV', 1)
2757 self.fake_gerrit.addEvent(aprv)
2758 self.waitUntilSettled()
2759 # Should have run a check job
2760 self.assertEqual(len(self.history), 1)
2761 self.assertEqual(self.history[0].name, 'project-check')
2762
2763 # Report the result of that check job (overrides previous vrfy)
2764 # Skynet alert: this should trigger a gate job now that
2765 # all reqs are met
2766 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2767 self.waitUntilSettled()
2768 self.assertEqual(len(self.history), 2)
2769 self.assertEqual(self.history[1].name, 'project-gate')
2770
2771 def test_required_approval_newer(self):
2772 "Test required-approval newer trigger parameter"
2773 self.config.set('zuul', 'layout_config',
2774 'tests/fixtures/layout-require-approval.yaml')
2775 self.sched.reconfigure(self.config)
2776 self.registerJobs()
2777
2778 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2779 A.addApproval('CRVW', 2)
2780 aprv = A.addApproval('APRV', 1)
2781 self.fake_gerrit.addEvent(aprv)
2782 self.waitUntilSettled()
2783 # No +1 from Jenkins so should not be enqueued
2784 self.assertEqual(len(self.history), 0)
2785
2786 # Add a too-old +1, should trigger check but not gate
2787 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2788 self.fake_gerrit.addEvent(aprv)
2789 self.waitUntilSettled()
2790 self.assertEqual(len(self.history), 1)
2791 self.assertEqual(self.history[0].name, 'project-check')
2792
2793 # Add a recent +1
2794 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2795 self.fake_gerrit.addEvent(aprv)
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_older(self):
2801 "Test required-approval older 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 crvw = A.addApproval('CRVW', 2)
2809 self.fake_gerrit.addEvent(crvw)
2810 self.waitUntilSettled()
2811 # No +1 from Jenkins so should not be enqueued
2812 self.assertEqual(len(self.history), 0)
2813
2814 # Add an old +1 and trigger check with a comment
2815 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2816 self.fake_gerrit.addEvent(crvw)
2817 self.waitUntilSettled()
2818 self.assertEqual(len(self.history), 1)
2819 self.assertEqual(self.history[0].name, 'project-check')
2820
2821 # Add a recent +1 and make sure nothing changes
2822 A.addApproval('VRFY', 1)
2823 self.fake_gerrit.addEvent(crvw)
2824 self.waitUntilSettled()
2825 self.assertEqual(len(self.history), 1)
2826
2827 # The last thing we did was query a change then do nothing
2828 # with a pipeline, so it will be in the cache; clean it up so
2829 # it does not fail the test.
2830 for pipeline in self.sched.layout.pipelines.values():
2831 pipeline.trigger.maintainCache([])
2832
James E. Blair4a28a882013-08-23 15:17:33 -07002833 def test_rerun_on_error(self):
2834 "Test that if a worker fails to run a job, it is run again"
2835 self.worker.hold_jobs_in_build = True
2836 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2837 A.addApproval('CRVW', 2)
2838 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2839 self.waitUntilSettled()
2840
2841 self.builds[0].run_error = True
2842 self.worker.hold_jobs_in_build = False
2843 self.worker.release()
2844 self.waitUntilSettled()
2845 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2846 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2847
James E. Blair412e5582013-04-22 15:50:12 -07002848 def test_statsd(self):
2849 "Test each of the statsd methods used in the scheduler"
2850 import extras
2851 statsd = extras.try_import('statsd.statsd')
2852 statsd.incr('test-incr')
2853 statsd.timing('test-timing', 3)
2854 statsd.gauge('test-guage', 12)
2855 self.assertReportedStat('test-incr', '1|c')
2856 self.assertReportedStat('test-timing', '3|ms')
2857 self.assertReportedStat('test-guage', '12|g')
2858
James E. Blairdad52252014-02-07 16:59:17 -08002859 def test_stuck_job_cleanup(self):
2860 "Test that pending jobs are cleaned up if removed from layout"
2861 self.gearman_server.hold_jobs_in_queue = True
2862 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2863 A.addApproval('CRVW', 2)
2864 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2865 self.waitUntilSettled()
2866 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2867
2868 self.config.set('zuul', 'layout_config',
2869 'tests/fixtures/layout-no-jobs.yaml')
2870 self.sched.reconfigure(self.config)
2871 self.waitUntilSettled()
2872
2873 self.gearman_server.release('noop')
2874 self.waitUntilSettled()
2875 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2876 self.assertTrue(self.sched._areAllBuildsComplete())
2877
2878 self.assertEqual(len(self.history), 1)
2879 self.assertEqual(self.history[0].name, 'noop')
2880 self.assertEqual(self.history[0].result, 'SUCCESS')
2881
James E. Blair70c71582013-03-06 08:50:50 -08002882 def test_file_jobs(self):
2883 "Test that file jobs run only when appropriate"
2884 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2885 A.addPatchset(['pip-requires'])
2886 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2887 A.addApproval('CRVW', 2)
2888 B.addApproval('CRVW', 2)
2889 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2890 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2891 self.waitUntilSettled()
2892
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002893 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002894 if x.name == 'project-testfile']
2895
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002896 self.assertEqual(len(testfile_jobs), 1)
2897 self.assertEqual(testfile_jobs[0].changes, '1,2')
2898 self.assertEqual(A.data['status'], 'MERGED')
2899 self.assertEqual(A.reported, 2)
2900 self.assertEqual(B.data['status'], 'MERGED')
2901 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002902
2903 def test_test_config(self):
2904 "Test that we can test the config"
2905 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002906 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002907 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002908 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002909
2910 def test_build_description(self):
2911 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002912 self.worker.registerFunction('set_description:' +
2913 self.worker.worker_id)
2914
2915 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2916 A.addApproval('CRVW', 2)
2917 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2918 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002919 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002920 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002921 self.assertTrue(re.search("Branch.*master", desc))
2922 self.assertTrue(re.search("Pipeline.*gate", desc))
2923 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2924 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2925 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2926 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002927
James E. Blair64ed6f22013-07-10 14:07:23 -07002928 def test_queue_precedence(self):
2929 "Test that queue precedence works"
2930
2931 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002932 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002933 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2934 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2935 A.addApproval('CRVW', 2)
2936 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2937
2938 self.waitUntilSettled()
2939 self.gearman_server.hold_jobs_in_queue = False
2940 self.gearman_server.release()
2941 self.waitUntilSettled()
2942
James E. Blair8de58bd2013-07-18 16:23:33 -07002943 # Run one build at a time to ensure non-race order:
2944 for x in range(6):
2945 self.release(self.builds[0])
2946 self.waitUntilSettled()
2947 self.worker.hold_jobs_in_build = False
2948 self.waitUntilSettled()
2949
James E. Blair64ed6f22013-07-10 14:07:23 -07002950 self.log.debug(self.history)
2951 self.assertEqual(self.history[0].pipeline, 'gate')
2952 self.assertEqual(self.history[1].pipeline, 'check')
2953 self.assertEqual(self.history[2].pipeline, 'gate')
2954 self.assertEqual(self.history[3].pipeline, 'gate')
2955 self.assertEqual(self.history[4].pipeline, 'check')
2956 self.assertEqual(self.history[5].pipeline, 'check')
2957
James E. Blair1843a552013-07-03 14:19:52 -07002958 def test_json_status(self):
2959 "Test that we can retrieve JSON status info"
2960 self.worker.hold_jobs_in_build = True
2961 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2962 A.addApproval('CRVW', 2)
2963 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2964 self.waitUntilSettled()
2965
2966 port = self.webapp.server.socket.getsockname()[1]
2967
2968 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2969 data = f.read()
2970
2971 self.worker.hold_jobs_in_build = False
2972 self.worker.release()
2973 self.waitUntilSettled()
2974
2975 data = json.loads(data)
2976 status_jobs = set()
2977 for p in data['pipelines']:
2978 for q in p['change_queues']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002979 if q['dependent']:
2980 self.assertEqual(q['window'], 20)
2981 else:
2982 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002983 for head in q['heads']:
2984 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002985 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002986 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002987 for job in change['jobs']:
2988 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002989 self.assertIn('project-merge', status_jobs)
2990 self.assertIn('project-test1', status_jobs)
2991 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07002992
James E. Blairc3d428e2013-12-03 15:06:48 -08002993 def test_merging_queues(self):
2994 "Test that transitively-connected change queues are merged"
2995 self.config.set('zuul', 'layout_config',
2996 'tests/fixtures/layout-merge-queues.yaml')
2997 self.sched.reconfigure(self.config)
2998 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
2999
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003000 def test_node_label(self):
3001 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003002 self.worker.registerFunction('build:node-project-test1:debian')
3003
3004 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
3005 A.addApproval('CRVW', 2)
3006 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3007 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07003008
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003009 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3010 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3011 'debian')
3012 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07003013
3014 def test_live_reconfiguration(self):
3015 "Test that live reconfiguration works"
3016 self.worker.hold_jobs_in_build = True
3017 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3018 A.addApproval('CRVW', 2)
3019 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3020 self.waitUntilSettled()
3021
3022 self.sched.reconfigure(self.config)
3023
3024 self.worker.hold_jobs_in_build = False
3025 self.worker.release()
3026 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003027 self.assertEqual(self.getJobFromHistory('project-merge').result,
3028 'SUCCESS')
3029 self.assertEqual(self.getJobFromHistory('project-test1').result,
3030 'SUCCESS')
3031 self.assertEqual(self.getJobFromHistory('project-test2').result,
3032 'SUCCESS')
3033 self.assertEqual(A.data['status'], 'MERGED')
3034 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07003035
James E. Blaire712d9f2013-07-31 11:40:11 -07003036 def test_live_reconfiguration_functions(self):
3037 "Test live reconfiguration with a custom function"
3038 self.worker.registerFunction('build:node-project-test1:debian')
3039 self.worker.registerFunction('build:node-project-test1:wheezy')
3040 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
3041 A.addApproval('CRVW', 2)
3042 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3043 self.waitUntilSettled()
3044
3045 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3046 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3047 'debian')
3048 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
3049
3050 self.config.set('zuul', 'layout_config',
3051 'tests/fixtures/layout-live-'
3052 'reconfiguration-functions.yaml')
3053 self.sched.reconfigure(self.config)
3054 self.worker.build_history = []
3055
3056 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
3057 B.addApproval('CRVW', 2)
3058 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3059 self.waitUntilSettled()
3060
3061 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3062 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3063 'wheezy')
3064 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
3065
James E. Blair287c06d2013-07-24 10:39:30 -07003066 def test_delayed_repo_init(self):
3067 self.config.set('zuul', 'layout_config',
3068 'tests/fixtures/layout-delayed-repo-init.yaml')
3069 self.sched.reconfigure(self.config)
3070
3071 self.init_repo("org/new-project")
3072 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
3073
3074 A.addApproval('CRVW', 2)
3075 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3076 self.waitUntilSettled()
3077 self.assertEqual(self.getJobFromHistory('project-merge').result,
3078 'SUCCESS')
3079 self.assertEqual(self.getJobFromHistory('project-test1').result,
3080 'SUCCESS')
3081 self.assertEqual(self.getJobFromHistory('project-test2').result,
3082 'SUCCESS')
3083 self.assertEqual(A.data['status'], 'MERGED')
3084 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003085
Clark Boylan6dbbc482013-10-18 10:57:31 -07003086 def test_repo_deleted(self):
3087 self.config.set('zuul', 'layout_config',
3088 'tests/fixtures/layout-repo-deleted.yaml')
3089 self.sched.reconfigure(self.config)
3090
3091 self.init_repo("org/delete-project")
3092 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
3093
3094 A.addApproval('CRVW', 2)
3095 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3096 self.waitUntilSettled()
3097 self.assertEqual(self.getJobFromHistory('project-merge').result,
3098 'SUCCESS')
3099 self.assertEqual(self.getJobFromHistory('project-test1').result,
3100 'SUCCESS')
3101 self.assertEqual(self.getJobFromHistory('project-test2').result,
3102 'SUCCESS')
3103 self.assertEqual(A.data['status'], 'MERGED')
3104 self.assertEqual(A.reported, 2)
3105
3106 # Delete org/new-project zuul repo. Should be recloned.
3107 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
3108
3109 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
3110
3111 B.addApproval('CRVW', 2)
3112 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3113 self.waitUntilSettled()
3114 self.assertEqual(self.getJobFromHistory('project-merge').result,
3115 'SUCCESS')
3116 self.assertEqual(self.getJobFromHistory('project-test1').result,
3117 'SUCCESS')
3118 self.assertEqual(self.getJobFromHistory('project-test2').result,
3119 'SUCCESS')
3120 self.assertEqual(B.data['status'], 'MERGED')
3121 self.assertEqual(B.reported, 2)
3122
James E. Blair63bb0ef2013-07-29 17:14:51 -07003123 def test_timer(self):
3124 "Test that a periodic job is triggered"
3125 self.worker.hold_jobs_in_build = True
3126 self.config.set('zuul', 'layout_config',
3127 'tests/fixtures/layout-timer.yaml')
3128 self.sched.reconfigure(self.config)
3129 self.registerJobs()
3130
3131 start = time.time()
3132 failed = True
3133 while ((time.time() - start) < 30):
3134 if len(self.builds) == 2:
3135 failed = False
3136 break
3137 else:
3138 time.sleep(1)
3139
3140 if failed:
3141 raise Exception("Expected jobs never ran")
3142
3143 self.waitUntilSettled()
3144 port = self.webapp.server.socket.getsockname()[1]
3145
3146 f = urllib.urlopen("http://localhost:%s/status.json" % port)
3147 data = f.read()
3148
3149 self.worker.hold_jobs_in_build = False
3150 self.worker.release()
3151 self.waitUntilSettled()
3152
3153 self.assertEqual(self.getJobFromHistory(
3154 'project-bitrot-stable-old').result, 'SUCCESS')
3155 self.assertEqual(self.getJobFromHistory(
3156 'project-bitrot-stable-older').result, 'SUCCESS')
3157
3158 data = json.loads(data)
3159 status_jobs = set()
3160 for p in data['pipelines']:
3161 for q in p['change_queues']:
3162 for head in q['heads']:
3163 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07003164 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003165 for job in change['jobs']:
3166 status_jobs.add(job['name'])
3167 self.assertIn('project-bitrot-stable-old', status_jobs)
3168 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003169
3170 def test_check_smtp_pool(self):
3171 self.config.set('zuul', 'layout_config',
3172 'tests/fixtures/layout-smtp.yaml')
3173 self.sched.reconfigure(self.config)
3174
3175 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3176 self.waitUntilSettled()
3177
3178 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3179 self.waitUntilSettled()
3180
James E. Blairff80a2f2013-12-27 13:24:06 -08003181 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003182
3183 # A.messages only holds what FakeGerrit places in it. Thus we
3184 # work on the knowledge of what the first message should be as
3185 # it is only configured to go to SMTP.
3186
3187 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003188 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003189 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003190 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003191 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003192 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003193
3194 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003195 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003196 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003197 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003198 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003199 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003200
James E. Blaire5910202013-12-27 09:50:31 -08003201 def test_timer_smtp(self):
3202 "Test that a periodic job is triggered"
3203 self.config.set('zuul', 'layout_config',
3204 'tests/fixtures/layout-timer-smtp.yaml')
3205 self.sched.reconfigure(self.config)
3206 self.registerJobs()
3207
3208 start = time.time()
3209 failed = True
3210 while ((time.time() - start) < 30):
3211 if len(self.history) == 2:
3212 failed = False
3213 break
3214 else:
3215 time.sleep(1)
3216
3217 if failed:
3218 raise Exception("Expected jobs never ran")
3219
3220 self.waitUntilSettled()
3221
3222 self.assertEqual(self.getJobFromHistory(
3223 'project-bitrot-stable-old').result, 'SUCCESS')
3224 self.assertEqual(self.getJobFromHistory(
3225 'project-bitrot-stable-older').result, 'SUCCESS')
3226
James E. Blairff80a2f2013-12-27 13:24:06 -08003227 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003228
3229 # A.messages only holds what FakeGerrit places in it. Thus we
3230 # work on the knowledge of what the first message should be as
3231 # it is only configured to go to SMTP.
3232
3233 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003234 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003235 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003236 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003237 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003238 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003239
James E. Blairad28e912013-11-27 10:43:22 -08003240 def test_client_enqueue(self):
3241 "Test that the RPC client can enqueue a change"
3242 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3243 A.addApproval('CRVW', 2)
3244 A.addApproval('APRV', 1)
3245
3246 client = zuul.rpcclient.RPCClient('127.0.0.1',
3247 self.gearman_server.port)
3248 r = client.enqueue(pipeline='gate',
3249 project='org/project',
3250 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003251 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003252 self.waitUntilSettled()
3253 self.assertEqual(self.getJobFromHistory('project-merge').result,
3254 'SUCCESS')
3255 self.assertEqual(self.getJobFromHistory('project-test1').result,
3256 'SUCCESS')
3257 self.assertEqual(self.getJobFromHistory('project-test2').result,
3258 'SUCCESS')
3259 self.assertEqual(A.data['status'], 'MERGED')
3260 self.assertEqual(A.reported, 2)
3261 self.assertEqual(r, True)
3262
3263 def test_client_enqueue_negative(self):
3264 "Test that the RPC client returns errors"
3265 client = zuul.rpcclient.RPCClient('127.0.0.1',
3266 self.gearman_server.port)
3267 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3268 "Invalid project"):
3269 r = client.enqueue(pipeline='gate',
3270 project='project-does-not-exist',
3271 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003272 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003273 client.shutdown()
3274 self.assertEqual(r, False)
3275
3276 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3277 "Invalid pipeline"):
3278 r = client.enqueue(pipeline='pipeline-does-not-exist',
3279 project='org/project',
3280 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003281 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003282 client.shutdown()
3283 self.assertEqual(r, False)
3284
3285 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3286 "Invalid trigger"):
3287 r = client.enqueue(pipeline='gate',
3288 project='org/project',
3289 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003290 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003291 client.shutdown()
3292 self.assertEqual(r, False)
3293
3294 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3295 "Invalid change"):
3296 r = client.enqueue(pipeline='gate',
3297 project='org/project',
3298 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003299 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003300 client.shutdown()
3301 self.assertEqual(r, False)
3302
3303 self.waitUntilSettled()
3304 self.assertEqual(len(self.history), 0)
3305 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003306
3307 def test_client_promote(self):
3308 "Test that the RPC client can promote a change"
3309 self.worker.hold_jobs_in_build = True
3310 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3311 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3312 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3313 A.addApproval('CRVW', 2)
3314 B.addApproval('CRVW', 2)
3315 C.addApproval('CRVW', 2)
3316
3317 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3318 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3319 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3320
3321 self.waitUntilSettled()
3322
Sean Daguef39b9ca2014-01-10 21:34:35 -05003323 items = self.sched.layout.pipelines['gate'].getAllItems()
3324 enqueue_times = {}
3325 for item in items:
3326 enqueue_times[str(item.change)] = item.enqueue_time
3327
James E. Blair36658cf2013-12-06 17:53:48 -08003328 client = zuul.rpcclient.RPCClient('127.0.0.1',
3329 self.gearman_server.port)
3330 r = client.promote(pipeline='gate',
3331 change_ids=['2,1', '3,1'])
3332
Sean Daguef39b9ca2014-01-10 21:34:35 -05003333 # ensure that enqueue times are durable
3334 items = self.sched.layout.pipelines['gate'].getAllItems()
3335 for item in items:
3336 self.assertEqual(
3337 enqueue_times[str(item.change)], item.enqueue_time)
3338
James E. Blair78acec92014-02-06 07:11:32 -08003339 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003340 self.worker.release('.*-merge')
3341 self.waitUntilSettled()
3342 self.worker.release('.*-merge')
3343 self.waitUntilSettled()
3344 self.worker.release('.*-merge')
3345 self.waitUntilSettled()
3346
3347 self.assertEqual(len(self.builds), 6)
3348 self.assertEqual(self.builds[0].name, 'project-test1')
3349 self.assertEqual(self.builds[1].name, 'project-test2')
3350 self.assertEqual(self.builds[2].name, 'project-test1')
3351 self.assertEqual(self.builds[3].name, 'project-test2')
3352 self.assertEqual(self.builds[4].name, 'project-test1')
3353 self.assertEqual(self.builds[5].name, 'project-test2')
3354
3355 self.assertTrue(self.job_has_changes(self.builds[0], B))
3356 self.assertFalse(self.job_has_changes(self.builds[0], A))
3357 self.assertFalse(self.job_has_changes(self.builds[0], C))
3358
3359 self.assertTrue(self.job_has_changes(self.builds[2], B))
3360 self.assertTrue(self.job_has_changes(self.builds[2], C))
3361 self.assertFalse(self.job_has_changes(self.builds[2], A))
3362
3363 self.assertTrue(self.job_has_changes(self.builds[4], B))
3364 self.assertTrue(self.job_has_changes(self.builds[4], C))
3365 self.assertTrue(self.job_has_changes(self.builds[4], A))
3366
3367 self.worker.release()
3368 self.waitUntilSettled()
3369
3370 self.assertEqual(A.data['status'], 'MERGED')
3371 self.assertEqual(A.reported, 2)
3372 self.assertEqual(B.data['status'], 'MERGED')
3373 self.assertEqual(B.reported, 2)
3374 self.assertEqual(C.data['status'], 'MERGED')
3375 self.assertEqual(C.reported, 2)
3376
3377 client.shutdown()
3378 self.assertEqual(r, True)
3379
3380 def test_client_promote_dependent(self):
3381 "Test that the RPC client can promote a dependent change"
3382 # C (depends on B) -> B -> A ; then promote C to get:
3383 # A -> C (depends on B) -> B
3384 self.worker.hold_jobs_in_build = True
3385 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3386 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3387 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3388
3389 C.setDependsOn(B, 1)
3390
3391 A.addApproval('CRVW', 2)
3392 B.addApproval('CRVW', 2)
3393 C.addApproval('CRVW', 2)
3394
3395 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3396 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3397 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3398
3399 self.waitUntilSettled()
3400
3401 client = zuul.rpcclient.RPCClient('127.0.0.1',
3402 self.gearman_server.port)
3403 r = client.promote(pipeline='gate',
3404 change_ids=['3,1'])
3405
James E. Blair78acec92014-02-06 07:11:32 -08003406 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003407 self.worker.release('.*-merge')
3408 self.waitUntilSettled()
3409 self.worker.release('.*-merge')
3410 self.waitUntilSettled()
3411 self.worker.release('.*-merge')
3412 self.waitUntilSettled()
3413
3414 self.assertEqual(len(self.builds), 6)
3415 self.assertEqual(self.builds[0].name, 'project-test1')
3416 self.assertEqual(self.builds[1].name, 'project-test2')
3417 self.assertEqual(self.builds[2].name, 'project-test1')
3418 self.assertEqual(self.builds[3].name, 'project-test2')
3419 self.assertEqual(self.builds[4].name, 'project-test1')
3420 self.assertEqual(self.builds[5].name, 'project-test2')
3421
3422 self.assertTrue(self.job_has_changes(self.builds[0], B))
3423 self.assertFalse(self.job_has_changes(self.builds[0], A))
3424 self.assertFalse(self.job_has_changes(self.builds[0], C))
3425
3426 self.assertTrue(self.job_has_changes(self.builds[2], B))
3427 self.assertTrue(self.job_has_changes(self.builds[2], C))
3428 self.assertFalse(self.job_has_changes(self.builds[2], A))
3429
3430 self.assertTrue(self.job_has_changes(self.builds[4], B))
3431 self.assertTrue(self.job_has_changes(self.builds[4], C))
3432 self.assertTrue(self.job_has_changes(self.builds[4], A))
3433
3434 self.worker.release()
3435 self.waitUntilSettled()
3436
3437 self.assertEqual(A.data['status'], 'MERGED')
3438 self.assertEqual(A.reported, 2)
3439 self.assertEqual(B.data['status'], 'MERGED')
3440 self.assertEqual(B.reported, 2)
3441 self.assertEqual(C.data['status'], 'MERGED')
3442 self.assertEqual(C.reported, 2)
3443
3444 client.shutdown()
3445 self.assertEqual(r, True)
3446
3447 def test_client_promote_negative(self):
3448 "Test that the RPC client returns errors for promotion"
3449 self.worker.hold_jobs_in_build = True
3450 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3451 A.addApproval('CRVW', 2)
3452 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3453 self.waitUntilSettled()
3454
3455 client = zuul.rpcclient.RPCClient('127.0.0.1',
3456 self.gearman_server.port)
3457
3458 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3459 r = client.promote(pipeline='nonexistent',
3460 change_ids=['2,1', '3,1'])
3461 client.shutdown()
3462 self.assertEqual(r, False)
3463
3464 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3465 r = client.promote(pipeline='gate',
3466 change_ids=['4,1'])
3467 client.shutdown()
3468 self.assertEqual(r, False)
3469
3470 self.worker.hold_jobs_in_build = False
3471 self.worker.release()
3472 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003473
3474 def test_queue_rate_limiting(self):
3475 "Test that DependentPipelines are rate limited with dep across window"
3476 self.config.set('zuul', 'layout_config',
3477 'tests/fixtures/layout-rate-limit.yaml')
3478 self.sched.reconfigure(self.config)
3479 self.worker.hold_jobs_in_build = True
3480 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3481 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3482 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3483
3484 C.setDependsOn(B, 1)
3485 self.worker.addFailTest('project-test1', A)
3486
3487 A.addApproval('CRVW', 2)
3488 B.addApproval('CRVW', 2)
3489 C.addApproval('CRVW', 2)
3490
3491 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3492 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3493 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3494 self.waitUntilSettled()
3495
3496 # Only A and B will have their merge jobs queued because
3497 # window is 2.
3498 self.assertEqual(len(self.builds), 2)
3499 self.assertEqual(self.builds[0].name, 'project-merge')
3500 self.assertEqual(self.builds[1].name, 'project-merge')
3501
3502 self.worker.release('.*-merge')
3503 self.waitUntilSettled()
3504 self.worker.release('.*-merge')
3505 self.waitUntilSettled()
3506
3507 # Only A and B will have their test jobs queued because
3508 # window is 2.
3509 self.assertEqual(len(self.builds), 4)
3510 self.assertEqual(self.builds[0].name, 'project-test1')
3511 self.assertEqual(self.builds[1].name, 'project-test2')
3512 self.assertEqual(self.builds[2].name, 'project-test1')
3513 self.assertEqual(self.builds[3].name, 'project-test2')
3514
3515 self.worker.release('project-.*')
3516 self.waitUntilSettled()
3517
3518 queue = self.sched.layout.pipelines['gate'].queues[0]
3519 # A failed so window is reduced by 1 to 1.
3520 self.assertEqual(queue.window, 1)
3521 self.assertEqual(queue.window_floor, 1)
3522 self.assertEqual(A.data['status'], 'NEW')
3523
3524 # Gate is reset and only B's merge job is queued because
3525 # window shrunk to 1.
3526 self.assertEqual(len(self.builds), 1)
3527 self.assertEqual(self.builds[0].name, 'project-merge')
3528
3529 self.worker.release('.*-merge')
3530 self.waitUntilSettled()
3531
3532 # Only B's test jobs are queued because window is still 1.
3533 self.assertEqual(len(self.builds), 2)
3534 self.assertEqual(self.builds[0].name, 'project-test1')
3535 self.assertEqual(self.builds[1].name, 'project-test2')
3536
3537 self.worker.release('project-.*')
3538 self.waitUntilSettled()
3539
3540 # B was successfully merged so window is increased to 2.
3541 self.assertEqual(queue.window, 2)
3542 self.assertEqual(queue.window_floor, 1)
3543 self.assertEqual(B.data['status'], 'MERGED')
3544
3545 # Only C is left and its merge job is queued.
3546 self.assertEqual(len(self.builds), 1)
3547 self.assertEqual(self.builds[0].name, 'project-merge')
3548
3549 self.worker.release('.*-merge')
3550 self.waitUntilSettled()
3551
3552 # After successful merge job the test jobs for C are queued.
3553 self.assertEqual(len(self.builds), 2)
3554 self.assertEqual(self.builds[0].name, 'project-test1')
3555 self.assertEqual(self.builds[1].name, 'project-test2')
3556
3557 self.worker.release('project-.*')
3558 self.waitUntilSettled()
3559
3560 # C successfully merged so window is bumped to 3.
3561 self.assertEqual(queue.window, 3)
3562 self.assertEqual(queue.window_floor, 1)
3563 self.assertEqual(C.data['status'], 'MERGED')
3564
3565 def test_queue_rate_limiting_dependent(self):
3566 "Test that DependentPipelines are rate limited with dep in window"
3567 self.config.set('zuul', 'layout_config',
3568 'tests/fixtures/layout-rate-limit.yaml')
3569 self.sched.reconfigure(self.config)
3570 self.worker.hold_jobs_in_build = True
3571 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3572 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3573 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3574
3575 B.setDependsOn(A, 1)
3576
3577 self.worker.addFailTest('project-test1', A)
3578
3579 A.addApproval('CRVW', 2)
3580 B.addApproval('CRVW', 2)
3581 C.addApproval('CRVW', 2)
3582
3583 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3584 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3585 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3586 self.waitUntilSettled()
3587
3588 # Only A and B will have their merge jobs queued because
3589 # window is 2.
3590 self.assertEqual(len(self.builds), 2)
3591 self.assertEqual(self.builds[0].name, 'project-merge')
3592 self.assertEqual(self.builds[1].name, 'project-merge')
3593
3594 self.worker.release('.*-merge')
3595 self.waitUntilSettled()
3596 self.worker.release('.*-merge')
3597 self.waitUntilSettled()
3598
3599 # Only A and B will have their test jobs queued because
3600 # window is 2.
3601 self.assertEqual(len(self.builds), 4)
3602 self.assertEqual(self.builds[0].name, 'project-test1')
3603 self.assertEqual(self.builds[1].name, 'project-test2')
3604 self.assertEqual(self.builds[2].name, 'project-test1')
3605 self.assertEqual(self.builds[3].name, 'project-test2')
3606
3607 self.worker.release('project-.*')
3608 self.waitUntilSettled()
3609
3610 queue = self.sched.layout.pipelines['gate'].queues[0]
3611 # A failed so window is reduced by 1 to 1.
3612 self.assertEqual(queue.window, 1)
3613 self.assertEqual(queue.window_floor, 1)
3614 self.assertEqual(A.data['status'], 'NEW')
3615 self.assertEqual(B.data['status'], 'NEW')
3616
3617 # Gate is reset and only C's merge job is queued because
3618 # window shrunk to 1 and A and B were dequeued.
3619 self.assertEqual(len(self.builds), 1)
3620 self.assertEqual(self.builds[0].name, 'project-merge')
3621
3622 self.worker.release('.*-merge')
3623 self.waitUntilSettled()
3624
3625 # Only C's test jobs are queued because window is still 1.
3626 self.assertEqual(len(self.builds), 2)
3627 self.assertEqual(self.builds[0].name, 'project-test1')
3628 self.assertEqual(self.builds[1].name, 'project-test2')
3629
3630 self.worker.release('project-.*')
3631 self.waitUntilSettled()
3632
3633 # C was successfully merged so window is increased to 2.
3634 self.assertEqual(queue.window, 2)
3635 self.assertEqual(queue.window_floor, 1)
3636 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003637
3638 def test_worker_update_metadata(self):
3639 "Test if a worker can send back metadata about itself"
3640 self.worker.hold_jobs_in_build = True
3641
3642 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3643 A.addApproval('CRVW', 2)
3644 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3645 self.waitUntilSettled()
3646
3647 self.assertEqual(len(self.launcher.builds), 1)
3648
3649 self.log.debug('Current builds:')
3650 self.log.debug(self.launcher.builds)
3651
3652 start = time.time()
3653 while True:
3654 if time.time() - start > 10:
3655 raise Exception("Timeout waiting for gearman server to report "
3656 + "back to the client")
3657 build = self.launcher.builds.values()[0]
3658 if build.worker.name == "My Worker":
3659 break
3660 else:
3661 time.sleep(0)
3662
3663 self.log.debug(build)
3664 self.assertEqual("My Worker", build.worker.name)
3665 self.assertEqual("localhost", build.worker.hostname)
3666 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3667 self.assertEqual("zuul.example.org", build.worker.fqdn)
3668 self.assertEqual("FakeBuilder", build.worker.program)
3669 self.assertEqual("v1.1", build.worker.version)
3670 self.assertEqual({'something': 'else'}, build.worker.extra)
3671
3672 self.worker.hold_jobs_in_build = False
3673 self.worker.release()
3674 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003675
3676 def test_footer_message(self):
3677 "Test a pipeline's footer message is correctly added to the report."
3678 self.config.set('zuul', 'layout_config',
3679 'tests/fixtures/layout-footer-message.yaml')
3680 self.sched.reconfigure(self.config)
3681 self.registerJobs()
3682
3683 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3684 A.addApproval('CRVW', 2)
3685 self.worker.addFailTest('test1', A)
3686 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3687 self.waitUntilSettled()
3688
3689 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3690 B.addApproval('CRVW', 2)
3691 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3692 self.waitUntilSettled()
3693
3694 self.assertEqual(2, len(self.smtp_messages))
3695
3696 failure_body = """\
3697Build failed. For information on how to proceed, see \
3698http://wiki.example.org/Test_Failures
3699
3700- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3701- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3702
3703For CI problems and help debugging, contact ci@example.org"""
3704
3705 success_body = """\
3706Build succeeded.
3707
3708- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3709- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3710
3711For CI problems and help debugging, contact ci@example.org"""
3712
3713 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3714 self.assertEqual(success_body, self.smtp_messages[1]['body'])