blob: f5070bb945f40ec29e13d2b6581b90b35658edf0 [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()
Clark Boylana9702ad2014-05-08 17:17:24 -0700135 self.open = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700136
Monty Taylorbc758832013-06-17 17:22:42 -0400137 def add_fake_change_to_repo(self, msg, fn, large):
138 path = os.path.join(self.upstream_root, self.project)
139 repo = git.Repo(path)
140 ref = ChangeReference.create(repo, '1/%s/%s' % (self.number,
141 self.latest_patchset),
142 'refs/tags/init')
143 repo.head.reference = ref
144 repo.head.reset(index=True, working_tree=True)
145 repo.git.clean('-x', '-f', '-d')
146
147 path = os.path.join(self.upstream_root, self.project)
148 if not large:
149 fn = os.path.join(path, fn)
150 f = open(fn, 'w')
151 f.write("test %s %s %s\n" %
152 (self.branch, self.number, self.latest_patchset))
153 f.close()
154 repo.index.add([fn])
155 else:
156 for fni in range(100):
157 fn = os.path.join(path, str(fni))
158 f = open(fn, 'w')
159 for ci in range(4096):
160 f.write(random.choice(string.printable))
161 f.close()
162 repo.index.add([fn])
163
James E. Blair287c06d2013-07-24 10:39:30 -0700164 r = repo.index.commit(msg)
165 repo.head.reference = 'master'
166 repo.head.reset(index=True, working_tree=True)
167 repo.git.clean('-x', '-f', '-d')
James E. Blairac2c3242014-01-24 13:38:51 -0800168 repo.heads['master'].checkout()
James E. Blair287c06d2013-07-24 10:39:30 -0700169 return r
Monty Taylorbc758832013-06-17 17:22:42 -0400170
James E. Blair70c71582013-03-06 08:50:50 -0800171 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700172 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700173 if files:
174 fn = files[0]
175 else:
176 fn = '%s-%s' % (self.branch, self.number)
177 msg = self.subject + '-' + str(self.latest_patchset)
Monty Taylorbc758832013-06-17 17:22:42 -0400178 c = self.add_fake_change_to_repo(msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800179 ps_files = [{'file': '/COMMIT_MSG',
180 'type': 'ADDED'},
181 {'file': 'README',
182 'type': 'MODIFIED'}]
183 for f in files:
184 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700185 d = {'approvals': [],
186 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800187 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700188 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700189 'ref': 'refs/changes/1/%s/%s' % (self.number,
190 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700191 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700192 'uploader': {'email': 'user@example.com',
193 'name': 'User name',
194 'username': 'user'}}
195 self.data['currentPatchSet'] = d
196 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700197 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700198
James E. Blaire0487072012-08-29 17:38:31 -0700199 def getPatchsetCreatedEvent(self, patchset):
200 event = {"type": "patchset-created",
201 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800202 "branch": self.branch,
203 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
204 "number": str(self.number),
205 "subject": self.subject,
206 "owner": {"name": "User Name"},
207 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700208 "patchSet": self.patchsets[patchset - 1],
209 "uploader": {"name": "User Name"}}
210 return event
211
James E. Blair42f74822013-05-14 15:18:03 -0700212 def getChangeRestoredEvent(self):
213 event = {"type": "change-restored",
214 "change": {"project": self.project,
215 "branch": self.branch,
216 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
217 "number": str(self.number),
218 "subject": self.subject,
219 "owner": {"name": "User Name"},
220 "url": "https://hostname/3"},
221 "restorer": {"name": "User Name"},
222 "reason": ""}
223 return event
224
Clark Boylana9702ad2014-05-08 17:17:24 -0700225 def getChangeCommentEvent(self, patchset):
226 event = {"type": "comment-added",
227 "change": {"project": self.project,
228 "branch": self.branch,
229 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
230 "number": str(self.number),
231 "subject": self.subject,
232 "owner": {"name": "User Name"},
233 "url": "https://hostname/3"},
234 "patchSet": self.patchsets[patchset - 1],
235 "author": {"name": "User Name"},
236 "approvals": [{"type": "Code-Review",
237 "description": "Code-Review",
238 "value": "0"}],
239 "comment": "This is a comment"}
240 return event
241
James E. Blairc053d022014-01-22 14:57:33 -0800242 def addApproval(self, category, value, username='jenkins',
243 granted_on=None):
244 if not granted_on:
245 granted_on = time.time()
James E. Blair8c803f82012-07-31 16:25:42 -0700246 approval = {'description': self.categories[category][0],
247 'type': category,
James E. Blairc053d022014-01-22 14:57:33 -0800248 'value': str(value),
James E. Blair64ff4ef2014-01-24 13:50:23 -0800249 'by': {
250 'username': username,
251 'email': username + '@example.com',
252 },
James E. Blairc053d022014-01-22 14:57:33 -0800253 'grantedOn': int(granted_on)}
254 for i, x in enumerate(self.patchsets[-1]['approvals'][:]):
James E. Blair64ff4ef2014-01-24 13:50:23 -0800255 if x['by']['username'] == username and x['type'] == category:
James E. Blairc053d022014-01-22 14:57:33 -0800256 del self.patchsets[-1]['approvals'][i]
James E. Blair8c803f82012-07-31 16:25:42 -0700257 self.patchsets[-1]['approvals'].append(approval)
258 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700259 'author': {'email': 'user@example.com',
260 'name': 'User Name',
261 'username': 'username'},
262 'change': {'branch': self.branch,
263 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
264 'number': str(self.number),
265 'owner': {'email': 'user@example.com',
266 'name': 'User Name',
267 'username': 'username'},
268 'project': self.project,
269 'subject': self.subject,
270 'topic': 'master',
271 'url': 'https://hostname/459'},
272 'comment': '',
273 'patchSet': self.patchsets[-1],
274 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700275 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700276 return json.loads(json.dumps(event))
277
James E. Blair8c803f82012-07-31 16:25:42 -0700278 def getSubmitRecords(self):
279 status = {}
280 for cat in self.categories.keys():
281 status[cat] = 0
282
283 for a in self.patchsets[-1]['approvals']:
284 cur = status[a['type']]
285 cat_min, cat_max = self.categories[a['type']][1:]
286 new = int(a['value'])
287 if new == cat_min:
288 cur = new
289 elif abs(new) > abs(cur):
290 cur = new
291 status[a['type']] = cur
292
293 labels = []
294 ok = True
295 for typ, cat in self.categories.items():
296 cur = status[typ]
297 cat_min, cat_max = cat[1:]
298 if cur == cat_min:
299 value = 'REJECT'
300 ok = False
301 elif cur == cat_max:
302 value = 'OK'
303 else:
304 value = 'NEED'
305 ok = False
306 labels.append({'label': cat[0], 'status': value})
307 if ok:
308 return [{'status': 'OK'}]
309 return [{'status': 'NOT_READY',
310 'labels': labels}]
311
312 def setDependsOn(self, other, patchset):
313 self.depends_on_change = other
314 d = {'id': other.data['id'],
315 'number': other.data['number'],
316 'ref': other.patchsets[patchset - 1]['ref']
317 }
318 self.data['dependsOn'] = [d]
319
320 other.needed_by_changes.append(self)
321 needed = other.data.get('neededBy', [])
322 d = {'id': self.data['id'],
323 'number': self.data['number'],
324 'ref': self.patchsets[patchset - 1]['ref'],
325 'revision': self.patchsets[patchset - 1]['revision']
326 }
327 needed.append(d)
328 other.data['neededBy'] = needed
329
James E. Blairb0fcae42012-07-17 11:12:10 -0700330 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700331 self.queried += 1
332 d = self.data.get('dependsOn')
333 if d:
334 d = d[0]
335 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
336 d['isCurrentPatchSet'] = True
337 else:
338 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700339 return json.loads(json.dumps(self.data))
340
341 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800342 if (self.depends_on_change and
343 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700344 return
James E. Blair127bc182012-08-28 15:55:15 -0700345 if self.fail_merge:
346 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700347 self.data['status'] = 'MERGED'
348 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700349
Monty Taylorbc758832013-06-17 17:22:42 -0400350 path = os.path.join(self.upstream_root, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700351 repo = git.Repo(path)
352 repo.heads[self.branch].commit = \
353 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700354
James E. Blaird466dc42012-07-31 10:42:56 -0700355 def setReported(self):
356 self.reported += 1
357
James E. Blairb0fcae42012-07-17 11:12:10 -0700358
359class FakeGerrit(object):
360 def __init__(self, *args, **kw):
361 self.event_queue = Queue.Queue()
362 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
363 self.change_number = 0
364 self.changes = {}
365
James E. Blair11041d22014-05-02 14:49:53 -0700366 def addFakeChange(self, project, branch, subject, status='NEW'):
James E. Blairb0fcae42012-07-17 11:12:10 -0700367 self.change_number += 1
Monty Taylorbc758832013-06-17 17:22:42 -0400368 c = FakeChange(self, self.change_number, project, branch, subject,
James E. Blair11041d22014-05-02 14:49:53 -0700369 upstream_root=self.upstream_root,
370 status=status)
James E. Blairb0fcae42012-07-17 11:12:10 -0700371 self.changes[self.change_number] = c
372 return c
373
374 def addEvent(self, data):
375 return self.event_queue.put(data)
376
377 def getEvent(self):
378 return self.event_queue.get()
379
380 def eventDone(self):
381 self.event_queue.task_done()
382
383 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700384 number, ps = changeid.split(',')
385 change = self.changes[int(number)]
James E. Blair42f74822013-05-14 15:18:03 -0700386 change.messages.append(message)
James E. Blairb0fcae42012-07-17 11:12:10 -0700387 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700388 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700389 if message:
390 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700391
392 def query(self, number):
James E. Blairad28e912013-11-27 10:43:22 -0800393 change = self.changes.get(int(number))
394 if change:
395 return change.query()
396 return {}
James E. Blairb0fcae42012-07-17 11:12:10 -0700397
398 def startWatching(self, *args, **kw):
399 pass
400
401
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700402class BuildHistory(object):
403 def __init__(self, **kw):
404 self.__dict__.update(kw)
James E. Blairb0fcae42012-07-17 11:12:10 -0700405
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700406 def __repr__(self):
407 return ("<Completed build, result: %s name: %s #%s changes: %s>" %
408 (self.result, self.name, self.number, self.changes))
James E. Blairb0fcae42012-07-17 11:12:10 -0700409
410
James E. Blair8cc15a82012-08-01 11:17:57 -0700411class FakeURLOpener(object):
Monty Taylorbc758832013-06-17 17:22:42 -0400412 def __init__(self, upstream_root, fake_gerrit, url):
413 self.upstream_root = upstream_root
James E. Blair8cc15a82012-08-01 11:17:57 -0700414 self.fake_gerrit = fake_gerrit
415 self.url = url
416
417 def read(self):
418 res = urlparse.urlparse(self.url)
419 path = res.path
420 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200421 ret = '001e# service=git-upload-pack\n'
422 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
423 'multi_ack thin-pack side-band side-band-64k ofs-delta '
424 'shallow no-progress include-tag multi_ack_detailed no-done\n')
Monty Taylorbc758832013-06-17 17:22:42 -0400425 path = os.path.join(self.upstream_root, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700426 repo = git.Repo(path)
427 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200428 r = ref.object.hexsha + ' ' + ref.path + '\n'
429 ret += '%04x%s' % (len(r) + 4, r)
430 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700431 return ret
432
433
James E. Blair4886cc12012-07-18 15:39:41 -0700434class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000435 name = 'gerrit'
436
Monty Taylorbc758832013-06-17 17:22:42 -0400437 def __init__(self, upstream_root, *args):
438 super(FakeGerritTrigger, self).__init__(*args)
439 self.upstream_root = upstream_root
440
James E. Blair4886cc12012-07-18 15:39:41 -0700441 def getGitUrl(self, project):
Monty Taylorbc758832013-06-17 17:22:42 -0400442 return os.path.join(self.upstream_root, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700443
444
James E. Blair412e5582013-04-22 15:50:12 -0700445class FakeStatsd(threading.Thread):
446 def __init__(self):
447 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400448 self.daemon = True
James E. Blair412e5582013-04-22 15:50:12 -0700449 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
450 self.sock.bind(('', 0))
451 self.port = self.sock.getsockname()[1]
452 self.wake_read, self.wake_write = os.pipe()
453 self.stats = []
454
455 def run(self):
456 while True:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700457 poll = select.poll()
458 poll.register(self.sock, select.POLLIN)
459 poll.register(self.wake_read, select.POLLIN)
460 ret = poll.poll()
461 for (fd, event) in ret:
462 if fd == self.sock.fileno():
James E. Blair412e5582013-04-22 15:50:12 -0700463 data = self.sock.recvfrom(1024)
464 if not data:
465 return
466 self.stats.append(data[0])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700467 if fd == self.wake_read:
James E. Blair412e5582013-04-22 15:50:12 -0700468 return
469
470 def stop(self):
471 os.write(self.wake_write, '1\n')
472
473
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700474class FakeBuild(threading.Thread):
475 log = logging.getLogger("zuul.test")
476
477 def __init__(self, worker, job, number, node):
478 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400479 self.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700480 self.worker = worker
481 self.job = job
482 self.name = job.name.split(':')[1]
483 self.number = number
484 self.node = node
485 self.parameters = json.loads(job.arguments)
486 self.unique = self.parameters['ZUUL_UUID']
487 self.wait_condition = threading.Condition()
488 self.waiting = False
489 self.aborted = False
490 self.created = time.time()
491 self.description = ''
James E. Blair4a28a882013-08-23 15:17:33 -0700492 self.run_error = False
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700493
494 def release(self):
495 self.wait_condition.acquire()
496 self.wait_condition.notify()
497 self.waiting = False
498 self.log.debug("Build %s released" % self.unique)
499 self.wait_condition.release()
500
501 def isWaiting(self):
502 self.wait_condition.acquire()
503 if self.waiting:
504 ret = True
505 else:
506 ret = False
507 self.wait_condition.release()
508 return ret
509
510 def _wait(self):
511 self.wait_condition.acquire()
512 self.waiting = True
513 self.log.debug("Build %s waiting" % self.unique)
514 self.wait_condition.wait()
515 self.wait_condition.release()
516
517 def run(self):
518 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700519 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
520 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700521 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700522 'manager': self.worker.worker_id,
Joshua Heskethba8776a2014-01-12 14:35:40 +0800523 'worker_name': 'My Worker',
524 'worker_hostname': 'localhost',
525 'worker_ips': ['127.0.0.1', '192.168.1.1'],
526 'worker_fqdn': 'zuul.example.org',
527 'worker_program': 'FakeBuilder',
528 'worker_version': 'v1.1',
529 'worker_extra': {'something': 'else'}
Paul Belangerec49b4c2013-07-20 20:32:20 -0400530 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700531
Joshua Heskethba8776a2014-01-12 14:35:40 +0800532 self.log.debug('Running build %s' % self.unique)
533
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700534 self.job.sendWorkData(json.dumps(data))
Joshua Heskethba8776a2014-01-12 14:35:40 +0800535 self.log.debug('Sent WorkData packet with %s' % json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700536 self.job.sendWorkStatus(0, 100)
537
538 if self.worker.hold_jobs_in_build:
Joshua Heskethba8776a2014-01-12 14:35:40 +0800539 self.log.debug('Holding build %s' % self.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700540 self._wait()
541 self.log.debug("Build %s continuing" % self.unique)
542
543 self.worker.lock.acquire()
544
545 result = 'SUCCESS'
546 if (('ZUUL_REF' in self.parameters) and
547 self.worker.shouldFailTest(self.name,
548 self.parameters['ZUUL_REF'])):
549 result = 'FAILURE'
550 if self.aborted:
551 result = 'ABORTED'
552
James E. Blair4a28a882013-08-23 15:17:33 -0700553 if self.run_error:
554 work_fail = True
555 result = 'RUN_ERROR'
556 else:
557 data['result'] = result
558 work_fail = False
559
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700560 changes = None
561 if 'ZUUL_CHANGE_IDS' in self.parameters:
562 changes = self.parameters['ZUUL_CHANGE_IDS']
563
564 self.worker.build_history.append(
565 BuildHistory(name=self.name, number=self.number,
566 result=result, changes=changes, node=self.node,
James E. Blair64ed6f22013-07-10 14:07:23 -0700567 uuid=self.unique, description=self.description,
568 pipeline=self.parameters['ZUUL_PIPELINE'])
Paul Belangerec49b4c2013-07-20 20:32:20 -0400569 )
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700570
James E. Blair4a28a882013-08-23 15:17:33 -0700571 self.job.sendWorkData(json.dumps(data))
572 if work_fail:
573 self.job.sendWorkFail()
574 else:
575 self.job.sendWorkComplete(json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700576 del self.worker.gearman_jobs[self.job.unique]
577 self.worker.running_builds.remove(self)
578 self.worker.lock.release()
579
580
581class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400582 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700583 super(FakeWorker, self).__init__(worker_id)
584 self.gearman_jobs = {}
585 self.build_history = []
586 self.running_builds = []
587 self.build_counter = 0
588 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400589 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700590
591 self.hold_jobs_in_build = False
592 self.lock = threading.Lock()
593 self.__work_thread = threading.Thread(target=self.work)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400594 self.__work_thread.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700595 self.__work_thread.start()
596
597 def handleJob(self, job):
598 parts = job.name.split(":")
599 cmd = parts[0]
600 name = parts[1]
601 if len(parts) > 2:
602 node = parts[2]
603 else:
604 node = None
605 if cmd == 'build':
606 self.handleBuild(job, name, node)
607 elif cmd == 'stop':
608 self.handleStop(job, name)
609 elif cmd == 'set_description':
610 self.handleSetDescription(job, name)
611
612 def handleBuild(self, job, name, node):
613 build = FakeBuild(self, job, self.build_counter, node)
614 job.build = build
615 self.gearman_jobs[job.unique] = job
616 self.build_counter += 1
617
618 self.running_builds.append(build)
619 build.start()
620
621 def handleStop(self, job, name):
622 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700623 parameters = json.loads(job.arguments)
624 name = parameters['name']
625 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700626 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700627 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700628 build.aborted = True
629 build.release()
630 job.sendWorkComplete()
631 return
632 job.sendWorkFail()
633
634 def handleSetDescription(self, job, name):
635 self.log.debug("handle set description")
636 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700637 name = parameters['name']
638 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700639 descr = parameters['html_description']
640 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700641 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700642 build.description = descr
643 job.sendWorkComplete()
644 return
645 for build in self.build_history:
James E. Blair3c483cf2013-06-04 16:30:43 -0700646 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700647 build.description = descr
648 job.sendWorkComplete()
649 return
650 job.sendWorkFail()
651
652 def work(self):
653 while self.running:
654 try:
655 job = self.getJob()
656 except gear.InterruptedError:
657 continue
658 try:
659 self.handleJob(job)
660 except:
661 self.log.exception("Worker exception:")
662
663 def addFailTest(self, name, change):
664 l = self.fail_tests.get(name, [])
665 l.append(change)
666 self.fail_tests[name] = l
667
668 def shouldFailTest(self, name, ref):
669 l = self.fail_tests.get(name, [])
670 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400671 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700672 return True
673 return False
674
675 def release(self, regex=None):
676 builds = self.running_builds[:]
677 self.log.debug("releasing build %s (%s)" % (regex,
James E. Blair78e31b32013-07-09 09:11:34 -0700678 len(self.running_builds)))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700679 for build in builds:
680 if not regex or re.match(regex, build.name):
681 self.log.debug("releasing build %s" %
682 (build.parameters['ZUUL_UUID']))
683 build.release()
684 else:
685 self.log.debug("not releasing build %s" %
686 (build.parameters['ZUUL_UUID']))
687 self.log.debug("done releasing builds %s (%s)" %
688 (regex, len(self.running_builds)))
689
690
691class FakeGearmanServer(gear.Server):
692 def __init__(self):
693 self.hold_jobs_in_queue = False
694 super(FakeGearmanServer, self).__init__(0)
695
696 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700697 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
698 for job in queue:
699 if not hasattr(job, 'waiting'):
700 if job.name.startswith('build:'):
701 job.waiting = self.hold_jobs_in_queue
702 else:
703 job.waiting = False
704 if job.waiting:
705 continue
706 if job.name in connection.functions:
707 if not peek:
708 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400709 connection.related_jobs[job.handle] = job
710 job.worker_connection = connection
711 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700712 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700713 return None
714
715 def release(self, regex=None):
716 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700717 qlen = (len(self.high_queue) + len(self.normal_queue) +
718 len(self.low_queue))
719 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
James E. Blairdda6c912013-07-29 14:12:12 -0700720 for job in self.getQueue():
721 cmd, name = job.name.split(':')
722 if cmd != 'build':
723 continue
724 if not regex or re.match(regex, name):
725 self.log.debug("releasing queued job %s" %
726 job.unique)
727 job.waiting = False
728 released = True
729 else:
730 self.log.debug("not releasing queued job %s" %
731 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700732 if released:
733 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700734 qlen = (len(self.high_queue) + len(self.normal_queue) +
735 len(self.low_queue))
736 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700737
738
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000739class FakeSMTP(object):
740 log = logging.getLogger('zuul.FakeSMTP')
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000741
James E. Blairff80a2f2013-12-27 13:24:06 -0800742 def __init__(self, messages, server, port):
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000743 self.server = server
744 self.port = port
James E. Blairff80a2f2013-12-27 13:24:06 -0800745 self.messages = messages
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000746
747 def sendmail(self, from_email, to_email, msg):
748 self.log.info("Sending email from %s, to %s, with msg %s" % (
749 from_email, to_email, msg))
750
751 headers = msg.split('\n\n', 1)[0]
752 body = msg.split('\n\n', 1)[1]
753
James E. Blairff80a2f2013-12-27 13:24:06 -0800754 self.messages.append(dict(
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000755 from_email=from_email,
756 to_email=to_email,
757 msg=msg,
758 headers=headers,
759 body=body,
760 ))
761
762 return True
763
764 def quit(self):
765 return True
766
767
Joshua Hesketh36c3fa52014-01-22 11:40:52 +1100768class FakeSwiftClientConnection(swiftclient.client.Connection):
769 def post_account(self, headers):
770 # Do nothing
771 pass
772
773 def get_auth(self):
774 # Returns endpoint and (unused) auth token
775 endpoint = os.path.join('https://storage.example.org', 'V1',
776 'AUTH_account')
777 return endpoint, ''
778
779
Monty Taylorbc758832013-06-17 17:22:42 -0400780class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700781 log = logging.getLogger("zuul.test")
782
783 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400784 super(TestScheduler, self).setUp()
785 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
786 try:
787 test_timeout = int(test_timeout)
788 except ValueError:
789 # If timeout value is invalid do not set a timeout.
790 test_timeout = 0
791 if test_timeout > 0:
James E. Blairff80a2f2013-12-27 13:24:06 -0800792 self.useFixture(fixtures.Timeout(test_timeout, gentle=False))
Monty Taylorbc758832013-06-17 17:22:42 -0400793
794 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
795 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
796 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
797 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
798 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
799 os.environ.get('OS_STDERR_CAPTURE') == '1'):
800 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
801 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Monty Taylore6a6c402013-07-02 09:25:55 -0700802 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
803 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400804 self.useFixture(fixtures.FakeLogger(
805 level=logging.DEBUG,
806 format='%(asctime)s %(name)-32s '
807 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700808 tmp_root = self.useFixture(fixtures.TempDir(
809 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400810 self.test_root = os.path.join(tmp_root, "zuul-test")
811 self.upstream_root = os.path.join(self.test_root, "upstream")
812 self.git_root = os.path.join(self.test_root, "git")
813
James E. Blair4076e2b2014-01-28 12:42:20 -0800814 CONFIG.set('merger', 'git_dir', self.git_root)
Monty Taylorbc758832013-06-17 17:22:42 -0400815 if os.path.exists(self.test_root):
816 shutil.rmtree(self.test_root)
817 os.makedirs(self.test_root)
818 os.makedirs(self.upstream_root)
819 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700820
821 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400822 self.init_repo("org/project")
823 self.init_repo("org/project1")
824 self.init_repo("org/project2")
825 self.init_repo("org/project3")
826 self.init_repo("org/one-job-project")
827 self.init_repo("org/nonvoting-project")
828 self.init_repo("org/templated-project")
James E. Blair3e98c022013-12-16 15:25:38 -0800829 self.init_repo("org/layered-project")
Monty Taylorbc758832013-06-17 17:22:42 -0400830 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700831 self.init_repo("org/conflict-project")
James E. Blair18c64442014-03-18 10:14:45 -0700832 self.init_repo("org/noop-project")
James E. Blairbadc1ad2014-04-28 13:55:14 -0700833 self.init_repo("org/experimental-project")
James E. Blair412e5582013-04-22 15:50:12 -0700834
835 self.statsd = FakeStatsd()
836 os.environ['STATSD_HOST'] = 'localhost'
837 os.environ['STATSD_PORT'] = str(self.statsd.port)
838 self.statsd.start()
839 # the statsd client object is configured in the statsd module import
840 reload(statsd)
841 reload(zuul.scheduler)
842
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700843 self.gearman_server = FakeGearmanServer()
844
845 self.config = ConfigParser.ConfigParser()
846 cfg = StringIO()
847 CONFIG.write(cfg)
848 cfg.seek(0)
849 self.config.readfp(cfg)
850 self.config.set('gearman', 'port', str(self.gearman_server.port))
851
Monty Taylorbc758832013-06-17 17:22:42 -0400852 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700853 self.worker.addServer('127.0.0.1', self.gearman_server.port)
854 self.gearman_server.worker = self.worker
855
James E. Blair4076e2b2014-01-28 12:42:20 -0800856 self.merge_server = zuul.merger.server.MergeServer(self.config)
857 self.merge_server.start()
858
James E. Blairb0fcae42012-07-17 11:12:10 -0700859 self.sched = zuul.scheduler.Scheduler()
860
Joshua Hesketh36c3fa52014-01-22 11:40:52 +1100861 self.useFixture(fixtures.MonkeyPatch('swiftclient.client.Connection',
862 FakeSwiftClientConnection))
863 self.swift = zuul.lib.swift.Swift(self.config)
864
James E. Blair8cc15a82012-08-01 11:17:57 -0700865 def URLOpenerFactory(*args, **kw):
Yuriy Taradaya6d452f2014-04-16 12:36:20 +0400866 if isinstance(args[0], urllib2.Request):
867 return old_urlopen(*args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700868 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400869 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700870
Yuriy Taradaya6d452f2014-04-16 12:36:20 +0400871 old_urlopen = urllib2.urlopen
James E. Blair8cc15a82012-08-01 11:17:57 -0700872 urllib2.urlopen = URLOpenerFactory
Joshua Hesketh36c3fa52014-01-22 11:40:52 +1100873
874 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched,
875 self.swift)
James E. Blair4076e2b2014-01-28 12:42:20 -0800876 self.merge_client = zuul.merger.client.MergeClient(
877 self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700878
James E. Blairff80a2f2013-12-27 13:24:06 -0800879 self.smtp_messages = []
880
881 def FakeSMTPFactory(*args, **kw):
882 args = [self.smtp_messages] + list(args)
883 return FakeSMTP(*args, **kw)
884
James E. Blairb0fcae42012-07-17 11:12:10 -0700885 zuul.lib.gerrit.Gerrit = FakeGerrit
James E. Blairff80a2f2013-12-27 13:24:06 -0800886 self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTPFactory))
James E. Blairb0fcae42012-07-17 11:12:10 -0700887
Monty Taylorbc758832013-06-17 17:22:42 -0400888 self.gerrit = FakeGerritTrigger(
889 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700890 self.gerrit.replication_timeout = 1.5
891 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700892 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400893 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700894
James E. Blair1843a552013-07-03 14:19:52 -0700895 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
James E. Blairad28e912013-11-27 10:43:22 -0800896 self.rpc = zuul.rpclistener.RPCListener(self.config, self.sched)
James E. Blair1843a552013-07-03 14:19:52 -0700897
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700898 self.sched.setLauncher(self.launcher)
James E. Blair4076e2b2014-01-28 12:42:20 -0800899 self.sched.setMerger(self.merge_client)
James E. Blair6c358e72013-07-29 17:06:47 -0700900 self.sched.registerTrigger(self.gerrit)
James E. Blair63bb0ef2013-07-29 17:14:51 -0700901 self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
902 self.sched.registerTrigger(self.timer)
James E. Blairb0fcae42012-07-17 11:12:10 -0700903
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000904 self.sched.registerReporter(
905 zuul.reporter.gerrit.Reporter(self.gerrit))
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000906 self.smtp_reporter = zuul.reporter.smtp.Reporter(
907 self.config.get('smtp', 'default_from'),
908 self.config.get('smtp', 'default_to'),
909 self.config.get('smtp', 'server'))
910 self.sched.registerReporter(self.smtp_reporter)
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000911
James E. Blairb0fcae42012-07-17 11:12:10 -0700912 self.sched.start()
913 self.sched.reconfigure(self.config)
914 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700915 self.webapp.start()
James E. Blairad28e912013-11-27 10:43:22 -0800916 self.rpc.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700917 self.launcher.gearman.waitForServer()
918 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400919 self.builds = self.worker.running_builds
920 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700921
James E. Blairfee8d652013-06-07 08:57:52 -0700922 self.addCleanup(self.assertFinalState)
923 self.addCleanup(self.shutdown)
924
925 def assertFinalState(self):
926 # Make sure that the change cache is cleared
James E. Blair6c358e72013-07-29 17:06:47 -0700927 self.assertEqual(len(self.gerrit._change_cache.keys()), 0)
Clark Boylan4ba48d92013-11-11 18:03:53 -0800928 # Make sure that git.Repo objects have been garbage collected.
929 repos = []
930 gc.collect()
931 for obj in gc.get_objects():
932 if isinstance(obj, git.Repo):
933 repos.append(obj)
934 self.assertEqual(len(repos), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700935 self.assertEmptyQueues()
936
937 def shutdown(self):
938 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700939 self.launcher.stop()
James E. Blair4076e2b2014-01-28 12:42:20 -0800940 self.merge_server.stop()
941 self.merge_server.join()
942 self.merge_client.stop()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700943 self.worker.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700944 self.gerrit.stop()
James E. Blair63bb0ef2013-07-29 17:14:51 -0700945 self.timer.stop()
James E. Blairb0fcae42012-07-17 11:12:10 -0700946 self.sched.stop()
947 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700948 self.statsd.stop()
949 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700950 self.webapp.stop()
951 self.webapp.join()
James E. Blairad28e912013-11-27 10:43:22 -0800952 self.rpc.stop()
953 self.rpc.join()
James E. Blair491b3172014-03-10 15:05:36 -0700954 self.gearman_server.shutdown()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700955 threads = threading.enumerate()
956 if len(threads) > 1:
957 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400958 super(TestScheduler, self).tearDown()
959
960 def init_repo(self, project):
961 parts = project.split('/')
962 path = os.path.join(self.upstream_root, *parts[:-1])
963 if not os.path.exists(path):
964 os.makedirs(path)
965 path = os.path.join(self.upstream_root, project)
966 repo = git.Repo.init(path)
967
968 repo.config_writer().set_value('user', 'email', 'user@example.com')
969 repo.config_writer().set_value('user', 'name', 'User Name')
970 repo.config_writer().write()
971
972 fn = os.path.join(path, 'README')
973 f = open(fn, 'w')
974 f.write("test\n")
975 f.close()
976 repo.index.add([fn])
977 repo.index.commit('initial commit')
978 master = repo.create_head('master')
979 repo.create_tag('init')
980
981 mp = repo.create_head('mp')
982 repo.head.reference = mp
983 f = open(fn, 'a')
984 f.write("test mp\n")
985 f.close()
986 repo.index.add([fn])
987 repo.index.commit('mp commit')
988
989 repo.head.reference = master
990 repo.head.reset(index=True, working_tree=True)
991 repo.git.clean('-x', '-f', '-d')
992
993 def ref_has_change(self, ref, change):
994 path = os.path.join(self.git_root, change.project)
995 repo = git.Repo(path)
996 for commit in repo.iter_commits(ref):
997 if commit.message.strip() == ('%s-1' % change.subject):
998 return True
999 return False
1000
1001 def job_has_changes(self, *args):
1002 job = args[0]
1003 commits = args[1:]
1004 if isinstance(job, FakeBuild):
1005 parameters = job.parameters
1006 else:
1007 parameters = json.loads(job.arguments)
1008 project = parameters['ZUUL_PROJECT']
1009 path = os.path.join(self.git_root, project)
1010 repo = git.Repo(path)
1011 ref = parameters['ZUUL_REF']
1012 sha = parameters['ZUUL_COMMIT']
1013 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1014 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
1015 commit_messages = ['%s-1' % commit.subject for commit in commits]
James E. Blairac2c3242014-01-24 13:38:51 -08001016 self.log.debug("Checking if job %s has changes; commit_messages %s;"
1017 " repo_messages %s; sha %s" % (job, commit_messages,
1018 repo_messages, sha))
Monty Taylorbc758832013-06-17 17:22:42 -04001019 for msg in commit_messages:
1020 if msg not in repo_messages:
James E. Blairac2c3242014-01-24 13:38:51 -08001021 self.log.debug(" messages do not match")
Monty Taylorbc758832013-06-17 17:22:42 -04001022 return False
1023 if repo_shas[0] != sha:
James E. Blairac2c3242014-01-24 13:38:51 -08001024 self.log.debug(" sha does not match")
Monty Taylorbc758832013-06-17 17:22:42 -04001025 return False
James E. Blairac2c3242014-01-24 13:38:51 -08001026 self.log.debug(" OK")
Monty Taylorbc758832013-06-17 17:22:42 -04001027 return True
James E. Blairb0fcae42012-07-17 11:12:10 -07001028
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001029 def registerJobs(self):
1030 count = 0
James E. Blaireff88162013-07-01 12:44:14 -04001031 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001032 self.worker.registerFunction('build:' + job)
1033 count += 1
1034 self.worker.registerFunction('stop:' + self.worker.worker_id)
1035 count += 1
1036
1037 while len(self.gearman_server.functions) < count:
1038 time.sleep(0)
1039
1040 def release(self, job):
1041 if isinstance(job, FakeBuild):
1042 job.release()
1043 else:
1044 job.waiting = False
1045 self.log.debug("Queued job %s released" % job.unique)
1046 self.gearman_server.wakeConnections()
1047
1048 def getParameter(self, job, name):
1049 if isinstance(job, FakeBuild):
1050 return job.parameters[name]
1051 else:
1052 parameters = json.loads(job.arguments)
1053 return parameters[name]
1054
1055 def resetGearmanServer(self):
1056 self.worker.setFunctions([])
1057 while True:
1058 done = True
1059 for connection in self.gearman_server.active_connections:
James E. Blairad28e912013-11-27 10:43:22 -08001060 if (connection.functions and
James E. Blair4076e2b2014-01-28 12:42:20 -08001061 connection.client_id not in ['Zuul RPC Listener',
1062 'Zuul Merger']):
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001063 done = False
1064 if done:
1065 break
1066 time.sleep(0)
1067 self.gearman_server.functions = set()
James E. Blairad28e912013-11-27 10:43:22 -08001068 self.rpc.register()
James E. Blair4076e2b2014-01-28 12:42:20 -08001069 self.merge_server.register()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001070
1071 def haveAllBuildsReported(self):
1072 # See if Zuul is waiting on a meta job to complete
1073 if self.launcher.meta_jobs:
1074 return False
1075 # Find out if every build that the worker has completed has been
1076 # reported back to Zuul. If it hasn't then that means a Gearman
1077 # event is still in transit and the system is not stable.
1078 for build in self.worker.build_history:
1079 zbuild = self.launcher.builds.get(build.uuid)
1080 if not zbuild:
1081 # It has already been reported
1082 continue
1083 # It hasn't been reported yet.
1084 return False
1085 # Make sure that none of the worker connections are in GRAB_WAIT
1086 for connection in self.worker.active_connections:
1087 if connection.state == 'GRAB_WAIT':
1088 return False
1089 return True
1090
1091 def areAllBuildsWaiting(self):
1092 ret = True
1093
1094 builds = self.launcher.builds.values()
1095 for build in builds:
1096 client_job = None
1097 for conn in self.launcher.gearman.active_connections:
1098 for j in conn.related_jobs.values():
1099 if j.unique == build.uuid:
1100 client_job = j
1101 break
1102 if not client_job:
1103 self.log.debug("%s is not known to the gearman client" %
1104 build)
1105 ret = False
1106 continue
1107 if not client_job.handle:
1108 self.log.debug("%s has no handle" % client_job)
1109 ret = False
1110 continue
1111 server_job = self.gearman_server.jobs.get(client_job.handle)
1112 if not server_job:
1113 self.log.debug("%s is not known to the gearman server" %
1114 client_job)
1115 ret = False
1116 continue
1117 if not hasattr(server_job, 'waiting'):
1118 self.log.debug("%s is being enqueued" % server_job)
1119 ret = False
1120 continue
1121 if server_job.waiting:
1122 continue
1123 worker_job = self.worker.gearman_jobs.get(server_job.unique)
1124 if worker_job:
1125 if worker_job.build.isWaiting():
1126 continue
1127 else:
1128 self.log.debug("%s is running" % worker_job)
1129 ret = False
1130 else:
1131 self.log.debug("%s is unassigned" % server_job)
1132 ret = False
1133 return ret
1134
James E. Blairb0fcae42012-07-17 11:12:10 -07001135 def waitUntilSettled(self):
1136 self.log.debug("Waiting until settled...")
1137 start = time.time()
1138 while True:
1139 if time.time() - start > 10:
1140 print 'queue status:',
1141 print self.sched.trigger_event_queue.empty(),
1142 print self.sched.result_event_queue.empty(),
1143 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001144 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -07001145 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001146 # Make sure no new events show up while we're checking
1147 self.worker.lock.acquire()
1148 # have all build states propogated to zuul?
1149 if self.haveAllBuildsReported():
1150 # Join ensures that the queue is empty _and_ events have been
1151 # processed
1152 self.fake_gerrit.event_queue.join()
1153 self.sched.trigger_event_queue.join()
1154 self.sched.result_event_queue.join()
James E. Blaira84f0e42014-02-06 07:09:22 -08001155 self.sched.run_handler_lock.acquire()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001156 if (self.sched.trigger_event_queue.empty() and
1157 self.sched.result_event_queue.empty() and
1158 self.fake_gerrit.event_queue.empty() and
James E. Blair4076e2b2014-01-28 12:42:20 -08001159 not self.merge_client.build_sets and
James E. Blair9c2985a2014-03-10 10:59:05 -07001160 self.haveAllBuildsReported() and
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001161 self.areAllBuildsWaiting()):
James E. Blaira84f0e42014-02-06 07:09:22 -08001162 self.sched.run_handler_lock.release()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001163 self.worker.lock.release()
1164 self.log.debug("...settled.")
1165 return
James E. Blaira84f0e42014-02-06 07:09:22 -08001166 self.sched.run_handler_lock.release()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001167 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001168 self.sched.wake_event.wait(0.1)
1169
James E. Blaird466dc42012-07-31 10:42:56 -07001170 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -08001171 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -07001172 return len(jobs)
1173
James E. Blair4ca985f2013-05-30 12:27:43 -07001174 def getJobFromHistory(self, name):
1175 history = self.worker.build_history
1176 for job in history:
1177 if job.name == name:
1178 return job
1179 raise Exception("Unable to find job %s in history" % name)
1180
James E. Blaire0487072012-08-29 17:38:31 -07001181 def assertEmptyQueues(self):
1182 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001183 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001184 for queue in pipeline.queues:
1185 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001186 print 'pipeline %s queue %s contents %s' % (
1187 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001188 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001189
James E. Blair66eeebf2013-07-27 17:44:32 -07001190 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001191 start = time.time()
1192 while time.time() < (start + 5):
1193 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001194 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001195 k, v = stat.split(':')
1196 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001197 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001198 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001199 elif value:
1200 if value == v:
1201 return
1202 elif kind:
1203 if v.endswith('|' + kind):
1204 return
James E. Blair412e5582013-04-22 15:50:12 -07001205 time.sleep(0.1)
1206
1207 pprint.pprint(self.statsd.stats)
1208 raise Exception("Key %s not found in reported stats" % key)
1209
James E. Blairb0fcae42012-07-17 11:12:10 -07001210 def test_jobs_launched(self):
1211 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001212
James E. Blairb0fcae42012-07-17 11:12:10 -07001213 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001214 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001215 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1216 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001217 self.assertEqual(self.getJobFromHistory('project-merge').result,
1218 'SUCCESS')
1219 self.assertEqual(self.getJobFromHistory('project-test1').result,
1220 'SUCCESS')
1221 self.assertEqual(self.getJobFromHistory('project-test2').result,
1222 'SUCCESS')
1223 self.assertEqual(A.data['status'], 'MERGED')
1224 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001225
James E. Blair66eeebf2013-07-27 17:44:32 -07001226 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1227 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1228 value='1|g')
1229 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1230 kind='ms')
1231 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1232 value='1|c')
1233 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1234 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1235 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001236 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001237 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001238 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001239 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001240
James E. Blair3cb10702013-08-24 08:56:03 -07001241 def test_initial_pipeline_gauges(self):
1242 "Test that each pipeline reported its length on start"
1243 pipeline_names = self.sched.layout.pipelines.keys()
1244 self.assertNotEqual(len(pipeline_names), 0)
1245 for name in pipeline_names:
1246 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
1247 value='0|g')
1248
James E. Blair42f74822013-05-14 15:18:03 -07001249 def test_duplicate_pipelines(self):
1250 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001251
James E. Blair42f74822013-05-14 15:18:03 -07001252 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1253 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1254 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001255
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001256 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001257 self.history[0].name == 'project-test1'
1258 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001259
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001260 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001261 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001262 self.assertIn('dup1/project-test1', A.messages[0])
1263 self.assertNotIn('dup2/project-test1', A.messages[0])
1264 self.assertNotIn('dup1/project-test1', A.messages[1])
1265 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001266 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001267 self.assertIn('dup1/project-test1', A.messages[1])
1268 self.assertNotIn('dup2/project-test1', A.messages[1])
1269 self.assertNotIn('dup1/project-test1', A.messages[0])
1270 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001271
James E. Blairb0fcae42012-07-17 11:12:10 -07001272 def test_parallel_changes(self):
1273 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001274
1275 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001276 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1277 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1278 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001279 A.addApproval('CRVW', 2)
1280 B.addApproval('CRVW', 2)
1281 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001282
1283 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1284 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1285 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1286
1287 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001288 self.assertEqual(len(self.builds), 1)
1289 self.assertEqual(self.builds[0].name, 'project-merge')
1290 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001291
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001292 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001293 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001294 self.assertEqual(len(self.builds), 3)
1295 self.assertEqual(self.builds[0].name, 'project-test1')
1296 self.assertTrue(self.job_has_changes(self.builds[0], A))
1297 self.assertEqual(self.builds[1].name, 'project-test2')
1298 self.assertTrue(self.job_has_changes(self.builds[1], A))
1299 self.assertEqual(self.builds[2].name, 'project-merge')
1300 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001301
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001302 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001303 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001304 self.assertEqual(len(self.builds), 5)
1305 self.assertEqual(self.builds[0].name, 'project-test1')
1306 self.assertTrue(self.job_has_changes(self.builds[0], A))
1307 self.assertEqual(self.builds[1].name, 'project-test2')
1308 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001309
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001310 self.assertEqual(self.builds[2].name, 'project-test1')
1311 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1312 self.assertEqual(self.builds[3].name, 'project-test2')
1313 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001314
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001315 self.assertEqual(self.builds[4].name, 'project-merge')
1316 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001317
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001318 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001319 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001320 self.assertEqual(len(self.builds), 6)
1321 self.assertEqual(self.builds[0].name, 'project-test1')
1322 self.assertTrue(self.job_has_changes(self.builds[0], A))
1323 self.assertEqual(self.builds[1].name, 'project-test2')
1324 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001325
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001326 self.assertEqual(self.builds[2].name, 'project-test1')
1327 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1328 self.assertEqual(self.builds[3].name, 'project-test2')
1329 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001330
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001331 self.assertEqual(self.builds[4].name, 'project-test1')
1332 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1333 self.assertEqual(self.builds[5].name, 'project-test2')
1334 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001335
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001336 self.worker.hold_jobs_in_build = False
1337 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001338 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001339 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001340
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001341 self.assertEqual(len(self.history), 9)
1342 self.assertEqual(A.data['status'], 'MERGED')
1343 self.assertEqual(B.data['status'], 'MERGED')
1344 self.assertEqual(C.data['status'], 'MERGED')
1345 self.assertEqual(A.reported, 2)
1346 self.assertEqual(B.reported, 2)
1347 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001348
1349 def test_failed_changes(self):
1350 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001351 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001352
James E. Blairb02a3bb2012-07-30 17:49:55 -07001353 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1354 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001355 A.addApproval('CRVW', 2)
1356 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001357
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001358 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001359
James E. Blaire2819012013-06-28 17:17:26 -04001360 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1361 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001362 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001363
1364 self.worker.release('.*-merge')
1365 self.waitUntilSettled()
1366
1367 self.worker.hold_jobs_in_build = False
1368 self.worker.release()
1369
1370 self.waitUntilSettled()
1371 # It's certain that the merge job for change 2 will run, but
1372 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001373 self.assertTrue(len(self.history) > 6)
1374 self.assertEqual(A.data['status'], 'NEW')
1375 self.assertEqual(B.data['status'], 'MERGED')
1376 self.assertEqual(A.reported, 2)
1377 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001378
1379 def test_independent_queues(self):
1380 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001381
1382 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001383 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001384 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1385 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001386 A.addApproval('CRVW', 2)
1387 B.addApproval('CRVW', 2)
1388 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001389
1390 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1391 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1392 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1393
James E. Blairb02a3bb2012-07-30 17:49:55 -07001394 self.waitUntilSettled()
1395
1396 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001397 self.assertEqual(len(self.builds), 2)
1398 self.assertEqual(self.builds[0].name, 'project-merge')
1399 self.assertTrue(self.job_has_changes(self.builds[0], A))
1400 self.assertEqual(self.builds[1].name, 'project1-merge')
1401 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001402
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001403 # Release the current merge builds
1404 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001405 self.waitUntilSettled()
1406 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001407 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001408 self.waitUntilSettled()
1409
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001410 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001411 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001412 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001413
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001414 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001415 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001416 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001417
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001418 self.assertEqual(len(self.history), 11)
1419 self.assertEqual(A.data['status'], 'MERGED')
1420 self.assertEqual(B.data['status'], 'MERGED')
1421 self.assertEqual(C.data['status'], 'MERGED')
1422 self.assertEqual(A.reported, 2)
1423 self.assertEqual(B.reported, 2)
1424 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001425
1426 def test_failed_change_at_head(self):
1427 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001428
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001429 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001430 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1431 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1432 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001433 A.addApproval('CRVW', 2)
1434 B.addApproval('CRVW', 2)
1435 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001436
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001437 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001438
1439 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1440 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1441 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1442
1443 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001444
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001445 self.assertEqual(len(self.builds), 1)
1446 self.assertEqual(self.builds[0].name, 'project-merge')
1447 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001448
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001449 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001450 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001451 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001452 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001453 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001454 self.waitUntilSettled()
1455
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001456 self.assertEqual(len(self.builds), 6)
1457 self.assertEqual(self.builds[0].name, 'project-test1')
1458 self.assertEqual(self.builds[1].name, 'project-test2')
1459 self.assertEqual(self.builds[2].name, 'project-test1')
1460 self.assertEqual(self.builds[3].name, 'project-test2')
1461 self.assertEqual(self.builds[4].name, 'project-test1')
1462 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001463
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001464 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001465 self.waitUntilSettled()
1466
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001467 # project-test2, project-merge for B
1468 self.assertEqual(len(self.builds), 2)
1469 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001470
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001471 self.worker.hold_jobs_in_build = False
1472 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001473 self.waitUntilSettled()
1474
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001475 self.assertEqual(len(self.builds), 0)
1476 self.assertEqual(len(self.history), 15)
1477 self.assertEqual(A.data['status'], 'NEW')
1478 self.assertEqual(B.data['status'], 'MERGED')
1479 self.assertEqual(C.data['status'], 'MERGED')
1480 self.assertEqual(A.reported, 2)
1481 self.assertEqual(B.reported, 2)
1482 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001483
James E. Blair0aac4872013-08-23 14:02:38 -07001484 def test_failed_change_in_middle(self):
1485 "Test a failed change in the middle of the queue"
1486
1487 self.worker.hold_jobs_in_build = True
1488 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1489 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1490 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1491 A.addApproval('CRVW', 2)
1492 B.addApproval('CRVW', 2)
1493 C.addApproval('CRVW', 2)
1494
1495 self.worker.addFailTest('project-test1', B)
1496
1497 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1498 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1499 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1500
1501 self.waitUntilSettled()
1502
1503 self.worker.release('.*-merge')
1504 self.waitUntilSettled()
1505 self.worker.release('.*-merge')
1506 self.waitUntilSettled()
1507 self.worker.release('.*-merge')
1508 self.waitUntilSettled()
1509
1510 self.assertEqual(len(self.builds), 6)
1511 self.assertEqual(self.builds[0].name, 'project-test1')
1512 self.assertEqual(self.builds[1].name, 'project-test2')
1513 self.assertEqual(self.builds[2].name, 'project-test1')
1514 self.assertEqual(self.builds[3].name, 'project-test2')
1515 self.assertEqual(self.builds[4].name, 'project-test1')
1516 self.assertEqual(self.builds[5].name, 'project-test2')
1517
1518 self.release(self.builds[2])
1519 self.waitUntilSettled()
1520
James E. Blair972e3c72013-08-29 12:04:55 -07001521 # project-test1 and project-test2 for A
1522 # project-test2 for B
1523 # project-merge for C (without B)
1524 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -07001525 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1526
James E. Blair972e3c72013-08-29 12:04:55 -07001527 self.worker.release('.*-merge')
1528 self.waitUntilSettled()
1529
1530 # project-test1 and project-test2 for A
1531 # project-test2 for B
1532 # project-test1 and project-test2 for C
1533 self.assertEqual(len(self.builds), 5)
1534
James E. Blair0aac4872013-08-23 14:02:38 -07001535 items = self.sched.layout.pipelines['gate'].getAllItems()
1536 builds = items[0].current_build_set.getBuilds()
1537 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1538 self.assertEqual(self.countJobResults(builds, None), 2)
1539 builds = items[1].current_build_set.getBuilds()
1540 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1541 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1542 self.assertEqual(self.countJobResults(builds, None), 1)
1543 builds = items[2].current_build_set.getBuilds()
1544 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -07001545 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -07001546
1547 self.worker.hold_jobs_in_build = False
1548 self.worker.release()
1549 self.waitUntilSettled()
1550
1551 self.assertEqual(len(self.builds), 0)
1552 self.assertEqual(len(self.history), 12)
1553 self.assertEqual(A.data['status'], 'MERGED')
1554 self.assertEqual(B.data['status'], 'NEW')
1555 self.assertEqual(C.data['status'], 'MERGED')
1556 self.assertEqual(A.reported, 2)
1557 self.assertEqual(B.reported, 2)
1558 self.assertEqual(C.reported, 2)
1559
James E. Blaird466dc42012-07-31 10:42:56 -07001560 def test_failed_change_at_head_with_queue(self):
1561 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001562
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001563 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001564 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1565 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1566 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001567 A.addApproval('CRVW', 2)
1568 B.addApproval('CRVW', 2)
1569 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001570
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001571 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001572
1573 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1574 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1575 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1576
1577 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001578 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001579 self.assertEqual(len(self.builds), 0)
1580 self.assertEqual(len(queue), 1)
1581 self.assertEqual(queue[0].name, 'build:project-merge')
1582 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001583
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001584 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001585 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001586 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001587 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001588 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001589 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001590 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001591
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001592 self.assertEqual(len(self.builds), 0)
1593 self.assertEqual(len(queue), 6)
1594 self.assertEqual(queue[0].name, 'build:project-test1')
1595 self.assertEqual(queue[1].name, 'build:project-test2')
1596 self.assertEqual(queue[2].name, 'build:project-test1')
1597 self.assertEqual(queue[3].name, 'build:project-test2')
1598 self.assertEqual(queue[4].name, 'build:project-test1')
1599 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001600
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001601 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001602 self.waitUntilSettled()
1603
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001604 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001605 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001606 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1607 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001608
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001609 self.gearman_server.hold_jobs_in_queue = False
1610 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001611 self.waitUntilSettled()
1612
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001613 self.assertEqual(len(self.builds), 0)
1614 self.assertEqual(len(self.history), 11)
1615 self.assertEqual(A.data['status'], 'NEW')
1616 self.assertEqual(B.data['status'], 'MERGED')
1617 self.assertEqual(C.data['status'], 'MERGED')
1618 self.assertEqual(A.reported, 2)
1619 self.assertEqual(B.reported, 2)
1620 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001621
James E. Blairfef71632013-09-23 11:15:47 -07001622 def test_two_failed_changes_at_head(self):
1623 "Test that changes are reparented correctly if 2 fail at head"
1624
1625 self.worker.hold_jobs_in_build = True
1626 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1627 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1628 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1629 A.addApproval('CRVW', 2)
1630 B.addApproval('CRVW', 2)
1631 C.addApproval('CRVW', 2)
1632
1633 self.worker.addFailTest('project-test1', A)
1634 self.worker.addFailTest('project-test1', B)
1635
1636 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1637 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1638 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1639 self.waitUntilSettled()
1640
1641 self.worker.release('.*-merge')
1642 self.waitUntilSettled()
1643 self.worker.release('.*-merge')
1644 self.waitUntilSettled()
1645 self.worker.release('.*-merge')
1646 self.waitUntilSettled()
1647
1648 self.assertEqual(len(self.builds), 6)
1649 self.assertEqual(self.builds[0].name, 'project-test1')
1650 self.assertEqual(self.builds[1].name, 'project-test2')
1651 self.assertEqual(self.builds[2].name, 'project-test1')
1652 self.assertEqual(self.builds[3].name, 'project-test2')
1653 self.assertEqual(self.builds[4].name, 'project-test1')
1654 self.assertEqual(self.builds[5].name, 'project-test2')
1655
1656 self.assertTrue(self.job_has_changes(self.builds[0], A))
1657 self.assertTrue(self.job_has_changes(self.builds[2], A))
1658 self.assertTrue(self.job_has_changes(self.builds[2], B))
1659 self.assertTrue(self.job_has_changes(self.builds[4], A))
1660 self.assertTrue(self.job_has_changes(self.builds[4], B))
1661 self.assertTrue(self.job_has_changes(self.builds[4], C))
1662
1663 # Fail change B first
1664 self.release(self.builds[2])
1665 self.waitUntilSettled()
1666
1667 # restart of C after B failure
1668 self.worker.release('.*-merge')
1669 self.waitUntilSettled()
1670
1671 self.assertEqual(len(self.builds), 5)
1672 self.assertEqual(self.builds[0].name, 'project-test1')
1673 self.assertEqual(self.builds[1].name, 'project-test2')
1674 self.assertEqual(self.builds[2].name, 'project-test2')
1675 self.assertEqual(self.builds[3].name, 'project-test1')
1676 self.assertEqual(self.builds[4].name, 'project-test2')
1677
1678 self.assertTrue(self.job_has_changes(self.builds[1], A))
1679 self.assertTrue(self.job_has_changes(self.builds[2], A))
1680 self.assertTrue(self.job_has_changes(self.builds[2], B))
1681 self.assertTrue(self.job_has_changes(self.builds[4], A))
1682 self.assertFalse(self.job_has_changes(self.builds[4], B))
1683 self.assertTrue(self.job_has_changes(self.builds[4], C))
1684
1685 # Finish running all passing jobs for change A
1686 self.release(self.builds[1])
1687 self.waitUntilSettled()
1688 # Fail and report change A
1689 self.release(self.builds[0])
1690 self.waitUntilSettled()
1691
1692 # restart of B,C after A failure
1693 self.worker.release('.*-merge')
1694 self.waitUntilSettled()
1695 self.worker.release('.*-merge')
1696 self.waitUntilSettled()
1697
1698 self.assertEqual(len(self.builds), 4)
1699 self.assertEqual(self.builds[0].name, 'project-test1') # B
1700 self.assertEqual(self.builds[1].name, 'project-test2') # B
1701 self.assertEqual(self.builds[2].name, 'project-test1') # C
1702 self.assertEqual(self.builds[3].name, 'project-test2') # C
1703
1704 self.assertFalse(self.job_has_changes(self.builds[1], A))
1705 self.assertTrue(self.job_has_changes(self.builds[1], B))
1706 self.assertFalse(self.job_has_changes(self.builds[1], C))
1707
1708 self.assertFalse(self.job_has_changes(self.builds[2], A))
1709 # After A failed and B and C restarted, B should be back in
1710 # C's tests because it has not failed yet.
1711 self.assertTrue(self.job_has_changes(self.builds[2], B))
1712 self.assertTrue(self.job_has_changes(self.builds[2], C))
1713
1714 self.worker.hold_jobs_in_build = False
1715 self.worker.release()
1716 self.waitUntilSettled()
1717
1718 self.assertEqual(len(self.builds), 0)
1719 self.assertEqual(len(self.history), 21)
1720 self.assertEqual(A.data['status'], 'NEW')
1721 self.assertEqual(B.data['status'], 'NEW')
1722 self.assertEqual(C.data['status'], 'MERGED')
1723 self.assertEqual(A.reported, 2)
1724 self.assertEqual(B.reported, 2)
1725 self.assertEqual(C.reported, 2)
1726
James E. Blair8c803f82012-07-31 16:25:42 -07001727 def test_patch_order(self):
1728 "Test that dependent patches are tested in the right order"
1729 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1730 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1731 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1732 A.addApproval('CRVW', 2)
1733 B.addApproval('CRVW', 2)
1734 C.addApproval('CRVW', 2)
1735
1736 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1737 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1738 M2.setMerged()
1739 M1.setMerged()
1740
1741 # C -> B -> A -> M1 -> M2
1742 # M2 is here to make sure it is never queried. If it is, it
1743 # means zuul is walking down the entire history of merged
1744 # changes.
1745
1746 C.setDependsOn(B, 1)
1747 B.setDependsOn(A, 1)
1748 A.setDependsOn(M1, 1)
1749 M1.setDependsOn(M2, 1)
1750
1751 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1752
1753 self.waitUntilSettled()
1754
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001755 self.assertEqual(A.data['status'], 'NEW')
1756 self.assertEqual(B.data['status'], 'NEW')
1757 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001758
1759 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1760 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1761
1762 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001763 self.assertEqual(M2.queried, 0)
1764 self.assertEqual(A.data['status'], 'MERGED')
1765 self.assertEqual(B.data['status'], 'MERGED')
1766 self.assertEqual(C.data['status'], 'MERGED')
1767 self.assertEqual(A.reported, 2)
1768 self.assertEqual(B.reported, 2)
1769 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001770
James E. Blair0e933c52013-07-11 10:18:52 -07001771 def test_trigger_cache(self):
1772 "Test that the trigger cache operates correctly"
1773 self.worker.hold_jobs_in_build = True
1774
1775 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1776 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1777 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1778 A.addApproval('CRVW', 2)
1779 B.addApproval('CRVW', 2)
1780
1781 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1782 M1.setMerged()
1783
1784 B.setDependsOn(A, 1)
1785 A.setDependsOn(M1, 1)
1786
1787 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1788 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1789
1790 self.waitUntilSettled()
1791
1792 for build in self.builds:
1793 if build.parameters['ZUUL_PIPELINE'] == 'check':
1794 build.release()
1795 self.waitUntilSettled()
1796 for build in self.builds:
1797 if build.parameters['ZUUL_PIPELINE'] == 'check':
1798 build.release()
1799 self.waitUntilSettled()
1800
1801 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1802 self.waitUntilSettled()
1803
James E. Blair6c358e72013-07-29 17:06:47 -07001804 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001805 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001806 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001807
1808 self.worker.hold_jobs_in_build = False
1809 self.worker.release()
1810 self.waitUntilSettled()
1811
1812 self.assertEqual(A.data['status'], 'MERGED')
1813 self.assertEqual(B.data['status'], 'MERGED')
1814 self.assertEqual(A.queried, 2) # Initial and isMerged
1815 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1816
James E. Blair8c803f82012-07-31 16:25:42 -07001817 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001818 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001819 # TODO: move to test_gerrit (this is a unit test!)
1820 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001821 trigger = self.sched.layout.pipelines['gate'].trigger
1822 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001823 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001824 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001825
1826 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001827 a = trigger.getChange(1, 2, refresh=True)
1828 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001829
1830 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001831 a = trigger.getChange(1, 2, refresh=True)
1832 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1833 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001834
James E. Blair11041d22014-05-02 14:49:53 -07001835 def test_pipeline_requirements_closed_change(self):
1836 "Test that pipeline requirements for closed changes are effective"
1837 self.config.set('zuul', 'layout_config',
1838 'tests/fixtures/layout-pipeline-requirements.yaml')
1839 self.sched.reconfigure(self.config)
1840
1841 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1842 status='MERGED')
1843 self.fake_gerrit.addEvent(A.addApproval('CRVW', 2))
1844 self.waitUntilSettled()
1845 self.assertEqual(len(self.history), 0)
1846 self.assertEqual(len(self.builds), 0)
1847
1848 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
1849 status='MERGED')
1850 B.addApproval('CRVW', 2)
1851 B.addApproval('VRFY', 1)
1852 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1853 self.waitUntilSettled()
1854 self.assertEqual(len(self.history), 0)
1855 self.assertEqual(len(self.builds), 0)
1856
1857 for pipeline in self.sched.layout.pipelines.values():
1858 pipeline.trigger.maintainCache([])
1859
James E. Blair4886cc12012-07-18 15:39:41 -07001860 def test_build_configuration(self):
1861 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001862
1863 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001864 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1865 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1866 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1867 A.addApproval('CRVW', 2)
1868 B.addApproval('CRVW', 2)
1869 C.addApproval('CRVW', 2)
1870 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1871 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1872 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1873 self.waitUntilSettled()
1874
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001875 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001876 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001877 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001878 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001879 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001880 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001881 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001882 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1883 self.gearman_server.hold_jobs_in_queue = False
1884 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001885 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001886
Monty Taylorbc758832013-06-17 17:22:42 -04001887 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001888 repo = git.Repo(path)
1889 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1890 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001891 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001892 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001893
1894 def test_build_configuration_conflict(self):
1895 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001896
1897 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001898 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1899 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001900 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001901 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1902 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001903 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001904 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1905 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001906 A.addApproval('CRVW', 2)
1907 B.addApproval('CRVW', 2)
1908 C.addApproval('CRVW', 2)
1909 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1910 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1911 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1912 self.waitUntilSettled()
1913
James E. Blair6736beb2013-07-11 15:18:15 -07001914 self.assertEqual(A.reported, 1)
1915 self.assertEqual(B.reported, 1)
1916 self.assertEqual(C.reported, 1)
1917
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001918 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001919 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001920 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001921 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001922 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001923 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001924
1925 self.assertEqual(len(self.history), 2) # A and C merge jobs
1926
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001927 self.gearman_server.hold_jobs_in_queue = False
1928 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001929 self.waitUntilSettled()
1930
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001931 self.assertEqual(A.data['status'], 'MERGED')
1932 self.assertEqual(B.data['status'], 'NEW')
1933 self.assertEqual(C.data['status'], 'MERGED')
1934 self.assertEqual(A.reported, 2)
1935 self.assertEqual(B.reported, 2)
1936 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001937 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001938
James E. Blairdaabed22012-08-15 15:38:57 -07001939 def test_post(self):
1940 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001941
Zhongyue Luo5d556072012-09-21 02:00:47 +09001942 e = {
1943 "type": "ref-updated",
1944 "submitter": {
1945 "name": "User Name",
1946 },
1947 "refUpdate": {
1948 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1949 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1950 "refName": "master",
1951 "project": "org/project",
1952 }
1953 }
James E. Blairdaabed22012-08-15 15:38:57 -07001954 self.fake_gerrit.addEvent(e)
1955 self.waitUntilSettled()
1956
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001957 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001958 self.assertEqual(len(self.history), 1)
1959 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001960
1961 def test_build_configuration_branch(self):
1962 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001963
1964 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001965 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1966 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1967 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1968 A.addApproval('CRVW', 2)
1969 B.addApproval('CRVW', 2)
1970 C.addApproval('CRVW', 2)
1971 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1972 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1973 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1974 self.waitUntilSettled()
1975
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001976 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001977 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001978 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001979 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001980 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001981 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001982 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001983 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1984 self.gearman_server.hold_jobs_in_queue = False
1985 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001986 self.waitUntilSettled()
1987
Monty Taylorbc758832013-06-17 17:22:42 -04001988 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001989 repo = git.Repo(path)
1990 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1991 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001992 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001993 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001994
1995 def test_build_configuration_branch_interaction(self):
1996 "Test that switching between branches works"
1997 self.test_build_configuration()
1998 self.test_build_configuration_branch()
1999 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04002000 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07002001 repo = git.Repo(path)
2002 repo.heads.master.commit = repo.commit('init')
2003 self.test_build_configuration()
2004
2005 def test_build_configuration_multi_branch(self):
2006 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002007
2008 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07002009 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2010 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
2011 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2012 A.addApproval('CRVW', 2)
2013 B.addApproval('CRVW', 2)
2014 C.addApproval('CRVW', 2)
2015 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2016 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2017 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2018 self.waitUntilSettled()
James E. Blairbb1fe502014-03-04 10:15:06 -08002019 queue = self.gearman_server.getQueue()
2020 job_A = None
2021 for job in queue:
2022 if 'project-merge' in job.name:
2023 job_A = job
2024 ref_A = self.getParameter(job_A, 'ZUUL_REF')
2025 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
2026 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
2027 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
James E. Blairc6294a52012-08-17 10:19:48 -07002028
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002029 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07002030 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07002031 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07002032 job_B = None
2033 for job in queue:
2034 if 'project-merge' in job.name:
2035 job_B = job
2036 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08002037 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07002038 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blairbb1fe502014-03-04 10:15:06 -08002039 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
2040
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002041 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07002042 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07002043 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07002044 for job in queue:
2045 if 'project-merge' in job.name:
2046 job_C = job
2047 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08002048 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07002049 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blairbb1fe502014-03-04 10:15:06 -08002050 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002051 self.gearman_server.hold_jobs_in_queue = False
2052 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07002053 self.waitUntilSettled()
2054
Monty Taylorbc758832013-06-17 17:22:42 -04002055 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07002056 repo = git.Repo(path)
2057
2058 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07002059 for c in repo.iter_commits(ref_C)]
James E. Blairbb1fe502014-03-04 10:15:06 -08002060 repo_shas = [c.hexsha for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07002061 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07002062 correct_messages = ['initial commit', 'A-1', 'C-1']
James E. Blairbb1fe502014-03-04 10:15:06 -08002063 # Ensure the right commits are in the history for this ref
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002064 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08002065 # Ensure ZUUL_REF -> ZUUL_COMMIT
2066 self.assertEqual(repo_shas[0], commit_C)
James E. Blairc6294a52012-08-17 10:19:48 -07002067
2068 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07002069 for c in repo.iter_commits(ref_B)]
James E. Blairbb1fe502014-03-04 10:15:06 -08002070 repo_shas = [c.hexsha for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07002071 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07002072 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002073 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08002074 self.assertEqual(repo_shas[0], commit_B)
2075
2076 repo_messages = [c.message.strip()
2077 for c in repo.iter_commits(ref_A)]
2078 repo_shas = [c.hexsha for c in repo.iter_commits(ref_A)]
2079 repo_messages.reverse()
2080 correct_messages = ['initial commit', 'A-1']
2081 self.assertEqual(repo_messages, correct_messages)
2082 self.assertEqual(repo_shas[0], commit_A)
2083
2084 self.assertNotEqual(ref_A, ref_B, ref_C)
2085 self.assertNotEqual(commit_A, commit_B, commit_C)
James E. Blair7f71c802012-08-22 13:04:32 -07002086
2087 def test_one_job_project(self):
2088 "Test that queueing works with one job"
2089 A = self.fake_gerrit.addFakeChange('org/one-job-project',
2090 'master', 'A')
2091 B = self.fake_gerrit.addFakeChange('org/one-job-project',
2092 'master', 'B')
2093 A.addApproval('CRVW', 2)
2094 B.addApproval('CRVW', 2)
2095 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2096 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2097 self.waitUntilSettled()
2098
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002099 self.assertEqual(A.data['status'], 'MERGED')
2100 self.assertEqual(A.reported, 2)
2101 self.assertEqual(B.data['status'], 'MERGED')
2102 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07002103
Antoine Musso80edd5a2013-02-13 15:37:53 +01002104 def test_job_from_templates_launched(self):
2105 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002106
Antoine Musso80edd5a2013-02-13 15:37:53 +01002107 A = self.fake_gerrit.addFakeChange(
2108 'org/templated-project', 'master', 'A')
2109 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2110 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01002111
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002112 self.assertEqual(self.getJobFromHistory('project-test1').result,
2113 'SUCCESS')
2114 self.assertEqual(self.getJobFromHistory('project-test2').result,
2115 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01002116
James E. Blair3e98c022013-12-16 15:25:38 -08002117 def test_layered_templates(self):
2118 "Test whether a job generated via a template can be launched"
2119
2120 A = self.fake_gerrit.addFakeChange(
2121 'org/layered-project', 'master', 'A')
2122 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2123 self.waitUntilSettled()
2124
2125 self.assertEqual(self.getJobFromHistory('project-test1').result,
2126 'SUCCESS')
2127 self.assertEqual(self.getJobFromHistory('project-test2').result,
2128 'SUCCESS')
James E. Blairaea6cf62013-12-16 15:38:12 -08002129 self.assertEqual(self.getJobFromHistory('layered-project-test3'
2130 ).result, 'SUCCESS')
2131 self.assertEqual(self.getJobFromHistory('layered-project-test4'
2132 ).result, 'SUCCESS')
James E. Blair12a92b12014-03-26 11:54:53 -07002133 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
2134 ).result, 'SUCCESS')
James E. Blair3e98c022013-12-16 15:25:38 -08002135 self.assertEqual(self.getJobFromHistory('project-test6').result,
2136 'SUCCESS')
2137
James E. Blaircaec0c52012-08-22 14:52:22 -07002138 def test_dependent_changes_dequeue(self):
2139 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002140
James E. Blaircaec0c52012-08-22 14:52:22 -07002141 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2142 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2143 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2144 A.addApproval('CRVW', 2)
2145 B.addApproval('CRVW', 2)
2146 C.addApproval('CRVW', 2)
2147
2148 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
2149 M1.setMerged()
2150
2151 # C -> B -> A -> M1
2152
2153 C.setDependsOn(B, 1)
2154 B.setDependsOn(A, 1)
2155 A.setDependsOn(M1, 1)
2156
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002157 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07002158
2159 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2160 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2161 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2162
2163 self.waitUntilSettled()
2164
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002165 self.assertEqual(A.data['status'], 'NEW')
2166 self.assertEqual(A.reported, 2)
2167 self.assertEqual(B.data['status'], 'NEW')
2168 self.assertEqual(B.reported, 2)
2169 self.assertEqual(C.data['status'], 'NEW')
2170 self.assertEqual(C.reported, 2)
2171 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07002172
James E. Blair972e3c72013-08-29 12:04:55 -07002173 def test_failing_dependent_changes(self):
2174 "Test that failing dependent patches are taken out of stream"
2175 self.worker.hold_jobs_in_build = True
2176 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2177 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2178 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2179 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2180 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
2181 A.addApproval('CRVW', 2)
2182 B.addApproval('CRVW', 2)
2183 C.addApproval('CRVW', 2)
2184 D.addApproval('CRVW', 2)
2185 E.addApproval('CRVW', 2)
2186
2187 # E, D -> C -> B, A
2188
2189 D.setDependsOn(C, 1)
2190 C.setDependsOn(B, 1)
2191
2192 self.worker.addFailTest('project-test1', B)
2193
2194 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2195 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2196 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2197 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2198 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2199
2200 self.waitUntilSettled()
2201 self.worker.release('.*-merge')
2202 self.waitUntilSettled()
2203 self.worker.release('.*-merge')
2204 self.waitUntilSettled()
2205 self.worker.release('.*-merge')
2206 self.waitUntilSettled()
2207 self.worker.release('.*-merge')
2208 self.waitUntilSettled()
2209 self.worker.release('.*-merge')
2210 self.waitUntilSettled()
2211
2212 self.worker.hold_jobs_in_build = False
2213 for build in self.builds:
2214 if build.parameters['ZUUL_CHANGE'] != '1':
2215 build.release()
2216 self.waitUntilSettled()
2217
2218 self.worker.release()
2219 self.waitUntilSettled()
2220
2221 self.assertEqual(A.data['status'], 'MERGED')
2222 self.assertEqual(A.reported, 2)
2223 self.assertEqual(B.data['status'], 'NEW')
2224 self.assertEqual(B.reported, 2)
2225 self.assertEqual(C.data['status'], 'NEW')
2226 self.assertEqual(C.reported, 2)
2227 self.assertEqual(D.data['status'], 'NEW')
2228 self.assertEqual(D.reported, 2)
2229 self.assertEqual(E.data['status'], 'MERGED')
2230 self.assertEqual(E.reported, 2)
2231 self.assertEqual(len(self.history), 18)
2232
James E. Blairec590122012-08-22 15:19:31 -07002233 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08002234 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07002235 # If it's dequeued more than once, we should see extra
2236 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07002237
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002238 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07002239 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2240 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2241 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2242 A.addApproval('CRVW', 2)
2243 B.addApproval('CRVW', 2)
2244 C.addApproval('CRVW', 2)
2245
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002246 self.worker.addFailTest('project1-test1', A)
2247 self.worker.addFailTest('project1-test2', A)
2248 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07002249
2250 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2251 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2252 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2253
2254 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07002255
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002256 self.assertEqual(len(self.builds), 1)
2257 self.assertEqual(self.builds[0].name, 'project1-merge')
2258 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07002259
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002260 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002261 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002262 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002263 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002264 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002265 self.waitUntilSettled()
2266
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002267 self.assertEqual(len(self.builds), 9)
2268 self.assertEqual(self.builds[0].name, 'project1-test1')
2269 self.assertEqual(self.builds[1].name, 'project1-test2')
2270 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
2271 self.assertEqual(self.builds[3].name, 'project1-test1')
2272 self.assertEqual(self.builds[4].name, 'project1-test2')
2273 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
2274 self.assertEqual(self.builds[6].name, 'project1-test1')
2275 self.assertEqual(self.builds[7].name, 'project1-test2')
2276 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07002277
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002278 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07002279 self.waitUntilSettled()
2280
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002281 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
2282 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07002283
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002284 self.worker.hold_jobs_in_build = False
2285 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07002286 self.waitUntilSettled()
2287
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002288 self.assertEqual(len(self.builds), 0)
2289 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07002290
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002291 self.assertEqual(A.data['status'], 'NEW')
2292 self.assertEqual(B.data['status'], 'MERGED')
2293 self.assertEqual(C.data['status'], 'MERGED')
2294 self.assertEqual(A.reported, 2)
2295 self.assertEqual(B.reported, 2)
2296 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07002297
2298 def test_nonvoting_job(self):
2299 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002300
James E. Blair4ec821f2012-08-23 15:28:28 -07002301 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
2302 'master', 'A')
2303 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002304 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07002305 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2306
2307 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07002308
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002309 self.assertEqual(A.data['status'], 'MERGED')
2310 self.assertEqual(A.reported, 2)
2311 self.assertEqual(
2312 self.getJobFromHistory('nonvoting-project-merge').result,
2313 'SUCCESS')
2314 self.assertEqual(
2315 self.getJobFromHistory('nonvoting-project-test1').result,
2316 'SUCCESS')
2317 self.assertEqual(
2318 self.getJobFromHistory('nonvoting-project-test2').result,
2319 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07002320
2321 def test_check_queue_success(self):
2322 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002323
James E. Blaire0487072012-08-29 17:38:31 -07002324 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2325 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,
2332 'SUCCESS')
2333 self.assertEqual(self.getJobFromHistory('project-test1').result,
2334 'SUCCESS')
2335 self.assertEqual(self.getJobFromHistory('project-test2').result,
2336 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07002337
2338 def test_check_queue_failure(self):
2339 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002340
James E. Blaire0487072012-08-29 17:38:31 -07002341 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002342 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07002343 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2344
2345 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002346
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002347 self.assertEqual(A.data['status'], 'NEW')
2348 self.assertEqual(A.reported, 1)
2349 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07002350 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002351 self.assertEqual(self.getJobFromHistory('project-test1').result,
2352 'SUCCESS')
2353 self.assertEqual(self.getJobFromHistory('project-test2').result,
2354 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07002355
2356 def test_dependent_behind_dequeue(self):
2357 "test that dependent changes behind dequeued changes work"
2358 # This complicated test is a reproduction of a real life bug
2359 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07002360
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002361 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07002362 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2363 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2364 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2365 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2366 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
2367 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
2368 D.setDependsOn(C, 1)
2369 E.setDependsOn(D, 1)
2370 A.addApproval('CRVW', 2)
2371 B.addApproval('CRVW', 2)
2372 C.addApproval('CRVW', 2)
2373 D.addApproval('CRVW', 2)
2374 E.addApproval('CRVW', 2)
2375 F.addApproval('CRVW', 2)
2376
2377 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07002378
2379 # Change object re-use in the gerrit trigger is hidden if
2380 # changes are added in quick succession; waiting makes it more
2381 # like real life.
2382 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2383 self.waitUntilSettled()
2384 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2385 self.waitUntilSettled()
2386
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 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2393 self.waitUntilSettled()
2394 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2395 self.waitUntilSettled()
2396 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2397 self.waitUntilSettled()
2398 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
2399 self.waitUntilSettled()
2400
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002401 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002402 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002403 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002404 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002405 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002406 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002407 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002408 self.waitUntilSettled()
2409
2410 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002411
2412 # Grab pointers to the jobs we want to release before
2413 # releasing any, because list indexes may change as
2414 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002415 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002416 a.release()
2417 b.release()
2418 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002419 self.waitUntilSettled()
2420
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002421 self.worker.hold_jobs_in_build = False
2422 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002423 self.waitUntilSettled()
2424
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002425 self.assertEqual(A.data['status'], 'NEW')
2426 self.assertEqual(B.data['status'], 'MERGED')
2427 self.assertEqual(C.data['status'], 'MERGED')
2428 self.assertEqual(D.data['status'], 'MERGED')
2429 self.assertEqual(E.data['status'], 'MERGED')
2430 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002431
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002432 self.assertEqual(A.reported, 2)
2433 self.assertEqual(B.reported, 2)
2434 self.assertEqual(C.reported, 2)
2435 self.assertEqual(D.reported, 2)
2436 self.assertEqual(E.reported, 2)
2437 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002438
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002439 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2440 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002441
2442 def test_merger_repack(self):
2443 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002444
James E. Blair05fed602012-09-07 12:45:24 -07002445 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. Blair05fed602012-09-07 12:45:24 -07002457 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002458 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002459
Monty Taylorbc758832013-06-17 17:22:42 -04002460 path = os.path.join(self.git_root, "org/project")
2461 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002462
2463 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2464 A.addApproval('CRVW', 2)
2465 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2466 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002467 self.assertEqual(self.getJobFromHistory('project-merge').result,
2468 'SUCCESS')
2469 self.assertEqual(self.getJobFromHistory('project-test1').result,
2470 'SUCCESS')
2471 self.assertEqual(self.getJobFromHistory('project-test2').result,
2472 'SUCCESS')
2473 self.assertEqual(A.data['status'], 'MERGED')
2474 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002475
James E. Blair4886f282012-11-15 09:27:33 -08002476 def test_merger_repack_large_change(self):
2477 "Test that the merger works with large changes after a repack"
2478 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08002479 # This test assumes the repo is already cloned; make sure it is
2480 url = self.sched.triggers['gerrit'].getGitUrl(
2481 self.sched.layout.projects['org/project1'])
James E. Blair4076e2b2014-01-28 12:42:20 -08002482 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08002483 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2484 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002485 path = os.path.join(self.upstream_root, "org/project1")
2486 print repack_repo(path)
2487 path = os.path.join(self.git_root, "org/project1")
2488 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002489
2490 A.addApproval('CRVW', 2)
2491 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2492 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002493 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2494 'SUCCESS')
2495 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2496 'SUCCESS')
2497 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2498 'SUCCESS')
2499 self.assertEqual(A.data['status'], 'MERGED')
2500 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002501
James E. Blair7ee88a22012-09-12 18:59:31 +02002502 def test_nonexistent_job(self):
2503 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002504 # Set to the state immediately after a restart
2505 self.resetGearmanServer()
2506 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002507
2508 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2509 A.addApproval('CRVW', 2)
2510 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2511 # There may be a thread about to report a lost change
2512 while A.reported < 2:
2513 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002514 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002515 self.assertFalse(job_names)
2516 self.assertEqual(A.data['status'], 'NEW')
2517 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002518 self.assertEmptyQueues()
2519
2520 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002521 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002522 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2523 A.addApproval('CRVW', 2)
2524 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2525 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002526 self.assertEqual(self.getJobFromHistory('project-merge').result,
2527 'SUCCESS')
2528 self.assertEqual(self.getJobFromHistory('project-test1').result,
2529 'SUCCESS')
2530 self.assertEqual(self.getJobFromHistory('project-test2').result,
2531 'SUCCESS')
2532 self.assertEqual(A.data['status'], 'MERGED')
2533 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002534
2535 def test_single_nonexistent_post_job(self):
2536 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002537 e = {
2538 "type": "ref-updated",
2539 "submitter": {
2540 "name": "User Name",
2541 },
2542 "refUpdate": {
2543 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2544 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2545 "refName": "master",
2546 "project": "org/project",
2547 }
2548 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002549 # Set to the state immediately after a restart
2550 self.resetGearmanServer()
2551 self.launcher.negative_function_cache_ttl = 0
2552
James E. Blairf62d4282012-12-31 17:01:50 -08002553 self.fake_gerrit.addEvent(e)
2554 self.waitUntilSettled()
2555
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002556 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002557
2558 def test_new_patchset_dequeues_old(self):
2559 "Test that a new patchset causes the old to be dequeued"
2560 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002561 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002562 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2563 M.setMerged()
2564
2565 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2566 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2567 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2568 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2569 A.addApproval('CRVW', 2)
2570 B.addApproval('CRVW', 2)
2571 C.addApproval('CRVW', 2)
2572 D.addApproval('CRVW', 2)
2573
2574 C.setDependsOn(B, 1)
2575 B.setDependsOn(A, 1)
2576 A.setDependsOn(M, 1)
2577
2578 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2579 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2580 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2581 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2582 self.waitUntilSettled()
2583
2584 B.addPatchset()
2585 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2586 self.waitUntilSettled()
2587
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002588 self.worker.hold_jobs_in_build = False
2589 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002590 self.waitUntilSettled()
2591
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002592 self.assertEqual(A.data['status'], 'MERGED')
2593 self.assertEqual(A.reported, 2)
2594 self.assertEqual(B.data['status'], 'NEW')
2595 self.assertEqual(B.reported, 2)
2596 self.assertEqual(C.data['status'], 'NEW')
2597 self.assertEqual(C.reported, 2)
2598 self.assertEqual(D.data['status'], 'MERGED')
2599 self.assertEqual(D.reported, 2)
2600 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002601
Arx Cruzb1b010d2013-10-28 19:49:59 -02002602 def test_zuul_url_return(self):
2603 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08002604 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Arx Cruzb1b010d2013-10-28 19:49:59 -02002605 self.worker.hold_jobs_in_build = True
2606
2607 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2608 A.addApproval('CRVW', 2)
2609 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2610 self.waitUntilSettled()
2611
2612 self.assertEqual(len(self.builds), 1)
2613 for build in self.builds:
2614 self.assertTrue('ZUUL_URL' in build.parameters)
2615
2616 self.worker.hold_jobs_in_build = False
2617 self.worker.release()
2618 self.waitUntilSettled()
2619
James E. Blair2fa50962013-01-30 21:50:41 -08002620 def test_new_patchset_dequeues_old_on_head(self):
2621 "Test that a new patchset causes the old to be dequeued (at head)"
2622 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002623 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002624 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2625 M.setMerged()
2626 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2627 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2628 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2629 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2630 A.addApproval('CRVW', 2)
2631 B.addApproval('CRVW', 2)
2632 C.addApproval('CRVW', 2)
2633 D.addApproval('CRVW', 2)
2634
2635 C.setDependsOn(B, 1)
2636 B.setDependsOn(A, 1)
2637 A.setDependsOn(M, 1)
2638
2639 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2640 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2641 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2642 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2643 self.waitUntilSettled()
2644
2645 A.addPatchset()
2646 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2647 self.waitUntilSettled()
2648
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002649 self.worker.hold_jobs_in_build = False
2650 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002651 self.waitUntilSettled()
2652
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002653 self.assertEqual(A.data['status'], 'NEW')
2654 self.assertEqual(A.reported, 2)
2655 self.assertEqual(B.data['status'], 'NEW')
2656 self.assertEqual(B.reported, 2)
2657 self.assertEqual(C.data['status'], 'NEW')
2658 self.assertEqual(C.reported, 2)
2659 self.assertEqual(D.data['status'], 'MERGED')
2660 self.assertEqual(D.reported, 2)
2661 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002662
2663 def test_new_patchset_dequeues_old_without_dependents(self):
2664 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002665 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002666 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2667 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2668 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2669 A.addApproval('CRVW', 2)
2670 B.addApproval('CRVW', 2)
2671 C.addApproval('CRVW', 2)
2672
2673 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2674 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2675 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2676 self.waitUntilSettled()
2677
2678 B.addPatchset()
2679 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2680 self.waitUntilSettled()
2681
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002682 self.worker.hold_jobs_in_build = False
2683 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002684 self.waitUntilSettled()
2685
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002686 self.assertEqual(A.data['status'], 'MERGED')
2687 self.assertEqual(A.reported, 2)
2688 self.assertEqual(B.data['status'], 'NEW')
2689 self.assertEqual(B.reported, 2)
2690 self.assertEqual(C.data['status'], 'MERGED')
2691 self.assertEqual(C.reported, 2)
2692 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002693
2694 def test_new_patchset_dequeues_old_independent_queue(self):
2695 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002696 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002697 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2698 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2699 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2700 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2701 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2702 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2703 self.waitUntilSettled()
2704
2705 B.addPatchset()
2706 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2707 self.waitUntilSettled()
2708
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002709 self.worker.hold_jobs_in_build = False
2710 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002711 self.waitUntilSettled()
2712
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002713 self.assertEqual(A.data['status'], 'NEW')
2714 self.assertEqual(A.reported, 1)
2715 self.assertEqual(B.data['status'], 'NEW')
2716 self.assertEqual(B.reported, 1)
2717 self.assertEqual(C.data['status'], 'NEW')
2718 self.assertEqual(C.reported, 1)
2719 self.assertEqual(len(self.history), 10)
2720 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002721
James E. Blair18c64442014-03-18 10:14:45 -07002722 def test_noop_job(self):
2723 "Test that the internal noop job works"
2724 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
2725 A.addApproval('CRVW', 2)
2726 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2727 self.waitUntilSettled()
2728
2729 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2730 self.assertTrue(self.sched._areAllBuildsComplete())
2731 self.assertEqual(len(self.history), 0)
2732 self.assertEqual(A.data['status'], 'MERGED')
2733 self.assertEqual(A.reported, 2)
2734
James E. Blair7d0dedc2013-02-21 17:26:09 -08002735 def test_zuul_refs(self):
2736 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002737 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002738 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2739 M1.setMerged()
2740 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2741 M2.setMerged()
2742
2743 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2744 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2745 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2746 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2747 A.addApproval('CRVW', 2)
2748 B.addApproval('CRVW', 2)
2749 C.addApproval('CRVW', 2)
2750 D.addApproval('CRVW', 2)
2751 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2752 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2753 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2754 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2755
2756 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002757 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002758 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002759 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002760 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002761 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002762 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002763 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002764 self.waitUntilSettled()
2765
James E. Blair7d0dedc2013-02-21 17:26:09 -08002766 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002767 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002768 if x.parameters['ZUUL_CHANGE'] == '3':
2769 a_zref = x.parameters['ZUUL_REF']
2770 if x.parameters['ZUUL_CHANGE'] == '4':
2771 b_zref = x.parameters['ZUUL_REF']
2772 if x.parameters['ZUUL_CHANGE'] == '5':
2773 c_zref = x.parameters['ZUUL_REF']
2774 if x.parameters['ZUUL_CHANGE'] == '6':
2775 d_zref = x.parameters['ZUUL_REF']
2776
2777 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002778 self.assertIsNotNone(a_zref)
2779 self.assertIsNotNone(b_zref)
2780 self.assertIsNotNone(c_zref)
2781 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002782
2783 # And they should all be different
2784 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002785 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002786
2787 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002788 self.assertTrue(self.ref_has_change(a_zref, A))
2789 self.assertFalse(self.ref_has_change(a_zref, B))
2790 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002791
2792 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002793 self.assertTrue(self.ref_has_change(b_zref, A))
2794 self.assertTrue(self.ref_has_change(b_zref, B))
2795 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002796
2797 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002798 self.assertTrue(self.ref_has_change(c_zref, A))
2799 self.assertTrue(self.ref_has_change(c_zref, B))
2800 self.assertTrue(self.ref_has_change(c_zref, C))
2801 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002802
2803 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002804 self.assertTrue(self.ref_has_change(d_zref, A))
2805 self.assertTrue(self.ref_has_change(d_zref, B))
2806 self.assertTrue(self.ref_has_change(d_zref, C))
2807 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002808
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002809 self.worker.hold_jobs_in_build = False
2810 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002811 self.waitUntilSettled()
2812
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002813 self.assertEqual(A.data['status'], 'MERGED')
2814 self.assertEqual(A.reported, 2)
2815 self.assertEqual(B.data['status'], 'MERGED')
2816 self.assertEqual(B.reported, 2)
2817 self.assertEqual(C.data['status'], 'MERGED')
2818 self.assertEqual(C.reported, 2)
2819 self.assertEqual(D.data['status'], 'MERGED')
2820 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002821
James E. Blair11041d22014-05-02 14:49:53 -07002822 def test_pipeline_requirements_approval_check_and_gate(self):
2823 "Test pipeline requirements triggers both check and gate"
2824 self.config.set('zuul', 'layout_config',
2825 'tests/fixtures/layout-pipeline-requirements.yaml')
2826 self.sched.reconfigure(self.config)
2827 self.registerJobs()
2828 self._test_required_approval_check_and_gate()
2829
James E. Blairc053d022014-01-22 14:57:33 -08002830 def test_required_approval_check_and_gate(self):
2831 "Test required-approval triggers both check and gate"
2832 self.config.set('zuul', 'layout_config',
2833 'tests/fixtures/layout-require-approval.yaml')
2834 self.sched.reconfigure(self.config)
2835 self.registerJobs()
James E. Blair11041d22014-05-02 14:49:53 -07002836 self._test_required_approval_check_and_gate()
James E. Blairc053d022014-01-22 14:57:33 -08002837
James E. Blair11041d22014-05-02 14:49:53 -07002838 def _test_required_approval_check_and_gate(self):
James E. Blairc053d022014-01-22 14:57:33 -08002839 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2840 A.addApproval('CRVW', 2)
2841 # Add a too-old +1
2842 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2843
2844 aprv = A.addApproval('APRV', 1)
2845 self.fake_gerrit.addEvent(aprv)
2846 self.waitUntilSettled()
2847 # Should have run a check job
2848 self.assertEqual(len(self.history), 1)
2849 self.assertEqual(self.history[0].name, 'project-check')
2850
2851 # Report the result of that check job (overrides previous vrfy)
2852 # Skynet alert: this should trigger a gate job now that
2853 # all reqs are met
2854 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2855 self.waitUntilSettled()
2856 self.assertEqual(len(self.history), 2)
2857 self.assertEqual(self.history[1].name, 'project-gate')
2858
James E. Blair11041d22014-05-02 14:49:53 -07002859 def test_pipeline_requirements_approval_newer(self):
2860 "Test pipeline requirements newer trigger parameter"
2861 self.config.set('zuul', 'layout_config',
2862 'tests/fixtures/layout-pipeline-requirements.yaml')
2863 self.sched.reconfigure(self.config)
2864 self.registerJobs()
2865 self._test_required_approval_newer()
2866
James E. Blairc053d022014-01-22 14:57:33 -08002867 def test_required_approval_newer(self):
2868 "Test required-approval newer trigger parameter"
2869 self.config.set('zuul', 'layout_config',
2870 'tests/fixtures/layout-require-approval.yaml')
2871 self.sched.reconfigure(self.config)
2872 self.registerJobs()
James E. Blair11041d22014-05-02 14:49:53 -07002873 self._test_required_approval_newer()
2874
2875 def _test_required_approval_newer(self):
2876 self.config.set('zuul', 'layout_config',
2877 'tests/fixtures/layout-require-approval.yaml')
2878 self.sched.reconfigure(self.config)
2879 self.registerJobs()
James E. Blairc053d022014-01-22 14:57:33 -08002880
2881 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2882 A.addApproval('CRVW', 2)
2883 aprv = A.addApproval('APRV', 1)
2884 self.fake_gerrit.addEvent(aprv)
2885 self.waitUntilSettled()
2886 # No +1 from Jenkins so should not be enqueued
2887 self.assertEqual(len(self.history), 0)
2888
2889 # Add a too-old +1, should trigger check but not gate
2890 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2891 self.fake_gerrit.addEvent(aprv)
2892 self.waitUntilSettled()
2893 self.assertEqual(len(self.history), 1)
2894 self.assertEqual(self.history[0].name, 'project-check')
2895
2896 # Add a recent +1
2897 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2898 self.fake_gerrit.addEvent(aprv)
2899 self.waitUntilSettled()
2900 self.assertEqual(len(self.history), 2)
2901 self.assertEqual(self.history[1].name, 'project-gate')
2902
James E. Blair11041d22014-05-02 14:49:53 -07002903 def test_pipeline_requirements_approval_older(self):
2904 "Test pipeline requirements older trigger parameter"
2905 self.config.set('zuul', 'layout_config',
2906 'tests/fixtures/layout-pipeline-requirements.yaml')
2907 self.sched.reconfigure(self.config)
2908 self.registerJobs()
2909 self._test_required_approval_older()
2910
James E. Blairc053d022014-01-22 14:57:33 -08002911 def test_required_approval_older(self):
2912 "Test required-approval older trigger parameter"
2913 self.config.set('zuul', 'layout_config',
2914 'tests/fixtures/layout-require-approval.yaml')
2915 self.sched.reconfigure(self.config)
2916 self.registerJobs()
James E. Blair11041d22014-05-02 14:49:53 -07002917 self._test_required_approval_older()
2918
2919 def _test_required_approval_older(self):
2920 self.config.set('zuul', 'layout_config',
2921 'tests/fixtures/layout-require-approval.yaml')
2922 self.sched.reconfigure(self.config)
2923 self.registerJobs()
James E. Blairc053d022014-01-22 14:57:33 -08002924
2925 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2926 crvw = A.addApproval('CRVW', 2)
2927 self.fake_gerrit.addEvent(crvw)
2928 self.waitUntilSettled()
2929 # No +1 from Jenkins so should not be enqueued
2930 self.assertEqual(len(self.history), 0)
2931
2932 # Add an old +1 and trigger check with a comment
2933 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2934 self.fake_gerrit.addEvent(crvw)
2935 self.waitUntilSettled()
2936 self.assertEqual(len(self.history), 1)
2937 self.assertEqual(self.history[0].name, 'project-check')
2938
2939 # Add a recent +1 and make sure nothing changes
2940 A.addApproval('VRFY', 1)
2941 self.fake_gerrit.addEvent(crvw)
2942 self.waitUntilSettled()
2943 self.assertEqual(len(self.history), 1)
2944
2945 # The last thing we did was query a change then do nothing
2946 # with a pipeline, so it will be in the cache; clean it up so
2947 # it does not fail the test.
2948 for pipeline in self.sched.layout.pipelines.values():
2949 pipeline.trigger.maintainCache([])
2950
James E. Blair4a28a882013-08-23 15:17:33 -07002951 def test_rerun_on_error(self):
2952 "Test that if a worker fails to run a job, it is run again"
2953 self.worker.hold_jobs_in_build = True
2954 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2955 A.addApproval('CRVW', 2)
2956 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2957 self.waitUntilSettled()
2958
2959 self.builds[0].run_error = True
2960 self.worker.hold_jobs_in_build = False
2961 self.worker.release()
2962 self.waitUntilSettled()
2963 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2964 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2965
James E. Blair412e5582013-04-22 15:50:12 -07002966 def test_statsd(self):
2967 "Test each of the statsd methods used in the scheduler"
2968 import extras
2969 statsd = extras.try_import('statsd.statsd')
2970 statsd.incr('test-incr')
2971 statsd.timing('test-timing', 3)
2972 statsd.gauge('test-guage', 12)
2973 self.assertReportedStat('test-incr', '1|c')
2974 self.assertReportedStat('test-timing', '3|ms')
2975 self.assertReportedStat('test-guage', '12|g')
2976
James E. Blairdad52252014-02-07 16:59:17 -08002977 def test_stuck_job_cleanup(self):
2978 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07002979 # This job won't be registered at startup because it is not in
2980 # the standard layout, but we need it to already be registerd
2981 # for when we reconfigure, as that is when Zuul will attempt
2982 # to run the new job.
2983 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002984 self.gearman_server.hold_jobs_in_queue = True
2985 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2986 A.addApproval('CRVW', 2)
2987 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2988 self.waitUntilSettled()
2989 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2990
2991 self.config.set('zuul', 'layout_config',
2992 'tests/fixtures/layout-no-jobs.yaml')
2993 self.sched.reconfigure(self.config)
2994 self.waitUntilSettled()
2995
James E. Blair18c64442014-03-18 10:14:45 -07002996 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002997 self.waitUntilSettled()
2998 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2999 self.assertTrue(self.sched._areAllBuildsComplete())
3000
3001 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07003002 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08003003 self.assertEqual(self.history[0].result, 'SUCCESS')
3004
James E. Blair70c71582013-03-06 08:50:50 -08003005 def test_file_jobs(self):
3006 "Test that file jobs run only when appropriate"
3007 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3008 A.addPatchset(['pip-requires'])
3009 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3010 A.addApproval('CRVW', 2)
3011 B.addApproval('CRVW', 2)
3012 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3013 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3014 self.waitUntilSettled()
3015
Monty Taylor6bef8ef2013-06-02 08:17:12 -04003016 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08003017 if x.name == 'project-testfile']
3018
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003019 self.assertEqual(len(testfile_jobs), 1)
3020 self.assertEqual(testfile_jobs[0].changes, '1,2')
3021 self.assertEqual(A.data['status'], 'MERGED')
3022 self.assertEqual(A.reported, 2)
3023 self.assertEqual(B.data['status'], 'MERGED')
3024 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07003025
3026 def test_test_config(self):
3027 "Test that we can test the config"
3028 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07003029 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07003030 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07003031 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003032
3033 def test_build_description(self):
3034 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003035 self.worker.registerFunction('set_description:' +
3036 self.worker.worker_id)
3037
3038 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3039 A.addApproval('CRVW', 2)
3040 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3041 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04003042 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003043 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003044 self.assertTrue(re.search("Branch.*master", desc))
3045 self.assertTrue(re.search("Pipeline.*gate", desc))
3046 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
3047 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
3048 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
3049 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003050
James E. Blairc8a1e052014-02-25 09:29:26 -08003051 def test_queue_names(self):
3052 "Test shared change queue names"
3053 project1 = self.sched.layout.projects['org/project1']
3054 project2 = self.sched.layout.projects['org/project2']
3055 q1 = self.sched.layout.pipelines['gate'].getQueue(project1)
3056 q2 = self.sched.layout.pipelines['gate'].getQueue(project2)
3057 self.assertEqual(q1.name, 'integration')
3058 self.assertEqual(q2.name, 'integration')
3059
3060 self.config.set('zuul', 'layout_config',
3061 'tests/fixtures/layout-bad-queue.yaml')
3062 with testtools.ExpectedException(
3063 Exception, "More than one name assigned to change queue"):
3064 self.sched.reconfigure(self.config)
3065
James E. Blair64ed6f22013-07-10 14:07:23 -07003066 def test_queue_precedence(self):
3067 "Test that queue precedence works"
3068
3069 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07003070 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07003071 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3072 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3073 A.addApproval('CRVW', 2)
3074 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3075
3076 self.waitUntilSettled()
3077 self.gearman_server.hold_jobs_in_queue = False
3078 self.gearman_server.release()
3079 self.waitUntilSettled()
3080
James E. Blair8de58bd2013-07-18 16:23:33 -07003081 # Run one build at a time to ensure non-race order:
3082 for x in range(6):
3083 self.release(self.builds[0])
3084 self.waitUntilSettled()
3085 self.worker.hold_jobs_in_build = False
3086 self.waitUntilSettled()
3087
James E. Blair64ed6f22013-07-10 14:07:23 -07003088 self.log.debug(self.history)
3089 self.assertEqual(self.history[0].pipeline, 'gate')
3090 self.assertEqual(self.history[1].pipeline, 'check')
3091 self.assertEqual(self.history[2].pipeline, 'gate')
3092 self.assertEqual(self.history[3].pipeline, 'gate')
3093 self.assertEqual(self.history[4].pipeline, 'check')
3094 self.assertEqual(self.history[5].pipeline, 'check')
3095
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04003096 def test_json_status(self, compressed=False):
James E. Blair1843a552013-07-03 14:19:52 -07003097 "Test that we can retrieve JSON status info"
3098 self.worker.hold_jobs_in_build = True
3099 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3100 A.addApproval('CRVW', 2)
3101 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3102 self.waitUntilSettled()
3103
3104 port = self.webapp.server.socket.getsockname()[1]
3105
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04003106 req = urllib2.Request("http://localhost:%s/status.json" % port)
3107 if compressed:
3108 req.add_header("accept-encoding", "gzip")
3109 f = urllib2.urlopen(req)
James E. Blair1843a552013-07-03 14:19:52 -07003110 data = f.read()
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04003111 if compressed:
3112 gz = gzip.GzipFile(fileobj=StringIO(data))
3113 data = gz.read()
James E. Blair1843a552013-07-03 14:19:52 -07003114
3115 self.worker.hold_jobs_in_build = False
3116 self.worker.release()
3117 self.waitUntilSettled()
3118
3119 data = json.loads(data)
3120 status_jobs = set()
3121 for p in data['pipelines']:
3122 for q in p['change_queues']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08003123 if q['dependent']:
3124 self.assertEqual(q['window'], 20)
3125 else:
3126 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07003127 for head in q['heads']:
3128 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08003129 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003130 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07003131 for job in change['jobs']:
3132 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003133 self.assertIn('project-merge', status_jobs)
3134 self.assertIn('project-test1', status_jobs)
3135 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07003136
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04003137 def test_json_status_gzip(self):
3138 self.test_json_status(True)
3139
James E. Blairc3d428e2013-12-03 15:06:48 -08003140 def test_merging_queues(self):
3141 "Test that transitively-connected change queues are merged"
3142 self.config.set('zuul', 'layout_config',
3143 'tests/fixtures/layout-merge-queues.yaml')
3144 self.sched.reconfigure(self.config)
3145 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
3146
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003147 def test_node_label(self):
3148 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003149 self.worker.registerFunction('build:node-project-test1:debian')
3150
3151 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
3152 A.addApproval('CRVW', 2)
3153 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3154 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07003155
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003156 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3157 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3158 'debian')
3159 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07003160
3161 def test_live_reconfiguration(self):
3162 "Test that live reconfiguration works"
3163 self.worker.hold_jobs_in_build = True
3164 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3165 A.addApproval('CRVW', 2)
3166 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3167 self.waitUntilSettled()
3168
3169 self.sched.reconfigure(self.config)
3170
3171 self.worker.hold_jobs_in_build = False
3172 self.worker.release()
3173 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003174 self.assertEqual(self.getJobFromHistory('project-merge').result,
3175 'SUCCESS')
3176 self.assertEqual(self.getJobFromHistory('project-test1').result,
3177 'SUCCESS')
3178 self.assertEqual(self.getJobFromHistory('project-test2').result,
3179 'SUCCESS')
3180 self.assertEqual(A.data['status'], 'MERGED')
3181 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07003182
James E. Blaire712d9f2013-07-31 11:40:11 -07003183 def test_live_reconfiguration_functions(self):
3184 "Test live reconfiguration with a custom function"
3185 self.worker.registerFunction('build:node-project-test1:debian')
3186 self.worker.registerFunction('build:node-project-test1:wheezy')
3187 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
3188 A.addApproval('CRVW', 2)
3189 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3190 self.waitUntilSettled()
3191
3192 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3193 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3194 'debian')
3195 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
3196
3197 self.config.set('zuul', 'layout_config',
3198 'tests/fixtures/layout-live-'
3199 'reconfiguration-functions.yaml')
3200 self.sched.reconfigure(self.config)
3201 self.worker.build_history = []
3202
3203 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
3204 B.addApproval('CRVW', 2)
3205 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3206 self.waitUntilSettled()
3207
3208 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3209 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3210 'wheezy')
3211 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
3212
James E. Blair287c06d2013-07-24 10:39:30 -07003213 def test_delayed_repo_init(self):
3214 self.config.set('zuul', 'layout_config',
3215 'tests/fixtures/layout-delayed-repo-init.yaml')
3216 self.sched.reconfigure(self.config)
3217
3218 self.init_repo("org/new-project")
3219 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
3220
3221 A.addApproval('CRVW', 2)
3222 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3223 self.waitUntilSettled()
3224 self.assertEqual(self.getJobFromHistory('project-merge').result,
3225 'SUCCESS')
3226 self.assertEqual(self.getJobFromHistory('project-test1').result,
3227 'SUCCESS')
3228 self.assertEqual(self.getJobFromHistory('project-test2').result,
3229 'SUCCESS')
3230 self.assertEqual(A.data['status'], 'MERGED')
3231 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003232
Clark Boylan6dbbc482013-10-18 10:57:31 -07003233 def test_repo_deleted(self):
3234 self.config.set('zuul', 'layout_config',
3235 'tests/fixtures/layout-repo-deleted.yaml')
3236 self.sched.reconfigure(self.config)
3237
3238 self.init_repo("org/delete-project")
3239 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
3240
3241 A.addApproval('CRVW', 2)
3242 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3243 self.waitUntilSettled()
3244 self.assertEqual(self.getJobFromHistory('project-merge').result,
3245 'SUCCESS')
3246 self.assertEqual(self.getJobFromHistory('project-test1').result,
3247 'SUCCESS')
3248 self.assertEqual(self.getJobFromHistory('project-test2').result,
3249 'SUCCESS')
3250 self.assertEqual(A.data['status'], 'MERGED')
3251 self.assertEqual(A.reported, 2)
3252
3253 # Delete org/new-project zuul repo. Should be recloned.
3254 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
3255
3256 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
3257
3258 B.addApproval('CRVW', 2)
3259 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3260 self.waitUntilSettled()
3261 self.assertEqual(self.getJobFromHistory('project-merge').result,
3262 'SUCCESS')
3263 self.assertEqual(self.getJobFromHistory('project-test1').result,
3264 'SUCCESS')
3265 self.assertEqual(self.getJobFromHistory('project-test2').result,
3266 'SUCCESS')
3267 self.assertEqual(B.data['status'], 'MERGED')
3268 self.assertEqual(B.reported, 2)
3269
James E. Blair63bb0ef2013-07-29 17:14:51 -07003270 def test_timer(self):
3271 "Test that a periodic job is triggered"
3272 self.worker.hold_jobs_in_build = True
3273 self.config.set('zuul', 'layout_config',
3274 'tests/fixtures/layout-timer.yaml')
3275 self.sched.reconfigure(self.config)
3276 self.registerJobs()
3277
3278 start = time.time()
3279 failed = True
3280 while ((time.time() - start) < 30):
3281 if len(self.builds) == 2:
3282 failed = False
3283 break
3284 else:
3285 time.sleep(1)
3286
3287 if failed:
3288 raise Exception("Expected jobs never ran")
3289
3290 self.waitUntilSettled()
3291 port = self.webapp.server.socket.getsockname()[1]
3292
3293 f = urllib.urlopen("http://localhost:%s/status.json" % port)
3294 data = f.read()
3295
3296 self.worker.hold_jobs_in_build = False
3297 self.worker.release()
3298 self.waitUntilSettled()
3299
3300 self.assertEqual(self.getJobFromHistory(
3301 'project-bitrot-stable-old').result, 'SUCCESS')
3302 self.assertEqual(self.getJobFromHistory(
3303 'project-bitrot-stable-older').result, 'SUCCESS')
3304
3305 data = json.loads(data)
3306 status_jobs = set()
3307 for p in data['pipelines']:
3308 for q in p['change_queues']:
3309 for head in q['heads']:
3310 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07003311 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003312 for job in change['jobs']:
3313 status_jobs.add(job['name'])
3314 self.assertIn('project-bitrot-stable-old', status_jobs)
3315 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003316
James E. Blair4f6033c2014-03-27 15:49:09 -07003317 def test_idle(self):
3318 "Test that frequent periodic jobs work"
3319 self.worker.hold_jobs_in_build = True
3320 self.config.set('zuul', 'layout_config',
3321 'tests/fixtures/layout-idle.yaml')
3322 self.sched.reconfigure(self.config)
3323 self.registerJobs()
3324
3325 # The pipeline triggers every second, so we should have seen
3326 # several by now.
3327 time.sleep(5)
3328 self.waitUntilSettled()
3329 self.assertEqual(len(self.builds), 2)
3330 self.worker.release('.*')
3331 self.waitUntilSettled()
3332 self.assertEqual(len(self.builds), 0)
3333 self.assertEqual(len(self.history), 2)
3334
3335 time.sleep(5)
3336 self.waitUntilSettled()
3337 self.assertEqual(len(self.builds), 2)
3338 self.assertEqual(len(self.history), 2)
3339 self.worker.release('.*')
3340 self.waitUntilSettled()
3341 self.assertEqual(len(self.builds), 0)
3342 self.assertEqual(len(self.history), 4)
3343
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003344 def test_check_smtp_pool(self):
3345 self.config.set('zuul', 'layout_config',
3346 'tests/fixtures/layout-smtp.yaml')
3347 self.sched.reconfigure(self.config)
3348
3349 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3350 self.waitUntilSettled()
3351
3352 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3353 self.waitUntilSettled()
3354
James E. Blairff80a2f2013-12-27 13:24:06 -08003355 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003356
3357 # A.messages only holds what FakeGerrit places in it. Thus we
3358 # work on the knowledge of what the first message should be as
3359 # it is only configured to go to SMTP.
3360
3361 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003362 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003363 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003364 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003365 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003366 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003367
3368 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003369 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003370 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003371 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003372 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003373 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003374
James E. Blaire5910202013-12-27 09:50:31 -08003375 def test_timer_smtp(self):
3376 "Test that a periodic job is triggered"
3377 self.config.set('zuul', 'layout_config',
3378 'tests/fixtures/layout-timer-smtp.yaml')
3379 self.sched.reconfigure(self.config)
3380 self.registerJobs()
3381
3382 start = time.time()
3383 failed = True
3384 while ((time.time() - start) < 30):
3385 if len(self.history) == 2:
3386 failed = False
3387 break
3388 else:
3389 time.sleep(1)
3390
3391 if failed:
3392 raise Exception("Expected jobs never ran")
3393
3394 self.waitUntilSettled()
3395
3396 self.assertEqual(self.getJobFromHistory(
3397 'project-bitrot-stable-old').result, 'SUCCESS')
3398 self.assertEqual(self.getJobFromHistory(
3399 'project-bitrot-stable-older').result, 'SUCCESS')
3400
James E. Blairff80a2f2013-12-27 13:24:06 -08003401 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003402
3403 # A.messages only holds what FakeGerrit places in it. Thus we
3404 # work on the knowledge of what the first message should be as
3405 # it is only configured to go to SMTP.
3406
3407 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003408 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003409 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003410 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003411 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003412 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003413
James E. Blairad28e912013-11-27 10:43:22 -08003414 def test_client_enqueue(self):
3415 "Test that the RPC client can enqueue a change"
3416 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3417 A.addApproval('CRVW', 2)
3418 A.addApproval('APRV', 1)
3419
3420 client = zuul.rpcclient.RPCClient('127.0.0.1',
3421 self.gearman_server.port)
3422 r = client.enqueue(pipeline='gate',
3423 project='org/project',
3424 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003425 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003426 self.waitUntilSettled()
3427 self.assertEqual(self.getJobFromHistory('project-merge').result,
3428 'SUCCESS')
3429 self.assertEqual(self.getJobFromHistory('project-test1').result,
3430 'SUCCESS')
3431 self.assertEqual(self.getJobFromHistory('project-test2').result,
3432 'SUCCESS')
3433 self.assertEqual(A.data['status'], 'MERGED')
3434 self.assertEqual(A.reported, 2)
3435 self.assertEqual(r, True)
3436
3437 def test_client_enqueue_negative(self):
3438 "Test that the RPC client returns errors"
3439 client = zuul.rpcclient.RPCClient('127.0.0.1',
3440 self.gearman_server.port)
3441 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3442 "Invalid project"):
3443 r = client.enqueue(pipeline='gate',
3444 project='project-does-not-exist',
3445 trigger='gerrit',
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 pipeline"):
3452 r = client.enqueue(pipeline='pipeline-does-not-exist',
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 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3460 "Invalid trigger"):
3461 r = client.enqueue(pipeline='gate',
3462 project='org/project',
3463 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003464 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003465 client.shutdown()
3466 self.assertEqual(r, False)
3467
3468 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3469 "Invalid change"):
3470 r = client.enqueue(pipeline='gate',
3471 project='org/project',
3472 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003473 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003474 client.shutdown()
3475 self.assertEqual(r, False)
3476
3477 self.waitUntilSettled()
3478 self.assertEqual(len(self.history), 0)
3479 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003480
3481 def test_client_promote(self):
3482 "Test that the RPC client can promote a change"
3483 self.worker.hold_jobs_in_build = True
3484 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3485 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3486 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3487 A.addApproval('CRVW', 2)
3488 B.addApproval('CRVW', 2)
3489 C.addApproval('CRVW', 2)
3490
3491 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3492 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3493 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3494
3495 self.waitUntilSettled()
3496
Sean Daguef39b9ca2014-01-10 21:34:35 -05003497 items = self.sched.layout.pipelines['gate'].getAllItems()
3498 enqueue_times = {}
3499 for item in items:
3500 enqueue_times[str(item.change)] = item.enqueue_time
3501
James E. Blair36658cf2013-12-06 17:53:48 -08003502 client = zuul.rpcclient.RPCClient('127.0.0.1',
3503 self.gearman_server.port)
3504 r = client.promote(pipeline='gate',
3505 change_ids=['2,1', '3,1'])
3506
Sean Daguef39b9ca2014-01-10 21:34:35 -05003507 # ensure that enqueue times are durable
3508 items = self.sched.layout.pipelines['gate'].getAllItems()
3509 for item in items:
3510 self.assertEqual(
3511 enqueue_times[str(item.change)], item.enqueue_time)
3512
James E. Blair78acec92014-02-06 07:11:32 -08003513 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003514 self.worker.release('.*-merge')
3515 self.waitUntilSettled()
3516 self.worker.release('.*-merge')
3517 self.waitUntilSettled()
3518 self.worker.release('.*-merge')
3519 self.waitUntilSettled()
3520
3521 self.assertEqual(len(self.builds), 6)
3522 self.assertEqual(self.builds[0].name, 'project-test1')
3523 self.assertEqual(self.builds[1].name, 'project-test2')
3524 self.assertEqual(self.builds[2].name, 'project-test1')
3525 self.assertEqual(self.builds[3].name, 'project-test2')
3526 self.assertEqual(self.builds[4].name, 'project-test1')
3527 self.assertEqual(self.builds[5].name, 'project-test2')
3528
3529 self.assertTrue(self.job_has_changes(self.builds[0], B))
3530 self.assertFalse(self.job_has_changes(self.builds[0], A))
3531 self.assertFalse(self.job_has_changes(self.builds[0], C))
3532
3533 self.assertTrue(self.job_has_changes(self.builds[2], B))
3534 self.assertTrue(self.job_has_changes(self.builds[2], C))
3535 self.assertFalse(self.job_has_changes(self.builds[2], A))
3536
3537 self.assertTrue(self.job_has_changes(self.builds[4], B))
3538 self.assertTrue(self.job_has_changes(self.builds[4], C))
3539 self.assertTrue(self.job_has_changes(self.builds[4], A))
3540
3541 self.worker.release()
3542 self.waitUntilSettled()
3543
3544 self.assertEqual(A.data['status'], 'MERGED')
3545 self.assertEqual(A.reported, 2)
3546 self.assertEqual(B.data['status'], 'MERGED')
3547 self.assertEqual(B.reported, 2)
3548 self.assertEqual(C.data['status'], 'MERGED')
3549 self.assertEqual(C.reported, 2)
3550
3551 client.shutdown()
3552 self.assertEqual(r, True)
3553
3554 def test_client_promote_dependent(self):
3555 "Test that the RPC client can promote a dependent change"
3556 # C (depends on B) -> B -> A ; then promote C to get:
3557 # A -> C (depends on B) -> B
3558 self.worker.hold_jobs_in_build = True
3559 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3560 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3561 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3562
3563 C.setDependsOn(B, 1)
3564
3565 A.addApproval('CRVW', 2)
3566 B.addApproval('CRVW', 2)
3567 C.addApproval('CRVW', 2)
3568
3569 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3570 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3571 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3572
3573 self.waitUntilSettled()
3574
3575 client = zuul.rpcclient.RPCClient('127.0.0.1',
3576 self.gearman_server.port)
3577 r = client.promote(pipeline='gate',
3578 change_ids=['3,1'])
3579
James E. Blair78acec92014-02-06 07:11:32 -08003580 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003581 self.worker.release('.*-merge')
3582 self.waitUntilSettled()
3583 self.worker.release('.*-merge')
3584 self.waitUntilSettled()
3585 self.worker.release('.*-merge')
3586 self.waitUntilSettled()
3587
3588 self.assertEqual(len(self.builds), 6)
3589 self.assertEqual(self.builds[0].name, 'project-test1')
3590 self.assertEqual(self.builds[1].name, 'project-test2')
3591 self.assertEqual(self.builds[2].name, 'project-test1')
3592 self.assertEqual(self.builds[3].name, 'project-test2')
3593 self.assertEqual(self.builds[4].name, 'project-test1')
3594 self.assertEqual(self.builds[5].name, 'project-test2')
3595
3596 self.assertTrue(self.job_has_changes(self.builds[0], B))
3597 self.assertFalse(self.job_has_changes(self.builds[0], A))
3598 self.assertFalse(self.job_has_changes(self.builds[0], C))
3599
3600 self.assertTrue(self.job_has_changes(self.builds[2], B))
3601 self.assertTrue(self.job_has_changes(self.builds[2], C))
3602 self.assertFalse(self.job_has_changes(self.builds[2], A))
3603
3604 self.assertTrue(self.job_has_changes(self.builds[4], B))
3605 self.assertTrue(self.job_has_changes(self.builds[4], C))
3606 self.assertTrue(self.job_has_changes(self.builds[4], A))
3607
3608 self.worker.release()
3609 self.waitUntilSettled()
3610
3611 self.assertEqual(A.data['status'], 'MERGED')
3612 self.assertEqual(A.reported, 2)
3613 self.assertEqual(B.data['status'], 'MERGED')
3614 self.assertEqual(B.reported, 2)
3615 self.assertEqual(C.data['status'], 'MERGED')
3616 self.assertEqual(C.reported, 2)
3617
3618 client.shutdown()
3619 self.assertEqual(r, True)
3620
3621 def test_client_promote_negative(self):
3622 "Test that the RPC client returns errors for promotion"
3623 self.worker.hold_jobs_in_build = True
3624 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3625 A.addApproval('CRVW', 2)
3626 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3627 self.waitUntilSettled()
3628
3629 client = zuul.rpcclient.RPCClient('127.0.0.1',
3630 self.gearman_server.port)
3631
3632 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3633 r = client.promote(pipeline='nonexistent',
3634 change_ids=['2,1', '3,1'])
3635 client.shutdown()
3636 self.assertEqual(r, False)
3637
3638 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3639 r = client.promote(pipeline='gate',
3640 change_ids=['4,1'])
3641 client.shutdown()
3642 self.assertEqual(r, False)
3643
3644 self.worker.hold_jobs_in_build = False
3645 self.worker.release()
3646 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003647
3648 def test_queue_rate_limiting(self):
3649 "Test that DependentPipelines are rate limited with dep across window"
3650 self.config.set('zuul', 'layout_config',
3651 'tests/fixtures/layout-rate-limit.yaml')
3652 self.sched.reconfigure(self.config)
3653 self.worker.hold_jobs_in_build = True
3654 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3655 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3656 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3657
3658 C.setDependsOn(B, 1)
3659 self.worker.addFailTest('project-test1', A)
3660
3661 A.addApproval('CRVW', 2)
3662 B.addApproval('CRVW', 2)
3663 C.addApproval('CRVW', 2)
3664
3665 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3666 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3667 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3668 self.waitUntilSettled()
3669
3670 # Only A and B will have their merge jobs queued because
3671 # window is 2.
3672 self.assertEqual(len(self.builds), 2)
3673 self.assertEqual(self.builds[0].name, 'project-merge')
3674 self.assertEqual(self.builds[1].name, 'project-merge')
3675
3676 self.worker.release('.*-merge')
3677 self.waitUntilSettled()
3678 self.worker.release('.*-merge')
3679 self.waitUntilSettled()
3680
3681 # Only A and B will have their test jobs queued because
3682 # window is 2.
3683 self.assertEqual(len(self.builds), 4)
3684 self.assertEqual(self.builds[0].name, 'project-test1')
3685 self.assertEqual(self.builds[1].name, 'project-test2')
3686 self.assertEqual(self.builds[2].name, 'project-test1')
3687 self.assertEqual(self.builds[3].name, 'project-test2')
3688
3689 self.worker.release('project-.*')
3690 self.waitUntilSettled()
3691
3692 queue = self.sched.layout.pipelines['gate'].queues[0]
3693 # A failed so window is reduced by 1 to 1.
3694 self.assertEqual(queue.window, 1)
3695 self.assertEqual(queue.window_floor, 1)
3696 self.assertEqual(A.data['status'], 'NEW')
3697
3698 # Gate is reset and only B's merge job is queued because
3699 # window shrunk to 1.
3700 self.assertEqual(len(self.builds), 1)
3701 self.assertEqual(self.builds[0].name, 'project-merge')
3702
3703 self.worker.release('.*-merge')
3704 self.waitUntilSettled()
3705
3706 # Only B's test jobs are queued because window is still 1.
3707 self.assertEqual(len(self.builds), 2)
3708 self.assertEqual(self.builds[0].name, 'project-test1')
3709 self.assertEqual(self.builds[1].name, 'project-test2')
3710
3711 self.worker.release('project-.*')
3712 self.waitUntilSettled()
3713
3714 # B was successfully merged so window is increased to 2.
3715 self.assertEqual(queue.window, 2)
3716 self.assertEqual(queue.window_floor, 1)
3717 self.assertEqual(B.data['status'], 'MERGED')
3718
3719 # Only C is left and its merge job is queued.
3720 self.assertEqual(len(self.builds), 1)
3721 self.assertEqual(self.builds[0].name, 'project-merge')
3722
3723 self.worker.release('.*-merge')
3724 self.waitUntilSettled()
3725
3726 # After successful merge job the test jobs for C are queued.
3727 self.assertEqual(len(self.builds), 2)
3728 self.assertEqual(self.builds[0].name, 'project-test1')
3729 self.assertEqual(self.builds[1].name, 'project-test2')
3730
3731 self.worker.release('project-.*')
3732 self.waitUntilSettled()
3733
3734 # C successfully merged so window is bumped to 3.
3735 self.assertEqual(queue.window, 3)
3736 self.assertEqual(queue.window_floor, 1)
3737 self.assertEqual(C.data['status'], 'MERGED')
3738
3739 def test_queue_rate_limiting_dependent(self):
3740 "Test that DependentPipelines are rate limited with dep in window"
3741 self.config.set('zuul', 'layout_config',
3742 'tests/fixtures/layout-rate-limit.yaml')
3743 self.sched.reconfigure(self.config)
3744 self.worker.hold_jobs_in_build = True
3745 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3746 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3747 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3748
3749 B.setDependsOn(A, 1)
3750
3751 self.worker.addFailTest('project-test1', A)
3752
3753 A.addApproval('CRVW', 2)
3754 B.addApproval('CRVW', 2)
3755 C.addApproval('CRVW', 2)
3756
3757 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3758 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3759 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3760 self.waitUntilSettled()
3761
3762 # Only A and B will have their merge jobs queued because
3763 # window is 2.
3764 self.assertEqual(len(self.builds), 2)
3765 self.assertEqual(self.builds[0].name, 'project-merge')
3766 self.assertEqual(self.builds[1].name, 'project-merge')
3767
3768 self.worker.release('.*-merge')
3769 self.waitUntilSettled()
3770 self.worker.release('.*-merge')
3771 self.waitUntilSettled()
3772
3773 # Only A and B will have their test jobs queued because
3774 # window is 2.
3775 self.assertEqual(len(self.builds), 4)
3776 self.assertEqual(self.builds[0].name, 'project-test1')
3777 self.assertEqual(self.builds[1].name, 'project-test2')
3778 self.assertEqual(self.builds[2].name, 'project-test1')
3779 self.assertEqual(self.builds[3].name, 'project-test2')
3780
3781 self.worker.release('project-.*')
3782 self.waitUntilSettled()
3783
3784 queue = self.sched.layout.pipelines['gate'].queues[0]
3785 # A failed so window is reduced by 1 to 1.
3786 self.assertEqual(queue.window, 1)
3787 self.assertEqual(queue.window_floor, 1)
3788 self.assertEqual(A.data['status'], 'NEW')
3789 self.assertEqual(B.data['status'], 'NEW')
3790
3791 # Gate is reset and only C's merge job is queued because
3792 # window shrunk to 1 and A and B were dequeued.
3793 self.assertEqual(len(self.builds), 1)
3794 self.assertEqual(self.builds[0].name, 'project-merge')
3795
3796 self.worker.release('.*-merge')
3797 self.waitUntilSettled()
3798
3799 # Only C's test jobs are queued because window is still 1.
3800 self.assertEqual(len(self.builds), 2)
3801 self.assertEqual(self.builds[0].name, 'project-test1')
3802 self.assertEqual(self.builds[1].name, 'project-test2')
3803
3804 self.worker.release('project-.*')
3805 self.waitUntilSettled()
3806
3807 # C was successfully merged so window is increased to 2.
3808 self.assertEqual(queue.window, 2)
3809 self.assertEqual(queue.window_floor, 1)
3810 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003811
3812 def test_worker_update_metadata(self):
3813 "Test if a worker can send back metadata about itself"
3814 self.worker.hold_jobs_in_build = True
3815
3816 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3817 A.addApproval('CRVW', 2)
3818 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3819 self.waitUntilSettled()
3820
3821 self.assertEqual(len(self.launcher.builds), 1)
3822
3823 self.log.debug('Current builds:')
3824 self.log.debug(self.launcher.builds)
3825
3826 start = time.time()
3827 while True:
3828 if time.time() - start > 10:
3829 raise Exception("Timeout waiting for gearman server to report "
3830 + "back to the client")
3831 build = self.launcher.builds.values()[0]
3832 if build.worker.name == "My Worker":
3833 break
3834 else:
3835 time.sleep(0)
3836
3837 self.log.debug(build)
3838 self.assertEqual("My Worker", build.worker.name)
3839 self.assertEqual("localhost", build.worker.hostname)
3840 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3841 self.assertEqual("zuul.example.org", build.worker.fqdn)
3842 self.assertEqual("FakeBuilder", build.worker.program)
3843 self.assertEqual("v1.1", build.worker.version)
3844 self.assertEqual({'something': 'else'}, build.worker.extra)
3845
3846 self.worker.hold_jobs_in_build = False
3847 self.worker.release()
3848 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003849
3850 def test_footer_message(self):
3851 "Test a pipeline's footer message is correctly added to the report."
3852 self.config.set('zuul', 'layout_config',
3853 'tests/fixtures/layout-footer-message.yaml')
3854 self.sched.reconfigure(self.config)
3855 self.registerJobs()
3856
3857 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3858 A.addApproval('CRVW', 2)
3859 self.worker.addFailTest('test1', A)
3860 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3861 self.waitUntilSettled()
3862
3863 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3864 B.addApproval('CRVW', 2)
3865 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3866 self.waitUntilSettled()
3867
3868 self.assertEqual(2, len(self.smtp_messages))
3869
3870 failure_body = """\
3871Build failed. For information on how to proceed, see \
3872http://wiki.example.org/Test_Failures
3873
3874- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3875- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3876
3877For CI problems and help debugging, contact ci@example.org"""
3878
3879 success_body = """\
3880Build succeeded.
3881
3882- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3883- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3884
3885For CI problems and help debugging, contact ci@example.org"""
3886
3887 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3888 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11003889
3890 def test_merge_failure_reporters(self):
3891 """Check that the config is set up correctly"""
3892
3893 self.config.set('zuul', 'layout_config',
3894 'tests/fixtures/layout-merge-failure.yaml')
3895 self.sched.reconfigure(self.config)
3896 self.registerJobs()
3897
3898 self.assertEqual(
3899 "Merge Failed.\n\nThis change was unable to be automatically "
3900 "merged with the current state of the repository. Please rebase "
3901 "your change and upload a new patchset.",
3902 self.sched.layout.pipelines['check'].merge_failure_message)
3903 self.assertEqual(
3904 "The merge failed! For more information...",
3905 self.sched.layout.pipelines['gate'].merge_failure_message)
3906
3907 self.assertEqual(
3908 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3909 self.assertEqual(
3910 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3911
3912 self.assertTrue(isinstance(
3913 self.sched.layout.pipelines['check'].merge_failure_actions[0].
3914 reporter, zuul.reporter.gerrit.Reporter))
3915
3916 self.assertTrue(
3917 (
3918 isinstance(self.sched.layout.pipelines['gate'].
3919 merge_failure_actions[0].reporter,
3920 zuul.reporter.smtp.Reporter) and
3921 isinstance(self.sched.layout.pipelines['gate'].
3922 merge_failure_actions[1].reporter,
3923 zuul.reporter.gerrit.Reporter)
3924 ) or (
3925 isinstance(self.sched.layout.pipelines['gate'].
3926 merge_failure_actions[0].reporter,
3927 zuul.reporter.gerrit.Reporter) and
3928 isinstance(self.sched.layout.pipelines['gate'].
3929 merge_failure_actions[1].reporter,
3930 zuul.reporter.smtp.Reporter)
3931 )
3932 )
3933
3934 def test_merge_failure_reports(self):
3935 """Check that when a change fails to merge the correct message is sent
3936 to the correct reporter"""
3937 self.config.set('zuul', 'layout_config',
3938 'tests/fixtures/layout-merge-failure.yaml')
3939 self.sched.reconfigure(self.config)
3940 self.registerJobs()
3941
3942 # Check a test failure isn't reported to SMTP
3943 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3944 A.addApproval('CRVW', 2)
3945 self.worker.addFailTest('project-test1', A)
3946 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3947 self.waitUntilSettled()
3948
3949 self.assertEqual(3, len(self.history)) # 3 jobs
3950 self.assertEqual(0, len(self.smtp_messages))
3951
3952 # Check a merge failure is reported to SMTP
3953 # B should be merged, but C will conflict with B
3954 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3955 B.addPatchset(['conflict'])
3956 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3957 C.addPatchset(['conflict'])
3958 B.addApproval('CRVW', 2)
3959 C.addApproval('CRVW', 2)
3960 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3961 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3962 self.waitUntilSettled()
3963
3964 self.assertEqual(6, len(self.history)) # A and B jobs
3965 self.assertEqual(1, len(self.smtp_messages))
3966 self.assertEqual('The merge failed! For more information...',
3967 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003968
3969 def test_swift_instructions(self):
3970 "Test that the correct swift instructions are sent to the workers"
3971 self.config.set('zuul', 'layout_config',
3972 'tests/fixtures/layout-swift.yaml')
3973 self.sched.reconfigure(self.config)
3974 self.registerJobs()
3975
3976 self.worker.hold_jobs_in_build = True
3977 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3978
3979 A.addApproval('CRVW', 2)
3980 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3981 self.waitUntilSettled()
3982
3983 self.assertEqual(
3984 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
3985 "gate/test-merge/",
3986 self.builds[0].parameters['SWIFT_logs_URL'][:-32])
3987 self.assertEqual(5,
3988 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
3989 split('\n')))
3990 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
3991
3992 self.assertEqual(
3993 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
3994 "gate/test-test/",
3995 self.builds[1].parameters['SWIFT_logs_URL'][:-32])
3996 self.assertEqual(5,
3997 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
3998 split('\n')))
3999 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
4000
4001 self.assertEqual(
4002 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
4003 "gate/test-test/",
4004 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-32])
4005 self.assertEqual(5,
4006 len(self.builds[1].
4007 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
4008 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
4009
4010 self.worker.hold_jobs_in_build = False
4011 self.worker.release()
4012 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004013
4014 def test_client_get_running_jobs(self):
4015 "Test that the RPC client can get a list of running jobs"
4016 self.worker.hold_jobs_in_build = True
4017 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4018 A.addApproval('CRVW', 2)
4019 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
4020 self.waitUntilSettled()
4021
4022 client = zuul.rpcclient.RPCClient('127.0.0.1',
4023 self.gearman_server.port)
4024
4025 # Wait for gearman server to send the initial workData back to zuul
4026 start = time.time()
4027 while True:
4028 if time.time() - start > 10:
4029 raise Exception("Timeout waiting for gearman server to report "
4030 + "back to the client")
4031 build = self.launcher.builds.values()[0]
4032 if build.worker.name == "My Worker":
4033 break
4034 else:
4035 time.sleep(0)
4036
4037 running_items = client.get_running_jobs()
4038
4039 self.assertEqual(1, len(running_items))
4040 running_item = running_items[0]
4041 self.assertEqual([], running_item['failing_reasons'])
4042 self.assertEqual([], running_item['items_behind'])
4043 self.assertEqual('https://hostname/1', running_item['url'])
4044 self.assertEqual(None, running_item['item_ahead'])
4045 self.assertEqual('org/project', running_item['project'])
4046 self.assertEqual(None, running_item['remaining_time'])
4047 self.assertEqual(True, running_item['active'])
4048 self.assertEqual('1,1', running_item['id'])
4049
4050 self.assertEqual(3, len(running_item['jobs']))
4051 for job in running_item['jobs']:
4052 if job['name'] == 'project-merge':
4053 self.assertEqual('project-merge', job['name'])
4054 self.assertEqual('gate', job['pipeline'])
4055 self.assertEqual(False, job['retry'])
4056 self.assertEqual(13, len(job['parameters']))
4057 self.assertEqual('https://server/job/project-merge/0/',
4058 job['url'])
4059 self.assertEqual(7, len(job['worker']))
4060 self.assertEqual(False, job['canceled'])
4061 self.assertEqual(True, job['voting'])
4062 self.assertEqual(None, job['result'])
4063 self.assertEqual('gate', job['pipeline'])
4064 break
4065
4066 self.worker.hold_jobs_in_build = False
4067 self.worker.release()
4068 self.waitUntilSettled()
4069
4070 running_items = client.get_running_jobs()
4071 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07004072
4073 def test_nonvoting_pipeline(self):
4074 "Test that a nonvoting pipeline (experimental) can still report"
4075
Joshua Heskethcc017ea2014-04-30 19:55:25 +10004076 A = self.fake_gerrit.addFakeChange('org/experimental-project',
4077 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07004078 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4079 self.waitUntilSettled()
Joshua Heskethcc017ea2014-04-30 19:55:25 +10004080 self.assertEqual(
4081 self.getJobFromHistory('experimental-project-test').result,
4082 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07004083 self.assertEqual(A.reported, 1)
Clark Boylana9702ad2014-05-08 17:17:24 -07004084
4085 def test_old_patchset_doesnt_trigger(self):
4086 "Test that jobs never run against old patchsets"
4087 self.config.set('zuul', 'layout_config',
4088 'tests/fixtures/layout-current-patchset.yaml')
4089 self.sched.reconfigure(self.config)
4090 self.registerJobs()
4091 # Create two patchsets and let their tests settle out. Then
4092 # comment on first patchset and check that no additional
4093 # jobs are run.
4094 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4095 # Added because the layout file really wants an approval but this
4096 # doesn't match anyways.
4097 self.fake_gerrit.addEvent(A.addApproval('CRVW', 1))
4098 self.waitUntilSettled()
4099 A.addPatchset()
4100 self.fake_gerrit.addEvent(A.addApproval('CRVW', 1))
4101 self.waitUntilSettled()
4102
4103 old_history_count = len(self.history)
4104 self.assertEqual(old_history_count, 2) # one job for each ps
4105 self.fake_gerrit.addEvent(A.getChangeCommentEvent(1))
4106 self.waitUntilSettled()
4107
4108 # Assert no new jobs ran after event for old patchset.
4109 self.assertEqual(len(self.history), old_history_count)
4110
4111 # The last thing we did was add an event for a change then do
4112 # nothing with a pipeline, so it will be in the cache;
4113 # clean it up so it does not fail the test.
4114 for pipeline in self.sched.layout.pipelines.values():
4115 pipeline.trigger.maintainCache([])