blob: 2d8d6bb28f70e3a163cc94e2adb8b20cf54e7cc4 [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. Blair18c64442014-03-18 10:14:45 -0700798 self.init_repo("org/noop-project")
James E. Blair412e5582013-04-22 15:50:12 -0700799
800 self.statsd = FakeStatsd()
801 os.environ['STATSD_HOST'] = 'localhost'
802 os.environ['STATSD_PORT'] = str(self.statsd.port)
803 self.statsd.start()
804 # the statsd client object is configured in the statsd module import
805 reload(statsd)
806 reload(zuul.scheduler)
807
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700808 self.gearman_server = FakeGearmanServer()
809
810 self.config = ConfigParser.ConfigParser()
811 cfg = StringIO()
812 CONFIG.write(cfg)
813 cfg.seek(0)
814 self.config.readfp(cfg)
815 self.config.set('gearman', 'port', str(self.gearman_server.port))
816
Monty Taylorbc758832013-06-17 17:22:42 -0400817 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700818 self.worker.addServer('127.0.0.1', self.gearman_server.port)
819 self.gearman_server.worker = self.worker
820
James E. Blair4076e2b2014-01-28 12:42:20 -0800821 self.merge_server = zuul.merger.server.MergeServer(self.config)
822 self.merge_server.start()
823
James E. Blairb0fcae42012-07-17 11:12:10 -0700824 self.sched = zuul.scheduler.Scheduler()
825
James E. Blair8cc15a82012-08-01 11:17:57 -0700826 def URLOpenerFactory(*args, **kw):
827 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400828 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700829
James E. Blair8cc15a82012-08-01 11:17:57 -0700830 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700831 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blair4076e2b2014-01-28 12:42:20 -0800832 self.merge_client = zuul.merger.client.MergeClient(
833 self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700834
James E. Blairff80a2f2013-12-27 13:24:06 -0800835 self.smtp_messages = []
836
837 def FakeSMTPFactory(*args, **kw):
838 args = [self.smtp_messages] + list(args)
839 return FakeSMTP(*args, **kw)
840
James E. Blairb0fcae42012-07-17 11:12:10 -0700841 zuul.lib.gerrit.Gerrit = FakeGerrit
James E. Blairff80a2f2013-12-27 13:24:06 -0800842 self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTPFactory))
James E. Blairb0fcae42012-07-17 11:12:10 -0700843
Monty Taylorbc758832013-06-17 17:22:42 -0400844 self.gerrit = FakeGerritTrigger(
845 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700846 self.gerrit.replication_timeout = 1.5
847 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700848 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400849 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700850
James E. Blair1843a552013-07-03 14:19:52 -0700851 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
James E. Blairad28e912013-11-27 10:43:22 -0800852 self.rpc = zuul.rpclistener.RPCListener(self.config, self.sched)
James E. Blair1843a552013-07-03 14:19:52 -0700853
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700854 self.sched.setLauncher(self.launcher)
James E. Blair4076e2b2014-01-28 12:42:20 -0800855 self.sched.setMerger(self.merge_client)
James E. Blair6c358e72013-07-29 17:06:47 -0700856 self.sched.registerTrigger(self.gerrit)
James E. Blair63bb0ef2013-07-29 17:14:51 -0700857 self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
858 self.sched.registerTrigger(self.timer)
James E. Blairb0fcae42012-07-17 11:12:10 -0700859
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000860 self.sched.registerReporter(
861 zuul.reporter.gerrit.Reporter(self.gerrit))
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000862 self.smtp_reporter = zuul.reporter.smtp.Reporter(
863 self.config.get('smtp', 'default_from'),
864 self.config.get('smtp', 'default_to'),
865 self.config.get('smtp', 'server'))
866 self.sched.registerReporter(self.smtp_reporter)
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000867
James E. Blairb0fcae42012-07-17 11:12:10 -0700868 self.sched.start()
869 self.sched.reconfigure(self.config)
870 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700871 self.webapp.start()
James E. Blairad28e912013-11-27 10:43:22 -0800872 self.rpc.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700873 self.launcher.gearman.waitForServer()
874 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400875 self.builds = self.worker.running_builds
876 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700877
James E. Blairfee8d652013-06-07 08:57:52 -0700878 self.addCleanup(self.assertFinalState)
879 self.addCleanup(self.shutdown)
880
881 def assertFinalState(self):
882 # Make sure that the change cache is cleared
James E. Blair6c358e72013-07-29 17:06:47 -0700883 self.assertEqual(len(self.gerrit._change_cache.keys()), 0)
Clark Boylan4ba48d92013-11-11 18:03:53 -0800884 # Make sure that git.Repo objects have been garbage collected.
885 repos = []
886 gc.collect()
887 for obj in gc.get_objects():
888 if isinstance(obj, git.Repo):
889 repos.append(obj)
890 self.assertEqual(len(repos), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700891 self.assertEmptyQueues()
892
893 def shutdown(self):
894 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700895 self.launcher.stop()
James E. Blair4076e2b2014-01-28 12:42:20 -0800896 self.merge_server.stop()
897 self.merge_server.join()
898 self.merge_client.stop()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700899 self.worker.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700900 self.gerrit.stop()
James E. Blair63bb0ef2013-07-29 17:14:51 -0700901 self.timer.stop()
James E. Blairb0fcae42012-07-17 11:12:10 -0700902 self.sched.stop()
903 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700904 self.statsd.stop()
905 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700906 self.webapp.stop()
907 self.webapp.join()
James E. Blairad28e912013-11-27 10:43:22 -0800908 self.rpc.stop()
909 self.rpc.join()
James E. Blair491b3172014-03-10 15:05:36 -0700910 self.gearman_server.shutdown()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700911 threads = threading.enumerate()
912 if len(threads) > 1:
913 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400914 super(TestScheduler, self).tearDown()
915
916 def init_repo(self, project):
917 parts = project.split('/')
918 path = os.path.join(self.upstream_root, *parts[:-1])
919 if not os.path.exists(path):
920 os.makedirs(path)
921 path = os.path.join(self.upstream_root, project)
922 repo = git.Repo.init(path)
923
924 repo.config_writer().set_value('user', 'email', 'user@example.com')
925 repo.config_writer().set_value('user', 'name', 'User Name')
926 repo.config_writer().write()
927
928 fn = os.path.join(path, 'README')
929 f = open(fn, 'w')
930 f.write("test\n")
931 f.close()
932 repo.index.add([fn])
933 repo.index.commit('initial commit')
934 master = repo.create_head('master')
935 repo.create_tag('init')
936
937 mp = repo.create_head('mp')
938 repo.head.reference = mp
939 f = open(fn, 'a')
940 f.write("test mp\n")
941 f.close()
942 repo.index.add([fn])
943 repo.index.commit('mp commit')
944
945 repo.head.reference = master
946 repo.head.reset(index=True, working_tree=True)
947 repo.git.clean('-x', '-f', '-d')
948
949 def ref_has_change(self, ref, change):
950 path = os.path.join(self.git_root, change.project)
951 repo = git.Repo(path)
952 for commit in repo.iter_commits(ref):
953 if commit.message.strip() == ('%s-1' % change.subject):
954 return True
955 return False
956
957 def job_has_changes(self, *args):
958 job = args[0]
959 commits = args[1:]
960 if isinstance(job, FakeBuild):
961 parameters = job.parameters
962 else:
963 parameters = json.loads(job.arguments)
964 project = parameters['ZUUL_PROJECT']
965 path = os.path.join(self.git_root, project)
966 repo = git.Repo(path)
967 ref = parameters['ZUUL_REF']
968 sha = parameters['ZUUL_COMMIT']
969 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
970 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
971 commit_messages = ['%s-1' % commit.subject for commit in commits]
James E. Blairac2c3242014-01-24 13:38:51 -0800972 self.log.debug("Checking if job %s has changes; commit_messages %s;"
973 " repo_messages %s; sha %s" % (job, commit_messages,
974 repo_messages, sha))
Monty Taylorbc758832013-06-17 17:22:42 -0400975 for msg in commit_messages:
976 if msg not in repo_messages:
James E. Blairac2c3242014-01-24 13:38:51 -0800977 self.log.debug(" messages do not match")
Monty Taylorbc758832013-06-17 17:22:42 -0400978 return False
979 if repo_shas[0] != sha:
James E. Blairac2c3242014-01-24 13:38:51 -0800980 self.log.debug(" sha does not match")
Monty Taylorbc758832013-06-17 17:22:42 -0400981 return False
James E. Blairac2c3242014-01-24 13:38:51 -0800982 self.log.debug(" OK")
Monty Taylorbc758832013-06-17 17:22:42 -0400983 return True
James E. Blairb0fcae42012-07-17 11:12:10 -0700984
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700985 def registerJobs(self):
986 count = 0
James E. Blaireff88162013-07-01 12:44:14 -0400987 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700988 self.worker.registerFunction('build:' + job)
989 count += 1
990 self.worker.registerFunction('stop:' + self.worker.worker_id)
991 count += 1
992
993 while len(self.gearman_server.functions) < count:
994 time.sleep(0)
995
996 def release(self, job):
997 if isinstance(job, FakeBuild):
998 job.release()
999 else:
1000 job.waiting = False
1001 self.log.debug("Queued job %s released" % job.unique)
1002 self.gearman_server.wakeConnections()
1003
1004 def getParameter(self, job, name):
1005 if isinstance(job, FakeBuild):
1006 return job.parameters[name]
1007 else:
1008 parameters = json.loads(job.arguments)
1009 return parameters[name]
1010
1011 def resetGearmanServer(self):
1012 self.worker.setFunctions([])
1013 while True:
1014 done = True
1015 for connection in self.gearman_server.active_connections:
James E. Blairad28e912013-11-27 10:43:22 -08001016 if (connection.functions and
James E. Blair4076e2b2014-01-28 12:42:20 -08001017 connection.client_id not in ['Zuul RPC Listener',
1018 'Zuul Merger']):
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001019 done = False
1020 if done:
1021 break
1022 time.sleep(0)
1023 self.gearman_server.functions = set()
James E. Blairad28e912013-11-27 10:43:22 -08001024 self.rpc.register()
James E. Blair4076e2b2014-01-28 12:42:20 -08001025 self.merge_server.register()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001026
1027 def haveAllBuildsReported(self):
1028 # See if Zuul is waiting on a meta job to complete
1029 if self.launcher.meta_jobs:
1030 return False
1031 # Find out if every build that the worker has completed has been
1032 # reported back to Zuul. If it hasn't then that means a Gearman
1033 # event is still in transit and the system is not stable.
1034 for build in self.worker.build_history:
1035 zbuild = self.launcher.builds.get(build.uuid)
1036 if not zbuild:
1037 # It has already been reported
1038 continue
1039 # It hasn't been reported yet.
1040 return False
1041 # Make sure that none of the worker connections are in GRAB_WAIT
1042 for connection in self.worker.active_connections:
1043 if connection.state == 'GRAB_WAIT':
1044 return False
1045 return True
1046
1047 def areAllBuildsWaiting(self):
1048 ret = True
1049
1050 builds = self.launcher.builds.values()
1051 for build in builds:
1052 client_job = None
1053 for conn in self.launcher.gearman.active_connections:
1054 for j in conn.related_jobs.values():
1055 if j.unique == build.uuid:
1056 client_job = j
1057 break
1058 if not client_job:
1059 self.log.debug("%s is not known to the gearman client" %
1060 build)
1061 ret = False
1062 continue
1063 if not client_job.handle:
1064 self.log.debug("%s has no handle" % client_job)
1065 ret = False
1066 continue
1067 server_job = self.gearman_server.jobs.get(client_job.handle)
1068 if not server_job:
1069 self.log.debug("%s is not known to the gearman server" %
1070 client_job)
1071 ret = False
1072 continue
1073 if not hasattr(server_job, 'waiting'):
1074 self.log.debug("%s is being enqueued" % server_job)
1075 ret = False
1076 continue
1077 if server_job.waiting:
1078 continue
1079 worker_job = self.worker.gearman_jobs.get(server_job.unique)
1080 if worker_job:
1081 if worker_job.build.isWaiting():
1082 continue
1083 else:
1084 self.log.debug("%s is running" % worker_job)
1085 ret = False
1086 else:
1087 self.log.debug("%s is unassigned" % server_job)
1088 ret = False
1089 return ret
1090
James E. Blairb0fcae42012-07-17 11:12:10 -07001091 def waitUntilSettled(self):
1092 self.log.debug("Waiting until settled...")
1093 start = time.time()
1094 while True:
1095 if time.time() - start > 10:
1096 print 'queue status:',
1097 print self.sched.trigger_event_queue.empty(),
1098 print self.sched.result_event_queue.empty(),
1099 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001100 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -07001101 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001102 # Make sure no new events show up while we're checking
1103 self.worker.lock.acquire()
1104 # have all build states propogated to zuul?
1105 if self.haveAllBuildsReported():
1106 # Join ensures that the queue is empty _and_ events have been
1107 # processed
1108 self.fake_gerrit.event_queue.join()
1109 self.sched.trigger_event_queue.join()
1110 self.sched.result_event_queue.join()
James E. Blaira84f0e42014-02-06 07:09:22 -08001111 self.sched.run_handler_lock.acquire()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001112 if (self.sched.trigger_event_queue.empty() and
1113 self.sched.result_event_queue.empty() and
1114 self.fake_gerrit.event_queue.empty() and
James E. Blair4076e2b2014-01-28 12:42:20 -08001115 not self.merge_client.build_sets and
James E. Blair9c2985a2014-03-10 10:59:05 -07001116 self.haveAllBuildsReported() and
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001117 self.areAllBuildsWaiting()):
James E. Blaira84f0e42014-02-06 07:09:22 -08001118 self.sched.run_handler_lock.release()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001119 self.worker.lock.release()
1120 self.log.debug("...settled.")
1121 return
James E. Blaira84f0e42014-02-06 07:09:22 -08001122 self.sched.run_handler_lock.release()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001123 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001124 self.sched.wake_event.wait(0.1)
1125
James E. Blaird466dc42012-07-31 10:42:56 -07001126 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -08001127 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -07001128 return len(jobs)
1129
James E. Blair4ca985f2013-05-30 12:27:43 -07001130 def getJobFromHistory(self, name):
1131 history = self.worker.build_history
1132 for job in history:
1133 if job.name == name:
1134 return job
1135 raise Exception("Unable to find job %s in history" % name)
1136
James E. Blaire0487072012-08-29 17:38:31 -07001137 def assertEmptyQueues(self):
1138 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001139 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001140 for queue in pipeline.queues:
1141 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001142 print 'pipeline %s queue %s contents %s' % (
1143 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001144 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001145
James E. Blair66eeebf2013-07-27 17:44:32 -07001146 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001147 start = time.time()
1148 while time.time() < (start + 5):
1149 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001150 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001151 k, v = stat.split(':')
1152 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001153 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001154 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001155 elif value:
1156 if value == v:
1157 return
1158 elif kind:
1159 if v.endswith('|' + kind):
1160 return
James E. Blair412e5582013-04-22 15:50:12 -07001161 time.sleep(0.1)
1162
1163 pprint.pprint(self.statsd.stats)
1164 raise Exception("Key %s not found in reported stats" % key)
1165
James E. Blairb0fcae42012-07-17 11:12:10 -07001166 def test_jobs_launched(self):
1167 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001168
James E. Blairb0fcae42012-07-17 11:12:10 -07001169 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001170 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001171 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1172 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001173 self.assertEqual(self.getJobFromHistory('project-merge').result,
1174 'SUCCESS')
1175 self.assertEqual(self.getJobFromHistory('project-test1').result,
1176 'SUCCESS')
1177 self.assertEqual(self.getJobFromHistory('project-test2').result,
1178 'SUCCESS')
1179 self.assertEqual(A.data['status'], 'MERGED')
1180 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001181
James E. Blair66eeebf2013-07-27 17:44:32 -07001182 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1183 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1184 value='1|g')
1185 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1186 kind='ms')
1187 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1188 value='1|c')
1189 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1190 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1191 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001192 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001193 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001194 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001195 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001196
James E. Blair3cb10702013-08-24 08:56:03 -07001197 def test_initial_pipeline_gauges(self):
1198 "Test that each pipeline reported its length on start"
1199 pipeline_names = self.sched.layout.pipelines.keys()
1200 self.assertNotEqual(len(pipeline_names), 0)
1201 for name in pipeline_names:
1202 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
1203 value='0|g')
1204
James E. Blair42f74822013-05-14 15:18:03 -07001205 def test_duplicate_pipelines(self):
1206 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001207
James E. Blair42f74822013-05-14 15:18:03 -07001208 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1209 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1210 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001211
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001212 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001213 self.history[0].name == 'project-test1'
1214 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001215
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001216 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001217 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001218 self.assertIn('dup1/project-test1', A.messages[0])
1219 self.assertNotIn('dup2/project-test1', A.messages[0])
1220 self.assertNotIn('dup1/project-test1', A.messages[1])
1221 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001222 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001223 self.assertIn('dup1/project-test1', A.messages[1])
1224 self.assertNotIn('dup2/project-test1', A.messages[1])
1225 self.assertNotIn('dup1/project-test1', A.messages[0])
1226 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001227
James E. Blairb0fcae42012-07-17 11:12:10 -07001228 def test_parallel_changes(self):
1229 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001230
1231 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001232 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1233 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1234 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001235 A.addApproval('CRVW', 2)
1236 B.addApproval('CRVW', 2)
1237 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001238
1239 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1240 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1241 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1242
1243 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001244 self.assertEqual(len(self.builds), 1)
1245 self.assertEqual(self.builds[0].name, 'project-merge')
1246 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001247
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001248 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001249 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001250 self.assertEqual(len(self.builds), 3)
1251 self.assertEqual(self.builds[0].name, 'project-test1')
1252 self.assertTrue(self.job_has_changes(self.builds[0], A))
1253 self.assertEqual(self.builds[1].name, 'project-test2')
1254 self.assertTrue(self.job_has_changes(self.builds[1], A))
1255 self.assertEqual(self.builds[2].name, 'project-merge')
1256 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001257
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001258 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001259 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001260 self.assertEqual(len(self.builds), 5)
1261 self.assertEqual(self.builds[0].name, 'project-test1')
1262 self.assertTrue(self.job_has_changes(self.builds[0], A))
1263 self.assertEqual(self.builds[1].name, 'project-test2')
1264 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001265
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001266 self.assertEqual(self.builds[2].name, 'project-test1')
1267 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1268 self.assertEqual(self.builds[3].name, 'project-test2')
1269 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001270
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001271 self.assertEqual(self.builds[4].name, 'project-merge')
1272 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001273
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001274 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001275 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001276 self.assertEqual(len(self.builds), 6)
1277 self.assertEqual(self.builds[0].name, 'project-test1')
1278 self.assertTrue(self.job_has_changes(self.builds[0], A))
1279 self.assertEqual(self.builds[1].name, 'project-test2')
1280 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001281
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001282 self.assertEqual(self.builds[2].name, 'project-test1')
1283 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1284 self.assertEqual(self.builds[3].name, 'project-test2')
1285 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001286
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001287 self.assertEqual(self.builds[4].name, 'project-test1')
1288 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1289 self.assertEqual(self.builds[5].name, 'project-test2')
1290 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001291
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001292 self.worker.hold_jobs_in_build = False
1293 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001294 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001295 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001296
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001297 self.assertEqual(len(self.history), 9)
1298 self.assertEqual(A.data['status'], 'MERGED')
1299 self.assertEqual(B.data['status'], 'MERGED')
1300 self.assertEqual(C.data['status'], 'MERGED')
1301 self.assertEqual(A.reported, 2)
1302 self.assertEqual(B.reported, 2)
1303 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001304
1305 def test_failed_changes(self):
1306 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001307 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001308
James E. Blairb02a3bb2012-07-30 17:49:55 -07001309 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1310 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001311 A.addApproval('CRVW', 2)
1312 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001313
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001314 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001315
James E. Blaire2819012013-06-28 17:17:26 -04001316 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1317 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001318 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001319
1320 self.worker.release('.*-merge')
1321 self.waitUntilSettled()
1322
1323 self.worker.hold_jobs_in_build = False
1324 self.worker.release()
1325
1326 self.waitUntilSettled()
1327 # It's certain that the merge job for change 2 will run, but
1328 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001329 self.assertTrue(len(self.history) > 6)
1330 self.assertEqual(A.data['status'], 'NEW')
1331 self.assertEqual(B.data['status'], 'MERGED')
1332 self.assertEqual(A.reported, 2)
1333 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001334
1335 def test_independent_queues(self):
1336 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001337
1338 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001339 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001340 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1341 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001342 A.addApproval('CRVW', 2)
1343 B.addApproval('CRVW', 2)
1344 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001345
1346 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1347 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1348 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1349
James E. Blairb02a3bb2012-07-30 17:49:55 -07001350 self.waitUntilSettled()
1351
1352 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001353 self.assertEqual(len(self.builds), 2)
1354 self.assertEqual(self.builds[0].name, 'project-merge')
1355 self.assertTrue(self.job_has_changes(self.builds[0], A))
1356 self.assertEqual(self.builds[1].name, 'project1-merge')
1357 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001358
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001359 # Release the current merge builds
1360 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001361 self.waitUntilSettled()
1362 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001363 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001364 self.waitUntilSettled()
1365
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001366 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001367 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001368 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001369
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001370 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001371 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001372 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001373
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001374 self.assertEqual(len(self.history), 11)
1375 self.assertEqual(A.data['status'], 'MERGED')
1376 self.assertEqual(B.data['status'], 'MERGED')
1377 self.assertEqual(C.data['status'], 'MERGED')
1378 self.assertEqual(A.reported, 2)
1379 self.assertEqual(B.reported, 2)
1380 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001381
1382 def test_failed_change_at_head(self):
1383 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001384
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001385 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001386 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1387 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1388 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001389 A.addApproval('CRVW', 2)
1390 B.addApproval('CRVW', 2)
1391 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001392
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001393 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001394
1395 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1396 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1397 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1398
1399 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001400
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001401 self.assertEqual(len(self.builds), 1)
1402 self.assertEqual(self.builds[0].name, 'project-merge')
1403 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001404
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001405 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001406 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001407 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001408 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001409 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001410 self.waitUntilSettled()
1411
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001412 self.assertEqual(len(self.builds), 6)
1413 self.assertEqual(self.builds[0].name, 'project-test1')
1414 self.assertEqual(self.builds[1].name, 'project-test2')
1415 self.assertEqual(self.builds[2].name, 'project-test1')
1416 self.assertEqual(self.builds[3].name, 'project-test2')
1417 self.assertEqual(self.builds[4].name, 'project-test1')
1418 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001419
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001420 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001421 self.waitUntilSettled()
1422
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001423 # project-test2, project-merge for B
1424 self.assertEqual(len(self.builds), 2)
1425 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001426
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001427 self.worker.hold_jobs_in_build = False
1428 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001429 self.waitUntilSettled()
1430
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001431 self.assertEqual(len(self.builds), 0)
1432 self.assertEqual(len(self.history), 15)
1433 self.assertEqual(A.data['status'], 'NEW')
1434 self.assertEqual(B.data['status'], 'MERGED')
1435 self.assertEqual(C.data['status'], 'MERGED')
1436 self.assertEqual(A.reported, 2)
1437 self.assertEqual(B.reported, 2)
1438 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001439
James E. Blair0aac4872013-08-23 14:02:38 -07001440 def test_failed_change_in_middle(self):
1441 "Test a failed change in the middle of the queue"
1442
1443 self.worker.hold_jobs_in_build = True
1444 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1445 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1446 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1447 A.addApproval('CRVW', 2)
1448 B.addApproval('CRVW', 2)
1449 C.addApproval('CRVW', 2)
1450
1451 self.worker.addFailTest('project-test1', B)
1452
1453 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1454 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1455 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1456
1457 self.waitUntilSettled()
1458
1459 self.worker.release('.*-merge')
1460 self.waitUntilSettled()
1461 self.worker.release('.*-merge')
1462 self.waitUntilSettled()
1463 self.worker.release('.*-merge')
1464 self.waitUntilSettled()
1465
1466 self.assertEqual(len(self.builds), 6)
1467 self.assertEqual(self.builds[0].name, 'project-test1')
1468 self.assertEqual(self.builds[1].name, 'project-test2')
1469 self.assertEqual(self.builds[2].name, 'project-test1')
1470 self.assertEqual(self.builds[3].name, 'project-test2')
1471 self.assertEqual(self.builds[4].name, 'project-test1')
1472 self.assertEqual(self.builds[5].name, 'project-test2')
1473
1474 self.release(self.builds[2])
1475 self.waitUntilSettled()
1476
James E. Blair972e3c72013-08-29 12:04:55 -07001477 # project-test1 and project-test2 for A
1478 # project-test2 for B
1479 # project-merge for C (without B)
1480 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -07001481 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1482
James E. Blair972e3c72013-08-29 12:04:55 -07001483 self.worker.release('.*-merge')
1484 self.waitUntilSettled()
1485
1486 # project-test1 and project-test2 for A
1487 # project-test2 for B
1488 # project-test1 and project-test2 for C
1489 self.assertEqual(len(self.builds), 5)
1490
James E. Blair0aac4872013-08-23 14:02:38 -07001491 items = self.sched.layout.pipelines['gate'].getAllItems()
1492 builds = items[0].current_build_set.getBuilds()
1493 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1494 self.assertEqual(self.countJobResults(builds, None), 2)
1495 builds = items[1].current_build_set.getBuilds()
1496 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1497 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1498 self.assertEqual(self.countJobResults(builds, None), 1)
1499 builds = items[2].current_build_set.getBuilds()
1500 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -07001501 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -07001502
1503 self.worker.hold_jobs_in_build = False
1504 self.worker.release()
1505 self.waitUntilSettled()
1506
1507 self.assertEqual(len(self.builds), 0)
1508 self.assertEqual(len(self.history), 12)
1509 self.assertEqual(A.data['status'], 'MERGED')
1510 self.assertEqual(B.data['status'], 'NEW')
1511 self.assertEqual(C.data['status'], 'MERGED')
1512 self.assertEqual(A.reported, 2)
1513 self.assertEqual(B.reported, 2)
1514 self.assertEqual(C.reported, 2)
1515
James E. Blaird466dc42012-07-31 10:42:56 -07001516 def test_failed_change_at_head_with_queue(self):
1517 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001518
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001519 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001520 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1521 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1522 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001523 A.addApproval('CRVW', 2)
1524 B.addApproval('CRVW', 2)
1525 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001526
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001527 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001528
1529 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1530 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1531 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1532
1533 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001534 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001535 self.assertEqual(len(self.builds), 0)
1536 self.assertEqual(len(queue), 1)
1537 self.assertEqual(queue[0].name, 'build:project-merge')
1538 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001539
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001540 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001541 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001542 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001543 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001544 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001545 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001546 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001547
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001548 self.assertEqual(len(self.builds), 0)
1549 self.assertEqual(len(queue), 6)
1550 self.assertEqual(queue[0].name, 'build:project-test1')
1551 self.assertEqual(queue[1].name, 'build:project-test2')
1552 self.assertEqual(queue[2].name, 'build:project-test1')
1553 self.assertEqual(queue[3].name, 'build:project-test2')
1554 self.assertEqual(queue[4].name, 'build:project-test1')
1555 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001556
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001557 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001558 self.waitUntilSettled()
1559
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001560 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001561 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001562 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1563 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001564
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001565 self.gearman_server.hold_jobs_in_queue = False
1566 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001567 self.waitUntilSettled()
1568
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001569 self.assertEqual(len(self.builds), 0)
1570 self.assertEqual(len(self.history), 11)
1571 self.assertEqual(A.data['status'], 'NEW')
1572 self.assertEqual(B.data['status'], 'MERGED')
1573 self.assertEqual(C.data['status'], 'MERGED')
1574 self.assertEqual(A.reported, 2)
1575 self.assertEqual(B.reported, 2)
1576 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001577
James E. Blairfef71632013-09-23 11:15:47 -07001578 def test_two_failed_changes_at_head(self):
1579 "Test that changes are reparented correctly if 2 fail at head"
1580
1581 self.worker.hold_jobs_in_build = True
1582 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1583 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1584 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1585 A.addApproval('CRVW', 2)
1586 B.addApproval('CRVW', 2)
1587 C.addApproval('CRVW', 2)
1588
1589 self.worker.addFailTest('project-test1', A)
1590 self.worker.addFailTest('project-test1', B)
1591
1592 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1593 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1594 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1595 self.waitUntilSettled()
1596
1597 self.worker.release('.*-merge')
1598 self.waitUntilSettled()
1599 self.worker.release('.*-merge')
1600 self.waitUntilSettled()
1601 self.worker.release('.*-merge')
1602 self.waitUntilSettled()
1603
1604 self.assertEqual(len(self.builds), 6)
1605 self.assertEqual(self.builds[0].name, 'project-test1')
1606 self.assertEqual(self.builds[1].name, 'project-test2')
1607 self.assertEqual(self.builds[2].name, 'project-test1')
1608 self.assertEqual(self.builds[3].name, 'project-test2')
1609 self.assertEqual(self.builds[4].name, 'project-test1')
1610 self.assertEqual(self.builds[5].name, 'project-test2')
1611
1612 self.assertTrue(self.job_has_changes(self.builds[0], A))
1613 self.assertTrue(self.job_has_changes(self.builds[2], A))
1614 self.assertTrue(self.job_has_changes(self.builds[2], B))
1615 self.assertTrue(self.job_has_changes(self.builds[4], A))
1616 self.assertTrue(self.job_has_changes(self.builds[4], B))
1617 self.assertTrue(self.job_has_changes(self.builds[4], C))
1618
1619 # Fail change B first
1620 self.release(self.builds[2])
1621 self.waitUntilSettled()
1622
1623 # restart of C after B failure
1624 self.worker.release('.*-merge')
1625 self.waitUntilSettled()
1626
1627 self.assertEqual(len(self.builds), 5)
1628 self.assertEqual(self.builds[0].name, 'project-test1')
1629 self.assertEqual(self.builds[1].name, 'project-test2')
1630 self.assertEqual(self.builds[2].name, 'project-test2')
1631 self.assertEqual(self.builds[3].name, 'project-test1')
1632 self.assertEqual(self.builds[4].name, 'project-test2')
1633
1634 self.assertTrue(self.job_has_changes(self.builds[1], A))
1635 self.assertTrue(self.job_has_changes(self.builds[2], A))
1636 self.assertTrue(self.job_has_changes(self.builds[2], B))
1637 self.assertTrue(self.job_has_changes(self.builds[4], A))
1638 self.assertFalse(self.job_has_changes(self.builds[4], B))
1639 self.assertTrue(self.job_has_changes(self.builds[4], C))
1640
1641 # Finish running all passing jobs for change A
1642 self.release(self.builds[1])
1643 self.waitUntilSettled()
1644 # Fail and report change A
1645 self.release(self.builds[0])
1646 self.waitUntilSettled()
1647
1648 # restart of B,C after A failure
1649 self.worker.release('.*-merge')
1650 self.waitUntilSettled()
1651 self.worker.release('.*-merge')
1652 self.waitUntilSettled()
1653
1654 self.assertEqual(len(self.builds), 4)
1655 self.assertEqual(self.builds[0].name, 'project-test1') # B
1656 self.assertEqual(self.builds[1].name, 'project-test2') # B
1657 self.assertEqual(self.builds[2].name, 'project-test1') # C
1658 self.assertEqual(self.builds[3].name, 'project-test2') # C
1659
1660 self.assertFalse(self.job_has_changes(self.builds[1], A))
1661 self.assertTrue(self.job_has_changes(self.builds[1], B))
1662 self.assertFalse(self.job_has_changes(self.builds[1], C))
1663
1664 self.assertFalse(self.job_has_changes(self.builds[2], A))
1665 # After A failed and B and C restarted, B should be back in
1666 # C's tests because it has not failed yet.
1667 self.assertTrue(self.job_has_changes(self.builds[2], B))
1668 self.assertTrue(self.job_has_changes(self.builds[2], C))
1669
1670 self.worker.hold_jobs_in_build = False
1671 self.worker.release()
1672 self.waitUntilSettled()
1673
1674 self.assertEqual(len(self.builds), 0)
1675 self.assertEqual(len(self.history), 21)
1676 self.assertEqual(A.data['status'], 'NEW')
1677 self.assertEqual(B.data['status'], 'NEW')
1678 self.assertEqual(C.data['status'], 'MERGED')
1679 self.assertEqual(A.reported, 2)
1680 self.assertEqual(B.reported, 2)
1681 self.assertEqual(C.reported, 2)
1682
James E. Blair8c803f82012-07-31 16:25:42 -07001683 def test_patch_order(self):
1684 "Test that dependent patches are tested in the right order"
1685 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1686 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1687 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1688 A.addApproval('CRVW', 2)
1689 B.addApproval('CRVW', 2)
1690 C.addApproval('CRVW', 2)
1691
1692 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1693 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1694 M2.setMerged()
1695 M1.setMerged()
1696
1697 # C -> B -> A -> M1 -> M2
1698 # M2 is here to make sure it is never queried. If it is, it
1699 # means zuul is walking down the entire history of merged
1700 # changes.
1701
1702 C.setDependsOn(B, 1)
1703 B.setDependsOn(A, 1)
1704 A.setDependsOn(M1, 1)
1705 M1.setDependsOn(M2, 1)
1706
1707 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1708
1709 self.waitUntilSettled()
1710
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001711 self.assertEqual(A.data['status'], 'NEW')
1712 self.assertEqual(B.data['status'], 'NEW')
1713 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001714
1715 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1716 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1717
1718 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001719 self.assertEqual(M2.queried, 0)
1720 self.assertEqual(A.data['status'], 'MERGED')
1721 self.assertEqual(B.data['status'], 'MERGED')
1722 self.assertEqual(C.data['status'], 'MERGED')
1723 self.assertEqual(A.reported, 2)
1724 self.assertEqual(B.reported, 2)
1725 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001726
James E. Blair0e933c52013-07-11 10:18:52 -07001727 def test_trigger_cache(self):
1728 "Test that the trigger cache operates correctly"
1729 self.worker.hold_jobs_in_build = True
1730
1731 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1732 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1733 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1734 A.addApproval('CRVW', 2)
1735 B.addApproval('CRVW', 2)
1736
1737 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1738 M1.setMerged()
1739
1740 B.setDependsOn(A, 1)
1741 A.setDependsOn(M1, 1)
1742
1743 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1744 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1745
1746 self.waitUntilSettled()
1747
1748 for build in self.builds:
1749 if build.parameters['ZUUL_PIPELINE'] == 'check':
1750 build.release()
1751 self.waitUntilSettled()
1752 for build in self.builds:
1753 if build.parameters['ZUUL_PIPELINE'] == 'check':
1754 build.release()
1755 self.waitUntilSettled()
1756
1757 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1758 self.waitUntilSettled()
1759
James E. Blair6c358e72013-07-29 17:06:47 -07001760 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001761 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001762 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001763
1764 self.worker.hold_jobs_in_build = False
1765 self.worker.release()
1766 self.waitUntilSettled()
1767
1768 self.assertEqual(A.data['status'], 'MERGED')
1769 self.assertEqual(B.data['status'], 'MERGED')
1770 self.assertEqual(A.queried, 2) # Initial and isMerged
1771 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1772
James E. Blair8c803f82012-07-31 16:25:42 -07001773 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001774 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001775 # TODO: move to test_gerrit (this is a unit test!)
1776 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001777 trigger = self.sched.layout.pipelines['gate'].trigger
1778 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001779 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001780 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001781
1782 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001783 a = trigger.getChange(1, 2, refresh=True)
1784 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001785
1786 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001787 a = trigger.getChange(1, 2, refresh=True)
1788 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1789 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001790
1791 def test_build_configuration(self):
1792 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001793
1794 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001795 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1796 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1797 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1798 A.addApproval('CRVW', 2)
1799 B.addApproval('CRVW', 2)
1800 C.addApproval('CRVW', 2)
1801 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1802 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1803 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1804 self.waitUntilSettled()
1805
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001806 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001807 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001808 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001809 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001810 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001811 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001812 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001813 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1814 self.gearman_server.hold_jobs_in_queue = False
1815 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001816 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001817
Monty Taylorbc758832013-06-17 17:22:42 -04001818 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001819 repo = git.Repo(path)
1820 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1821 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001822 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001823 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001824
1825 def test_build_configuration_conflict(self):
1826 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001827
1828 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001829 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1830 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001831 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001832 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1833 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001834 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001835 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1836 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001837 A.addApproval('CRVW', 2)
1838 B.addApproval('CRVW', 2)
1839 C.addApproval('CRVW', 2)
1840 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1841 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1842 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1843 self.waitUntilSettled()
1844
James E. Blair6736beb2013-07-11 15:18:15 -07001845 self.assertEqual(A.reported, 1)
1846 self.assertEqual(B.reported, 1)
1847 self.assertEqual(C.reported, 1)
1848
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001849 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001850 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001851 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001852 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001853 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001854 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001855
1856 self.assertEqual(len(self.history), 2) # A and C merge jobs
1857
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001858 self.gearman_server.hold_jobs_in_queue = False
1859 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001860 self.waitUntilSettled()
1861
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001862 self.assertEqual(A.data['status'], 'MERGED')
1863 self.assertEqual(B.data['status'], 'NEW')
1864 self.assertEqual(C.data['status'], 'MERGED')
1865 self.assertEqual(A.reported, 2)
1866 self.assertEqual(B.reported, 2)
1867 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001868 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001869
James E. Blairdaabed22012-08-15 15:38:57 -07001870 def test_post(self):
1871 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001872
Zhongyue Luo5d556072012-09-21 02:00:47 +09001873 e = {
1874 "type": "ref-updated",
1875 "submitter": {
1876 "name": "User Name",
1877 },
1878 "refUpdate": {
1879 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1880 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1881 "refName": "master",
1882 "project": "org/project",
1883 }
1884 }
James E. Blairdaabed22012-08-15 15:38:57 -07001885 self.fake_gerrit.addEvent(e)
1886 self.waitUntilSettled()
1887
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001888 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001889 self.assertEqual(len(self.history), 1)
1890 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001891
1892 def test_build_configuration_branch(self):
1893 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001894
1895 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001896 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1897 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1898 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1899 A.addApproval('CRVW', 2)
1900 B.addApproval('CRVW', 2)
1901 C.addApproval('CRVW', 2)
1902 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1903 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1904 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1905 self.waitUntilSettled()
1906
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001907 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001908 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001909 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001910 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001911 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001912 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001913 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001914 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1915 self.gearman_server.hold_jobs_in_queue = False
1916 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001917 self.waitUntilSettled()
1918
Monty Taylorbc758832013-06-17 17:22:42 -04001919 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001920 repo = git.Repo(path)
1921 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1922 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001923 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001924 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001925
1926 def test_build_configuration_branch_interaction(self):
1927 "Test that switching between branches works"
1928 self.test_build_configuration()
1929 self.test_build_configuration_branch()
1930 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001931 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001932 repo = git.Repo(path)
1933 repo.heads.master.commit = repo.commit('init')
1934 self.test_build_configuration()
1935
1936 def test_build_configuration_multi_branch(self):
1937 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001938
1939 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001940 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1941 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1942 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1943 A.addApproval('CRVW', 2)
1944 B.addApproval('CRVW', 2)
1945 C.addApproval('CRVW', 2)
1946 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1947 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1948 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1949 self.waitUntilSettled()
James E. Blairbb1fe502014-03-04 10:15:06 -08001950 queue = self.gearman_server.getQueue()
1951 job_A = None
1952 for job in queue:
1953 if 'project-merge' in job.name:
1954 job_A = job
1955 ref_A = self.getParameter(job_A, 'ZUUL_REF')
1956 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
1957 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
1958 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
James E. Blairc6294a52012-08-17 10:19:48 -07001959
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001960 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001961 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001962 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001963 job_B = None
1964 for job in queue:
1965 if 'project-merge' in job.name:
1966 job_B = job
1967 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001968 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001969 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blairbb1fe502014-03-04 10:15:06 -08001970 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
1971
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001972 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001973 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001974 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001975 for job in queue:
1976 if 'project-merge' in job.name:
1977 job_C = job
1978 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001979 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001980 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blairbb1fe502014-03-04 10:15:06 -08001981 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001982 self.gearman_server.hold_jobs_in_queue = False
1983 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001984 self.waitUntilSettled()
1985
Monty Taylorbc758832013-06-17 17:22:42 -04001986 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001987 repo = git.Repo(path)
1988
1989 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07001990 for c in repo.iter_commits(ref_C)]
James E. Blairbb1fe502014-03-04 10:15:06 -08001991 repo_shas = [c.hexsha for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07001992 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001993 correct_messages = ['initial commit', 'A-1', 'C-1']
James E. Blairbb1fe502014-03-04 10:15:06 -08001994 # Ensure the right commits are in the history for this ref
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001995 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08001996 # Ensure ZUUL_REF -> ZUUL_COMMIT
1997 self.assertEqual(repo_shas[0], commit_C)
James E. Blairc6294a52012-08-17 10:19:48 -07001998
1999 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07002000 for c in repo.iter_commits(ref_B)]
James E. Blairbb1fe502014-03-04 10:15:06 -08002001 repo_shas = [c.hexsha for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07002002 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07002003 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002004 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08002005 self.assertEqual(repo_shas[0], commit_B)
2006
2007 repo_messages = [c.message.strip()
2008 for c in repo.iter_commits(ref_A)]
2009 repo_shas = [c.hexsha for c in repo.iter_commits(ref_A)]
2010 repo_messages.reverse()
2011 correct_messages = ['initial commit', 'A-1']
2012 self.assertEqual(repo_messages, correct_messages)
2013 self.assertEqual(repo_shas[0], commit_A)
2014
2015 self.assertNotEqual(ref_A, ref_B, ref_C)
2016 self.assertNotEqual(commit_A, commit_B, commit_C)
James E. Blair7f71c802012-08-22 13:04:32 -07002017
2018 def test_one_job_project(self):
2019 "Test that queueing works with one job"
2020 A = self.fake_gerrit.addFakeChange('org/one-job-project',
2021 'master', 'A')
2022 B = self.fake_gerrit.addFakeChange('org/one-job-project',
2023 'master', 'B')
2024 A.addApproval('CRVW', 2)
2025 B.addApproval('CRVW', 2)
2026 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2027 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2028 self.waitUntilSettled()
2029
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002030 self.assertEqual(A.data['status'], 'MERGED')
2031 self.assertEqual(A.reported, 2)
2032 self.assertEqual(B.data['status'], 'MERGED')
2033 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07002034
Antoine Musso80edd5a2013-02-13 15:37:53 +01002035 def test_job_from_templates_launched(self):
2036 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002037
Antoine Musso80edd5a2013-02-13 15:37:53 +01002038 A = self.fake_gerrit.addFakeChange(
2039 'org/templated-project', 'master', 'A')
2040 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2041 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01002042
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002043 self.assertEqual(self.getJobFromHistory('project-test1').result,
2044 'SUCCESS')
2045 self.assertEqual(self.getJobFromHistory('project-test2').result,
2046 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01002047
James E. Blair3e98c022013-12-16 15:25:38 -08002048 def test_layered_templates(self):
2049 "Test whether a job generated via a template can be launched"
2050
2051 A = self.fake_gerrit.addFakeChange(
2052 'org/layered-project', 'master', 'A')
2053 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2054 self.waitUntilSettled()
2055
2056 self.assertEqual(self.getJobFromHistory('project-test1').result,
2057 'SUCCESS')
2058 self.assertEqual(self.getJobFromHistory('project-test2').result,
2059 'SUCCESS')
James E. Blairaea6cf62013-12-16 15:38:12 -08002060 self.assertEqual(self.getJobFromHistory('layered-project-test3'
2061 ).result, 'SUCCESS')
2062 self.assertEqual(self.getJobFromHistory('layered-project-test4'
2063 ).result, 'SUCCESS')
2064 # test5 should run twice because two templates define it
James E. Blair3e98c022013-12-16 15:25:38 -08002065 test5_count = 0
2066 for job in self.worker.build_history:
James E. Blairaea6cf62013-12-16 15:38:12 -08002067 if job.name == 'layered-project-foo-test5':
James E. Blair3e98c022013-12-16 15:25:38 -08002068 test5_count += 1
2069 self.assertEqual(job.result, 'SUCCESS')
2070 self.assertEqual(test5_count, 2)
2071 self.assertEqual(self.getJobFromHistory('project-test6').result,
2072 'SUCCESS')
2073
James E. Blaircaec0c52012-08-22 14:52:22 -07002074 def test_dependent_changes_dequeue(self):
2075 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002076
James E. Blaircaec0c52012-08-22 14:52:22 -07002077 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2078 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2079 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2080 A.addApproval('CRVW', 2)
2081 B.addApproval('CRVW', 2)
2082 C.addApproval('CRVW', 2)
2083
2084 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
2085 M1.setMerged()
2086
2087 # C -> B -> A -> M1
2088
2089 C.setDependsOn(B, 1)
2090 B.setDependsOn(A, 1)
2091 A.setDependsOn(M1, 1)
2092
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002093 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07002094
2095 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2096 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2097 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2098
2099 self.waitUntilSettled()
2100
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002101 self.assertEqual(A.data['status'], 'NEW')
2102 self.assertEqual(A.reported, 2)
2103 self.assertEqual(B.data['status'], 'NEW')
2104 self.assertEqual(B.reported, 2)
2105 self.assertEqual(C.data['status'], 'NEW')
2106 self.assertEqual(C.reported, 2)
2107 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07002108
James E. Blair972e3c72013-08-29 12:04:55 -07002109 def test_failing_dependent_changes(self):
2110 "Test that failing dependent patches are taken out of stream"
2111 self.worker.hold_jobs_in_build = True
2112 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2113 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2114 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2115 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2116 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
2117 A.addApproval('CRVW', 2)
2118 B.addApproval('CRVW', 2)
2119 C.addApproval('CRVW', 2)
2120 D.addApproval('CRVW', 2)
2121 E.addApproval('CRVW', 2)
2122
2123 # E, D -> C -> B, A
2124
2125 D.setDependsOn(C, 1)
2126 C.setDependsOn(B, 1)
2127
2128 self.worker.addFailTest('project-test1', B)
2129
2130 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2131 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2132 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2133 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2134 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2135
2136 self.waitUntilSettled()
2137 self.worker.release('.*-merge')
2138 self.waitUntilSettled()
2139 self.worker.release('.*-merge')
2140 self.waitUntilSettled()
2141 self.worker.release('.*-merge')
2142 self.waitUntilSettled()
2143 self.worker.release('.*-merge')
2144 self.waitUntilSettled()
2145 self.worker.release('.*-merge')
2146 self.waitUntilSettled()
2147
2148 self.worker.hold_jobs_in_build = False
2149 for build in self.builds:
2150 if build.parameters['ZUUL_CHANGE'] != '1':
2151 build.release()
2152 self.waitUntilSettled()
2153
2154 self.worker.release()
2155 self.waitUntilSettled()
2156
2157 self.assertEqual(A.data['status'], 'MERGED')
2158 self.assertEqual(A.reported, 2)
2159 self.assertEqual(B.data['status'], 'NEW')
2160 self.assertEqual(B.reported, 2)
2161 self.assertEqual(C.data['status'], 'NEW')
2162 self.assertEqual(C.reported, 2)
2163 self.assertEqual(D.data['status'], 'NEW')
2164 self.assertEqual(D.reported, 2)
2165 self.assertEqual(E.data['status'], 'MERGED')
2166 self.assertEqual(E.reported, 2)
2167 self.assertEqual(len(self.history), 18)
2168
James E. Blairec590122012-08-22 15:19:31 -07002169 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08002170 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07002171 # If it's dequeued more than once, we should see extra
2172 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07002173
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002174 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07002175 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2176 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2177 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2178 A.addApproval('CRVW', 2)
2179 B.addApproval('CRVW', 2)
2180 C.addApproval('CRVW', 2)
2181
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002182 self.worker.addFailTest('project1-test1', A)
2183 self.worker.addFailTest('project1-test2', A)
2184 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07002185
2186 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2187 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2188 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2189
2190 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07002191
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002192 self.assertEqual(len(self.builds), 1)
2193 self.assertEqual(self.builds[0].name, 'project1-merge')
2194 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07002195
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002196 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002197 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002198 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002199 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002200 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002201 self.waitUntilSettled()
2202
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002203 self.assertEqual(len(self.builds), 9)
2204 self.assertEqual(self.builds[0].name, 'project1-test1')
2205 self.assertEqual(self.builds[1].name, 'project1-test2')
2206 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
2207 self.assertEqual(self.builds[3].name, 'project1-test1')
2208 self.assertEqual(self.builds[4].name, 'project1-test2')
2209 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
2210 self.assertEqual(self.builds[6].name, 'project1-test1')
2211 self.assertEqual(self.builds[7].name, 'project1-test2')
2212 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07002213
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002214 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07002215 self.waitUntilSettled()
2216
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002217 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
2218 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07002219
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002220 self.worker.hold_jobs_in_build = False
2221 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07002222 self.waitUntilSettled()
2223
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002224 self.assertEqual(len(self.builds), 0)
2225 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07002226
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002227 self.assertEqual(A.data['status'], 'NEW')
2228 self.assertEqual(B.data['status'], 'MERGED')
2229 self.assertEqual(C.data['status'], 'MERGED')
2230 self.assertEqual(A.reported, 2)
2231 self.assertEqual(B.reported, 2)
2232 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07002233
2234 def test_nonvoting_job(self):
2235 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002236
James E. Blair4ec821f2012-08-23 15:28:28 -07002237 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
2238 'master', 'A')
2239 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002240 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07002241 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2242
2243 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07002244
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002245 self.assertEqual(A.data['status'], 'MERGED')
2246 self.assertEqual(A.reported, 2)
2247 self.assertEqual(
2248 self.getJobFromHistory('nonvoting-project-merge').result,
2249 'SUCCESS')
2250 self.assertEqual(
2251 self.getJobFromHistory('nonvoting-project-test1').result,
2252 'SUCCESS')
2253 self.assertEqual(
2254 self.getJobFromHistory('nonvoting-project-test2').result,
2255 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07002256
2257 def test_check_queue_success(self):
2258 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002259
James E. Blaire0487072012-08-29 17:38:31 -07002260 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2261 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2262
2263 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002264
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002265 self.assertEqual(A.data['status'], 'NEW')
2266 self.assertEqual(A.reported, 1)
2267 self.assertEqual(self.getJobFromHistory('project-merge').result,
2268 'SUCCESS')
2269 self.assertEqual(self.getJobFromHistory('project-test1').result,
2270 'SUCCESS')
2271 self.assertEqual(self.getJobFromHistory('project-test2').result,
2272 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07002273
2274 def test_check_queue_failure(self):
2275 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002276
James E. Blaire0487072012-08-29 17:38:31 -07002277 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002278 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07002279 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2280
2281 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002282
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002283 self.assertEqual(A.data['status'], 'NEW')
2284 self.assertEqual(A.reported, 1)
2285 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07002286 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002287 self.assertEqual(self.getJobFromHistory('project-test1').result,
2288 'SUCCESS')
2289 self.assertEqual(self.getJobFromHistory('project-test2').result,
2290 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07002291
2292 def test_dependent_behind_dequeue(self):
2293 "test that dependent changes behind dequeued changes work"
2294 # This complicated test is a reproduction of a real life bug
2295 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07002296
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002297 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07002298 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2299 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2300 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2301 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2302 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
2303 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
2304 D.setDependsOn(C, 1)
2305 E.setDependsOn(D, 1)
2306 A.addApproval('CRVW', 2)
2307 B.addApproval('CRVW', 2)
2308 C.addApproval('CRVW', 2)
2309 D.addApproval('CRVW', 2)
2310 E.addApproval('CRVW', 2)
2311 F.addApproval('CRVW', 2)
2312
2313 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07002314
2315 # Change object re-use in the gerrit trigger is hidden if
2316 # changes are added in quick succession; waiting makes it more
2317 # like real life.
2318 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2319 self.waitUntilSettled()
2320 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2321 self.waitUntilSettled()
2322
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002323 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002324 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002325 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002326 self.waitUntilSettled()
2327
2328 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2329 self.waitUntilSettled()
2330 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2331 self.waitUntilSettled()
2332 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2333 self.waitUntilSettled()
2334 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
2335 self.waitUntilSettled()
2336
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002337 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002338 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002339 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002340 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002341 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002342 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002343 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002344 self.waitUntilSettled()
2345
2346 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002347
2348 # Grab pointers to the jobs we want to release before
2349 # releasing any, because list indexes may change as
2350 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002351 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002352 a.release()
2353 b.release()
2354 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002355 self.waitUntilSettled()
2356
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002357 self.worker.hold_jobs_in_build = False
2358 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002359 self.waitUntilSettled()
2360
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002361 self.assertEqual(A.data['status'], 'NEW')
2362 self.assertEqual(B.data['status'], 'MERGED')
2363 self.assertEqual(C.data['status'], 'MERGED')
2364 self.assertEqual(D.data['status'], 'MERGED')
2365 self.assertEqual(E.data['status'], 'MERGED')
2366 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002367
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002368 self.assertEqual(A.reported, 2)
2369 self.assertEqual(B.reported, 2)
2370 self.assertEqual(C.reported, 2)
2371 self.assertEqual(D.reported, 2)
2372 self.assertEqual(E.reported, 2)
2373 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002374
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002375 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2376 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002377
2378 def test_merger_repack(self):
2379 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002380
James E. Blair05fed602012-09-07 12:45:24 -07002381 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2382 A.addApproval('CRVW', 2)
2383 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2384 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002385 self.assertEqual(self.getJobFromHistory('project-merge').result,
2386 'SUCCESS')
2387 self.assertEqual(self.getJobFromHistory('project-test1').result,
2388 'SUCCESS')
2389 self.assertEqual(self.getJobFromHistory('project-test2').result,
2390 'SUCCESS')
2391 self.assertEqual(A.data['status'], 'MERGED')
2392 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07002393 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002394 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002395
Monty Taylorbc758832013-06-17 17:22:42 -04002396 path = os.path.join(self.git_root, "org/project")
2397 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002398
2399 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2400 A.addApproval('CRVW', 2)
2401 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2402 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002403 self.assertEqual(self.getJobFromHistory('project-merge').result,
2404 'SUCCESS')
2405 self.assertEqual(self.getJobFromHistory('project-test1').result,
2406 'SUCCESS')
2407 self.assertEqual(self.getJobFromHistory('project-test2').result,
2408 'SUCCESS')
2409 self.assertEqual(A.data['status'], 'MERGED')
2410 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002411
James E. Blair4886f282012-11-15 09:27:33 -08002412 def test_merger_repack_large_change(self):
2413 "Test that the merger works with large changes after a repack"
2414 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08002415 # This test assumes the repo is already cloned; make sure it is
2416 url = self.sched.triggers['gerrit'].getGitUrl(
2417 self.sched.layout.projects['org/project1'])
James E. Blair4076e2b2014-01-28 12:42:20 -08002418 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08002419 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2420 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002421 path = os.path.join(self.upstream_root, "org/project1")
2422 print repack_repo(path)
2423 path = os.path.join(self.git_root, "org/project1")
2424 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002425
2426 A.addApproval('CRVW', 2)
2427 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2428 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002429 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2430 'SUCCESS')
2431 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2432 'SUCCESS')
2433 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2434 'SUCCESS')
2435 self.assertEqual(A.data['status'], 'MERGED')
2436 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002437
James E. Blair7ee88a22012-09-12 18:59:31 +02002438 def test_nonexistent_job(self):
2439 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002440 # Set to the state immediately after a restart
2441 self.resetGearmanServer()
2442 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002443
2444 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2445 A.addApproval('CRVW', 2)
2446 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2447 # There may be a thread about to report a lost change
2448 while A.reported < 2:
2449 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002450 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002451 self.assertFalse(job_names)
2452 self.assertEqual(A.data['status'], 'NEW')
2453 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002454 self.assertEmptyQueues()
2455
2456 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002457 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002458 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2459 A.addApproval('CRVW', 2)
2460 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2461 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002462 self.assertEqual(self.getJobFromHistory('project-merge').result,
2463 'SUCCESS')
2464 self.assertEqual(self.getJobFromHistory('project-test1').result,
2465 'SUCCESS')
2466 self.assertEqual(self.getJobFromHistory('project-test2').result,
2467 'SUCCESS')
2468 self.assertEqual(A.data['status'], 'MERGED')
2469 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002470
2471 def test_single_nonexistent_post_job(self):
2472 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002473 e = {
2474 "type": "ref-updated",
2475 "submitter": {
2476 "name": "User Name",
2477 },
2478 "refUpdate": {
2479 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2480 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2481 "refName": "master",
2482 "project": "org/project",
2483 }
2484 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002485 # Set to the state immediately after a restart
2486 self.resetGearmanServer()
2487 self.launcher.negative_function_cache_ttl = 0
2488
James E. Blairf62d4282012-12-31 17:01:50 -08002489 self.fake_gerrit.addEvent(e)
2490 self.waitUntilSettled()
2491
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002492 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002493
2494 def test_new_patchset_dequeues_old(self):
2495 "Test that a new patchset causes the old to be dequeued"
2496 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002497 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002498 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2499 M.setMerged()
2500
2501 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2502 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2503 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2504 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2505 A.addApproval('CRVW', 2)
2506 B.addApproval('CRVW', 2)
2507 C.addApproval('CRVW', 2)
2508 D.addApproval('CRVW', 2)
2509
2510 C.setDependsOn(B, 1)
2511 B.setDependsOn(A, 1)
2512 A.setDependsOn(M, 1)
2513
2514 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2515 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2516 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2517 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2518 self.waitUntilSettled()
2519
2520 B.addPatchset()
2521 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2522 self.waitUntilSettled()
2523
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002524 self.worker.hold_jobs_in_build = False
2525 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002526 self.waitUntilSettled()
2527
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002528 self.assertEqual(A.data['status'], 'MERGED')
2529 self.assertEqual(A.reported, 2)
2530 self.assertEqual(B.data['status'], 'NEW')
2531 self.assertEqual(B.reported, 2)
2532 self.assertEqual(C.data['status'], 'NEW')
2533 self.assertEqual(C.reported, 2)
2534 self.assertEqual(D.data['status'], 'MERGED')
2535 self.assertEqual(D.reported, 2)
2536 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002537
Arx Cruzb1b010d2013-10-28 19:49:59 -02002538 def test_zuul_url_return(self):
2539 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08002540 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Arx Cruzb1b010d2013-10-28 19:49:59 -02002541 self.worker.hold_jobs_in_build = True
2542
2543 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2544 A.addApproval('CRVW', 2)
2545 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2546 self.waitUntilSettled()
2547
2548 self.assertEqual(len(self.builds), 1)
2549 for build in self.builds:
2550 self.assertTrue('ZUUL_URL' in build.parameters)
2551
2552 self.worker.hold_jobs_in_build = False
2553 self.worker.release()
2554 self.waitUntilSettled()
2555
James E. Blair2fa50962013-01-30 21:50:41 -08002556 def test_new_patchset_dequeues_old_on_head(self):
2557 "Test that a new patchset causes the old to be dequeued (at head)"
2558 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002559 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002560 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2561 M.setMerged()
2562 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2563 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2564 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2565 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2566 A.addApproval('CRVW', 2)
2567 B.addApproval('CRVW', 2)
2568 C.addApproval('CRVW', 2)
2569 D.addApproval('CRVW', 2)
2570
2571 C.setDependsOn(B, 1)
2572 B.setDependsOn(A, 1)
2573 A.setDependsOn(M, 1)
2574
2575 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2576 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2577 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2578 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2579 self.waitUntilSettled()
2580
2581 A.addPatchset()
2582 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2583 self.waitUntilSettled()
2584
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002585 self.worker.hold_jobs_in_build = False
2586 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002587 self.waitUntilSettled()
2588
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002589 self.assertEqual(A.data['status'], 'NEW')
2590 self.assertEqual(A.reported, 2)
2591 self.assertEqual(B.data['status'], 'NEW')
2592 self.assertEqual(B.reported, 2)
2593 self.assertEqual(C.data['status'], 'NEW')
2594 self.assertEqual(C.reported, 2)
2595 self.assertEqual(D.data['status'], 'MERGED')
2596 self.assertEqual(D.reported, 2)
2597 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002598
2599 def test_new_patchset_dequeues_old_without_dependents(self):
2600 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002601 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002602 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2603 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2604 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2605 A.addApproval('CRVW', 2)
2606 B.addApproval('CRVW', 2)
2607 C.addApproval('CRVW', 2)
2608
2609 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2610 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2611 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2612 self.waitUntilSettled()
2613
2614 B.addPatchset()
2615 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2616 self.waitUntilSettled()
2617
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002618 self.worker.hold_jobs_in_build = False
2619 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002620 self.waitUntilSettled()
2621
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002622 self.assertEqual(A.data['status'], 'MERGED')
2623 self.assertEqual(A.reported, 2)
2624 self.assertEqual(B.data['status'], 'NEW')
2625 self.assertEqual(B.reported, 2)
2626 self.assertEqual(C.data['status'], 'MERGED')
2627 self.assertEqual(C.reported, 2)
2628 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002629
2630 def test_new_patchset_dequeues_old_independent_queue(self):
2631 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002632 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002633 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2634 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2635 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2636 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2637 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2638 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2639 self.waitUntilSettled()
2640
2641 B.addPatchset()
2642 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2643 self.waitUntilSettled()
2644
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002645 self.worker.hold_jobs_in_build = False
2646 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002647 self.waitUntilSettled()
2648
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002649 self.assertEqual(A.data['status'], 'NEW')
2650 self.assertEqual(A.reported, 1)
2651 self.assertEqual(B.data['status'], 'NEW')
2652 self.assertEqual(B.reported, 1)
2653 self.assertEqual(C.data['status'], 'NEW')
2654 self.assertEqual(C.reported, 1)
2655 self.assertEqual(len(self.history), 10)
2656 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002657
James E. Blair18c64442014-03-18 10:14:45 -07002658 def test_noop_job(self):
2659 "Test that the internal noop job works"
2660 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
2661 A.addApproval('CRVW', 2)
2662 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2663 self.waitUntilSettled()
2664
2665 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2666 self.assertTrue(self.sched._areAllBuildsComplete())
2667 self.assertEqual(len(self.history), 0)
2668 self.assertEqual(A.data['status'], 'MERGED')
2669 self.assertEqual(A.reported, 2)
2670
James E. Blair7d0dedc2013-02-21 17:26:09 -08002671 def test_zuul_refs(self):
2672 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002673 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002674 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2675 M1.setMerged()
2676 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2677 M2.setMerged()
2678
2679 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2680 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2681 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2682 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2683 A.addApproval('CRVW', 2)
2684 B.addApproval('CRVW', 2)
2685 C.addApproval('CRVW', 2)
2686 D.addApproval('CRVW', 2)
2687 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2688 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2689 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2690 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2691
2692 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002693 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002694 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002695 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002696 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002697 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002698 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002699 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002700 self.waitUntilSettled()
2701
James E. Blair7d0dedc2013-02-21 17:26:09 -08002702 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002703 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002704 if x.parameters['ZUUL_CHANGE'] == '3':
2705 a_zref = x.parameters['ZUUL_REF']
2706 if x.parameters['ZUUL_CHANGE'] == '4':
2707 b_zref = x.parameters['ZUUL_REF']
2708 if x.parameters['ZUUL_CHANGE'] == '5':
2709 c_zref = x.parameters['ZUUL_REF']
2710 if x.parameters['ZUUL_CHANGE'] == '6':
2711 d_zref = x.parameters['ZUUL_REF']
2712
2713 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002714 self.assertIsNotNone(a_zref)
2715 self.assertIsNotNone(b_zref)
2716 self.assertIsNotNone(c_zref)
2717 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002718
2719 # And they should all be different
2720 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002721 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002722
2723 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002724 self.assertTrue(self.ref_has_change(a_zref, A))
2725 self.assertFalse(self.ref_has_change(a_zref, B))
2726 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002727
2728 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002729 self.assertTrue(self.ref_has_change(b_zref, A))
2730 self.assertTrue(self.ref_has_change(b_zref, B))
2731 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002732
2733 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002734 self.assertTrue(self.ref_has_change(c_zref, A))
2735 self.assertTrue(self.ref_has_change(c_zref, B))
2736 self.assertTrue(self.ref_has_change(c_zref, C))
2737 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002738
2739 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002740 self.assertTrue(self.ref_has_change(d_zref, A))
2741 self.assertTrue(self.ref_has_change(d_zref, B))
2742 self.assertTrue(self.ref_has_change(d_zref, C))
2743 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002744
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002745 self.worker.hold_jobs_in_build = False
2746 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002747 self.waitUntilSettled()
2748
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002749 self.assertEqual(A.data['status'], 'MERGED')
2750 self.assertEqual(A.reported, 2)
2751 self.assertEqual(B.data['status'], 'MERGED')
2752 self.assertEqual(B.reported, 2)
2753 self.assertEqual(C.data['status'], 'MERGED')
2754 self.assertEqual(C.reported, 2)
2755 self.assertEqual(D.data['status'], 'MERGED')
2756 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002757
James E. Blairc053d022014-01-22 14:57:33 -08002758 def test_required_approval_check_and_gate(self):
2759 "Test required-approval triggers both check and gate"
2760 self.config.set('zuul', 'layout_config',
2761 'tests/fixtures/layout-require-approval.yaml')
2762 self.sched.reconfigure(self.config)
2763 self.registerJobs()
2764
2765 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2766 A.addApproval('CRVW', 2)
2767 # Add a too-old +1
2768 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2769
2770 aprv = A.addApproval('APRV', 1)
2771 self.fake_gerrit.addEvent(aprv)
2772 self.waitUntilSettled()
2773 # Should have run a check job
2774 self.assertEqual(len(self.history), 1)
2775 self.assertEqual(self.history[0].name, 'project-check')
2776
2777 # Report the result of that check job (overrides previous vrfy)
2778 # Skynet alert: this should trigger a gate job now that
2779 # all reqs are met
2780 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2781 self.waitUntilSettled()
2782 self.assertEqual(len(self.history), 2)
2783 self.assertEqual(self.history[1].name, 'project-gate')
2784
2785 def test_required_approval_newer(self):
2786 "Test required-approval newer trigger parameter"
2787 self.config.set('zuul', 'layout_config',
2788 'tests/fixtures/layout-require-approval.yaml')
2789 self.sched.reconfigure(self.config)
2790 self.registerJobs()
2791
2792 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2793 A.addApproval('CRVW', 2)
2794 aprv = A.addApproval('APRV', 1)
2795 self.fake_gerrit.addEvent(aprv)
2796 self.waitUntilSettled()
2797 # No +1 from Jenkins so should not be enqueued
2798 self.assertEqual(len(self.history), 0)
2799
2800 # Add a too-old +1, should trigger check but not gate
2801 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2802 self.fake_gerrit.addEvent(aprv)
2803 self.waitUntilSettled()
2804 self.assertEqual(len(self.history), 1)
2805 self.assertEqual(self.history[0].name, 'project-check')
2806
2807 # Add a recent +1
2808 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2809 self.fake_gerrit.addEvent(aprv)
2810 self.waitUntilSettled()
2811 self.assertEqual(len(self.history), 2)
2812 self.assertEqual(self.history[1].name, 'project-gate')
2813
2814 def test_required_approval_older(self):
2815 "Test required-approval older trigger parameter"
2816 self.config.set('zuul', 'layout_config',
2817 'tests/fixtures/layout-require-approval.yaml')
2818 self.sched.reconfigure(self.config)
2819 self.registerJobs()
2820
2821 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2822 crvw = A.addApproval('CRVW', 2)
2823 self.fake_gerrit.addEvent(crvw)
2824 self.waitUntilSettled()
2825 # No +1 from Jenkins so should not be enqueued
2826 self.assertEqual(len(self.history), 0)
2827
2828 # Add an old +1 and trigger check with a comment
2829 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2830 self.fake_gerrit.addEvent(crvw)
2831 self.waitUntilSettled()
2832 self.assertEqual(len(self.history), 1)
2833 self.assertEqual(self.history[0].name, 'project-check')
2834
2835 # Add a recent +1 and make sure nothing changes
2836 A.addApproval('VRFY', 1)
2837 self.fake_gerrit.addEvent(crvw)
2838 self.waitUntilSettled()
2839 self.assertEqual(len(self.history), 1)
2840
2841 # The last thing we did was query a change then do nothing
2842 # with a pipeline, so it will be in the cache; clean it up so
2843 # it does not fail the test.
2844 for pipeline in self.sched.layout.pipelines.values():
2845 pipeline.trigger.maintainCache([])
2846
James E. Blair4a28a882013-08-23 15:17:33 -07002847 def test_rerun_on_error(self):
2848 "Test that if a worker fails to run a job, it is run again"
2849 self.worker.hold_jobs_in_build = True
2850 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2851 A.addApproval('CRVW', 2)
2852 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2853 self.waitUntilSettled()
2854
2855 self.builds[0].run_error = True
2856 self.worker.hold_jobs_in_build = False
2857 self.worker.release()
2858 self.waitUntilSettled()
2859 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2860 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2861
James E. Blair412e5582013-04-22 15:50:12 -07002862 def test_statsd(self):
2863 "Test each of the statsd methods used in the scheduler"
2864 import extras
2865 statsd = extras.try_import('statsd.statsd')
2866 statsd.incr('test-incr')
2867 statsd.timing('test-timing', 3)
2868 statsd.gauge('test-guage', 12)
2869 self.assertReportedStat('test-incr', '1|c')
2870 self.assertReportedStat('test-timing', '3|ms')
2871 self.assertReportedStat('test-guage', '12|g')
2872
James E. Blairdad52252014-02-07 16:59:17 -08002873 def test_stuck_job_cleanup(self):
2874 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07002875 # This job won't be registered at startup because it is not in
2876 # the standard layout, but we need it to already be registerd
2877 # for when we reconfigure, as that is when Zuul will attempt
2878 # to run the new job.
2879 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002880 self.gearman_server.hold_jobs_in_queue = True
2881 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2882 A.addApproval('CRVW', 2)
2883 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2884 self.waitUntilSettled()
2885 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2886
2887 self.config.set('zuul', 'layout_config',
2888 'tests/fixtures/layout-no-jobs.yaml')
2889 self.sched.reconfigure(self.config)
2890 self.waitUntilSettled()
2891
James E. Blair18c64442014-03-18 10:14:45 -07002892 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002893 self.waitUntilSettled()
2894 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2895 self.assertTrue(self.sched._areAllBuildsComplete())
2896
2897 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002898 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002899 self.assertEqual(self.history[0].result, 'SUCCESS')
2900
James E. Blair70c71582013-03-06 08:50:50 -08002901 def test_file_jobs(self):
2902 "Test that file jobs run only when appropriate"
2903 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2904 A.addPatchset(['pip-requires'])
2905 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2906 A.addApproval('CRVW', 2)
2907 B.addApproval('CRVW', 2)
2908 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2909 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2910 self.waitUntilSettled()
2911
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002912 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002913 if x.name == 'project-testfile']
2914
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002915 self.assertEqual(len(testfile_jobs), 1)
2916 self.assertEqual(testfile_jobs[0].changes, '1,2')
2917 self.assertEqual(A.data['status'], 'MERGED')
2918 self.assertEqual(A.reported, 2)
2919 self.assertEqual(B.data['status'], 'MERGED')
2920 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002921
2922 def test_test_config(self):
2923 "Test that we can test the config"
2924 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002925 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002926 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002927 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002928
2929 def test_build_description(self):
2930 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002931 self.worker.registerFunction('set_description:' +
2932 self.worker.worker_id)
2933
2934 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2935 A.addApproval('CRVW', 2)
2936 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2937 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002938 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002939 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002940 self.assertTrue(re.search("Branch.*master", desc))
2941 self.assertTrue(re.search("Pipeline.*gate", desc))
2942 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2943 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2944 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2945 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002946
James E. Blair64ed6f22013-07-10 14:07:23 -07002947 def test_queue_precedence(self):
2948 "Test that queue precedence works"
2949
2950 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002951 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002952 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2953 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2954 A.addApproval('CRVW', 2)
2955 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2956
2957 self.waitUntilSettled()
2958 self.gearman_server.hold_jobs_in_queue = False
2959 self.gearman_server.release()
2960 self.waitUntilSettled()
2961
James E. Blair8de58bd2013-07-18 16:23:33 -07002962 # Run one build at a time to ensure non-race order:
2963 for x in range(6):
2964 self.release(self.builds[0])
2965 self.waitUntilSettled()
2966 self.worker.hold_jobs_in_build = False
2967 self.waitUntilSettled()
2968
James E. Blair64ed6f22013-07-10 14:07:23 -07002969 self.log.debug(self.history)
2970 self.assertEqual(self.history[0].pipeline, 'gate')
2971 self.assertEqual(self.history[1].pipeline, 'check')
2972 self.assertEqual(self.history[2].pipeline, 'gate')
2973 self.assertEqual(self.history[3].pipeline, 'gate')
2974 self.assertEqual(self.history[4].pipeline, 'check')
2975 self.assertEqual(self.history[5].pipeline, 'check')
2976
James E. Blair1843a552013-07-03 14:19:52 -07002977 def test_json_status(self):
2978 "Test that we can retrieve JSON status info"
2979 self.worker.hold_jobs_in_build = True
2980 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2981 A.addApproval('CRVW', 2)
2982 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2983 self.waitUntilSettled()
2984
2985 port = self.webapp.server.socket.getsockname()[1]
2986
2987 f = urllib.urlopen("http://localhost:%s/status.json" % port)
2988 data = f.read()
2989
2990 self.worker.hold_jobs_in_build = False
2991 self.worker.release()
2992 self.waitUntilSettled()
2993
2994 data = json.loads(data)
2995 status_jobs = set()
2996 for p in data['pipelines']:
2997 for q in p['change_queues']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002998 if q['dependent']:
2999 self.assertEqual(q['window'], 20)
3000 else:
3001 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07003002 for head in q['heads']:
3003 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08003004 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003005 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07003006 for job in change['jobs']:
3007 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003008 self.assertIn('project-merge', status_jobs)
3009 self.assertIn('project-test1', status_jobs)
3010 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07003011
James E. Blairc3d428e2013-12-03 15:06:48 -08003012 def test_merging_queues(self):
3013 "Test that transitively-connected change queues are merged"
3014 self.config.set('zuul', 'layout_config',
3015 'tests/fixtures/layout-merge-queues.yaml')
3016 self.sched.reconfigure(self.config)
3017 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
3018
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003019 def test_node_label(self):
3020 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003021 self.worker.registerFunction('build:node-project-test1:debian')
3022
3023 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
3024 A.addApproval('CRVW', 2)
3025 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3026 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07003027
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003028 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3029 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3030 'debian')
3031 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07003032
3033 def test_live_reconfiguration(self):
3034 "Test that live reconfiguration works"
3035 self.worker.hold_jobs_in_build = True
3036 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3037 A.addApproval('CRVW', 2)
3038 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3039 self.waitUntilSettled()
3040
3041 self.sched.reconfigure(self.config)
3042
3043 self.worker.hold_jobs_in_build = False
3044 self.worker.release()
3045 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003046 self.assertEqual(self.getJobFromHistory('project-merge').result,
3047 'SUCCESS')
3048 self.assertEqual(self.getJobFromHistory('project-test1').result,
3049 'SUCCESS')
3050 self.assertEqual(self.getJobFromHistory('project-test2').result,
3051 'SUCCESS')
3052 self.assertEqual(A.data['status'], 'MERGED')
3053 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07003054
James E. Blaire712d9f2013-07-31 11:40:11 -07003055 def test_live_reconfiguration_functions(self):
3056 "Test live reconfiguration with a custom function"
3057 self.worker.registerFunction('build:node-project-test1:debian')
3058 self.worker.registerFunction('build:node-project-test1:wheezy')
3059 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
3060 A.addApproval('CRVW', 2)
3061 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3062 self.waitUntilSettled()
3063
3064 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3065 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3066 'debian')
3067 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
3068
3069 self.config.set('zuul', 'layout_config',
3070 'tests/fixtures/layout-live-'
3071 'reconfiguration-functions.yaml')
3072 self.sched.reconfigure(self.config)
3073 self.worker.build_history = []
3074
3075 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
3076 B.addApproval('CRVW', 2)
3077 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3078 self.waitUntilSettled()
3079
3080 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3081 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3082 'wheezy')
3083 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
3084
James E. Blair287c06d2013-07-24 10:39:30 -07003085 def test_delayed_repo_init(self):
3086 self.config.set('zuul', 'layout_config',
3087 'tests/fixtures/layout-delayed-repo-init.yaml')
3088 self.sched.reconfigure(self.config)
3089
3090 self.init_repo("org/new-project")
3091 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
3092
3093 A.addApproval('CRVW', 2)
3094 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3095 self.waitUntilSettled()
3096 self.assertEqual(self.getJobFromHistory('project-merge').result,
3097 'SUCCESS')
3098 self.assertEqual(self.getJobFromHistory('project-test1').result,
3099 'SUCCESS')
3100 self.assertEqual(self.getJobFromHistory('project-test2').result,
3101 'SUCCESS')
3102 self.assertEqual(A.data['status'], 'MERGED')
3103 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003104
Clark Boylan6dbbc482013-10-18 10:57:31 -07003105 def test_repo_deleted(self):
3106 self.config.set('zuul', 'layout_config',
3107 'tests/fixtures/layout-repo-deleted.yaml')
3108 self.sched.reconfigure(self.config)
3109
3110 self.init_repo("org/delete-project")
3111 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
3112
3113 A.addApproval('CRVW', 2)
3114 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3115 self.waitUntilSettled()
3116 self.assertEqual(self.getJobFromHistory('project-merge').result,
3117 'SUCCESS')
3118 self.assertEqual(self.getJobFromHistory('project-test1').result,
3119 'SUCCESS')
3120 self.assertEqual(self.getJobFromHistory('project-test2').result,
3121 'SUCCESS')
3122 self.assertEqual(A.data['status'], 'MERGED')
3123 self.assertEqual(A.reported, 2)
3124
3125 # Delete org/new-project zuul repo. Should be recloned.
3126 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
3127
3128 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
3129
3130 B.addApproval('CRVW', 2)
3131 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3132 self.waitUntilSettled()
3133 self.assertEqual(self.getJobFromHistory('project-merge').result,
3134 'SUCCESS')
3135 self.assertEqual(self.getJobFromHistory('project-test1').result,
3136 'SUCCESS')
3137 self.assertEqual(self.getJobFromHistory('project-test2').result,
3138 'SUCCESS')
3139 self.assertEqual(B.data['status'], 'MERGED')
3140 self.assertEqual(B.reported, 2)
3141
James E. Blair63bb0ef2013-07-29 17:14:51 -07003142 def test_timer(self):
3143 "Test that a periodic job is triggered"
3144 self.worker.hold_jobs_in_build = True
3145 self.config.set('zuul', 'layout_config',
3146 'tests/fixtures/layout-timer.yaml')
3147 self.sched.reconfigure(self.config)
3148 self.registerJobs()
3149
3150 start = time.time()
3151 failed = True
3152 while ((time.time() - start) < 30):
3153 if len(self.builds) == 2:
3154 failed = False
3155 break
3156 else:
3157 time.sleep(1)
3158
3159 if failed:
3160 raise Exception("Expected jobs never ran")
3161
3162 self.waitUntilSettled()
3163 port = self.webapp.server.socket.getsockname()[1]
3164
3165 f = urllib.urlopen("http://localhost:%s/status.json" % port)
3166 data = f.read()
3167
3168 self.worker.hold_jobs_in_build = False
3169 self.worker.release()
3170 self.waitUntilSettled()
3171
3172 self.assertEqual(self.getJobFromHistory(
3173 'project-bitrot-stable-old').result, 'SUCCESS')
3174 self.assertEqual(self.getJobFromHistory(
3175 'project-bitrot-stable-older').result, 'SUCCESS')
3176
3177 data = json.loads(data)
3178 status_jobs = set()
3179 for p in data['pipelines']:
3180 for q in p['change_queues']:
3181 for head in q['heads']:
3182 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07003183 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003184 for job in change['jobs']:
3185 status_jobs.add(job['name'])
3186 self.assertIn('project-bitrot-stable-old', status_jobs)
3187 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003188
3189 def test_check_smtp_pool(self):
3190 self.config.set('zuul', 'layout_config',
3191 'tests/fixtures/layout-smtp.yaml')
3192 self.sched.reconfigure(self.config)
3193
3194 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3195 self.waitUntilSettled()
3196
3197 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3198 self.waitUntilSettled()
3199
James E. Blairff80a2f2013-12-27 13:24:06 -08003200 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003201
3202 # A.messages only holds what FakeGerrit places in it. Thus we
3203 # work on the knowledge of what the first message should be as
3204 # it is only configured to go to SMTP.
3205
3206 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003207 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003208 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003209 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003210 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003211 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003212
3213 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003214 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003215 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003216 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003217 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003218 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003219
James E. Blaire5910202013-12-27 09:50:31 -08003220 def test_timer_smtp(self):
3221 "Test that a periodic job is triggered"
3222 self.config.set('zuul', 'layout_config',
3223 'tests/fixtures/layout-timer-smtp.yaml')
3224 self.sched.reconfigure(self.config)
3225 self.registerJobs()
3226
3227 start = time.time()
3228 failed = True
3229 while ((time.time() - start) < 30):
3230 if len(self.history) == 2:
3231 failed = False
3232 break
3233 else:
3234 time.sleep(1)
3235
3236 if failed:
3237 raise Exception("Expected jobs never ran")
3238
3239 self.waitUntilSettled()
3240
3241 self.assertEqual(self.getJobFromHistory(
3242 'project-bitrot-stable-old').result, 'SUCCESS')
3243 self.assertEqual(self.getJobFromHistory(
3244 'project-bitrot-stable-older').result, 'SUCCESS')
3245
James E. Blairff80a2f2013-12-27 13:24:06 -08003246 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003247
3248 # A.messages only holds what FakeGerrit places in it. Thus we
3249 # work on the knowledge of what the first message should be as
3250 # it is only configured to go to SMTP.
3251
3252 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003253 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003254 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003255 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003256 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003257 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003258
James E. Blairad28e912013-11-27 10:43:22 -08003259 def test_client_enqueue(self):
3260 "Test that the RPC client can enqueue a change"
3261 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3262 A.addApproval('CRVW', 2)
3263 A.addApproval('APRV', 1)
3264
3265 client = zuul.rpcclient.RPCClient('127.0.0.1',
3266 self.gearman_server.port)
3267 r = client.enqueue(pipeline='gate',
3268 project='org/project',
3269 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003270 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003271 self.waitUntilSettled()
3272 self.assertEqual(self.getJobFromHistory('project-merge').result,
3273 'SUCCESS')
3274 self.assertEqual(self.getJobFromHistory('project-test1').result,
3275 'SUCCESS')
3276 self.assertEqual(self.getJobFromHistory('project-test2').result,
3277 'SUCCESS')
3278 self.assertEqual(A.data['status'], 'MERGED')
3279 self.assertEqual(A.reported, 2)
3280 self.assertEqual(r, True)
3281
3282 def test_client_enqueue_negative(self):
3283 "Test that the RPC client returns errors"
3284 client = zuul.rpcclient.RPCClient('127.0.0.1',
3285 self.gearman_server.port)
3286 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3287 "Invalid project"):
3288 r = client.enqueue(pipeline='gate',
3289 project='project-does-not-exist',
3290 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003291 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003292 client.shutdown()
3293 self.assertEqual(r, False)
3294
3295 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3296 "Invalid pipeline"):
3297 r = client.enqueue(pipeline='pipeline-does-not-exist',
3298 project='org/project',
3299 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003300 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003301 client.shutdown()
3302 self.assertEqual(r, False)
3303
3304 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3305 "Invalid trigger"):
3306 r = client.enqueue(pipeline='gate',
3307 project='org/project',
3308 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003309 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003310 client.shutdown()
3311 self.assertEqual(r, False)
3312
3313 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3314 "Invalid change"):
3315 r = client.enqueue(pipeline='gate',
3316 project='org/project',
3317 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003318 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003319 client.shutdown()
3320 self.assertEqual(r, False)
3321
3322 self.waitUntilSettled()
3323 self.assertEqual(len(self.history), 0)
3324 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003325
3326 def test_client_promote(self):
3327 "Test that the RPC client can promote a change"
3328 self.worker.hold_jobs_in_build = True
3329 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3330 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3331 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3332 A.addApproval('CRVW', 2)
3333 B.addApproval('CRVW', 2)
3334 C.addApproval('CRVW', 2)
3335
3336 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3337 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3338 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3339
3340 self.waitUntilSettled()
3341
Sean Daguef39b9ca2014-01-10 21:34:35 -05003342 items = self.sched.layout.pipelines['gate'].getAllItems()
3343 enqueue_times = {}
3344 for item in items:
3345 enqueue_times[str(item.change)] = item.enqueue_time
3346
James E. Blair36658cf2013-12-06 17:53:48 -08003347 client = zuul.rpcclient.RPCClient('127.0.0.1',
3348 self.gearman_server.port)
3349 r = client.promote(pipeline='gate',
3350 change_ids=['2,1', '3,1'])
3351
Sean Daguef39b9ca2014-01-10 21:34:35 -05003352 # ensure that enqueue times are durable
3353 items = self.sched.layout.pipelines['gate'].getAllItems()
3354 for item in items:
3355 self.assertEqual(
3356 enqueue_times[str(item.change)], item.enqueue_time)
3357
James E. Blair78acec92014-02-06 07:11:32 -08003358 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003359 self.worker.release('.*-merge')
3360 self.waitUntilSettled()
3361 self.worker.release('.*-merge')
3362 self.waitUntilSettled()
3363 self.worker.release('.*-merge')
3364 self.waitUntilSettled()
3365
3366 self.assertEqual(len(self.builds), 6)
3367 self.assertEqual(self.builds[0].name, 'project-test1')
3368 self.assertEqual(self.builds[1].name, 'project-test2')
3369 self.assertEqual(self.builds[2].name, 'project-test1')
3370 self.assertEqual(self.builds[3].name, 'project-test2')
3371 self.assertEqual(self.builds[4].name, 'project-test1')
3372 self.assertEqual(self.builds[5].name, 'project-test2')
3373
3374 self.assertTrue(self.job_has_changes(self.builds[0], B))
3375 self.assertFalse(self.job_has_changes(self.builds[0], A))
3376 self.assertFalse(self.job_has_changes(self.builds[0], C))
3377
3378 self.assertTrue(self.job_has_changes(self.builds[2], B))
3379 self.assertTrue(self.job_has_changes(self.builds[2], C))
3380 self.assertFalse(self.job_has_changes(self.builds[2], A))
3381
3382 self.assertTrue(self.job_has_changes(self.builds[4], B))
3383 self.assertTrue(self.job_has_changes(self.builds[4], C))
3384 self.assertTrue(self.job_has_changes(self.builds[4], A))
3385
3386 self.worker.release()
3387 self.waitUntilSettled()
3388
3389 self.assertEqual(A.data['status'], 'MERGED')
3390 self.assertEqual(A.reported, 2)
3391 self.assertEqual(B.data['status'], 'MERGED')
3392 self.assertEqual(B.reported, 2)
3393 self.assertEqual(C.data['status'], 'MERGED')
3394 self.assertEqual(C.reported, 2)
3395
3396 client.shutdown()
3397 self.assertEqual(r, True)
3398
3399 def test_client_promote_dependent(self):
3400 "Test that the RPC client can promote a dependent change"
3401 # C (depends on B) -> B -> A ; then promote C to get:
3402 # A -> C (depends on B) -> B
3403 self.worker.hold_jobs_in_build = True
3404 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3405 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3406 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3407
3408 C.setDependsOn(B, 1)
3409
3410 A.addApproval('CRVW', 2)
3411 B.addApproval('CRVW', 2)
3412 C.addApproval('CRVW', 2)
3413
3414 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3415 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3416 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3417
3418 self.waitUntilSettled()
3419
3420 client = zuul.rpcclient.RPCClient('127.0.0.1',
3421 self.gearman_server.port)
3422 r = client.promote(pipeline='gate',
3423 change_ids=['3,1'])
3424
James E. Blair78acec92014-02-06 07:11:32 -08003425 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003426 self.worker.release('.*-merge')
3427 self.waitUntilSettled()
3428 self.worker.release('.*-merge')
3429 self.waitUntilSettled()
3430 self.worker.release('.*-merge')
3431 self.waitUntilSettled()
3432
3433 self.assertEqual(len(self.builds), 6)
3434 self.assertEqual(self.builds[0].name, 'project-test1')
3435 self.assertEqual(self.builds[1].name, 'project-test2')
3436 self.assertEqual(self.builds[2].name, 'project-test1')
3437 self.assertEqual(self.builds[3].name, 'project-test2')
3438 self.assertEqual(self.builds[4].name, 'project-test1')
3439 self.assertEqual(self.builds[5].name, 'project-test2')
3440
3441 self.assertTrue(self.job_has_changes(self.builds[0], B))
3442 self.assertFalse(self.job_has_changes(self.builds[0], A))
3443 self.assertFalse(self.job_has_changes(self.builds[0], C))
3444
3445 self.assertTrue(self.job_has_changes(self.builds[2], B))
3446 self.assertTrue(self.job_has_changes(self.builds[2], C))
3447 self.assertFalse(self.job_has_changes(self.builds[2], A))
3448
3449 self.assertTrue(self.job_has_changes(self.builds[4], B))
3450 self.assertTrue(self.job_has_changes(self.builds[4], C))
3451 self.assertTrue(self.job_has_changes(self.builds[4], A))
3452
3453 self.worker.release()
3454 self.waitUntilSettled()
3455
3456 self.assertEqual(A.data['status'], 'MERGED')
3457 self.assertEqual(A.reported, 2)
3458 self.assertEqual(B.data['status'], 'MERGED')
3459 self.assertEqual(B.reported, 2)
3460 self.assertEqual(C.data['status'], 'MERGED')
3461 self.assertEqual(C.reported, 2)
3462
3463 client.shutdown()
3464 self.assertEqual(r, True)
3465
3466 def test_client_promote_negative(self):
3467 "Test that the RPC client returns errors for promotion"
3468 self.worker.hold_jobs_in_build = True
3469 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3470 A.addApproval('CRVW', 2)
3471 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3472 self.waitUntilSettled()
3473
3474 client = zuul.rpcclient.RPCClient('127.0.0.1',
3475 self.gearman_server.port)
3476
3477 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3478 r = client.promote(pipeline='nonexistent',
3479 change_ids=['2,1', '3,1'])
3480 client.shutdown()
3481 self.assertEqual(r, False)
3482
3483 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3484 r = client.promote(pipeline='gate',
3485 change_ids=['4,1'])
3486 client.shutdown()
3487 self.assertEqual(r, False)
3488
3489 self.worker.hold_jobs_in_build = False
3490 self.worker.release()
3491 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003492
3493 def test_queue_rate_limiting(self):
3494 "Test that DependentPipelines are rate limited with dep across window"
3495 self.config.set('zuul', 'layout_config',
3496 'tests/fixtures/layout-rate-limit.yaml')
3497 self.sched.reconfigure(self.config)
3498 self.worker.hold_jobs_in_build = True
3499 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3500 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3501 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3502
3503 C.setDependsOn(B, 1)
3504 self.worker.addFailTest('project-test1', A)
3505
3506 A.addApproval('CRVW', 2)
3507 B.addApproval('CRVW', 2)
3508 C.addApproval('CRVW', 2)
3509
3510 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3511 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3512 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3513 self.waitUntilSettled()
3514
3515 # Only A and B will have their merge jobs queued because
3516 # window is 2.
3517 self.assertEqual(len(self.builds), 2)
3518 self.assertEqual(self.builds[0].name, 'project-merge')
3519 self.assertEqual(self.builds[1].name, 'project-merge')
3520
3521 self.worker.release('.*-merge')
3522 self.waitUntilSettled()
3523 self.worker.release('.*-merge')
3524 self.waitUntilSettled()
3525
3526 # Only A and B will have their test jobs queued because
3527 # window is 2.
3528 self.assertEqual(len(self.builds), 4)
3529 self.assertEqual(self.builds[0].name, 'project-test1')
3530 self.assertEqual(self.builds[1].name, 'project-test2')
3531 self.assertEqual(self.builds[2].name, 'project-test1')
3532 self.assertEqual(self.builds[3].name, 'project-test2')
3533
3534 self.worker.release('project-.*')
3535 self.waitUntilSettled()
3536
3537 queue = self.sched.layout.pipelines['gate'].queues[0]
3538 # A failed so window is reduced by 1 to 1.
3539 self.assertEqual(queue.window, 1)
3540 self.assertEqual(queue.window_floor, 1)
3541 self.assertEqual(A.data['status'], 'NEW')
3542
3543 # Gate is reset and only B's merge job is queued because
3544 # window shrunk to 1.
3545 self.assertEqual(len(self.builds), 1)
3546 self.assertEqual(self.builds[0].name, 'project-merge')
3547
3548 self.worker.release('.*-merge')
3549 self.waitUntilSettled()
3550
3551 # Only B's test jobs are queued because window is still 1.
3552 self.assertEqual(len(self.builds), 2)
3553 self.assertEqual(self.builds[0].name, 'project-test1')
3554 self.assertEqual(self.builds[1].name, 'project-test2')
3555
3556 self.worker.release('project-.*')
3557 self.waitUntilSettled()
3558
3559 # B was successfully merged so window is increased to 2.
3560 self.assertEqual(queue.window, 2)
3561 self.assertEqual(queue.window_floor, 1)
3562 self.assertEqual(B.data['status'], 'MERGED')
3563
3564 # Only C is left and its merge job is queued.
3565 self.assertEqual(len(self.builds), 1)
3566 self.assertEqual(self.builds[0].name, 'project-merge')
3567
3568 self.worker.release('.*-merge')
3569 self.waitUntilSettled()
3570
3571 # After successful merge job the test jobs for C are queued.
3572 self.assertEqual(len(self.builds), 2)
3573 self.assertEqual(self.builds[0].name, 'project-test1')
3574 self.assertEqual(self.builds[1].name, 'project-test2')
3575
3576 self.worker.release('project-.*')
3577 self.waitUntilSettled()
3578
3579 # C successfully merged so window is bumped to 3.
3580 self.assertEqual(queue.window, 3)
3581 self.assertEqual(queue.window_floor, 1)
3582 self.assertEqual(C.data['status'], 'MERGED')
3583
3584 def test_queue_rate_limiting_dependent(self):
3585 "Test that DependentPipelines are rate limited with dep in window"
3586 self.config.set('zuul', 'layout_config',
3587 'tests/fixtures/layout-rate-limit.yaml')
3588 self.sched.reconfigure(self.config)
3589 self.worker.hold_jobs_in_build = True
3590 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3591 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3592 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3593
3594 B.setDependsOn(A, 1)
3595
3596 self.worker.addFailTest('project-test1', A)
3597
3598 A.addApproval('CRVW', 2)
3599 B.addApproval('CRVW', 2)
3600 C.addApproval('CRVW', 2)
3601
3602 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3603 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3604 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3605 self.waitUntilSettled()
3606
3607 # Only A and B will have their merge jobs queued because
3608 # window is 2.
3609 self.assertEqual(len(self.builds), 2)
3610 self.assertEqual(self.builds[0].name, 'project-merge')
3611 self.assertEqual(self.builds[1].name, 'project-merge')
3612
3613 self.worker.release('.*-merge')
3614 self.waitUntilSettled()
3615 self.worker.release('.*-merge')
3616 self.waitUntilSettled()
3617
3618 # Only A and B will have their test jobs queued because
3619 # window is 2.
3620 self.assertEqual(len(self.builds), 4)
3621 self.assertEqual(self.builds[0].name, 'project-test1')
3622 self.assertEqual(self.builds[1].name, 'project-test2')
3623 self.assertEqual(self.builds[2].name, 'project-test1')
3624 self.assertEqual(self.builds[3].name, 'project-test2')
3625
3626 self.worker.release('project-.*')
3627 self.waitUntilSettled()
3628
3629 queue = self.sched.layout.pipelines['gate'].queues[0]
3630 # A failed so window is reduced by 1 to 1.
3631 self.assertEqual(queue.window, 1)
3632 self.assertEqual(queue.window_floor, 1)
3633 self.assertEqual(A.data['status'], 'NEW')
3634 self.assertEqual(B.data['status'], 'NEW')
3635
3636 # Gate is reset and only C's merge job is queued because
3637 # window shrunk to 1 and A and B were dequeued.
3638 self.assertEqual(len(self.builds), 1)
3639 self.assertEqual(self.builds[0].name, 'project-merge')
3640
3641 self.worker.release('.*-merge')
3642 self.waitUntilSettled()
3643
3644 # Only C's test jobs are queued because window is still 1.
3645 self.assertEqual(len(self.builds), 2)
3646 self.assertEqual(self.builds[0].name, 'project-test1')
3647 self.assertEqual(self.builds[1].name, 'project-test2')
3648
3649 self.worker.release('project-.*')
3650 self.waitUntilSettled()
3651
3652 # C was successfully merged so window is increased to 2.
3653 self.assertEqual(queue.window, 2)
3654 self.assertEqual(queue.window_floor, 1)
3655 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003656
3657 def test_worker_update_metadata(self):
3658 "Test if a worker can send back metadata about itself"
3659 self.worker.hold_jobs_in_build = True
3660
3661 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3662 A.addApproval('CRVW', 2)
3663 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3664 self.waitUntilSettled()
3665
3666 self.assertEqual(len(self.launcher.builds), 1)
3667
3668 self.log.debug('Current builds:')
3669 self.log.debug(self.launcher.builds)
3670
3671 start = time.time()
3672 while True:
3673 if time.time() - start > 10:
3674 raise Exception("Timeout waiting for gearman server to report "
3675 + "back to the client")
3676 build = self.launcher.builds.values()[0]
3677 if build.worker.name == "My Worker":
3678 break
3679 else:
3680 time.sleep(0)
3681
3682 self.log.debug(build)
3683 self.assertEqual("My Worker", build.worker.name)
3684 self.assertEqual("localhost", build.worker.hostname)
3685 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3686 self.assertEqual("zuul.example.org", build.worker.fqdn)
3687 self.assertEqual("FakeBuilder", build.worker.program)
3688 self.assertEqual("v1.1", build.worker.version)
3689 self.assertEqual({'something': 'else'}, build.worker.extra)
3690
3691 self.worker.hold_jobs_in_build = False
3692 self.worker.release()
3693 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003694
3695 def test_footer_message(self):
3696 "Test a pipeline's footer message is correctly added to the report."
3697 self.config.set('zuul', 'layout_config',
3698 'tests/fixtures/layout-footer-message.yaml')
3699 self.sched.reconfigure(self.config)
3700 self.registerJobs()
3701
3702 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3703 A.addApproval('CRVW', 2)
3704 self.worker.addFailTest('test1', A)
3705 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3706 self.waitUntilSettled()
3707
3708 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3709 B.addApproval('CRVW', 2)
3710 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3711 self.waitUntilSettled()
3712
3713 self.assertEqual(2, len(self.smtp_messages))
3714
3715 failure_body = """\
3716Build failed. For information on how to proceed, see \
3717http://wiki.example.org/Test_Failures
3718
3719- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3720- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3721
3722For CI problems and help debugging, contact ci@example.org"""
3723
3724 success_body = """\
3725Build succeeded.
3726
3727- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3728- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3729
3730For CI problems and help debugging, contact ci@example.org"""
3731
3732 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3733 self.assertEqual(success_body, self.smtp_messages[1]['body'])