blob: d060b00778ad19cb1a68c964e17ab64a95a99760 [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),
121 'open': True,
122 'owner': {'email': 'user@example.com',
123 'name': 'User Name',
124 'username': 'username'},
125 'patchSets': self.patchsets,
126 'project': project,
127 'status': status,
128 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700129 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700130 'url': 'https://hostname/%s' % number}
131
Monty Taylorbc758832013-06-17 17:22:42 -0400132 self.upstream_root = upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700133 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700134 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700135
Monty Taylorbc758832013-06-17 17:22:42 -0400136 def add_fake_change_to_repo(self, msg, fn, large):
137 path = os.path.join(self.upstream_root, self.project)
138 repo = git.Repo(path)
139 ref = ChangeReference.create(repo, '1/%s/%s' % (self.number,
140 self.latest_patchset),
141 'refs/tags/init')
142 repo.head.reference = ref
143 repo.head.reset(index=True, working_tree=True)
144 repo.git.clean('-x', '-f', '-d')
145
146 path = os.path.join(self.upstream_root, self.project)
147 if not large:
148 fn = os.path.join(path, fn)
149 f = open(fn, 'w')
150 f.write("test %s %s %s\n" %
151 (self.branch, self.number, self.latest_patchset))
152 f.close()
153 repo.index.add([fn])
154 else:
155 for fni in range(100):
156 fn = os.path.join(path, str(fni))
157 f = open(fn, 'w')
158 for ci in range(4096):
159 f.write(random.choice(string.printable))
160 f.close()
161 repo.index.add([fn])
162
James E. Blair287c06d2013-07-24 10:39:30 -0700163 r = repo.index.commit(msg)
164 repo.head.reference = 'master'
165 repo.head.reset(index=True, working_tree=True)
166 repo.git.clean('-x', '-f', '-d')
James E. Blairac2c3242014-01-24 13:38:51 -0800167 repo.heads['master'].checkout()
James E. Blair287c06d2013-07-24 10:39:30 -0700168 return r
Monty Taylorbc758832013-06-17 17:22:42 -0400169
James E. Blair70c71582013-03-06 08:50:50 -0800170 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700171 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700172 if files:
173 fn = files[0]
174 else:
175 fn = '%s-%s' % (self.branch, self.number)
176 msg = self.subject + '-' + str(self.latest_patchset)
Monty Taylorbc758832013-06-17 17:22:42 -0400177 c = self.add_fake_change_to_repo(msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800178 ps_files = [{'file': '/COMMIT_MSG',
179 'type': 'ADDED'},
180 {'file': 'README',
181 'type': 'MODIFIED'}]
182 for f in files:
183 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700184 d = {'approvals': [],
185 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800186 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700187 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700188 'ref': 'refs/changes/1/%s/%s' % (self.number,
189 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700190 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700191 'uploader': {'email': 'user@example.com',
192 'name': 'User name',
193 'username': 'user'}}
194 self.data['currentPatchSet'] = d
195 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700196 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700197
James E. Blaire0487072012-08-29 17:38:31 -0700198 def getPatchsetCreatedEvent(self, patchset):
199 event = {"type": "patchset-created",
200 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800201 "branch": self.branch,
202 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
203 "number": str(self.number),
204 "subject": self.subject,
205 "owner": {"name": "User Name"},
206 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700207 "patchSet": self.patchsets[patchset - 1],
208 "uploader": {"name": "User Name"}}
209 return event
210
James E. Blair42f74822013-05-14 15:18:03 -0700211 def getChangeRestoredEvent(self):
212 event = {"type": "change-restored",
213 "change": {"project": self.project,
214 "branch": self.branch,
215 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
216 "number": str(self.number),
217 "subject": self.subject,
218 "owner": {"name": "User Name"},
219 "url": "https://hostname/3"},
220 "restorer": {"name": "User Name"},
221 "reason": ""}
222 return event
223
James E. Blairc053d022014-01-22 14:57:33 -0800224 def addApproval(self, category, value, username='jenkins',
225 granted_on=None):
226 if not granted_on:
227 granted_on = time.time()
James E. Blair8c803f82012-07-31 16:25:42 -0700228 approval = {'description': self.categories[category][0],
229 'type': category,
James E. Blairc053d022014-01-22 14:57:33 -0800230 'value': str(value),
James E. Blair64ff4ef2014-01-24 13:50:23 -0800231 'by': {
232 'username': username,
233 'email': username + '@example.com',
234 },
James E. Blairc053d022014-01-22 14:57:33 -0800235 'grantedOn': int(granted_on)}
236 for i, x in enumerate(self.patchsets[-1]['approvals'][:]):
James E. Blair64ff4ef2014-01-24 13:50:23 -0800237 if x['by']['username'] == username and x['type'] == category:
James E. Blairc053d022014-01-22 14:57:33 -0800238 del self.patchsets[-1]['approvals'][i]
James E. Blair8c803f82012-07-31 16:25:42 -0700239 self.patchsets[-1]['approvals'].append(approval)
240 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700241 'author': {'email': 'user@example.com',
242 'name': 'User Name',
243 'username': 'username'},
244 'change': {'branch': self.branch,
245 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
246 'number': str(self.number),
247 'owner': {'email': 'user@example.com',
248 'name': 'User Name',
249 'username': 'username'},
250 'project': self.project,
251 'subject': self.subject,
252 'topic': 'master',
253 'url': 'https://hostname/459'},
254 'comment': '',
255 'patchSet': self.patchsets[-1],
256 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700257 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700258 return json.loads(json.dumps(event))
259
James E. Blair8c803f82012-07-31 16:25:42 -0700260 def getSubmitRecords(self):
261 status = {}
262 for cat in self.categories.keys():
263 status[cat] = 0
264
265 for a in self.patchsets[-1]['approvals']:
266 cur = status[a['type']]
267 cat_min, cat_max = self.categories[a['type']][1:]
268 new = int(a['value'])
269 if new == cat_min:
270 cur = new
271 elif abs(new) > abs(cur):
272 cur = new
273 status[a['type']] = cur
274
275 labels = []
276 ok = True
277 for typ, cat in self.categories.items():
278 cur = status[typ]
279 cat_min, cat_max = cat[1:]
280 if cur == cat_min:
281 value = 'REJECT'
282 ok = False
283 elif cur == cat_max:
284 value = 'OK'
285 else:
286 value = 'NEED'
287 ok = False
288 labels.append({'label': cat[0], 'status': value})
289 if ok:
290 return [{'status': 'OK'}]
291 return [{'status': 'NOT_READY',
292 'labels': labels}]
293
294 def setDependsOn(self, other, patchset):
295 self.depends_on_change = other
296 d = {'id': other.data['id'],
297 'number': other.data['number'],
298 'ref': other.patchsets[patchset - 1]['ref']
299 }
300 self.data['dependsOn'] = [d]
301
302 other.needed_by_changes.append(self)
303 needed = other.data.get('neededBy', [])
304 d = {'id': self.data['id'],
305 'number': self.data['number'],
306 'ref': self.patchsets[patchset - 1]['ref'],
307 'revision': self.patchsets[patchset - 1]['revision']
308 }
309 needed.append(d)
310 other.data['neededBy'] = needed
311
James E. Blairb0fcae42012-07-17 11:12:10 -0700312 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700313 self.queried += 1
314 d = self.data.get('dependsOn')
315 if d:
316 d = d[0]
317 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
318 d['isCurrentPatchSet'] = True
319 else:
320 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700321 return json.loads(json.dumps(self.data))
322
323 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800324 if (self.depends_on_change and
325 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700326 return
James E. Blair127bc182012-08-28 15:55:15 -0700327 if self.fail_merge:
328 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700329 self.data['status'] = 'MERGED'
330 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700331
Monty Taylorbc758832013-06-17 17:22:42 -0400332 path = os.path.join(self.upstream_root, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700333 repo = git.Repo(path)
334 repo.heads[self.branch].commit = \
335 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700336
James E. Blaird466dc42012-07-31 10:42:56 -0700337 def setReported(self):
338 self.reported += 1
339
James E. Blairb0fcae42012-07-17 11:12:10 -0700340
341class FakeGerrit(object):
342 def __init__(self, *args, **kw):
343 self.event_queue = Queue.Queue()
344 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
345 self.change_number = 0
346 self.changes = {}
347
348 def addFakeChange(self, project, branch, subject):
349 self.change_number += 1
Monty Taylorbc758832013-06-17 17:22:42 -0400350 c = FakeChange(self, self.change_number, project, branch, subject,
351 upstream_root=self.upstream_root)
James E. Blairb0fcae42012-07-17 11:12:10 -0700352 self.changes[self.change_number] = c
353 return c
354
355 def addEvent(self, data):
356 return self.event_queue.put(data)
357
358 def getEvent(self):
359 return self.event_queue.get()
360
361 def eventDone(self):
362 self.event_queue.task_done()
363
364 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700365 number, ps = changeid.split(',')
366 change = self.changes[int(number)]
James E. Blair42f74822013-05-14 15:18:03 -0700367 change.messages.append(message)
James E. Blairb0fcae42012-07-17 11:12:10 -0700368 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700369 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700370 if message:
371 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700372
373 def query(self, number):
James E. Blairad28e912013-11-27 10:43:22 -0800374 change = self.changes.get(int(number))
375 if change:
376 return change.query()
377 return {}
James E. Blairb0fcae42012-07-17 11:12:10 -0700378
379 def startWatching(self, *args, **kw):
380 pass
381
382
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700383class BuildHistory(object):
384 def __init__(self, **kw):
385 self.__dict__.update(kw)
James E. Blairb0fcae42012-07-17 11:12:10 -0700386
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700387 def __repr__(self):
388 return ("<Completed build, result: %s name: %s #%s changes: %s>" %
389 (self.result, self.name, self.number, self.changes))
James E. Blairb0fcae42012-07-17 11:12:10 -0700390
391
James E. Blair8cc15a82012-08-01 11:17:57 -0700392class FakeURLOpener(object):
Monty Taylorbc758832013-06-17 17:22:42 -0400393 def __init__(self, upstream_root, fake_gerrit, url):
394 self.upstream_root = upstream_root
James E. Blair8cc15a82012-08-01 11:17:57 -0700395 self.fake_gerrit = fake_gerrit
396 self.url = url
397
398 def read(self):
399 res = urlparse.urlparse(self.url)
400 path = res.path
401 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200402 ret = '001e# service=git-upload-pack\n'
403 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
404 'multi_ack thin-pack side-band side-band-64k ofs-delta '
405 'shallow no-progress include-tag multi_ack_detailed no-done\n')
Monty Taylorbc758832013-06-17 17:22:42 -0400406 path = os.path.join(self.upstream_root, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700407 repo = git.Repo(path)
408 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200409 r = ref.object.hexsha + ' ' + ref.path + '\n'
410 ret += '%04x%s' % (len(r) + 4, r)
411 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700412 return ret
413
414
James E. Blair4886cc12012-07-18 15:39:41 -0700415class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000416 name = 'gerrit'
417
Monty Taylorbc758832013-06-17 17:22:42 -0400418 def __init__(self, upstream_root, *args):
419 super(FakeGerritTrigger, self).__init__(*args)
420 self.upstream_root = upstream_root
421
James E. Blair4886cc12012-07-18 15:39:41 -0700422 def getGitUrl(self, project):
Monty Taylorbc758832013-06-17 17:22:42 -0400423 return os.path.join(self.upstream_root, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700424
425
James E. Blair412e5582013-04-22 15:50:12 -0700426class FakeStatsd(threading.Thread):
427 def __init__(self):
428 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400429 self.daemon = True
James E. Blair412e5582013-04-22 15:50:12 -0700430 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
431 self.sock.bind(('', 0))
432 self.port = self.sock.getsockname()[1]
433 self.wake_read, self.wake_write = os.pipe()
434 self.stats = []
435
436 def run(self):
437 while True:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700438 poll = select.poll()
439 poll.register(self.sock, select.POLLIN)
440 poll.register(self.wake_read, select.POLLIN)
441 ret = poll.poll()
442 for (fd, event) in ret:
443 if fd == self.sock.fileno():
James E. Blair412e5582013-04-22 15:50:12 -0700444 data = self.sock.recvfrom(1024)
445 if not data:
446 return
447 self.stats.append(data[0])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700448 if fd == self.wake_read:
James E. Blair412e5582013-04-22 15:50:12 -0700449 return
450
451 def stop(self):
452 os.write(self.wake_write, '1\n')
453
454
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700455class FakeBuild(threading.Thread):
456 log = logging.getLogger("zuul.test")
457
458 def __init__(self, worker, job, number, node):
459 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400460 self.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700461 self.worker = worker
462 self.job = job
463 self.name = job.name.split(':')[1]
464 self.number = number
465 self.node = node
466 self.parameters = json.loads(job.arguments)
467 self.unique = self.parameters['ZUUL_UUID']
468 self.wait_condition = threading.Condition()
469 self.waiting = False
470 self.aborted = False
471 self.created = time.time()
472 self.description = ''
James E. Blair4a28a882013-08-23 15:17:33 -0700473 self.run_error = False
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700474
475 def release(self):
476 self.wait_condition.acquire()
477 self.wait_condition.notify()
478 self.waiting = False
479 self.log.debug("Build %s released" % self.unique)
480 self.wait_condition.release()
481
482 def isWaiting(self):
483 self.wait_condition.acquire()
484 if self.waiting:
485 ret = True
486 else:
487 ret = False
488 self.wait_condition.release()
489 return ret
490
491 def _wait(self):
492 self.wait_condition.acquire()
493 self.waiting = True
494 self.log.debug("Build %s waiting" % self.unique)
495 self.wait_condition.wait()
496 self.wait_condition.release()
497
498 def run(self):
499 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700500 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
501 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700502 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700503 'manager': self.worker.worker_id,
Joshua Heskethba8776a2014-01-12 14:35:40 +0800504 'worker_name': 'My Worker',
505 'worker_hostname': 'localhost',
506 'worker_ips': ['127.0.0.1', '192.168.1.1'],
507 'worker_fqdn': 'zuul.example.org',
508 'worker_program': 'FakeBuilder',
509 'worker_version': 'v1.1',
510 'worker_extra': {'something': 'else'}
Paul Belangerec49b4c2013-07-20 20:32:20 -0400511 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700512
Joshua Heskethba8776a2014-01-12 14:35:40 +0800513 self.log.debug('Running build %s' % self.unique)
514
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700515 self.job.sendWorkData(json.dumps(data))
Joshua Heskethba8776a2014-01-12 14:35:40 +0800516 self.log.debug('Sent WorkData packet with %s' % json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700517 self.job.sendWorkStatus(0, 100)
518
519 if self.worker.hold_jobs_in_build:
Joshua Heskethba8776a2014-01-12 14:35:40 +0800520 self.log.debug('Holding build %s' % self.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700521 self._wait()
522 self.log.debug("Build %s continuing" % self.unique)
523
524 self.worker.lock.acquire()
525
526 result = 'SUCCESS'
527 if (('ZUUL_REF' in self.parameters) and
528 self.worker.shouldFailTest(self.name,
529 self.parameters['ZUUL_REF'])):
530 result = 'FAILURE'
531 if self.aborted:
532 result = 'ABORTED'
533
James E. Blair4a28a882013-08-23 15:17:33 -0700534 if self.run_error:
535 work_fail = True
536 result = 'RUN_ERROR'
537 else:
538 data['result'] = result
539 work_fail = False
540
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700541 changes = None
542 if 'ZUUL_CHANGE_IDS' in self.parameters:
543 changes = self.parameters['ZUUL_CHANGE_IDS']
544
545 self.worker.build_history.append(
546 BuildHistory(name=self.name, number=self.number,
547 result=result, changes=changes, node=self.node,
James E. Blair64ed6f22013-07-10 14:07:23 -0700548 uuid=self.unique, description=self.description,
549 pipeline=self.parameters['ZUUL_PIPELINE'])
Paul Belangerec49b4c2013-07-20 20:32:20 -0400550 )
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700551
James E. Blair4a28a882013-08-23 15:17:33 -0700552 self.job.sendWorkData(json.dumps(data))
553 if work_fail:
554 self.job.sendWorkFail()
555 else:
556 self.job.sendWorkComplete(json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700557 del self.worker.gearman_jobs[self.job.unique]
558 self.worker.running_builds.remove(self)
559 self.worker.lock.release()
560
561
562class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400563 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700564 super(FakeWorker, self).__init__(worker_id)
565 self.gearman_jobs = {}
566 self.build_history = []
567 self.running_builds = []
568 self.build_counter = 0
569 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400570 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700571
572 self.hold_jobs_in_build = False
573 self.lock = threading.Lock()
574 self.__work_thread = threading.Thread(target=self.work)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400575 self.__work_thread.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700576 self.__work_thread.start()
577
578 def handleJob(self, job):
579 parts = job.name.split(":")
580 cmd = parts[0]
581 name = parts[1]
582 if len(parts) > 2:
583 node = parts[2]
584 else:
585 node = None
586 if cmd == 'build':
587 self.handleBuild(job, name, node)
588 elif cmd == 'stop':
589 self.handleStop(job, name)
590 elif cmd == 'set_description':
591 self.handleSetDescription(job, name)
592
593 def handleBuild(self, job, name, node):
594 build = FakeBuild(self, job, self.build_counter, node)
595 job.build = build
596 self.gearman_jobs[job.unique] = job
597 self.build_counter += 1
598
599 self.running_builds.append(build)
600 build.start()
601
602 def handleStop(self, job, name):
603 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700604 parameters = json.loads(job.arguments)
605 name = parameters['name']
606 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700607 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700608 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700609 build.aborted = True
610 build.release()
611 job.sendWorkComplete()
612 return
613 job.sendWorkFail()
614
615 def handleSetDescription(self, job, name):
616 self.log.debug("handle set description")
617 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700618 name = parameters['name']
619 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700620 descr = parameters['html_description']
621 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700622 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700623 build.description = descr
624 job.sendWorkComplete()
625 return
626 for build in self.build_history:
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.description = descr
629 job.sendWorkComplete()
630 return
631 job.sendWorkFail()
632
633 def work(self):
634 while self.running:
635 try:
636 job = self.getJob()
637 except gear.InterruptedError:
638 continue
639 try:
640 self.handleJob(job)
641 except:
642 self.log.exception("Worker exception:")
643
644 def addFailTest(self, name, change):
645 l = self.fail_tests.get(name, [])
646 l.append(change)
647 self.fail_tests[name] = l
648
649 def shouldFailTest(self, name, ref):
650 l = self.fail_tests.get(name, [])
651 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400652 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700653 return True
654 return False
655
656 def release(self, regex=None):
657 builds = self.running_builds[:]
658 self.log.debug("releasing build %s (%s)" % (regex,
James E. Blair78e31b32013-07-09 09:11:34 -0700659 len(self.running_builds)))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700660 for build in builds:
661 if not regex or re.match(regex, build.name):
662 self.log.debug("releasing build %s" %
663 (build.parameters['ZUUL_UUID']))
664 build.release()
665 else:
666 self.log.debug("not releasing build %s" %
667 (build.parameters['ZUUL_UUID']))
668 self.log.debug("done releasing builds %s (%s)" %
669 (regex, len(self.running_builds)))
670
671
672class FakeGearmanServer(gear.Server):
673 def __init__(self):
674 self.hold_jobs_in_queue = False
675 super(FakeGearmanServer, self).__init__(0)
676
677 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700678 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
679 for job in queue:
680 if not hasattr(job, 'waiting'):
681 if job.name.startswith('build:'):
682 job.waiting = self.hold_jobs_in_queue
683 else:
684 job.waiting = False
685 if job.waiting:
686 continue
687 if job.name in connection.functions:
688 if not peek:
689 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400690 connection.related_jobs[job.handle] = job
691 job.worker_connection = connection
692 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700693 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700694 return None
695
696 def release(self, regex=None):
697 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700698 qlen = (len(self.high_queue) + len(self.normal_queue) +
699 len(self.low_queue))
700 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
James E. Blairdda6c912013-07-29 14:12:12 -0700701 for job in self.getQueue():
702 cmd, name = job.name.split(':')
703 if cmd != 'build':
704 continue
705 if not regex or re.match(regex, name):
706 self.log.debug("releasing queued job %s" %
707 job.unique)
708 job.waiting = False
709 released = True
710 else:
711 self.log.debug("not releasing queued job %s" %
712 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700713 if released:
714 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700715 qlen = (len(self.high_queue) + len(self.normal_queue) +
716 len(self.low_queue))
717 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700718
719
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000720class FakeSMTP(object):
721 log = logging.getLogger('zuul.FakeSMTP')
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000722
James E. Blairff80a2f2013-12-27 13:24:06 -0800723 def __init__(self, messages, server, port):
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000724 self.server = server
725 self.port = port
James E. Blairff80a2f2013-12-27 13:24:06 -0800726 self.messages = messages
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000727
728 def sendmail(self, from_email, to_email, msg):
729 self.log.info("Sending email from %s, to %s, with msg %s" % (
730 from_email, to_email, msg))
731
732 headers = msg.split('\n\n', 1)[0]
733 body = msg.split('\n\n', 1)[1]
734
James E. Blairff80a2f2013-12-27 13:24:06 -0800735 self.messages.append(dict(
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000736 from_email=from_email,
737 to_email=to_email,
738 msg=msg,
739 headers=headers,
740 body=body,
741 ))
742
743 return True
744
745 def quit(self):
746 return True
747
748
Joshua Hesketh36c3fa52014-01-22 11:40:52 +1100749class FakeSwiftClientConnection(swiftclient.client.Connection):
750 def post_account(self, headers):
751 # Do nothing
752 pass
753
754 def get_auth(self):
755 # Returns endpoint and (unused) auth token
756 endpoint = os.path.join('https://storage.example.org', 'V1',
757 'AUTH_account')
758 return endpoint, ''
759
760
Monty Taylorbc758832013-06-17 17:22:42 -0400761class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700762 log = logging.getLogger("zuul.test")
763
764 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400765 super(TestScheduler, self).setUp()
766 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
767 try:
768 test_timeout = int(test_timeout)
769 except ValueError:
770 # If timeout value is invalid do not set a timeout.
771 test_timeout = 0
772 if test_timeout > 0:
James E. Blairff80a2f2013-12-27 13:24:06 -0800773 self.useFixture(fixtures.Timeout(test_timeout, gentle=False))
Monty Taylorbc758832013-06-17 17:22:42 -0400774
775 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
776 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
777 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
778 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
779 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
780 os.environ.get('OS_STDERR_CAPTURE') == '1'):
781 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
782 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Monty Taylore6a6c402013-07-02 09:25:55 -0700783 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
784 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400785 self.useFixture(fixtures.FakeLogger(
786 level=logging.DEBUG,
787 format='%(asctime)s %(name)-32s '
788 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700789 tmp_root = self.useFixture(fixtures.TempDir(
790 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400791 self.test_root = os.path.join(tmp_root, "zuul-test")
792 self.upstream_root = os.path.join(self.test_root, "upstream")
793 self.git_root = os.path.join(self.test_root, "git")
794
James E. Blair4076e2b2014-01-28 12:42:20 -0800795 CONFIG.set('merger', 'git_dir', self.git_root)
Monty Taylorbc758832013-06-17 17:22:42 -0400796 if os.path.exists(self.test_root):
797 shutil.rmtree(self.test_root)
798 os.makedirs(self.test_root)
799 os.makedirs(self.upstream_root)
800 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700801
802 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400803 self.init_repo("org/project")
804 self.init_repo("org/project1")
805 self.init_repo("org/project2")
806 self.init_repo("org/project3")
807 self.init_repo("org/one-job-project")
808 self.init_repo("org/nonvoting-project")
809 self.init_repo("org/templated-project")
James E. Blair3e98c022013-12-16 15:25:38 -0800810 self.init_repo("org/layered-project")
Monty Taylorbc758832013-06-17 17:22:42 -0400811 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700812 self.init_repo("org/conflict-project")
James E. Blair18c64442014-03-18 10:14:45 -0700813 self.init_repo("org/noop-project")
James E. Blair412e5582013-04-22 15:50:12 -0700814
815 self.statsd = FakeStatsd()
816 os.environ['STATSD_HOST'] = 'localhost'
817 os.environ['STATSD_PORT'] = str(self.statsd.port)
818 self.statsd.start()
819 # the statsd client object is configured in the statsd module import
820 reload(statsd)
821 reload(zuul.scheduler)
822
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700823 self.gearman_server = FakeGearmanServer()
824
825 self.config = ConfigParser.ConfigParser()
826 cfg = StringIO()
827 CONFIG.write(cfg)
828 cfg.seek(0)
829 self.config.readfp(cfg)
830 self.config.set('gearman', 'port', str(self.gearman_server.port))
831
Monty Taylorbc758832013-06-17 17:22:42 -0400832 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700833 self.worker.addServer('127.0.0.1', self.gearman_server.port)
834 self.gearman_server.worker = self.worker
835
James E. Blair4076e2b2014-01-28 12:42:20 -0800836 self.merge_server = zuul.merger.server.MergeServer(self.config)
837 self.merge_server.start()
838
James E. Blairb0fcae42012-07-17 11:12:10 -0700839 self.sched = zuul.scheduler.Scheduler()
840
Joshua Hesketh36c3fa52014-01-22 11:40:52 +1100841 self.useFixture(fixtures.MonkeyPatch('swiftclient.client.Connection',
842 FakeSwiftClientConnection))
843 self.swift = zuul.lib.swift.Swift(self.config)
844
James E. Blair8cc15a82012-08-01 11:17:57 -0700845 def URLOpenerFactory(*args, **kw):
Yuriy Taradaya6d452f2014-04-16 12:36:20 +0400846 if isinstance(args[0], urllib2.Request):
847 return old_urlopen(*args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700848 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400849 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700850
Yuriy Taradaya6d452f2014-04-16 12:36:20 +0400851 old_urlopen = urllib2.urlopen
James E. Blair8cc15a82012-08-01 11:17:57 -0700852 urllib2.urlopen = URLOpenerFactory
Joshua Hesketh36c3fa52014-01-22 11:40:52 +1100853
854 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched,
855 self.swift)
James E. Blair4076e2b2014-01-28 12:42:20 -0800856 self.merge_client = zuul.merger.client.MergeClient(
857 self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700858
James E. Blairff80a2f2013-12-27 13:24:06 -0800859 self.smtp_messages = []
860
861 def FakeSMTPFactory(*args, **kw):
862 args = [self.smtp_messages] + list(args)
863 return FakeSMTP(*args, **kw)
864
James E. Blairb0fcae42012-07-17 11:12:10 -0700865 zuul.lib.gerrit.Gerrit = FakeGerrit
James E. Blairff80a2f2013-12-27 13:24:06 -0800866 self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTPFactory))
James E. Blairb0fcae42012-07-17 11:12:10 -0700867
Monty Taylorbc758832013-06-17 17:22:42 -0400868 self.gerrit = FakeGerritTrigger(
869 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700870 self.gerrit.replication_timeout = 1.5
871 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700872 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400873 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700874
James E. Blair1843a552013-07-03 14:19:52 -0700875 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
James E. Blairad28e912013-11-27 10:43:22 -0800876 self.rpc = zuul.rpclistener.RPCListener(self.config, self.sched)
James E. Blair1843a552013-07-03 14:19:52 -0700877
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700878 self.sched.setLauncher(self.launcher)
James E. Blair4076e2b2014-01-28 12:42:20 -0800879 self.sched.setMerger(self.merge_client)
James E. Blair6c358e72013-07-29 17:06:47 -0700880 self.sched.registerTrigger(self.gerrit)
James E. Blair63bb0ef2013-07-29 17:14:51 -0700881 self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
882 self.sched.registerTrigger(self.timer)
James E. Blairb0fcae42012-07-17 11:12:10 -0700883
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000884 self.sched.registerReporter(
885 zuul.reporter.gerrit.Reporter(self.gerrit))
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000886 self.smtp_reporter = zuul.reporter.smtp.Reporter(
887 self.config.get('smtp', 'default_from'),
888 self.config.get('smtp', 'default_to'),
889 self.config.get('smtp', 'server'))
890 self.sched.registerReporter(self.smtp_reporter)
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000891
James E. Blairb0fcae42012-07-17 11:12:10 -0700892 self.sched.start()
893 self.sched.reconfigure(self.config)
894 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700895 self.webapp.start()
James E. Blairad28e912013-11-27 10:43:22 -0800896 self.rpc.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700897 self.launcher.gearman.waitForServer()
898 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400899 self.builds = self.worker.running_builds
900 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700901
James E. Blairfee8d652013-06-07 08:57:52 -0700902 self.addCleanup(self.assertFinalState)
903 self.addCleanup(self.shutdown)
904
905 def assertFinalState(self):
906 # Make sure that the change cache is cleared
James E. Blair6c358e72013-07-29 17:06:47 -0700907 self.assertEqual(len(self.gerrit._change_cache.keys()), 0)
Clark Boylan4ba48d92013-11-11 18:03:53 -0800908 # Make sure that git.Repo objects have been garbage collected.
909 repos = []
910 gc.collect()
911 for obj in gc.get_objects():
912 if isinstance(obj, git.Repo):
913 repos.append(obj)
914 self.assertEqual(len(repos), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700915 self.assertEmptyQueues()
916
917 def shutdown(self):
918 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700919 self.launcher.stop()
James E. Blair4076e2b2014-01-28 12:42:20 -0800920 self.merge_server.stop()
921 self.merge_server.join()
922 self.merge_client.stop()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700923 self.worker.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700924 self.gerrit.stop()
James E. Blair63bb0ef2013-07-29 17:14:51 -0700925 self.timer.stop()
James E. Blairb0fcae42012-07-17 11:12:10 -0700926 self.sched.stop()
927 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700928 self.statsd.stop()
929 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700930 self.webapp.stop()
931 self.webapp.join()
James E. Blairad28e912013-11-27 10:43:22 -0800932 self.rpc.stop()
933 self.rpc.join()
James E. Blair491b3172014-03-10 15:05:36 -0700934 self.gearman_server.shutdown()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700935 threads = threading.enumerate()
936 if len(threads) > 1:
937 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400938 super(TestScheduler, self).tearDown()
939
940 def init_repo(self, project):
941 parts = project.split('/')
942 path = os.path.join(self.upstream_root, *parts[:-1])
943 if not os.path.exists(path):
944 os.makedirs(path)
945 path = os.path.join(self.upstream_root, project)
946 repo = git.Repo.init(path)
947
948 repo.config_writer().set_value('user', 'email', 'user@example.com')
949 repo.config_writer().set_value('user', 'name', 'User Name')
950 repo.config_writer().write()
951
952 fn = os.path.join(path, 'README')
953 f = open(fn, 'w')
954 f.write("test\n")
955 f.close()
956 repo.index.add([fn])
957 repo.index.commit('initial commit')
958 master = repo.create_head('master')
959 repo.create_tag('init')
960
961 mp = repo.create_head('mp')
962 repo.head.reference = mp
963 f = open(fn, 'a')
964 f.write("test mp\n")
965 f.close()
966 repo.index.add([fn])
967 repo.index.commit('mp commit')
968
969 repo.head.reference = master
970 repo.head.reset(index=True, working_tree=True)
971 repo.git.clean('-x', '-f', '-d')
972
973 def ref_has_change(self, ref, change):
974 path = os.path.join(self.git_root, change.project)
975 repo = git.Repo(path)
976 for commit in repo.iter_commits(ref):
977 if commit.message.strip() == ('%s-1' % change.subject):
978 return True
979 return False
980
981 def job_has_changes(self, *args):
982 job = args[0]
983 commits = args[1:]
984 if isinstance(job, FakeBuild):
985 parameters = job.parameters
986 else:
987 parameters = json.loads(job.arguments)
988 project = parameters['ZUUL_PROJECT']
989 path = os.path.join(self.git_root, project)
990 repo = git.Repo(path)
991 ref = parameters['ZUUL_REF']
992 sha = parameters['ZUUL_COMMIT']
993 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
994 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
995 commit_messages = ['%s-1' % commit.subject for commit in commits]
James E. Blairac2c3242014-01-24 13:38:51 -0800996 self.log.debug("Checking if job %s has changes; commit_messages %s;"
997 " repo_messages %s; sha %s" % (job, commit_messages,
998 repo_messages, sha))
Monty Taylorbc758832013-06-17 17:22:42 -0400999 for msg in commit_messages:
1000 if msg not in repo_messages:
James E. Blairac2c3242014-01-24 13:38:51 -08001001 self.log.debug(" messages do not match")
Monty Taylorbc758832013-06-17 17:22:42 -04001002 return False
1003 if repo_shas[0] != sha:
James E. Blairac2c3242014-01-24 13:38:51 -08001004 self.log.debug(" sha does not match")
Monty Taylorbc758832013-06-17 17:22:42 -04001005 return False
James E. Blairac2c3242014-01-24 13:38:51 -08001006 self.log.debug(" OK")
Monty Taylorbc758832013-06-17 17:22:42 -04001007 return True
James E. Blairb0fcae42012-07-17 11:12:10 -07001008
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001009 def registerJobs(self):
1010 count = 0
James E. Blaireff88162013-07-01 12:44:14 -04001011 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001012 self.worker.registerFunction('build:' + job)
1013 count += 1
1014 self.worker.registerFunction('stop:' + self.worker.worker_id)
1015 count += 1
1016
1017 while len(self.gearman_server.functions) < count:
1018 time.sleep(0)
1019
1020 def release(self, job):
1021 if isinstance(job, FakeBuild):
1022 job.release()
1023 else:
1024 job.waiting = False
1025 self.log.debug("Queued job %s released" % job.unique)
1026 self.gearman_server.wakeConnections()
1027
1028 def getParameter(self, job, name):
1029 if isinstance(job, FakeBuild):
1030 return job.parameters[name]
1031 else:
1032 parameters = json.loads(job.arguments)
1033 return parameters[name]
1034
1035 def resetGearmanServer(self):
1036 self.worker.setFunctions([])
1037 while True:
1038 done = True
1039 for connection in self.gearman_server.active_connections:
James E. Blairad28e912013-11-27 10:43:22 -08001040 if (connection.functions and
James E. Blair4076e2b2014-01-28 12:42:20 -08001041 connection.client_id not in ['Zuul RPC Listener',
1042 'Zuul Merger']):
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001043 done = False
1044 if done:
1045 break
1046 time.sleep(0)
1047 self.gearman_server.functions = set()
James E. Blairad28e912013-11-27 10:43:22 -08001048 self.rpc.register()
James E. Blair4076e2b2014-01-28 12:42:20 -08001049 self.merge_server.register()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001050
1051 def haveAllBuildsReported(self):
1052 # See if Zuul is waiting on a meta job to complete
1053 if self.launcher.meta_jobs:
1054 return False
1055 # Find out if every build that the worker has completed has been
1056 # reported back to Zuul. If it hasn't then that means a Gearman
1057 # event is still in transit and the system is not stable.
1058 for build in self.worker.build_history:
1059 zbuild = self.launcher.builds.get(build.uuid)
1060 if not zbuild:
1061 # It has already been reported
1062 continue
1063 # It hasn't been reported yet.
1064 return False
1065 # Make sure that none of the worker connections are in GRAB_WAIT
1066 for connection in self.worker.active_connections:
1067 if connection.state == 'GRAB_WAIT':
1068 return False
1069 return True
1070
1071 def areAllBuildsWaiting(self):
1072 ret = True
1073
1074 builds = self.launcher.builds.values()
1075 for build in builds:
1076 client_job = None
1077 for conn in self.launcher.gearman.active_connections:
1078 for j in conn.related_jobs.values():
1079 if j.unique == build.uuid:
1080 client_job = j
1081 break
1082 if not client_job:
1083 self.log.debug("%s is not known to the gearman client" %
1084 build)
1085 ret = False
1086 continue
1087 if not client_job.handle:
1088 self.log.debug("%s has no handle" % client_job)
1089 ret = False
1090 continue
1091 server_job = self.gearman_server.jobs.get(client_job.handle)
1092 if not server_job:
1093 self.log.debug("%s is not known to the gearman server" %
1094 client_job)
1095 ret = False
1096 continue
1097 if not hasattr(server_job, 'waiting'):
1098 self.log.debug("%s is being enqueued" % server_job)
1099 ret = False
1100 continue
1101 if server_job.waiting:
1102 continue
1103 worker_job = self.worker.gearman_jobs.get(server_job.unique)
1104 if worker_job:
1105 if worker_job.build.isWaiting():
1106 continue
1107 else:
1108 self.log.debug("%s is running" % worker_job)
1109 ret = False
1110 else:
1111 self.log.debug("%s is unassigned" % server_job)
1112 ret = False
1113 return ret
1114
James E. Blairb0fcae42012-07-17 11:12:10 -07001115 def waitUntilSettled(self):
1116 self.log.debug("Waiting until settled...")
1117 start = time.time()
1118 while True:
1119 if time.time() - start > 10:
1120 print 'queue status:',
1121 print self.sched.trigger_event_queue.empty(),
1122 print self.sched.result_event_queue.empty(),
1123 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001124 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -07001125 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001126 # Make sure no new events show up while we're checking
1127 self.worker.lock.acquire()
1128 # have all build states propogated to zuul?
1129 if self.haveAllBuildsReported():
1130 # Join ensures that the queue is empty _and_ events have been
1131 # processed
1132 self.fake_gerrit.event_queue.join()
1133 self.sched.trigger_event_queue.join()
1134 self.sched.result_event_queue.join()
James E. Blaira84f0e42014-02-06 07:09:22 -08001135 self.sched.run_handler_lock.acquire()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001136 if (self.sched.trigger_event_queue.empty() and
1137 self.sched.result_event_queue.empty() and
1138 self.fake_gerrit.event_queue.empty() and
James E. Blair4076e2b2014-01-28 12:42:20 -08001139 not self.merge_client.build_sets and
James E. Blair9c2985a2014-03-10 10:59:05 -07001140 self.haveAllBuildsReported() and
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001141 self.areAllBuildsWaiting()):
James E. Blaira84f0e42014-02-06 07:09:22 -08001142 self.sched.run_handler_lock.release()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001143 self.worker.lock.release()
1144 self.log.debug("...settled.")
1145 return
James E. Blaira84f0e42014-02-06 07:09:22 -08001146 self.sched.run_handler_lock.release()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001147 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001148 self.sched.wake_event.wait(0.1)
1149
James E. Blaird466dc42012-07-31 10:42:56 -07001150 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -08001151 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -07001152 return len(jobs)
1153
James E. Blair4ca985f2013-05-30 12:27:43 -07001154 def getJobFromHistory(self, name):
1155 history = self.worker.build_history
1156 for job in history:
1157 if job.name == name:
1158 return job
1159 raise Exception("Unable to find job %s in history" % name)
1160
James E. Blaire0487072012-08-29 17:38:31 -07001161 def assertEmptyQueues(self):
1162 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001163 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001164 for queue in pipeline.queues:
1165 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001166 print 'pipeline %s queue %s contents %s' % (
1167 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001168 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001169
James E. Blair66eeebf2013-07-27 17:44:32 -07001170 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001171 start = time.time()
1172 while time.time() < (start + 5):
1173 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001174 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001175 k, v = stat.split(':')
1176 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001177 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001178 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001179 elif value:
1180 if value == v:
1181 return
1182 elif kind:
1183 if v.endswith('|' + kind):
1184 return
James E. Blair412e5582013-04-22 15:50:12 -07001185 time.sleep(0.1)
1186
1187 pprint.pprint(self.statsd.stats)
1188 raise Exception("Key %s not found in reported stats" % key)
1189
James E. Blairb0fcae42012-07-17 11:12:10 -07001190 def test_jobs_launched(self):
1191 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001192
James E. Blairb0fcae42012-07-17 11:12:10 -07001193 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001194 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001195 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1196 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001197 self.assertEqual(self.getJobFromHistory('project-merge').result,
1198 'SUCCESS')
1199 self.assertEqual(self.getJobFromHistory('project-test1').result,
1200 'SUCCESS')
1201 self.assertEqual(self.getJobFromHistory('project-test2').result,
1202 'SUCCESS')
1203 self.assertEqual(A.data['status'], 'MERGED')
1204 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001205
James E. Blair66eeebf2013-07-27 17:44:32 -07001206 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1207 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1208 value='1|g')
1209 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1210 kind='ms')
1211 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1212 value='1|c')
1213 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1214 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1215 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001216 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001217 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001218 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001219 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001220
James E. Blair3cb10702013-08-24 08:56:03 -07001221 def test_initial_pipeline_gauges(self):
1222 "Test that each pipeline reported its length on start"
1223 pipeline_names = self.sched.layout.pipelines.keys()
1224 self.assertNotEqual(len(pipeline_names), 0)
1225 for name in pipeline_names:
1226 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
1227 value='0|g')
1228
James E. Blair42f74822013-05-14 15:18:03 -07001229 def test_duplicate_pipelines(self):
1230 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001231
James E. Blair42f74822013-05-14 15:18:03 -07001232 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1233 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1234 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001235
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001236 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001237 self.history[0].name == 'project-test1'
1238 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001239
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001240 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001241 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001242 self.assertIn('dup1/project-test1', A.messages[0])
1243 self.assertNotIn('dup2/project-test1', A.messages[0])
1244 self.assertNotIn('dup1/project-test1', A.messages[1])
1245 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001246 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001247 self.assertIn('dup1/project-test1', A.messages[1])
1248 self.assertNotIn('dup2/project-test1', A.messages[1])
1249 self.assertNotIn('dup1/project-test1', A.messages[0])
1250 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001251
James E. Blairb0fcae42012-07-17 11:12:10 -07001252 def test_parallel_changes(self):
1253 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001254
1255 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001256 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1257 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1258 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001259 A.addApproval('CRVW', 2)
1260 B.addApproval('CRVW', 2)
1261 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001262
1263 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1264 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1265 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1266
1267 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001268 self.assertEqual(len(self.builds), 1)
1269 self.assertEqual(self.builds[0].name, 'project-merge')
1270 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001271
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001272 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001273 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001274 self.assertEqual(len(self.builds), 3)
1275 self.assertEqual(self.builds[0].name, 'project-test1')
1276 self.assertTrue(self.job_has_changes(self.builds[0], A))
1277 self.assertEqual(self.builds[1].name, 'project-test2')
1278 self.assertTrue(self.job_has_changes(self.builds[1], A))
1279 self.assertEqual(self.builds[2].name, 'project-merge')
1280 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001281
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001282 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001283 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001284 self.assertEqual(len(self.builds), 5)
1285 self.assertEqual(self.builds[0].name, 'project-test1')
1286 self.assertTrue(self.job_has_changes(self.builds[0], A))
1287 self.assertEqual(self.builds[1].name, 'project-test2')
1288 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001289
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001290 self.assertEqual(self.builds[2].name, 'project-test1')
1291 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1292 self.assertEqual(self.builds[3].name, 'project-test2')
1293 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001294
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001295 self.assertEqual(self.builds[4].name, 'project-merge')
1296 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001297
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001298 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001299 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001300 self.assertEqual(len(self.builds), 6)
1301 self.assertEqual(self.builds[0].name, 'project-test1')
1302 self.assertTrue(self.job_has_changes(self.builds[0], A))
1303 self.assertEqual(self.builds[1].name, 'project-test2')
1304 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001305
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001306 self.assertEqual(self.builds[2].name, 'project-test1')
1307 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1308 self.assertEqual(self.builds[3].name, 'project-test2')
1309 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001310
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001311 self.assertEqual(self.builds[4].name, 'project-test1')
1312 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1313 self.assertEqual(self.builds[5].name, 'project-test2')
1314 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001315
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001316 self.worker.hold_jobs_in_build = False
1317 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001318 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001319 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001320
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001321 self.assertEqual(len(self.history), 9)
1322 self.assertEqual(A.data['status'], 'MERGED')
1323 self.assertEqual(B.data['status'], 'MERGED')
1324 self.assertEqual(C.data['status'], 'MERGED')
1325 self.assertEqual(A.reported, 2)
1326 self.assertEqual(B.reported, 2)
1327 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001328
1329 def test_failed_changes(self):
1330 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001331 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001332
James E. Blairb02a3bb2012-07-30 17:49:55 -07001333 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1334 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001335 A.addApproval('CRVW', 2)
1336 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001337
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001338 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001339
James E. Blaire2819012013-06-28 17:17:26 -04001340 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1341 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001342 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001343
1344 self.worker.release('.*-merge')
1345 self.waitUntilSettled()
1346
1347 self.worker.hold_jobs_in_build = False
1348 self.worker.release()
1349
1350 self.waitUntilSettled()
1351 # It's certain that the merge job for change 2 will run, but
1352 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001353 self.assertTrue(len(self.history) > 6)
1354 self.assertEqual(A.data['status'], 'NEW')
1355 self.assertEqual(B.data['status'], 'MERGED')
1356 self.assertEqual(A.reported, 2)
1357 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001358
1359 def test_independent_queues(self):
1360 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001361
1362 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001363 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001364 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1365 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001366 A.addApproval('CRVW', 2)
1367 B.addApproval('CRVW', 2)
1368 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001369
1370 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1371 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1372 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1373
James E. Blairb02a3bb2012-07-30 17:49:55 -07001374 self.waitUntilSettled()
1375
1376 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001377 self.assertEqual(len(self.builds), 2)
1378 self.assertEqual(self.builds[0].name, 'project-merge')
1379 self.assertTrue(self.job_has_changes(self.builds[0], A))
1380 self.assertEqual(self.builds[1].name, 'project1-merge')
1381 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001382
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001383 # Release the current merge builds
1384 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001385 self.waitUntilSettled()
1386 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001387 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001388 self.waitUntilSettled()
1389
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001390 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001391 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001392 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001393
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001394 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001395 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001396 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001397
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001398 self.assertEqual(len(self.history), 11)
1399 self.assertEqual(A.data['status'], 'MERGED')
1400 self.assertEqual(B.data['status'], 'MERGED')
1401 self.assertEqual(C.data['status'], 'MERGED')
1402 self.assertEqual(A.reported, 2)
1403 self.assertEqual(B.reported, 2)
1404 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001405
1406 def test_failed_change_at_head(self):
1407 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001408
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001409 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001410 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1411 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1412 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001413 A.addApproval('CRVW', 2)
1414 B.addApproval('CRVW', 2)
1415 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001416
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001417 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001418
1419 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1420 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1421 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1422
1423 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001424
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001425 self.assertEqual(len(self.builds), 1)
1426 self.assertEqual(self.builds[0].name, 'project-merge')
1427 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001428
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001429 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001430 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001431 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001432 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001433 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001434 self.waitUntilSettled()
1435
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001436 self.assertEqual(len(self.builds), 6)
1437 self.assertEqual(self.builds[0].name, 'project-test1')
1438 self.assertEqual(self.builds[1].name, 'project-test2')
1439 self.assertEqual(self.builds[2].name, 'project-test1')
1440 self.assertEqual(self.builds[3].name, 'project-test2')
1441 self.assertEqual(self.builds[4].name, 'project-test1')
1442 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001443
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001444 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001445 self.waitUntilSettled()
1446
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001447 # project-test2, project-merge for B
1448 self.assertEqual(len(self.builds), 2)
1449 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001450
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001451 self.worker.hold_jobs_in_build = False
1452 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001453 self.waitUntilSettled()
1454
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001455 self.assertEqual(len(self.builds), 0)
1456 self.assertEqual(len(self.history), 15)
1457 self.assertEqual(A.data['status'], 'NEW')
1458 self.assertEqual(B.data['status'], 'MERGED')
1459 self.assertEqual(C.data['status'], 'MERGED')
1460 self.assertEqual(A.reported, 2)
1461 self.assertEqual(B.reported, 2)
1462 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001463
James E. Blair0aac4872013-08-23 14:02:38 -07001464 def test_failed_change_in_middle(self):
1465 "Test a failed change in the middle of the queue"
1466
1467 self.worker.hold_jobs_in_build = True
1468 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1469 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1470 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1471 A.addApproval('CRVW', 2)
1472 B.addApproval('CRVW', 2)
1473 C.addApproval('CRVW', 2)
1474
1475 self.worker.addFailTest('project-test1', B)
1476
1477 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1478 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1479 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1480
1481 self.waitUntilSettled()
1482
1483 self.worker.release('.*-merge')
1484 self.waitUntilSettled()
1485 self.worker.release('.*-merge')
1486 self.waitUntilSettled()
1487 self.worker.release('.*-merge')
1488 self.waitUntilSettled()
1489
1490 self.assertEqual(len(self.builds), 6)
1491 self.assertEqual(self.builds[0].name, 'project-test1')
1492 self.assertEqual(self.builds[1].name, 'project-test2')
1493 self.assertEqual(self.builds[2].name, 'project-test1')
1494 self.assertEqual(self.builds[3].name, 'project-test2')
1495 self.assertEqual(self.builds[4].name, 'project-test1')
1496 self.assertEqual(self.builds[5].name, 'project-test2')
1497
1498 self.release(self.builds[2])
1499 self.waitUntilSettled()
1500
James E. Blair972e3c72013-08-29 12:04:55 -07001501 # project-test1 and project-test2 for A
1502 # project-test2 for B
1503 # project-merge for C (without B)
1504 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -07001505 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1506
James E. Blair972e3c72013-08-29 12:04:55 -07001507 self.worker.release('.*-merge')
1508 self.waitUntilSettled()
1509
1510 # project-test1 and project-test2 for A
1511 # project-test2 for B
1512 # project-test1 and project-test2 for C
1513 self.assertEqual(len(self.builds), 5)
1514
James E. Blair0aac4872013-08-23 14:02:38 -07001515 items = self.sched.layout.pipelines['gate'].getAllItems()
1516 builds = items[0].current_build_set.getBuilds()
1517 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1518 self.assertEqual(self.countJobResults(builds, None), 2)
1519 builds = items[1].current_build_set.getBuilds()
1520 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1521 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1522 self.assertEqual(self.countJobResults(builds, None), 1)
1523 builds = items[2].current_build_set.getBuilds()
1524 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -07001525 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -07001526
1527 self.worker.hold_jobs_in_build = False
1528 self.worker.release()
1529 self.waitUntilSettled()
1530
1531 self.assertEqual(len(self.builds), 0)
1532 self.assertEqual(len(self.history), 12)
1533 self.assertEqual(A.data['status'], 'MERGED')
1534 self.assertEqual(B.data['status'], 'NEW')
1535 self.assertEqual(C.data['status'], 'MERGED')
1536 self.assertEqual(A.reported, 2)
1537 self.assertEqual(B.reported, 2)
1538 self.assertEqual(C.reported, 2)
1539
James E. Blaird466dc42012-07-31 10:42:56 -07001540 def test_failed_change_at_head_with_queue(self):
1541 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001542
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001543 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001544 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1545 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1546 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001547 A.addApproval('CRVW', 2)
1548 B.addApproval('CRVW', 2)
1549 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001550
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001551 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001552
1553 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1554 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1555 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1556
1557 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001558 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001559 self.assertEqual(len(self.builds), 0)
1560 self.assertEqual(len(queue), 1)
1561 self.assertEqual(queue[0].name, 'build:project-merge')
1562 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001563
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001564 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001565 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001566 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001567 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001568 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001569 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001570 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001571
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001572 self.assertEqual(len(self.builds), 0)
1573 self.assertEqual(len(queue), 6)
1574 self.assertEqual(queue[0].name, 'build:project-test1')
1575 self.assertEqual(queue[1].name, 'build:project-test2')
1576 self.assertEqual(queue[2].name, 'build:project-test1')
1577 self.assertEqual(queue[3].name, 'build:project-test2')
1578 self.assertEqual(queue[4].name, 'build:project-test1')
1579 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001580
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001581 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001582 self.waitUntilSettled()
1583
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001584 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001585 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001586 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1587 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001588
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001589 self.gearman_server.hold_jobs_in_queue = False
1590 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001591 self.waitUntilSettled()
1592
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001593 self.assertEqual(len(self.builds), 0)
1594 self.assertEqual(len(self.history), 11)
1595 self.assertEqual(A.data['status'], 'NEW')
1596 self.assertEqual(B.data['status'], 'MERGED')
1597 self.assertEqual(C.data['status'], 'MERGED')
1598 self.assertEqual(A.reported, 2)
1599 self.assertEqual(B.reported, 2)
1600 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001601
James E. Blairfef71632013-09-23 11:15:47 -07001602 def test_two_failed_changes_at_head(self):
1603 "Test that changes are reparented correctly if 2 fail at head"
1604
1605 self.worker.hold_jobs_in_build = True
1606 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1607 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1608 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1609 A.addApproval('CRVW', 2)
1610 B.addApproval('CRVW', 2)
1611 C.addApproval('CRVW', 2)
1612
1613 self.worker.addFailTest('project-test1', A)
1614 self.worker.addFailTest('project-test1', B)
1615
1616 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1617 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1618 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1619 self.waitUntilSettled()
1620
1621 self.worker.release('.*-merge')
1622 self.waitUntilSettled()
1623 self.worker.release('.*-merge')
1624 self.waitUntilSettled()
1625 self.worker.release('.*-merge')
1626 self.waitUntilSettled()
1627
1628 self.assertEqual(len(self.builds), 6)
1629 self.assertEqual(self.builds[0].name, 'project-test1')
1630 self.assertEqual(self.builds[1].name, 'project-test2')
1631 self.assertEqual(self.builds[2].name, 'project-test1')
1632 self.assertEqual(self.builds[3].name, 'project-test2')
1633 self.assertEqual(self.builds[4].name, 'project-test1')
1634 self.assertEqual(self.builds[5].name, 'project-test2')
1635
1636 self.assertTrue(self.job_has_changes(self.builds[0], A))
1637 self.assertTrue(self.job_has_changes(self.builds[2], A))
1638 self.assertTrue(self.job_has_changes(self.builds[2], B))
1639 self.assertTrue(self.job_has_changes(self.builds[4], A))
1640 self.assertTrue(self.job_has_changes(self.builds[4], B))
1641 self.assertTrue(self.job_has_changes(self.builds[4], C))
1642
1643 # Fail change B first
1644 self.release(self.builds[2])
1645 self.waitUntilSettled()
1646
1647 # restart of C after B failure
1648 self.worker.release('.*-merge')
1649 self.waitUntilSettled()
1650
1651 self.assertEqual(len(self.builds), 5)
1652 self.assertEqual(self.builds[0].name, 'project-test1')
1653 self.assertEqual(self.builds[1].name, 'project-test2')
1654 self.assertEqual(self.builds[2].name, 'project-test2')
1655 self.assertEqual(self.builds[3].name, 'project-test1')
1656 self.assertEqual(self.builds[4].name, 'project-test2')
1657
1658 self.assertTrue(self.job_has_changes(self.builds[1], A))
1659 self.assertTrue(self.job_has_changes(self.builds[2], A))
1660 self.assertTrue(self.job_has_changes(self.builds[2], B))
1661 self.assertTrue(self.job_has_changes(self.builds[4], A))
1662 self.assertFalse(self.job_has_changes(self.builds[4], B))
1663 self.assertTrue(self.job_has_changes(self.builds[4], C))
1664
1665 # Finish running all passing jobs for change A
1666 self.release(self.builds[1])
1667 self.waitUntilSettled()
1668 # Fail and report change A
1669 self.release(self.builds[0])
1670 self.waitUntilSettled()
1671
1672 # restart of B,C after A failure
1673 self.worker.release('.*-merge')
1674 self.waitUntilSettled()
1675 self.worker.release('.*-merge')
1676 self.waitUntilSettled()
1677
1678 self.assertEqual(len(self.builds), 4)
1679 self.assertEqual(self.builds[0].name, 'project-test1') # B
1680 self.assertEqual(self.builds[1].name, 'project-test2') # B
1681 self.assertEqual(self.builds[2].name, 'project-test1') # C
1682 self.assertEqual(self.builds[3].name, 'project-test2') # C
1683
1684 self.assertFalse(self.job_has_changes(self.builds[1], A))
1685 self.assertTrue(self.job_has_changes(self.builds[1], B))
1686 self.assertFalse(self.job_has_changes(self.builds[1], C))
1687
1688 self.assertFalse(self.job_has_changes(self.builds[2], A))
1689 # After A failed and B and C restarted, B should be back in
1690 # C's tests because it has not failed yet.
1691 self.assertTrue(self.job_has_changes(self.builds[2], B))
1692 self.assertTrue(self.job_has_changes(self.builds[2], C))
1693
1694 self.worker.hold_jobs_in_build = False
1695 self.worker.release()
1696 self.waitUntilSettled()
1697
1698 self.assertEqual(len(self.builds), 0)
1699 self.assertEqual(len(self.history), 21)
1700 self.assertEqual(A.data['status'], 'NEW')
1701 self.assertEqual(B.data['status'], 'NEW')
1702 self.assertEqual(C.data['status'], 'MERGED')
1703 self.assertEqual(A.reported, 2)
1704 self.assertEqual(B.reported, 2)
1705 self.assertEqual(C.reported, 2)
1706
James E. Blair8c803f82012-07-31 16:25:42 -07001707 def test_patch_order(self):
1708 "Test that dependent patches are tested in the right order"
1709 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1710 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1711 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1712 A.addApproval('CRVW', 2)
1713 B.addApproval('CRVW', 2)
1714 C.addApproval('CRVW', 2)
1715
1716 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1717 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1718 M2.setMerged()
1719 M1.setMerged()
1720
1721 # C -> B -> A -> M1 -> M2
1722 # M2 is here to make sure it is never queried. If it is, it
1723 # means zuul is walking down the entire history of merged
1724 # changes.
1725
1726 C.setDependsOn(B, 1)
1727 B.setDependsOn(A, 1)
1728 A.setDependsOn(M1, 1)
1729 M1.setDependsOn(M2, 1)
1730
1731 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1732
1733 self.waitUntilSettled()
1734
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001735 self.assertEqual(A.data['status'], 'NEW')
1736 self.assertEqual(B.data['status'], 'NEW')
1737 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001738
1739 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1740 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1741
1742 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001743 self.assertEqual(M2.queried, 0)
1744 self.assertEqual(A.data['status'], 'MERGED')
1745 self.assertEqual(B.data['status'], 'MERGED')
1746 self.assertEqual(C.data['status'], 'MERGED')
1747 self.assertEqual(A.reported, 2)
1748 self.assertEqual(B.reported, 2)
1749 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001750
James E. Blair0e933c52013-07-11 10:18:52 -07001751 def test_trigger_cache(self):
1752 "Test that the trigger cache operates correctly"
1753 self.worker.hold_jobs_in_build = True
1754
1755 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1756 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1757 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1758 A.addApproval('CRVW', 2)
1759 B.addApproval('CRVW', 2)
1760
1761 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1762 M1.setMerged()
1763
1764 B.setDependsOn(A, 1)
1765 A.setDependsOn(M1, 1)
1766
1767 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1768 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1769
1770 self.waitUntilSettled()
1771
1772 for build in self.builds:
1773 if build.parameters['ZUUL_PIPELINE'] == 'check':
1774 build.release()
1775 self.waitUntilSettled()
1776 for build in self.builds:
1777 if build.parameters['ZUUL_PIPELINE'] == 'check':
1778 build.release()
1779 self.waitUntilSettled()
1780
1781 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1782 self.waitUntilSettled()
1783
James E. Blair6c358e72013-07-29 17:06:47 -07001784 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001785 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001786 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001787
1788 self.worker.hold_jobs_in_build = False
1789 self.worker.release()
1790 self.waitUntilSettled()
1791
1792 self.assertEqual(A.data['status'], 'MERGED')
1793 self.assertEqual(B.data['status'], 'MERGED')
1794 self.assertEqual(A.queried, 2) # Initial and isMerged
1795 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1796
James E. Blair8c803f82012-07-31 16:25:42 -07001797 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001798 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001799 # TODO: move to test_gerrit (this is a unit test!)
1800 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001801 trigger = self.sched.layout.pipelines['gate'].trigger
1802 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001803 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001804 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001805
1806 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001807 a = trigger.getChange(1, 2, refresh=True)
1808 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001809
1810 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001811 a = trigger.getChange(1, 2, refresh=True)
1812 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1813 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001814
1815 def test_build_configuration(self):
1816 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001817
1818 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001819 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1820 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1821 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1822 A.addApproval('CRVW', 2)
1823 B.addApproval('CRVW', 2)
1824 C.addApproval('CRVW', 2)
1825 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1826 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1827 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1828 self.waitUntilSettled()
1829
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001830 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001831 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001832 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001833 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001834 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001835 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001836 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001837 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1838 self.gearman_server.hold_jobs_in_queue = False
1839 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001840 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001841
Monty Taylorbc758832013-06-17 17:22:42 -04001842 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001843 repo = git.Repo(path)
1844 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1845 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001846 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001847 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001848
1849 def test_build_configuration_conflict(self):
1850 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001851
1852 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001853 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1854 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001855 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001856 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1857 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001858 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001859 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1860 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001861 A.addApproval('CRVW', 2)
1862 B.addApproval('CRVW', 2)
1863 C.addApproval('CRVW', 2)
1864 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1865 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1866 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1867 self.waitUntilSettled()
1868
James E. Blair6736beb2013-07-11 15:18:15 -07001869 self.assertEqual(A.reported, 1)
1870 self.assertEqual(B.reported, 1)
1871 self.assertEqual(C.reported, 1)
1872
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001873 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001874 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001875 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001876 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001877 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001878 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001879
1880 self.assertEqual(len(self.history), 2) # A and C merge jobs
1881
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001882 self.gearman_server.hold_jobs_in_queue = False
1883 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001884 self.waitUntilSettled()
1885
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001886 self.assertEqual(A.data['status'], 'MERGED')
1887 self.assertEqual(B.data['status'], 'NEW')
1888 self.assertEqual(C.data['status'], 'MERGED')
1889 self.assertEqual(A.reported, 2)
1890 self.assertEqual(B.reported, 2)
1891 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001892 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001893
James E. Blairdaabed22012-08-15 15:38:57 -07001894 def test_post(self):
1895 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001896
Zhongyue Luo5d556072012-09-21 02:00:47 +09001897 e = {
1898 "type": "ref-updated",
1899 "submitter": {
1900 "name": "User Name",
1901 },
1902 "refUpdate": {
1903 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1904 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1905 "refName": "master",
1906 "project": "org/project",
1907 }
1908 }
James E. Blairdaabed22012-08-15 15:38:57 -07001909 self.fake_gerrit.addEvent(e)
1910 self.waitUntilSettled()
1911
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001912 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001913 self.assertEqual(len(self.history), 1)
1914 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001915
1916 def test_build_configuration_branch(self):
1917 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001918
1919 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001920 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1921 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1922 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1923 A.addApproval('CRVW', 2)
1924 B.addApproval('CRVW', 2)
1925 C.addApproval('CRVW', 2)
1926 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1927 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1928 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1929 self.waitUntilSettled()
1930
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001931 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001932 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001933 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001934 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001935 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001936 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001937 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001938 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1939 self.gearman_server.hold_jobs_in_queue = False
1940 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001941 self.waitUntilSettled()
1942
Monty Taylorbc758832013-06-17 17:22:42 -04001943 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001944 repo = git.Repo(path)
1945 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1946 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001947 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001948 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001949
1950 def test_build_configuration_branch_interaction(self):
1951 "Test that switching between branches works"
1952 self.test_build_configuration()
1953 self.test_build_configuration_branch()
1954 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001955 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001956 repo = git.Repo(path)
1957 repo.heads.master.commit = repo.commit('init')
1958 self.test_build_configuration()
1959
1960 def test_build_configuration_multi_branch(self):
1961 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001962
1963 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001964 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1965 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1966 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1967 A.addApproval('CRVW', 2)
1968 B.addApproval('CRVW', 2)
1969 C.addApproval('CRVW', 2)
1970 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1971 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1972 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1973 self.waitUntilSettled()
James E. Blairbb1fe502014-03-04 10:15:06 -08001974 queue = self.gearman_server.getQueue()
1975 job_A = None
1976 for job in queue:
1977 if 'project-merge' in job.name:
1978 job_A = job
1979 ref_A = self.getParameter(job_A, 'ZUUL_REF')
1980 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
1981 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
1982 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
James E. Blairc6294a52012-08-17 10:19:48 -07001983
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001984 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001985 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001986 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001987 job_B = None
1988 for job in queue:
1989 if 'project-merge' in job.name:
1990 job_B = job
1991 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001992 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001993 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blairbb1fe502014-03-04 10:15:06 -08001994 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
1995
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001996 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001997 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001998 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001999 for job in queue:
2000 if 'project-merge' in job.name:
2001 job_C = job
2002 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08002003 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07002004 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blairbb1fe502014-03-04 10:15:06 -08002005 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002006 self.gearman_server.hold_jobs_in_queue = False
2007 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07002008 self.waitUntilSettled()
2009
Monty Taylorbc758832013-06-17 17:22:42 -04002010 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07002011 repo = git.Repo(path)
2012
2013 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07002014 for c in repo.iter_commits(ref_C)]
James E. Blairbb1fe502014-03-04 10:15:06 -08002015 repo_shas = [c.hexsha for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07002016 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07002017 correct_messages = ['initial commit', 'A-1', 'C-1']
James E. Blairbb1fe502014-03-04 10:15:06 -08002018 # Ensure the right commits are in the history for this ref
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002019 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08002020 # Ensure ZUUL_REF -> ZUUL_COMMIT
2021 self.assertEqual(repo_shas[0], commit_C)
James E. Blairc6294a52012-08-17 10:19:48 -07002022
2023 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07002024 for c in repo.iter_commits(ref_B)]
James E. Blairbb1fe502014-03-04 10:15:06 -08002025 repo_shas = [c.hexsha for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07002026 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07002027 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002028 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08002029 self.assertEqual(repo_shas[0], commit_B)
2030
2031 repo_messages = [c.message.strip()
2032 for c in repo.iter_commits(ref_A)]
2033 repo_shas = [c.hexsha for c in repo.iter_commits(ref_A)]
2034 repo_messages.reverse()
2035 correct_messages = ['initial commit', 'A-1']
2036 self.assertEqual(repo_messages, correct_messages)
2037 self.assertEqual(repo_shas[0], commit_A)
2038
2039 self.assertNotEqual(ref_A, ref_B, ref_C)
2040 self.assertNotEqual(commit_A, commit_B, commit_C)
James E. Blair7f71c802012-08-22 13:04:32 -07002041
2042 def test_one_job_project(self):
2043 "Test that queueing works with one job"
2044 A = self.fake_gerrit.addFakeChange('org/one-job-project',
2045 'master', 'A')
2046 B = self.fake_gerrit.addFakeChange('org/one-job-project',
2047 'master', 'B')
2048 A.addApproval('CRVW', 2)
2049 B.addApproval('CRVW', 2)
2050 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2051 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2052 self.waitUntilSettled()
2053
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002054 self.assertEqual(A.data['status'], 'MERGED')
2055 self.assertEqual(A.reported, 2)
2056 self.assertEqual(B.data['status'], 'MERGED')
2057 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07002058
Antoine Musso80edd5a2013-02-13 15:37:53 +01002059 def test_job_from_templates_launched(self):
2060 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002061
Antoine Musso80edd5a2013-02-13 15:37:53 +01002062 A = self.fake_gerrit.addFakeChange(
2063 'org/templated-project', 'master', 'A')
2064 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2065 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01002066
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002067 self.assertEqual(self.getJobFromHistory('project-test1').result,
2068 'SUCCESS')
2069 self.assertEqual(self.getJobFromHistory('project-test2').result,
2070 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01002071
James E. Blair3e98c022013-12-16 15:25:38 -08002072 def test_layered_templates(self):
2073 "Test whether a job generated via a template can be launched"
2074
2075 A = self.fake_gerrit.addFakeChange(
2076 'org/layered-project', 'master', 'A')
2077 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2078 self.waitUntilSettled()
2079
2080 self.assertEqual(self.getJobFromHistory('project-test1').result,
2081 'SUCCESS')
2082 self.assertEqual(self.getJobFromHistory('project-test2').result,
2083 'SUCCESS')
James E. Blairaea6cf62013-12-16 15:38:12 -08002084 self.assertEqual(self.getJobFromHistory('layered-project-test3'
2085 ).result, 'SUCCESS')
2086 self.assertEqual(self.getJobFromHistory('layered-project-test4'
2087 ).result, 'SUCCESS')
James E. Blair12a92b12014-03-26 11:54:53 -07002088 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
2089 ).result, 'SUCCESS')
James E. Blair3e98c022013-12-16 15:25:38 -08002090 self.assertEqual(self.getJobFromHistory('project-test6').result,
2091 'SUCCESS')
2092
James E. Blaircaec0c52012-08-22 14:52:22 -07002093 def test_dependent_changes_dequeue(self):
2094 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002095
James E. Blaircaec0c52012-08-22 14:52:22 -07002096 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2097 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2098 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2099 A.addApproval('CRVW', 2)
2100 B.addApproval('CRVW', 2)
2101 C.addApproval('CRVW', 2)
2102
2103 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
2104 M1.setMerged()
2105
2106 # C -> B -> A -> M1
2107
2108 C.setDependsOn(B, 1)
2109 B.setDependsOn(A, 1)
2110 A.setDependsOn(M1, 1)
2111
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002112 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07002113
2114 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2115 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2116 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2117
2118 self.waitUntilSettled()
2119
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002120 self.assertEqual(A.data['status'], 'NEW')
2121 self.assertEqual(A.reported, 2)
2122 self.assertEqual(B.data['status'], 'NEW')
2123 self.assertEqual(B.reported, 2)
2124 self.assertEqual(C.data['status'], 'NEW')
2125 self.assertEqual(C.reported, 2)
2126 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07002127
James E. Blair972e3c72013-08-29 12:04:55 -07002128 def test_failing_dependent_changes(self):
2129 "Test that failing dependent patches are taken out of stream"
2130 self.worker.hold_jobs_in_build = True
2131 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2132 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2133 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2134 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2135 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
2136 A.addApproval('CRVW', 2)
2137 B.addApproval('CRVW', 2)
2138 C.addApproval('CRVW', 2)
2139 D.addApproval('CRVW', 2)
2140 E.addApproval('CRVW', 2)
2141
2142 # E, D -> C -> B, A
2143
2144 D.setDependsOn(C, 1)
2145 C.setDependsOn(B, 1)
2146
2147 self.worker.addFailTest('project-test1', B)
2148
2149 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2150 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2151 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2152 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2153 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2154
2155 self.waitUntilSettled()
2156 self.worker.release('.*-merge')
2157 self.waitUntilSettled()
2158 self.worker.release('.*-merge')
2159 self.waitUntilSettled()
2160 self.worker.release('.*-merge')
2161 self.waitUntilSettled()
2162 self.worker.release('.*-merge')
2163 self.waitUntilSettled()
2164 self.worker.release('.*-merge')
2165 self.waitUntilSettled()
2166
2167 self.worker.hold_jobs_in_build = False
2168 for build in self.builds:
2169 if build.parameters['ZUUL_CHANGE'] != '1':
2170 build.release()
2171 self.waitUntilSettled()
2172
2173 self.worker.release()
2174 self.waitUntilSettled()
2175
2176 self.assertEqual(A.data['status'], 'MERGED')
2177 self.assertEqual(A.reported, 2)
2178 self.assertEqual(B.data['status'], 'NEW')
2179 self.assertEqual(B.reported, 2)
2180 self.assertEqual(C.data['status'], 'NEW')
2181 self.assertEqual(C.reported, 2)
2182 self.assertEqual(D.data['status'], 'NEW')
2183 self.assertEqual(D.reported, 2)
2184 self.assertEqual(E.data['status'], 'MERGED')
2185 self.assertEqual(E.reported, 2)
2186 self.assertEqual(len(self.history), 18)
2187
James E. Blairec590122012-08-22 15:19:31 -07002188 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08002189 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07002190 # If it's dequeued more than once, we should see extra
2191 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07002192
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002193 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07002194 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2195 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2196 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2197 A.addApproval('CRVW', 2)
2198 B.addApproval('CRVW', 2)
2199 C.addApproval('CRVW', 2)
2200
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002201 self.worker.addFailTest('project1-test1', A)
2202 self.worker.addFailTest('project1-test2', A)
2203 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07002204
2205 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2206 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2207 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2208
2209 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07002210
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002211 self.assertEqual(len(self.builds), 1)
2212 self.assertEqual(self.builds[0].name, 'project1-merge')
2213 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07002214
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002215 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002216 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002217 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002218 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002219 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002220 self.waitUntilSettled()
2221
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002222 self.assertEqual(len(self.builds), 9)
2223 self.assertEqual(self.builds[0].name, 'project1-test1')
2224 self.assertEqual(self.builds[1].name, 'project1-test2')
2225 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
2226 self.assertEqual(self.builds[3].name, 'project1-test1')
2227 self.assertEqual(self.builds[4].name, 'project1-test2')
2228 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
2229 self.assertEqual(self.builds[6].name, 'project1-test1')
2230 self.assertEqual(self.builds[7].name, 'project1-test2')
2231 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07002232
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002233 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07002234 self.waitUntilSettled()
2235
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002236 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
2237 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07002238
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002239 self.worker.hold_jobs_in_build = False
2240 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07002241 self.waitUntilSettled()
2242
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002243 self.assertEqual(len(self.builds), 0)
2244 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07002245
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002246 self.assertEqual(A.data['status'], 'NEW')
2247 self.assertEqual(B.data['status'], 'MERGED')
2248 self.assertEqual(C.data['status'], 'MERGED')
2249 self.assertEqual(A.reported, 2)
2250 self.assertEqual(B.reported, 2)
2251 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07002252
2253 def test_nonvoting_job(self):
2254 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002255
James E. Blair4ec821f2012-08-23 15:28:28 -07002256 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
2257 'master', 'A')
2258 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002259 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07002260 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2261
2262 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07002263
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002264 self.assertEqual(A.data['status'], 'MERGED')
2265 self.assertEqual(A.reported, 2)
2266 self.assertEqual(
2267 self.getJobFromHistory('nonvoting-project-merge').result,
2268 'SUCCESS')
2269 self.assertEqual(
2270 self.getJobFromHistory('nonvoting-project-test1').result,
2271 'SUCCESS')
2272 self.assertEqual(
2273 self.getJobFromHistory('nonvoting-project-test2').result,
2274 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07002275
2276 def test_check_queue_success(self):
2277 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002278
James E. Blaire0487072012-08-29 17:38:31 -07002279 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2280 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2281
2282 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002283
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002284 self.assertEqual(A.data['status'], 'NEW')
2285 self.assertEqual(A.reported, 1)
2286 self.assertEqual(self.getJobFromHistory('project-merge').result,
2287 'SUCCESS')
2288 self.assertEqual(self.getJobFromHistory('project-test1').result,
2289 'SUCCESS')
2290 self.assertEqual(self.getJobFromHistory('project-test2').result,
2291 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07002292
2293 def test_check_queue_failure(self):
2294 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002295
James E. Blaire0487072012-08-29 17:38:31 -07002296 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002297 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07002298 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2299
2300 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002301
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002302 self.assertEqual(A.data['status'], 'NEW')
2303 self.assertEqual(A.reported, 1)
2304 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07002305 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002306 self.assertEqual(self.getJobFromHistory('project-test1').result,
2307 'SUCCESS')
2308 self.assertEqual(self.getJobFromHistory('project-test2').result,
2309 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07002310
2311 def test_dependent_behind_dequeue(self):
2312 "test that dependent changes behind dequeued changes work"
2313 # This complicated test is a reproduction of a real life bug
2314 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07002315
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002316 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07002317 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2318 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2319 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2320 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2321 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
2322 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
2323 D.setDependsOn(C, 1)
2324 E.setDependsOn(D, 1)
2325 A.addApproval('CRVW', 2)
2326 B.addApproval('CRVW', 2)
2327 C.addApproval('CRVW', 2)
2328 D.addApproval('CRVW', 2)
2329 E.addApproval('CRVW', 2)
2330 F.addApproval('CRVW', 2)
2331
2332 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07002333
2334 # Change object re-use in the gerrit trigger is hidden if
2335 # changes are added in quick succession; waiting makes it more
2336 # like real life.
2337 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2338 self.waitUntilSettled()
2339 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2340 self.waitUntilSettled()
2341
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002342 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002343 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002344 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002345 self.waitUntilSettled()
2346
2347 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2348 self.waitUntilSettled()
2349 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2350 self.waitUntilSettled()
2351 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2352 self.waitUntilSettled()
2353 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
2354 self.waitUntilSettled()
2355
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002356 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002357 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002358 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002359 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002360 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002361 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002362 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002363 self.waitUntilSettled()
2364
2365 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002366
2367 # Grab pointers to the jobs we want to release before
2368 # releasing any, because list indexes may change as
2369 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002370 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002371 a.release()
2372 b.release()
2373 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002374 self.waitUntilSettled()
2375
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002376 self.worker.hold_jobs_in_build = False
2377 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002378 self.waitUntilSettled()
2379
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002380 self.assertEqual(A.data['status'], 'NEW')
2381 self.assertEqual(B.data['status'], 'MERGED')
2382 self.assertEqual(C.data['status'], 'MERGED')
2383 self.assertEqual(D.data['status'], 'MERGED')
2384 self.assertEqual(E.data['status'], 'MERGED')
2385 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002386
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002387 self.assertEqual(A.reported, 2)
2388 self.assertEqual(B.reported, 2)
2389 self.assertEqual(C.reported, 2)
2390 self.assertEqual(D.reported, 2)
2391 self.assertEqual(E.reported, 2)
2392 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002393
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002394 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2395 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002396
2397 def test_merger_repack(self):
2398 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002399
James E. Blair05fed602012-09-07 12:45:24 -07002400 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2401 A.addApproval('CRVW', 2)
2402 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2403 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002404 self.assertEqual(self.getJobFromHistory('project-merge').result,
2405 'SUCCESS')
2406 self.assertEqual(self.getJobFromHistory('project-test1').result,
2407 'SUCCESS')
2408 self.assertEqual(self.getJobFromHistory('project-test2').result,
2409 'SUCCESS')
2410 self.assertEqual(A.data['status'], 'MERGED')
2411 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07002412 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002413 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002414
Monty Taylorbc758832013-06-17 17:22:42 -04002415 path = os.path.join(self.git_root, "org/project")
2416 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002417
2418 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2419 A.addApproval('CRVW', 2)
2420 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2421 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002422 self.assertEqual(self.getJobFromHistory('project-merge').result,
2423 'SUCCESS')
2424 self.assertEqual(self.getJobFromHistory('project-test1').result,
2425 'SUCCESS')
2426 self.assertEqual(self.getJobFromHistory('project-test2').result,
2427 'SUCCESS')
2428 self.assertEqual(A.data['status'], 'MERGED')
2429 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002430
James E. Blair4886f282012-11-15 09:27:33 -08002431 def test_merger_repack_large_change(self):
2432 "Test that the merger works with large changes after a repack"
2433 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08002434 # This test assumes the repo is already cloned; make sure it is
2435 url = self.sched.triggers['gerrit'].getGitUrl(
2436 self.sched.layout.projects['org/project1'])
James E. Blair4076e2b2014-01-28 12:42:20 -08002437 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08002438 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2439 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002440 path = os.path.join(self.upstream_root, "org/project1")
2441 print repack_repo(path)
2442 path = os.path.join(self.git_root, "org/project1")
2443 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002444
2445 A.addApproval('CRVW', 2)
2446 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2447 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002448 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2449 'SUCCESS')
2450 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2451 'SUCCESS')
2452 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2453 'SUCCESS')
2454 self.assertEqual(A.data['status'], 'MERGED')
2455 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002456
James E. Blair7ee88a22012-09-12 18:59:31 +02002457 def test_nonexistent_job(self):
2458 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002459 # Set to the state immediately after a restart
2460 self.resetGearmanServer()
2461 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002462
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 # There may be a thread about to report a lost change
2467 while A.reported < 2:
2468 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002469 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002470 self.assertFalse(job_names)
2471 self.assertEqual(A.data['status'], 'NEW')
2472 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002473 self.assertEmptyQueues()
2474
2475 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002476 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002477 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2478 A.addApproval('CRVW', 2)
2479 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2480 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002481 self.assertEqual(self.getJobFromHistory('project-merge').result,
2482 'SUCCESS')
2483 self.assertEqual(self.getJobFromHistory('project-test1').result,
2484 'SUCCESS')
2485 self.assertEqual(self.getJobFromHistory('project-test2').result,
2486 'SUCCESS')
2487 self.assertEqual(A.data['status'], 'MERGED')
2488 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002489
2490 def test_single_nonexistent_post_job(self):
2491 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002492 e = {
2493 "type": "ref-updated",
2494 "submitter": {
2495 "name": "User Name",
2496 },
2497 "refUpdate": {
2498 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2499 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2500 "refName": "master",
2501 "project": "org/project",
2502 }
2503 }
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
2507
James E. Blairf62d4282012-12-31 17:01:50 -08002508 self.fake_gerrit.addEvent(e)
2509 self.waitUntilSettled()
2510
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002511 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002512
2513 def test_new_patchset_dequeues_old(self):
2514 "Test that a new patchset causes the old to be dequeued"
2515 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002516 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002517 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2518 M.setMerged()
2519
2520 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2521 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2522 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2523 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2524 A.addApproval('CRVW', 2)
2525 B.addApproval('CRVW', 2)
2526 C.addApproval('CRVW', 2)
2527 D.addApproval('CRVW', 2)
2528
2529 C.setDependsOn(B, 1)
2530 B.setDependsOn(A, 1)
2531 A.setDependsOn(M, 1)
2532
2533 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2534 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2535 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2536 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2537 self.waitUntilSettled()
2538
2539 B.addPatchset()
2540 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2541 self.waitUntilSettled()
2542
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002543 self.worker.hold_jobs_in_build = False
2544 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002545 self.waitUntilSettled()
2546
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002547 self.assertEqual(A.data['status'], 'MERGED')
2548 self.assertEqual(A.reported, 2)
2549 self.assertEqual(B.data['status'], 'NEW')
2550 self.assertEqual(B.reported, 2)
2551 self.assertEqual(C.data['status'], 'NEW')
2552 self.assertEqual(C.reported, 2)
2553 self.assertEqual(D.data['status'], 'MERGED')
2554 self.assertEqual(D.reported, 2)
2555 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002556
Arx Cruzb1b010d2013-10-28 19:49:59 -02002557 def test_zuul_url_return(self):
2558 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08002559 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Arx Cruzb1b010d2013-10-28 19:49:59 -02002560 self.worker.hold_jobs_in_build = True
2561
2562 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2563 A.addApproval('CRVW', 2)
2564 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2565 self.waitUntilSettled()
2566
2567 self.assertEqual(len(self.builds), 1)
2568 for build in self.builds:
2569 self.assertTrue('ZUUL_URL' in build.parameters)
2570
2571 self.worker.hold_jobs_in_build = False
2572 self.worker.release()
2573 self.waitUntilSettled()
2574
James E. Blair2fa50962013-01-30 21:50:41 -08002575 def test_new_patchset_dequeues_old_on_head(self):
2576 "Test that a new patchset causes the old to be dequeued (at head)"
2577 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002578 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002579 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2580 M.setMerged()
2581 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2582 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2583 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2584 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2585 A.addApproval('CRVW', 2)
2586 B.addApproval('CRVW', 2)
2587 C.addApproval('CRVW', 2)
2588 D.addApproval('CRVW', 2)
2589
2590 C.setDependsOn(B, 1)
2591 B.setDependsOn(A, 1)
2592 A.setDependsOn(M, 1)
2593
2594 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2595 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2596 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2597 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2598 self.waitUntilSettled()
2599
2600 A.addPatchset()
2601 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2602 self.waitUntilSettled()
2603
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002604 self.worker.hold_jobs_in_build = False
2605 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002606 self.waitUntilSettled()
2607
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002608 self.assertEqual(A.data['status'], 'NEW')
2609 self.assertEqual(A.reported, 2)
2610 self.assertEqual(B.data['status'], 'NEW')
2611 self.assertEqual(B.reported, 2)
2612 self.assertEqual(C.data['status'], 'NEW')
2613 self.assertEqual(C.reported, 2)
2614 self.assertEqual(D.data['status'], 'MERGED')
2615 self.assertEqual(D.reported, 2)
2616 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002617
2618 def test_new_patchset_dequeues_old_without_dependents(self):
2619 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002620 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002621 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2622 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2623 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2624 A.addApproval('CRVW', 2)
2625 B.addApproval('CRVW', 2)
2626 C.addApproval('CRVW', 2)
2627
2628 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2629 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2630 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2631 self.waitUntilSettled()
2632
2633 B.addPatchset()
2634 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2635 self.waitUntilSettled()
2636
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002637 self.worker.hold_jobs_in_build = False
2638 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002639 self.waitUntilSettled()
2640
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002641 self.assertEqual(A.data['status'], 'MERGED')
2642 self.assertEqual(A.reported, 2)
2643 self.assertEqual(B.data['status'], 'NEW')
2644 self.assertEqual(B.reported, 2)
2645 self.assertEqual(C.data['status'], 'MERGED')
2646 self.assertEqual(C.reported, 2)
2647 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002648
2649 def test_new_patchset_dequeues_old_independent_queue(self):
2650 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002651 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002652 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2653 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2654 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2655 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2656 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2657 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2658 self.waitUntilSettled()
2659
2660 B.addPatchset()
2661 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2662 self.waitUntilSettled()
2663
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002664 self.worker.hold_jobs_in_build = False
2665 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002666 self.waitUntilSettled()
2667
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002668 self.assertEqual(A.data['status'], 'NEW')
2669 self.assertEqual(A.reported, 1)
2670 self.assertEqual(B.data['status'], 'NEW')
2671 self.assertEqual(B.reported, 1)
2672 self.assertEqual(C.data['status'], 'NEW')
2673 self.assertEqual(C.reported, 1)
2674 self.assertEqual(len(self.history), 10)
2675 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002676
James E. Blair18c64442014-03-18 10:14:45 -07002677 def test_noop_job(self):
2678 "Test that the internal noop job works"
2679 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
2680 A.addApproval('CRVW', 2)
2681 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2682 self.waitUntilSettled()
2683
2684 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2685 self.assertTrue(self.sched._areAllBuildsComplete())
2686 self.assertEqual(len(self.history), 0)
2687 self.assertEqual(A.data['status'], 'MERGED')
2688 self.assertEqual(A.reported, 2)
2689
James E. Blair7d0dedc2013-02-21 17:26:09 -08002690 def test_zuul_refs(self):
2691 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002692 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002693 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2694 M1.setMerged()
2695 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2696 M2.setMerged()
2697
2698 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2699 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2700 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2701 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2702 A.addApproval('CRVW', 2)
2703 B.addApproval('CRVW', 2)
2704 C.addApproval('CRVW', 2)
2705 D.addApproval('CRVW', 2)
2706 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2707 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2708 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2709 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2710
2711 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002712 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002713 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002714 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002715 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002716 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002717 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002718 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002719 self.waitUntilSettled()
2720
James E. Blair7d0dedc2013-02-21 17:26:09 -08002721 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002722 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002723 if x.parameters['ZUUL_CHANGE'] == '3':
2724 a_zref = x.parameters['ZUUL_REF']
2725 if x.parameters['ZUUL_CHANGE'] == '4':
2726 b_zref = x.parameters['ZUUL_REF']
2727 if x.parameters['ZUUL_CHANGE'] == '5':
2728 c_zref = x.parameters['ZUUL_REF']
2729 if x.parameters['ZUUL_CHANGE'] == '6':
2730 d_zref = x.parameters['ZUUL_REF']
2731
2732 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002733 self.assertIsNotNone(a_zref)
2734 self.assertIsNotNone(b_zref)
2735 self.assertIsNotNone(c_zref)
2736 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002737
2738 # And they should all be different
2739 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002740 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002741
2742 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002743 self.assertTrue(self.ref_has_change(a_zref, A))
2744 self.assertFalse(self.ref_has_change(a_zref, B))
2745 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002746
2747 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002748 self.assertTrue(self.ref_has_change(b_zref, A))
2749 self.assertTrue(self.ref_has_change(b_zref, B))
2750 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002751
2752 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002753 self.assertTrue(self.ref_has_change(c_zref, A))
2754 self.assertTrue(self.ref_has_change(c_zref, B))
2755 self.assertTrue(self.ref_has_change(c_zref, C))
2756 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002757
2758 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002759 self.assertTrue(self.ref_has_change(d_zref, A))
2760 self.assertTrue(self.ref_has_change(d_zref, B))
2761 self.assertTrue(self.ref_has_change(d_zref, C))
2762 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002763
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002764 self.worker.hold_jobs_in_build = False
2765 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002766 self.waitUntilSettled()
2767
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002768 self.assertEqual(A.data['status'], 'MERGED')
2769 self.assertEqual(A.reported, 2)
2770 self.assertEqual(B.data['status'], 'MERGED')
2771 self.assertEqual(B.reported, 2)
2772 self.assertEqual(C.data['status'], 'MERGED')
2773 self.assertEqual(C.reported, 2)
2774 self.assertEqual(D.data['status'], 'MERGED')
2775 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002776
James E. Blairc053d022014-01-22 14:57:33 -08002777 def test_required_approval_check_and_gate(self):
2778 "Test required-approval triggers both check and gate"
2779 self.config.set('zuul', 'layout_config',
2780 'tests/fixtures/layout-require-approval.yaml')
2781 self.sched.reconfigure(self.config)
2782 self.registerJobs()
2783
2784 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2785 A.addApproval('CRVW', 2)
2786 # Add a too-old +1
2787 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2788
2789 aprv = A.addApproval('APRV', 1)
2790 self.fake_gerrit.addEvent(aprv)
2791 self.waitUntilSettled()
2792 # Should have run a check job
2793 self.assertEqual(len(self.history), 1)
2794 self.assertEqual(self.history[0].name, 'project-check')
2795
2796 # Report the result of that check job (overrides previous vrfy)
2797 # Skynet alert: this should trigger a gate job now that
2798 # all reqs are met
2799 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2800 self.waitUntilSettled()
2801 self.assertEqual(len(self.history), 2)
2802 self.assertEqual(self.history[1].name, 'project-gate')
2803
2804 def test_required_approval_newer(self):
2805 "Test required-approval newer trigger parameter"
2806 self.config.set('zuul', 'layout_config',
2807 'tests/fixtures/layout-require-approval.yaml')
2808 self.sched.reconfigure(self.config)
2809 self.registerJobs()
2810
2811 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2812 A.addApproval('CRVW', 2)
2813 aprv = A.addApproval('APRV', 1)
2814 self.fake_gerrit.addEvent(aprv)
2815 self.waitUntilSettled()
2816 # No +1 from Jenkins so should not be enqueued
2817 self.assertEqual(len(self.history), 0)
2818
2819 # Add a too-old +1, should trigger check but not gate
2820 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2821 self.fake_gerrit.addEvent(aprv)
2822 self.waitUntilSettled()
2823 self.assertEqual(len(self.history), 1)
2824 self.assertEqual(self.history[0].name, 'project-check')
2825
2826 # Add a recent +1
2827 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2828 self.fake_gerrit.addEvent(aprv)
2829 self.waitUntilSettled()
2830 self.assertEqual(len(self.history), 2)
2831 self.assertEqual(self.history[1].name, 'project-gate')
2832
2833 def test_required_approval_older(self):
2834 "Test required-approval older trigger parameter"
2835 self.config.set('zuul', 'layout_config',
2836 'tests/fixtures/layout-require-approval.yaml')
2837 self.sched.reconfigure(self.config)
2838 self.registerJobs()
2839
2840 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2841 crvw = A.addApproval('CRVW', 2)
2842 self.fake_gerrit.addEvent(crvw)
2843 self.waitUntilSettled()
2844 # No +1 from Jenkins so should not be enqueued
2845 self.assertEqual(len(self.history), 0)
2846
2847 # Add an old +1 and trigger check with a comment
2848 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2849 self.fake_gerrit.addEvent(crvw)
2850 self.waitUntilSettled()
2851 self.assertEqual(len(self.history), 1)
2852 self.assertEqual(self.history[0].name, 'project-check')
2853
2854 # Add a recent +1 and make sure nothing changes
2855 A.addApproval('VRFY', 1)
2856 self.fake_gerrit.addEvent(crvw)
2857 self.waitUntilSettled()
2858 self.assertEqual(len(self.history), 1)
2859
2860 # The last thing we did was query a change then do nothing
2861 # with a pipeline, so it will be in the cache; clean it up so
2862 # it does not fail the test.
2863 for pipeline in self.sched.layout.pipelines.values():
2864 pipeline.trigger.maintainCache([])
2865
James E. Blair4a28a882013-08-23 15:17:33 -07002866 def test_rerun_on_error(self):
2867 "Test that if a worker fails to run a job, it is run again"
2868 self.worker.hold_jobs_in_build = True
2869 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2870 A.addApproval('CRVW', 2)
2871 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2872 self.waitUntilSettled()
2873
2874 self.builds[0].run_error = True
2875 self.worker.hold_jobs_in_build = False
2876 self.worker.release()
2877 self.waitUntilSettled()
2878 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2879 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2880
James E. Blair412e5582013-04-22 15:50:12 -07002881 def test_statsd(self):
2882 "Test each of the statsd methods used in the scheduler"
2883 import extras
2884 statsd = extras.try_import('statsd.statsd')
2885 statsd.incr('test-incr')
2886 statsd.timing('test-timing', 3)
2887 statsd.gauge('test-guage', 12)
2888 self.assertReportedStat('test-incr', '1|c')
2889 self.assertReportedStat('test-timing', '3|ms')
2890 self.assertReportedStat('test-guage', '12|g')
2891
James E. Blairdad52252014-02-07 16:59:17 -08002892 def test_stuck_job_cleanup(self):
2893 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07002894 # This job won't be registered at startup because it is not in
2895 # the standard layout, but we need it to already be registerd
2896 # for when we reconfigure, as that is when Zuul will attempt
2897 # to run the new job.
2898 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002899 self.gearman_server.hold_jobs_in_queue = True
2900 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2901 A.addApproval('CRVW', 2)
2902 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2903 self.waitUntilSettled()
2904 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2905
2906 self.config.set('zuul', 'layout_config',
2907 'tests/fixtures/layout-no-jobs.yaml')
2908 self.sched.reconfigure(self.config)
2909 self.waitUntilSettled()
2910
James E. Blair18c64442014-03-18 10:14:45 -07002911 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002912 self.waitUntilSettled()
2913 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2914 self.assertTrue(self.sched._areAllBuildsComplete())
2915
2916 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002917 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002918 self.assertEqual(self.history[0].result, 'SUCCESS')
2919
James E. Blair70c71582013-03-06 08:50:50 -08002920 def test_file_jobs(self):
2921 "Test that file jobs run only when appropriate"
2922 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2923 A.addPatchset(['pip-requires'])
2924 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2925 A.addApproval('CRVW', 2)
2926 B.addApproval('CRVW', 2)
2927 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2928 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2929 self.waitUntilSettled()
2930
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002931 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002932 if x.name == 'project-testfile']
2933
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002934 self.assertEqual(len(testfile_jobs), 1)
2935 self.assertEqual(testfile_jobs[0].changes, '1,2')
2936 self.assertEqual(A.data['status'], 'MERGED')
2937 self.assertEqual(A.reported, 2)
2938 self.assertEqual(B.data['status'], 'MERGED')
2939 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002940
2941 def test_test_config(self):
2942 "Test that we can test the config"
2943 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002944 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002945 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002946 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002947
2948 def test_build_description(self):
2949 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002950 self.worker.registerFunction('set_description:' +
2951 self.worker.worker_id)
2952
2953 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2954 A.addApproval('CRVW', 2)
2955 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2956 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002957 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002958 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002959 self.assertTrue(re.search("Branch.*master", desc))
2960 self.assertTrue(re.search("Pipeline.*gate", desc))
2961 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2962 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2963 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2964 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002965
James E. Blair64ed6f22013-07-10 14:07:23 -07002966 def test_queue_precedence(self):
2967 "Test that queue precedence works"
2968
2969 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002970 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002971 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2972 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2973 A.addApproval('CRVW', 2)
2974 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2975
2976 self.waitUntilSettled()
2977 self.gearman_server.hold_jobs_in_queue = False
2978 self.gearman_server.release()
2979 self.waitUntilSettled()
2980
James E. Blair8de58bd2013-07-18 16:23:33 -07002981 # Run one build at a time to ensure non-race order:
2982 for x in range(6):
2983 self.release(self.builds[0])
2984 self.waitUntilSettled()
2985 self.worker.hold_jobs_in_build = False
2986 self.waitUntilSettled()
2987
James E. Blair64ed6f22013-07-10 14:07:23 -07002988 self.log.debug(self.history)
2989 self.assertEqual(self.history[0].pipeline, 'gate')
2990 self.assertEqual(self.history[1].pipeline, 'check')
2991 self.assertEqual(self.history[2].pipeline, 'gate')
2992 self.assertEqual(self.history[3].pipeline, 'gate')
2993 self.assertEqual(self.history[4].pipeline, 'check')
2994 self.assertEqual(self.history[5].pipeline, 'check')
2995
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04002996 def test_json_status(self, compressed=False):
James E. Blair1843a552013-07-03 14:19:52 -07002997 "Test that we can retrieve JSON status info"
2998 self.worker.hold_jobs_in_build = True
2999 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3000 A.addApproval('CRVW', 2)
3001 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3002 self.waitUntilSettled()
3003
3004 port = self.webapp.server.socket.getsockname()[1]
3005
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04003006 req = urllib2.Request("http://localhost:%s/status.json" % port)
3007 if compressed:
3008 req.add_header("accept-encoding", "gzip")
3009 f = urllib2.urlopen(req)
James E. Blair1843a552013-07-03 14:19:52 -07003010 data = f.read()
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04003011 if compressed:
3012 gz = gzip.GzipFile(fileobj=StringIO(data))
3013 data = gz.read()
James E. Blair1843a552013-07-03 14:19:52 -07003014
3015 self.worker.hold_jobs_in_build = False
3016 self.worker.release()
3017 self.waitUntilSettled()
3018
3019 data = json.loads(data)
3020 status_jobs = set()
3021 for p in data['pipelines']:
3022 for q in p['change_queues']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08003023 if q['dependent']:
3024 self.assertEqual(q['window'], 20)
3025 else:
3026 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07003027 for head in q['heads']:
3028 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08003029 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003030 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07003031 for job in change['jobs']:
3032 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003033 self.assertIn('project-merge', status_jobs)
3034 self.assertIn('project-test1', status_jobs)
3035 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07003036
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04003037 def test_json_status_gzip(self):
3038 self.test_json_status(True)
3039
James E. Blairc3d428e2013-12-03 15:06:48 -08003040 def test_merging_queues(self):
3041 "Test that transitively-connected change queues are merged"
3042 self.config.set('zuul', 'layout_config',
3043 'tests/fixtures/layout-merge-queues.yaml')
3044 self.sched.reconfigure(self.config)
3045 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
3046
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003047 def test_node_label(self):
3048 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003049 self.worker.registerFunction('build:node-project-test1:debian')
3050
3051 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
3052 A.addApproval('CRVW', 2)
3053 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3054 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07003055
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003056 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3057 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3058 'debian')
3059 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07003060
3061 def test_live_reconfiguration(self):
3062 "Test that live reconfiguration works"
3063 self.worker.hold_jobs_in_build = True
3064 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3065 A.addApproval('CRVW', 2)
3066 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3067 self.waitUntilSettled()
3068
3069 self.sched.reconfigure(self.config)
3070
3071 self.worker.hold_jobs_in_build = False
3072 self.worker.release()
3073 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003074 self.assertEqual(self.getJobFromHistory('project-merge').result,
3075 'SUCCESS')
3076 self.assertEqual(self.getJobFromHistory('project-test1').result,
3077 'SUCCESS')
3078 self.assertEqual(self.getJobFromHistory('project-test2').result,
3079 'SUCCESS')
3080 self.assertEqual(A.data['status'], 'MERGED')
3081 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07003082
James E. Blaire712d9f2013-07-31 11:40:11 -07003083 def test_live_reconfiguration_functions(self):
3084 "Test live reconfiguration with a custom function"
3085 self.worker.registerFunction('build:node-project-test1:debian')
3086 self.worker.registerFunction('build:node-project-test1:wheezy')
3087 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
3088 A.addApproval('CRVW', 2)
3089 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3090 self.waitUntilSettled()
3091
3092 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3093 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3094 'debian')
3095 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
3096
3097 self.config.set('zuul', 'layout_config',
3098 'tests/fixtures/layout-live-'
3099 'reconfiguration-functions.yaml')
3100 self.sched.reconfigure(self.config)
3101 self.worker.build_history = []
3102
3103 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
3104 B.addApproval('CRVW', 2)
3105 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3106 self.waitUntilSettled()
3107
3108 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3109 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3110 'wheezy')
3111 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
3112
James E. Blair287c06d2013-07-24 10:39:30 -07003113 def test_delayed_repo_init(self):
3114 self.config.set('zuul', 'layout_config',
3115 'tests/fixtures/layout-delayed-repo-init.yaml')
3116 self.sched.reconfigure(self.config)
3117
3118 self.init_repo("org/new-project")
3119 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
3120
3121 A.addApproval('CRVW', 2)
3122 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3123 self.waitUntilSettled()
3124 self.assertEqual(self.getJobFromHistory('project-merge').result,
3125 'SUCCESS')
3126 self.assertEqual(self.getJobFromHistory('project-test1').result,
3127 'SUCCESS')
3128 self.assertEqual(self.getJobFromHistory('project-test2').result,
3129 'SUCCESS')
3130 self.assertEqual(A.data['status'], 'MERGED')
3131 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003132
Clark Boylan6dbbc482013-10-18 10:57:31 -07003133 def test_repo_deleted(self):
3134 self.config.set('zuul', 'layout_config',
3135 'tests/fixtures/layout-repo-deleted.yaml')
3136 self.sched.reconfigure(self.config)
3137
3138 self.init_repo("org/delete-project")
3139 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
3140
3141 A.addApproval('CRVW', 2)
3142 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3143 self.waitUntilSettled()
3144 self.assertEqual(self.getJobFromHistory('project-merge').result,
3145 'SUCCESS')
3146 self.assertEqual(self.getJobFromHistory('project-test1').result,
3147 'SUCCESS')
3148 self.assertEqual(self.getJobFromHistory('project-test2').result,
3149 'SUCCESS')
3150 self.assertEqual(A.data['status'], 'MERGED')
3151 self.assertEqual(A.reported, 2)
3152
3153 # Delete org/new-project zuul repo. Should be recloned.
3154 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
3155
3156 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
3157
3158 B.addApproval('CRVW', 2)
3159 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3160 self.waitUntilSettled()
3161 self.assertEqual(self.getJobFromHistory('project-merge').result,
3162 'SUCCESS')
3163 self.assertEqual(self.getJobFromHistory('project-test1').result,
3164 'SUCCESS')
3165 self.assertEqual(self.getJobFromHistory('project-test2').result,
3166 'SUCCESS')
3167 self.assertEqual(B.data['status'], 'MERGED')
3168 self.assertEqual(B.reported, 2)
3169
James E. Blair63bb0ef2013-07-29 17:14:51 -07003170 def test_timer(self):
3171 "Test that a periodic job is triggered"
3172 self.worker.hold_jobs_in_build = True
3173 self.config.set('zuul', 'layout_config',
3174 'tests/fixtures/layout-timer.yaml')
3175 self.sched.reconfigure(self.config)
3176 self.registerJobs()
3177
Clark Boylan3ee090a2014-04-03 20:55:09 -07003178 # The pipeline triggers every second, so we should have seen
3179 # several by now.
3180 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003181 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07003182
3183 self.assertEqual(len(self.builds), 2)
3184
James E. Blair63bb0ef2013-07-29 17:14:51 -07003185 port = self.webapp.server.socket.getsockname()[1]
3186
3187 f = urllib.urlopen("http://localhost:%s/status.json" % port)
3188 data = f.read()
3189
3190 self.worker.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07003191 # Stop queuing timer triggered jobs so that the assertions
3192 # below don't race against more jobs being queued.
3193 self.config.set('zuul', 'layout_config',
3194 'tests/fixtures/layout-no-timer.yaml')
3195 self.sched.reconfigure(self.config)
3196 self.registerJobs()
James E. Blair63bb0ef2013-07-29 17:14:51 -07003197 self.worker.release()
3198 self.waitUntilSettled()
3199
3200 self.assertEqual(self.getJobFromHistory(
3201 'project-bitrot-stable-old').result, 'SUCCESS')
3202 self.assertEqual(self.getJobFromHistory(
3203 'project-bitrot-stable-older').result, 'SUCCESS')
3204
3205 data = json.loads(data)
3206 status_jobs = set()
3207 for p in data['pipelines']:
3208 for q in p['change_queues']:
3209 for head in q['heads']:
3210 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07003211 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003212 for job in change['jobs']:
3213 status_jobs.add(job['name'])
3214 self.assertIn('project-bitrot-stable-old', status_jobs)
3215 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003216
James E. Blair4f6033c2014-03-27 15:49:09 -07003217 def test_idle(self):
3218 "Test that frequent periodic jobs work"
3219 self.worker.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07003220
Clark Boylan3ee090a2014-04-03 20:55:09 -07003221 for x in range(1, 3):
3222 # Test that timer triggers periodic jobs even across
3223 # layout config reloads.
3224 # Start timer trigger
3225 self.config.set('zuul', 'layout_config',
3226 'tests/fixtures/layout-idle.yaml')
3227 self.sched.reconfigure(self.config)
3228 self.registerJobs()
James E. Blair4f6033c2014-03-27 15:49:09 -07003229
Clark Boylan3ee090a2014-04-03 20:55:09 -07003230 # The pipeline triggers every second, so we should have seen
3231 # several by now.
3232 time.sleep(5)
3233 self.waitUntilSettled()
3234
3235 # Stop queuing timer triggered jobs so that the assertions
3236 # below don't race against more jobs being queued.
3237 self.config.set('zuul', 'layout_config',
3238 'tests/fixtures/layout-no-timer.yaml')
3239 self.sched.reconfigure(self.config)
3240 self.registerJobs()
3241
3242 self.assertEqual(len(self.builds), 2)
3243 self.worker.release('.*')
3244 self.waitUntilSettled()
3245 self.assertEqual(len(self.builds), 0)
3246 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07003247
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003248 def test_check_smtp_pool(self):
3249 self.config.set('zuul', 'layout_config',
3250 'tests/fixtures/layout-smtp.yaml')
3251 self.sched.reconfigure(self.config)
3252
3253 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3254 self.waitUntilSettled()
3255
3256 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3257 self.waitUntilSettled()
3258
James E. Blairff80a2f2013-12-27 13:24:06 -08003259 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003260
3261 # A.messages only holds what FakeGerrit places in it. Thus we
3262 # work on the knowledge of what the first message should be as
3263 # it is only configured to go to SMTP.
3264
3265 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003266 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003267 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003268 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003269 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003270 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003271
3272 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003273 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003274 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003275 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003276 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003277 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003278
James E. Blaire5910202013-12-27 09:50:31 -08003279 def test_timer_smtp(self):
3280 "Test that a periodic job is triggered"
Clark Boylan3ee090a2014-04-03 20:55:09 -07003281 self.worker.hold_jobs_in_build = True
James E. Blaire5910202013-12-27 09:50:31 -08003282 self.config.set('zuul', 'layout_config',
3283 'tests/fixtures/layout-timer-smtp.yaml')
3284 self.sched.reconfigure(self.config)
3285 self.registerJobs()
3286
Clark Boylan3ee090a2014-04-03 20:55:09 -07003287 # The pipeline triggers every second, so we should have seen
3288 # several by now.
3289 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003290 self.waitUntilSettled()
3291
Clark Boylan3ee090a2014-04-03 20:55:09 -07003292 self.assertEqual(len(self.builds), 2)
3293 self.worker.release('.*')
3294 self.waitUntilSettled()
3295 self.assertEqual(len(self.history), 2)
3296
James E. Blaire5910202013-12-27 09:50:31 -08003297 self.assertEqual(self.getJobFromHistory(
3298 'project-bitrot-stable-old').result, 'SUCCESS')
3299 self.assertEqual(self.getJobFromHistory(
3300 'project-bitrot-stable-older').result, 'SUCCESS')
3301
James E. Blairff80a2f2013-12-27 13:24:06 -08003302 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003303
3304 # A.messages only holds what FakeGerrit places in it. Thus we
3305 # work on the knowledge of what the first message should be as
3306 # it is only configured to go to SMTP.
3307
3308 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003309 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003310 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003311 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003312 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003313 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003314
Clark Boylan3ee090a2014-04-03 20:55:09 -07003315 # Stop queuing timer triggered jobs and let any that may have
3316 # queued through so that end of test assertions pass.
3317 self.config.set('zuul', 'layout_config',
3318 'tests/fixtures/layout-no-timer.yaml')
3319 self.sched.reconfigure(self.config)
3320 self.registerJobs()
3321 self.worker.release('.*')
3322 self.waitUntilSettled()
3323
James E. Blairad28e912013-11-27 10:43:22 -08003324 def test_client_enqueue(self):
3325 "Test that the RPC client can enqueue a change"
3326 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3327 A.addApproval('CRVW', 2)
3328 A.addApproval('APRV', 1)
3329
3330 client = zuul.rpcclient.RPCClient('127.0.0.1',
3331 self.gearman_server.port)
3332 r = client.enqueue(pipeline='gate',
3333 project='org/project',
3334 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003335 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003336 self.waitUntilSettled()
3337 self.assertEqual(self.getJobFromHistory('project-merge').result,
3338 'SUCCESS')
3339 self.assertEqual(self.getJobFromHistory('project-test1').result,
3340 'SUCCESS')
3341 self.assertEqual(self.getJobFromHistory('project-test2').result,
3342 'SUCCESS')
3343 self.assertEqual(A.data['status'], 'MERGED')
3344 self.assertEqual(A.reported, 2)
3345 self.assertEqual(r, True)
3346
3347 def test_client_enqueue_negative(self):
3348 "Test that the RPC client returns errors"
3349 client = zuul.rpcclient.RPCClient('127.0.0.1',
3350 self.gearman_server.port)
3351 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3352 "Invalid project"):
3353 r = client.enqueue(pipeline='gate',
3354 project='project-does-not-exist',
3355 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003356 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003357 client.shutdown()
3358 self.assertEqual(r, False)
3359
3360 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3361 "Invalid pipeline"):
3362 r = client.enqueue(pipeline='pipeline-does-not-exist',
3363 project='org/project',
3364 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003365 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003366 client.shutdown()
3367 self.assertEqual(r, False)
3368
3369 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3370 "Invalid trigger"):
3371 r = client.enqueue(pipeline='gate',
3372 project='org/project',
3373 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003374 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003375 client.shutdown()
3376 self.assertEqual(r, False)
3377
3378 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3379 "Invalid change"):
3380 r = client.enqueue(pipeline='gate',
3381 project='org/project',
3382 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003383 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003384 client.shutdown()
3385 self.assertEqual(r, False)
3386
3387 self.waitUntilSettled()
3388 self.assertEqual(len(self.history), 0)
3389 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003390
3391 def test_client_promote(self):
3392 "Test that the RPC client can promote a change"
3393 self.worker.hold_jobs_in_build = True
3394 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3395 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3396 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3397 A.addApproval('CRVW', 2)
3398 B.addApproval('CRVW', 2)
3399 C.addApproval('CRVW', 2)
3400
3401 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3402 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3403 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3404
3405 self.waitUntilSettled()
3406
Sean Daguef39b9ca2014-01-10 21:34:35 -05003407 items = self.sched.layout.pipelines['gate'].getAllItems()
3408 enqueue_times = {}
3409 for item in items:
3410 enqueue_times[str(item.change)] = item.enqueue_time
3411
James E. Blair36658cf2013-12-06 17:53:48 -08003412 client = zuul.rpcclient.RPCClient('127.0.0.1',
3413 self.gearman_server.port)
3414 r = client.promote(pipeline='gate',
3415 change_ids=['2,1', '3,1'])
3416
Sean Daguef39b9ca2014-01-10 21:34:35 -05003417 # ensure that enqueue times are durable
3418 items = self.sched.layout.pipelines['gate'].getAllItems()
3419 for item in items:
3420 self.assertEqual(
3421 enqueue_times[str(item.change)], item.enqueue_time)
3422
James E. Blair78acec92014-02-06 07:11:32 -08003423 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003424 self.worker.release('.*-merge')
3425 self.waitUntilSettled()
3426 self.worker.release('.*-merge')
3427 self.waitUntilSettled()
3428 self.worker.release('.*-merge')
3429 self.waitUntilSettled()
3430
3431 self.assertEqual(len(self.builds), 6)
3432 self.assertEqual(self.builds[0].name, 'project-test1')
3433 self.assertEqual(self.builds[1].name, 'project-test2')
3434 self.assertEqual(self.builds[2].name, 'project-test1')
3435 self.assertEqual(self.builds[3].name, 'project-test2')
3436 self.assertEqual(self.builds[4].name, 'project-test1')
3437 self.assertEqual(self.builds[5].name, 'project-test2')
3438
3439 self.assertTrue(self.job_has_changes(self.builds[0], B))
3440 self.assertFalse(self.job_has_changes(self.builds[0], A))
3441 self.assertFalse(self.job_has_changes(self.builds[0], C))
3442
3443 self.assertTrue(self.job_has_changes(self.builds[2], B))
3444 self.assertTrue(self.job_has_changes(self.builds[2], C))
3445 self.assertFalse(self.job_has_changes(self.builds[2], A))
3446
3447 self.assertTrue(self.job_has_changes(self.builds[4], B))
3448 self.assertTrue(self.job_has_changes(self.builds[4], C))
3449 self.assertTrue(self.job_has_changes(self.builds[4], A))
3450
3451 self.worker.release()
3452 self.waitUntilSettled()
3453
3454 self.assertEqual(A.data['status'], 'MERGED')
3455 self.assertEqual(A.reported, 2)
3456 self.assertEqual(B.data['status'], 'MERGED')
3457 self.assertEqual(B.reported, 2)
3458 self.assertEqual(C.data['status'], 'MERGED')
3459 self.assertEqual(C.reported, 2)
3460
3461 client.shutdown()
3462 self.assertEqual(r, True)
3463
3464 def test_client_promote_dependent(self):
3465 "Test that the RPC client can promote a dependent change"
3466 # C (depends on B) -> B -> A ; then promote C to get:
3467 # A -> C (depends on B) -> B
3468 self.worker.hold_jobs_in_build = True
3469 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3470 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3471 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3472
3473 C.setDependsOn(B, 1)
3474
3475 A.addApproval('CRVW', 2)
3476 B.addApproval('CRVW', 2)
3477 C.addApproval('CRVW', 2)
3478
3479 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3480 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3481 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3482
3483 self.waitUntilSettled()
3484
3485 client = zuul.rpcclient.RPCClient('127.0.0.1',
3486 self.gearman_server.port)
3487 r = client.promote(pipeline='gate',
3488 change_ids=['3,1'])
3489
James E. Blair78acec92014-02-06 07:11:32 -08003490 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003491 self.worker.release('.*-merge')
3492 self.waitUntilSettled()
3493 self.worker.release('.*-merge')
3494 self.waitUntilSettled()
3495 self.worker.release('.*-merge')
3496 self.waitUntilSettled()
3497
3498 self.assertEqual(len(self.builds), 6)
3499 self.assertEqual(self.builds[0].name, 'project-test1')
3500 self.assertEqual(self.builds[1].name, 'project-test2')
3501 self.assertEqual(self.builds[2].name, 'project-test1')
3502 self.assertEqual(self.builds[3].name, 'project-test2')
3503 self.assertEqual(self.builds[4].name, 'project-test1')
3504 self.assertEqual(self.builds[5].name, 'project-test2')
3505
3506 self.assertTrue(self.job_has_changes(self.builds[0], B))
3507 self.assertFalse(self.job_has_changes(self.builds[0], A))
3508 self.assertFalse(self.job_has_changes(self.builds[0], C))
3509
3510 self.assertTrue(self.job_has_changes(self.builds[2], B))
3511 self.assertTrue(self.job_has_changes(self.builds[2], C))
3512 self.assertFalse(self.job_has_changes(self.builds[2], A))
3513
3514 self.assertTrue(self.job_has_changes(self.builds[4], B))
3515 self.assertTrue(self.job_has_changes(self.builds[4], C))
3516 self.assertTrue(self.job_has_changes(self.builds[4], A))
3517
3518 self.worker.release()
3519 self.waitUntilSettled()
3520
3521 self.assertEqual(A.data['status'], 'MERGED')
3522 self.assertEqual(A.reported, 2)
3523 self.assertEqual(B.data['status'], 'MERGED')
3524 self.assertEqual(B.reported, 2)
3525 self.assertEqual(C.data['status'], 'MERGED')
3526 self.assertEqual(C.reported, 2)
3527
3528 client.shutdown()
3529 self.assertEqual(r, True)
3530
3531 def test_client_promote_negative(self):
3532 "Test that the RPC client returns errors for promotion"
3533 self.worker.hold_jobs_in_build = True
3534 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3535 A.addApproval('CRVW', 2)
3536 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3537 self.waitUntilSettled()
3538
3539 client = zuul.rpcclient.RPCClient('127.0.0.1',
3540 self.gearman_server.port)
3541
3542 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3543 r = client.promote(pipeline='nonexistent',
3544 change_ids=['2,1', '3,1'])
3545 client.shutdown()
3546 self.assertEqual(r, False)
3547
3548 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3549 r = client.promote(pipeline='gate',
3550 change_ids=['4,1'])
3551 client.shutdown()
3552 self.assertEqual(r, False)
3553
3554 self.worker.hold_jobs_in_build = False
3555 self.worker.release()
3556 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003557
3558 def test_queue_rate_limiting(self):
3559 "Test that DependentPipelines are rate limited with dep across window"
3560 self.config.set('zuul', 'layout_config',
3561 'tests/fixtures/layout-rate-limit.yaml')
3562 self.sched.reconfigure(self.config)
3563 self.worker.hold_jobs_in_build = True
3564 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3565 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3566 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3567
3568 C.setDependsOn(B, 1)
3569 self.worker.addFailTest('project-test1', A)
3570
3571 A.addApproval('CRVW', 2)
3572 B.addApproval('CRVW', 2)
3573 C.addApproval('CRVW', 2)
3574
3575 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3576 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3577 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3578 self.waitUntilSettled()
3579
3580 # Only A and B will have their merge jobs queued because
3581 # window is 2.
3582 self.assertEqual(len(self.builds), 2)
3583 self.assertEqual(self.builds[0].name, 'project-merge')
3584 self.assertEqual(self.builds[1].name, 'project-merge')
3585
3586 self.worker.release('.*-merge')
3587 self.waitUntilSettled()
3588 self.worker.release('.*-merge')
3589 self.waitUntilSettled()
3590
3591 # Only A and B will have their test jobs queued because
3592 # window is 2.
3593 self.assertEqual(len(self.builds), 4)
3594 self.assertEqual(self.builds[0].name, 'project-test1')
3595 self.assertEqual(self.builds[1].name, 'project-test2')
3596 self.assertEqual(self.builds[2].name, 'project-test1')
3597 self.assertEqual(self.builds[3].name, 'project-test2')
3598
3599 self.worker.release('project-.*')
3600 self.waitUntilSettled()
3601
3602 queue = self.sched.layout.pipelines['gate'].queues[0]
3603 # A failed so window is reduced by 1 to 1.
3604 self.assertEqual(queue.window, 1)
3605 self.assertEqual(queue.window_floor, 1)
3606 self.assertEqual(A.data['status'], 'NEW')
3607
3608 # Gate is reset and only B's merge job is queued because
3609 # window shrunk to 1.
3610 self.assertEqual(len(self.builds), 1)
3611 self.assertEqual(self.builds[0].name, 'project-merge')
3612
3613 self.worker.release('.*-merge')
3614 self.waitUntilSettled()
3615
3616 # Only B's test jobs are queued because window is still 1.
3617 self.assertEqual(len(self.builds), 2)
3618 self.assertEqual(self.builds[0].name, 'project-test1')
3619 self.assertEqual(self.builds[1].name, 'project-test2')
3620
3621 self.worker.release('project-.*')
3622 self.waitUntilSettled()
3623
3624 # B was successfully merged so window is increased to 2.
3625 self.assertEqual(queue.window, 2)
3626 self.assertEqual(queue.window_floor, 1)
3627 self.assertEqual(B.data['status'], 'MERGED')
3628
3629 # Only C is left and its merge job is queued.
3630 self.assertEqual(len(self.builds), 1)
3631 self.assertEqual(self.builds[0].name, 'project-merge')
3632
3633 self.worker.release('.*-merge')
3634 self.waitUntilSettled()
3635
3636 # After successful merge job the test jobs for C are queued.
3637 self.assertEqual(len(self.builds), 2)
3638 self.assertEqual(self.builds[0].name, 'project-test1')
3639 self.assertEqual(self.builds[1].name, 'project-test2')
3640
3641 self.worker.release('project-.*')
3642 self.waitUntilSettled()
3643
3644 # C successfully merged so window is bumped to 3.
3645 self.assertEqual(queue.window, 3)
3646 self.assertEqual(queue.window_floor, 1)
3647 self.assertEqual(C.data['status'], 'MERGED')
3648
3649 def test_queue_rate_limiting_dependent(self):
3650 "Test that DependentPipelines are rate limited with dep in window"
3651 self.config.set('zuul', 'layout_config',
3652 'tests/fixtures/layout-rate-limit.yaml')
3653 self.sched.reconfigure(self.config)
3654 self.worker.hold_jobs_in_build = True
3655 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3656 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3657 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3658
3659 B.setDependsOn(A, 1)
3660
3661 self.worker.addFailTest('project-test1', A)
3662
3663 A.addApproval('CRVW', 2)
3664 B.addApproval('CRVW', 2)
3665 C.addApproval('CRVW', 2)
3666
3667 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3668 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3669 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3670 self.waitUntilSettled()
3671
3672 # Only A and B will have their merge jobs queued because
3673 # window is 2.
3674 self.assertEqual(len(self.builds), 2)
3675 self.assertEqual(self.builds[0].name, 'project-merge')
3676 self.assertEqual(self.builds[1].name, 'project-merge')
3677
3678 self.worker.release('.*-merge')
3679 self.waitUntilSettled()
3680 self.worker.release('.*-merge')
3681 self.waitUntilSettled()
3682
3683 # Only A and B will have their test jobs queued because
3684 # window is 2.
3685 self.assertEqual(len(self.builds), 4)
3686 self.assertEqual(self.builds[0].name, 'project-test1')
3687 self.assertEqual(self.builds[1].name, 'project-test2')
3688 self.assertEqual(self.builds[2].name, 'project-test1')
3689 self.assertEqual(self.builds[3].name, 'project-test2')
3690
3691 self.worker.release('project-.*')
3692 self.waitUntilSettled()
3693
3694 queue = self.sched.layout.pipelines['gate'].queues[0]
3695 # A failed so window is reduced by 1 to 1.
3696 self.assertEqual(queue.window, 1)
3697 self.assertEqual(queue.window_floor, 1)
3698 self.assertEqual(A.data['status'], 'NEW')
3699 self.assertEqual(B.data['status'], 'NEW')
3700
3701 # Gate is reset and only C's merge job is queued because
3702 # window shrunk to 1 and A and B were dequeued.
3703 self.assertEqual(len(self.builds), 1)
3704 self.assertEqual(self.builds[0].name, 'project-merge')
3705
3706 self.worker.release('.*-merge')
3707 self.waitUntilSettled()
3708
3709 # Only C's test jobs are queued because window is still 1.
3710 self.assertEqual(len(self.builds), 2)
3711 self.assertEqual(self.builds[0].name, 'project-test1')
3712 self.assertEqual(self.builds[1].name, 'project-test2')
3713
3714 self.worker.release('project-.*')
3715 self.waitUntilSettled()
3716
3717 # C was successfully merged so window is increased to 2.
3718 self.assertEqual(queue.window, 2)
3719 self.assertEqual(queue.window_floor, 1)
3720 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003721
3722 def test_worker_update_metadata(self):
3723 "Test if a worker can send back metadata about itself"
3724 self.worker.hold_jobs_in_build = True
3725
3726 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3727 A.addApproval('CRVW', 2)
3728 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3729 self.waitUntilSettled()
3730
3731 self.assertEqual(len(self.launcher.builds), 1)
3732
3733 self.log.debug('Current builds:')
3734 self.log.debug(self.launcher.builds)
3735
3736 start = time.time()
3737 while True:
3738 if time.time() - start > 10:
3739 raise Exception("Timeout waiting for gearman server to report "
3740 + "back to the client")
3741 build = self.launcher.builds.values()[0]
3742 if build.worker.name == "My Worker":
3743 break
3744 else:
3745 time.sleep(0)
3746
3747 self.log.debug(build)
3748 self.assertEqual("My Worker", build.worker.name)
3749 self.assertEqual("localhost", build.worker.hostname)
3750 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3751 self.assertEqual("zuul.example.org", build.worker.fqdn)
3752 self.assertEqual("FakeBuilder", build.worker.program)
3753 self.assertEqual("v1.1", build.worker.version)
3754 self.assertEqual({'something': 'else'}, build.worker.extra)
3755
3756 self.worker.hold_jobs_in_build = False
3757 self.worker.release()
3758 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003759
3760 def test_footer_message(self):
3761 "Test a pipeline's footer message is correctly added to the report."
3762 self.config.set('zuul', 'layout_config',
3763 'tests/fixtures/layout-footer-message.yaml')
3764 self.sched.reconfigure(self.config)
3765 self.registerJobs()
3766
3767 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3768 A.addApproval('CRVW', 2)
3769 self.worker.addFailTest('test1', A)
3770 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3771 self.waitUntilSettled()
3772
3773 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3774 B.addApproval('CRVW', 2)
3775 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3776 self.waitUntilSettled()
3777
3778 self.assertEqual(2, len(self.smtp_messages))
3779
3780 failure_body = """\
3781Build failed. For information on how to proceed, see \
3782http://wiki.example.org/Test_Failures
3783
3784- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3785- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3786
3787For CI problems and help debugging, contact ci@example.org"""
3788
3789 success_body = """\
3790Build succeeded.
3791
3792- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3793- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3794
3795For CI problems and help debugging, contact ci@example.org"""
3796
3797 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3798 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11003799
3800 def test_merge_failure_reporters(self):
3801 """Check that the config is set up correctly"""
3802
3803 self.config.set('zuul', 'layout_config',
3804 'tests/fixtures/layout-merge-failure.yaml')
3805 self.sched.reconfigure(self.config)
3806 self.registerJobs()
3807
3808 self.assertEqual(
3809 "Merge Failed.\n\nThis change was unable to be automatically "
3810 "merged with the current state of the repository. Please rebase "
3811 "your change and upload a new patchset.",
3812 self.sched.layout.pipelines['check'].merge_failure_message)
3813 self.assertEqual(
3814 "The merge failed! For more information...",
3815 self.sched.layout.pipelines['gate'].merge_failure_message)
3816
3817 self.assertEqual(
3818 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3819 self.assertEqual(
3820 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3821
3822 self.assertTrue(isinstance(
3823 self.sched.layout.pipelines['check'].merge_failure_actions[0].
3824 reporter, zuul.reporter.gerrit.Reporter))
3825
3826 self.assertTrue(
3827 (
3828 isinstance(self.sched.layout.pipelines['gate'].
3829 merge_failure_actions[0].reporter,
3830 zuul.reporter.smtp.Reporter) and
3831 isinstance(self.sched.layout.pipelines['gate'].
3832 merge_failure_actions[1].reporter,
3833 zuul.reporter.gerrit.Reporter)
3834 ) or (
3835 isinstance(self.sched.layout.pipelines['gate'].
3836 merge_failure_actions[0].reporter,
3837 zuul.reporter.gerrit.Reporter) and
3838 isinstance(self.sched.layout.pipelines['gate'].
3839 merge_failure_actions[1].reporter,
3840 zuul.reporter.smtp.Reporter)
3841 )
3842 )
3843
3844 def test_merge_failure_reports(self):
3845 """Check that when a change fails to merge the correct message is sent
3846 to the correct reporter"""
3847 self.config.set('zuul', 'layout_config',
3848 'tests/fixtures/layout-merge-failure.yaml')
3849 self.sched.reconfigure(self.config)
3850 self.registerJobs()
3851
3852 # Check a test failure isn't reported to SMTP
3853 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3854 A.addApproval('CRVW', 2)
3855 self.worker.addFailTest('project-test1', A)
3856 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3857 self.waitUntilSettled()
3858
3859 self.assertEqual(3, len(self.history)) # 3 jobs
3860 self.assertEqual(0, len(self.smtp_messages))
3861
3862 # Check a merge failure is reported to SMTP
3863 # B should be merged, but C will conflict with B
3864 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3865 B.addPatchset(['conflict'])
3866 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3867 C.addPatchset(['conflict'])
3868 B.addApproval('CRVW', 2)
3869 C.addApproval('CRVW', 2)
3870 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3871 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3872 self.waitUntilSettled()
3873
3874 self.assertEqual(6, len(self.history)) # A and B jobs
3875 self.assertEqual(1, len(self.smtp_messages))
3876 self.assertEqual('The merge failed! For more information...',
3877 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003878
3879 def test_swift_instructions(self):
3880 "Test that the correct swift instructions are sent to the workers"
3881 self.config.set('zuul', 'layout_config',
3882 'tests/fixtures/layout-swift.yaml')
3883 self.sched.reconfigure(self.config)
3884 self.registerJobs()
3885
3886 self.worker.hold_jobs_in_build = True
3887 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3888
3889 A.addApproval('CRVW', 2)
3890 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3891 self.waitUntilSettled()
3892
3893 self.assertEqual(
3894 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
3895 "gate/test-merge/",
3896 self.builds[0].parameters['SWIFT_logs_URL'][:-32])
3897 self.assertEqual(5,
3898 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
3899 split('\n')))
3900 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
3901
3902 self.assertEqual(
3903 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
3904 "gate/test-test/",
3905 self.builds[1].parameters['SWIFT_logs_URL'][:-32])
3906 self.assertEqual(5,
3907 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
3908 split('\n')))
3909 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
3910
3911 self.assertEqual(
3912 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
3913 "gate/test-test/",
3914 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-32])
3915 self.assertEqual(5,
3916 len(self.builds[1].
3917 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
3918 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
3919
3920 self.worker.hold_jobs_in_build = False
3921 self.worker.release()
3922 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003923
3924 def test_client_get_running_jobs(self):
3925 "Test that the RPC client can get a list of running jobs"
3926 self.worker.hold_jobs_in_build = True
3927 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3928 A.addApproval('CRVW', 2)
3929 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3930 self.waitUntilSettled()
3931
3932 client = zuul.rpcclient.RPCClient('127.0.0.1',
3933 self.gearman_server.port)
3934
3935 # Wait for gearman server to send the initial workData back to zuul
3936 start = time.time()
3937 while True:
3938 if time.time() - start > 10:
3939 raise Exception("Timeout waiting for gearman server to report "
3940 + "back to the client")
3941 build = self.launcher.builds.values()[0]
3942 if build.worker.name == "My Worker":
3943 break
3944 else:
3945 time.sleep(0)
3946
3947 running_items = client.get_running_jobs()
3948
3949 self.assertEqual(1, len(running_items))
3950 running_item = running_items[0]
3951 self.assertEqual([], running_item['failing_reasons'])
3952 self.assertEqual([], running_item['items_behind'])
3953 self.assertEqual('https://hostname/1', running_item['url'])
3954 self.assertEqual(None, running_item['item_ahead'])
3955 self.assertEqual('org/project', running_item['project'])
3956 self.assertEqual(None, running_item['remaining_time'])
3957 self.assertEqual(True, running_item['active'])
3958 self.assertEqual('1,1', running_item['id'])
3959
3960 self.assertEqual(3, len(running_item['jobs']))
3961 for job in running_item['jobs']:
3962 if job['name'] == 'project-merge':
3963 self.assertEqual('project-merge', job['name'])
3964 self.assertEqual('gate', job['pipeline'])
3965 self.assertEqual(False, job['retry'])
3966 self.assertEqual(13, len(job['parameters']))
3967 self.assertEqual('https://server/job/project-merge/0/',
3968 job['url'])
3969 self.assertEqual(7, len(job['worker']))
3970 self.assertEqual(False, job['canceled'])
3971 self.assertEqual(True, job['voting'])
3972 self.assertEqual(None, job['result'])
3973 self.assertEqual('gate', job['pipeline'])
3974 break
3975
3976 self.worker.hold_jobs_in_build = False
3977 self.worker.release()
3978 self.waitUntilSettled()
3979
3980 running_items = client.get_running_jobs()
3981 self.assertEqual(0, len(running_items))