blob: fcc666f2c2b1d596b5bb2b1092c5f0502bfc8289 [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. Blairbadc1ad2014-04-28 13:55:14 -0700814 self.init_repo("org/experimental-project")
James E. Blair412e5582013-04-22 15:50:12 -0700815
816 self.statsd = FakeStatsd()
817 os.environ['STATSD_HOST'] = 'localhost'
818 os.environ['STATSD_PORT'] = str(self.statsd.port)
819 self.statsd.start()
820 # the statsd client object is configured in the statsd module import
821 reload(statsd)
822 reload(zuul.scheduler)
823
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700824 self.gearman_server = FakeGearmanServer()
825
826 self.config = ConfigParser.ConfigParser()
827 cfg = StringIO()
828 CONFIG.write(cfg)
829 cfg.seek(0)
830 self.config.readfp(cfg)
831 self.config.set('gearman', 'port', str(self.gearman_server.port))
832
Monty Taylorbc758832013-06-17 17:22:42 -0400833 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700834 self.worker.addServer('127.0.0.1', self.gearman_server.port)
835 self.gearman_server.worker = self.worker
836
James E. Blair4076e2b2014-01-28 12:42:20 -0800837 self.merge_server = zuul.merger.server.MergeServer(self.config)
838 self.merge_server.start()
839
James E. Blairb0fcae42012-07-17 11:12:10 -0700840 self.sched = zuul.scheduler.Scheduler()
841
Joshua Hesketh36c3fa52014-01-22 11:40:52 +1100842 self.useFixture(fixtures.MonkeyPatch('swiftclient.client.Connection',
843 FakeSwiftClientConnection))
844 self.swift = zuul.lib.swift.Swift(self.config)
845
James E. Blair8cc15a82012-08-01 11:17:57 -0700846 def URLOpenerFactory(*args, **kw):
Yuriy Taradaya6d452f2014-04-16 12:36:20 +0400847 if isinstance(args[0], urllib2.Request):
848 return old_urlopen(*args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700849 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400850 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700851
Yuriy Taradaya6d452f2014-04-16 12:36:20 +0400852 old_urlopen = urllib2.urlopen
James E. Blair8cc15a82012-08-01 11:17:57 -0700853 urllib2.urlopen = URLOpenerFactory
Joshua Hesketh36c3fa52014-01-22 11:40:52 +1100854
855 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched,
856 self.swift)
James E. Blair4076e2b2014-01-28 12:42:20 -0800857 self.merge_client = zuul.merger.client.MergeClient(
858 self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700859
James E. Blairff80a2f2013-12-27 13:24:06 -0800860 self.smtp_messages = []
861
862 def FakeSMTPFactory(*args, **kw):
863 args = [self.smtp_messages] + list(args)
864 return FakeSMTP(*args, **kw)
865
James E. Blairb0fcae42012-07-17 11:12:10 -0700866 zuul.lib.gerrit.Gerrit = FakeGerrit
James E. Blairff80a2f2013-12-27 13:24:06 -0800867 self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTPFactory))
James E. Blairb0fcae42012-07-17 11:12:10 -0700868
Monty Taylorbc758832013-06-17 17:22:42 -0400869 self.gerrit = FakeGerritTrigger(
870 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700871 self.gerrit.replication_timeout = 1.5
872 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700873 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400874 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700875
James E. Blair1843a552013-07-03 14:19:52 -0700876 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
James E. Blairad28e912013-11-27 10:43:22 -0800877 self.rpc = zuul.rpclistener.RPCListener(self.config, self.sched)
James E. Blair1843a552013-07-03 14:19:52 -0700878
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700879 self.sched.setLauncher(self.launcher)
James E. Blair4076e2b2014-01-28 12:42:20 -0800880 self.sched.setMerger(self.merge_client)
James E. Blair6c358e72013-07-29 17:06:47 -0700881 self.sched.registerTrigger(self.gerrit)
James E. Blair63bb0ef2013-07-29 17:14:51 -0700882 self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
883 self.sched.registerTrigger(self.timer)
James E. Blairb0fcae42012-07-17 11:12:10 -0700884
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000885 self.sched.registerReporter(
886 zuul.reporter.gerrit.Reporter(self.gerrit))
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000887 self.smtp_reporter = zuul.reporter.smtp.Reporter(
888 self.config.get('smtp', 'default_from'),
889 self.config.get('smtp', 'default_to'),
890 self.config.get('smtp', 'server'))
891 self.sched.registerReporter(self.smtp_reporter)
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000892
James E. Blairb0fcae42012-07-17 11:12:10 -0700893 self.sched.start()
894 self.sched.reconfigure(self.config)
895 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700896 self.webapp.start()
James E. Blairad28e912013-11-27 10:43:22 -0800897 self.rpc.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700898 self.launcher.gearman.waitForServer()
899 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400900 self.builds = self.worker.running_builds
901 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700902
James E. Blairfee8d652013-06-07 08:57:52 -0700903 self.addCleanup(self.assertFinalState)
904 self.addCleanup(self.shutdown)
905
906 def assertFinalState(self):
907 # Make sure that the change cache is cleared
James E. Blair6c358e72013-07-29 17:06:47 -0700908 self.assertEqual(len(self.gerrit._change_cache.keys()), 0)
Clark Boylan4ba48d92013-11-11 18:03:53 -0800909 # Make sure that git.Repo objects have been garbage collected.
910 repos = []
911 gc.collect()
912 for obj in gc.get_objects():
913 if isinstance(obj, git.Repo):
914 repos.append(obj)
915 self.assertEqual(len(repos), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700916 self.assertEmptyQueues()
917
918 def shutdown(self):
919 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700920 self.launcher.stop()
James E. Blair4076e2b2014-01-28 12:42:20 -0800921 self.merge_server.stop()
922 self.merge_server.join()
923 self.merge_client.stop()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700924 self.worker.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700925 self.gerrit.stop()
James E. Blair63bb0ef2013-07-29 17:14:51 -0700926 self.timer.stop()
James E. Blairb0fcae42012-07-17 11:12:10 -0700927 self.sched.stop()
928 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700929 self.statsd.stop()
930 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700931 self.webapp.stop()
932 self.webapp.join()
James E. Blairad28e912013-11-27 10:43:22 -0800933 self.rpc.stop()
934 self.rpc.join()
James E. Blair491b3172014-03-10 15:05:36 -0700935 self.gearman_server.shutdown()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700936 threads = threading.enumerate()
937 if len(threads) > 1:
938 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400939 super(TestScheduler, self).tearDown()
940
941 def init_repo(self, project):
942 parts = project.split('/')
943 path = os.path.join(self.upstream_root, *parts[:-1])
944 if not os.path.exists(path):
945 os.makedirs(path)
946 path = os.path.join(self.upstream_root, project)
947 repo = git.Repo.init(path)
948
949 repo.config_writer().set_value('user', 'email', 'user@example.com')
950 repo.config_writer().set_value('user', 'name', 'User Name')
951 repo.config_writer().write()
952
953 fn = os.path.join(path, 'README')
954 f = open(fn, 'w')
955 f.write("test\n")
956 f.close()
957 repo.index.add([fn])
958 repo.index.commit('initial commit')
959 master = repo.create_head('master')
960 repo.create_tag('init')
961
962 mp = repo.create_head('mp')
963 repo.head.reference = mp
964 f = open(fn, 'a')
965 f.write("test mp\n")
966 f.close()
967 repo.index.add([fn])
968 repo.index.commit('mp commit')
969
970 repo.head.reference = master
971 repo.head.reset(index=True, working_tree=True)
972 repo.git.clean('-x', '-f', '-d')
973
974 def ref_has_change(self, ref, change):
975 path = os.path.join(self.git_root, change.project)
976 repo = git.Repo(path)
977 for commit in repo.iter_commits(ref):
978 if commit.message.strip() == ('%s-1' % change.subject):
979 return True
980 return False
981
982 def job_has_changes(self, *args):
983 job = args[0]
984 commits = args[1:]
985 if isinstance(job, FakeBuild):
986 parameters = job.parameters
987 else:
988 parameters = json.loads(job.arguments)
989 project = parameters['ZUUL_PROJECT']
990 path = os.path.join(self.git_root, project)
991 repo = git.Repo(path)
992 ref = parameters['ZUUL_REF']
993 sha = parameters['ZUUL_COMMIT']
994 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
995 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
996 commit_messages = ['%s-1' % commit.subject for commit in commits]
James E. Blairac2c3242014-01-24 13:38:51 -0800997 self.log.debug("Checking if job %s has changes; commit_messages %s;"
998 " repo_messages %s; sha %s" % (job, commit_messages,
999 repo_messages, sha))
Monty Taylorbc758832013-06-17 17:22:42 -04001000 for msg in commit_messages:
1001 if msg not in repo_messages:
James E. Blairac2c3242014-01-24 13:38:51 -08001002 self.log.debug(" messages do not match")
Monty Taylorbc758832013-06-17 17:22:42 -04001003 return False
1004 if repo_shas[0] != sha:
James E. Blairac2c3242014-01-24 13:38:51 -08001005 self.log.debug(" sha does not match")
Monty Taylorbc758832013-06-17 17:22:42 -04001006 return False
James E. Blairac2c3242014-01-24 13:38:51 -08001007 self.log.debug(" OK")
Monty Taylorbc758832013-06-17 17:22:42 -04001008 return True
James E. Blairb0fcae42012-07-17 11:12:10 -07001009
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001010 def registerJobs(self):
1011 count = 0
James E. Blaireff88162013-07-01 12:44:14 -04001012 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001013 self.worker.registerFunction('build:' + job)
1014 count += 1
1015 self.worker.registerFunction('stop:' + self.worker.worker_id)
1016 count += 1
1017
1018 while len(self.gearman_server.functions) < count:
1019 time.sleep(0)
1020
1021 def release(self, job):
1022 if isinstance(job, FakeBuild):
1023 job.release()
1024 else:
1025 job.waiting = False
1026 self.log.debug("Queued job %s released" % job.unique)
1027 self.gearman_server.wakeConnections()
1028
1029 def getParameter(self, job, name):
1030 if isinstance(job, FakeBuild):
1031 return job.parameters[name]
1032 else:
1033 parameters = json.loads(job.arguments)
1034 return parameters[name]
1035
1036 def resetGearmanServer(self):
1037 self.worker.setFunctions([])
1038 while True:
1039 done = True
1040 for connection in self.gearman_server.active_connections:
James E. Blairad28e912013-11-27 10:43:22 -08001041 if (connection.functions and
James E. Blair4076e2b2014-01-28 12:42:20 -08001042 connection.client_id not in ['Zuul RPC Listener',
1043 'Zuul Merger']):
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001044 done = False
1045 if done:
1046 break
1047 time.sleep(0)
1048 self.gearman_server.functions = set()
James E. Blairad28e912013-11-27 10:43:22 -08001049 self.rpc.register()
James E. Blair4076e2b2014-01-28 12:42:20 -08001050 self.merge_server.register()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001051
1052 def haveAllBuildsReported(self):
1053 # See if Zuul is waiting on a meta job to complete
1054 if self.launcher.meta_jobs:
1055 return False
1056 # Find out if every build that the worker has completed has been
1057 # reported back to Zuul. If it hasn't then that means a Gearman
1058 # event is still in transit and the system is not stable.
1059 for build in self.worker.build_history:
1060 zbuild = self.launcher.builds.get(build.uuid)
1061 if not zbuild:
1062 # It has already been reported
1063 continue
1064 # It hasn't been reported yet.
1065 return False
1066 # Make sure that none of the worker connections are in GRAB_WAIT
1067 for connection in self.worker.active_connections:
1068 if connection.state == 'GRAB_WAIT':
1069 return False
1070 return True
1071
1072 def areAllBuildsWaiting(self):
1073 ret = True
1074
1075 builds = self.launcher.builds.values()
1076 for build in builds:
1077 client_job = None
1078 for conn in self.launcher.gearman.active_connections:
1079 for j in conn.related_jobs.values():
1080 if j.unique == build.uuid:
1081 client_job = j
1082 break
1083 if not client_job:
1084 self.log.debug("%s is not known to the gearman client" %
1085 build)
1086 ret = False
1087 continue
1088 if not client_job.handle:
1089 self.log.debug("%s has no handle" % client_job)
1090 ret = False
1091 continue
1092 server_job = self.gearman_server.jobs.get(client_job.handle)
1093 if not server_job:
1094 self.log.debug("%s is not known to the gearman server" %
1095 client_job)
1096 ret = False
1097 continue
1098 if not hasattr(server_job, 'waiting'):
1099 self.log.debug("%s is being enqueued" % server_job)
1100 ret = False
1101 continue
1102 if server_job.waiting:
1103 continue
1104 worker_job = self.worker.gearman_jobs.get(server_job.unique)
1105 if worker_job:
1106 if worker_job.build.isWaiting():
1107 continue
1108 else:
1109 self.log.debug("%s is running" % worker_job)
1110 ret = False
1111 else:
1112 self.log.debug("%s is unassigned" % server_job)
1113 ret = False
1114 return ret
1115
James E. Blairb0fcae42012-07-17 11:12:10 -07001116 def waitUntilSettled(self):
1117 self.log.debug("Waiting until settled...")
1118 start = time.time()
1119 while True:
1120 if time.time() - start > 10:
1121 print 'queue status:',
1122 print self.sched.trigger_event_queue.empty(),
1123 print self.sched.result_event_queue.empty(),
1124 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001125 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -07001126 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001127 # Make sure no new events show up while we're checking
1128 self.worker.lock.acquire()
1129 # have all build states propogated to zuul?
1130 if self.haveAllBuildsReported():
1131 # Join ensures that the queue is empty _and_ events have been
1132 # processed
1133 self.fake_gerrit.event_queue.join()
1134 self.sched.trigger_event_queue.join()
1135 self.sched.result_event_queue.join()
James E. Blaira84f0e42014-02-06 07:09:22 -08001136 self.sched.run_handler_lock.acquire()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001137 if (self.sched.trigger_event_queue.empty() and
1138 self.sched.result_event_queue.empty() and
1139 self.fake_gerrit.event_queue.empty() and
James E. Blair4076e2b2014-01-28 12:42:20 -08001140 not self.merge_client.build_sets and
James E. Blair9c2985a2014-03-10 10:59:05 -07001141 self.haveAllBuildsReported() and
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001142 self.areAllBuildsWaiting()):
James E. Blaira84f0e42014-02-06 07:09:22 -08001143 self.sched.run_handler_lock.release()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001144 self.worker.lock.release()
1145 self.log.debug("...settled.")
1146 return
James E. Blaira84f0e42014-02-06 07:09:22 -08001147 self.sched.run_handler_lock.release()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001148 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001149 self.sched.wake_event.wait(0.1)
1150
James E. Blaird466dc42012-07-31 10:42:56 -07001151 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -08001152 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -07001153 return len(jobs)
1154
James E. Blair4ca985f2013-05-30 12:27:43 -07001155 def getJobFromHistory(self, name):
1156 history = self.worker.build_history
1157 for job in history:
1158 if job.name == name:
1159 return job
1160 raise Exception("Unable to find job %s in history" % name)
1161
James E. Blaire0487072012-08-29 17:38:31 -07001162 def assertEmptyQueues(self):
1163 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001164 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001165 for queue in pipeline.queues:
1166 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001167 print 'pipeline %s queue %s contents %s' % (
1168 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001169 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001170
James E. Blair66eeebf2013-07-27 17:44:32 -07001171 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001172 start = time.time()
1173 while time.time() < (start + 5):
1174 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001175 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001176 k, v = stat.split(':')
1177 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001178 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001179 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001180 elif value:
1181 if value == v:
1182 return
1183 elif kind:
1184 if v.endswith('|' + kind):
1185 return
James E. Blair412e5582013-04-22 15:50:12 -07001186 time.sleep(0.1)
1187
1188 pprint.pprint(self.statsd.stats)
1189 raise Exception("Key %s not found in reported stats" % key)
1190
James E. Blairb0fcae42012-07-17 11:12:10 -07001191 def test_jobs_launched(self):
1192 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001193
James E. Blairb0fcae42012-07-17 11:12:10 -07001194 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001195 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001196 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1197 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001198 self.assertEqual(self.getJobFromHistory('project-merge').result,
1199 'SUCCESS')
1200 self.assertEqual(self.getJobFromHistory('project-test1').result,
1201 'SUCCESS')
1202 self.assertEqual(self.getJobFromHistory('project-test2').result,
1203 'SUCCESS')
1204 self.assertEqual(A.data['status'], 'MERGED')
1205 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001206
James E. Blair66eeebf2013-07-27 17:44:32 -07001207 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1208 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1209 value='1|g')
1210 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1211 kind='ms')
1212 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1213 value='1|c')
1214 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1215 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1216 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001217 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001218 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001219 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001220 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001221
James E. Blair3cb10702013-08-24 08:56:03 -07001222 def test_initial_pipeline_gauges(self):
1223 "Test that each pipeline reported its length on start"
1224 pipeline_names = self.sched.layout.pipelines.keys()
1225 self.assertNotEqual(len(pipeline_names), 0)
1226 for name in pipeline_names:
1227 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
1228 value='0|g')
1229
James E. Blair42f74822013-05-14 15:18:03 -07001230 def test_duplicate_pipelines(self):
1231 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001232
James E. Blair42f74822013-05-14 15:18:03 -07001233 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1234 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1235 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001236
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001237 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001238 self.history[0].name == 'project-test1'
1239 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001240
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001241 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001242 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001243 self.assertIn('dup1/project-test1', A.messages[0])
1244 self.assertNotIn('dup2/project-test1', A.messages[0])
1245 self.assertNotIn('dup1/project-test1', A.messages[1])
1246 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001247 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001248 self.assertIn('dup1/project-test1', A.messages[1])
1249 self.assertNotIn('dup2/project-test1', A.messages[1])
1250 self.assertNotIn('dup1/project-test1', A.messages[0])
1251 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001252
James E. Blairb0fcae42012-07-17 11:12:10 -07001253 def test_parallel_changes(self):
1254 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001255
1256 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001257 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1258 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1259 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001260 A.addApproval('CRVW', 2)
1261 B.addApproval('CRVW', 2)
1262 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001263
1264 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1265 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1266 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1267
1268 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001269 self.assertEqual(len(self.builds), 1)
1270 self.assertEqual(self.builds[0].name, 'project-merge')
1271 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001272
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001273 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001274 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001275 self.assertEqual(len(self.builds), 3)
1276 self.assertEqual(self.builds[0].name, 'project-test1')
1277 self.assertTrue(self.job_has_changes(self.builds[0], A))
1278 self.assertEqual(self.builds[1].name, 'project-test2')
1279 self.assertTrue(self.job_has_changes(self.builds[1], A))
1280 self.assertEqual(self.builds[2].name, 'project-merge')
1281 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001282
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001283 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001284 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001285 self.assertEqual(len(self.builds), 5)
1286 self.assertEqual(self.builds[0].name, 'project-test1')
1287 self.assertTrue(self.job_has_changes(self.builds[0], A))
1288 self.assertEqual(self.builds[1].name, 'project-test2')
1289 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001290
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001291 self.assertEqual(self.builds[2].name, 'project-test1')
1292 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1293 self.assertEqual(self.builds[3].name, 'project-test2')
1294 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001295
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001296 self.assertEqual(self.builds[4].name, 'project-merge')
1297 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001298
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001299 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001300 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001301 self.assertEqual(len(self.builds), 6)
1302 self.assertEqual(self.builds[0].name, 'project-test1')
1303 self.assertTrue(self.job_has_changes(self.builds[0], A))
1304 self.assertEqual(self.builds[1].name, 'project-test2')
1305 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001306
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001307 self.assertEqual(self.builds[2].name, 'project-test1')
1308 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1309 self.assertEqual(self.builds[3].name, 'project-test2')
1310 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001311
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001312 self.assertEqual(self.builds[4].name, 'project-test1')
1313 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1314 self.assertEqual(self.builds[5].name, 'project-test2')
1315 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001316
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001317 self.worker.hold_jobs_in_build = False
1318 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001319 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001320 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001321
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001322 self.assertEqual(len(self.history), 9)
1323 self.assertEqual(A.data['status'], 'MERGED')
1324 self.assertEqual(B.data['status'], 'MERGED')
1325 self.assertEqual(C.data['status'], 'MERGED')
1326 self.assertEqual(A.reported, 2)
1327 self.assertEqual(B.reported, 2)
1328 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001329
1330 def test_failed_changes(self):
1331 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001332 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001333
James E. Blairb02a3bb2012-07-30 17:49:55 -07001334 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1335 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001336 A.addApproval('CRVW', 2)
1337 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001338
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001339 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001340
James E. Blaire2819012013-06-28 17:17:26 -04001341 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1342 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001343 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001344
1345 self.worker.release('.*-merge')
1346 self.waitUntilSettled()
1347
1348 self.worker.hold_jobs_in_build = False
1349 self.worker.release()
1350
1351 self.waitUntilSettled()
1352 # It's certain that the merge job for change 2 will run, but
1353 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001354 self.assertTrue(len(self.history) > 6)
1355 self.assertEqual(A.data['status'], 'NEW')
1356 self.assertEqual(B.data['status'], 'MERGED')
1357 self.assertEqual(A.reported, 2)
1358 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001359
1360 def test_independent_queues(self):
1361 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001362
1363 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001364 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001365 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1366 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001367 A.addApproval('CRVW', 2)
1368 B.addApproval('CRVW', 2)
1369 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001370
1371 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1372 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1373 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1374
James E. Blairb02a3bb2012-07-30 17:49:55 -07001375 self.waitUntilSettled()
1376
1377 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001378 self.assertEqual(len(self.builds), 2)
1379 self.assertEqual(self.builds[0].name, 'project-merge')
1380 self.assertTrue(self.job_has_changes(self.builds[0], A))
1381 self.assertEqual(self.builds[1].name, 'project1-merge')
1382 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001383
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001384 # Release the current merge builds
1385 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001386 self.waitUntilSettled()
1387 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001388 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001389 self.waitUntilSettled()
1390
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001391 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001392 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001393 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001394
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001395 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001396 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001397 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001398
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001399 self.assertEqual(len(self.history), 11)
1400 self.assertEqual(A.data['status'], 'MERGED')
1401 self.assertEqual(B.data['status'], 'MERGED')
1402 self.assertEqual(C.data['status'], 'MERGED')
1403 self.assertEqual(A.reported, 2)
1404 self.assertEqual(B.reported, 2)
1405 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001406
1407 def test_failed_change_at_head(self):
1408 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001409
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001410 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001411 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1412 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1413 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001414 A.addApproval('CRVW', 2)
1415 B.addApproval('CRVW', 2)
1416 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001417
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001418 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001419
1420 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1421 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1422 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1423
1424 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001425
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001426 self.assertEqual(len(self.builds), 1)
1427 self.assertEqual(self.builds[0].name, 'project-merge')
1428 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001429
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001430 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001431 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001432 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001433 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001434 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001435 self.waitUntilSettled()
1436
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001437 self.assertEqual(len(self.builds), 6)
1438 self.assertEqual(self.builds[0].name, 'project-test1')
1439 self.assertEqual(self.builds[1].name, 'project-test2')
1440 self.assertEqual(self.builds[2].name, 'project-test1')
1441 self.assertEqual(self.builds[3].name, 'project-test2')
1442 self.assertEqual(self.builds[4].name, 'project-test1')
1443 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001444
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001445 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001446 self.waitUntilSettled()
1447
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001448 # project-test2, project-merge for B
1449 self.assertEqual(len(self.builds), 2)
1450 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001451
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001452 self.worker.hold_jobs_in_build = False
1453 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001454 self.waitUntilSettled()
1455
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001456 self.assertEqual(len(self.builds), 0)
1457 self.assertEqual(len(self.history), 15)
1458 self.assertEqual(A.data['status'], 'NEW')
1459 self.assertEqual(B.data['status'], 'MERGED')
1460 self.assertEqual(C.data['status'], 'MERGED')
1461 self.assertEqual(A.reported, 2)
1462 self.assertEqual(B.reported, 2)
1463 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001464
James E. Blair0aac4872013-08-23 14:02:38 -07001465 def test_failed_change_in_middle(self):
1466 "Test a failed change in the middle of the queue"
1467
1468 self.worker.hold_jobs_in_build = True
1469 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1470 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1471 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1472 A.addApproval('CRVW', 2)
1473 B.addApproval('CRVW', 2)
1474 C.addApproval('CRVW', 2)
1475
1476 self.worker.addFailTest('project-test1', B)
1477
1478 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1479 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1480 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1481
1482 self.waitUntilSettled()
1483
1484 self.worker.release('.*-merge')
1485 self.waitUntilSettled()
1486 self.worker.release('.*-merge')
1487 self.waitUntilSettled()
1488 self.worker.release('.*-merge')
1489 self.waitUntilSettled()
1490
1491 self.assertEqual(len(self.builds), 6)
1492 self.assertEqual(self.builds[0].name, 'project-test1')
1493 self.assertEqual(self.builds[1].name, 'project-test2')
1494 self.assertEqual(self.builds[2].name, 'project-test1')
1495 self.assertEqual(self.builds[3].name, 'project-test2')
1496 self.assertEqual(self.builds[4].name, 'project-test1')
1497 self.assertEqual(self.builds[5].name, 'project-test2')
1498
1499 self.release(self.builds[2])
1500 self.waitUntilSettled()
1501
James E. Blair972e3c72013-08-29 12:04:55 -07001502 # project-test1 and project-test2 for A
1503 # project-test2 for B
1504 # project-merge for C (without B)
1505 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -07001506 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1507
James E. Blair972e3c72013-08-29 12:04:55 -07001508 self.worker.release('.*-merge')
1509 self.waitUntilSettled()
1510
1511 # project-test1 and project-test2 for A
1512 # project-test2 for B
1513 # project-test1 and project-test2 for C
1514 self.assertEqual(len(self.builds), 5)
1515
James E. Blair0aac4872013-08-23 14:02:38 -07001516 items = self.sched.layout.pipelines['gate'].getAllItems()
1517 builds = items[0].current_build_set.getBuilds()
1518 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1519 self.assertEqual(self.countJobResults(builds, None), 2)
1520 builds = items[1].current_build_set.getBuilds()
1521 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1522 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1523 self.assertEqual(self.countJobResults(builds, None), 1)
1524 builds = items[2].current_build_set.getBuilds()
1525 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -07001526 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -07001527
1528 self.worker.hold_jobs_in_build = False
1529 self.worker.release()
1530 self.waitUntilSettled()
1531
1532 self.assertEqual(len(self.builds), 0)
1533 self.assertEqual(len(self.history), 12)
1534 self.assertEqual(A.data['status'], 'MERGED')
1535 self.assertEqual(B.data['status'], 'NEW')
1536 self.assertEqual(C.data['status'], 'MERGED')
1537 self.assertEqual(A.reported, 2)
1538 self.assertEqual(B.reported, 2)
1539 self.assertEqual(C.reported, 2)
1540
James E. Blaird466dc42012-07-31 10:42:56 -07001541 def test_failed_change_at_head_with_queue(self):
1542 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001543
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001544 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001545 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1546 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1547 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001548 A.addApproval('CRVW', 2)
1549 B.addApproval('CRVW', 2)
1550 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001551
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001552 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001553
1554 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1555 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1556 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1557
1558 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001559 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001560 self.assertEqual(len(self.builds), 0)
1561 self.assertEqual(len(queue), 1)
1562 self.assertEqual(queue[0].name, 'build:project-merge')
1563 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001564
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001565 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001566 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001567 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001568 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001569 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001570 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001571 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001572
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001573 self.assertEqual(len(self.builds), 0)
1574 self.assertEqual(len(queue), 6)
1575 self.assertEqual(queue[0].name, 'build:project-test1')
1576 self.assertEqual(queue[1].name, 'build:project-test2')
1577 self.assertEqual(queue[2].name, 'build:project-test1')
1578 self.assertEqual(queue[3].name, 'build:project-test2')
1579 self.assertEqual(queue[4].name, 'build:project-test1')
1580 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001581
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001582 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001583 self.waitUntilSettled()
1584
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001585 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001586 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001587 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1588 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001589
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001590 self.gearman_server.hold_jobs_in_queue = False
1591 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001592 self.waitUntilSettled()
1593
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001594 self.assertEqual(len(self.builds), 0)
1595 self.assertEqual(len(self.history), 11)
1596 self.assertEqual(A.data['status'], 'NEW')
1597 self.assertEqual(B.data['status'], 'MERGED')
1598 self.assertEqual(C.data['status'], 'MERGED')
1599 self.assertEqual(A.reported, 2)
1600 self.assertEqual(B.reported, 2)
1601 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001602
James E. Blairfef71632013-09-23 11:15:47 -07001603 def test_two_failed_changes_at_head(self):
1604 "Test that changes are reparented correctly if 2 fail at head"
1605
1606 self.worker.hold_jobs_in_build = True
1607 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1608 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1609 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1610 A.addApproval('CRVW', 2)
1611 B.addApproval('CRVW', 2)
1612 C.addApproval('CRVW', 2)
1613
1614 self.worker.addFailTest('project-test1', A)
1615 self.worker.addFailTest('project-test1', B)
1616
1617 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1618 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1619 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1620 self.waitUntilSettled()
1621
1622 self.worker.release('.*-merge')
1623 self.waitUntilSettled()
1624 self.worker.release('.*-merge')
1625 self.waitUntilSettled()
1626 self.worker.release('.*-merge')
1627 self.waitUntilSettled()
1628
1629 self.assertEqual(len(self.builds), 6)
1630 self.assertEqual(self.builds[0].name, 'project-test1')
1631 self.assertEqual(self.builds[1].name, 'project-test2')
1632 self.assertEqual(self.builds[2].name, 'project-test1')
1633 self.assertEqual(self.builds[3].name, 'project-test2')
1634 self.assertEqual(self.builds[4].name, 'project-test1')
1635 self.assertEqual(self.builds[5].name, 'project-test2')
1636
1637 self.assertTrue(self.job_has_changes(self.builds[0], A))
1638 self.assertTrue(self.job_has_changes(self.builds[2], A))
1639 self.assertTrue(self.job_has_changes(self.builds[2], B))
1640 self.assertTrue(self.job_has_changes(self.builds[4], A))
1641 self.assertTrue(self.job_has_changes(self.builds[4], B))
1642 self.assertTrue(self.job_has_changes(self.builds[4], C))
1643
1644 # Fail change B first
1645 self.release(self.builds[2])
1646 self.waitUntilSettled()
1647
1648 # restart of C after B failure
1649 self.worker.release('.*-merge')
1650 self.waitUntilSettled()
1651
1652 self.assertEqual(len(self.builds), 5)
1653 self.assertEqual(self.builds[0].name, 'project-test1')
1654 self.assertEqual(self.builds[1].name, 'project-test2')
1655 self.assertEqual(self.builds[2].name, 'project-test2')
1656 self.assertEqual(self.builds[3].name, 'project-test1')
1657 self.assertEqual(self.builds[4].name, 'project-test2')
1658
1659 self.assertTrue(self.job_has_changes(self.builds[1], A))
1660 self.assertTrue(self.job_has_changes(self.builds[2], A))
1661 self.assertTrue(self.job_has_changes(self.builds[2], B))
1662 self.assertTrue(self.job_has_changes(self.builds[4], A))
1663 self.assertFalse(self.job_has_changes(self.builds[4], B))
1664 self.assertTrue(self.job_has_changes(self.builds[4], C))
1665
1666 # Finish running all passing jobs for change A
1667 self.release(self.builds[1])
1668 self.waitUntilSettled()
1669 # Fail and report change A
1670 self.release(self.builds[0])
1671 self.waitUntilSettled()
1672
1673 # restart of B,C after A failure
1674 self.worker.release('.*-merge')
1675 self.waitUntilSettled()
1676 self.worker.release('.*-merge')
1677 self.waitUntilSettled()
1678
1679 self.assertEqual(len(self.builds), 4)
1680 self.assertEqual(self.builds[0].name, 'project-test1') # B
1681 self.assertEqual(self.builds[1].name, 'project-test2') # B
1682 self.assertEqual(self.builds[2].name, 'project-test1') # C
1683 self.assertEqual(self.builds[3].name, 'project-test2') # C
1684
1685 self.assertFalse(self.job_has_changes(self.builds[1], A))
1686 self.assertTrue(self.job_has_changes(self.builds[1], B))
1687 self.assertFalse(self.job_has_changes(self.builds[1], C))
1688
1689 self.assertFalse(self.job_has_changes(self.builds[2], A))
1690 # After A failed and B and C restarted, B should be back in
1691 # C's tests because it has not failed yet.
1692 self.assertTrue(self.job_has_changes(self.builds[2], B))
1693 self.assertTrue(self.job_has_changes(self.builds[2], C))
1694
1695 self.worker.hold_jobs_in_build = False
1696 self.worker.release()
1697 self.waitUntilSettled()
1698
1699 self.assertEqual(len(self.builds), 0)
1700 self.assertEqual(len(self.history), 21)
1701 self.assertEqual(A.data['status'], 'NEW')
1702 self.assertEqual(B.data['status'], 'NEW')
1703 self.assertEqual(C.data['status'], 'MERGED')
1704 self.assertEqual(A.reported, 2)
1705 self.assertEqual(B.reported, 2)
1706 self.assertEqual(C.reported, 2)
1707
James E. Blair8c803f82012-07-31 16:25:42 -07001708 def test_patch_order(self):
1709 "Test that dependent patches are tested in the right order"
1710 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1711 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1712 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1713 A.addApproval('CRVW', 2)
1714 B.addApproval('CRVW', 2)
1715 C.addApproval('CRVW', 2)
1716
1717 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1718 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1719 M2.setMerged()
1720 M1.setMerged()
1721
1722 # C -> B -> A -> M1 -> M2
1723 # M2 is here to make sure it is never queried. If it is, it
1724 # means zuul is walking down the entire history of merged
1725 # changes.
1726
1727 C.setDependsOn(B, 1)
1728 B.setDependsOn(A, 1)
1729 A.setDependsOn(M1, 1)
1730 M1.setDependsOn(M2, 1)
1731
1732 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1733
1734 self.waitUntilSettled()
1735
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001736 self.assertEqual(A.data['status'], 'NEW')
1737 self.assertEqual(B.data['status'], 'NEW')
1738 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001739
1740 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1741 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1742
1743 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001744 self.assertEqual(M2.queried, 0)
1745 self.assertEqual(A.data['status'], 'MERGED')
1746 self.assertEqual(B.data['status'], 'MERGED')
1747 self.assertEqual(C.data['status'], 'MERGED')
1748 self.assertEqual(A.reported, 2)
1749 self.assertEqual(B.reported, 2)
1750 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001751
James E. Blair0e933c52013-07-11 10:18:52 -07001752 def test_trigger_cache(self):
1753 "Test that the trigger cache operates correctly"
1754 self.worker.hold_jobs_in_build = True
1755
1756 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1757 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1758 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1759 A.addApproval('CRVW', 2)
1760 B.addApproval('CRVW', 2)
1761
1762 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1763 M1.setMerged()
1764
1765 B.setDependsOn(A, 1)
1766 A.setDependsOn(M1, 1)
1767
1768 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1769 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1770
1771 self.waitUntilSettled()
1772
1773 for build in self.builds:
1774 if build.parameters['ZUUL_PIPELINE'] == 'check':
1775 build.release()
1776 self.waitUntilSettled()
1777 for build in self.builds:
1778 if build.parameters['ZUUL_PIPELINE'] == 'check':
1779 build.release()
1780 self.waitUntilSettled()
1781
1782 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1783 self.waitUntilSettled()
1784
James E. Blair6c358e72013-07-29 17:06:47 -07001785 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001786 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001787 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001788
1789 self.worker.hold_jobs_in_build = False
1790 self.worker.release()
1791 self.waitUntilSettled()
1792
1793 self.assertEqual(A.data['status'], 'MERGED')
1794 self.assertEqual(B.data['status'], 'MERGED')
1795 self.assertEqual(A.queried, 2) # Initial and isMerged
1796 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1797
James E. Blair8c803f82012-07-31 16:25:42 -07001798 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001799 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001800 # TODO: move to test_gerrit (this is a unit test!)
1801 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001802 trigger = self.sched.layout.pipelines['gate'].trigger
1803 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001804 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001805 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001806
1807 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001808 a = trigger.getChange(1, 2, refresh=True)
1809 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001810
1811 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001812 a = trigger.getChange(1, 2, refresh=True)
1813 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1814 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001815
1816 def test_build_configuration(self):
1817 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001818
1819 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001820 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1821 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1822 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1823 A.addApproval('CRVW', 2)
1824 B.addApproval('CRVW', 2)
1825 C.addApproval('CRVW', 2)
1826 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1827 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1828 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1829 self.waitUntilSettled()
1830
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001831 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001832 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001833 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001834 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001835 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001836 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001837 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001838 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1839 self.gearman_server.hold_jobs_in_queue = False
1840 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001841 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001842
Monty Taylorbc758832013-06-17 17:22:42 -04001843 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001844 repo = git.Repo(path)
1845 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1846 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001847 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001848 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001849
1850 def test_build_configuration_conflict(self):
1851 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001852
1853 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001854 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1855 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001856 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001857 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1858 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001859 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001860 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1861 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001862 A.addApproval('CRVW', 2)
1863 B.addApproval('CRVW', 2)
1864 C.addApproval('CRVW', 2)
1865 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1866 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1867 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1868 self.waitUntilSettled()
1869
James E. Blair6736beb2013-07-11 15:18:15 -07001870 self.assertEqual(A.reported, 1)
1871 self.assertEqual(B.reported, 1)
1872 self.assertEqual(C.reported, 1)
1873
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001874 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001875 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001876 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001877 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001878 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001879 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001880
1881 self.assertEqual(len(self.history), 2) # A and C merge jobs
1882
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001883 self.gearman_server.hold_jobs_in_queue = False
1884 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001885 self.waitUntilSettled()
1886
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001887 self.assertEqual(A.data['status'], 'MERGED')
1888 self.assertEqual(B.data['status'], 'NEW')
1889 self.assertEqual(C.data['status'], 'MERGED')
1890 self.assertEqual(A.reported, 2)
1891 self.assertEqual(B.reported, 2)
1892 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001893 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001894
James E. Blairdaabed22012-08-15 15:38:57 -07001895 def test_post(self):
1896 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001897
Zhongyue Luo5d556072012-09-21 02:00:47 +09001898 e = {
1899 "type": "ref-updated",
1900 "submitter": {
1901 "name": "User Name",
1902 },
1903 "refUpdate": {
1904 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1905 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1906 "refName": "master",
1907 "project": "org/project",
1908 }
1909 }
James E. Blairdaabed22012-08-15 15:38:57 -07001910 self.fake_gerrit.addEvent(e)
1911 self.waitUntilSettled()
1912
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001913 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001914 self.assertEqual(len(self.history), 1)
1915 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001916
1917 def test_build_configuration_branch(self):
1918 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001919
1920 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001921 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1922 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1923 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1924 A.addApproval('CRVW', 2)
1925 B.addApproval('CRVW', 2)
1926 C.addApproval('CRVW', 2)
1927 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1928 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1929 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1930 self.waitUntilSettled()
1931
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001932 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001933 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001934 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001935 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001936 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001937 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001938 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001939 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1940 self.gearman_server.hold_jobs_in_queue = False
1941 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001942 self.waitUntilSettled()
1943
Monty Taylorbc758832013-06-17 17:22:42 -04001944 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001945 repo = git.Repo(path)
1946 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1947 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001948 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001949 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001950
1951 def test_build_configuration_branch_interaction(self):
1952 "Test that switching between branches works"
1953 self.test_build_configuration()
1954 self.test_build_configuration_branch()
1955 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001956 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001957 repo = git.Repo(path)
1958 repo.heads.master.commit = repo.commit('init')
1959 self.test_build_configuration()
1960
1961 def test_build_configuration_multi_branch(self):
1962 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001963
1964 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001965 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1966 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1967 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1968 A.addApproval('CRVW', 2)
1969 B.addApproval('CRVW', 2)
1970 C.addApproval('CRVW', 2)
1971 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1972 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1973 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1974 self.waitUntilSettled()
James E. Blairbb1fe502014-03-04 10:15:06 -08001975 queue = self.gearman_server.getQueue()
1976 job_A = None
1977 for job in queue:
1978 if 'project-merge' in job.name:
1979 job_A = job
1980 ref_A = self.getParameter(job_A, 'ZUUL_REF')
1981 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
1982 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
1983 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
James E. Blairc6294a52012-08-17 10:19:48 -07001984
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001985 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001986 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001987 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001988 job_B = None
1989 for job in queue:
1990 if 'project-merge' in job.name:
1991 job_B = job
1992 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001993 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001994 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blairbb1fe502014-03-04 10:15:06 -08001995 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
1996
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001997 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001998 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001999 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07002000 for job in queue:
2001 if 'project-merge' in job.name:
2002 job_C = job
2003 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08002004 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07002005 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blairbb1fe502014-03-04 10:15:06 -08002006 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002007 self.gearman_server.hold_jobs_in_queue = False
2008 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07002009 self.waitUntilSettled()
2010
Monty Taylorbc758832013-06-17 17:22:42 -04002011 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07002012 repo = git.Repo(path)
2013
2014 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07002015 for c in repo.iter_commits(ref_C)]
James E. Blairbb1fe502014-03-04 10:15:06 -08002016 repo_shas = [c.hexsha for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07002017 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07002018 correct_messages = ['initial commit', 'A-1', 'C-1']
James E. Blairbb1fe502014-03-04 10:15:06 -08002019 # Ensure the right commits are in the history for this ref
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002020 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08002021 # Ensure ZUUL_REF -> ZUUL_COMMIT
2022 self.assertEqual(repo_shas[0], commit_C)
James E. Blairc6294a52012-08-17 10:19:48 -07002023
2024 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07002025 for c in repo.iter_commits(ref_B)]
James E. Blairbb1fe502014-03-04 10:15:06 -08002026 repo_shas = [c.hexsha for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07002027 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07002028 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002029 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08002030 self.assertEqual(repo_shas[0], commit_B)
2031
2032 repo_messages = [c.message.strip()
2033 for c in repo.iter_commits(ref_A)]
2034 repo_shas = [c.hexsha for c in repo.iter_commits(ref_A)]
2035 repo_messages.reverse()
2036 correct_messages = ['initial commit', 'A-1']
2037 self.assertEqual(repo_messages, correct_messages)
2038 self.assertEqual(repo_shas[0], commit_A)
2039
2040 self.assertNotEqual(ref_A, ref_B, ref_C)
2041 self.assertNotEqual(commit_A, commit_B, commit_C)
James E. Blair7f71c802012-08-22 13:04:32 -07002042
2043 def test_one_job_project(self):
2044 "Test that queueing works with one job"
2045 A = self.fake_gerrit.addFakeChange('org/one-job-project',
2046 'master', 'A')
2047 B = self.fake_gerrit.addFakeChange('org/one-job-project',
2048 'master', 'B')
2049 A.addApproval('CRVW', 2)
2050 B.addApproval('CRVW', 2)
2051 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2052 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2053 self.waitUntilSettled()
2054
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002055 self.assertEqual(A.data['status'], 'MERGED')
2056 self.assertEqual(A.reported, 2)
2057 self.assertEqual(B.data['status'], 'MERGED')
2058 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07002059
Antoine Musso80edd5a2013-02-13 15:37:53 +01002060 def test_job_from_templates_launched(self):
2061 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002062
Antoine Musso80edd5a2013-02-13 15:37:53 +01002063 A = self.fake_gerrit.addFakeChange(
2064 'org/templated-project', 'master', 'A')
2065 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2066 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01002067
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002068 self.assertEqual(self.getJobFromHistory('project-test1').result,
2069 'SUCCESS')
2070 self.assertEqual(self.getJobFromHistory('project-test2').result,
2071 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01002072
James E. Blair3e98c022013-12-16 15:25:38 -08002073 def test_layered_templates(self):
2074 "Test whether a job generated via a template can be launched"
2075
2076 A = self.fake_gerrit.addFakeChange(
2077 'org/layered-project', 'master', 'A')
2078 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2079 self.waitUntilSettled()
2080
2081 self.assertEqual(self.getJobFromHistory('project-test1').result,
2082 'SUCCESS')
2083 self.assertEqual(self.getJobFromHistory('project-test2').result,
2084 'SUCCESS')
James E. Blairaea6cf62013-12-16 15:38:12 -08002085 self.assertEqual(self.getJobFromHistory('layered-project-test3'
2086 ).result, 'SUCCESS')
2087 self.assertEqual(self.getJobFromHistory('layered-project-test4'
2088 ).result, 'SUCCESS')
James E. Blair12a92b12014-03-26 11:54:53 -07002089 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
2090 ).result, 'SUCCESS')
James E. Blair3e98c022013-12-16 15:25:38 -08002091 self.assertEqual(self.getJobFromHistory('project-test6').result,
2092 'SUCCESS')
2093
James E. Blaircaec0c52012-08-22 14:52:22 -07002094 def test_dependent_changes_dequeue(self):
2095 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002096
James E. Blaircaec0c52012-08-22 14:52:22 -07002097 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2098 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2099 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2100 A.addApproval('CRVW', 2)
2101 B.addApproval('CRVW', 2)
2102 C.addApproval('CRVW', 2)
2103
2104 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
2105 M1.setMerged()
2106
2107 # C -> B -> A -> M1
2108
2109 C.setDependsOn(B, 1)
2110 B.setDependsOn(A, 1)
2111 A.setDependsOn(M1, 1)
2112
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002113 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07002114
2115 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2116 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2117 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2118
2119 self.waitUntilSettled()
2120
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002121 self.assertEqual(A.data['status'], 'NEW')
2122 self.assertEqual(A.reported, 2)
2123 self.assertEqual(B.data['status'], 'NEW')
2124 self.assertEqual(B.reported, 2)
2125 self.assertEqual(C.data['status'], 'NEW')
2126 self.assertEqual(C.reported, 2)
2127 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07002128
James E. Blair972e3c72013-08-29 12:04:55 -07002129 def test_failing_dependent_changes(self):
2130 "Test that failing dependent patches are taken out of stream"
2131 self.worker.hold_jobs_in_build = True
2132 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2133 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2134 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2135 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2136 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
2137 A.addApproval('CRVW', 2)
2138 B.addApproval('CRVW', 2)
2139 C.addApproval('CRVW', 2)
2140 D.addApproval('CRVW', 2)
2141 E.addApproval('CRVW', 2)
2142
2143 # E, D -> C -> B, A
2144
2145 D.setDependsOn(C, 1)
2146 C.setDependsOn(B, 1)
2147
2148 self.worker.addFailTest('project-test1', B)
2149
2150 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2151 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2152 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2153 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2154 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2155
2156 self.waitUntilSettled()
2157 self.worker.release('.*-merge')
2158 self.waitUntilSettled()
2159 self.worker.release('.*-merge')
2160 self.waitUntilSettled()
2161 self.worker.release('.*-merge')
2162 self.waitUntilSettled()
2163 self.worker.release('.*-merge')
2164 self.waitUntilSettled()
2165 self.worker.release('.*-merge')
2166 self.waitUntilSettled()
2167
2168 self.worker.hold_jobs_in_build = False
2169 for build in self.builds:
2170 if build.parameters['ZUUL_CHANGE'] != '1':
2171 build.release()
2172 self.waitUntilSettled()
2173
2174 self.worker.release()
2175 self.waitUntilSettled()
2176
2177 self.assertEqual(A.data['status'], 'MERGED')
2178 self.assertEqual(A.reported, 2)
2179 self.assertEqual(B.data['status'], 'NEW')
2180 self.assertEqual(B.reported, 2)
2181 self.assertEqual(C.data['status'], 'NEW')
2182 self.assertEqual(C.reported, 2)
2183 self.assertEqual(D.data['status'], 'NEW')
2184 self.assertEqual(D.reported, 2)
2185 self.assertEqual(E.data['status'], 'MERGED')
2186 self.assertEqual(E.reported, 2)
2187 self.assertEqual(len(self.history), 18)
2188
James E. Blairec590122012-08-22 15:19:31 -07002189 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08002190 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07002191 # If it's dequeued more than once, we should see extra
2192 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07002193
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002194 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07002195 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2196 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2197 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2198 A.addApproval('CRVW', 2)
2199 B.addApproval('CRVW', 2)
2200 C.addApproval('CRVW', 2)
2201
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002202 self.worker.addFailTest('project1-test1', A)
2203 self.worker.addFailTest('project1-test2', A)
2204 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07002205
2206 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2207 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2208 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2209
2210 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07002211
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002212 self.assertEqual(len(self.builds), 1)
2213 self.assertEqual(self.builds[0].name, 'project1-merge')
2214 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairec590122012-08-22 15:19:31 -07002215
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002216 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002217 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002218 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002219 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002220 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07002221 self.waitUntilSettled()
2222
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002223 self.assertEqual(len(self.builds), 9)
2224 self.assertEqual(self.builds[0].name, 'project1-test1')
2225 self.assertEqual(self.builds[1].name, 'project1-test2')
2226 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
2227 self.assertEqual(self.builds[3].name, 'project1-test1')
2228 self.assertEqual(self.builds[4].name, 'project1-test2')
2229 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
2230 self.assertEqual(self.builds[6].name, 'project1-test1')
2231 self.assertEqual(self.builds[7].name, 'project1-test2')
2232 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07002233
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002234 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07002235 self.waitUntilSettled()
2236
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002237 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
2238 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07002239
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002240 self.worker.hold_jobs_in_build = False
2241 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07002242 self.waitUntilSettled()
2243
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002244 self.assertEqual(len(self.builds), 0)
2245 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07002246
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002247 self.assertEqual(A.data['status'], 'NEW')
2248 self.assertEqual(B.data['status'], 'MERGED')
2249 self.assertEqual(C.data['status'], 'MERGED')
2250 self.assertEqual(A.reported, 2)
2251 self.assertEqual(B.reported, 2)
2252 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07002253
2254 def test_nonvoting_job(self):
2255 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002256
James E. Blair4ec821f2012-08-23 15:28:28 -07002257 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
2258 'master', 'A')
2259 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002260 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07002261 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2262
2263 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07002264
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002265 self.assertEqual(A.data['status'], 'MERGED')
2266 self.assertEqual(A.reported, 2)
2267 self.assertEqual(
2268 self.getJobFromHistory('nonvoting-project-merge').result,
2269 'SUCCESS')
2270 self.assertEqual(
2271 self.getJobFromHistory('nonvoting-project-test1').result,
2272 'SUCCESS')
2273 self.assertEqual(
2274 self.getJobFromHistory('nonvoting-project-test2').result,
2275 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07002276
2277 def test_check_queue_success(self):
2278 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002279
James E. Blaire0487072012-08-29 17:38:31 -07002280 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2281 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2282
2283 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002284
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002285 self.assertEqual(A.data['status'], 'NEW')
2286 self.assertEqual(A.reported, 1)
2287 self.assertEqual(self.getJobFromHistory('project-merge').result,
2288 'SUCCESS')
2289 self.assertEqual(self.getJobFromHistory('project-test1').result,
2290 'SUCCESS')
2291 self.assertEqual(self.getJobFromHistory('project-test2').result,
2292 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07002293
2294 def test_check_queue_failure(self):
2295 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002296
James E. Blaire0487072012-08-29 17:38:31 -07002297 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002298 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07002299 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2300
2301 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07002302
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002303 self.assertEqual(A.data['status'], 'NEW')
2304 self.assertEqual(A.reported, 1)
2305 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07002306 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002307 self.assertEqual(self.getJobFromHistory('project-test1').result,
2308 'SUCCESS')
2309 self.assertEqual(self.getJobFromHistory('project-test2').result,
2310 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07002311
2312 def test_dependent_behind_dequeue(self):
2313 "test that dependent changes behind dequeued changes work"
2314 # This complicated test is a reproduction of a real life bug
2315 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07002316
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002317 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07002318 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2319 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2320 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2321 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2322 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
2323 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
2324 D.setDependsOn(C, 1)
2325 E.setDependsOn(D, 1)
2326 A.addApproval('CRVW', 2)
2327 B.addApproval('CRVW', 2)
2328 C.addApproval('CRVW', 2)
2329 D.addApproval('CRVW', 2)
2330 E.addApproval('CRVW', 2)
2331 F.addApproval('CRVW', 2)
2332
2333 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07002334
2335 # Change object re-use in the gerrit trigger is hidden if
2336 # changes are added in quick succession; waiting makes it more
2337 # like real life.
2338 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2339 self.waitUntilSettled()
2340 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2341 self.waitUntilSettled()
2342
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002343 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002344 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002345 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002346 self.waitUntilSettled()
2347
2348 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2349 self.waitUntilSettled()
2350 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2351 self.waitUntilSettled()
2352 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
2353 self.waitUntilSettled()
2354 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
2355 self.waitUntilSettled()
2356
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002357 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002358 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002359 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002360 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002361 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002362 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002363 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07002364 self.waitUntilSettled()
2365
2366 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07002367
2368 # Grab pointers to the jobs we want to release before
2369 # releasing any, because list indexes may change as
2370 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002371 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07002372 a.release()
2373 b.release()
2374 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07002375 self.waitUntilSettled()
2376
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002377 self.worker.hold_jobs_in_build = False
2378 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07002379 self.waitUntilSettled()
2380
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002381 self.assertEqual(A.data['status'], 'NEW')
2382 self.assertEqual(B.data['status'], 'MERGED')
2383 self.assertEqual(C.data['status'], 'MERGED')
2384 self.assertEqual(D.data['status'], 'MERGED')
2385 self.assertEqual(E.data['status'], 'MERGED')
2386 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07002387
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002388 self.assertEqual(A.reported, 2)
2389 self.assertEqual(B.reported, 2)
2390 self.assertEqual(C.reported, 2)
2391 self.assertEqual(D.reported, 2)
2392 self.assertEqual(E.reported, 2)
2393 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07002394
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002395 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
2396 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07002397
2398 def test_merger_repack(self):
2399 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002400
James E. Blair05fed602012-09-07 12:45:24 -07002401 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2402 A.addApproval('CRVW', 2)
2403 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2404 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002405 self.assertEqual(self.getJobFromHistory('project-merge').result,
2406 'SUCCESS')
2407 self.assertEqual(self.getJobFromHistory('project-test1').result,
2408 'SUCCESS')
2409 self.assertEqual(self.getJobFromHistory('project-test2').result,
2410 'SUCCESS')
2411 self.assertEqual(A.data['status'], 'MERGED')
2412 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07002413 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07002414 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07002415
Monty Taylorbc758832013-06-17 17:22:42 -04002416 path = os.path.join(self.git_root, "org/project")
2417 print repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07002418
2419 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2420 A.addApproval('CRVW', 2)
2421 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2422 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002423 self.assertEqual(self.getJobFromHistory('project-merge').result,
2424 'SUCCESS')
2425 self.assertEqual(self.getJobFromHistory('project-test1').result,
2426 'SUCCESS')
2427 self.assertEqual(self.getJobFromHistory('project-test2').result,
2428 'SUCCESS')
2429 self.assertEqual(A.data['status'], 'MERGED')
2430 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002431
James E. Blair4886f282012-11-15 09:27:33 -08002432 def test_merger_repack_large_change(self):
2433 "Test that the merger works with large changes after a repack"
2434 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08002435 # This test assumes the repo is already cloned; make sure it is
2436 url = self.sched.triggers['gerrit'].getGitUrl(
2437 self.sched.layout.projects['org/project1'])
James E. Blair4076e2b2014-01-28 12:42:20 -08002438 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08002439 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2440 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04002441 path = os.path.join(self.upstream_root, "org/project1")
2442 print repack_repo(path)
2443 path = os.path.join(self.git_root, "org/project1")
2444 print repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08002445
2446 A.addApproval('CRVW', 2)
2447 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2448 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002449 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2450 'SUCCESS')
2451 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2452 'SUCCESS')
2453 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2454 'SUCCESS')
2455 self.assertEqual(A.data['status'], 'MERGED')
2456 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08002457
James E. Blair7ee88a22012-09-12 18:59:31 +02002458 def test_nonexistent_job(self):
2459 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002460 # Set to the state immediately after a restart
2461 self.resetGearmanServer()
2462 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02002463
2464 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2465 A.addApproval('CRVW', 2)
2466 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2467 # There may be a thread about to report a lost change
2468 while A.reported < 2:
2469 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002470 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002471 self.assertFalse(job_names)
2472 self.assertEqual(A.data['status'], 'NEW')
2473 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02002474 self.assertEmptyQueues()
2475
2476 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002477 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02002478 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2479 A.addApproval('CRVW', 2)
2480 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2481 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002482 self.assertEqual(self.getJobFromHistory('project-merge').result,
2483 'SUCCESS')
2484 self.assertEqual(self.getJobFromHistory('project-test1').result,
2485 'SUCCESS')
2486 self.assertEqual(self.getJobFromHistory('project-test2').result,
2487 'SUCCESS')
2488 self.assertEqual(A.data['status'], 'MERGED')
2489 self.assertEqual(A.reported, 2)
James E. Blairf62d4282012-12-31 17:01:50 -08002490
2491 def test_single_nonexistent_post_job(self):
2492 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08002493 e = {
2494 "type": "ref-updated",
2495 "submitter": {
2496 "name": "User Name",
2497 },
2498 "refUpdate": {
2499 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
2500 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
2501 "refName": "master",
2502 "project": "org/project",
2503 }
2504 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002505 # Set to the state immediately after a restart
2506 self.resetGearmanServer()
2507 self.launcher.negative_function_cache_ttl = 0
2508
James E. Blairf62d4282012-12-31 17:01:50 -08002509 self.fake_gerrit.addEvent(e)
2510 self.waitUntilSettled()
2511
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002512 self.assertEqual(len(self.history), 0)
James E. Blair2fa50962013-01-30 21:50:41 -08002513
2514 def test_new_patchset_dequeues_old(self):
2515 "Test that a new patchset causes the old to be dequeued"
2516 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002517 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002518 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2519 M.setMerged()
2520
2521 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2522 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2523 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2524 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2525 A.addApproval('CRVW', 2)
2526 B.addApproval('CRVW', 2)
2527 C.addApproval('CRVW', 2)
2528 D.addApproval('CRVW', 2)
2529
2530 C.setDependsOn(B, 1)
2531 B.setDependsOn(A, 1)
2532 A.setDependsOn(M, 1)
2533
2534 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2535 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2536 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2537 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2538 self.waitUntilSettled()
2539
2540 B.addPatchset()
2541 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2542 self.waitUntilSettled()
2543
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002544 self.worker.hold_jobs_in_build = False
2545 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002546 self.waitUntilSettled()
2547
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002548 self.assertEqual(A.data['status'], 'MERGED')
2549 self.assertEqual(A.reported, 2)
2550 self.assertEqual(B.data['status'], 'NEW')
2551 self.assertEqual(B.reported, 2)
2552 self.assertEqual(C.data['status'], 'NEW')
2553 self.assertEqual(C.reported, 2)
2554 self.assertEqual(D.data['status'], 'MERGED')
2555 self.assertEqual(D.reported, 2)
2556 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08002557
Arx Cruzb1b010d2013-10-28 19:49:59 -02002558 def test_zuul_url_return(self):
2559 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08002560 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Arx Cruzb1b010d2013-10-28 19:49:59 -02002561 self.worker.hold_jobs_in_build = True
2562
2563 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2564 A.addApproval('CRVW', 2)
2565 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2566 self.waitUntilSettled()
2567
2568 self.assertEqual(len(self.builds), 1)
2569 for build in self.builds:
2570 self.assertTrue('ZUUL_URL' in build.parameters)
2571
2572 self.worker.hold_jobs_in_build = False
2573 self.worker.release()
2574 self.waitUntilSettled()
2575
James E. Blair2fa50962013-01-30 21:50:41 -08002576 def test_new_patchset_dequeues_old_on_head(self):
2577 "Test that a new patchset causes the old to be dequeued (at head)"
2578 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002579 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002580 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2581 M.setMerged()
2582 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2583 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2584 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2585 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2586 A.addApproval('CRVW', 2)
2587 B.addApproval('CRVW', 2)
2588 C.addApproval('CRVW', 2)
2589 D.addApproval('CRVW', 2)
2590
2591 C.setDependsOn(B, 1)
2592 B.setDependsOn(A, 1)
2593 A.setDependsOn(M, 1)
2594
2595 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2596 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2597 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2598 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2599 self.waitUntilSettled()
2600
2601 A.addPatchset()
2602 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2603 self.waitUntilSettled()
2604
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002605 self.worker.hold_jobs_in_build = False
2606 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002607 self.waitUntilSettled()
2608
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002609 self.assertEqual(A.data['status'], 'NEW')
2610 self.assertEqual(A.reported, 2)
2611 self.assertEqual(B.data['status'], 'NEW')
2612 self.assertEqual(B.reported, 2)
2613 self.assertEqual(C.data['status'], 'NEW')
2614 self.assertEqual(C.reported, 2)
2615 self.assertEqual(D.data['status'], 'MERGED')
2616 self.assertEqual(D.reported, 2)
2617 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002618
2619 def test_new_patchset_dequeues_old_without_dependents(self):
2620 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002621 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002622 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2623 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2624 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2625 A.addApproval('CRVW', 2)
2626 B.addApproval('CRVW', 2)
2627 C.addApproval('CRVW', 2)
2628
2629 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2630 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2631 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2632 self.waitUntilSettled()
2633
2634 B.addPatchset()
2635 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2636 self.waitUntilSettled()
2637
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002638 self.worker.hold_jobs_in_build = False
2639 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002640 self.waitUntilSettled()
2641
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002642 self.assertEqual(A.data['status'], 'MERGED')
2643 self.assertEqual(A.reported, 2)
2644 self.assertEqual(B.data['status'], 'NEW')
2645 self.assertEqual(B.reported, 2)
2646 self.assertEqual(C.data['status'], 'MERGED')
2647 self.assertEqual(C.reported, 2)
2648 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002649
2650 def test_new_patchset_dequeues_old_independent_queue(self):
2651 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002652 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002653 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2654 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2655 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2656 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2657 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2658 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2659 self.waitUntilSettled()
2660
2661 B.addPatchset()
2662 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2663 self.waitUntilSettled()
2664
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002665 self.worker.hold_jobs_in_build = False
2666 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002667 self.waitUntilSettled()
2668
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002669 self.assertEqual(A.data['status'], 'NEW')
2670 self.assertEqual(A.reported, 1)
2671 self.assertEqual(B.data['status'], 'NEW')
2672 self.assertEqual(B.reported, 1)
2673 self.assertEqual(C.data['status'], 'NEW')
2674 self.assertEqual(C.reported, 1)
2675 self.assertEqual(len(self.history), 10)
2676 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002677
James E. Blair18c64442014-03-18 10:14:45 -07002678 def test_noop_job(self):
2679 "Test that the internal noop job works"
2680 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
2681 A.addApproval('CRVW', 2)
2682 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2683 self.waitUntilSettled()
2684
2685 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2686 self.assertTrue(self.sched._areAllBuildsComplete())
2687 self.assertEqual(len(self.history), 0)
2688 self.assertEqual(A.data['status'], 'MERGED')
2689 self.assertEqual(A.reported, 2)
2690
James E. Blair7d0dedc2013-02-21 17:26:09 -08002691 def test_zuul_refs(self):
2692 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002693 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002694 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2695 M1.setMerged()
2696 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2697 M2.setMerged()
2698
2699 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2700 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2701 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2702 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2703 A.addApproval('CRVW', 2)
2704 B.addApproval('CRVW', 2)
2705 C.addApproval('CRVW', 2)
2706 D.addApproval('CRVW', 2)
2707 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2708 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2709 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2710 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2711
2712 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002713 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002714 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002715 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002716 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002717 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002718 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002719 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002720 self.waitUntilSettled()
2721
James E. Blair7d0dedc2013-02-21 17:26:09 -08002722 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002723 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002724 if x.parameters['ZUUL_CHANGE'] == '3':
2725 a_zref = x.parameters['ZUUL_REF']
2726 if x.parameters['ZUUL_CHANGE'] == '4':
2727 b_zref = x.parameters['ZUUL_REF']
2728 if x.parameters['ZUUL_CHANGE'] == '5':
2729 c_zref = x.parameters['ZUUL_REF']
2730 if x.parameters['ZUUL_CHANGE'] == '6':
2731 d_zref = x.parameters['ZUUL_REF']
2732
2733 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002734 self.assertIsNotNone(a_zref)
2735 self.assertIsNotNone(b_zref)
2736 self.assertIsNotNone(c_zref)
2737 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002738
2739 # And they should all be different
2740 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002741 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002742
2743 # a ref should have a, not b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002744 self.assertTrue(self.ref_has_change(a_zref, A))
2745 self.assertFalse(self.ref_has_change(a_zref, B))
2746 self.assertFalse(self.ref_has_change(a_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002747
2748 # b ref should have a and b, and should not be in project2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002749 self.assertTrue(self.ref_has_change(b_zref, A))
2750 self.assertTrue(self.ref_has_change(b_zref, B))
2751 self.assertFalse(self.ref_has_change(b_zref, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002752
2753 # c ref should have a and b in 1, c in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002754 self.assertTrue(self.ref_has_change(c_zref, A))
2755 self.assertTrue(self.ref_has_change(c_zref, B))
2756 self.assertTrue(self.ref_has_change(c_zref, C))
2757 self.assertFalse(self.ref_has_change(c_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002758
2759 # d ref should have a and b in 1, c and d in 2
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002760 self.assertTrue(self.ref_has_change(d_zref, A))
2761 self.assertTrue(self.ref_has_change(d_zref, B))
2762 self.assertTrue(self.ref_has_change(d_zref, C))
2763 self.assertTrue(self.ref_has_change(d_zref, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002764
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002765 self.worker.hold_jobs_in_build = False
2766 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002767 self.waitUntilSettled()
2768
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002769 self.assertEqual(A.data['status'], 'MERGED')
2770 self.assertEqual(A.reported, 2)
2771 self.assertEqual(B.data['status'], 'MERGED')
2772 self.assertEqual(B.reported, 2)
2773 self.assertEqual(C.data['status'], 'MERGED')
2774 self.assertEqual(C.reported, 2)
2775 self.assertEqual(D.data['status'], 'MERGED')
2776 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002777
James E. Blairc053d022014-01-22 14:57:33 -08002778 def test_required_approval_check_and_gate(self):
2779 "Test required-approval triggers both check and gate"
2780 self.config.set('zuul', 'layout_config',
2781 'tests/fixtures/layout-require-approval.yaml')
2782 self.sched.reconfigure(self.config)
2783 self.registerJobs()
2784
2785 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2786 A.addApproval('CRVW', 2)
2787 # Add a too-old +1
2788 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2789
2790 aprv = A.addApproval('APRV', 1)
2791 self.fake_gerrit.addEvent(aprv)
2792 self.waitUntilSettled()
2793 # Should have run a check job
2794 self.assertEqual(len(self.history), 1)
2795 self.assertEqual(self.history[0].name, 'project-check')
2796
2797 # Report the result of that check job (overrides previous vrfy)
2798 # Skynet alert: this should trigger a gate job now that
2799 # all reqs are met
2800 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2801 self.waitUntilSettled()
2802 self.assertEqual(len(self.history), 2)
2803 self.assertEqual(self.history[1].name, 'project-gate')
2804
2805 def test_required_approval_newer(self):
2806 "Test required-approval newer trigger parameter"
2807 self.config.set('zuul', 'layout_config',
2808 'tests/fixtures/layout-require-approval.yaml')
2809 self.sched.reconfigure(self.config)
2810 self.registerJobs()
2811
2812 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2813 A.addApproval('CRVW', 2)
2814 aprv = A.addApproval('APRV', 1)
2815 self.fake_gerrit.addEvent(aprv)
2816 self.waitUntilSettled()
2817 # No +1 from Jenkins so should not be enqueued
2818 self.assertEqual(len(self.history), 0)
2819
2820 # Add a too-old +1, should trigger check but not gate
2821 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2822 self.fake_gerrit.addEvent(aprv)
2823 self.waitUntilSettled()
2824 self.assertEqual(len(self.history), 1)
2825 self.assertEqual(self.history[0].name, 'project-check')
2826
2827 # Add a recent +1
2828 self.fake_gerrit.addEvent(A.addApproval('VRFY', 1))
2829 self.fake_gerrit.addEvent(aprv)
2830 self.waitUntilSettled()
2831 self.assertEqual(len(self.history), 2)
2832 self.assertEqual(self.history[1].name, 'project-gate')
2833
2834 def test_required_approval_older(self):
2835 "Test required-approval older trigger parameter"
2836 self.config.set('zuul', 'layout_config',
2837 'tests/fixtures/layout-require-approval.yaml')
2838 self.sched.reconfigure(self.config)
2839 self.registerJobs()
2840
2841 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2842 crvw = A.addApproval('CRVW', 2)
2843 self.fake_gerrit.addEvent(crvw)
2844 self.waitUntilSettled()
2845 # No +1 from Jenkins so should not be enqueued
2846 self.assertEqual(len(self.history), 0)
2847
2848 # Add an old +1 and trigger check with a comment
2849 A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60)
2850 self.fake_gerrit.addEvent(crvw)
2851 self.waitUntilSettled()
2852 self.assertEqual(len(self.history), 1)
2853 self.assertEqual(self.history[0].name, 'project-check')
2854
2855 # Add a recent +1 and make sure nothing changes
2856 A.addApproval('VRFY', 1)
2857 self.fake_gerrit.addEvent(crvw)
2858 self.waitUntilSettled()
2859 self.assertEqual(len(self.history), 1)
2860
2861 # The last thing we did was query a change then do nothing
2862 # with a pipeline, so it will be in the cache; clean it up so
2863 # it does not fail the test.
2864 for pipeline in self.sched.layout.pipelines.values():
2865 pipeline.trigger.maintainCache([])
2866
James E. Blair4a28a882013-08-23 15:17:33 -07002867 def test_rerun_on_error(self):
2868 "Test that if a worker fails to run a job, it is run again"
2869 self.worker.hold_jobs_in_build = True
2870 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2871 A.addApproval('CRVW', 2)
2872 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2873 self.waitUntilSettled()
2874
2875 self.builds[0].run_error = True
2876 self.worker.hold_jobs_in_build = False
2877 self.worker.release()
2878 self.waitUntilSettled()
2879 self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1)
2880 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2881
James E. Blair412e5582013-04-22 15:50:12 -07002882 def test_statsd(self):
2883 "Test each of the statsd methods used in the scheduler"
2884 import extras
2885 statsd = extras.try_import('statsd.statsd')
2886 statsd.incr('test-incr')
2887 statsd.timing('test-timing', 3)
2888 statsd.gauge('test-guage', 12)
2889 self.assertReportedStat('test-incr', '1|c')
2890 self.assertReportedStat('test-timing', '3|ms')
2891 self.assertReportedStat('test-guage', '12|g')
2892
James E. Blairdad52252014-02-07 16:59:17 -08002893 def test_stuck_job_cleanup(self):
2894 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07002895 # This job won't be registered at startup because it is not in
2896 # the standard layout, but we need it to already be registerd
2897 # for when we reconfigure, as that is when Zuul will attempt
2898 # to run the new job.
2899 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002900 self.gearman_server.hold_jobs_in_queue = True
2901 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2902 A.addApproval('CRVW', 2)
2903 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2904 self.waitUntilSettled()
2905 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2906
2907 self.config.set('zuul', 'layout_config',
2908 'tests/fixtures/layout-no-jobs.yaml')
2909 self.sched.reconfigure(self.config)
2910 self.waitUntilSettled()
2911
James E. Blair18c64442014-03-18 10:14:45 -07002912 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002913 self.waitUntilSettled()
2914 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2915 self.assertTrue(self.sched._areAllBuildsComplete())
2916
2917 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002918 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002919 self.assertEqual(self.history[0].result, 'SUCCESS')
2920
James E. Blair70c71582013-03-06 08:50:50 -08002921 def test_file_jobs(self):
2922 "Test that file jobs run only when appropriate"
2923 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2924 A.addPatchset(['pip-requires'])
2925 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2926 A.addApproval('CRVW', 2)
2927 B.addApproval('CRVW', 2)
2928 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2929 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2930 self.waitUntilSettled()
2931
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002932 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002933 if x.name == 'project-testfile']
2934
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002935 self.assertEqual(len(testfile_jobs), 1)
2936 self.assertEqual(testfile_jobs[0].changes, '1,2')
2937 self.assertEqual(A.data['status'], 'MERGED')
2938 self.assertEqual(A.reported, 2)
2939 self.assertEqual(B.data['status'], 'MERGED')
2940 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002941
2942 def test_test_config(self):
2943 "Test that we can test the config"
2944 sched = zuul.scheduler.Scheduler()
James E. Blair6c358e72013-07-29 17:06:47 -07002945 sched.registerTrigger(None, 'gerrit')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002946 sched.registerTrigger(None, 'timer')
James E. Blair3c5e5b52013-04-26 11:17:03 -07002947 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002948
2949 def test_build_description(self):
2950 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002951 self.worker.registerFunction('set_description:' +
2952 self.worker.worker_id)
2953
2954 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2955 A.addApproval('CRVW', 2)
2956 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2957 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002958 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002959 self.log.debug("Description: %s" % desc)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002960 self.assertTrue(re.search("Branch.*master", desc))
2961 self.assertTrue(re.search("Pipeline.*gate", desc))
2962 self.assertTrue(re.search("project-merge.*SUCCESS", desc))
2963 self.assertTrue(re.search("project-test1.*SUCCESS", desc))
2964 self.assertTrue(re.search("project-test2.*SUCCESS", desc))
2965 self.assertTrue(re.search("Reported result.*SUCCESS", desc))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002966
James E. Blair64ed6f22013-07-10 14:07:23 -07002967 def test_queue_precedence(self):
2968 "Test that queue precedence works"
2969
2970 self.gearman_server.hold_jobs_in_queue = True
James E. Blair8de58bd2013-07-18 16:23:33 -07002971 self.worker.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002972 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2973 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2974 A.addApproval('CRVW', 2)
2975 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2976
2977 self.waitUntilSettled()
2978 self.gearman_server.hold_jobs_in_queue = False
2979 self.gearman_server.release()
2980 self.waitUntilSettled()
2981
James E. Blair8de58bd2013-07-18 16:23:33 -07002982 # Run one build at a time to ensure non-race order:
2983 for x in range(6):
2984 self.release(self.builds[0])
2985 self.waitUntilSettled()
2986 self.worker.hold_jobs_in_build = False
2987 self.waitUntilSettled()
2988
James E. Blair64ed6f22013-07-10 14:07:23 -07002989 self.log.debug(self.history)
2990 self.assertEqual(self.history[0].pipeline, 'gate')
2991 self.assertEqual(self.history[1].pipeline, 'check')
2992 self.assertEqual(self.history[2].pipeline, 'gate')
2993 self.assertEqual(self.history[3].pipeline, 'gate')
2994 self.assertEqual(self.history[4].pipeline, 'check')
2995 self.assertEqual(self.history[5].pipeline, 'check')
2996
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04002997 def test_json_status(self, compressed=False):
James E. Blair1843a552013-07-03 14:19:52 -07002998 "Test that we can retrieve JSON status info"
2999 self.worker.hold_jobs_in_build = True
3000 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3001 A.addApproval('CRVW', 2)
3002 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3003 self.waitUntilSettled()
3004
3005 port = self.webapp.server.socket.getsockname()[1]
3006
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04003007 req = urllib2.Request("http://localhost:%s/status.json" % port)
3008 if compressed:
3009 req.add_header("accept-encoding", "gzip")
3010 f = urllib2.urlopen(req)
James E. Blair1843a552013-07-03 14:19:52 -07003011 data = f.read()
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04003012 if compressed:
3013 gz = gzip.GzipFile(fileobj=StringIO(data))
3014 data = gz.read()
James E. Blair1843a552013-07-03 14:19:52 -07003015
3016 self.worker.hold_jobs_in_build = False
3017 self.worker.release()
3018 self.waitUntilSettled()
3019
3020 data = json.loads(data)
3021 status_jobs = set()
3022 for p in data['pipelines']:
3023 for q in p['change_queues']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08003024 if q['dependent']:
3025 self.assertEqual(q['window'], 20)
3026 else:
3027 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07003028 for head in q['heads']:
3029 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08003030 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003031 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07003032 for job in change['jobs']:
3033 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003034 self.assertIn('project-merge', status_jobs)
3035 self.assertIn('project-test1', status_jobs)
3036 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07003037
Yuriy Taradaya6d452f2014-04-16 12:36:20 +04003038 def test_json_status_gzip(self):
3039 self.test_json_status(True)
3040
James E. Blairc3d428e2013-12-03 15:06:48 -08003041 def test_merging_queues(self):
3042 "Test that transitively-connected change queues are merged"
3043 self.config.set('zuul', 'layout_config',
3044 'tests/fixtures/layout-merge-queues.yaml')
3045 self.sched.reconfigure(self.config)
3046 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
3047
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003048 def test_node_label(self):
3049 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003050 self.worker.registerFunction('build:node-project-test1:debian')
3051
3052 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
3053 A.addApproval('CRVW', 2)
3054 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3055 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07003056
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003057 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3058 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3059 'debian')
3060 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07003061
3062 def test_live_reconfiguration(self):
3063 "Test that live reconfiguration works"
3064 self.worker.hold_jobs_in_build = True
3065 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3066 A.addApproval('CRVW', 2)
3067 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3068 self.waitUntilSettled()
3069
3070 self.sched.reconfigure(self.config)
3071
3072 self.worker.hold_jobs_in_build = False
3073 self.worker.release()
3074 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003075 self.assertEqual(self.getJobFromHistory('project-merge').result,
3076 'SUCCESS')
3077 self.assertEqual(self.getJobFromHistory('project-test1').result,
3078 'SUCCESS')
3079 self.assertEqual(self.getJobFromHistory('project-test2').result,
3080 'SUCCESS')
3081 self.assertEqual(A.data['status'], 'MERGED')
3082 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07003083
James E. Blaire712d9f2013-07-31 11:40:11 -07003084 def test_live_reconfiguration_functions(self):
3085 "Test live reconfiguration with a custom function"
3086 self.worker.registerFunction('build:node-project-test1:debian')
3087 self.worker.registerFunction('build:node-project-test1:wheezy')
3088 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
3089 A.addApproval('CRVW', 2)
3090 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3091 self.waitUntilSettled()
3092
3093 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3094 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3095 'debian')
3096 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
3097
3098 self.config.set('zuul', 'layout_config',
3099 'tests/fixtures/layout-live-'
3100 'reconfiguration-functions.yaml')
3101 self.sched.reconfigure(self.config)
3102 self.worker.build_history = []
3103
3104 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
3105 B.addApproval('CRVW', 2)
3106 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3107 self.waitUntilSettled()
3108
3109 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3110 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3111 'wheezy')
3112 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
3113
James E. Blair287c06d2013-07-24 10:39:30 -07003114 def test_delayed_repo_init(self):
3115 self.config.set('zuul', 'layout_config',
3116 'tests/fixtures/layout-delayed-repo-init.yaml')
3117 self.sched.reconfigure(self.config)
3118
3119 self.init_repo("org/new-project")
3120 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
3121
3122 A.addApproval('CRVW', 2)
3123 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3124 self.waitUntilSettled()
3125 self.assertEqual(self.getJobFromHistory('project-merge').result,
3126 'SUCCESS')
3127 self.assertEqual(self.getJobFromHistory('project-test1').result,
3128 'SUCCESS')
3129 self.assertEqual(self.getJobFromHistory('project-test2').result,
3130 'SUCCESS')
3131 self.assertEqual(A.data['status'], 'MERGED')
3132 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003133
Clark Boylan6dbbc482013-10-18 10:57:31 -07003134 def test_repo_deleted(self):
3135 self.config.set('zuul', 'layout_config',
3136 'tests/fixtures/layout-repo-deleted.yaml')
3137 self.sched.reconfigure(self.config)
3138
3139 self.init_repo("org/delete-project")
3140 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
3141
3142 A.addApproval('CRVW', 2)
3143 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3144 self.waitUntilSettled()
3145 self.assertEqual(self.getJobFromHistory('project-merge').result,
3146 'SUCCESS')
3147 self.assertEqual(self.getJobFromHistory('project-test1').result,
3148 'SUCCESS')
3149 self.assertEqual(self.getJobFromHistory('project-test2').result,
3150 'SUCCESS')
3151 self.assertEqual(A.data['status'], 'MERGED')
3152 self.assertEqual(A.reported, 2)
3153
3154 # Delete org/new-project zuul repo. Should be recloned.
3155 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
3156
3157 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
3158
3159 B.addApproval('CRVW', 2)
3160 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3161 self.waitUntilSettled()
3162 self.assertEqual(self.getJobFromHistory('project-merge').result,
3163 'SUCCESS')
3164 self.assertEqual(self.getJobFromHistory('project-test1').result,
3165 'SUCCESS')
3166 self.assertEqual(self.getJobFromHistory('project-test2').result,
3167 'SUCCESS')
3168 self.assertEqual(B.data['status'], 'MERGED')
3169 self.assertEqual(B.reported, 2)
3170
James E. Blair63bb0ef2013-07-29 17:14:51 -07003171 def test_timer(self):
3172 "Test that a periodic job is triggered"
3173 self.worker.hold_jobs_in_build = True
3174 self.config.set('zuul', 'layout_config',
3175 'tests/fixtures/layout-timer.yaml')
3176 self.sched.reconfigure(self.config)
3177 self.registerJobs()
3178
3179 start = time.time()
3180 failed = True
3181 while ((time.time() - start) < 30):
3182 if len(self.builds) == 2:
3183 failed = False
3184 break
3185 else:
3186 time.sleep(1)
3187
3188 if failed:
3189 raise Exception("Expected jobs never ran")
3190
3191 self.waitUntilSettled()
3192 port = self.webapp.server.socket.getsockname()[1]
3193
3194 f = urllib.urlopen("http://localhost:%s/status.json" % port)
3195 data = f.read()
3196
3197 self.worker.hold_jobs_in_build = False
3198 self.worker.release()
3199 self.waitUntilSettled()
3200
3201 self.assertEqual(self.getJobFromHistory(
3202 'project-bitrot-stable-old').result, 'SUCCESS')
3203 self.assertEqual(self.getJobFromHistory(
3204 'project-bitrot-stable-older').result, 'SUCCESS')
3205
3206 data = json.loads(data)
3207 status_jobs = set()
3208 for p in data['pipelines']:
3209 for q in p['change_queues']:
3210 for head in q['heads']:
3211 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07003212 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003213 for job in change['jobs']:
3214 status_jobs.add(job['name'])
3215 self.assertIn('project-bitrot-stable-old', status_jobs)
3216 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003217
James E. Blair4f6033c2014-03-27 15:49:09 -07003218 def test_idle(self):
3219 "Test that frequent periodic jobs work"
3220 self.worker.hold_jobs_in_build = True
3221 self.config.set('zuul', 'layout_config',
3222 'tests/fixtures/layout-idle.yaml')
3223 self.sched.reconfigure(self.config)
3224 self.registerJobs()
3225
3226 # The pipeline triggers every second, so we should have seen
3227 # several by now.
3228 time.sleep(5)
3229 self.waitUntilSettled()
3230 self.assertEqual(len(self.builds), 2)
3231 self.worker.release('.*')
3232 self.waitUntilSettled()
3233 self.assertEqual(len(self.builds), 0)
3234 self.assertEqual(len(self.history), 2)
3235
3236 time.sleep(5)
3237 self.waitUntilSettled()
3238 self.assertEqual(len(self.builds), 2)
3239 self.assertEqual(len(self.history), 2)
3240 self.worker.release('.*')
3241 self.waitUntilSettled()
3242 self.assertEqual(len(self.builds), 0)
3243 self.assertEqual(len(self.history), 4)
3244
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003245 def test_check_smtp_pool(self):
3246 self.config.set('zuul', 'layout_config',
3247 'tests/fixtures/layout-smtp.yaml')
3248 self.sched.reconfigure(self.config)
3249
3250 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3251 self.waitUntilSettled()
3252
3253 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3254 self.waitUntilSettled()
3255
James E. Blairff80a2f2013-12-27 13:24:06 -08003256 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003257
3258 # A.messages only holds what FakeGerrit places in it. Thus we
3259 # work on the knowledge of what the first message should be as
3260 # it is only configured to go to SMTP.
3261
3262 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003263 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003264 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003265 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003266 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003267 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003268
3269 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003270 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003271 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003272 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003273 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003274 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003275
James E. Blaire5910202013-12-27 09:50:31 -08003276 def test_timer_smtp(self):
3277 "Test that a periodic job is triggered"
3278 self.config.set('zuul', 'layout_config',
3279 'tests/fixtures/layout-timer-smtp.yaml')
3280 self.sched.reconfigure(self.config)
3281 self.registerJobs()
3282
3283 start = time.time()
3284 failed = True
3285 while ((time.time() - start) < 30):
3286 if len(self.history) == 2:
3287 failed = False
3288 break
3289 else:
3290 time.sleep(1)
3291
3292 if failed:
3293 raise Exception("Expected jobs never ran")
3294
3295 self.waitUntilSettled()
3296
3297 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
James E. Blairad28e912013-11-27 10:43:22 -08003315 def test_client_enqueue(self):
3316 "Test that the RPC client can enqueue a change"
3317 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3318 A.addApproval('CRVW', 2)
3319 A.addApproval('APRV', 1)
3320
3321 client = zuul.rpcclient.RPCClient('127.0.0.1',
3322 self.gearman_server.port)
3323 r = client.enqueue(pipeline='gate',
3324 project='org/project',
3325 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003326 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003327 self.waitUntilSettled()
3328 self.assertEqual(self.getJobFromHistory('project-merge').result,
3329 'SUCCESS')
3330 self.assertEqual(self.getJobFromHistory('project-test1').result,
3331 'SUCCESS')
3332 self.assertEqual(self.getJobFromHistory('project-test2').result,
3333 'SUCCESS')
3334 self.assertEqual(A.data['status'], 'MERGED')
3335 self.assertEqual(A.reported, 2)
3336 self.assertEqual(r, True)
3337
3338 def test_client_enqueue_negative(self):
3339 "Test that the RPC client returns errors"
3340 client = zuul.rpcclient.RPCClient('127.0.0.1',
3341 self.gearman_server.port)
3342 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3343 "Invalid project"):
3344 r = client.enqueue(pipeline='gate',
3345 project='project-does-not-exist',
3346 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003347 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003348 client.shutdown()
3349 self.assertEqual(r, False)
3350
3351 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3352 "Invalid pipeline"):
3353 r = client.enqueue(pipeline='pipeline-does-not-exist',
3354 project='org/project',
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 trigger"):
3362 r = client.enqueue(pipeline='gate',
3363 project='org/project',
3364 trigger='trigger-does-not-exist',
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 change"):
3371 r = client.enqueue(pipeline='gate',
3372 project='org/project',
3373 trigger='gerrit',
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 self.waitUntilSettled()
3379 self.assertEqual(len(self.history), 0)
3380 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003381
3382 def test_client_promote(self):
3383 "Test that the RPC client can promote a change"
3384 self.worker.hold_jobs_in_build = True
3385 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3386 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3387 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3388 A.addApproval('CRVW', 2)
3389 B.addApproval('CRVW', 2)
3390 C.addApproval('CRVW', 2)
3391
3392 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3393 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3394 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3395
3396 self.waitUntilSettled()
3397
Sean Daguef39b9ca2014-01-10 21:34:35 -05003398 items = self.sched.layout.pipelines['gate'].getAllItems()
3399 enqueue_times = {}
3400 for item in items:
3401 enqueue_times[str(item.change)] = item.enqueue_time
3402
James E. Blair36658cf2013-12-06 17:53:48 -08003403 client = zuul.rpcclient.RPCClient('127.0.0.1',
3404 self.gearman_server.port)
3405 r = client.promote(pipeline='gate',
3406 change_ids=['2,1', '3,1'])
3407
Sean Daguef39b9ca2014-01-10 21:34:35 -05003408 # ensure that enqueue times are durable
3409 items = self.sched.layout.pipelines['gate'].getAllItems()
3410 for item in items:
3411 self.assertEqual(
3412 enqueue_times[str(item.change)], item.enqueue_time)
3413
James E. Blair78acec92014-02-06 07:11:32 -08003414 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003415 self.worker.release('.*-merge')
3416 self.waitUntilSettled()
3417 self.worker.release('.*-merge')
3418 self.waitUntilSettled()
3419 self.worker.release('.*-merge')
3420 self.waitUntilSettled()
3421
3422 self.assertEqual(len(self.builds), 6)
3423 self.assertEqual(self.builds[0].name, 'project-test1')
3424 self.assertEqual(self.builds[1].name, 'project-test2')
3425 self.assertEqual(self.builds[2].name, 'project-test1')
3426 self.assertEqual(self.builds[3].name, 'project-test2')
3427 self.assertEqual(self.builds[4].name, 'project-test1')
3428 self.assertEqual(self.builds[5].name, 'project-test2')
3429
3430 self.assertTrue(self.job_has_changes(self.builds[0], B))
3431 self.assertFalse(self.job_has_changes(self.builds[0], A))
3432 self.assertFalse(self.job_has_changes(self.builds[0], C))
3433
3434 self.assertTrue(self.job_has_changes(self.builds[2], B))
3435 self.assertTrue(self.job_has_changes(self.builds[2], C))
3436 self.assertFalse(self.job_has_changes(self.builds[2], A))
3437
3438 self.assertTrue(self.job_has_changes(self.builds[4], B))
3439 self.assertTrue(self.job_has_changes(self.builds[4], C))
3440 self.assertTrue(self.job_has_changes(self.builds[4], A))
3441
3442 self.worker.release()
3443 self.waitUntilSettled()
3444
3445 self.assertEqual(A.data['status'], 'MERGED')
3446 self.assertEqual(A.reported, 2)
3447 self.assertEqual(B.data['status'], 'MERGED')
3448 self.assertEqual(B.reported, 2)
3449 self.assertEqual(C.data['status'], 'MERGED')
3450 self.assertEqual(C.reported, 2)
3451
3452 client.shutdown()
3453 self.assertEqual(r, True)
3454
3455 def test_client_promote_dependent(self):
3456 "Test that the RPC client can promote a dependent change"
3457 # C (depends on B) -> B -> A ; then promote C to get:
3458 # A -> C (depends on B) -> B
3459 self.worker.hold_jobs_in_build = True
3460 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3461 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3462 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3463
3464 C.setDependsOn(B, 1)
3465
3466 A.addApproval('CRVW', 2)
3467 B.addApproval('CRVW', 2)
3468 C.addApproval('CRVW', 2)
3469
3470 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3471 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3472 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3473
3474 self.waitUntilSettled()
3475
3476 client = zuul.rpcclient.RPCClient('127.0.0.1',
3477 self.gearman_server.port)
3478 r = client.promote(pipeline='gate',
3479 change_ids=['3,1'])
3480
James E. Blair78acec92014-02-06 07:11:32 -08003481 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003482 self.worker.release('.*-merge')
3483 self.waitUntilSettled()
3484 self.worker.release('.*-merge')
3485 self.waitUntilSettled()
3486 self.worker.release('.*-merge')
3487 self.waitUntilSettled()
3488
3489 self.assertEqual(len(self.builds), 6)
3490 self.assertEqual(self.builds[0].name, 'project-test1')
3491 self.assertEqual(self.builds[1].name, 'project-test2')
3492 self.assertEqual(self.builds[2].name, 'project-test1')
3493 self.assertEqual(self.builds[3].name, 'project-test2')
3494 self.assertEqual(self.builds[4].name, 'project-test1')
3495 self.assertEqual(self.builds[5].name, 'project-test2')
3496
3497 self.assertTrue(self.job_has_changes(self.builds[0], B))
3498 self.assertFalse(self.job_has_changes(self.builds[0], A))
3499 self.assertFalse(self.job_has_changes(self.builds[0], C))
3500
3501 self.assertTrue(self.job_has_changes(self.builds[2], B))
3502 self.assertTrue(self.job_has_changes(self.builds[2], C))
3503 self.assertFalse(self.job_has_changes(self.builds[2], A))
3504
3505 self.assertTrue(self.job_has_changes(self.builds[4], B))
3506 self.assertTrue(self.job_has_changes(self.builds[4], C))
3507 self.assertTrue(self.job_has_changes(self.builds[4], A))
3508
3509 self.worker.release()
3510 self.waitUntilSettled()
3511
3512 self.assertEqual(A.data['status'], 'MERGED')
3513 self.assertEqual(A.reported, 2)
3514 self.assertEqual(B.data['status'], 'MERGED')
3515 self.assertEqual(B.reported, 2)
3516 self.assertEqual(C.data['status'], 'MERGED')
3517 self.assertEqual(C.reported, 2)
3518
3519 client.shutdown()
3520 self.assertEqual(r, True)
3521
3522 def test_client_promote_negative(self):
3523 "Test that the RPC client returns errors for promotion"
3524 self.worker.hold_jobs_in_build = True
3525 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3526 A.addApproval('CRVW', 2)
3527 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3528 self.waitUntilSettled()
3529
3530 client = zuul.rpcclient.RPCClient('127.0.0.1',
3531 self.gearman_server.port)
3532
3533 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3534 r = client.promote(pipeline='nonexistent',
3535 change_ids=['2,1', '3,1'])
3536 client.shutdown()
3537 self.assertEqual(r, False)
3538
3539 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3540 r = client.promote(pipeline='gate',
3541 change_ids=['4,1'])
3542 client.shutdown()
3543 self.assertEqual(r, False)
3544
3545 self.worker.hold_jobs_in_build = False
3546 self.worker.release()
3547 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003548
3549 def test_queue_rate_limiting(self):
3550 "Test that DependentPipelines are rate limited with dep across window"
3551 self.config.set('zuul', 'layout_config',
3552 'tests/fixtures/layout-rate-limit.yaml')
3553 self.sched.reconfigure(self.config)
3554 self.worker.hold_jobs_in_build = True
3555 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3556 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3557 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3558
3559 C.setDependsOn(B, 1)
3560 self.worker.addFailTest('project-test1', A)
3561
3562 A.addApproval('CRVW', 2)
3563 B.addApproval('CRVW', 2)
3564 C.addApproval('CRVW', 2)
3565
3566 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3567 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3568 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3569 self.waitUntilSettled()
3570
3571 # Only A and B will have their merge jobs queued because
3572 # window is 2.
3573 self.assertEqual(len(self.builds), 2)
3574 self.assertEqual(self.builds[0].name, 'project-merge')
3575 self.assertEqual(self.builds[1].name, 'project-merge')
3576
3577 self.worker.release('.*-merge')
3578 self.waitUntilSettled()
3579 self.worker.release('.*-merge')
3580 self.waitUntilSettled()
3581
3582 # Only A and B will have their test jobs queued because
3583 # window is 2.
3584 self.assertEqual(len(self.builds), 4)
3585 self.assertEqual(self.builds[0].name, 'project-test1')
3586 self.assertEqual(self.builds[1].name, 'project-test2')
3587 self.assertEqual(self.builds[2].name, 'project-test1')
3588 self.assertEqual(self.builds[3].name, 'project-test2')
3589
3590 self.worker.release('project-.*')
3591 self.waitUntilSettled()
3592
3593 queue = self.sched.layout.pipelines['gate'].queues[0]
3594 # A failed so window is reduced by 1 to 1.
3595 self.assertEqual(queue.window, 1)
3596 self.assertEqual(queue.window_floor, 1)
3597 self.assertEqual(A.data['status'], 'NEW')
3598
3599 # Gate is reset and only B's merge job is queued because
3600 # window shrunk to 1.
3601 self.assertEqual(len(self.builds), 1)
3602 self.assertEqual(self.builds[0].name, 'project-merge')
3603
3604 self.worker.release('.*-merge')
3605 self.waitUntilSettled()
3606
3607 # Only B's test jobs are queued because window is still 1.
3608 self.assertEqual(len(self.builds), 2)
3609 self.assertEqual(self.builds[0].name, 'project-test1')
3610 self.assertEqual(self.builds[1].name, 'project-test2')
3611
3612 self.worker.release('project-.*')
3613 self.waitUntilSettled()
3614
3615 # B was successfully merged so window is increased to 2.
3616 self.assertEqual(queue.window, 2)
3617 self.assertEqual(queue.window_floor, 1)
3618 self.assertEqual(B.data['status'], 'MERGED')
3619
3620 # Only C is left and its merge job is queued.
3621 self.assertEqual(len(self.builds), 1)
3622 self.assertEqual(self.builds[0].name, 'project-merge')
3623
3624 self.worker.release('.*-merge')
3625 self.waitUntilSettled()
3626
3627 # After successful merge job the test jobs for C are queued.
3628 self.assertEqual(len(self.builds), 2)
3629 self.assertEqual(self.builds[0].name, 'project-test1')
3630 self.assertEqual(self.builds[1].name, 'project-test2')
3631
3632 self.worker.release('project-.*')
3633 self.waitUntilSettled()
3634
3635 # C successfully merged so window is bumped to 3.
3636 self.assertEqual(queue.window, 3)
3637 self.assertEqual(queue.window_floor, 1)
3638 self.assertEqual(C.data['status'], 'MERGED')
3639
3640 def test_queue_rate_limiting_dependent(self):
3641 "Test that DependentPipelines are rate limited with dep in window"
3642 self.config.set('zuul', 'layout_config',
3643 'tests/fixtures/layout-rate-limit.yaml')
3644 self.sched.reconfigure(self.config)
3645 self.worker.hold_jobs_in_build = True
3646 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3647 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3648 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3649
3650 B.setDependsOn(A, 1)
3651
3652 self.worker.addFailTest('project-test1', A)
3653
3654 A.addApproval('CRVW', 2)
3655 B.addApproval('CRVW', 2)
3656 C.addApproval('CRVW', 2)
3657
3658 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3659 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3660 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3661 self.waitUntilSettled()
3662
3663 # Only A and B will have their merge jobs queued because
3664 # window is 2.
3665 self.assertEqual(len(self.builds), 2)
3666 self.assertEqual(self.builds[0].name, 'project-merge')
3667 self.assertEqual(self.builds[1].name, 'project-merge')
3668
3669 self.worker.release('.*-merge')
3670 self.waitUntilSettled()
3671 self.worker.release('.*-merge')
3672 self.waitUntilSettled()
3673
3674 # Only A and B will have their test jobs queued because
3675 # window is 2.
3676 self.assertEqual(len(self.builds), 4)
3677 self.assertEqual(self.builds[0].name, 'project-test1')
3678 self.assertEqual(self.builds[1].name, 'project-test2')
3679 self.assertEqual(self.builds[2].name, 'project-test1')
3680 self.assertEqual(self.builds[3].name, 'project-test2')
3681
3682 self.worker.release('project-.*')
3683 self.waitUntilSettled()
3684
3685 queue = self.sched.layout.pipelines['gate'].queues[0]
3686 # A failed so window is reduced by 1 to 1.
3687 self.assertEqual(queue.window, 1)
3688 self.assertEqual(queue.window_floor, 1)
3689 self.assertEqual(A.data['status'], 'NEW')
3690 self.assertEqual(B.data['status'], 'NEW')
3691
3692 # Gate is reset and only C's merge job is queued because
3693 # window shrunk to 1 and A and B were dequeued.
3694 self.assertEqual(len(self.builds), 1)
3695 self.assertEqual(self.builds[0].name, 'project-merge')
3696
3697 self.worker.release('.*-merge')
3698 self.waitUntilSettled()
3699
3700 # Only C's test jobs are queued because window is still 1.
3701 self.assertEqual(len(self.builds), 2)
3702 self.assertEqual(self.builds[0].name, 'project-test1')
3703 self.assertEqual(self.builds[1].name, 'project-test2')
3704
3705 self.worker.release('project-.*')
3706 self.waitUntilSettled()
3707
3708 # C was successfully merged so window is increased to 2.
3709 self.assertEqual(queue.window, 2)
3710 self.assertEqual(queue.window_floor, 1)
3711 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003712
3713 def test_worker_update_metadata(self):
3714 "Test if a worker can send back metadata about itself"
3715 self.worker.hold_jobs_in_build = True
3716
3717 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3718 A.addApproval('CRVW', 2)
3719 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3720 self.waitUntilSettled()
3721
3722 self.assertEqual(len(self.launcher.builds), 1)
3723
3724 self.log.debug('Current builds:')
3725 self.log.debug(self.launcher.builds)
3726
3727 start = time.time()
3728 while True:
3729 if time.time() - start > 10:
3730 raise Exception("Timeout waiting for gearman server to report "
3731 + "back to the client")
3732 build = self.launcher.builds.values()[0]
3733 if build.worker.name == "My Worker":
3734 break
3735 else:
3736 time.sleep(0)
3737
3738 self.log.debug(build)
3739 self.assertEqual("My Worker", build.worker.name)
3740 self.assertEqual("localhost", build.worker.hostname)
3741 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3742 self.assertEqual("zuul.example.org", build.worker.fqdn)
3743 self.assertEqual("FakeBuilder", build.worker.program)
3744 self.assertEqual("v1.1", build.worker.version)
3745 self.assertEqual({'something': 'else'}, build.worker.extra)
3746
3747 self.worker.hold_jobs_in_build = False
3748 self.worker.release()
3749 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003750
3751 def test_footer_message(self):
3752 "Test a pipeline's footer message is correctly added to the report."
3753 self.config.set('zuul', 'layout_config',
3754 'tests/fixtures/layout-footer-message.yaml')
3755 self.sched.reconfigure(self.config)
3756 self.registerJobs()
3757
3758 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3759 A.addApproval('CRVW', 2)
3760 self.worker.addFailTest('test1', A)
3761 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3762 self.waitUntilSettled()
3763
3764 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3765 B.addApproval('CRVW', 2)
3766 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3767 self.waitUntilSettled()
3768
3769 self.assertEqual(2, len(self.smtp_messages))
3770
3771 failure_body = """\
3772Build failed. For information on how to proceed, see \
3773http://wiki.example.org/Test_Failures
3774
3775- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3776- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3777
3778For CI problems and help debugging, contact ci@example.org"""
3779
3780 success_body = """\
3781Build succeeded.
3782
3783- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3784- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3785
3786For CI problems and help debugging, contact ci@example.org"""
3787
3788 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3789 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11003790
3791 def test_merge_failure_reporters(self):
3792 """Check that the config is set up correctly"""
3793
3794 self.config.set('zuul', 'layout_config',
3795 'tests/fixtures/layout-merge-failure.yaml')
3796 self.sched.reconfigure(self.config)
3797 self.registerJobs()
3798
3799 self.assertEqual(
3800 "Merge Failed.\n\nThis change was unable to be automatically "
3801 "merged with the current state of the repository. Please rebase "
3802 "your change and upload a new patchset.",
3803 self.sched.layout.pipelines['check'].merge_failure_message)
3804 self.assertEqual(
3805 "The merge failed! For more information...",
3806 self.sched.layout.pipelines['gate'].merge_failure_message)
3807
3808 self.assertEqual(
3809 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3810 self.assertEqual(
3811 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3812
3813 self.assertTrue(isinstance(
3814 self.sched.layout.pipelines['check'].merge_failure_actions[0].
3815 reporter, zuul.reporter.gerrit.Reporter))
3816
3817 self.assertTrue(
3818 (
3819 isinstance(self.sched.layout.pipelines['gate'].
3820 merge_failure_actions[0].reporter,
3821 zuul.reporter.smtp.Reporter) and
3822 isinstance(self.sched.layout.pipelines['gate'].
3823 merge_failure_actions[1].reporter,
3824 zuul.reporter.gerrit.Reporter)
3825 ) or (
3826 isinstance(self.sched.layout.pipelines['gate'].
3827 merge_failure_actions[0].reporter,
3828 zuul.reporter.gerrit.Reporter) and
3829 isinstance(self.sched.layout.pipelines['gate'].
3830 merge_failure_actions[1].reporter,
3831 zuul.reporter.smtp.Reporter)
3832 )
3833 )
3834
3835 def test_merge_failure_reports(self):
3836 """Check that when a change fails to merge the correct message is sent
3837 to the correct reporter"""
3838 self.config.set('zuul', 'layout_config',
3839 'tests/fixtures/layout-merge-failure.yaml')
3840 self.sched.reconfigure(self.config)
3841 self.registerJobs()
3842
3843 # Check a test failure isn't reported to SMTP
3844 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3845 A.addApproval('CRVW', 2)
3846 self.worker.addFailTest('project-test1', A)
3847 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3848 self.waitUntilSettled()
3849
3850 self.assertEqual(3, len(self.history)) # 3 jobs
3851 self.assertEqual(0, len(self.smtp_messages))
3852
3853 # Check a merge failure is reported to SMTP
3854 # B should be merged, but C will conflict with B
3855 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3856 B.addPatchset(['conflict'])
3857 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3858 C.addPatchset(['conflict'])
3859 B.addApproval('CRVW', 2)
3860 C.addApproval('CRVW', 2)
3861 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3862 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3863 self.waitUntilSettled()
3864
3865 self.assertEqual(6, len(self.history)) # A and B jobs
3866 self.assertEqual(1, len(self.smtp_messages))
3867 self.assertEqual('The merge failed! For more information...',
3868 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003869
3870 def test_swift_instructions(self):
3871 "Test that the correct swift instructions are sent to the workers"
3872 self.config.set('zuul', 'layout_config',
3873 'tests/fixtures/layout-swift.yaml')
3874 self.sched.reconfigure(self.config)
3875 self.registerJobs()
3876
3877 self.worker.hold_jobs_in_build = True
3878 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3879
3880 A.addApproval('CRVW', 2)
3881 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3882 self.waitUntilSettled()
3883
3884 self.assertEqual(
3885 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
3886 "gate/test-merge/",
3887 self.builds[0].parameters['SWIFT_logs_URL'][:-32])
3888 self.assertEqual(5,
3889 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
3890 split('\n')))
3891 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
3892
3893 self.assertEqual(
3894 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
3895 "gate/test-test/",
3896 self.builds[1].parameters['SWIFT_logs_URL'][:-32])
3897 self.assertEqual(5,
3898 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
3899 split('\n')))
3900 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
3901
3902 self.assertEqual(
3903 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
3904 "gate/test-test/",
3905 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-32])
3906 self.assertEqual(5,
3907 len(self.builds[1].
3908 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
3909 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
3910
3911 self.worker.hold_jobs_in_build = False
3912 self.worker.release()
3913 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003914
3915 def test_client_get_running_jobs(self):
3916 "Test that the RPC client can get a list of running jobs"
3917 self.worker.hold_jobs_in_build = True
3918 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3919 A.addApproval('CRVW', 2)
3920 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3921 self.waitUntilSettled()
3922
3923 client = zuul.rpcclient.RPCClient('127.0.0.1',
3924 self.gearman_server.port)
3925
3926 # Wait for gearman server to send the initial workData back to zuul
3927 start = time.time()
3928 while True:
3929 if time.time() - start > 10:
3930 raise Exception("Timeout waiting for gearman server to report "
3931 + "back to the client")
3932 build = self.launcher.builds.values()[0]
3933 if build.worker.name == "My Worker":
3934 break
3935 else:
3936 time.sleep(0)
3937
3938 running_items = client.get_running_jobs()
3939
3940 self.assertEqual(1, len(running_items))
3941 running_item = running_items[0]
3942 self.assertEqual([], running_item['failing_reasons'])
3943 self.assertEqual([], running_item['items_behind'])
3944 self.assertEqual('https://hostname/1', running_item['url'])
3945 self.assertEqual(None, running_item['item_ahead'])
3946 self.assertEqual('org/project', running_item['project'])
3947 self.assertEqual(None, running_item['remaining_time'])
3948 self.assertEqual(True, running_item['active'])
3949 self.assertEqual('1,1', running_item['id'])
3950
3951 self.assertEqual(3, len(running_item['jobs']))
3952 for job in running_item['jobs']:
3953 if job['name'] == 'project-merge':
3954 self.assertEqual('project-merge', job['name'])
3955 self.assertEqual('gate', job['pipeline'])
3956 self.assertEqual(False, job['retry'])
3957 self.assertEqual(13, len(job['parameters']))
3958 self.assertEqual('https://server/job/project-merge/0/',
3959 job['url'])
3960 self.assertEqual(7, len(job['worker']))
3961 self.assertEqual(False, job['canceled'])
3962 self.assertEqual(True, job['voting'])
3963 self.assertEqual(None, job['result'])
3964 self.assertEqual('gate', job['pipeline'])
3965 break
3966
3967 self.worker.hold_jobs_in_build = False
3968 self.worker.release()
3969 self.waitUntilSettled()
3970
3971 running_items = client.get_running_jobs()
3972 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003973
3974 def test_nonvoting_pipeline(self):
3975 "Test that a nonvoting pipeline (experimental) can still report"
3976
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003977 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3978 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003979 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3980 self.waitUntilSettled()
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003981 self.assertEqual(
3982 self.getJobFromHistory('experimental-project-test').result,
3983 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003984 self.assertEqual(A.reported, 1)