blob: d9f9afec20b4ddaf263c6dffe96ee864a666a4fb [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
Yuriy Taradaya6d452f2014-04-16 12:36:20 +040020import gzip
James E. Blair8cc15a82012-08-01 11:17:57 -070021import hashlib
James E. Blairb0fcae42012-07-17 11:12:10 -070022import json
Monty Taylorbc758832013-06-17 17:22:42 -040023import logging
24import os
James E. Blairb0fcae42012-07-17 11:12:10 -070025import pprint
Monty Taylorbc758832013-06-17 17:22:42 -040026import Queue
27import random
James E. Blairb0fcae42012-07-17 11:12:10 -070028import re
James E. Blair412e5582013-04-22 15:50:12 -070029import select
James E. Blair4886cc12012-07-18 15:39:41 -070030import shutil
James E. Blair412e5582013-04-22 15:50:12 -070031import socket
James E. Blair4886f282012-11-15 09:27:33 -080032import string
Monty Taylorbc758832013-06-17 17:22:42 -040033import subprocess
Joshua Hesketh36c3fa52014-01-22 11:40:52 +110034import swiftclient
Monty Taylorbc758832013-06-17 17:22:42 -040035import threading
36import time
James E. Blair1843a552013-07-03 14:19:52 -070037import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040038import urllib2
Monty Taylorbc758832013-06-17 17:22:42 -040039
James E. Blair4886cc12012-07-18 15:39:41 -070040import git
James E. Blair1f4c2bb2013-04-26 08:40:46 -070041import gear
Monty Taylorbc758832013-06-17 17:22:42 -040042import fixtures
tanline621a412014-02-12 11:16:36 +080043import six.moves.urllib.parse as urlparse
Monty Taylorbc758832013-06-17 17:22:42 -040044import statsd
45import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070046
James E. Blairb0fcae42012-07-17 11:12:10 -070047import zuul.scheduler
James E. Blair1843a552013-07-03 14:19:52 -070048import zuul.webapp
James E. Blairad28e912013-11-27 10:43:22 -080049import zuul.rpclistener
50import zuul.rpcclient
James E. Blair1f4c2bb2013-04-26 08:40:46 -070051import zuul.launcher.gearman
Joshua Hesketh36c3fa52014-01-22 11:40:52 +110052import zuul.lib.swift
James E. Blair4076e2b2014-01-28 12:42:20 -080053import zuul.merger.server
54import zuul.merger.client
Joshua Hesketh1879cf72013-08-19 14:13:15 +100055import zuul.reporter.gerrit
Joshua Hesketh5fea8672013-08-19 17:32:01 +100056import zuul.reporter.smtp
James E. Blairb0fcae42012-07-17 11:12:10 -070057import zuul.trigger.gerrit
James E. Blair63bb0ef2013-07-29 17:14:51 -070058import zuul.trigger.timer
James E. Blairb0fcae42012-07-17 11:12:10 -070059
60FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
61 'fixtures')
62CONFIG = ConfigParser.ConfigParser()
63CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
64
65CONFIG.set('zuul', 'layout_config',
66 os.path.join(FIXTURE_DIR, "layout.yaml"))
67
James E. Blair1f4c2bb2013-04-26 08:40:46 -070068logging.basicConfig(level=logging.DEBUG,
69 format='%(asctime)s %(name)-32s '
70 '%(levelname)-8s %(message)s')
James E. Blairb0fcae42012-07-17 11:12:10 -070071
72
Monty Taylorbc758832013-06-17 17:22:42 -040073def repack_repo(path):
James E. Blairac2c3242014-01-24 13:38:51 -080074 cmd = ['git', '--git-dir=%s/.git' % path, 'repack', '-afd']
75 output = subprocess.Popen(cmd, close_fds=True,
76 stdout=subprocess.PIPE,
77 stderr=subprocess.PIPE)
Monty Taylorbc758832013-06-17 17:22:42 -040078 out = output.communicate()
79 if output.returncode:
80 raise Exception("git repack returned %d" % output.returncode)
81 return out
82
83
James E. Blair8cc15a82012-08-01 11:17:57 -070084def random_sha1():
85 return hashlib.sha1(str(random.random())).hexdigest()
86
87
James E. Blair4886cc12012-07-18 15:39:41 -070088class ChangeReference(git.Reference):
89 _common_path_default = "refs/changes"
90 _points_to_commits_only = True
91
92
James E. Blairb0fcae42012-07-17 11:12:10 -070093class FakeChange(object):
James E. Blair8c803f82012-07-31 16:25:42 -070094 categories = {'APRV': ('Approved', -1, 1),
95 'CRVW': ('Code-Review', -2, 2),
96 'VRFY': ('Verified', -2, 2)}
James E. Blairb0fcae42012-07-17 11:12:10 -070097
Monty Taylorbc758832013-06-17 17:22:42 -040098 def __init__(self, gerrit, number, project, branch, subject,
99 status='NEW', upstream_root=None):
James E. Blair8cc15a82012-08-01 11:17:57 -0700100 self.gerrit = gerrit
James E. Blaird466dc42012-07-31 10:42:56 -0700101 self.reported = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700102 self.queried = 0
James E. Blairb0fcae42012-07-17 11:12:10 -0700103 self.patchsets = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700104 self.number = number
105 self.project = project
106 self.branch = branch
107 self.subject = subject
108 self.latest_patchset = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700109 self.depends_on_change = None
110 self.needed_by_changes = []
James E. Blair127bc182012-08-28 15:55:15 -0700111 self.fail_merge = False
James E. Blair42f74822013-05-14 15:18:03 -0700112 self.messages = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700113 self.data = {
114 'branch': branch,
115 'comments': [],
116 'commitMessage': subject,
117 'createdOn': time.time(),
James E. Blair8cc15a82012-08-01 11:17:57 -0700118 'id': 'I' + random_sha1(),
James E. Blairb0fcae42012-07-17 11:12:10 -0700119 'lastUpdated': time.time(),
120 'number': str(number),
James E. Blair11041d22014-05-02 14:49:53 -0700121 'open': status == 'NEW',
James E. Blairb0fcae42012-07-17 11:12:10 -0700122 'owner': {'email': 'user@example.com',
123 'name': 'User Name',
124 'username': 'username'},
125 'patchSets': self.patchsets,
126 'project': project,
127 'status': status,
128 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700129 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700130 'url': 'https://hostname/%s' % number}
131
Monty Taylorbc758832013-06-17 17:22:42 -0400132 self.upstream_root = upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700133 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700134 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700135
Monty Taylorbc758832013-06-17 17:22:42 -0400136 def add_fake_change_to_repo(self, msg, fn, large):
137 path = os.path.join(self.upstream_root, self.project)
138 repo = git.Repo(path)
139 ref = ChangeReference.create(repo, '1/%s/%s' % (self.number,
140 self.latest_patchset),
141 'refs/tags/init')
142 repo.head.reference = ref
143 repo.head.reset(index=True, working_tree=True)
144 repo.git.clean('-x', '-f', '-d')
145
146 path = os.path.join(self.upstream_root, self.project)
147 if not large:
148 fn = os.path.join(path, fn)
149 f = open(fn, 'w')
150 f.write("test %s %s %s\n" %
151 (self.branch, self.number, self.latest_patchset))
152 f.close()
153 repo.index.add([fn])
154 else:
155 for fni in range(100):
156 fn = os.path.join(path, str(fni))
157 f = open(fn, 'w')
158 for ci in range(4096):
159 f.write(random.choice(string.printable))
160 f.close()
161 repo.index.add([fn])
162
James E. Blair287c06d2013-07-24 10:39:30 -0700163 r = repo.index.commit(msg)
164 repo.head.reference = 'master'
165 repo.head.reset(index=True, working_tree=True)
166 repo.git.clean('-x', '-f', '-d')
James E. Blairac2c3242014-01-24 13:38:51 -0800167 repo.heads['master'].checkout()
James E. Blair287c06d2013-07-24 10:39:30 -0700168 return r
Monty Taylorbc758832013-06-17 17:22:42 -0400169
James E. Blair70c71582013-03-06 08:50:50 -0800170 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700171 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700172 if files:
173 fn = files[0]
174 else:
175 fn = '%s-%s' % (self.branch, self.number)
176 msg = self.subject + '-' + str(self.latest_patchset)
Monty Taylorbc758832013-06-17 17:22:42 -0400177 c = self.add_fake_change_to_repo(msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800178 ps_files = [{'file': '/COMMIT_MSG',
179 'type': 'ADDED'},
180 {'file': 'README',
181 'type': 'MODIFIED'}]
182 for f in files:
183 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700184 d = {'approvals': [],
185 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800186 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700187 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700188 'ref': 'refs/changes/1/%s/%s' % (self.number,
189 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700190 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700191 'uploader': {'email': 'user@example.com',
192 'name': 'User name',
193 'username': 'user'}}
194 self.data['currentPatchSet'] = d
195 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700196 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700197
James E. Blaire0487072012-08-29 17:38:31 -0700198 def getPatchsetCreatedEvent(self, patchset):
199 event = {"type": "patchset-created",
200 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800201 "branch": self.branch,
202 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
203 "number": str(self.number),
204 "subject": self.subject,
205 "owner": {"name": "User Name"},
206 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700207 "patchSet": self.patchsets[patchset - 1],
208 "uploader": {"name": "User Name"}}
209 return event
210
James E. Blair42f74822013-05-14 15:18:03 -0700211 def getChangeRestoredEvent(self):
212 event = {"type": "change-restored",
213 "change": {"project": self.project,
214 "branch": self.branch,
215 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
216 "number": str(self.number),
217 "subject": self.subject,
218 "owner": {"name": "User Name"},
219 "url": "https://hostname/3"},
220 "restorer": {"name": "User Name"},
221 "reason": ""}
222 return event
223
James E. Blairc053d022014-01-22 14:57:33 -0800224 def addApproval(self, category, value, username='jenkins',
225 granted_on=None):
226 if not granted_on:
227 granted_on = time.time()
James E. Blair8c803f82012-07-31 16:25:42 -0700228 approval = {'description': self.categories[category][0],
229 'type': category,
James E. Blairc053d022014-01-22 14:57:33 -0800230 'value': str(value),
James E. Blair64ff4ef2014-01-24 13:50:23 -0800231 'by': {
232 'username': username,
233 'email': username + '@example.com',
234 },
James E. Blairc053d022014-01-22 14:57:33 -0800235 'grantedOn': int(granted_on)}
236 for i, x in enumerate(self.patchsets[-1]['approvals'][:]):
James E. Blair64ff4ef2014-01-24 13:50:23 -0800237 if x['by']['username'] == username and x['type'] == category:
James E. Blairc053d022014-01-22 14:57:33 -0800238 del self.patchsets[-1]['approvals'][i]
James E. Blair8c803f82012-07-31 16:25:42 -0700239 self.patchsets[-1]['approvals'].append(approval)
240 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700241 'author': {'email': 'user@example.com',
242 'name': 'User Name',
243 'username': 'username'},
244 'change': {'branch': self.branch,
245 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
246 'number': str(self.number),
247 'owner': {'email': 'user@example.com',
248 'name': 'User Name',
249 'username': 'username'},
250 'project': self.project,
251 'subject': self.subject,
252 'topic': 'master',
253 'url': 'https://hostname/459'},
254 'comment': '',
255 'patchSet': self.patchsets[-1],
256 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700257 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700258 return json.loads(json.dumps(event))
259
James E. Blair8c803f82012-07-31 16:25:42 -0700260 def getSubmitRecords(self):
261 status = {}
262 for cat in self.categories.keys():
263 status[cat] = 0
264
265 for a in self.patchsets[-1]['approvals']:
266 cur = status[a['type']]
267 cat_min, cat_max = self.categories[a['type']][1:]
268 new = int(a['value'])
269 if new == cat_min:
270 cur = new
271 elif abs(new) > abs(cur):
272 cur = new
273 status[a['type']] = cur
274
275 labels = []
276 ok = True
277 for typ, cat in self.categories.items():
278 cur = status[typ]
279 cat_min, cat_max = cat[1:]
280 if cur == cat_min:
281 value = 'REJECT'
282 ok = False
283 elif cur == cat_max:
284 value = 'OK'
285 else:
286 value = 'NEED'
287 ok = False
288 labels.append({'label': cat[0], 'status': value})
289 if ok:
290 return [{'status': 'OK'}]
291 return [{'status': 'NOT_READY',
292 'labels': labels}]
293
294 def setDependsOn(self, other, patchset):
295 self.depends_on_change = other
296 d = {'id': other.data['id'],
297 'number': other.data['number'],
298 'ref': other.patchsets[patchset - 1]['ref']
299 }
300 self.data['dependsOn'] = [d]
301
302 other.needed_by_changes.append(self)
303 needed = other.data.get('neededBy', [])
304 d = {'id': self.data['id'],
305 'number': self.data['number'],
306 'ref': self.patchsets[patchset - 1]['ref'],
307 'revision': self.patchsets[patchset - 1]['revision']
308 }
309 needed.append(d)
310 other.data['neededBy'] = needed
311
James E. Blairb0fcae42012-07-17 11:12:10 -0700312 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700313 self.queried += 1
314 d = self.data.get('dependsOn')
315 if d:
316 d = d[0]
317 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
318 d['isCurrentPatchSet'] = True
319 else:
320 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700321 return json.loads(json.dumps(self.data))
322
323 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800324 if (self.depends_on_change and
325 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700326 return
James E. Blair127bc182012-08-28 15:55:15 -0700327 if self.fail_merge:
328 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700329 self.data['status'] = 'MERGED'
330 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700331
Monty Taylorbc758832013-06-17 17:22:42 -0400332 path = os.path.join(self.upstream_root, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700333 repo = git.Repo(path)
334 repo.heads[self.branch].commit = \
335 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700336
James E. Blaird466dc42012-07-31 10:42:56 -0700337 def setReported(self):
338 self.reported += 1
339
James E. Blairb0fcae42012-07-17 11:12:10 -0700340
341class FakeGerrit(object):
342 def __init__(self, *args, **kw):
343 self.event_queue = Queue.Queue()
344 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
345 self.change_number = 0
346 self.changes = {}
347
James E. Blair11041d22014-05-02 14:49:53 -0700348 def addFakeChange(self, project, branch, subject, status='NEW'):
James E. Blairb0fcae42012-07-17 11:12:10 -0700349 self.change_number += 1
Monty Taylorbc758832013-06-17 17:22:42 -0400350 c = FakeChange(self, self.change_number, project, branch, subject,
James E. Blair11041d22014-05-02 14:49:53 -0700351 upstream_root=self.upstream_root,
352 status=status)
James E. Blairb0fcae42012-07-17 11:12:10 -0700353 self.changes[self.change_number] = c
354 return c
355
356 def addEvent(self, data):
357 return self.event_queue.put(data)
358
359 def getEvent(self):
360 return self.event_queue.get()
361
362 def eventDone(self):
363 self.event_queue.task_done()
364
365 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700366 number, ps = changeid.split(',')
367 change = self.changes[int(number)]
James E. Blair42f74822013-05-14 15:18:03 -0700368 change.messages.append(message)
James E. Blairb0fcae42012-07-17 11:12:10 -0700369 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700370 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700371 if message:
372 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700373
374 def query(self, number):
James E. Blairad28e912013-11-27 10:43:22 -0800375 change = self.changes.get(int(number))
376 if change:
377 return change.query()
378 return {}
James E. Blairb0fcae42012-07-17 11:12:10 -0700379
380 def startWatching(self, *args, **kw):
381 pass
382
383
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700384class BuildHistory(object):
385 def __init__(self, **kw):
386 self.__dict__.update(kw)
James E. Blairb0fcae42012-07-17 11:12:10 -0700387
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700388 def __repr__(self):
389 return ("<Completed build, result: %s name: %s #%s changes: %s>" %
390 (self.result, self.name, self.number, self.changes))
James E. Blairb0fcae42012-07-17 11:12:10 -0700391
392
James E. Blair8cc15a82012-08-01 11:17:57 -0700393class FakeURLOpener(object):
Monty Taylorbc758832013-06-17 17:22:42 -0400394 def __init__(self, upstream_root, fake_gerrit, url):
395 self.upstream_root = upstream_root
James E. Blair8cc15a82012-08-01 11:17:57 -0700396 self.fake_gerrit = fake_gerrit
397 self.url = url
398
399 def read(self):
400 res = urlparse.urlparse(self.url)
401 path = res.path
402 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200403 ret = '001e# service=git-upload-pack\n'
404 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
405 'multi_ack thin-pack side-band side-band-64k ofs-delta '
406 'shallow no-progress include-tag multi_ack_detailed no-done\n')
Monty Taylorbc758832013-06-17 17:22:42 -0400407 path = os.path.join(self.upstream_root, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700408 repo = git.Repo(path)
409 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200410 r = ref.object.hexsha + ' ' + ref.path + '\n'
411 ret += '%04x%s' % (len(r) + 4, r)
412 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700413 return ret
414
415
James E. Blair4886cc12012-07-18 15:39:41 -0700416class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000417 name = 'gerrit'
418
Monty Taylorbc758832013-06-17 17:22:42 -0400419 def __init__(self, upstream_root, *args):
420 super(FakeGerritTrigger, self).__init__(*args)
421 self.upstream_root = upstream_root
422
James E. Blair4886cc12012-07-18 15:39:41 -0700423 def getGitUrl(self, project):
Monty Taylorbc758832013-06-17 17:22:42 -0400424 return os.path.join(self.upstream_root, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700425
426
James E. Blair412e5582013-04-22 15:50:12 -0700427class FakeStatsd(threading.Thread):
428 def __init__(self):
429 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400430 self.daemon = True
James E. Blair412e5582013-04-22 15:50:12 -0700431 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
432 self.sock.bind(('', 0))
433 self.port = self.sock.getsockname()[1]
434 self.wake_read, self.wake_write = os.pipe()
435 self.stats = []
436
437 def run(self):
438 while True:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700439 poll = select.poll()
440 poll.register(self.sock, select.POLLIN)
441 poll.register(self.wake_read, select.POLLIN)
442 ret = poll.poll()
443 for (fd, event) in ret:
444 if fd == self.sock.fileno():
James E. Blair412e5582013-04-22 15:50:12 -0700445 data = self.sock.recvfrom(1024)
446 if not data:
447 return
448 self.stats.append(data[0])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700449 if fd == self.wake_read:
James E. Blair412e5582013-04-22 15:50:12 -0700450 return
451
452 def stop(self):
453 os.write(self.wake_write, '1\n')
454
455
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700456class FakeBuild(threading.Thread):
457 log = logging.getLogger("zuul.test")
458
459 def __init__(self, worker, job, number, node):
460 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400461 self.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700462 self.worker = worker
463 self.job = job
464 self.name = job.name.split(':')[1]
465 self.number = number
466 self.node = node
467 self.parameters = json.loads(job.arguments)
468 self.unique = self.parameters['ZUUL_UUID']
469 self.wait_condition = threading.Condition()
470 self.waiting = False
471 self.aborted = False
472 self.created = time.time()
473 self.description = ''
James E. Blair4a28a882013-08-23 15:17:33 -0700474 self.run_error = False
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700475
476 def release(self):
477 self.wait_condition.acquire()
478 self.wait_condition.notify()
479 self.waiting = False
480 self.log.debug("Build %s released" % self.unique)
481 self.wait_condition.release()
482
483 def isWaiting(self):
484 self.wait_condition.acquire()
485 if self.waiting:
486 ret = True
487 else:
488 ret = False
489 self.wait_condition.release()
490 return ret
491
492 def _wait(self):
493 self.wait_condition.acquire()
494 self.waiting = True
495 self.log.debug("Build %s waiting" % self.unique)
496 self.wait_condition.wait()
497 self.wait_condition.release()
498
499 def run(self):
500 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700501 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
502 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700503 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700504 'manager': self.worker.worker_id,
Joshua Heskethba8776a2014-01-12 14:35:40 +0800505 'worker_name': 'My Worker',
506 'worker_hostname': 'localhost',
507 'worker_ips': ['127.0.0.1', '192.168.1.1'],
508 'worker_fqdn': 'zuul.example.org',
509 'worker_program': 'FakeBuilder',
510 'worker_version': 'v1.1',
511 'worker_extra': {'something': 'else'}
Paul Belangerec49b4c2013-07-20 20:32:20 -0400512 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700513
Joshua Heskethba8776a2014-01-12 14:35:40 +0800514 self.log.debug('Running build %s' % self.unique)
515
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700516 self.job.sendWorkData(json.dumps(data))
Joshua Heskethba8776a2014-01-12 14:35:40 +0800517 self.log.debug('Sent WorkData packet with %s' % json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700518 self.job.sendWorkStatus(0, 100)
519
520 if self.worker.hold_jobs_in_build:
Joshua Heskethba8776a2014-01-12 14:35:40 +0800521 self.log.debug('Holding build %s' % self.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700522 self._wait()
523 self.log.debug("Build %s continuing" % self.unique)
524
525 self.worker.lock.acquire()
526
527 result = 'SUCCESS'
528 if (('ZUUL_REF' in self.parameters) and
529 self.worker.shouldFailTest(self.name,
530 self.parameters['ZUUL_REF'])):
531 result = 'FAILURE'
532 if self.aborted:
533 result = 'ABORTED'
534
James E. Blair4a28a882013-08-23 15:17:33 -0700535 if self.run_error:
536 work_fail = True
537 result = 'RUN_ERROR'
538 else:
539 data['result'] = result
540 work_fail = False
541
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700542 changes = None
543 if 'ZUUL_CHANGE_IDS' in self.parameters:
544 changes = self.parameters['ZUUL_CHANGE_IDS']
545
546 self.worker.build_history.append(
547 BuildHistory(name=self.name, number=self.number,
548 result=result, changes=changes, node=self.node,
James E. Blair64ed6f22013-07-10 14:07:23 -0700549 uuid=self.unique, description=self.description,
550 pipeline=self.parameters['ZUUL_PIPELINE'])
Paul Belangerec49b4c2013-07-20 20:32:20 -0400551 )
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700552
James E. Blair4a28a882013-08-23 15:17:33 -0700553 self.job.sendWorkData(json.dumps(data))
554 if work_fail:
555 self.job.sendWorkFail()
556 else:
557 self.job.sendWorkComplete(json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700558 del self.worker.gearman_jobs[self.job.unique]
559 self.worker.running_builds.remove(self)
560 self.worker.lock.release()
561
562
563class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400564 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700565 super(FakeWorker, self).__init__(worker_id)
566 self.gearman_jobs = {}
567 self.build_history = []
568 self.running_builds = []
569 self.build_counter = 0
570 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400571 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700572
573 self.hold_jobs_in_build = False
574 self.lock = threading.Lock()
575 self.__work_thread = threading.Thread(target=self.work)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400576 self.__work_thread.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700577 self.__work_thread.start()
578
579 def handleJob(self, job):
580 parts = job.name.split(":")
581 cmd = parts[0]
582 name = parts[1]
583 if len(parts) > 2:
584 node = parts[2]
585 else:
586 node = None
587 if cmd == 'build':
588 self.handleBuild(job, name, node)
589 elif cmd == 'stop':
590 self.handleStop(job, name)
591 elif cmd == 'set_description':
592 self.handleSetDescription(job, name)
593
594 def handleBuild(self, job, name, node):
595 build = FakeBuild(self, job, self.build_counter, node)
596 job.build = build
597 self.gearman_jobs[job.unique] = job
598 self.build_counter += 1
599
600 self.running_builds.append(build)
601 build.start()
602
603 def handleStop(self, job, name):
604 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700605 parameters = json.loads(job.arguments)
606 name = parameters['name']
607 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700608 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700609 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700610 build.aborted = True
611 build.release()
612 job.sendWorkComplete()
613 return
614 job.sendWorkFail()
615
616 def handleSetDescription(self, job, name):
617 self.log.debug("handle set description")
618 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700619 name = parameters['name']
620 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700621 descr = parameters['html_description']
622 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700623 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700624 build.description = descr
625 job.sendWorkComplete()
626 return
627 for build in self.build_history:
James E. Blair3c483cf2013-06-04 16:30:43 -0700628 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700629 build.description = descr
630 job.sendWorkComplete()
631 return
632 job.sendWorkFail()
633
634 def work(self):
635 while self.running:
636 try:
637 job = self.getJob()
638 except gear.InterruptedError:
639 continue
640 try:
641 self.handleJob(job)
642 except:
643 self.log.exception("Worker exception:")
644
645 def addFailTest(self, name, change):
646 l = self.fail_tests.get(name, [])
647 l.append(change)
648 self.fail_tests[name] = l
649
650 def shouldFailTest(self, name, ref):
651 l = self.fail_tests.get(name, [])
652 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400653 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700654 return True
655 return False
656
657 def release(self, regex=None):
658 builds = self.running_builds[:]
659 self.log.debug("releasing build %s (%s)" % (regex,
James E. Blair78e31b32013-07-09 09:11:34 -0700660 len(self.running_builds)))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700661 for build in builds:
662 if not regex or re.match(regex, build.name):
663 self.log.debug("releasing build %s" %
664 (build.parameters['ZUUL_UUID']))
665 build.release()
666 else:
667 self.log.debug("not releasing build %s" %
668 (build.parameters['ZUUL_UUID']))
669 self.log.debug("done releasing builds %s (%s)" %
670 (regex, len(self.running_builds)))
671
672
673class FakeGearmanServer(gear.Server):
674 def __init__(self):
675 self.hold_jobs_in_queue = False
676 super(FakeGearmanServer, self).__init__(0)
677
678 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700679 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
680 for job in queue:
681 if not hasattr(job, 'waiting'):
682 if job.name.startswith('build:'):
683 job.waiting = self.hold_jobs_in_queue
684 else:
685 job.waiting = False
686 if job.waiting:
687 continue
688 if job.name in connection.functions:
689 if not peek:
690 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400691 connection.related_jobs[job.handle] = job
692 job.worker_connection = connection
693 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700694 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700695 return None
696
697 def release(self, regex=None):
698 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700699 qlen = (len(self.high_queue) + len(self.normal_queue) +
700 len(self.low_queue))
701 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
James E. Blairdda6c912013-07-29 14:12:12 -0700702 for job in self.getQueue():
703 cmd, name = job.name.split(':')
704 if cmd != 'build':
705 continue
706 if not regex or re.match(regex, name):
707 self.log.debug("releasing queued job %s" %
708 job.unique)
709 job.waiting = False
710 released = True
711 else:
712 self.log.debug("not releasing queued job %s" %
713 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700714 if released:
715 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700716 qlen = (len(self.high_queue) + len(self.normal_queue) +
717 len(self.low_queue))
718 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700719
720
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000721class FakeSMTP(object):
722 log = logging.getLogger('zuul.FakeSMTP')
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000723
James E. Blairff80a2f2013-12-27 13:24:06 -0800724 def __init__(self, messages, server, port):
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000725 self.server = server
726 self.port = port
James E. Blairff80a2f2013-12-27 13:24:06 -0800727 self.messages = messages
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000728
729 def sendmail(self, from_email, to_email, msg):
730 self.log.info("Sending email from %s, to %s, with msg %s" % (
731 from_email, to_email, msg))
732
733 headers = msg.split('\n\n', 1)[0]
734 body = msg.split('\n\n', 1)[1]
735
James E. Blairff80a2f2013-12-27 13:24:06 -0800736 self.messages.append(dict(
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000737 from_email=from_email,
738 to_email=to_email,
739 msg=msg,
740 headers=headers,
741 body=body,
742 ))
743
744 return True
745
746 def quit(self):
747 return True
748
749
Joshua Hesketh36c3fa52014-01-22 11:40:52 +1100750class FakeSwiftClientConnection(swiftclient.client.Connection):
751 def post_account(self, headers):
752 # Do nothing
753 pass
754
755 def get_auth(self):
756 # Returns endpoint and (unused) auth token
757 endpoint = os.path.join('https://storage.example.org', 'V1',
758 'AUTH_account')
759 return endpoint, ''
760
761
Monty Taylorbc758832013-06-17 17:22:42 -0400762class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700763 log = logging.getLogger("zuul.test")
764
765 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400766 super(TestScheduler, self).setUp()
767 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
768 try:
769 test_timeout = int(test_timeout)
770 except ValueError:
771 # If timeout value is invalid do not set a timeout.
772 test_timeout = 0
773 if test_timeout > 0:
James E. Blairff80a2f2013-12-27 13:24:06 -0800774 self.useFixture(fixtures.Timeout(test_timeout, gentle=False))
Monty Taylorbc758832013-06-17 17:22:42 -0400775
776 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
777 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
778 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
779 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
780 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
781 os.environ.get('OS_STDERR_CAPTURE') == '1'):
782 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
783 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Monty Taylore6a6c402013-07-02 09:25:55 -0700784 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
785 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400786 self.useFixture(fixtures.FakeLogger(
787 level=logging.DEBUG,
788 format='%(asctime)s %(name)-32s '
789 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700790 tmp_root = self.useFixture(fixtures.TempDir(
791 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400792 self.test_root = os.path.join(tmp_root, "zuul-test")
793 self.upstream_root = os.path.join(self.test_root, "upstream")
794 self.git_root = os.path.join(self.test_root, "git")
795
James E. Blair4076e2b2014-01-28 12:42:20 -0800796 CONFIG.set('merger', 'git_dir', self.git_root)
Monty Taylorbc758832013-06-17 17:22:42 -0400797 if os.path.exists(self.test_root):
798 shutil.rmtree(self.test_root)
799 os.makedirs(self.test_root)
800 os.makedirs(self.upstream_root)
801 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700802
803 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400804 self.init_repo("org/project")
805 self.init_repo("org/project1")
806 self.init_repo("org/project2")
807 self.init_repo("org/project3")
808 self.init_repo("org/one-job-project")
809 self.init_repo("org/nonvoting-project")
810 self.init_repo("org/templated-project")
James E. Blair3e98c022013-12-16 15:25:38 -0800811 self.init_repo("org/layered-project")
Monty Taylorbc758832013-06-17 17:22:42 -0400812 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700813 self.init_repo("org/conflict-project")
James E. Blair18c64442014-03-18 10:14:45 -0700814 self.init_repo("org/noop-project")
James E. Blairbadc1ad2014-04-28 13:55:14 -0700815 self.init_repo("org/experimental-project")
James E. Blair412e5582013-04-22 15:50:12 -0700816
817 self.statsd = FakeStatsd()
818 os.environ['STATSD_HOST'] = 'localhost'
819 os.environ['STATSD_PORT'] = str(self.statsd.port)
820 self.statsd.start()
821 # the statsd client object is configured in the statsd module import
822 reload(statsd)
823 reload(zuul.scheduler)
824
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700825 self.gearman_server = FakeGearmanServer()
826
827 self.config = ConfigParser.ConfigParser()
828 cfg = StringIO()
829 CONFIG.write(cfg)
830 cfg.seek(0)
831 self.config.readfp(cfg)
832 self.config.set('gearman', 'port', str(self.gearman_server.port))
833
Monty Taylorbc758832013-06-17 17:22:42 -0400834 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700835 self.worker.addServer('127.0.0.1', self.gearman_server.port)
836 self.gearman_server.worker = self.worker
837
James E. Blair4076e2b2014-01-28 12:42:20 -0800838 self.merge_server = zuul.merger.server.MergeServer(self.config)
839 self.merge_server.start()
840
James E. Blairb0fcae42012-07-17 11:12:10 -0700841 self.sched = zuul.scheduler.Scheduler()
842
Joshua Hesketh36c3fa52014-01-22 11:40:52 +1100843 self.useFixture(fixtures.MonkeyPatch('swiftclient.client.Connection',
844 FakeSwiftClientConnection))
845 self.swift = zuul.lib.swift.Swift(self.config)
846
James E. Blair8cc15a82012-08-01 11:17:57 -0700847 def URLOpenerFactory(*args, **kw):
Yuriy Taradaya6d452f2014-04-16 12:36:20 +0400848 if isinstance(args[0], urllib2.Request):
849 return old_urlopen(*args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700850 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400851 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700852
Yuriy Taradaya6d452f2014-04-16 12:36:20 +0400853 old_urlopen = urllib2.urlopen
James E. Blair8cc15a82012-08-01 11:17:57 -0700854 urllib2.urlopen = URLOpenerFactory
Joshua Hesketh36c3fa52014-01-22 11:40:52 +1100855
856 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched,
857 self.swift)
James E. Blair4076e2b2014-01-28 12:42:20 -0800858 self.merge_client = zuul.merger.client.MergeClient(
859 self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700860
James E. Blairff80a2f2013-12-27 13:24:06 -0800861 self.smtp_messages = []
862
863 def FakeSMTPFactory(*args, **kw):
864 args = [self.smtp_messages] + list(args)
865 return FakeSMTP(*args, **kw)
866
James E. Blairb0fcae42012-07-17 11:12:10 -0700867 zuul.lib.gerrit.Gerrit = FakeGerrit
James E. Blairff80a2f2013-12-27 13:24:06 -0800868 self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTPFactory))
James E. Blairb0fcae42012-07-17 11:12:10 -0700869
Monty Taylorbc758832013-06-17 17:22:42 -0400870 self.gerrit = FakeGerritTrigger(
871 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700872 self.gerrit.replication_timeout = 1.5
873 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700874 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400875 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700876
James E. Blair1843a552013-07-03 14:19:52 -0700877 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
James E. Blairad28e912013-11-27 10:43:22 -0800878 self.rpc = zuul.rpclistener.RPCListener(self.config, self.sched)
James E. Blair1843a552013-07-03 14:19:52 -0700879
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700880 self.sched.setLauncher(self.launcher)
James E. Blair4076e2b2014-01-28 12:42:20 -0800881 self.sched.setMerger(self.merge_client)
James E. Blair6c358e72013-07-29 17:06:47 -0700882 self.sched.registerTrigger(self.gerrit)
James E. Blair63bb0ef2013-07-29 17:14:51 -0700883 self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
884 self.sched.registerTrigger(self.timer)
James E. Blairb0fcae42012-07-17 11:12:10 -0700885
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000886 self.sched.registerReporter(
887 zuul.reporter.gerrit.Reporter(self.gerrit))
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000888 self.smtp_reporter = zuul.reporter.smtp.Reporter(
889 self.config.get('smtp', 'default_from'),
890 self.config.get('smtp', 'default_to'),
891 self.config.get('smtp', 'server'))
892 self.sched.registerReporter(self.smtp_reporter)
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000893
James E. Blairb0fcae42012-07-17 11:12:10 -0700894 self.sched.start()
895 self.sched.reconfigure(self.config)
896 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700897 self.webapp.start()
James E. Blairad28e912013-11-27 10:43:22 -0800898 self.rpc.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700899 self.launcher.gearman.waitForServer()
900 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400901 self.builds = self.worker.running_builds
902 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700903
James E. Blairfee8d652013-06-07 08:57:52 -0700904 self.addCleanup(self.assertFinalState)
905 self.addCleanup(self.shutdown)
906
907 def assertFinalState(self):
908 # Make sure that the change cache is cleared
James E. Blair6c358e72013-07-29 17:06:47 -0700909 self.assertEqual(len(self.gerrit._change_cache.keys()), 0)
Clark Boylan4ba48d92013-11-11 18:03:53 -0800910 # Make sure that git.Repo objects have been garbage collected.
911 repos = []
912 gc.collect()
913 for obj in gc.get_objects():
914 if isinstance(obj, git.Repo):
915 repos.append(obj)
916 self.assertEqual(len(repos), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700917 self.assertEmptyQueues()
918
919 def shutdown(self):
920 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700921 self.launcher.stop()
James E. Blair4076e2b2014-01-28 12:42:20 -0800922 self.merge_server.stop()
923 self.merge_server.join()
924 self.merge_client.stop()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700925 self.worker.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700926 self.gerrit.stop()
James E. Blair63bb0ef2013-07-29 17:14:51 -0700927 self.timer.stop()
James E. Blairb0fcae42012-07-17 11:12:10 -0700928 self.sched.stop()
929 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700930 self.statsd.stop()
931 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700932 self.webapp.stop()
933 self.webapp.join()
James E. Blairad28e912013-11-27 10:43:22 -0800934 self.rpc.stop()
935 self.rpc.join()
James E. Blair491b3172014-03-10 15:05:36 -0700936 self.gearman_server.shutdown()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700937 threads = threading.enumerate()
938 if len(threads) > 1:
939 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400940 super(TestScheduler, self).tearDown()
941
942 def init_repo(self, project):
943 parts = project.split('/')
944 path = os.path.join(self.upstream_root, *parts[:-1])
945 if not os.path.exists(path):
946 os.makedirs(path)
947 path = os.path.join(self.upstream_root, project)
948 repo = git.Repo.init(path)
949
950 repo.config_writer().set_value('user', 'email', 'user@example.com')
951 repo.config_writer().set_value('user', 'name', 'User Name')
952 repo.config_writer().write()
953
954 fn = os.path.join(path, 'README')
955 f = open(fn, 'w')
956 f.write("test\n")
957 f.close()
958 repo.index.add([fn])
959 repo.index.commit('initial commit')
960 master = repo.create_head('master')
961 repo.create_tag('init')
962
963 mp = repo.create_head('mp')
964 repo.head.reference = mp
965 f = open(fn, 'a')
966 f.write("test mp\n")
967 f.close()
968 repo.index.add([fn])
969 repo.index.commit('mp commit')
970
971 repo.head.reference = master
972 repo.head.reset(index=True, working_tree=True)
973 repo.git.clean('-x', '-f', '-d')
974
975 def ref_has_change(self, ref, change):
976 path = os.path.join(self.git_root, change.project)
977 repo = git.Repo(path)
978 for commit in repo.iter_commits(ref):
979 if commit.message.strip() == ('%s-1' % change.subject):
980 return True
981 return False
982
983 def job_has_changes(self, *args):
984 job = args[0]
985 commits = args[1:]
986 if isinstance(job, FakeBuild):
987 parameters = job.parameters
988 else:
989 parameters = json.loads(job.arguments)
990 project = parameters['ZUUL_PROJECT']
991 path = os.path.join(self.git_root, project)
992 repo = git.Repo(path)
993 ref = parameters['ZUUL_REF']
994 sha = parameters['ZUUL_COMMIT']
995 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
996 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
997 commit_messages = ['%s-1' % commit.subject for commit in commits]
James E. Blairac2c3242014-01-24 13:38:51 -0800998 self.log.debug("Checking if job %s has changes; commit_messages %s;"
999 " repo_messages %s; sha %s" % (job, commit_messages,
1000 repo_messages, sha))
Monty Taylorbc758832013-06-17 17:22:42 -04001001 for msg in commit_messages:
1002 if msg not in repo_messages:
James E. Blairac2c3242014-01-24 13:38:51 -08001003 self.log.debug(" messages do not match")
Monty Taylorbc758832013-06-17 17:22:42 -04001004 return False
1005 if repo_shas[0] != sha:
James E. Blairac2c3242014-01-24 13:38:51 -08001006 self.log.debug(" sha does not match")
Monty Taylorbc758832013-06-17 17:22:42 -04001007 return False
James E. Blairac2c3242014-01-24 13:38:51 -08001008 self.log.debug(" OK")
Monty Taylorbc758832013-06-17 17:22:42 -04001009 return True
James E. Blairb0fcae42012-07-17 11:12:10 -07001010
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001011 def registerJobs(self):
1012 count = 0
James E. Blaireff88162013-07-01 12:44:14 -04001013 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001014 self.worker.registerFunction('build:' + job)
1015 count += 1
1016 self.worker.registerFunction('stop:' + self.worker.worker_id)
1017 count += 1
1018
1019 while len(self.gearman_server.functions) < count:
1020 time.sleep(0)
1021
1022 def release(self, job):
1023 if isinstance(job, FakeBuild):
1024 job.release()
1025 else:
1026 job.waiting = False
1027 self.log.debug("Queued job %s released" % job.unique)
1028 self.gearman_server.wakeConnections()
1029
1030 def getParameter(self, job, name):
1031 if isinstance(job, FakeBuild):
1032 return job.parameters[name]
1033 else:
1034 parameters = json.loads(job.arguments)
1035 return parameters[name]
1036
1037 def resetGearmanServer(self):
1038 self.worker.setFunctions([])
1039 while True:
1040 done = True
1041 for connection in self.gearman_server.active_connections:
James E. Blairad28e912013-11-27 10:43:22 -08001042 if (connection.functions and
James E. Blair4076e2b2014-01-28 12:42:20 -08001043 connection.client_id not in ['Zuul RPC Listener',
1044 'Zuul Merger']):
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001045 done = False
1046 if done:
1047 break
1048 time.sleep(0)
1049 self.gearman_server.functions = set()
James E. Blairad28e912013-11-27 10:43:22 -08001050 self.rpc.register()
James E. Blair4076e2b2014-01-28 12:42:20 -08001051 self.merge_server.register()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001052
1053 def haveAllBuildsReported(self):
1054 # See if Zuul is waiting on a meta job to complete
1055 if self.launcher.meta_jobs:
1056 return False
1057 # Find out if every build that the worker has completed has been
1058 # reported back to Zuul. If it hasn't then that means a Gearman
1059 # event is still in transit and the system is not stable.
1060 for build in self.worker.build_history:
1061 zbuild = self.launcher.builds.get(build.uuid)
1062 if not zbuild:
1063 # It has already been reported
1064 continue
1065 # It hasn't been reported yet.
1066 return False
1067 # Make sure that none of the worker connections are in GRAB_WAIT
1068 for connection in self.worker.active_connections:
1069 if connection.state == 'GRAB_WAIT':
1070 return False
1071 return True
1072
1073 def areAllBuildsWaiting(self):
1074 ret = True
1075
1076 builds = self.launcher.builds.values()
1077 for build in builds:
1078 client_job = None
1079 for conn in self.launcher.gearman.active_connections:
1080 for j in conn.related_jobs.values():
1081 if j.unique == build.uuid:
1082 client_job = j
1083 break
1084 if not client_job:
1085 self.log.debug("%s is not known to the gearman client" %
1086 build)
1087 ret = False
1088 continue
1089 if not client_job.handle:
1090 self.log.debug("%s has no handle" % client_job)
1091 ret = False
1092 continue
1093 server_job = self.gearman_server.jobs.get(client_job.handle)
1094 if not server_job:
1095 self.log.debug("%s is not known to the gearman server" %
1096 client_job)
1097 ret = False
1098 continue
1099 if not hasattr(server_job, 'waiting'):
1100 self.log.debug("%s is being enqueued" % server_job)
1101 ret = False
1102 continue
1103 if server_job.waiting:
1104 continue
1105 worker_job = self.worker.gearman_jobs.get(server_job.unique)
1106 if worker_job:
1107 if worker_job.build.isWaiting():
1108 continue
1109 else:
1110 self.log.debug("%s is running" % worker_job)
1111 ret = False
1112 else:
1113 self.log.debug("%s is unassigned" % server_job)
1114 ret = False
1115 return ret
1116
James E. Blairb0fcae42012-07-17 11:12:10 -07001117 def waitUntilSettled(self):
1118 self.log.debug("Waiting until settled...")
1119 start = time.time()
1120 while True:
1121 if time.time() - start > 10:
1122 print 'queue status:',
1123 print self.sched.trigger_event_queue.empty(),
1124 print self.sched.result_event_queue.empty(),
1125 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001126 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -07001127 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001128 # Make sure no new events show up while we're checking
1129 self.worker.lock.acquire()
Alex Gaynor813d39b2014-05-17 16:17:16 -07001130 # have all build states propagated to zuul?
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001131 if self.haveAllBuildsReported():
1132 # Join ensures that the queue is empty _and_ events have been
1133 # processed
1134 self.fake_gerrit.event_queue.join()
1135 self.sched.trigger_event_queue.join()
1136 self.sched.result_event_queue.join()
James E. Blaira84f0e42014-02-06 07:09:22 -08001137 self.sched.run_handler_lock.acquire()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001138 if (self.sched.trigger_event_queue.empty() and
1139 self.sched.result_event_queue.empty() and
1140 self.fake_gerrit.event_queue.empty() and
James E. Blair4076e2b2014-01-28 12:42:20 -08001141 not self.merge_client.build_sets and
James E. Blair9c2985a2014-03-10 10:59:05 -07001142 self.haveAllBuildsReported() and
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001143 self.areAllBuildsWaiting()):
James E. Blaira84f0e42014-02-06 07:09:22 -08001144 self.sched.run_handler_lock.release()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001145 self.worker.lock.release()
1146 self.log.debug("...settled.")
1147 return
James E. Blaira84f0e42014-02-06 07:09:22 -08001148 self.sched.run_handler_lock.release()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001149 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001150 self.sched.wake_event.wait(0.1)
1151
James E. Blaird466dc42012-07-31 10:42:56 -07001152 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -08001153 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -07001154 return len(jobs)
1155
James E. Blair4ca985f2013-05-30 12:27:43 -07001156 def getJobFromHistory(self, name):
1157 history = self.worker.build_history
1158 for job in history:
1159 if job.name == name:
1160 return job
1161 raise Exception("Unable to find job %s in history" % name)
1162
James E. Blaire0487072012-08-29 17:38:31 -07001163 def assertEmptyQueues(self):
1164 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001165 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001166 for queue in pipeline.queues:
1167 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001168 print 'pipeline %s queue %s contents %s' % (
1169 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001170 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001171
James E. Blair66eeebf2013-07-27 17:44:32 -07001172 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001173 start = time.time()
1174 while time.time() < (start + 5):
1175 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001176 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001177 k, v = stat.split(':')
1178 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001179 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001180 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001181 elif value:
1182 if value == v:
1183 return
1184 elif kind:
1185 if v.endswith('|' + kind):
1186 return
James E. Blair412e5582013-04-22 15:50:12 -07001187 time.sleep(0.1)
1188
1189 pprint.pprint(self.statsd.stats)
1190 raise Exception("Key %s not found in reported stats" % key)
1191
James E. Blairb0fcae42012-07-17 11:12:10 -07001192 def test_jobs_launched(self):
1193 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001194
James E. Blairb0fcae42012-07-17 11:12:10 -07001195 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001196 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001197 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1198 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001199 self.assertEqual(self.getJobFromHistory('project-merge').result,
1200 'SUCCESS')
1201 self.assertEqual(self.getJobFromHistory('project-test1').result,
1202 'SUCCESS')
1203 self.assertEqual(self.getJobFromHistory('project-test2').result,
1204 'SUCCESS')
1205 self.assertEqual(A.data['status'], 'MERGED')
1206 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001207
James E. Blair66eeebf2013-07-27 17:44:32 -07001208 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1209 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1210 value='1|g')
1211 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1212 kind='ms')
1213 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1214 value='1|c')
1215 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1216 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1217 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001218 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001219 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001220 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001221 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001222
James E. Blair3cb10702013-08-24 08:56:03 -07001223 def test_initial_pipeline_gauges(self):
1224 "Test that each pipeline reported its length on start"
1225 pipeline_names = self.sched.layout.pipelines.keys()
1226 self.assertNotEqual(len(pipeline_names), 0)
1227 for name in pipeline_names:
1228 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
1229 value='0|g')
1230
James E. Blair42f74822013-05-14 15:18:03 -07001231 def test_duplicate_pipelines(self):
1232 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001233
James E. Blair42f74822013-05-14 15:18:03 -07001234 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1235 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1236 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001237
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001238 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001239 self.history[0].name == 'project-test1'
1240 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001241
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001242 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001243 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001244 self.assertIn('dup1/project-test1', A.messages[0])
1245 self.assertNotIn('dup2/project-test1', A.messages[0])
1246 self.assertNotIn('dup1/project-test1', A.messages[1])
1247 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001248 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001249 self.assertIn('dup1/project-test1', A.messages[1])
1250 self.assertNotIn('dup2/project-test1', A.messages[1])
1251 self.assertNotIn('dup1/project-test1', A.messages[0])
1252 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001253
James E. Blairb0fcae42012-07-17 11:12:10 -07001254 def test_parallel_changes(self):
1255 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001256
1257 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001258 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1259 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1260 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001261 A.addApproval('CRVW', 2)
1262 B.addApproval('CRVW', 2)
1263 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001264
1265 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1266 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1267 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1268
1269 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001270 self.assertEqual(len(self.builds), 1)
1271 self.assertEqual(self.builds[0].name, 'project-merge')
1272 self.assertTrue(self.job_has_changes(self.builds[0], A))
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), 3)
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))
1281 self.assertEqual(self.builds[2].name, 'project-merge')
1282 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001283
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001284 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001285 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001286 self.assertEqual(len(self.builds), 5)
1287 self.assertEqual(self.builds[0].name, 'project-test1')
1288 self.assertTrue(self.job_has_changes(self.builds[0], A))
1289 self.assertEqual(self.builds[1].name, 'project-test2')
1290 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001291
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001292 self.assertEqual(self.builds[2].name, 'project-test1')
1293 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1294 self.assertEqual(self.builds[3].name, 'project-test2')
1295 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001296
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001297 self.assertEqual(self.builds[4].name, 'project-merge')
1298 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001299
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001300 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001301 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001302 self.assertEqual(len(self.builds), 6)
1303 self.assertEqual(self.builds[0].name, 'project-test1')
1304 self.assertTrue(self.job_has_changes(self.builds[0], A))
1305 self.assertEqual(self.builds[1].name, 'project-test2')
1306 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001307
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001308 self.assertEqual(self.builds[2].name, 'project-test1')
1309 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1310 self.assertEqual(self.builds[3].name, 'project-test2')
1311 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001312
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001313 self.assertEqual(self.builds[4].name, 'project-test1')
1314 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1315 self.assertEqual(self.builds[5].name, 'project-test2')
1316 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001317
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001318 self.worker.hold_jobs_in_build = False
1319 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001320 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001321 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001322
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001323 self.assertEqual(len(self.history), 9)
1324 self.assertEqual(A.data['status'], 'MERGED')
1325 self.assertEqual(B.data['status'], 'MERGED')
1326 self.assertEqual(C.data['status'], 'MERGED')
1327 self.assertEqual(A.reported, 2)
1328 self.assertEqual(B.reported, 2)
1329 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001330
1331 def test_failed_changes(self):
1332 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001333 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001334
James E. Blairb02a3bb2012-07-30 17:49:55 -07001335 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1336 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001337 A.addApproval('CRVW', 2)
1338 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001339
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001340 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001341
James E. Blaire2819012013-06-28 17:17:26 -04001342 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1343 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001344 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001345
1346 self.worker.release('.*-merge')
1347 self.waitUntilSettled()
1348
1349 self.worker.hold_jobs_in_build = False
1350 self.worker.release()
1351
1352 self.waitUntilSettled()
1353 # It's certain that the merge job for change 2 will run, but
1354 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001355 self.assertTrue(len(self.history) > 6)
1356 self.assertEqual(A.data['status'], 'NEW')
1357 self.assertEqual(B.data['status'], 'MERGED')
1358 self.assertEqual(A.reported, 2)
1359 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001360
1361 def test_independent_queues(self):
1362 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001363
1364 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001365 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001366 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1367 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001368 A.addApproval('CRVW', 2)
1369 B.addApproval('CRVW', 2)
1370 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001371
1372 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1373 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1374 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1375
James E. Blairb02a3bb2012-07-30 17:49:55 -07001376 self.waitUntilSettled()
1377
1378 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001379 self.assertEqual(len(self.builds), 2)
1380 self.assertEqual(self.builds[0].name, 'project-merge')
1381 self.assertTrue(self.job_has_changes(self.builds[0], A))
1382 self.assertEqual(self.builds[1].name, 'project1-merge')
1383 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001384
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001385 # Release the current merge builds
1386 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001387 self.waitUntilSettled()
1388 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001389 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001390 self.waitUntilSettled()
1391
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001392 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001393 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001394 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001395
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001396 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001397 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001398 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001399
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001400 self.assertEqual(len(self.history), 11)
1401 self.assertEqual(A.data['status'], 'MERGED')
1402 self.assertEqual(B.data['status'], 'MERGED')
1403 self.assertEqual(C.data['status'], 'MERGED')
1404 self.assertEqual(A.reported, 2)
1405 self.assertEqual(B.reported, 2)
1406 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001407
1408 def test_failed_change_at_head(self):
1409 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001410
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001411 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001412 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1413 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1414 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001415 A.addApproval('CRVW', 2)
1416 B.addApproval('CRVW', 2)
1417 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001418
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001419 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001420
1421 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1422 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1423 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1424
1425 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001426
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001427 self.assertEqual(len(self.builds), 1)
1428 self.assertEqual(self.builds[0].name, 'project-merge')
1429 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001430
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001431 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001432 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001433 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001434 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001435 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001436 self.waitUntilSettled()
1437
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001438 self.assertEqual(len(self.builds), 6)
1439 self.assertEqual(self.builds[0].name, 'project-test1')
1440 self.assertEqual(self.builds[1].name, 'project-test2')
1441 self.assertEqual(self.builds[2].name, 'project-test1')
1442 self.assertEqual(self.builds[3].name, 'project-test2')
1443 self.assertEqual(self.builds[4].name, 'project-test1')
1444 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001445
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001446 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001447 self.waitUntilSettled()
1448
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001449 # project-test2, project-merge for B
1450 self.assertEqual(len(self.builds), 2)
1451 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001452
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001453 self.worker.hold_jobs_in_build = False
1454 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001455 self.waitUntilSettled()
1456
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001457 self.assertEqual(len(self.builds), 0)
1458 self.assertEqual(len(self.history), 15)
1459 self.assertEqual(A.data['status'], 'NEW')
1460 self.assertEqual(B.data['status'], 'MERGED')
1461 self.assertEqual(C.data['status'], 'MERGED')
1462 self.assertEqual(A.reported, 2)
1463 self.assertEqual(B.reported, 2)
1464 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001465
James E. Blair0aac4872013-08-23 14:02:38 -07001466 def test_failed_change_in_middle(self):
1467 "Test a failed change in the middle of the queue"
1468
1469 self.worker.hold_jobs_in_build = True
1470 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1471 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1472 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1473 A.addApproval('CRVW', 2)
1474 B.addApproval('CRVW', 2)
1475 C.addApproval('CRVW', 2)
1476
1477 self.worker.addFailTest('project-test1', B)
1478
1479 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1480 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1481 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1482
1483 self.waitUntilSettled()
1484
1485 self.worker.release('.*-merge')
1486 self.waitUntilSettled()
1487 self.worker.release('.*-merge')
1488 self.waitUntilSettled()
1489 self.worker.release('.*-merge')
1490 self.waitUntilSettled()
1491
1492 self.assertEqual(len(self.builds), 6)
1493 self.assertEqual(self.builds[0].name, 'project-test1')
1494 self.assertEqual(self.builds[1].name, 'project-test2')
1495 self.assertEqual(self.builds[2].name, 'project-test1')
1496 self.assertEqual(self.builds[3].name, 'project-test2')
1497 self.assertEqual(self.builds[4].name, 'project-test1')
1498 self.assertEqual(self.builds[5].name, 'project-test2')
1499
1500 self.release(self.builds[2])
1501 self.waitUntilSettled()
1502
James E. Blair972e3c72013-08-29 12:04:55 -07001503 # project-test1 and project-test2 for A
1504 # project-test2 for B
1505 # project-merge for C (without B)
1506 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -07001507 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1508
James E. Blair972e3c72013-08-29 12:04:55 -07001509 self.worker.release('.*-merge')
1510 self.waitUntilSettled()
1511
1512 # project-test1 and project-test2 for A
1513 # project-test2 for B
1514 # project-test1 and project-test2 for C
1515 self.assertEqual(len(self.builds), 5)
1516
James E. Blair0aac4872013-08-23 14:02:38 -07001517 items = self.sched.layout.pipelines['gate'].getAllItems()
1518 builds = items[0].current_build_set.getBuilds()
1519 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1520 self.assertEqual(self.countJobResults(builds, None), 2)
1521 builds = items[1].current_build_set.getBuilds()
1522 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1523 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1524 self.assertEqual(self.countJobResults(builds, None), 1)
1525 builds = items[2].current_build_set.getBuilds()
1526 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -07001527 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -07001528
1529 self.worker.hold_jobs_in_build = False
1530 self.worker.release()
1531 self.waitUntilSettled()
1532
1533 self.assertEqual(len(self.builds), 0)
1534 self.assertEqual(len(self.history), 12)
1535 self.assertEqual(A.data['status'], 'MERGED')
1536 self.assertEqual(B.data['status'], 'NEW')
1537 self.assertEqual(C.data['status'], 'MERGED')
1538 self.assertEqual(A.reported, 2)
1539 self.assertEqual(B.reported, 2)
1540 self.assertEqual(C.reported, 2)
1541
James E. Blaird466dc42012-07-31 10:42:56 -07001542 def test_failed_change_at_head_with_queue(self):
1543 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001544
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001545 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001546 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1547 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1548 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001549 A.addApproval('CRVW', 2)
1550 B.addApproval('CRVW', 2)
1551 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001552
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001553 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001554
1555 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1556 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1557 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1558
1559 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001560 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001561 self.assertEqual(len(self.builds), 0)
1562 self.assertEqual(len(queue), 1)
1563 self.assertEqual(queue[0].name, 'build:project-merge')
1564 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001565
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001566 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001567 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001568 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001569 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001570 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001571 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001572 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001573
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001574 self.assertEqual(len(self.builds), 0)
1575 self.assertEqual(len(queue), 6)
1576 self.assertEqual(queue[0].name, 'build:project-test1')
1577 self.assertEqual(queue[1].name, 'build:project-test2')
1578 self.assertEqual(queue[2].name, 'build:project-test1')
1579 self.assertEqual(queue[3].name, 'build:project-test2')
1580 self.assertEqual(queue[4].name, 'build:project-test1')
1581 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001582
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001583 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001584 self.waitUntilSettled()
1585
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001586 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001587 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001588 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1589 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001590
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001591 self.gearman_server.hold_jobs_in_queue = False
1592 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001593 self.waitUntilSettled()
1594
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001595 self.assertEqual(len(self.builds), 0)
1596 self.assertEqual(len(self.history), 11)
1597 self.assertEqual(A.data['status'], 'NEW')
1598 self.assertEqual(B.data['status'], 'MERGED')
1599 self.assertEqual(C.data['status'], 'MERGED')
1600 self.assertEqual(A.reported, 2)
1601 self.assertEqual(B.reported, 2)
1602 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001603
James E. Blairfef71632013-09-23 11:15:47 -07001604 def test_two_failed_changes_at_head(self):
1605 "Test that changes are reparented correctly if 2 fail at head"
1606
1607 self.worker.hold_jobs_in_build = True
1608 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1609 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1610 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1611 A.addApproval('CRVW', 2)
1612 B.addApproval('CRVW', 2)
1613 C.addApproval('CRVW', 2)
1614
1615 self.worker.addFailTest('project-test1', A)
1616 self.worker.addFailTest('project-test1', B)
1617
1618 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1619 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1620 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1621 self.waitUntilSettled()
1622
1623 self.worker.release('.*-merge')
1624 self.waitUntilSettled()
1625 self.worker.release('.*-merge')
1626 self.waitUntilSettled()
1627 self.worker.release('.*-merge')
1628 self.waitUntilSettled()
1629
1630 self.assertEqual(len(self.builds), 6)
1631 self.assertEqual(self.builds[0].name, 'project-test1')
1632 self.assertEqual(self.builds[1].name, 'project-test2')
1633 self.assertEqual(self.builds[2].name, 'project-test1')
1634 self.assertEqual(self.builds[3].name, 'project-test2')
1635 self.assertEqual(self.builds[4].name, 'project-test1')
1636 self.assertEqual(self.builds[5].name, 'project-test2')
1637
1638 self.assertTrue(self.job_has_changes(self.builds[0], A))
1639 self.assertTrue(self.job_has_changes(self.builds[2], A))
1640 self.assertTrue(self.job_has_changes(self.builds[2], B))
1641 self.assertTrue(self.job_has_changes(self.builds[4], A))
1642 self.assertTrue(self.job_has_changes(self.builds[4], B))
1643 self.assertTrue(self.job_has_changes(self.builds[4], C))
1644
1645 # Fail change B first
1646 self.release(self.builds[2])
1647 self.waitUntilSettled()
1648
1649 # restart of C after B failure
1650 self.worker.release('.*-merge')
1651 self.waitUntilSettled()
1652
1653 self.assertEqual(len(self.builds), 5)
1654 self.assertEqual(self.builds[0].name, 'project-test1')
1655 self.assertEqual(self.builds[1].name, 'project-test2')
1656 self.assertEqual(self.builds[2].name, 'project-test2')
1657 self.assertEqual(self.builds[3].name, 'project-test1')
1658 self.assertEqual(self.builds[4].name, 'project-test2')
1659
1660 self.assertTrue(self.job_has_changes(self.builds[1], A))
1661 self.assertTrue(self.job_has_changes(self.builds[2], A))
1662 self.assertTrue(self.job_has_changes(self.builds[2], B))
1663 self.assertTrue(self.job_has_changes(self.builds[4], A))
1664 self.assertFalse(self.job_has_changes(self.builds[4], B))
1665 self.assertTrue(self.job_has_changes(self.builds[4], C))
1666
1667 # Finish running all passing jobs for change A
1668 self.release(self.builds[1])
1669 self.waitUntilSettled()
1670 # Fail and report change A
1671 self.release(self.builds[0])
1672 self.waitUntilSettled()
1673
1674 # restart of B,C after A failure
1675 self.worker.release('.*-merge')
1676 self.waitUntilSettled()
1677 self.worker.release('.*-merge')
1678 self.waitUntilSettled()
1679
1680 self.assertEqual(len(self.builds), 4)
1681 self.assertEqual(self.builds[0].name, 'project-test1') # B
1682 self.assertEqual(self.builds[1].name, 'project-test2') # B
1683 self.assertEqual(self.builds[2].name, 'project-test1') # C
1684 self.assertEqual(self.builds[3].name, 'project-test2') # C
1685
1686 self.assertFalse(self.job_has_changes(self.builds[1], A))
1687 self.assertTrue(self.job_has_changes(self.builds[1], B))
1688 self.assertFalse(self.job_has_changes(self.builds[1], C))
1689
1690 self.assertFalse(self.job_has_changes(self.builds[2], A))
1691 # After A failed and B and C restarted, B should be back in
1692 # C's tests because it has not failed yet.
1693 self.assertTrue(self.job_has_changes(self.builds[2], B))
1694 self.assertTrue(self.job_has_changes(self.builds[2], C))
1695
1696 self.worker.hold_jobs_in_build = False
1697 self.worker.release()
1698 self.waitUntilSettled()
1699
1700 self.assertEqual(len(self.builds), 0)
1701 self.assertEqual(len(self.history), 21)
1702 self.assertEqual(A.data['status'], 'NEW')
1703 self.assertEqual(B.data['status'], 'NEW')
1704 self.assertEqual(C.data['status'], 'MERGED')
1705 self.assertEqual(A.reported, 2)
1706 self.assertEqual(B.reported, 2)
1707 self.assertEqual(C.reported, 2)
1708
James E. Blair8c803f82012-07-31 16:25:42 -07001709 def test_patch_order(self):
1710 "Test that dependent patches are tested in the right order"
1711 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1712 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1713 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1714 A.addApproval('CRVW', 2)
1715 B.addApproval('CRVW', 2)
1716 C.addApproval('CRVW', 2)
1717
1718 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1719 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1720 M2.setMerged()
1721 M1.setMerged()
1722
1723 # C -> B -> A -> M1 -> M2
1724 # M2 is here to make sure it is never queried. If it is, it
1725 # means zuul is walking down the entire history of merged
1726 # changes.
1727
1728 C.setDependsOn(B, 1)
1729 B.setDependsOn(A, 1)
1730 A.setDependsOn(M1, 1)
1731 M1.setDependsOn(M2, 1)
1732
1733 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1734
1735 self.waitUntilSettled()
1736
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001737 self.assertEqual(A.data['status'], 'NEW')
1738 self.assertEqual(B.data['status'], 'NEW')
1739 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001740
1741 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1742 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1743
1744 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001745 self.assertEqual(M2.queried, 0)
1746 self.assertEqual(A.data['status'], 'MERGED')
1747 self.assertEqual(B.data['status'], 'MERGED')
1748 self.assertEqual(C.data['status'], 'MERGED')
1749 self.assertEqual(A.reported, 2)
1750 self.assertEqual(B.reported, 2)
1751 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001752
James E. Blair0e933c52013-07-11 10:18:52 -07001753 def test_trigger_cache(self):
1754 "Test that the trigger cache operates correctly"
1755 self.worker.hold_jobs_in_build = True
1756
1757 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1758 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1759 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1760 A.addApproval('CRVW', 2)
1761 B.addApproval('CRVW', 2)
1762
1763 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1764 M1.setMerged()
1765
1766 B.setDependsOn(A, 1)
1767 A.setDependsOn(M1, 1)
1768
1769 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1770 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1771
1772 self.waitUntilSettled()
1773
1774 for build in self.builds:
1775 if build.parameters['ZUUL_PIPELINE'] == 'check':
1776 build.release()
1777 self.waitUntilSettled()
1778 for build in self.builds:
1779 if build.parameters['ZUUL_PIPELINE'] == 'check':
1780 build.release()
1781 self.waitUntilSettled()
1782
1783 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1784 self.waitUntilSettled()
1785
James E. Blair6c358e72013-07-29 17:06:47 -07001786 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001787 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001788 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001789
1790 self.worker.hold_jobs_in_build = False
1791 self.worker.release()
1792 self.waitUntilSettled()
1793
1794 self.assertEqual(A.data['status'], 'MERGED')
1795 self.assertEqual(B.data['status'], 'MERGED')
1796 self.assertEqual(A.queried, 2) # Initial and isMerged
1797 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1798
James E. Blair8c803f82012-07-31 16:25:42 -07001799 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001800 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001801 # TODO: move to test_gerrit (this is a unit test!)
1802 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001803 trigger = self.sched.layout.pipelines['gate'].trigger
1804 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001805 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001806 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001807
1808 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001809 a = trigger.getChange(1, 2, refresh=True)
1810 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001811
1812 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001813 a = trigger.getChange(1, 2, refresh=True)
1814 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1815 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001816
James E. Blair11041d22014-05-02 14:49:53 -07001817 def test_pipeline_requirements_closed_change(self):
1818 "Test that pipeline requirements for closed changes are effective"
1819 self.config.set('zuul', 'layout_config',
1820 'tests/fixtures/layout-pipeline-requirements.yaml')
1821 self.sched.reconfigure(self.config)
1822
1823 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1824 status='MERGED')
1825 self.fake_gerrit.addEvent(A.addApproval('CRVW', 2))
1826 self.waitUntilSettled()
1827 self.assertEqual(len(self.history), 0)
1828 self.assertEqual(len(self.builds), 0)
1829
1830 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
1831 status='MERGED')
1832 B.addApproval('CRVW', 2)
1833 B.addApproval('VRFY', 1)
1834 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1835 self.waitUntilSettled()
1836 self.assertEqual(len(self.history), 0)
1837 self.assertEqual(len(self.builds), 0)
1838
1839 for pipeline in self.sched.layout.pipelines.values():
1840 pipeline.trigger.maintainCache([])
1841
James E. Blair4886cc12012-07-18 15:39:41 -07001842 def test_build_configuration(self):
1843 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001844
1845 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001846 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1847 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1848 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1849 A.addApproval('CRVW', 2)
1850 B.addApproval('CRVW', 2)
1851 C.addApproval('CRVW', 2)
1852 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1853 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1854 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1855 self.waitUntilSettled()
1856
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001857 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001858 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001859 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001860 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001861 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001862 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001863 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001864 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1865 self.gearman_server.hold_jobs_in_queue = False
1866 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001867 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001868
Monty Taylorbc758832013-06-17 17:22:42 -04001869 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001870 repo = git.Repo(path)
1871 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1872 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001873 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001874 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001875
1876 def test_build_configuration_conflict(self):
1877 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001878
1879 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001880 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1881 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001882 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001883 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1884 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001885 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001886 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1887 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001888 A.addApproval('CRVW', 2)
1889 B.addApproval('CRVW', 2)
1890 C.addApproval('CRVW', 2)
1891 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1892 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1893 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1894 self.waitUntilSettled()
1895
James E. Blair6736beb2013-07-11 15:18:15 -07001896 self.assertEqual(A.reported, 1)
1897 self.assertEqual(B.reported, 1)
1898 self.assertEqual(C.reported, 1)
1899
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001900 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001901 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001902 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001903 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001904 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001905 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001906
1907 self.assertEqual(len(self.history), 2) # A and C merge jobs
1908
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001909 self.gearman_server.hold_jobs_in_queue = False
1910 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001911 self.waitUntilSettled()
1912
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001913 self.assertEqual(A.data['status'], 'MERGED')
1914 self.assertEqual(B.data['status'], 'NEW')
1915 self.assertEqual(C.data['status'], 'MERGED')
1916 self.assertEqual(A.reported, 2)
1917 self.assertEqual(B.reported, 2)
1918 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001919 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001920
James E. Blairdaabed22012-08-15 15:38:57 -07001921 def test_post(self):
1922 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001923
Zhongyue Luo5d556072012-09-21 02:00:47 +09001924 e = {
1925 "type": "ref-updated",
1926 "submitter": {
1927 "name": "User Name",
1928 },
1929 "refUpdate": {
1930 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1931 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1932 "refName": "master",
1933 "project": "org/project",
1934 }
1935 }
James E. Blairdaabed22012-08-15 15:38:57 -07001936 self.fake_gerrit.addEvent(e)
1937 self.waitUntilSettled()
1938
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001939 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001940 self.assertEqual(len(self.history), 1)
1941 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001942
1943 def test_build_configuration_branch(self):
1944 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001945
1946 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001947 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1948 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1949 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1950 A.addApproval('CRVW', 2)
1951 B.addApproval('CRVW', 2)
1952 C.addApproval('CRVW', 2)
1953 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1954 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1955 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1956 self.waitUntilSettled()
1957
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001958 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001959 self.waitUntilSettled()
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. Blair1f4c2bb2013-04-26 08:40:46 -07001962 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001963 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001964 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001965 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1966 self.gearman_server.hold_jobs_in_queue = False
1967 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001968 self.waitUntilSettled()
1969
Monty Taylorbc758832013-06-17 17:22:42 -04001970 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001971 repo = git.Repo(path)
1972 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1973 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001974 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001975 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001976
1977 def test_build_configuration_branch_interaction(self):
1978 "Test that switching between branches works"
1979 self.test_build_configuration()
1980 self.test_build_configuration_branch()
1981 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001982 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001983 repo = git.Repo(path)
1984 repo.heads.master.commit = repo.commit('init')
1985 self.test_build_configuration()
1986
1987 def test_build_configuration_multi_branch(self):
1988 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001989
1990 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001991 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1992 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1993 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1994 A.addApproval('CRVW', 2)
1995 B.addApproval('CRVW', 2)
1996 C.addApproval('CRVW', 2)
1997 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1998 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1999 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2000 self.waitUntilSettled()
James E. Blairbb1fe502014-03-04 10:15:06 -08002001 queue = self.gearman_server.getQueue()
2002 job_A = None
2003 for job in queue:
2004 if 'project-merge' in job.name:
2005 job_A = job
2006 ref_A = self.getParameter(job_A, 'ZUUL_REF')
2007 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
2008 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
2009 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
James E. Blairc6294a52012-08-17 10:19:48 -07002010
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002011 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07002012 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07002013 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07002014 job_B = None
2015 for job in queue:
2016 if 'project-merge' in job.name:
2017 job_B = job
2018 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08002019 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07002020 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blairbb1fe502014-03-04 10:15:06 -08002021 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
2022
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002023 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07002024 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07002025 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07002026 for job in queue:
2027 if 'project-merge' in job.name:
2028 job_C = job
2029 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08002030 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07002031 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blairbb1fe502014-03-04 10:15:06 -08002032 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002033 self.gearman_server.hold_jobs_in_queue = False
2034 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07002035 self.waitUntilSettled()
2036
Monty Taylorbc758832013-06-17 17:22:42 -04002037 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07002038 repo = git.Repo(path)
2039
2040 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07002041 for c in repo.iter_commits(ref_C)]
James E. Blairbb1fe502014-03-04 10:15:06 -08002042 repo_shas = [c.hexsha for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07002043 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07002044 correct_messages = ['initial commit', 'A-1', 'C-1']
James E. Blairbb1fe502014-03-04 10:15:06 -08002045 # Ensure the right commits are in the history for this ref
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002046 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08002047 # Ensure ZUUL_REF -> ZUUL_COMMIT
2048 self.assertEqual(repo_shas[0], commit_C)
James E. Blairc6294a52012-08-17 10:19:48 -07002049
2050 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07002051 for c in repo.iter_commits(ref_B)]
James E. Blairbb1fe502014-03-04 10:15:06 -08002052 repo_shas = [c.hexsha for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07002053 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07002054 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002055 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08002056 self.assertEqual(repo_shas[0], commit_B)
2057
2058 repo_messages = [c.message.strip()
2059 for c in repo.iter_commits(ref_A)]
2060 repo_shas = [c.hexsha for c in repo.iter_commits(ref_A)]
2061 repo_messages.reverse()
2062 correct_messages = ['initial commit', 'A-1']
2063 self.assertEqual(repo_messages, correct_messages)
2064 self.assertEqual(repo_shas[0], commit_A)
2065
2066 self.assertNotEqual(ref_A, ref_B, ref_C)
2067 self.assertNotEqual(commit_A, commit_B, commit_C)
James E. Blair7f71c802012-08-22 13:04:32 -07002068
2069 def test_one_job_project(self):
2070 "Test that queueing works with one job"
2071 A = self.fake_gerrit.addFakeChange('org/one-job-project',
2072 'master', 'A')
2073 B = self.fake_gerrit.addFakeChange('org/one-job-project',
2074 'master', 'B')
2075 A.addApproval('CRVW', 2)
2076 B.addApproval('CRVW', 2)
2077 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2078 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2079 self.waitUntilSettled()
2080
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002081 self.assertEqual(A.data['status'], 'MERGED')
2082 self.assertEqual(A.reported, 2)
2083 self.assertEqual(B.data['status'], 'MERGED')
2084 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07002085
Antoine Musso80edd5a2013-02-13 15:37:53 +01002086 def test_job_from_templates_launched(self):
2087 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002088
Antoine Musso80edd5a2013-02-13 15:37:53 +01002089 A = self.fake_gerrit.addFakeChange(
2090 'org/templated-project', 'master', 'A')
2091 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2092 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01002093
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002094 self.assertEqual(self.getJobFromHistory('project-test1').result,
2095 'SUCCESS')
2096 self.assertEqual(self.getJobFromHistory('project-test2').result,
2097 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01002098
James E. Blair3e98c022013-12-16 15:25:38 -08002099 def test_layered_templates(self):
2100 "Test whether a job generated via a template can be launched"
2101
2102 A = self.fake_gerrit.addFakeChange(
2103 'org/layered-project', 'master', 'A')
2104 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2105 self.waitUntilSettled()
2106
2107 self.assertEqual(self.getJobFromHistory('project-test1').result,
2108 'SUCCESS')
2109 self.assertEqual(self.getJobFromHistory('project-test2').result,
2110 'SUCCESS')
James E. Blairaea6cf62013-12-16 15:38:12 -08002111 self.assertEqual(self.getJobFromHistory('layered-project-test3'
2112 ).result, 'SUCCESS')
2113 self.assertEqual(self.getJobFromHistory('layered-project-test4'
2114 ).result, 'SUCCESS')
James E. Blair12a92b12014-03-26 11:54:53 -07002115 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
2116 ).result, 'SUCCESS')
James E. Blair3e98c022013-12-16 15:25:38 -08002117 self.assertEqual(self.getJobFromHistory('project-test6').result,
2118 'SUCCESS')
2119
James E. Blaircaec0c52012-08-22 14:52:22 -07002120 def test_dependent_changes_dequeue(self):
2121 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002122
James E. Blaircaec0c52012-08-22 14:52:22 -07002123 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2124 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2125 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2126 A.addApproval('CRVW', 2)
2127 B.addApproval('CRVW', 2)
2128 C.addApproval('CRVW', 2)
2129
2130 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
2131 M1.setMerged()
2132
2133 # C -> B -> A -> M1
2134
2135 C.setDependsOn(B, 1)
2136 B.setDependsOn(A, 1)
2137 A.setDependsOn(M1, 1)
2138
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002139 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07002140
2141 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2142 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2143 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2144
2145 self.waitUntilSettled()
2146
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002147 self.assertEqual(A.data['status'], 'NEW')
2148 self.assertEqual(A.reported, 2)
2149 self.assertEqual(B.data['status'], 'NEW')
2150 self.assertEqual(B.reported, 2)
2151 self.assertEqual(C.data['status'], 'NEW')
2152 self.assertEqual(C.reported, 2)
2153 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07002154
James E. Blair972e3c72013-08-29 12:04:55 -07002155 def test_failing_dependent_changes(self):
2156 "Test that failing dependent patches are taken out of stream"
2157 self.worker.hold_jobs_in_build = True
2158 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2159 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2160 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2161 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2162 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
2163 A.addApproval('CRVW', 2)
2164 B.addApproval('CRVW', 2)
2165 C.addApproval('CRVW', 2)
2166 D.addApproval('CRVW', 2)
2167 E.addApproval('CRVW', 2)
2168
2169 # E, D -> C -> B, A
2170
2171 D.setDependsOn(C, 1)
2172 C.setDependsOn(B, 1)
2173
2174 self.worker.addFailTest('project-test1', B)
2175
2176 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2177 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2178 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2179 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2180 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2181
2182 self.waitUntilSettled()
2183 self.worker.release('.*-merge')
2184 self.waitUntilSettled()
2185 self.worker.release('.*-merge')
2186 self.waitUntilSettled()
2187 self.worker.release('.*-merge')
2188 self.waitUntilSettled()
2189 self.worker.release('.*-merge')
2190 self.waitUntilSettled()
2191 self.worker.release('.*-merge')
2192 self.waitUntilSettled()
2193
2194 self.worker.hold_jobs_in_build = False
2195 for build in self.builds:
2196 if build.parameters['ZUUL_CHANGE'] != '1':
2197 build.release()
2198 self.waitUntilSettled()
2199
2200 self.worker.release()
2201 self.waitUntilSettled()
2202
2203 self.assertEqual(A.data['status'], 'MERGED')
2204 self.assertEqual(A.reported, 2)
2205 self.assertEqual(B.data['status'], 'NEW')
2206 self.assertEqual(B.reported, 2)
2207 self.assertEqual(C.data['status'], 'NEW')
2208 self.assertEqual(C.reported, 2)
2209 self.assertEqual(D.data['status'], 'NEW')
2210 self.assertEqual(D.reported, 2)
2211 self.assertEqual(E.data['status'], 'MERGED')
2212 self.assertEqual(E.reported, 2)
2213 self.assertEqual(len(self.history), 18)
2214
James E. Blairec590122012-08-22 15:19:31 -07002215 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08002216 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07002217 # If it's dequeued more than once, we should see extra
2218 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07002219
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002220 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07002221 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2222 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2223 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2224 A.addApproval('CRVW', 2)
2225 B.addApproval('CRVW', 2)
2226 C.addApproval('CRVW', 2)
2227
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002228 self.worker.addFailTest('project1-test1', A)
2229 self.worker.addFailTest('project1-test2', A)
2230 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07002231
2232 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2233 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2234 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2235
2236 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07002237
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002238 self.assertEqual(len(self.builds), 1)
2239 self.assertEqual(self.builds[0].name, 'project1-merge')
2240 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07002241
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002242 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002243 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002244 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002245 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002246 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002247 self.waitUntilSettled()
2248
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002249 self.assertEqual(len(self.builds), 9)
2250 self.assertEqual(self.builds[0].name, 'project1-test1')
2251 self.assertEqual(self.builds[1].name, 'project1-test2')
2252 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
2253 self.assertEqual(self.builds[3].name, 'project1-test1')
2254 self.assertEqual(self.builds[4].name, 'project1-test2')
2255 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
2256 self.assertEqual(self.builds[6].name, 'project1-test1')
2257 self.assertEqual(self.builds[7].name, 'project1-test2')
2258 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07002259
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002260 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07002261 self.waitUntilSettled()
2262
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002263 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
2264 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07002265
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002266 self.worker.hold_jobs_in_build = False
2267 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07002268 self.waitUntilSettled()
2269
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002270 self.assertEqual(len(self.builds), 0)
2271 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07002272
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002273 self.assertEqual(A.data['status'], 'NEW')
2274 self.assertEqual(B.data['status'], 'MERGED')
2275 self.assertEqual(C.data['status'], 'MERGED')
2276 self.assertEqual(A.reported, 2)
2277 self.assertEqual(B.reported, 2)
2278 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07002279
2280 def test_nonvoting_job(self):
2281 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002282
James E. Blair4ec821f2012-08-23 15:28:28 -07002283 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
2284 'master', 'A')
2285 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002286 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07002287 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2288
2289 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07002290
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002291 self.assertEqual(A.data['status'], 'MERGED')
2292 self.assertEqual(A.reported, 2)
2293 self.assertEqual(
2294 self.getJobFromHistory('nonvoting-project-merge').result,
2295 'SUCCESS')
2296 self.assertEqual(
2297 self.getJobFromHistory('nonvoting-project-test1').result,
2298 'SUCCESS')
2299 self.assertEqual(
2300 self.getJobFromHistory('nonvoting-project-test2').result,
2301 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07002302
2303 def test_check_queue_success(self):
2304 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002305
James E. Blaire0487072012-08-29 17:38:31 -07002306 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2307 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2308
2309 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002310
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002311 self.assertEqual(A.data['status'], 'NEW')
2312 self.assertEqual(A.reported, 1)
2313 self.assertEqual(self.getJobFromHistory('project-merge').result,
2314 'SUCCESS')
2315 self.assertEqual(self.getJobFromHistory('project-test1').result,
2316 'SUCCESS')
2317 self.assertEqual(self.getJobFromHistory('project-test2').result,
2318 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07002319
2320 def test_check_queue_failure(self):
2321 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002322
James E. Blaire0487072012-08-29 17:38:31 -07002323 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002324 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07002325 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2326
2327 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002328
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002329 self.assertEqual(A.data['status'], 'NEW')
2330 self.assertEqual(A.reported, 1)
2331 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07002332 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002333 self.assertEqual(self.getJobFromHistory('project-test1').result,
2334 'SUCCESS')
2335 self.assertEqual(self.getJobFromHistory('project-test2').result,
2336 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07002337
2338 def test_dependent_behind_dequeue(self):
2339 "test that dependent changes behind dequeued changes work"
2340 # This complicated test is a reproduction of a real life bug
2341 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07002342
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002343 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07002344 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2345 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2346 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2347 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2348 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
2349 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
2350 D.setDependsOn(C, 1)
2351 E.setDependsOn(D, 1)
2352 A.addApproval('CRVW', 2)
2353 B.addApproval('CRVW', 2)
2354 C.addApproval('CRVW', 2)
2355 D.addApproval('CRVW', 2)
2356 E.addApproval('CRVW', 2)
2357 F.addApproval('CRVW', 2)
2358
2359 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07002360
2361 # Change object re-use in the gerrit trigger is hidden if
2362 # changes are added in quick succession; waiting makes it more
2363 # like real life.
2364 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2365 self.waitUntilSettled()
2366 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2367 self.waitUntilSettled()
2368
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002369 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002370 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002371 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002372 self.waitUntilSettled()
2373
2374 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2375 self.waitUntilSettled()
2376 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2377 self.waitUntilSettled()
2378 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2379 self.waitUntilSettled()
2380 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
2381 self.waitUntilSettled()
2382
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002383 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002384 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002385 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002386 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002387 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002388 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002389 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002390 self.waitUntilSettled()
2391
2392 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002393
2394 # Grab pointers to the jobs we want to release before
2395 # releasing any, because list indexes may change as
2396 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002397 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002398 a.release()
2399 b.release()
2400 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002401 self.waitUntilSettled()
2402
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002403 self.worker.hold_jobs_in_build = False
2404 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002405 self.waitUntilSettled()
2406
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002407 self.assertEqual(A.data['status'], 'NEW')
2408 self.assertEqual(B.data['status'], 'MERGED')
2409 self.assertEqual(C.data['status'], 'MERGED')
2410 self.assertEqual(D.data['status'], 'MERGED')
2411 self.assertEqual(E.data['status'], 'MERGED')
2412 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002413
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002414 self.assertEqual(A.reported, 2)
2415 self.assertEqual(B.reported, 2)
2416 self.assertEqual(C.reported, 2)
2417 self.assertEqual(D.reported, 2)
2418 self.assertEqual(E.reported, 2)
2419 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002420
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002421 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2422 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002423
2424 def test_merger_repack(self):
2425 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002426
James E. Blair05fed602012-09-07 12:45:24 -07002427 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2428 A.addApproval('CRVW', 2)
2429 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2430 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002431 self.assertEqual(self.getJobFromHistory('project-merge').result,
2432 'SUCCESS')
2433 self.assertEqual(self.getJobFromHistory('project-test1').result,
2434 'SUCCESS')
2435 self.assertEqual(self.getJobFromHistory('project-test2').result,
2436 'SUCCESS')
2437 self.assertEqual(A.data['status'], 'MERGED')
2438 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07002439 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002440 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002441
Monty Taylorbc758832013-06-17 17:22:42 -04002442 path = os.path.join(self.git_root, "org/project")
2443 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002444
2445 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2446 A.addApproval('CRVW', 2)
2447 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2448 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002449 self.assertEqual(self.getJobFromHistory('project-merge').result,
2450 'SUCCESS')
2451 self.assertEqual(self.getJobFromHistory('project-test1').result,
2452 'SUCCESS')
2453 self.assertEqual(self.getJobFromHistory('project-test2').result,
2454 'SUCCESS')
2455 self.assertEqual(A.data['status'], 'MERGED')
2456 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002457
James E. Blair4886f282012-11-15 09:27:33 -08002458 def test_merger_repack_large_change(self):
2459 "Test that the merger works with large changes after a repack"
2460 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08002461 # This test assumes the repo is already cloned; make sure it is
2462 url = self.sched.triggers['gerrit'].getGitUrl(
2463 self.sched.layout.projects['org/project1'])
James E. Blair4076e2b2014-01-28 12:42:20 -08002464 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08002465 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2466 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002467 path = os.path.join(self.upstream_root, "org/project1")
2468 print repack_repo(path)
2469 path = os.path.join(self.git_root, "org/project1")
2470 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002471
2472 A.addApproval('CRVW', 2)
2473 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2474 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002475 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2476 'SUCCESS')
2477 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2478 'SUCCESS')
2479 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2480 'SUCCESS')
2481 self.assertEqual(A.data['status'], 'MERGED')
2482 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002483
James E. Blair7ee88a22012-09-12 18:59:31 +02002484 def test_nonexistent_job(self):
2485 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002486 # Set to the state immediately after a restart
2487 self.resetGearmanServer()
2488 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002489
2490 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2491 A.addApproval('CRVW', 2)
2492 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2493 # There may be a thread about to report a lost change
2494 while A.reported < 2:
2495 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002496 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002497 self.assertFalse(job_names)
2498 self.assertEqual(A.data['status'], 'NEW')
2499 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002500 self.assertEmptyQueues()
2501
2502 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002503 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002504 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2505 A.addApproval('CRVW', 2)
2506 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2507 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002508 self.assertEqual(self.getJobFromHistory('project-merge').result,
2509 'SUCCESS')
2510 self.assertEqual(self.getJobFromHistory('project-test1').result,
2511 'SUCCESS')
2512 self.assertEqual(self.getJobFromHistory('project-test2').result,
2513 'SUCCESS')
2514 self.assertEqual(A.data['status'], 'MERGED')
2515 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002516
2517 def test_single_nonexistent_post_job(self):
2518 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002519 e = {
2520 "type": "ref-updated",
2521 "submitter": {
2522 "name": "User Name",
2523 },
2524 "refUpdate": {
2525 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2526 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2527 "refName": "master",
2528 "project": "org/project",
2529 }
2530 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002531 # Set to the state immediately after a restart
2532 self.resetGearmanServer()
2533 self.launcher.negative_function_cache_ttl = 0
2534
James E. Blairf62d4282012-12-31 17:01:50 -08002535 self.fake_gerrit.addEvent(e)
2536 self.waitUntilSettled()
2537
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002538 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002539
2540 def test_new_patchset_dequeues_old(self):
2541 "Test that a new patchset causes the old to be dequeued"
2542 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002543 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002544 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2545 M.setMerged()
2546
2547 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2548 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2549 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2550 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2551 A.addApproval('CRVW', 2)
2552 B.addApproval('CRVW', 2)
2553 C.addApproval('CRVW', 2)
2554 D.addApproval('CRVW', 2)
2555
2556 C.setDependsOn(B, 1)
2557 B.setDependsOn(A, 1)
2558 A.setDependsOn(M, 1)
2559
2560 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2561 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2562 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2563 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2564 self.waitUntilSettled()
2565
2566 B.addPatchset()
2567 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2568 self.waitUntilSettled()
2569
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002570 self.worker.hold_jobs_in_build = False
2571 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002572 self.waitUntilSettled()
2573
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002574 self.assertEqual(A.data['status'], 'MERGED')
2575 self.assertEqual(A.reported, 2)
2576 self.assertEqual(B.data['status'], 'NEW')
2577 self.assertEqual(B.reported, 2)
2578 self.assertEqual(C.data['status'], 'NEW')
2579 self.assertEqual(C.reported, 2)
2580 self.assertEqual(D.data['status'], 'MERGED')
2581 self.assertEqual(D.reported, 2)
2582 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002583
Arx Cruzb1b010d2013-10-28 19:49:59 -02002584 def test_zuul_url_return(self):
2585 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08002586 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Arx Cruzb1b010d2013-10-28 19:49:59 -02002587 self.worker.hold_jobs_in_build = True
2588
2589 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2590 A.addApproval('CRVW', 2)
2591 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2592 self.waitUntilSettled()
2593
2594 self.assertEqual(len(self.builds), 1)
2595 for build in self.builds:
2596 self.assertTrue('ZUUL_URL' in build.parameters)
2597
2598 self.worker.hold_jobs_in_build = False
2599 self.worker.release()
2600 self.waitUntilSettled()
2601
James E. Blair2fa50962013-01-30 21:50:41 -08002602 def test_new_patchset_dequeues_old_on_head(self):
2603 "Test that a new patchset causes the old to be dequeued (at head)"
2604 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002605 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002606 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2607 M.setMerged()
2608 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2609 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2610 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2611 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2612 A.addApproval('CRVW', 2)
2613 B.addApproval('CRVW', 2)
2614 C.addApproval('CRVW', 2)
2615 D.addApproval('CRVW', 2)
2616
2617 C.setDependsOn(B, 1)
2618 B.setDependsOn(A, 1)
2619 A.setDependsOn(M, 1)
2620
2621 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2622 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2623 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2624 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2625 self.waitUntilSettled()
2626
2627 A.addPatchset()
2628 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2629 self.waitUntilSettled()
2630
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002631 self.worker.hold_jobs_in_build = False
2632 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002633 self.waitUntilSettled()
2634
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002635 self.assertEqual(A.data['status'], 'NEW')
2636 self.assertEqual(A.reported, 2)
2637 self.assertEqual(B.data['status'], 'NEW')
2638 self.assertEqual(B.reported, 2)
2639 self.assertEqual(C.data['status'], 'NEW')
2640 self.assertEqual(C.reported, 2)
2641 self.assertEqual(D.data['status'], 'MERGED')
2642 self.assertEqual(D.reported, 2)
2643 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002644
2645 def test_new_patchset_dequeues_old_without_dependents(self):
2646 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002647 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002648 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2649 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2650 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2651 A.addApproval('CRVW', 2)
2652 B.addApproval('CRVW', 2)
2653 C.addApproval('CRVW', 2)
2654
2655 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2656 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2657 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2658 self.waitUntilSettled()
2659
2660 B.addPatchset()
2661 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2662 self.waitUntilSettled()
2663
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002664 self.worker.hold_jobs_in_build = False
2665 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002666 self.waitUntilSettled()
2667
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002668 self.assertEqual(A.data['status'], 'MERGED')
2669 self.assertEqual(A.reported, 2)
2670 self.assertEqual(B.data['status'], 'NEW')
2671 self.assertEqual(B.reported, 2)
2672 self.assertEqual(C.data['status'], 'MERGED')
2673 self.assertEqual(C.reported, 2)
2674 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002675
2676 def test_new_patchset_dequeues_old_independent_queue(self):
2677 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002678 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002679 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2680 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2681 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2682 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2683 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2684 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2685 self.waitUntilSettled()
2686
2687 B.addPatchset()
2688 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2689 self.waitUntilSettled()
2690
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002691 self.worker.hold_jobs_in_build = False
2692 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002693 self.waitUntilSettled()
2694
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002695 self.assertEqual(A.data['status'], 'NEW')
2696 self.assertEqual(A.reported, 1)
2697 self.assertEqual(B.data['status'], 'NEW')
2698 self.assertEqual(B.reported, 1)
2699 self.assertEqual(C.data['status'], 'NEW')
2700 self.assertEqual(C.reported, 1)
2701 self.assertEqual(len(self.history), 10)
2702 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002703
James E. Blair18c64442014-03-18 10:14:45 -07002704 def test_noop_job(self):
2705 "Test that the internal noop job works"
2706 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
2707 A.addApproval('CRVW', 2)
2708 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2709 self.waitUntilSettled()
2710
2711 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2712 self.assertTrue(self.sched._areAllBuildsComplete())
2713 self.assertEqual(len(self.history), 0)
2714 self.assertEqual(A.data['status'], 'MERGED')
2715 self.assertEqual(A.reported, 2)
2716
James E. Blair7d0dedc2013-02-21 17:26:09 -08002717 def test_zuul_refs(self):
2718 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002719 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002720 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2721 M1.setMerged()
2722 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2723 M2.setMerged()
2724
2725 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2726 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2727 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2728 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2729 A.addApproval('CRVW', 2)
2730 B.addApproval('CRVW', 2)
2731 C.addApproval('CRVW', 2)
2732 D.addApproval('CRVW', 2)
2733 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2734 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2735 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2736 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2737
2738 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002739 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002740 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002741 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002742 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002743 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002744 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002745 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002746 self.waitUntilSettled()
2747
James E. Blair7d0dedc2013-02-21 17:26:09 -08002748 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002749 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002750 if x.parameters['ZUUL_CHANGE'] == '3':
2751 a_zref = x.parameters['ZUUL_REF']
2752 if x.parameters['ZUUL_CHANGE'] == '4':
2753 b_zref = x.parameters['ZUUL_REF']
2754 if x.parameters['ZUUL_CHANGE'] == '5':
2755 c_zref = x.parameters['ZUUL_REF']
2756 if x.parameters['ZUUL_CHANGE'] == '6':
2757 d_zref = x.parameters['ZUUL_REF']
2758
2759 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002760 self.assertIsNotNone(a_zref)
2761 self.assertIsNotNone(b_zref)
2762 self.assertIsNotNone(c_zref)
2763 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002764
2765 # And they should all be different
2766 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002767 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002768
2769 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002770 self.assertTrue(self.ref_has_change(a_zref, A))
2771 self.assertFalse(self.ref_has_change(a_zref, B))
2772 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002773
2774 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002775 self.assertTrue(self.ref_has_change(b_zref, A))
2776 self.assertTrue(self.ref_has_change(b_zref, B))
2777 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002778
2779 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002780 self.assertTrue(self.ref_has_change(c_zref, A))
2781 self.assertTrue(self.ref_has_change(c_zref, B))
2782 self.assertTrue(self.ref_has_change(c_zref, C))
2783 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002784
2785 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002786 self.assertTrue(self.ref_has_change(d_zref, A))
2787 self.assertTrue(self.ref_has_change(d_zref, B))
2788 self.assertTrue(self.ref_has_change(d_zref, C))
2789 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002790
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002791 self.worker.hold_jobs_in_build = False
2792 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002793 self.waitUntilSettled()
2794
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002795 self.assertEqual(A.data['status'], 'MERGED')
2796 self.assertEqual(A.reported, 2)
2797 self.assertEqual(B.data['status'], 'MERGED')
2798 self.assertEqual(B.reported, 2)
2799 self.assertEqual(C.data['status'], 'MERGED')
2800 self.assertEqual(C.reported, 2)
2801 self.assertEqual(D.data['status'], 'MERGED')
2802 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002803
James E. Blair11041d22014-05-02 14:49:53 -07002804 def test_pipeline_requirements_approval_check_and_gate(self):
2805 "Test pipeline requirements triggers both check and gate"
2806 self.config.set('zuul', 'layout_config',
2807 'tests/fixtures/layout-pipeline-requirements.yaml')
2808 self.sched.reconfigure(self.config)
2809 self.registerJobs()
2810 self._test_required_approval_check_and_gate()
2811
James E. Blairc053d022014-01-22 14:57:33 -08002812 def test_required_approval_check_and_gate(self):
2813 "Test required-approval triggers both check and gate"
2814 self.config.set('zuul', 'layout_config',
2815 'tests/fixtures/layout-require-approval.yaml')
2816 self.sched.reconfigure(self.config)
2817 self.registerJobs()
James E. Blair11041d22014-05-02 14:49:53 -07002818 self._test_required_approval_check_and_gate()
James E. Blairc053d022014-01-22 14:57:33 -08002819
James E. Blair11041d22014-05-02 14:49:53 -07002820 def _test_required_approval_check_and_gate(self):
James E. Blairc053d022014-01-22 14:57:33 -08002821 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2822 A.addApproval('CRVW', 2)
2823 # Add a too-old +1
2824 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2825
2826 aprv = A.addApproval('APRV', 1)
2827 self.fake_gerrit.addEvent(aprv)
2828 self.waitUntilSettled()
2829 # Should have run a check job
2830 self.assertEqual(len(self.history), 1)
2831 self.assertEqual(self.history[0].name, 'project-check')
2832
2833 # Report the result of that check job (overrides previous vrfy)
2834 # Skynet alert: this should trigger a gate job now that
2835 # all reqs are met
2836 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2837 self.waitUntilSettled()
2838 self.assertEqual(len(self.history), 2)
2839 self.assertEqual(self.history[1].name, 'project-gate')
2840
James E. Blair11041d22014-05-02 14:49:53 -07002841 def test_pipeline_requirements_approval_newer(self):
2842 "Test pipeline requirements newer trigger parameter"
2843 self.config.set('zuul', 'layout_config',
2844 'tests/fixtures/layout-pipeline-requirements.yaml')
2845 self.sched.reconfigure(self.config)
2846 self.registerJobs()
2847 self._test_required_approval_newer()
2848
James E. Blairc053d022014-01-22 14:57:33 -08002849 def test_required_approval_newer(self):
2850 "Test required-approval newer trigger parameter"
2851 self.config.set('zuul', 'layout_config',
2852 'tests/fixtures/layout-require-approval.yaml')
2853 self.sched.reconfigure(self.config)
2854 self.registerJobs()
James E. Blair11041d22014-05-02 14:49:53 -07002855 self._test_required_approval_newer()
2856
2857 def _test_required_approval_newer(self):
2858 self.config.set('zuul', 'layout_config',
2859 'tests/fixtures/layout-require-approval.yaml')
2860 self.sched.reconfigure(self.config)
2861 self.registerJobs()
James E. Blairc053d022014-01-22 14:57:33 -08002862
2863 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2864 A.addApproval('CRVW', 2)
2865 aprv = A.addApproval('APRV', 1)
2866 self.fake_gerrit.addEvent(aprv)
2867 self.waitUntilSettled()
2868 # No +1 from Jenkins so should not be enqueued
2869 self.assertEqual(len(self.history), 0)
2870
2871 # Add a too-old +1, should trigger check but not gate
2872 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2873 self.fake_gerrit.addEvent(aprv)
2874 self.waitUntilSettled()
2875 self.assertEqual(len(self.history), 1)
2876 self.assertEqual(self.history[0].name, 'project-check')
2877
2878 # Add a recent +1
2879 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2880 self.fake_gerrit.addEvent(aprv)
2881 self.waitUntilSettled()
2882 self.assertEqual(len(self.history), 2)
2883 self.assertEqual(self.history[1].name, 'project-gate')
2884
James E. Blair11041d22014-05-02 14:49:53 -07002885 def test_pipeline_requirements_approval_older(self):
2886 "Test pipeline requirements older trigger parameter"
2887 self.config.set('zuul', 'layout_config',
2888 'tests/fixtures/layout-pipeline-requirements.yaml')
2889 self.sched.reconfigure(self.config)
2890 self.registerJobs()
2891 self._test_required_approval_older()
2892
James E. Blairc053d022014-01-22 14:57:33 -08002893 def test_required_approval_older(self):
2894 "Test required-approval older trigger parameter"
2895 self.config.set('zuul', 'layout_config',
2896 'tests/fixtures/layout-require-approval.yaml')
2897 self.sched.reconfigure(self.config)
2898 self.registerJobs()
James E. Blair11041d22014-05-02 14:49:53 -07002899 self._test_required_approval_older()
2900
2901 def _test_required_approval_older(self):
2902 self.config.set('zuul', 'layout_config',
2903 'tests/fixtures/layout-require-approval.yaml')
2904 self.sched.reconfigure(self.config)
2905 self.registerJobs()
James E. Blairc053d022014-01-22 14:57:33 -08002906
2907 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2908 crvw = A.addApproval('CRVW', 2)
2909 self.fake_gerrit.addEvent(crvw)
2910 self.waitUntilSettled()
2911 # No +1 from Jenkins so should not be enqueued
2912 self.assertEqual(len(self.history), 0)
2913
2914 # Add an old +1 and trigger check with a comment
2915 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2916 self.fake_gerrit.addEvent(crvw)
2917 self.waitUntilSettled()
2918 self.assertEqual(len(self.history), 1)
2919 self.assertEqual(self.history[0].name, 'project-check')
2920
2921 # Add a recent +1 and make sure nothing changes
2922 A.addApproval('VRFY', 1)
2923 self.fake_gerrit.addEvent(crvw)
2924 self.waitUntilSettled()
2925 self.assertEqual(len(self.history), 1)
2926
2927 # The last thing we did was query a change then do nothing
2928 # with a pipeline, so it will be in the cache; clean it up so
2929 # it does not fail the test.
2930 for pipeline in self.sched.layout.pipelines.values():
2931 pipeline.trigger.maintainCache([])
2932
James E. Blair4a28a882013-08-23 15:17:33 -07002933 def test_rerun_on_error(self):
2934 "Test that if a worker fails to run a job, it is run again"
2935 self.worker.hold_jobs_in_build = True
2936 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2937 A.addApproval('CRVW', 2)
2938 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2939 self.waitUntilSettled()
2940
2941 self.builds[0].run_error = True
2942 self.worker.hold_jobs_in_build = False
2943 self.worker.release()
2944 self.waitUntilSettled()
2945 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2946 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2947
James E. Blair412e5582013-04-22 15:50:12 -07002948 def test_statsd(self):
2949 "Test each of the statsd methods used in the scheduler"
2950 import extras
2951 statsd = extras.try_import('statsd.statsd')
2952 statsd.incr('test-incr')
2953 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002954 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002955 self.assertReportedStat('test-incr', '1|c')
2956 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002957 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002958
James E. Blairdad52252014-02-07 16:59:17 -08002959 def test_stuck_job_cleanup(self):
2960 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07002961 # This job won't be registered at startup because it is not in
2962 # the standard layout, but we need it to already be registerd
2963 # for when we reconfigure, as that is when Zuul will attempt
2964 # to run the new job.
2965 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002966 self.gearman_server.hold_jobs_in_queue = True
2967 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2968 A.addApproval('CRVW', 2)
2969 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2970 self.waitUntilSettled()
2971 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2972
2973 self.config.set('zuul', 'layout_config',
2974 'tests/fixtures/layout-no-jobs.yaml')
2975 self.sched.reconfigure(self.config)
2976 self.waitUntilSettled()
2977
James E. Blair18c64442014-03-18 10:14:45 -07002978 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002979 self.waitUntilSettled()
2980 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2981 self.assertTrue(self.sched._areAllBuildsComplete())
2982
2983 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002984 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002985 self.assertEqual(self.history[0].result, 'SUCCESS')
2986
James E. Blair70c71582013-03-06 08:50:50 -08002987 def test_file_jobs(self):
2988 "Test that file jobs run only when appropriate"
2989 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2990 A.addPatchset(['pip-requires'])
2991 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2992 A.addApproval('CRVW', 2)
2993 B.addApproval('CRVW', 2)
2994 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2995 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2996 self.waitUntilSettled()
2997
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002998 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002999 if x.name == 'project-testfile']
3000
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003001 self.assertEqual(len(testfile_jobs), 1)
3002 self.assertEqual(testfile_jobs[0].changes, '1,2')
3003 self.assertEqual(A.data['status'], 'MERGED')
3004 self.assertEqual(A.reported, 2)
3005 self.assertEqual(B.data['status'], 'MERGED')
3006 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07003007
3008 def test_test_config(self):
3009 "Test that we can test the config"
3010 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07003011 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07003012 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07003013 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003014
3015 def test_build_description(self):
3016 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003017 self.worker.registerFunction('set_description:' +
3018 self.worker.worker_id)
3019
3020 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3021 A.addApproval('CRVW', 2)
3022 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3023 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04003024 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003025 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003026 self.assertTrue(re.search("Branch.*master", desc))
3027 self.assertTrue(re.search("Pipeline.*gate", desc))
3028 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
3029 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
3030 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
3031 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003032
James E. Blairc8a1e052014-02-25 09:29:26 -08003033 def test_queue_names(self):
3034 "Test shared change queue names"
3035 project1 = self.sched.layout.projects['org/project1']
3036 project2 = self.sched.layout.projects['org/project2']
3037 q1 = self.sched.layout.pipelines['gate'].getQueue(project1)
3038 q2 = self.sched.layout.pipelines['gate'].getQueue(project2)
3039 self.assertEqual(q1.name, 'integration')
3040 self.assertEqual(q2.name, 'integration')
3041
3042 self.config.set('zuul', 'layout_config',
3043 'tests/fixtures/layout-bad-queue.yaml')
3044 with testtools.ExpectedException(
3045 Exception, "More than one name assigned to change queue"):
3046 self.sched.reconfigure(self.config)
3047
James E. Blair64ed6f22013-07-10 14:07:23 -07003048 def test_queue_precedence(self):
3049 "Test that queue precedence works"
3050
3051 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07003052 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07003053 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3054 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3055 A.addApproval('CRVW', 2)
3056 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3057
3058 self.waitUntilSettled()
3059 self.gearman_server.hold_jobs_in_queue = False
3060 self.gearman_server.release()
3061 self.waitUntilSettled()
3062
James E. Blair8de58bd2013-07-18 16:23:33 -07003063 # Run one build at a time to ensure non-race order:
3064 for x in range(6):
3065 self.release(self.builds[0])
3066 self.waitUntilSettled()
3067 self.worker.hold_jobs_in_build = False
3068 self.waitUntilSettled()
3069
James E. Blair64ed6f22013-07-10 14:07:23 -07003070 self.log.debug(self.history)
3071 self.assertEqual(self.history[0].pipeline, 'gate')
3072 self.assertEqual(self.history[1].pipeline, 'check')
3073 self.assertEqual(self.history[2].pipeline, 'gate')
3074 self.assertEqual(self.history[3].pipeline, 'gate')
3075 self.assertEqual(self.history[4].pipeline, 'check')
3076 self.assertEqual(self.history[5].pipeline, 'check')
3077
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04003078 def test_json_status(self, compressed=False):
James E. Blair1843a552013-07-03 14:19:52 -07003079 "Test that we can retrieve JSON status info"
3080 self.worker.hold_jobs_in_build = True
3081 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3082 A.addApproval('CRVW', 2)
3083 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3084 self.waitUntilSettled()
3085
3086 port = self.webapp.server.socket.getsockname()[1]
3087
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04003088 req = urllib2.Request("http://localhost:%s/status.json" % port)
3089 if compressed:
3090 req.add_header("accept-encoding", "gzip")
3091 f = urllib2.urlopen(req)
James E. Blair1843a552013-07-03 14:19:52 -07003092 data = f.read()
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04003093 if compressed:
3094 gz = gzip.GzipFile(fileobj=StringIO(data))
3095 data = gz.read()
James E. Blair1843a552013-07-03 14:19:52 -07003096
3097 self.worker.hold_jobs_in_build = False
3098 self.worker.release()
3099 self.waitUntilSettled()
3100
3101 data = json.loads(data)
3102 status_jobs = set()
3103 for p in data['pipelines']:
3104 for q in p['change_queues']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08003105 if q['dependent']:
3106 self.assertEqual(q['window'], 20)
3107 else:
3108 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07003109 for head in q['heads']:
3110 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08003111 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003112 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07003113 for job in change['jobs']:
3114 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003115 self.assertIn('project-merge', status_jobs)
3116 self.assertIn('project-test1', status_jobs)
3117 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07003118
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04003119 def test_json_status_gzip(self):
3120 self.test_json_status(True)
3121
James E. Blairc3d428e2013-12-03 15:06:48 -08003122 def test_merging_queues(self):
3123 "Test that transitively-connected change queues are merged"
3124 self.config.set('zuul', 'layout_config',
3125 'tests/fixtures/layout-merge-queues.yaml')
3126 self.sched.reconfigure(self.config)
3127 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
3128
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003129 def test_node_label(self):
3130 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003131 self.worker.registerFunction('build:node-project-test1:debian')
3132
3133 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
3134 A.addApproval('CRVW', 2)
3135 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3136 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07003137
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003138 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3139 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3140 'debian')
3141 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07003142
3143 def test_live_reconfiguration(self):
3144 "Test that live reconfiguration works"
3145 self.worker.hold_jobs_in_build = True
3146 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3147 A.addApproval('CRVW', 2)
3148 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3149 self.waitUntilSettled()
3150
3151 self.sched.reconfigure(self.config)
3152
3153 self.worker.hold_jobs_in_build = False
3154 self.worker.release()
3155 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003156 self.assertEqual(self.getJobFromHistory('project-merge').result,
3157 'SUCCESS')
3158 self.assertEqual(self.getJobFromHistory('project-test1').result,
3159 'SUCCESS')
3160 self.assertEqual(self.getJobFromHistory('project-test2').result,
3161 'SUCCESS')
3162 self.assertEqual(A.data['status'], 'MERGED')
3163 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07003164
James E. Blaire712d9f2013-07-31 11:40:11 -07003165 def test_live_reconfiguration_functions(self):
3166 "Test live reconfiguration with a custom function"
3167 self.worker.registerFunction('build:node-project-test1:debian')
3168 self.worker.registerFunction('build:node-project-test1:wheezy')
3169 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
3170 A.addApproval('CRVW', 2)
3171 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3172 self.waitUntilSettled()
3173
3174 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3175 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3176 'debian')
3177 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
3178
3179 self.config.set('zuul', 'layout_config',
3180 'tests/fixtures/layout-live-'
3181 'reconfiguration-functions.yaml')
3182 self.sched.reconfigure(self.config)
3183 self.worker.build_history = []
3184
3185 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
3186 B.addApproval('CRVW', 2)
3187 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3188 self.waitUntilSettled()
3189
3190 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3191 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3192 'wheezy')
3193 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
3194
James E. Blair287c06d2013-07-24 10:39:30 -07003195 def test_delayed_repo_init(self):
3196 self.config.set('zuul', 'layout_config',
3197 'tests/fixtures/layout-delayed-repo-init.yaml')
3198 self.sched.reconfigure(self.config)
3199
3200 self.init_repo("org/new-project")
3201 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
3202
3203 A.addApproval('CRVW', 2)
3204 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3205 self.waitUntilSettled()
3206 self.assertEqual(self.getJobFromHistory('project-merge').result,
3207 'SUCCESS')
3208 self.assertEqual(self.getJobFromHistory('project-test1').result,
3209 'SUCCESS')
3210 self.assertEqual(self.getJobFromHistory('project-test2').result,
3211 'SUCCESS')
3212 self.assertEqual(A.data['status'], 'MERGED')
3213 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003214
Clark Boylan6dbbc482013-10-18 10:57:31 -07003215 def test_repo_deleted(self):
3216 self.config.set('zuul', 'layout_config',
3217 'tests/fixtures/layout-repo-deleted.yaml')
3218 self.sched.reconfigure(self.config)
3219
3220 self.init_repo("org/delete-project")
3221 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
3222
3223 A.addApproval('CRVW', 2)
3224 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3225 self.waitUntilSettled()
3226 self.assertEqual(self.getJobFromHistory('project-merge').result,
3227 'SUCCESS')
3228 self.assertEqual(self.getJobFromHistory('project-test1').result,
3229 'SUCCESS')
3230 self.assertEqual(self.getJobFromHistory('project-test2').result,
3231 'SUCCESS')
3232 self.assertEqual(A.data['status'], 'MERGED')
3233 self.assertEqual(A.reported, 2)
3234
3235 # Delete org/new-project zuul repo. Should be recloned.
3236 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
3237
3238 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
3239
3240 B.addApproval('CRVW', 2)
3241 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3242 self.waitUntilSettled()
3243 self.assertEqual(self.getJobFromHistory('project-merge').result,
3244 'SUCCESS')
3245 self.assertEqual(self.getJobFromHistory('project-test1').result,
3246 'SUCCESS')
3247 self.assertEqual(self.getJobFromHistory('project-test2').result,
3248 'SUCCESS')
3249 self.assertEqual(B.data['status'], 'MERGED')
3250 self.assertEqual(B.reported, 2)
3251
James E. Blair63bb0ef2013-07-29 17:14:51 -07003252 def test_timer(self):
3253 "Test that a periodic job is triggered"
3254 self.worker.hold_jobs_in_build = True
3255 self.config.set('zuul', 'layout_config',
3256 'tests/fixtures/layout-timer.yaml')
3257 self.sched.reconfigure(self.config)
3258 self.registerJobs()
3259
3260 start = time.time()
3261 failed = True
3262 while ((time.time() - start) < 30):
3263 if len(self.builds) == 2:
3264 failed = False
3265 break
3266 else:
3267 time.sleep(1)
3268
3269 if failed:
3270 raise Exception("Expected jobs never ran")
3271
3272 self.waitUntilSettled()
3273 port = self.webapp.server.socket.getsockname()[1]
3274
3275 f = urllib.urlopen("http://localhost:%s/status.json" % port)
3276 data = f.read()
3277
3278 self.worker.hold_jobs_in_build = False
3279 self.worker.release()
3280 self.waitUntilSettled()
3281
3282 self.assertEqual(self.getJobFromHistory(
3283 'project-bitrot-stable-old').result, 'SUCCESS')
3284 self.assertEqual(self.getJobFromHistory(
3285 'project-bitrot-stable-older').result, 'SUCCESS')
3286
3287 data = json.loads(data)
3288 status_jobs = set()
3289 for p in data['pipelines']:
3290 for q in p['change_queues']:
3291 for head in q['heads']:
3292 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07003293 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003294 for job in change['jobs']:
3295 status_jobs.add(job['name'])
3296 self.assertIn('project-bitrot-stable-old', status_jobs)
3297 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003298
James E. Blair4f6033c2014-03-27 15:49:09 -07003299 def test_idle(self):
3300 "Test that frequent periodic jobs work"
3301 self.worker.hold_jobs_in_build = True
3302 self.config.set('zuul', 'layout_config',
3303 'tests/fixtures/layout-idle.yaml')
3304 self.sched.reconfigure(self.config)
3305 self.registerJobs()
3306
3307 # The pipeline triggers every second, so we should have seen
3308 # several by now.
3309 time.sleep(5)
3310 self.waitUntilSettled()
3311 self.assertEqual(len(self.builds), 2)
3312 self.worker.release('.*')
3313 self.waitUntilSettled()
3314 self.assertEqual(len(self.builds), 0)
3315 self.assertEqual(len(self.history), 2)
3316
3317 time.sleep(5)
3318 self.waitUntilSettled()
3319 self.assertEqual(len(self.builds), 2)
3320 self.assertEqual(len(self.history), 2)
3321 self.worker.release('.*')
3322 self.waitUntilSettled()
3323 self.assertEqual(len(self.builds), 0)
3324 self.assertEqual(len(self.history), 4)
3325
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003326 def test_check_smtp_pool(self):
3327 self.config.set('zuul', 'layout_config',
3328 'tests/fixtures/layout-smtp.yaml')
3329 self.sched.reconfigure(self.config)
3330
3331 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3332 self.waitUntilSettled()
3333
3334 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3335 self.waitUntilSettled()
3336
James E. Blairff80a2f2013-12-27 13:24:06 -08003337 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003338
3339 # A.messages only holds what FakeGerrit places in it. Thus we
3340 # work on the knowledge of what the first message should be as
3341 # it is only configured to go to SMTP.
3342
3343 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003344 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003345 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003346 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003347 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003348 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003349
3350 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003351 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003352 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003353 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003354 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003355 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003356
James E. Blaire5910202013-12-27 09:50:31 -08003357 def test_timer_smtp(self):
3358 "Test that a periodic job is triggered"
3359 self.config.set('zuul', 'layout_config',
3360 'tests/fixtures/layout-timer-smtp.yaml')
3361 self.sched.reconfigure(self.config)
3362 self.registerJobs()
3363
3364 start = time.time()
3365 failed = True
3366 while ((time.time() - start) < 30):
3367 if len(self.history) == 2:
3368 failed = False
3369 break
3370 else:
3371 time.sleep(1)
3372
3373 if failed:
3374 raise Exception("Expected jobs never ran")
3375
3376 self.waitUntilSettled()
3377
3378 self.assertEqual(self.getJobFromHistory(
3379 'project-bitrot-stable-old').result, 'SUCCESS')
3380 self.assertEqual(self.getJobFromHistory(
3381 'project-bitrot-stable-older').result, 'SUCCESS')
3382
James E. Blairff80a2f2013-12-27 13:24:06 -08003383 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003384
3385 # A.messages only holds what FakeGerrit places in it. Thus we
3386 # work on the knowledge of what the first message should be as
3387 # it is only configured to go to SMTP.
3388
3389 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003390 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003391 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003392 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003393 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003394 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003395
James E. Blairad28e912013-11-27 10:43:22 -08003396 def test_client_enqueue(self):
3397 "Test that the RPC client can enqueue a change"
3398 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3399 A.addApproval('CRVW', 2)
3400 A.addApproval('APRV', 1)
3401
3402 client = zuul.rpcclient.RPCClient('127.0.0.1',
3403 self.gearman_server.port)
3404 r = client.enqueue(pipeline='gate',
3405 project='org/project',
3406 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003407 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003408 self.waitUntilSettled()
3409 self.assertEqual(self.getJobFromHistory('project-merge').result,
3410 'SUCCESS')
3411 self.assertEqual(self.getJobFromHistory('project-test1').result,
3412 'SUCCESS')
3413 self.assertEqual(self.getJobFromHistory('project-test2').result,
3414 'SUCCESS')
3415 self.assertEqual(A.data['status'], 'MERGED')
3416 self.assertEqual(A.reported, 2)
3417 self.assertEqual(r, True)
3418
3419 def test_client_enqueue_negative(self):
3420 "Test that the RPC client returns errors"
3421 client = zuul.rpcclient.RPCClient('127.0.0.1',
3422 self.gearman_server.port)
3423 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3424 "Invalid project"):
3425 r = client.enqueue(pipeline='gate',
3426 project='project-does-not-exist',
3427 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003428 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003429 client.shutdown()
3430 self.assertEqual(r, False)
3431
3432 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3433 "Invalid pipeline"):
3434 r = client.enqueue(pipeline='pipeline-does-not-exist',
3435 project='org/project',
3436 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003437 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003438 client.shutdown()
3439 self.assertEqual(r, False)
3440
3441 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3442 "Invalid trigger"):
3443 r = client.enqueue(pipeline='gate',
3444 project='org/project',
3445 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003446 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003447 client.shutdown()
3448 self.assertEqual(r, False)
3449
3450 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3451 "Invalid change"):
3452 r = client.enqueue(pipeline='gate',
3453 project='org/project',
3454 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003455 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003456 client.shutdown()
3457 self.assertEqual(r, False)
3458
3459 self.waitUntilSettled()
3460 self.assertEqual(len(self.history), 0)
3461 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003462
3463 def test_client_promote(self):
3464 "Test that the RPC client can promote a change"
3465 self.worker.hold_jobs_in_build = True
3466 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3467 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3468 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3469 A.addApproval('CRVW', 2)
3470 B.addApproval('CRVW', 2)
3471 C.addApproval('CRVW', 2)
3472
3473 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3474 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3475 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3476
3477 self.waitUntilSettled()
3478
Sean Daguef39b9ca2014-01-10 21:34:35 -05003479 items = self.sched.layout.pipelines['gate'].getAllItems()
3480 enqueue_times = {}
3481 for item in items:
3482 enqueue_times[str(item.change)] = item.enqueue_time
3483
James E. Blair36658cf2013-12-06 17:53:48 -08003484 client = zuul.rpcclient.RPCClient('127.0.0.1',
3485 self.gearman_server.port)
3486 r = client.promote(pipeline='gate',
3487 change_ids=['2,1', '3,1'])
3488
Sean Daguef39b9ca2014-01-10 21:34:35 -05003489 # ensure that enqueue times are durable
3490 items = self.sched.layout.pipelines['gate'].getAllItems()
3491 for item in items:
3492 self.assertEqual(
3493 enqueue_times[str(item.change)], item.enqueue_time)
3494
James E. Blair78acec92014-02-06 07:11:32 -08003495 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003496 self.worker.release('.*-merge')
3497 self.waitUntilSettled()
3498 self.worker.release('.*-merge')
3499 self.waitUntilSettled()
3500 self.worker.release('.*-merge')
3501 self.waitUntilSettled()
3502
3503 self.assertEqual(len(self.builds), 6)
3504 self.assertEqual(self.builds[0].name, 'project-test1')
3505 self.assertEqual(self.builds[1].name, 'project-test2')
3506 self.assertEqual(self.builds[2].name, 'project-test1')
3507 self.assertEqual(self.builds[3].name, 'project-test2')
3508 self.assertEqual(self.builds[4].name, 'project-test1')
3509 self.assertEqual(self.builds[5].name, 'project-test2')
3510
3511 self.assertTrue(self.job_has_changes(self.builds[0], B))
3512 self.assertFalse(self.job_has_changes(self.builds[0], A))
3513 self.assertFalse(self.job_has_changes(self.builds[0], C))
3514
3515 self.assertTrue(self.job_has_changes(self.builds[2], B))
3516 self.assertTrue(self.job_has_changes(self.builds[2], C))
3517 self.assertFalse(self.job_has_changes(self.builds[2], A))
3518
3519 self.assertTrue(self.job_has_changes(self.builds[4], B))
3520 self.assertTrue(self.job_has_changes(self.builds[4], C))
3521 self.assertTrue(self.job_has_changes(self.builds[4], A))
3522
3523 self.worker.release()
3524 self.waitUntilSettled()
3525
3526 self.assertEqual(A.data['status'], 'MERGED')
3527 self.assertEqual(A.reported, 2)
3528 self.assertEqual(B.data['status'], 'MERGED')
3529 self.assertEqual(B.reported, 2)
3530 self.assertEqual(C.data['status'], 'MERGED')
3531 self.assertEqual(C.reported, 2)
3532
3533 client.shutdown()
3534 self.assertEqual(r, True)
3535
3536 def test_client_promote_dependent(self):
3537 "Test that the RPC client can promote a dependent change"
3538 # C (depends on B) -> B -> A ; then promote C to get:
3539 # A -> C (depends on B) -> B
3540 self.worker.hold_jobs_in_build = True
3541 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3542 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3543 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3544
3545 C.setDependsOn(B, 1)
3546
3547 A.addApproval('CRVW', 2)
3548 B.addApproval('CRVW', 2)
3549 C.addApproval('CRVW', 2)
3550
3551 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3552 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3553 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3554
3555 self.waitUntilSettled()
3556
3557 client = zuul.rpcclient.RPCClient('127.0.0.1',
3558 self.gearman_server.port)
3559 r = client.promote(pipeline='gate',
3560 change_ids=['3,1'])
3561
James E. Blair78acec92014-02-06 07:11:32 -08003562 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003563 self.worker.release('.*-merge')
3564 self.waitUntilSettled()
3565 self.worker.release('.*-merge')
3566 self.waitUntilSettled()
3567 self.worker.release('.*-merge')
3568 self.waitUntilSettled()
3569
3570 self.assertEqual(len(self.builds), 6)
3571 self.assertEqual(self.builds[0].name, 'project-test1')
3572 self.assertEqual(self.builds[1].name, 'project-test2')
3573 self.assertEqual(self.builds[2].name, 'project-test1')
3574 self.assertEqual(self.builds[3].name, 'project-test2')
3575 self.assertEqual(self.builds[4].name, 'project-test1')
3576 self.assertEqual(self.builds[5].name, 'project-test2')
3577
3578 self.assertTrue(self.job_has_changes(self.builds[0], B))
3579 self.assertFalse(self.job_has_changes(self.builds[0], A))
3580 self.assertFalse(self.job_has_changes(self.builds[0], C))
3581
3582 self.assertTrue(self.job_has_changes(self.builds[2], B))
3583 self.assertTrue(self.job_has_changes(self.builds[2], C))
3584 self.assertFalse(self.job_has_changes(self.builds[2], A))
3585
3586 self.assertTrue(self.job_has_changes(self.builds[4], B))
3587 self.assertTrue(self.job_has_changes(self.builds[4], C))
3588 self.assertTrue(self.job_has_changes(self.builds[4], A))
3589
3590 self.worker.release()
3591 self.waitUntilSettled()
3592
3593 self.assertEqual(A.data['status'], 'MERGED')
3594 self.assertEqual(A.reported, 2)
3595 self.assertEqual(B.data['status'], 'MERGED')
3596 self.assertEqual(B.reported, 2)
3597 self.assertEqual(C.data['status'], 'MERGED')
3598 self.assertEqual(C.reported, 2)
3599
3600 client.shutdown()
3601 self.assertEqual(r, True)
3602
3603 def test_client_promote_negative(self):
3604 "Test that the RPC client returns errors for promotion"
3605 self.worker.hold_jobs_in_build = True
3606 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3607 A.addApproval('CRVW', 2)
3608 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3609 self.waitUntilSettled()
3610
3611 client = zuul.rpcclient.RPCClient('127.0.0.1',
3612 self.gearman_server.port)
3613
3614 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3615 r = client.promote(pipeline='nonexistent',
3616 change_ids=['2,1', '3,1'])
3617 client.shutdown()
3618 self.assertEqual(r, False)
3619
3620 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3621 r = client.promote(pipeline='gate',
3622 change_ids=['4,1'])
3623 client.shutdown()
3624 self.assertEqual(r, False)
3625
3626 self.worker.hold_jobs_in_build = False
3627 self.worker.release()
3628 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003629
3630 def test_queue_rate_limiting(self):
3631 "Test that DependentPipelines are rate limited with dep across window"
3632 self.config.set('zuul', 'layout_config',
3633 'tests/fixtures/layout-rate-limit.yaml')
3634 self.sched.reconfigure(self.config)
3635 self.worker.hold_jobs_in_build = True
3636 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3637 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3638 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3639
3640 C.setDependsOn(B, 1)
3641 self.worker.addFailTest('project-test1', A)
3642
3643 A.addApproval('CRVW', 2)
3644 B.addApproval('CRVW', 2)
3645 C.addApproval('CRVW', 2)
3646
3647 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3648 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3649 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3650 self.waitUntilSettled()
3651
3652 # Only A and B will have their merge jobs queued because
3653 # window is 2.
3654 self.assertEqual(len(self.builds), 2)
3655 self.assertEqual(self.builds[0].name, 'project-merge')
3656 self.assertEqual(self.builds[1].name, 'project-merge')
3657
3658 self.worker.release('.*-merge')
3659 self.waitUntilSettled()
3660 self.worker.release('.*-merge')
3661 self.waitUntilSettled()
3662
3663 # Only A and B will have their test jobs queued because
3664 # window is 2.
3665 self.assertEqual(len(self.builds), 4)
3666 self.assertEqual(self.builds[0].name, 'project-test1')
3667 self.assertEqual(self.builds[1].name, 'project-test2')
3668 self.assertEqual(self.builds[2].name, 'project-test1')
3669 self.assertEqual(self.builds[3].name, 'project-test2')
3670
3671 self.worker.release('project-.*')
3672 self.waitUntilSettled()
3673
3674 queue = self.sched.layout.pipelines['gate'].queues[0]
3675 # A failed so window is reduced by 1 to 1.
3676 self.assertEqual(queue.window, 1)
3677 self.assertEqual(queue.window_floor, 1)
3678 self.assertEqual(A.data['status'], 'NEW')
3679
3680 # Gate is reset and only B's merge job is queued because
3681 # window shrunk to 1.
3682 self.assertEqual(len(self.builds), 1)
3683 self.assertEqual(self.builds[0].name, 'project-merge')
3684
3685 self.worker.release('.*-merge')
3686 self.waitUntilSettled()
3687
3688 # Only B's test jobs are queued because window is still 1.
3689 self.assertEqual(len(self.builds), 2)
3690 self.assertEqual(self.builds[0].name, 'project-test1')
3691 self.assertEqual(self.builds[1].name, 'project-test2')
3692
3693 self.worker.release('project-.*')
3694 self.waitUntilSettled()
3695
3696 # B was successfully merged so window is increased to 2.
3697 self.assertEqual(queue.window, 2)
3698 self.assertEqual(queue.window_floor, 1)
3699 self.assertEqual(B.data['status'], 'MERGED')
3700
3701 # Only C is left and its merge job is queued.
3702 self.assertEqual(len(self.builds), 1)
3703 self.assertEqual(self.builds[0].name, 'project-merge')
3704
3705 self.worker.release('.*-merge')
3706 self.waitUntilSettled()
3707
3708 # After successful merge job the test jobs for C are queued.
3709 self.assertEqual(len(self.builds), 2)
3710 self.assertEqual(self.builds[0].name, 'project-test1')
3711 self.assertEqual(self.builds[1].name, 'project-test2')
3712
3713 self.worker.release('project-.*')
3714 self.waitUntilSettled()
3715
3716 # C successfully merged so window is bumped to 3.
3717 self.assertEqual(queue.window, 3)
3718 self.assertEqual(queue.window_floor, 1)
3719 self.assertEqual(C.data['status'], 'MERGED')
3720
3721 def test_queue_rate_limiting_dependent(self):
3722 "Test that DependentPipelines are rate limited with dep in window"
3723 self.config.set('zuul', 'layout_config',
3724 'tests/fixtures/layout-rate-limit.yaml')
3725 self.sched.reconfigure(self.config)
3726 self.worker.hold_jobs_in_build = True
3727 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3728 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3729 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3730
3731 B.setDependsOn(A, 1)
3732
3733 self.worker.addFailTest('project-test1', A)
3734
3735 A.addApproval('CRVW', 2)
3736 B.addApproval('CRVW', 2)
3737 C.addApproval('CRVW', 2)
3738
3739 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3740 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3741 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3742 self.waitUntilSettled()
3743
3744 # Only A and B will have their merge jobs queued because
3745 # window is 2.
3746 self.assertEqual(len(self.builds), 2)
3747 self.assertEqual(self.builds[0].name, 'project-merge')
3748 self.assertEqual(self.builds[1].name, 'project-merge')
3749
3750 self.worker.release('.*-merge')
3751 self.waitUntilSettled()
3752 self.worker.release('.*-merge')
3753 self.waitUntilSettled()
3754
3755 # Only A and B will have their test jobs queued because
3756 # window is 2.
3757 self.assertEqual(len(self.builds), 4)
3758 self.assertEqual(self.builds[0].name, 'project-test1')
3759 self.assertEqual(self.builds[1].name, 'project-test2')
3760 self.assertEqual(self.builds[2].name, 'project-test1')
3761 self.assertEqual(self.builds[3].name, 'project-test2')
3762
3763 self.worker.release('project-.*')
3764 self.waitUntilSettled()
3765
3766 queue = self.sched.layout.pipelines['gate'].queues[0]
3767 # A failed so window is reduced by 1 to 1.
3768 self.assertEqual(queue.window, 1)
3769 self.assertEqual(queue.window_floor, 1)
3770 self.assertEqual(A.data['status'], 'NEW')
3771 self.assertEqual(B.data['status'], 'NEW')
3772
3773 # Gate is reset and only C's merge job is queued because
3774 # window shrunk to 1 and A and B were dequeued.
3775 self.assertEqual(len(self.builds), 1)
3776 self.assertEqual(self.builds[0].name, 'project-merge')
3777
3778 self.worker.release('.*-merge')
3779 self.waitUntilSettled()
3780
3781 # Only C's test jobs are queued because window is still 1.
3782 self.assertEqual(len(self.builds), 2)
3783 self.assertEqual(self.builds[0].name, 'project-test1')
3784 self.assertEqual(self.builds[1].name, 'project-test2')
3785
3786 self.worker.release('project-.*')
3787 self.waitUntilSettled()
3788
3789 # C was successfully merged so window is increased to 2.
3790 self.assertEqual(queue.window, 2)
3791 self.assertEqual(queue.window_floor, 1)
3792 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003793
3794 def test_worker_update_metadata(self):
3795 "Test if a worker can send back metadata about itself"
3796 self.worker.hold_jobs_in_build = True
3797
3798 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3799 A.addApproval('CRVW', 2)
3800 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3801 self.waitUntilSettled()
3802
3803 self.assertEqual(len(self.launcher.builds), 1)
3804
3805 self.log.debug('Current builds:')
3806 self.log.debug(self.launcher.builds)
3807
3808 start = time.time()
3809 while True:
3810 if time.time() - start > 10:
3811 raise Exception("Timeout waiting for gearman server to report "
3812 + "back to the client")
3813 build = self.launcher.builds.values()[0]
3814 if build.worker.name == "My Worker":
3815 break
3816 else:
3817 time.sleep(0)
3818
3819 self.log.debug(build)
3820 self.assertEqual("My Worker", build.worker.name)
3821 self.assertEqual("localhost", build.worker.hostname)
3822 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3823 self.assertEqual("zuul.example.org", build.worker.fqdn)
3824 self.assertEqual("FakeBuilder", build.worker.program)
3825 self.assertEqual("v1.1", build.worker.version)
3826 self.assertEqual({'something': 'else'}, build.worker.extra)
3827
3828 self.worker.hold_jobs_in_build = False
3829 self.worker.release()
3830 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003831
3832 def test_footer_message(self):
3833 "Test a pipeline's footer message is correctly added to the report."
3834 self.config.set('zuul', 'layout_config',
3835 'tests/fixtures/layout-footer-message.yaml')
3836 self.sched.reconfigure(self.config)
3837 self.registerJobs()
3838
3839 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3840 A.addApproval('CRVW', 2)
3841 self.worker.addFailTest('test1', A)
3842 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3843 self.waitUntilSettled()
3844
3845 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3846 B.addApproval('CRVW', 2)
3847 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3848 self.waitUntilSettled()
3849
3850 self.assertEqual(2, len(self.smtp_messages))
3851
3852 failure_body = """\
3853Build failed. For information on how to proceed, see \
3854http://wiki.example.org/Test_Failures
3855
3856- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3857- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3858
3859For CI problems and help debugging, contact ci@example.org"""
3860
3861 success_body = """\
3862Build succeeded.
3863
3864- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3865- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3866
3867For CI problems and help debugging, contact ci@example.org"""
3868
3869 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3870 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11003871
3872 def test_merge_failure_reporters(self):
3873 """Check that the config is set up correctly"""
3874
3875 self.config.set('zuul', 'layout_config',
3876 'tests/fixtures/layout-merge-failure.yaml')
3877 self.sched.reconfigure(self.config)
3878 self.registerJobs()
3879
3880 self.assertEqual(
3881 "Merge Failed.\n\nThis change was unable to be automatically "
3882 "merged with the current state of the repository. Please rebase "
3883 "your change and upload a new patchset.",
3884 self.sched.layout.pipelines['check'].merge_failure_message)
3885 self.assertEqual(
3886 "The merge failed! For more information...",
3887 self.sched.layout.pipelines['gate'].merge_failure_message)
3888
3889 self.assertEqual(
3890 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3891 self.assertEqual(
3892 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3893
3894 self.assertTrue(isinstance(
3895 self.sched.layout.pipelines['check'].merge_failure_actions[0].
3896 reporter, zuul.reporter.gerrit.Reporter))
3897
3898 self.assertTrue(
3899 (
3900 isinstance(self.sched.layout.pipelines['gate'].
3901 merge_failure_actions[0].reporter,
3902 zuul.reporter.smtp.Reporter) and
3903 isinstance(self.sched.layout.pipelines['gate'].
3904 merge_failure_actions[1].reporter,
3905 zuul.reporter.gerrit.Reporter)
3906 ) or (
3907 isinstance(self.sched.layout.pipelines['gate'].
3908 merge_failure_actions[0].reporter,
3909 zuul.reporter.gerrit.Reporter) and
3910 isinstance(self.sched.layout.pipelines['gate'].
3911 merge_failure_actions[1].reporter,
3912 zuul.reporter.smtp.Reporter)
3913 )
3914 )
3915
3916 def test_merge_failure_reports(self):
3917 """Check that when a change fails to merge the correct message is sent
3918 to the correct reporter"""
3919 self.config.set('zuul', 'layout_config',
3920 'tests/fixtures/layout-merge-failure.yaml')
3921 self.sched.reconfigure(self.config)
3922 self.registerJobs()
3923
3924 # Check a test failure isn't reported to SMTP
3925 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3926 A.addApproval('CRVW', 2)
3927 self.worker.addFailTest('project-test1', A)
3928 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3929 self.waitUntilSettled()
3930
3931 self.assertEqual(3, len(self.history)) # 3 jobs
3932 self.assertEqual(0, len(self.smtp_messages))
3933
3934 # Check a merge failure is reported to SMTP
3935 # B should be merged, but C will conflict with B
3936 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3937 B.addPatchset(['conflict'])
3938 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3939 C.addPatchset(['conflict'])
3940 B.addApproval('CRVW', 2)
3941 C.addApproval('CRVW', 2)
3942 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3943 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3944 self.waitUntilSettled()
3945
3946 self.assertEqual(6, len(self.history)) # A and B jobs
3947 self.assertEqual(1, len(self.smtp_messages))
3948 self.assertEqual('The merge failed! For more information...',
3949 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003950
3951 def test_swift_instructions(self):
3952 "Test that the correct swift instructions are sent to the workers"
3953 self.config.set('zuul', 'layout_config',
3954 'tests/fixtures/layout-swift.yaml')
3955 self.sched.reconfigure(self.config)
3956 self.registerJobs()
3957
3958 self.worker.hold_jobs_in_build = True
3959 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3960
3961 A.addApproval('CRVW', 2)
3962 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3963 self.waitUntilSettled()
3964
3965 self.assertEqual(
3966 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
3967 "gate/test-merge/",
3968 self.builds[0].parameters['SWIFT_logs_URL'][:-32])
3969 self.assertEqual(5,
3970 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
3971 split('\n')))
3972 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
3973
3974 self.assertEqual(
3975 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
3976 "gate/test-test/",
3977 self.builds[1].parameters['SWIFT_logs_URL'][:-32])
3978 self.assertEqual(5,
3979 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
3980 split('\n')))
3981 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
3982
3983 self.assertEqual(
3984 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
3985 "gate/test-test/",
3986 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-32])
3987 self.assertEqual(5,
3988 len(self.builds[1].
3989 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
3990 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
3991
3992 self.worker.hold_jobs_in_build = False
3993 self.worker.release()
3994 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003995
3996 def test_client_get_running_jobs(self):
3997 "Test that the RPC client can get a list of running jobs"
3998 self.worker.hold_jobs_in_build = True
3999 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4000 A.addApproval('CRVW', 2)
4001 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
4002 self.waitUntilSettled()
4003
4004 client = zuul.rpcclient.RPCClient('127.0.0.1',
4005 self.gearman_server.port)
4006
4007 # Wait for gearman server to send the initial workData back to zuul
4008 start = time.time()
4009 while True:
4010 if time.time() - start > 10:
4011 raise Exception("Timeout waiting for gearman server to report "
4012 + "back to the client")
4013 build = self.launcher.builds.values()[0]
4014 if build.worker.name == "My Worker":
4015 break
4016 else:
4017 time.sleep(0)
4018
4019 running_items = client.get_running_jobs()
4020
4021 self.assertEqual(1, len(running_items))
4022 running_item = running_items[0]
4023 self.assertEqual([], running_item['failing_reasons'])
4024 self.assertEqual([], running_item['items_behind'])
4025 self.assertEqual('https://hostname/1', running_item['url'])
4026 self.assertEqual(None, running_item['item_ahead'])
4027 self.assertEqual('org/project', running_item['project'])
4028 self.assertEqual(None, running_item['remaining_time'])
4029 self.assertEqual(True, running_item['active'])
4030 self.assertEqual('1,1', running_item['id'])
4031
4032 self.assertEqual(3, len(running_item['jobs']))
4033 for job in running_item['jobs']:
4034 if job['name'] == 'project-merge':
4035 self.assertEqual('project-merge', job['name'])
4036 self.assertEqual('gate', job['pipeline'])
4037 self.assertEqual(False, job['retry'])
4038 self.assertEqual(13, len(job['parameters']))
4039 self.assertEqual('https://server/job/project-merge/0/',
4040 job['url'])
4041 self.assertEqual(7, len(job['worker']))
4042 self.assertEqual(False, job['canceled'])
4043 self.assertEqual(True, job['voting'])
4044 self.assertEqual(None, job['result'])
4045 self.assertEqual('gate', job['pipeline'])
4046 break
4047
4048 self.worker.hold_jobs_in_build = False
4049 self.worker.release()
4050 self.waitUntilSettled()
4051
4052 running_items = client.get_running_jobs()
4053 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07004054
4055 def test_nonvoting_pipeline(self):
4056 "Test that a nonvoting pipeline (experimental) can still report"
4057
Joshua Heskethcc017ea2014-04-30 19:55:25 +10004058 A = self.fake_gerrit.addFakeChange('org/experimental-project',
4059 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07004060 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4061 self.waitUntilSettled()
Joshua Heskethcc017ea2014-04-30 19:55:25 +10004062 self.assertEqual(
4063 self.getJobFromHistory('experimental-project-test').result,
4064 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07004065 self.assertEqual(A.reported, 1)