blob: ade884a433509e827af7d09bbd2475aba0679614 [file] [log] [blame]
James E. Blairb0fcae42012-07-17 11:12:10 -07001#!/usr/bin/env python
2
3# Copyright 2012 Hewlett-Packard Development Company, L.P.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
James E. Blairb0fcae42012-07-17 11:12:10 -070017import ConfigParser
Monty Taylorbc758832013-06-17 17:22:42 -040018from cStringIO import StringIO
Clark Boylan4ba48d92013-11-11 18:03:53 -080019import gc
James E. Blair8cc15a82012-08-01 11:17:57 -070020import hashlib
James E. Blairb0fcae42012-07-17 11:12:10 -070021import json
Monty Taylorbc758832013-06-17 17:22:42 -040022import logging
23import os
James E. Blairb0fcae42012-07-17 11:12:10 -070024import pprint
Monty Taylorbc758832013-06-17 17:22:42 -040025import Queue
26import random
James E. Blairb0fcae42012-07-17 11:12:10 -070027import re
James E. Blair412e5582013-04-22 15:50:12 -070028import select
James E. Blair4886cc12012-07-18 15:39:41 -070029import shutil
James E. Blair412e5582013-04-22 15:50:12 -070030import socket
James E. Blair4886f282012-11-15 09:27:33 -080031import string
Monty Taylorbc758832013-06-17 17:22:42 -040032import subprocess
Joshua Hesketh36c3fa52014-01-22 11:40:52 +110033import swiftclient
Monty Taylorbc758832013-06-17 17:22:42 -040034import threading
35import time
James E. Blair1843a552013-07-03 14:19:52 -070036import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040037import urllib2
Monty Taylorbc758832013-06-17 17:22:42 -040038
James E. Blair4886cc12012-07-18 15:39:41 -070039import git
James E. Blair1f4c2bb2013-04-26 08:40:46 -070040import gear
Monty Taylorbc758832013-06-17 17:22:42 -040041import fixtures
tanline621a412014-02-12 11:16:36 +080042import six.moves.urllib.parse as urlparse
Monty Taylorbc758832013-06-17 17:22:42 -040043import statsd
44import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070045
James E. Blairb0fcae42012-07-17 11:12:10 -070046import zuul.scheduler
James E. Blair1843a552013-07-03 14:19:52 -070047import zuul.webapp
James E. Blairad28e912013-11-27 10:43:22 -080048import zuul.rpclistener
49import zuul.rpcclient
James E. Blair1f4c2bb2013-04-26 08:40:46 -070050import zuul.launcher.gearman
Joshua Hesketh36c3fa52014-01-22 11:40:52 +110051import zuul.lib.swift
James E. Blair4076e2b2014-01-28 12:42:20 -080052import zuul.merger.server
53import zuul.merger.client
Joshua Hesketh1879cf72013-08-19 14:13:15 +100054import zuul.reporter.gerrit
Joshua Hesketh5fea8672013-08-19 17:32:01 +100055import zuul.reporter.smtp
James E. Blairb0fcae42012-07-17 11:12:10 -070056import zuul.trigger.gerrit
James E. Blair63bb0ef2013-07-29 17:14:51 -070057import zuul.trigger.timer
James E. Blairb0fcae42012-07-17 11:12:10 -070058
59FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
60 'fixtures')
61CONFIG = ConfigParser.ConfigParser()
62CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
63
64CONFIG.set('zuul', 'layout_config',
65 os.path.join(FIXTURE_DIR, "layout.yaml"))
66
James E. Blair1f4c2bb2013-04-26 08:40:46 -070067logging.basicConfig(level=logging.DEBUG,
68 format='%(asctime)s %(name)-32s '
69 '%(levelname)-8s %(message)s')
James E. Blairb0fcae42012-07-17 11:12:10 -070070
71
Monty Taylorbc758832013-06-17 17:22:42 -040072def repack_repo(path):
James E. Blairac2c3242014-01-24 13:38:51 -080073 cmd = ['git', '--git-dir=%s/.git' % path, 'repack', '-afd']
74 output = subprocess.Popen(cmd, close_fds=True,
75 stdout=subprocess.PIPE,
76 stderr=subprocess.PIPE)
Monty Taylorbc758832013-06-17 17:22:42 -040077 out = output.communicate()
78 if output.returncode:
79 raise Exception("git repack returned %d" % output.returncode)
80 return out
81
82
James E. Blair8cc15a82012-08-01 11:17:57 -070083def random_sha1():
84 return hashlib.sha1(str(random.random())).hexdigest()
85
86
James E. Blair4886cc12012-07-18 15:39:41 -070087class ChangeReference(git.Reference):
88 _common_path_default = "refs/changes"
89 _points_to_commits_only = True
90
91
James E. Blairb0fcae42012-07-17 11:12:10 -070092class FakeChange(object):
James E. Blair8c803f82012-07-31 16:25:42 -070093 categories = {'APRV': ('Approved', -1, 1),
94 'CRVW': ('Code-Review', -2, 2),
95 'VRFY': ('Verified', -2, 2)}
James E. Blairb0fcae42012-07-17 11:12:10 -070096
Monty Taylorbc758832013-06-17 17:22:42 -040097 def __init__(self, gerrit, number, project, branch, subject,
98 status='NEW', upstream_root=None):
James E. Blair8cc15a82012-08-01 11:17:57 -070099 self.gerrit = gerrit
James E. Blaird466dc42012-07-31 10:42:56 -0700100 self.reported = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700101 self.queried = 0
James E. Blairb0fcae42012-07-17 11:12:10 -0700102 self.patchsets = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700103 self.number = number
104 self.project = project
105 self.branch = branch
106 self.subject = subject
107 self.latest_patchset = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700108 self.depends_on_change = None
109 self.needed_by_changes = []
James E. Blair127bc182012-08-28 15:55:15 -0700110 self.fail_merge = False
James E. Blair42f74822013-05-14 15:18:03 -0700111 self.messages = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700112 self.data = {
113 'branch': branch,
114 'comments': [],
115 'commitMessage': subject,
116 'createdOn': time.time(),
James E. Blair8cc15a82012-08-01 11:17:57 -0700117 'id': 'I' + random_sha1(),
James E. Blairb0fcae42012-07-17 11:12:10 -0700118 'lastUpdated': time.time(),
119 'number': str(number),
120 'open': True,
121 'owner': {'email': 'user@example.com',
122 'name': 'User Name',
123 'username': 'username'},
124 'patchSets': self.patchsets,
125 'project': project,
126 'status': status,
127 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700128 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700129 'url': 'https://hostname/%s' % number}
130
Monty Taylorbc758832013-06-17 17:22:42 -0400131 self.upstream_root = upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700132 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700133 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700134
Monty Taylorbc758832013-06-17 17:22:42 -0400135 def add_fake_change_to_repo(self, msg, fn, large):
136 path = os.path.join(self.upstream_root, self.project)
137 repo = git.Repo(path)
138 ref = ChangeReference.create(repo, '1/%s/%s' % (self.number,
139 self.latest_patchset),
140 'refs/tags/init')
141 repo.head.reference = ref
142 repo.head.reset(index=True, working_tree=True)
143 repo.git.clean('-x', '-f', '-d')
144
145 path = os.path.join(self.upstream_root, self.project)
146 if not large:
147 fn = os.path.join(path, fn)
148 f = open(fn, 'w')
149 f.write("test %s %s %s\n" %
150 (self.branch, self.number, self.latest_patchset))
151 f.close()
152 repo.index.add([fn])
153 else:
154 for fni in range(100):
155 fn = os.path.join(path, str(fni))
156 f = open(fn, 'w')
157 for ci in range(4096):
158 f.write(random.choice(string.printable))
159 f.close()
160 repo.index.add([fn])
161
James E. Blair287c06d2013-07-24 10:39:30 -0700162 r = repo.index.commit(msg)
163 repo.head.reference = 'master'
164 repo.head.reset(index=True, working_tree=True)
165 repo.git.clean('-x', '-f', '-d')
James E. Blairac2c3242014-01-24 13:38:51 -0800166 repo.heads['master'].checkout()
James E. Blair287c06d2013-07-24 10:39:30 -0700167 return r
Monty Taylorbc758832013-06-17 17:22:42 -0400168
James E. Blair70c71582013-03-06 08:50:50 -0800169 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700170 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700171 if files:
172 fn = files[0]
173 else:
174 fn = '%s-%s' % (self.branch, self.number)
175 msg = self.subject + '-' + str(self.latest_patchset)
Monty Taylorbc758832013-06-17 17:22:42 -0400176 c = self.add_fake_change_to_repo(msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800177 ps_files = [{'file': '/COMMIT_MSG',
178 'type': 'ADDED'},
179 {'file': 'README',
180 'type': 'MODIFIED'}]
181 for f in files:
182 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700183 d = {'approvals': [],
184 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800185 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700186 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700187 'ref': 'refs/changes/1/%s/%s' % (self.number,
188 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700189 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700190 'uploader': {'email': 'user@example.com',
191 'name': 'User name',
192 'username': 'user'}}
193 self.data['currentPatchSet'] = d
194 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700195 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700196
James E. Blaire0487072012-08-29 17:38:31 -0700197 def getPatchsetCreatedEvent(self, patchset):
198 event = {"type": "patchset-created",
199 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800200 "branch": self.branch,
201 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
202 "number": str(self.number),
203 "subject": self.subject,
204 "owner": {"name": "User Name"},
205 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700206 "patchSet": self.patchsets[patchset - 1],
207 "uploader": {"name": "User Name"}}
208 return event
209
James E. Blair42f74822013-05-14 15:18:03 -0700210 def getChangeRestoredEvent(self):
211 event = {"type": "change-restored",
212 "change": {"project": self.project,
213 "branch": self.branch,
214 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
215 "number": str(self.number),
216 "subject": self.subject,
217 "owner": {"name": "User Name"},
218 "url": "https://hostname/3"},
219 "restorer": {"name": "User Name"},
220 "reason": ""}
221 return event
222
James E. Blairc053d022014-01-22 14:57:33 -0800223 def addApproval(self, category, value, username='jenkins',
224 granted_on=None):
225 if not granted_on:
226 granted_on = time.time()
James E. Blair8c803f82012-07-31 16:25:42 -0700227 approval = {'description': self.categories[category][0],
228 'type': category,
James E. Blairc053d022014-01-22 14:57:33 -0800229 'value': str(value),
James E. Blair64ff4ef2014-01-24 13:50:23 -0800230 'by': {
231 'username': username,
232 'email': username + '@example.com',
233 },
James E. Blairc053d022014-01-22 14:57:33 -0800234 'grantedOn': int(granted_on)}
235 for i, x in enumerate(self.patchsets[-1]['approvals'][:]):
James E. Blair64ff4ef2014-01-24 13:50:23 -0800236 if x['by']['username'] == username and x['type'] == category:
James E. Blairc053d022014-01-22 14:57:33 -0800237 del self.patchsets[-1]['approvals'][i]
James E. Blair8c803f82012-07-31 16:25:42 -0700238 self.patchsets[-1]['approvals'].append(approval)
239 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700240 'author': {'email': 'user@example.com',
241 'name': 'User Name',
242 'username': 'username'},
243 'change': {'branch': self.branch,
244 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
245 'number': str(self.number),
246 'owner': {'email': 'user@example.com',
247 'name': 'User Name',
248 'username': 'username'},
249 'project': self.project,
250 'subject': self.subject,
251 'topic': 'master',
252 'url': 'https://hostname/459'},
253 'comment': '',
254 'patchSet': self.patchsets[-1],
255 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700256 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700257 return json.loads(json.dumps(event))
258
James E. Blair8c803f82012-07-31 16:25:42 -0700259 def getSubmitRecords(self):
260 status = {}
261 for cat in self.categories.keys():
262 status[cat] = 0
263
264 for a in self.patchsets[-1]['approvals']:
265 cur = status[a['type']]
266 cat_min, cat_max = self.categories[a['type']][1:]
267 new = int(a['value'])
268 if new == cat_min:
269 cur = new
270 elif abs(new) > abs(cur):
271 cur = new
272 status[a['type']] = cur
273
274 labels = []
275 ok = True
276 for typ, cat in self.categories.items():
277 cur = status[typ]
278 cat_min, cat_max = cat[1:]
279 if cur == cat_min:
280 value = 'REJECT'
281 ok = False
282 elif cur == cat_max:
283 value = 'OK'
284 else:
285 value = 'NEED'
286 ok = False
287 labels.append({'label': cat[0], 'status': value})
288 if ok:
289 return [{'status': 'OK'}]
290 return [{'status': 'NOT_READY',
291 'labels': labels}]
292
293 def setDependsOn(self, other, patchset):
294 self.depends_on_change = other
295 d = {'id': other.data['id'],
296 'number': other.data['number'],
297 'ref': other.patchsets[patchset - 1]['ref']
298 }
299 self.data['dependsOn'] = [d]
300
301 other.needed_by_changes.append(self)
302 needed = other.data.get('neededBy', [])
303 d = {'id': self.data['id'],
304 'number': self.data['number'],
305 'ref': self.patchsets[patchset - 1]['ref'],
306 'revision': self.patchsets[patchset - 1]['revision']
307 }
308 needed.append(d)
309 other.data['neededBy'] = needed
310
James E. Blairb0fcae42012-07-17 11:12:10 -0700311 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700312 self.queried += 1
313 d = self.data.get('dependsOn')
314 if d:
315 d = d[0]
316 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
317 d['isCurrentPatchSet'] = True
318 else:
319 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700320 return json.loads(json.dumps(self.data))
321
322 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800323 if (self.depends_on_change and
324 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700325 return
James E. Blair127bc182012-08-28 15:55:15 -0700326 if self.fail_merge:
327 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700328 self.data['status'] = 'MERGED'
329 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700330
Monty Taylorbc758832013-06-17 17:22:42 -0400331 path = os.path.join(self.upstream_root, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700332 repo = git.Repo(path)
333 repo.heads[self.branch].commit = \
334 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700335
James E. Blaird466dc42012-07-31 10:42:56 -0700336 def setReported(self):
337 self.reported += 1
338
James E. Blairb0fcae42012-07-17 11:12:10 -0700339
340class FakeGerrit(object):
341 def __init__(self, *args, **kw):
342 self.event_queue = Queue.Queue()
343 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
344 self.change_number = 0
345 self.changes = {}
346
347 def addFakeChange(self, project, branch, subject):
348 self.change_number += 1
Monty Taylorbc758832013-06-17 17:22:42 -0400349 c = FakeChange(self, self.change_number, project, branch, subject,
350 upstream_root=self.upstream_root)
James E. Blairb0fcae42012-07-17 11:12:10 -0700351 self.changes[self.change_number] = c
352 return c
353
354 def addEvent(self, data):
355 return self.event_queue.put(data)
356
357 def getEvent(self):
358 return self.event_queue.get()
359
360 def eventDone(self):
361 self.event_queue.task_done()
362
363 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700364 number, ps = changeid.split(',')
365 change = self.changes[int(number)]
James E. Blair42f74822013-05-14 15:18:03 -0700366 change.messages.append(message)
James E. Blairb0fcae42012-07-17 11:12:10 -0700367 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700368 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700369 if message:
370 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700371
372 def query(self, number):
James E. Blairad28e912013-11-27 10:43:22 -0800373 change = self.changes.get(int(number))
374 if change:
375 return change.query()
376 return {}
James E. Blairb0fcae42012-07-17 11:12:10 -0700377
378 def startWatching(self, *args, **kw):
379 pass
380
381
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700382class BuildHistory(object):
383 def __init__(self, **kw):
384 self.__dict__.update(kw)
James E. Blairb0fcae42012-07-17 11:12:10 -0700385
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700386 def __repr__(self):
387 return ("<Completed build, result: %s name: %s #%s changes: %s>" %
388 (self.result, self.name, self.number, self.changes))
James E. Blairb0fcae42012-07-17 11:12:10 -0700389
390
James E. Blair8cc15a82012-08-01 11:17:57 -0700391class FakeURLOpener(object):
Monty Taylorbc758832013-06-17 17:22:42 -0400392 def __init__(self, upstream_root, fake_gerrit, url):
393 self.upstream_root = upstream_root
James E. Blair8cc15a82012-08-01 11:17:57 -0700394 self.fake_gerrit = fake_gerrit
395 self.url = url
396
397 def read(self):
398 res = urlparse.urlparse(self.url)
399 path = res.path
400 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200401 ret = '001e# service=git-upload-pack\n'
402 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
403 'multi_ack thin-pack side-band side-band-64k ofs-delta '
404 'shallow no-progress include-tag multi_ack_detailed no-done\n')
Monty Taylorbc758832013-06-17 17:22:42 -0400405 path = os.path.join(self.upstream_root, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700406 repo = git.Repo(path)
407 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200408 r = ref.object.hexsha + ' ' + ref.path + '\n'
409 ret += '%04x%s' % (len(r) + 4, r)
410 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700411 return ret
412
413
James E. Blair4886cc12012-07-18 15:39:41 -0700414class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000415 name = 'gerrit'
416
Monty Taylorbc758832013-06-17 17:22:42 -0400417 def __init__(self, upstream_root, *args):
418 super(FakeGerritTrigger, self).__init__(*args)
419 self.upstream_root = upstream_root
420
James E. Blair4886cc12012-07-18 15:39:41 -0700421 def getGitUrl(self, project):
Monty Taylorbc758832013-06-17 17:22:42 -0400422 return os.path.join(self.upstream_root, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700423
424
James E. Blair412e5582013-04-22 15:50:12 -0700425class FakeStatsd(threading.Thread):
426 def __init__(self):
427 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400428 self.daemon = True
James E. Blair412e5582013-04-22 15:50:12 -0700429 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
430 self.sock.bind(('', 0))
431 self.port = self.sock.getsockname()[1]
432 self.wake_read, self.wake_write = os.pipe()
433 self.stats = []
434
435 def run(self):
436 while True:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700437 poll = select.poll()
438 poll.register(self.sock, select.POLLIN)
439 poll.register(self.wake_read, select.POLLIN)
440 ret = poll.poll()
441 for (fd, event) in ret:
442 if fd == self.sock.fileno():
James E. Blair412e5582013-04-22 15:50:12 -0700443 data = self.sock.recvfrom(1024)
444 if not data:
445 return
446 self.stats.append(data[0])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700447 if fd == self.wake_read:
James E. Blair412e5582013-04-22 15:50:12 -0700448 return
449
450 def stop(self):
451 os.write(self.wake_write, '1\n')
452
453
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700454class FakeBuild(threading.Thread):
455 log = logging.getLogger("zuul.test")
456
457 def __init__(self, worker, job, number, node):
458 threading.Thread.__init__(self)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400459 self.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700460 self.worker = worker
461 self.job = job
462 self.name = job.name.split(':')[1]
463 self.number = number
464 self.node = node
465 self.parameters = json.loads(job.arguments)
466 self.unique = self.parameters['ZUUL_UUID']
467 self.wait_condition = threading.Condition()
468 self.waiting = False
469 self.aborted = False
470 self.created = time.time()
471 self.description = ''
James E. Blair4a28a882013-08-23 15:17:33 -0700472 self.run_error = False
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700473
474 def release(self):
475 self.wait_condition.acquire()
476 self.wait_condition.notify()
477 self.waiting = False
478 self.log.debug("Build %s released" % self.unique)
479 self.wait_condition.release()
480
481 def isWaiting(self):
482 self.wait_condition.acquire()
483 if self.waiting:
484 ret = True
485 else:
486 ret = False
487 self.wait_condition.release()
488 return ret
489
490 def _wait(self):
491 self.wait_condition.acquire()
492 self.waiting = True
493 self.log.debug("Build %s waiting" % self.unique)
494 self.wait_condition.wait()
495 self.wait_condition.release()
496
497 def run(self):
498 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700499 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
500 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700501 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700502 'manager': self.worker.worker_id,
Joshua Heskethba8776a2014-01-12 14:35:40 +0800503 'worker_name': 'My Worker',
504 'worker_hostname': 'localhost',
505 'worker_ips': ['127.0.0.1', '192.168.1.1'],
506 'worker_fqdn': 'zuul.example.org',
507 'worker_program': 'FakeBuilder',
508 'worker_version': 'v1.1',
509 'worker_extra': {'something': 'else'}
Paul Belangerec49b4c2013-07-20 20:32:20 -0400510 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700511
Joshua Heskethba8776a2014-01-12 14:35:40 +0800512 self.log.debug('Running build %s' % self.unique)
513
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700514 self.job.sendWorkData(json.dumps(data))
Joshua Heskethba8776a2014-01-12 14:35:40 +0800515 self.log.debug('Sent WorkData packet with %s' % json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700516 self.job.sendWorkStatus(0, 100)
517
518 if self.worker.hold_jobs_in_build:
Joshua Heskethba8776a2014-01-12 14:35:40 +0800519 self.log.debug('Holding build %s' % self.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700520 self._wait()
521 self.log.debug("Build %s continuing" % self.unique)
522
523 self.worker.lock.acquire()
524
525 result = 'SUCCESS'
526 if (('ZUUL_REF' in self.parameters) and
527 self.worker.shouldFailTest(self.name,
528 self.parameters['ZUUL_REF'])):
529 result = 'FAILURE'
530 if self.aborted:
531 result = 'ABORTED'
532
James E. Blair4a28a882013-08-23 15:17:33 -0700533 if self.run_error:
534 work_fail = True
535 result = 'RUN_ERROR'
536 else:
537 data['result'] = result
538 work_fail = False
539
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700540 changes = None
541 if 'ZUUL_CHANGE_IDS' in self.parameters:
542 changes = self.parameters['ZUUL_CHANGE_IDS']
543
544 self.worker.build_history.append(
545 BuildHistory(name=self.name, number=self.number,
546 result=result, changes=changes, node=self.node,
James E. Blair64ed6f22013-07-10 14:07:23 -0700547 uuid=self.unique, description=self.description,
548 pipeline=self.parameters['ZUUL_PIPELINE'])
Paul Belangerec49b4c2013-07-20 20:32:20 -0400549 )
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700550
James E. Blair4a28a882013-08-23 15:17:33 -0700551 self.job.sendWorkData(json.dumps(data))
552 if work_fail:
553 self.job.sendWorkFail()
554 else:
555 self.job.sendWorkComplete(json.dumps(data))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700556 del self.worker.gearman_jobs[self.job.unique]
557 self.worker.running_builds.remove(self)
558 self.worker.lock.release()
559
560
561class FakeWorker(gear.Worker):
Monty Taylorbc758832013-06-17 17:22:42 -0400562 def __init__(self, worker_id, test):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700563 super(FakeWorker, self).__init__(worker_id)
564 self.gearman_jobs = {}
565 self.build_history = []
566 self.running_builds = []
567 self.build_counter = 0
568 self.fail_tests = {}
Monty Taylorbc758832013-06-17 17:22:42 -0400569 self.test = test
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700570
571 self.hold_jobs_in_build = False
572 self.lock = threading.Lock()
573 self.__work_thread = threading.Thread(target=self.work)
James E. Blair8a6f0c22013-07-01 12:31:34 -0400574 self.__work_thread.daemon = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700575 self.__work_thread.start()
576
577 def handleJob(self, job):
578 parts = job.name.split(":")
579 cmd = parts[0]
580 name = parts[1]
581 if len(parts) > 2:
582 node = parts[2]
583 else:
584 node = None
585 if cmd == 'build':
586 self.handleBuild(job, name, node)
587 elif cmd == 'stop':
588 self.handleStop(job, name)
589 elif cmd == 'set_description':
590 self.handleSetDescription(job, name)
591
592 def handleBuild(self, job, name, node):
593 build = FakeBuild(self, job, self.build_counter, node)
594 job.build = build
595 self.gearman_jobs[job.unique] = job
596 self.build_counter += 1
597
598 self.running_builds.append(build)
599 build.start()
600
601 def handleStop(self, job, name):
602 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700603 parameters = json.loads(job.arguments)
604 name = parameters['name']
605 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700606 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700607 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700608 build.aborted = True
609 build.release()
610 job.sendWorkComplete()
611 return
612 job.sendWorkFail()
613
614 def handleSetDescription(self, job, name):
615 self.log.debug("handle set description")
616 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700617 name = parameters['name']
618 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700619 descr = parameters['html_description']
620 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700621 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700622 build.description = descr
623 job.sendWorkComplete()
624 return
625 for build in self.build_history:
James E. Blair3c483cf2013-06-04 16:30:43 -0700626 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700627 build.description = descr
628 job.sendWorkComplete()
629 return
630 job.sendWorkFail()
631
632 def work(self):
633 while self.running:
634 try:
635 job = self.getJob()
636 except gear.InterruptedError:
637 continue
638 try:
639 self.handleJob(job)
640 except:
641 self.log.exception("Worker exception:")
642
643 def addFailTest(self, name, change):
644 l = self.fail_tests.get(name, [])
645 l.append(change)
646 self.fail_tests[name] = l
647
648 def shouldFailTest(self, name, ref):
649 l = self.fail_tests.get(name, [])
650 for change in l:
Monty Taylorbc758832013-06-17 17:22:42 -0400651 if self.test.ref_has_change(ref, change):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700652 return True
653 return False
654
655 def release(self, regex=None):
656 builds = self.running_builds[:]
657 self.log.debug("releasing build %s (%s)" % (regex,
James E. Blair78e31b32013-07-09 09:11:34 -0700658 len(self.running_builds)))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700659 for build in builds:
660 if not regex or re.match(regex, build.name):
661 self.log.debug("releasing build %s" %
662 (build.parameters['ZUUL_UUID']))
663 build.release()
664 else:
665 self.log.debug("not releasing build %s" %
666 (build.parameters['ZUUL_UUID']))
667 self.log.debug("done releasing builds %s (%s)" %
668 (regex, len(self.running_builds)))
669
670
671class FakeGearmanServer(gear.Server):
672 def __init__(self):
673 self.hold_jobs_in_queue = False
674 super(FakeGearmanServer, self).__init__(0)
675
676 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700677 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
678 for job in queue:
679 if not hasattr(job, 'waiting'):
680 if job.name.startswith('build:'):
681 job.waiting = self.hold_jobs_in_queue
682 else:
683 job.waiting = False
684 if job.waiting:
685 continue
686 if job.name in connection.functions:
687 if not peek:
688 queue.remove(job)
James E. Blaire2819012013-06-28 17:17:26 -0400689 connection.related_jobs[job.handle] = job
690 job.worker_connection = connection
691 job.running = True
James E. Blair701c5b42013-06-06 09:34:59 -0700692 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700693 return None
694
695 def release(self, regex=None):
696 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700697 qlen = (len(self.high_queue) + len(self.normal_queue) +
698 len(self.low_queue))
699 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
James E. Blairdda6c912013-07-29 14:12:12 -0700700 for job in self.getQueue():
701 cmd, name = job.name.split(':')
702 if cmd != 'build':
703 continue
704 if not regex or re.match(regex, name):
705 self.log.debug("releasing queued job %s" %
706 job.unique)
707 job.waiting = False
708 released = True
709 else:
710 self.log.debug("not releasing queued job %s" %
711 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700712 if released:
713 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700714 qlen = (len(self.high_queue) + len(self.normal_queue) +
715 len(self.low_queue))
716 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700717
718
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000719class FakeSMTP(object):
720 log = logging.getLogger('zuul.FakeSMTP')
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000721
James E. Blairff80a2f2013-12-27 13:24:06 -0800722 def __init__(self, messages, server, port):
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000723 self.server = server
724 self.port = port
James E. Blairff80a2f2013-12-27 13:24:06 -0800725 self.messages = messages
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000726
727 def sendmail(self, from_email, to_email, msg):
728 self.log.info("Sending email from %s, to %s, with msg %s" % (
729 from_email, to_email, msg))
730
731 headers = msg.split('\n\n', 1)[0]
732 body = msg.split('\n\n', 1)[1]
733
James E. Blairff80a2f2013-12-27 13:24:06 -0800734 self.messages.append(dict(
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000735 from_email=from_email,
736 to_email=to_email,
737 msg=msg,
738 headers=headers,
739 body=body,
740 ))
741
742 return True
743
744 def quit(self):
745 return True
746
747
Joshua Hesketh36c3fa52014-01-22 11:40:52 +1100748class FakeSwiftClientConnection(swiftclient.client.Connection):
749 def post_account(self, headers):
750 # Do nothing
751 pass
752
753 def get_auth(self):
754 # Returns endpoint and (unused) auth token
755 endpoint = os.path.join('https://storage.example.org', 'V1',
756 'AUTH_account')
757 return endpoint, ''
758
759
Monty Taylorbc758832013-06-17 17:22:42 -0400760class TestScheduler(testtools.TestCase):
James E. Blairb0fcae42012-07-17 11:12:10 -0700761 log = logging.getLogger("zuul.test")
762
763 def setUp(self):
Monty Taylorbc758832013-06-17 17:22:42 -0400764 super(TestScheduler, self).setUp()
765 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
766 try:
767 test_timeout = int(test_timeout)
768 except ValueError:
769 # If timeout value is invalid do not set a timeout.
770 test_timeout = 0
771 if test_timeout > 0:
James E. Blairff80a2f2013-12-27 13:24:06 -0800772 self.useFixture(fixtures.Timeout(test_timeout, gentle=False))
Monty Taylorbc758832013-06-17 17:22:42 -0400773
774 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
775 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
776 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
777 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
778 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
779 os.environ.get('OS_STDERR_CAPTURE') == '1'):
780 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
781 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Monty Taylore6a6c402013-07-02 09:25:55 -0700782 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
783 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blaire2819012013-06-28 17:17:26 -0400784 self.useFixture(fixtures.FakeLogger(
785 level=logging.DEBUG,
786 format='%(asctime)s %(name)-32s '
787 '%(levelname)-8s %(message)s'))
Monty Taylor5a5a95c2013-07-03 13:39:14 -0700788 tmp_root = self.useFixture(fixtures.TempDir(
789 rootdir=os.environ.get("ZUUL_TEST_ROOT"))).path
Monty Taylorbc758832013-06-17 17:22:42 -0400790 self.test_root = os.path.join(tmp_root, "zuul-test")
791 self.upstream_root = os.path.join(self.test_root, "upstream")
792 self.git_root = os.path.join(self.test_root, "git")
793
James E. Blair4076e2b2014-01-28 12:42:20 -0800794 CONFIG.set('merger', 'git_dir', self.git_root)
Monty Taylorbc758832013-06-17 17:22:42 -0400795 if os.path.exists(self.test_root):
796 shutil.rmtree(self.test_root)
797 os.makedirs(self.test_root)
798 os.makedirs(self.upstream_root)
799 os.makedirs(self.git_root)
James E. Blair4886cc12012-07-18 15:39:41 -0700800
801 # For each project in config:
Monty Taylorbc758832013-06-17 17:22:42 -0400802 self.init_repo("org/project")
803 self.init_repo("org/project1")
804 self.init_repo("org/project2")
805 self.init_repo("org/project3")
806 self.init_repo("org/one-job-project")
807 self.init_repo("org/nonvoting-project")
808 self.init_repo("org/templated-project")
James E. Blair3e98c022013-12-16 15:25:38 -0800809 self.init_repo("org/layered-project")
Monty Taylorbc758832013-06-17 17:22:42 -0400810 self.init_repo("org/node-project")
James E. Blair6736beb2013-07-11 15:18:15 -0700811 self.init_repo("org/conflict-project")
James E. Blair18c64442014-03-18 10:14:45 -0700812 self.init_repo("org/noop-project")
James E. Blair412e5582013-04-22 15:50:12 -0700813
814 self.statsd = FakeStatsd()
815 os.environ['STATSD_HOST'] = 'localhost'
816 os.environ['STATSD_PORT'] = str(self.statsd.port)
817 self.statsd.start()
818 # the statsd client object is configured in the statsd module import
819 reload(statsd)
820 reload(zuul.scheduler)
821
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700822 self.gearman_server = FakeGearmanServer()
823
824 self.config = ConfigParser.ConfigParser()
825 cfg = StringIO()
826 CONFIG.write(cfg)
827 cfg.seek(0)
828 self.config.readfp(cfg)
829 self.config.set('gearman', 'port', str(self.gearman_server.port))
830
Monty Taylorbc758832013-06-17 17:22:42 -0400831 self.worker = FakeWorker('fake_worker', self)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700832 self.worker.addServer('127.0.0.1', self.gearman_server.port)
833 self.gearman_server.worker = self.worker
834
James E. Blair4076e2b2014-01-28 12:42:20 -0800835 self.merge_server = zuul.merger.server.MergeServer(self.config)
836 self.merge_server.start()
837
James E. Blairb0fcae42012-07-17 11:12:10 -0700838 self.sched = zuul.scheduler.Scheduler()
839
Joshua Hesketh36c3fa52014-01-22 11:40:52 +1100840 self.useFixture(fixtures.MonkeyPatch('swiftclient.client.Connection',
841 FakeSwiftClientConnection))
842 self.swift = zuul.lib.swift.Swift(self.config)
843
James E. Blair8cc15a82012-08-01 11:17:57 -0700844 def URLOpenerFactory(*args, **kw):
845 args = [self.fake_gerrit] + list(args)
Monty Taylorbc758832013-06-17 17:22:42 -0400846 return FakeURLOpener(self.upstream_root, *args, **kw)
James E. Blair8cc15a82012-08-01 11:17:57 -0700847
James E. Blair8cc15a82012-08-01 11:17:57 -0700848 urllib2.urlopen = URLOpenerFactory
Joshua Hesketh36c3fa52014-01-22 11:40:52 +1100849
850 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched,
851 self.swift)
James E. Blair4076e2b2014-01-28 12:42:20 -0800852 self.merge_client = zuul.merger.client.MergeClient(
853 self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700854
James E. Blairff80a2f2013-12-27 13:24:06 -0800855 self.smtp_messages = []
856
857 def FakeSMTPFactory(*args, **kw):
858 args = [self.smtp_messages] + list(args)
859 return FakeSMTP(*args, **kw)
860
James E. Blairb0fcae42012-07-17 11:12:10 -0700861 zuul.lib.gerrit.Gerrit = FakeGerrit
James E. Blairff80a2f2013-12-27 13:24:06 -0800862 self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTPFactory))
James E. Blairb0fcae42012-07-17 11:12:10 -0700863
Monty Taylorbc758832013-06-17 17:22:42 -0400864 self.gerrit = FakeGerritTrigger(
865 self.upstream_root, self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700866 self.gerrit.replication_timeout = 1.5
867 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700868 self.fake_gerrit = self.gerrit.gerrit
Monty Taylorbc758832013-06-17 17:22:42 -0400869 self.fake_gerrit.upstream_root = self.upstream_root
James E. Blairb0fcae42012-07-17 11:12:10 -0700870
James E. Blair1843a552013-07-03 14:19:52 -0700871 self.webapp = zuul.webapp.WebApp(self.sched, port=0)
James E. Blairad28e912013-11-27 10:43:22 -0800872 self.rpc = zuul.rpclistener.RPCListener(self.config, self.sched)
James E. Blair1843a552013-07-03 14:19:52 -0700873
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700874 self.sched.setLauncher(self.launcher)
James E. Blair4076e2b2014-01-28 12:42:20 -0800875 self.sched.setMerger(self.merge_client)
James E. Blair6c358e72013-07-29 17:06:47 -0700876 self.sched.registerTrigger(self.gerrit)
James E. Blair63bb0ef2013-07-29 17:14:51 -0700877 self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
878 self.sched.registerTrigger(self.timer)
James E. Blairb0fcae42012-07-17 11:12:10 -0700879
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000880 self.sched.registerReporter(
881 zuul.reporter.gerrit.Reporter(self.gerrit))
Joshua Hesketh5fea8672013-08-19 17:32:01 +1000882 self.smtp_reporter = zuul.reporter.smtp.Reporter(
883 self.config.get('smtp', 'default_from'),
884 self.config.get('smtp', 'default_to'),
885 self.config.get('smtp', 'server'))
886 self.sched.registerReporter(self.smtp_reporter)
Joshua Hesketh1879cf72013-08-19 14:13:15 +1000887
James E. Blairb0fcae42012-07-17 11:12:10 -0700888 self.sched.start()
889 self.sched.reconfigure(self.config)
890 self.sched.resume()
James E. Blair1843a552013-07-03 14:19:52 -0700891 self.webapp.start()
James E. Blairad28e912013-11-27 10:43:22 -0800892 self.rpc.start()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700893 self.launcher.gearman.waitForServer()
894 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400895 self.builds = self.worker.running_builds
896 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700897
James E. Blairfee8d652013-06-07 08:57:52 -0700898 self.addCleanup(self.assertFinalState)
899 self.addCleanup(self.shutdown)
900
901 def assertFinalState(self):
902 # Make sure that the change cache is cleared
James E. Blair6c358e72013-07-29 17:06:47 -0700903 self.assertEqual(len(self.gerrit._change_cache.keys()), 0)
Clark Boylan4ba48d92013-11-11 18:03:53 -0800904 # Make sure that git.Repo objects have been garbage collected.
905 repos = []
906 gc.collect()
907 for obj in gc.get_objects():
908 if isinstance(obj, git.Repo):
909 repos.append(obj)
910 self.assertEqual(len(repos), 0)
James E. Blairfee8d652013-06-07 08:57:52 -0700911 self.assertEmptyQueues()
912
913 def shutdown(self):
914 self.log.debug("Shutting down after tests")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700915 self.launcher.stop()
James E. Blair4076e2b2014-01-28 12:42:20 -0800916 self.merge_server.stop()
917 self.merge_server.join()
918 self.merge_client.stop()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700919 self.worker.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700920 self.gerrit.stop()
James E. Blair63bb0ef2013-07-29 17:14:51 -0700921 self.timer.stop()
James E. Blairb0fcae42012-07-17 11:12:10 -0700922 self.sched.stop()
923 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700924 self.statsd.stop()
925 self.statsd.join()
James E. Blair1843a552013-07-03 14:19:52 -0700926 self.webapp.stop()
927 self.webapp.join()
James E. Blairad28e912013-11-27 10:43:22 -0800928 self.rpc.stop()
929 self.rpc.join()
James E. Blair491b3172014-03-10 15:05:36 -0700930 self.gearman_server.shutdown()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700931 threads = threading.enumerate()
932 if len(threads) > 1:
933 self.log.error("More than one thread is running: %s" % threads)
Monty Taylorbc758832013-06-17 17:22:42 -0400934 super(TestScheduler, self).tearDown()
935
936 def init_repo(self, project):
937 parts = project.split('/')
938 path = os.path.join(self.upstream_root, *parts[:-1])
939 if not os.path.exists(path):
940 os.makedirs(path)
941 path = os.path.join(self.upstream_root, project)
942 repo = git.Repo.init(path)
943
944 repo.config_writer().set_value('user', 'email', 'user@example.com')
945 repo.config_writer().set_value('user', 'name', 'User Name')
946 repo.config_writer().write()
947
948 fn = os.path.join(path, 'README')
949 f = open(fn, 'w')
950 f.write("test\n")
951 f.close()
952 repo.index.add([fn])
953 repo.index.commit('initial commit')
954 master = repo.create_head('master')
955 repo.create_tag('init')
956
957 mp = repo.create_head('mp')
958 repo.head.reference = mp
959 f = open(fn, 'a')
960 f.write("test mp\n")
961 f.close()
962 repo.index.add([fn])
963 repo.index.commit('mp commit')
964
965 repo.head.reference = master
966 repo.head.reset(index=True, working_tree=True)
967 repo.git.clean('-x', '-f', '-d')
968
969 def ref_has_change(self, ref, change):
970 path = os.path.join(self.git_root, change.project)
971 repo = git.Repo(path)
972 for commit in repo.iter_commits(ref):
973 if commit.message.strip() == ('%s-1' % change.subject):
974 return True
975 return False
976
977 def job_has_changes(self, *args):
978 job = args[0]
979 commits = args[1:]
980 if isinstance(job, FakeBuild):
981 parameters = job.parameters
982 else:
983 parameters = json.loads(job.arguments)
984 project = parameters['ZUUL_PROJECT']
985 path = os.path.join(self.git_root, project)
986 repo = git.Repo(path)
987 ref = parameters['ZUUL_REF']
988 sha = parameters['ZUUL_COMMIT']
989 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
990 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
991 commit_messages = ['%s-1' % commit.subject for commit in commits]
James E. Blairac2c3242014-01-24 13:38:51 -0800992 self.log.debug("Checking if job %s has changes; commit_messages %s;"
993 " repo_messages %s; sha %s" % (job, commit_messages,
994 repo_messages, sha))
Monty Taylorbc758832013-06-17 17:22:42 -0400995 for msg in commit_messages:
996 if msg not in repo_messages:
James E. Blairac2c3242014-01-24 13:38:51 -0800997 self.log.debug(" messages do not match")
Monty Taylorbc758832013-06-17 17:22:42 -0400998 return False
999 if repo_shas[0] != sha:
James E. Blairac2c3242014-01-24 13:38:51 -08001000 self.log.debug(" sha does not match")
Monty Taylorbc758832013-06-17 17:22:42 -04001001 return False
James E. Blairac2c3242014-01-24 13:38:51 -08001002 self.log.debug(" OK")
Monty Taylorbc758832013-06-17 17:22:42 -04001003 return True
James E. Blairb0fcae42012-07-17 11:12:10 -07001004
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001005 def registerJobs(self):
1006 count = 0
James E. Blaireff88162013-07-01 12:44:14 -04001007 for job in self.sched.layout.jobs.keys():
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001008 self.worker.registerFunction('build:' + job)
1009 count += 1
1010 self.worker.registerFunction('stop:' + self.worker.worker_id)
1011 count += 1
1012
1013 while len(self.gearman_server.functions) < count:
1014 time.sleep(0)
1015
1016 def release(self, job):
1017 if isinstance(job, FakeBuild):
1018 job.release()
1019 else:
1020 job.waiting = False
1021 self.log.debug("Queued job %s released" % job.unique)
1022 self.gearman_server.wakeConnections()
1023
1024 def getParameter(self, job, name):
1025 if isinstance(job, FakeBuild):
1026 return job.parameters[name]
1027 else:
1028 parameters = json.loads(job.arguments)
1029 return parameters[name]
1030
1031 def resetGearmanServer(self):
1032 self.worker.setFunctions([])
1033 while True:
1034 done = True
1035 for connection in self.gearman_server.active_connections:
James E. Blairad28e912013-11-27 10:43:22 -08001036 if (connection.functions and
James E. Blair4076e2b2014-01-28 12:42:20 -08001037 connection.client_id not in ['Zuul RPC Listener',
1038 'Zuul Merger']):
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001039 done = False
1040 if done:
1041 break
1042 time.sleep(0)
1043 self.gearman_server.functions = set()
James E. Blairad28e912013-11-27 10:43:22 -08001044 self.rpc.register()
James E. Blair4076e2b2014-01-28 12:42:20 -08001045 self.merge_server.register()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001046
1047 def haveAllBuildsReported(self):
1048 # See if Zuul is waiting on a meta job to complete
1049 if self.launcher.meta_jobs:
1050 return False
1051 # Find out if every build that the worker has completed has been
1052 # reported back to Zuul. If it hasn't then that means a Gearman
1053 # event is still in transit and the system is not stable.
1054 for build in self.worker.build_history:
1055 zbuild = self.launcher.builds.get(build.uuid)
1056 if not zbuild:
1057 # It has already been reported
1058 continue
1059 # It hasn't been reported yet.
1060 return False
1061 # Make sure that none of the worker connections are in GRAB_WAIT
1062 for connection in self.worker.active_connections:
1063 if connection.state == 'GRAB_WAIT':
1064 return False
1065 return True
1066
1067 def areAllBuildsWaiting(self):
1068 ret = True
1069
1070 builds = self.launcher.builds.values()
1071 for build in builds:
1072 client_job = None
1073 for conn in self.launcher.gearman.active_connections:
1074 for j in conn.related_jobs.values():
1075 if j.unique == build.uuid:
1076 client_job = j
1077 break
1078 if not client_job:
1079 self.log.debug("%s is not known to the gearman client" %
1080 build)
1081 ret = False
1082 continue
1083 if not client_job.handle:
1084 self.log.debug("%s has no handle" % client_job)
1085 ret = False
1086 continue
1087 server_job = self.gearman_server.jobs.get(client_job.handle)
1088 if not server_job:
1089 self.log.debug("%s is not known to the gearman server" %
1090 client_job)
1091 ret = False
1092 continue
1093 if not hasattr(server_job, 'waiting'):
1094 self.log.debug("%s is being enqueued" % server_job)
1095 ret = False
1096 continue
1097 if server_job.waiting:
1098 continue
1099 worker_job = self.worker.gearman_jobs.get(server_job.unique)
1100 if worker_job:
1101 if worker_job.build.isWaiting():
1102 continue
1103 else:
1104 self.log.debug("%s is running" % worker_job)
1105 ret = False
1106 else:
1107 self.log.debug("%s is unassigned" % server_job)
1108 ret = False
1109 return ret
1110
James E. Blairb0fcae42012-07-17 11:12:10 -07001111 def waitUntilSettled(self):
1112 self.log.debug("Waiting until settled...")
1113 start = time.time()
1114 while True:
1115 if time.time() - start > 10:
1116 print 'queue status:',
1117 print self.sched.trigger_event_queue.empty(),
1118 print self.sched.result_event_queue.empty(),
1119 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001120 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -07001121 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001122 # Make sure no new events show up while we're checking
1123 self.worker.lock.acquire()
1124 # have all build states propogated to zuul?
1125 if self.haveAllBuildsReported():
1126 # Join ensures that the queue is empty _and_ events have been
1127 # processed
1128 self.fake_gerrit.event_queue.join()
1129 self.sched.trigger_event_queue.join()
1130 self.sched.result_event_queue.join()
James E. Blaira84f0e42014-02-06 07:09:22 -08001131 self.sched.run_handler_lock.acquire()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001132 if (self.sched.trigger_event_queue.empty() and
1133 self.sched.result_event_queue.empty() and
1134 self.fake_gerrit.event_queue.empty() and
James E. Blair4076e2b2014-01-28 12:42:20 -08001135 not self.merge_client.build_sets and
James E. Blair9c2985a2014-03-10 10:59:05 -07001136 self.haveAllBuildsReported() and
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001137 self.areAllBuildsWaiting()):
James E. Blaira84f0e42014-02-06 07:09:22 -08001138 self.sched.run_handler_lock.release()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001139 self.worker.lock.release()
1140 self.log.debug("...settled.")
1141 return
James E. Blaira84f0e42014-02-06 07:09:22 -08001142 self.sched.run_handler_lock.release()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001143 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001144 self.sched.wake_event.wait(0.1)
1145
James E. Blaird466dc42012-07-31 10:42:56 -07001146 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -08001147 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -07001148 return len(jobs)
1149
James E. Blair4ca985f2013-05-30 12:27:43 -07001150 def getJobFromHistory(self, name):
1151 history = self.worker.build_history
1152 for job in history:
1153 if job.name == name:
1154 return job
1155 raise Exception("Unable to find job %s in history" % name)
1156
James E. Blaire0487072012-08-29 17:38:31 -07001157 def assertEmptyQueues(self):
1158 # Make sure there are no orphaned jobs
James E. Blaireff88162013-07-01 12:44:14 -04001159 for pipeline in self.sched.layout.pipelines.values():
James E. Blaire0487072012-08-29 17:38:31 -07001160 for queue in pipeline.queues:
1161 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -08001162 print 'pipeline %s queue %s contents %s' % (
1163 pipeline.name, queue.name, queue.queue)
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001164 self.assertEqual(len(queue.queue), 0)
James E. Blaire0487072012-08-29 17:38:31 -07001165
James E. Blair66eeebf2013-07-27 17:44:32 -07001166 def assertReportedStat(self, key, value=None, kind=None):
James E. Blair412e5582013-04-22 15:50:12 -07001167 start = time.time()
1168 while time.time() < (start + 5):
1169 for stat in self.statsd.stats:
James E. Blair66eeebf2013-07-27 17:44:32 -07001170 pprint.pprint(self.statsd.stats)
James E. Blair412e5582013-04-22 15:50:12 -07001171 k, v = stat.split(':')
1172 if key == k:
James E. Blair66eeebf2013-07-27 17:44:32 -07001173 if value is None and kind is None:
James E. Blair412e5582013-04-22 15:50:12 -07001174 return
James E. Blair66eeebf2013-07-27 17:44:32 -07001175 elif value:
1176 if value == v:
1177 return
1178 elif kind:
1179 if v.endswith('|' + kind):
1180 return
James E. Blair412e5582013-04-22 15:50:12 -07001181 time.sleep(0.1)
1182
1183 pprint.pprint(self.statsd.stats)
1184 raise Exception("Key %s not found in reported stats" % key)
1185
James E. Blairb0fcae42012-07-17 11:12:10 -07001186 def test_jobs_launched(self):
1187 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001188
James E. Blairb0fcae42012-07-17 11:12:10 -07001189 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -07001190 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001191 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1192 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001193 self.assertEqual(self.getJobFromHistory('project-merge').result,
1194 'SUCCESS')
1195 self.assertEqual(self.getJobFromHistory('project-test1').result,
1196 'SUCCESS')
1197 self.assertEqual(self.getJobFromHistory('project-test2').result,
1198 'SUCCESS')
1199 self.assertEqual(A.data['status'], 'MERGED')
1200 self.assertEqual(A.reported, 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001201
James E. Blair66eeebf2013-07-27 17:44:32 -07001202 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
1203 self.assertReportedStat('zuul.pipeline.gate.current_changes',
1204 value='1|g')
1205 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1206 kind='ms')
1207 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
1208 value='1|c')
1209 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
1210 self.assertReportedStat('zuul.pipeline.gate.total_changes',
1211 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001212 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001213 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -07001214 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -07001215 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -07001216
James E. Blair3cb10702013-08-24 08:56:03 -07001217 def test_initial_pipeline_gauges(self):
1218 "Test that each pipeline reported its length on start"
1219 pipeline_names = self.sched.layout.pipelines.keys()
1220 self.assertNotEqual(len(pipeline_names), 0)
1221 for name in pipeline_names:
1222 self.assertReportedStat('zuul.pipeline.%s.current_changes' % name,
1223 value='0|g')
1224
James E. Blair42f74822013-05-14 15:18:03 -07001225 def test_duplicate_pipelines(self):
1226 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -07001227
James E. Blair42f74822013-05-14 15:18:03 -07001228 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1229 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
1230 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -07001231
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001232 self.assertEqual(len(self.history), 2)
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001233 self.history[0].name == 'project-test1'
1234 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001235
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001236 self.assertEqual(len(A.messages), 2)
James E. Blair42f74822013-05-14 15:18:03 -07001237 if 'dup1/project-test1' in A.messages[0]:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001238 self.assertIn('dup1/project-test1', A.messages[0])
1239 self.assertNotIn('dup2/project-test1', A.messages[0])
1240 self.assertNotIn('dup1/project-test1', A.messages[1])
1241 self.assertIn('dup2/project-test1', A.messages[1])
James E. Blair42f74822013-05-14 15:18:03 -07001242 else:
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001243 self.assertIn('dup1/project-test1', A.messages[1])
1244 self.assertNotIn('dup2/project-test1', A.messages[1])
1245 self.assertNotIn('dup1/project-test1', A.messages[0])
1246 self.assertIn('dup2/project-test1', A.messages[0])
James E. Blair42f74822013-05-14 15:18:03 -07001247
James E. Blairb0fcae42012-07-17 11:12:10 -07001248 def test_parallel_changes(self):
1249 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001250
1251 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001252 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1253 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1254 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001255 A.addApproval('CRVW', 2)
1256 B.addApproval('CRVW', 2)
1257 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001258
1259 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1260 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1261 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1262
1263 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001264 self.assertEqual(len(self.builds), 1)
1265 self.assertEqual(self.builds[0].name, 'project-merge')
1266 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001267
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001268 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001269 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001270 self.assertEqual(len(self.builds), 3)
1271 self.assertEqual(self.builds[0].name, 'project-test1')
1272 self.assertTrue(self.job_has_changes(self.builds[0], A))
1273 self.assertEqual(self.builds[1].name, 'project-test2')
1274 self.assertTrue(self.job_has_changes(self.builds[1], A))
1275 self.assertEqual(self.builds[2].name, 'project-merge')
1276 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001277
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001278 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001279 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001280 self.assertEqual(len(self.builds), 5)
1281 self.assertEqual(self.builds[0].name, 'project-test1')
1282 self.assertTrue(self.job_has_changes(self.builds[0], A))
1283 self.assertEqual(self.builds[1].name, 'project-test2')
1284 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001285
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001286 self.assertEqual(self.builds[2].name, 'project-test1')
1287 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1288 self.assertEqual(self.builds[3].name, 'project-test2')
1289 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001290
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001291 self.assertEqual(self.builds[4].name, 'project-merge')
1292 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001293
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001294 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001295 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001296 self.assertEqual(len(self.builds), 6)
1297 self.assertEqual(self.builds[0].name, 'project-test1')
1298 self.assertTrue(self.job_has_changes(self.builds[0], A))
1299 self.assertEqual(self.builds[1].name, 'project-test2')
1300 self.assertTrue(self.job_has_changes(self.builds[1], A))
James E. Blairb0fcae42012-07-17 11:12:10 -07001301
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001302 self.assertEqual(self.builds[2].name, 'project-test1')
1303 self.assertTrue(self.job_has_changes(self.builds[2], A, B))
1304 self.assertEqual(self.builds[3].name, 'project-test2')
1305 self.assertTrue(self.job_has_changes(self.builds[3], A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -07001306
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001307 self.assertEqual(self.builds[4].name, 'project-test1')
1308 self.assertTrue(self.job_has_changes(self.builds[4], A, B, C))
1309 self.assertEqual(self.builds[5].name, 'project-test2')
1310 self.assertTrue(self.job_has_changes(self.builds[5], A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -07001311
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001312 self.worker.hold_jobs_in_build = False
1313 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001314 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001315 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -07001316
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001317 self.assertEqual(len(self.history), 9)
1318 self.assertEqual(A.data['status'], 'MERGED')
1319 self.assertEqual(B.data['status'], 'MERGED')
1320 self.assertEqual(C.data['status'], 'MERGED')
1321 self.assertEqual(A.reported, 2)
1322 self.assertEqual(B.reported, 2)
1323 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001324
1325 def test_failed_changes(self):
1326 "Test that a change behind a failed change is retested"
James E. Blaire2819012013-06-28 17:17:26 -04001327 self.worker.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001328
James E. Blairb02a3bb2012-07-30 17:49:55 -07001329 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1330 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001331 A.addApproval('CRVW', 2)
1332 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001333
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001334 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001335
James E. Blaire2819012013-06-28 17:17:26 -04001336 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1337 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001338 self.waitUntilSettled()
James E. Blaire2819012013-06-28 17:17:26 -04001339
1340 self.worker.release('.*-merge')
1341 self.waitUntilSettled()
1342
1343 self.worker.hold_jobs_in_build = False
1344 self.worker.release()
1345
1346 self.waitUntilSettled()
1347 # It's certain that the merge job for change 2 will run, but
1348 # the test1 and test2 jobs may or may not run.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001349 self.assertTrue(len(self.history) > 6)
1350 self.assertEqual(A.data['status'], 'NEW')
1351 self.assertEqual(B.data['status'], 'MERGED')
1352 self.assertEqual(A.reported, 2)
1353 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001354
1355 def test_independent_queues(self):
1356 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001357
1358 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001359 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001360 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1361 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001362 A.addApproval('CRVW', 2)
1363 B.addApproval('CRVW', 2)
1364 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001365
1366 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1367 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1368 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1369
James E. Blairb02a3bb2012-07-30 17:49:55 -07001370 self.waitUntilSettled()
1371
1372 # There should be one merge job at the head of each queue running
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001373 self.assertEqual(len(self.builds), 2)
1374 self.assertEqual(self.builds[0].name, 'project-merge')
1375 self.assertTrue(self.job_has_changes(self.builds[0], A))
1376 self.assertEqual(self.builds[1].name, 'project1-merge')
1377 self.assertTrue(self.job_has_changes(self.builds[1], B))
James E. Blairb02a3bb2012-07-30 17:49:55 -07001378
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001379 # Release the current merge builds
1380 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001381 self.waitUntilSettled()
1382 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001383 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001384 self.waitUntilSettled()
1385
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001386 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001387 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001388 self.assertEqual(len(self.builds), 8)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001389
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001390 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001391 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001392 self.assertEqual(len(self.builds), 0)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001393
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001394 self.assertEqual(len(self.history), 11)
1395 self.assertEqual(A.data['status'], 'MERGED')
1396 self.assertEqual(B.data['status'], 'MERGED')
1397 self.assertEqual(C.data['status'], 'MERGED')
1398 self.assertEqual(A.reported, 2)
1399 self.assertEqual(B.reported, 2)
1400 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001401
1402 def test_failed_change_at_head(self):
1403 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001404
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001405 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001406 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1407 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1408 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001409 A.addApproval('CRVW', 2)
1410 B.addApproval('CRVW', 2)
1411 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001412
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001413 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001414
1415 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1416 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1417 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1418
1419 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001420
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001421 self.assertEqual(len(self.builds), 1)
1422 self.assertEqual(self.builds[0].name, 'project-merge')
1423 self.assertTrue(self.job_has_changes(self.builds[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001424
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001425 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001426 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001427 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001428 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001429 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001430 self.waitUntilSettled()
1431
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001432 self.assertEqual(len(self.builds), 6)
1433 self.assertEqual(self.builds[0].name, 'project-test1')
1434 self.assertEqual(self.builds[1].name, 'project-test2')
1435 self.assertEqual(self.builds[2].name, 'project-test1')
1436 self.assertEqual(self.builds[3].name, 'project-test2')
1437 self.assertEqual(self.builds[4].name, 'project-test1')
1438 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001439
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001440 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001441 self.waitUntilSettled()
1442
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001443 # project-test2, project-merge for B
1444 self.assertEqual(len(self.builds), 2)
1445 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blaird466dc42012-07-31 10:42:56 -07001446
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001447 self.worker.hold_jobs_in_build = False
1448 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001449 self.waitUntilSettled()
1450
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001451 self.assertEqual(len(self.builds), 0)
1452 self.assertEqual(len(self.history), 15)
1453 self.assertEqual(A.data['status'], 'NEW')
1454 self.assertEqual(B.data['status'], 'MERGED')
1455 self.assertEqual(C.data['status'], 'MERGED')
1456 self.assertEqual(A.reported, 2)
1457 self.assertEqual(B.reported, 2)
1458 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001459
James E. Blair0aac4872013-08-23 14:02:38 -07001460 def test_failed_change_in_middle(self):
1461 "Test a failed change in the middle of the queue"
1462
1463 self.worker.hold_jobs_in_build = True
1464 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1465 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1466 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1467 A.addApproval('CRVW', 2)
1468 B.addApproval('CRVW', 2)
1469 C.addApproval('CRVW', 2)
1470
1471 self.worker.addFailTest('project-test1', B)
1472
1473 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1474 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1475 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1476
1477 self.waitUntilSettled()
1478
1479 self.worker.release('.*-merge')
1480 self.waitUntilSettled()
1481 self.worker.release('.*-merge')
1482 self.waitUntilSettled()
1483 self.worker.release('.*-merge')
1484 self.waitUntilSettled()
1485
1486 self.assertEqual(len(self.builds), 6)
1487 self.assertEqual(self.builds[0].name, 'project-test1')
1488 self.assertEqual(self.builds[1].name, 'project-test2')
1489 self.assertEqual(self.builds[2].name, 'project-test1')
1490 self.assertEqual(self.builds[3].name, 'project-test2')
1491 self.assertEqual(self.builds[4].name, 'project-test1')
1492 self.assertEqual(self.builds[5].name, 'project-test2')
1493
1494 self.release(self.builds[2])
1495 self.waitUntilSettled()
1496
James E. Blair972e3c72013-08-29 12:04:55 -07001497 # project-test1 and project-test2 for A
1498 # project-test2 for B
1499 # project-merge for C (without B)
1500 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -07001501 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
1502
James E. Blair972e3c72013-08-29 12:04:55 -07001503 self.worker.release('.*-merge')
1504 self.waitUntilSettled()
1505
1506 # project-test1 and project-test2 for A
1507 # project-test2 for B
1508 # project-test1 and project-test2 for C
1509 self.assertEqual(len(self.builds), 5)
1510
James E. Blair0aac4872013-08-23 14:02:38 -07001511 items = self.sched.layout.pipelines['gate'].getAllItems()
1512 builds = items[0].current_build_set.getBuilds()
1513 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1514 self.assertEqual(self.countJobResults(builds, None), 2)
1515 builds = items[1].current_build_set.getBuilds()
1516 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
1517 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
1518 self.assertEqual(self.countJobResults(builds, None), 1)
1519 builds = items[2].current_build_set.getBuilds()
1520 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -07001521 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -07001522
1523 self.worker.hold_jobs_in_build = False
1524 self.worker.release()
1525 self.waitUntilSettled()
1526
1527 self.assertEqual(len(self.builds), 0)
1528 self.assertEqual(len(self.history), 12)
1529 self.assertEqual(A.data['status'], 'MERGED')
1530 self.assertEqual(B.data['status'], 'NEW')
1531 self.assertEqual(C.data['status'], 'MERGED')
1532 self.assertEqual(A.reported, 2)
1533 self.assertEqual(B.reported, 2)
1534 self.assertEqual(C.reported, 2)
1535
James E. Blaird466dc42012-07-31 10:42:56 -07001536 def test_failed_change_at_head_with_queue(self):
1537 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001538
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001539 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001540 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1541 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1542 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001543 A.addApproval('CRVW', 2)
1544 B.addApproval('CRVW', 2)
1545 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001546
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001547 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001548
1549 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1550 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1551 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1552
1553 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001554 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001555 self.assertEqual(len(self.builds), 0)
1556 self.assertEqual(len(queue), 1)
1557 self.assertEqual(queue[0].name, 'build:project-merge')
1558 self.assertTrue(self.job_has_changes(queue[0], A))
James E. Blaird466dc42012-07-31 10:42:56 -07001559
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001560 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001561 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001562 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001563 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001564 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001565 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001566 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001567
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001568 self.assertEqual(len(self.builds), 0)
1569 self.assertEqual(len(queue), 6)
1570 self.assertEqual(queue[0].name, 'build:project-test1')
1571 self.assertEqual(queue[1].name, 'build:project-test2')
1572 self.assertEqual(queue[2].name, 'build:project-test1')
1573 self.assertEqual(queue[3].name, 'build:project-test2')
1574 self.assertEqual(queue[4].name, 'build:project-test1')
1575 self.assertEqual(queue[5].name, 'build:project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -07001576
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001577 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001578 self.waitUntilSettled()
1579
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001580 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -07001581 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001582 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
1583 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -07001584
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001585 self.gearman_server.hold_jobs_in_queue = False
1586 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001587 self.waitUntilSettled()
1588
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001589 self.assertEqual(len(self.builds), 0)
1590 self.assertEqual(len(self.history), 11)
1591 self.assertEqual(A.data['status'], 'NEW')
1592 self.assertEqual(B.data['status'], 'MERGED')
1593 self.assertEqual(C.data['status'], 'MERGED')
1594 self.assertEqual(A.reported, 2)
1595 self.assertEqual(B.reported, 2)
1596 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001597
James E. Blairfef71632013-09-23 11:15:47 -07001598 def test_two_failed_changes_at_head(self):
1599 "Test that changes are reparented correctly if 2 fail at head"
1600
1601 self.worker.hold_jobs_in_build = True
1602 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1603 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1604 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1605 A.addApproval('CRVW', 2)
1606 B.addApproval('CRVW', 2)
1607 C.addApproval('CRVW', 2)
1608
1609 self.worker.addFailTest('project-test1', A)
1610 self.worker.addFailTest('project-test1', B)
1611
1612 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1613 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1614 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1615 self.waitUntilSettled()
1616
1617 self.worker.release('.*-merge')
1618 self.waitUntilSettled()
1619 self.worker.release('.*-merge')
1620 self.waitUntilSettled()
1621 self.worker.release('.*-merge')
1622 self.waitUntilSettled()
1623
1624 self.assertEqual(len(self.builds), 6)
1625 self.assertEqual(self.builds[0].name, 'project-test1')
1626 self.assertEqual(self.builds[1].name, 'project-test2')
1627 self.assertEqual(self.builds[2].name, 'project-test1')
1628 self.assertEqual(self.builds[3].name, 'project-test2')
1629 self.assertEqual(self.builds[4].name, 'project-test1')
1630 self.assertEqual(self.builds[5].name, 'project-test2')
1631
1632 self.assertTrue(self.job_has_changes(self.builds[0], A))
1633 self.assertTrue(self.job_has_changes(self.builds[2], A))
1634 self.assertTrue(self.job_has_changes(self.builds[2], B))
1635 self.assertTrue(self.job_has_changes(self.builds[4], A))
1636 self.assertTrue(self.job_has_changes(self.builds[4], B))
1637 self.assertTrue(self.job_has_changes(self.builds[4], C))
1638
1639 # Fail change B first
1640 self.release(self.builds[2])
1641 self.waitUntilSettled()
1642
1643 # restart of C after B failure
1644 self.worker.release('.*-merge')
1645 self.waitUntilSettled()
1646
1647 self.assertEqual(len(self.builds), 5)
1648 self.assertEqual(self.builds[0].name, 'project-test1')
1649 self.assertEqual(self.builds[1].name, 'project-test2')
1650 self.assertEqual(self.builds[2].name, 'project-test2')
1651 self.assertEqual(self.builds[3].name, 'project-test1')
1652 self.assertEqual(self.builds[4].name, 'project-test2')
1653
1654 self.assertTrue(self.job_has_changes(self.builds[1], A))
1655 self.assertTrue(self.job_has_changes(self.builds[2], A))
1656 self.assertTrue(self.job_has_changes(self.builds[2], B))
1657 self.assertTrue(self.job_has_changes(self.builds[4], A))
1658 self.assertFalse(self.job_has_changes(self.builds[4], B))
1659 self.assertTrue(self.job_has_changes(self.builds[4], C))
1660
1661 # Finish running all passing jobs for change A
1662 self.release(self.builds[1])
1663 self.waitUntilSettled()
1664 # Fail and report change A
1665 self.release(self.builds[0])
1666 self.waitUntilSettled()
1667
1668 # restart of B,C after A failure
1669 self.worker.release('.*-merge')
1670 self.waitUntilSettled()
1671 self.worker.release('.*-merge')
1672 self.waitUntilSettled()
1673
1674 self.assertEqual(len(self.builds), 4)
1675 self.assertEqual(self.builds[0].name, 'project-test1') # B
1676 self.assertEqual(self.builds[1].name, 'project-test2') # B
1677 self.assertEqual(self.builds[2].name, 'project-test1') # C
1678 self.assertEqual(self.builds[3].name, 'project-test2') # C
1679
1680 self.assertFalse(self.job_has_changes(self.builds[1], A))
1681 self.assertTrue(self.job_has_changes(self.builds[1], B))
1682 self.assertFalse(self.job_has_changes(self.builds[1], C))
1683
1684 self.assertFalse(self.job_has_changes(self.builds[2], A))
1685 # After A failed and B and C restarted, B should be back in
1686 # C's tests because it has not failed yet.
1687 self.assertTrue(self.job_has_changes(self.builds[2], B))
1688 self.assertTrue(self.job_has_changes(self.builds[2], C))
1689
1690 self.worker.hold_jobs_in_build = False
1691 self.worker.release()
1692 self.waitUntilSettled()
1693
1694 self.assertEqual(len(self.builds), 0)
1695 self.assertEqual(len(self.history), 21)
1696 self.assertEqual(A.data['status'], 'NEW')
1697 self.assertEqual(B.data['status'], 'NEW')
1698 self.assertEqual(C.data['status'], 'MERGED')
1699 self.assertEqual(A.reported, 2)
1700 self.assertEqual(B.reported, 2)
1701 self.assertEqual(C.reported, 2)
1702
James E. Blair8c803f82012-07-31 16:25:42 -07001703 def test_patch_order(self):
1704 "Test that dependent patches are tested in the right order"
1705 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1706 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1707 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1708 A.addApproval('CRVW', 2)
1709 B.addApproval('CRVW', 2)
1710 C.addApproval('CRVW', 2)
1711
1712 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1713 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1714 M2.setMerged()
1715 M1.setMerged()
1716
1717 # C -> B -> A -> M1 -> M2
1718 # M2 is here to make sure it is never queried. If it is, it
1719 # means zuul is walking down the entire history of merged
1720 # changes.
1721
1722 C.setDependsOn(B, 1)
1723 B.setDependsOn(A, 1)
1724 A.setDependsOn(M1, 1)
1725 M1.setDependsOn(M2, 1)
1726
1727 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1728
1729 self.waitUntilSettled()
1730
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001731 self.assertEqual(A.data['status'], 'NEW')
1732 self.assertEqual(B.data['status'], 'NEW')
1733 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -07001734
1735 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1736 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1737
1738 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001739 self.assertEqual(M2.queried, 0)
1740 self.assertEqual(A.data['status'], 'MERGED')
1741 self.assertEqual(B.data['status'], 'MERGED')
1742 self.assertEqual(C.data['status'], 'MERGED')
1743 self.assertEqual(A.reported, 2)
1744 self.assertEqual(B.reported, 2)
1745 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -07001746
James E. Blair0e933c52013-07-11 10:18:52 -07001747 def test_trigger_cache(self):
1748 "Test that the trigger cache operates correctly"
1749 self.worker.hold_jobs_in_build = True
1750
1751 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1752 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1753 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
1754 A.addApproval('CRVW', 2)
1755 B.addApproval('CRVW', 2)
1756
1757 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1758 M1.setMerged()
1759
1760 B.setDependsOn(A, 1)
1761 A.setDependsOn(M1, 1)
1762
1763 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1764 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
1765
1766 self.waitUntilSettled()
1767
1768 for build in self.builds:
1769 if build.parameters['ZUUL_PIPELINE'] == 'check':
1770 build.release()
1771 self.waitUntilSettled()
1772 for build in self.builds:
1773 if build.parameters['ZUUL_PIPELINE'] == 'check':
1774 build.release()
1775 self.waitUntilSettled()
1776
1777 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1778 self.waitUntilSettled()
1779
James E. Blair6c358e72013-07-29 17:06:47 -07001780 self.log.debug("len %s " % self.gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -07001781 # there should still be changes in the cache
James E. Blair6c358e72013-07-29 17:06:47 -07001782 self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -07001783
1784 self.worker.hold_jobs_in_build = False
1785 self.worker.release()
1786 self.waitUntilSettled()
1787
1788 self.assertEqual(A.data['status'], 'MERGED')
1789 self.assertEqual(B.data['status'], 'MERGED')
1790 self.assertEqual(A.queried, 2) # Initial and isMerged
1791 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1792
James E. Blair8c803f82012-07-31 16:25:42 -07001793 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001794 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001795 # TODO: move to test_gerrit (this is a unit test!)
1796 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair6c358e72013-07-29 17:06:47 -07001797 trigger = self.sched.layout.pipelines['gate'].trigger
1798 a = self.sched.triggers['gerrit'].getChange(1, 2)
James E. Blaireff88162013-07-01 12:44:14 -04001799 mgr = self.sched.layout.pipelines['gate'].manager
James E. Blair6c358e72013-07-29 17:06:47 -07001800 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001801
1802 A.addApproval('CRVW', 2)
James E. Blair6c358e72013-07-29 17:06:47 -07001803 a = trigger.getChange(1, 2, refresh=True)
1804 self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001805
1806 A.addApproval('APRV', 1)
James E. Blair6c358e72013-07-29 17:06:47 -07001807 a = trigger.getChange(1, 2, refresh=True)
1808 self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds()))
1809 trigger.maintainCache([])
James E. Blair4886cc12012-07-18 15:39:41 -07001810
1811 def test_build_configuration(self):
1812 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001813
1814 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001815 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1816 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1817 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1818 A.addApproval('CRVW', 2)
1819 B.addApproval('CRVW', 2)
1820 C.addApproval('CRVW', 2)
1821 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1822 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1823 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1824 self.waitUntilSettled()
1825
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001826 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001827 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001828 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001829 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001830 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001831 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001832 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001833 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1834 self.gearman_server.hold_jobs_in_queue = False
1835 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001836 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001837
Monty Taylorbc758832013-06-17 17:22:42 -04001838 path = os.path.join(self.git_root, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001839 repo = git.Repo(path)
1840 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1841 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001842 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001843 self.assertEqual(repo_messages, correct_messages)
James E. Blair973721f2012-08-15 10:19:43 -07001844
1845 def test_build_configuration_conflict(self):
1846 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001847
1848 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -07001849 A = self.fake_gerrit.addFakeChange('org/conflict-project',
1850 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -07001851 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001852 B = self.fake_gerrit.addFakeChange('org/conflict-project',
1853 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -07001854 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -07001855 C = self.fake_gerrit.addFakeChange('org/conflict-project',
1856 'master', 'C')
James E. Blair973721f2012-08-15 10:19:43 -07001857 A.addApproval('CRVW', 2)
1858 B.addApproval('CRVW', 2)
1859 C.addApproval('CRVW', 2)
1860 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1861 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1862 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1863 self.waitUntilSettled()
1864
James E. Blair6736beb2013-07-11 15:18:15 -07001865 self.assertEqual(A.reported, 1)
1866 self.assertEqual(B.reported, 1)
1867 self.assertEqual(C.reported, 1)
1868
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001869 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001870 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001871 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001872 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001873 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001874 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001875
1876 self.assertEqual(len(self.history), 2) # A and C merge jobs
1877
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001878 self.gearman_server.hold_jobs_in_queue = False
1879 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001880 self.waitUntilSettled()
1881
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001882 self.assertEqual(A.data['status'], 'MERGED')
1883 self.assertEqual(B.data['status'], 'NEW')
1884 self.assertEqual(C.data['status'], 'MERGED')
1885 self.assertEqual(A.reported, 2)
1886 self.assertEqual(B.reported, 2)
1887 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001888 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -07001889
James E. Blairdaabed22012-08-15 15:38:57 -07001890 def test_post(self):
1891 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001892
Zhongyue Luo5d556072012-09-21 02:00:47 +09001893 e = {
1894 "type": "ref-updated",
1895 "submitter": {
1896 "name": "User Name",
1897 },
1898 "refUpdate": {
1899 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1900 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1901 "refName": "master",
1902 "project": "org/project",
1903 }
1904 }
James E. Blairdaabed22012-08-15 15:38:57 -07001905 self.fake_gerrit.addEvent(e)
1906 self.waitUntilSettled()
1907
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001908 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001909 self.assertEqual(len(self.history), 1)
1910 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001911
1912 def test_build_configuration_branch(self):
1913 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001914
1915 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001916 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1917 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1918 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1919 A.addApproval('CRVW', 2)
1920 B.addApproval('CRVW', 2)
1921 C.addApproval('CRVW', 2)
1922 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1923 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1924 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1925 self.waitUntilSettled()
1926
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001927 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001928 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001929 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001930 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001931 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001932 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001933 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001934 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1935 self.gearman_server.hold_jobs_in_queue = False
1936 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001937 self.waitUntilSettled()
1938
Monty Taylorbc758832013-06-17 17:22:42 -04001939 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001940 repo = git.Repo(path)
1941 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1942 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001943 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001944 self.assertEqual(repo_messages, correct_messages)
James E. Blairc6294a52012-08-17 10:19:48 -07001945
1946 def test_build_configuration_branch_interaction(self):
1947 "Test that switching between branches works"
1948 self.test_build_configuration()
1949 self.test_build_configuration_branch()
1950 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001951 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001952 repo = git.Repo(path)
1953 repo.heads.master.commit = repo.commit('init')
1954 self.test_build_configuration()
1955
1956 def test_build_configuration_multi_branch(self):
1957 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001958
1959 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001960 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1961 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1962 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1963 A.addApproval('CRVW', 2)
1964 B.addApproval('CRVW', 2)
1965 C.addApproval('CRVW', 2)
1966 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1967 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1968 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1969 self.waitUntilSettled()
James E. Blairbb1fe502014-03-04 10:15:06 -08001970 queue = self.gearman_server.getQueue()
1971 job_A = None
1972 for job in queue:
1973 if 'project-merge' in job.name:
1974 job_A = job
1975 ref_A = self.getParameter(job_A, 'ZUUL_REF')
1976 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
1977 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
1978 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
James E. Blairc6294a52012-08-17 10:19:48 -07001979
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001980 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001981 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001982 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001983 job_B = None
1984 for job in queue:
1985 if 'project-merge' in job.name:
1986 job_B = job
1987 ref_B = self.getParameter(job_B, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001988 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07001989 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
James E. Blairbb1fe502014-03-04 10:15:06 -08001990 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
1991
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001992 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001993 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001994 queue = self.gearman_server.getQueue()
James E. Blaird320d7e2013-07-30 16:36:20 -07001995 for job in queue:
1996 if 'project-merge' in job.name:
1997 job_C = job
1998 ref_C = self.getParameter(job_C, 'ZUUL_REF')
James E. Blairbb1fe502014-03-04 10:15:06 -08001999 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
James E. Blairf750aa02013-07-15 14:11:24 -07002000 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
James E. Blairbb1fe502014-03-04 10:15:06 -08002001 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002002 self.gearman_server.hold_jobs_in_queue = False
2003 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07002004 self.waitUntilSettled()
2005
Monty Taylorbc758832013-06-17 17:22:42 -04002006 path = os.path.join(self.git_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07002007 repo = git.Repo(path)
2008
2009 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07002010 for c in repo.iter_commits(ref_C)]
James E. Blairbb1fe502014-03-04 10:15:06 -08002011 repo_shas = [c.hexsha for c in repo.iter_commits(ref_C)]
James E. Blairc6294a52012-08-17 10:19:48 -07002012 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07002013 correct_messages = ['initial commit', 'A-1', 'C-1']
James E. Blairbb1fe502014-03-04 10:15:06 -08002014 # Ensure the right commits are in the history for this ref
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002015 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08002016 # Ensure ZUUL_REF -> ZUUL_COMMIT
2017 self.assertEqual(repo_shas[0], commit_C)
James E. Blairc6294a52012-08-17 10:19:48 -07002018
2019 repo_messages = [c.message.strip()
James E. Blairf750aa02013-07-15 14:11:24 -07002020 for c in repo.iter_commits(ref_B)]
James E. Blairbb1fe502014-03-04 10:15:06 -08002021 repo_shas = [c.hexsha for c in repo.iter_commits(ref_B)]
James E. Blairc6294a52012-08-17 10:19:48 -07002022 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07002023 correct_messages = ['initial commit', 'mp commit', 'B-1']
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002024 self.assertEqual(repo_messages, correct_messages)
James E. Blairbb1fe502014-03-04 10:15:06 -08002025 self.assertEqual(repo_shas[0], commit_B)
2026
2027 repo_messages = [c.message.strip()
2028 for c in repo.iter_commits(ref_A)]
2029 repo_shas = [c.hexsha for c in repo.iter_commits(ref_A)]
2030 repo_messages.reverse()
2031 correct_messages = ['initial commit', 'A-1']
2032 self.assertEqual(repo_messages, correct_messages)
2033 self.assertEqual(repo_shas[0], commit_A)
2034
2035 self.assertNotEqual(ref_A, ref_B, ref_C)
2036 self.assertNotEqual(commit_A, commit_B, commit_C)
James E. Blair7f71c802012-08-22 13:04:32 -07002037
2038 def test_one_job_project(self):
2039 "Test that queueing works with one job"
2040 A = self.fake_gerrit.addFakeChange('org/one-job-project',
2041 'master', 'A')
2042 B = self.fake_gerrit.addFakeChange('org/one-job-project',
2043 'master', 'B')
2044 A.addApproval('CRVW', 2)
2045 B.addApproval('CRVW', 2)
2046 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2047 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2048 self.waitUntilSettled()
2049
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002050 self.assertEqual(A.data['status'], 'MERGED')
2051 self.assertEqual(A.reported, 2)
2052 self.assertEqual(B.data['status'], 'MERGED')
2053 self.assertEqual(B.reported, 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07002054
Antoine Musso80edd5a2013-02-13 15:37:53 +01002055 def test_job_from_templates_launched(self):
2056 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002057
Antoine Musso80edd5a2013-02-13 15:37:53 +01002058 A = self.fake_gerrit.addFakeChange(
2059 'org/templated-project', 'master', 'A')
2060 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2061 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01002062
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002063 self.assertEqual(self.getJobFromHistory('project-test1').result,
2064 'SUCCESS')
2065 self.assertEqual(self.getJobFromHistory('project-test2').result,
2066 'SUCCESS')
Antoine Musso80edd5a2013-02-13 15:37:53 +01002067
James E. Blair3e98c022013-12-16 15:25:38 -08002068 def test_layered_templates(self):
2069 "Test whether a job generated via a template can be launched"
2070
2071 A = self.fake_gerrit.addFakeChange(
2072 'org/layered-project', 'master', 'A')
2073 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2074 self.waitUntilSettled()
2075
2076 self.assertEqual(self.getJobFromHistory('project-test1').result,
2077 'SUCCESS')
2078 self.assertEqual(self.getJobFromHistory('project-test2').result,
2079 'SUCCESS')
James E. Blairaea6cf62013-12-16 15:38:12 -08002080 self.assertEqual(self.getJobFromHistory('layered-project-test3'
2081 ).result, 'SUCCESS')
2082 self.assertEqual(self.getJobFromHistory('layered-project-test4'
2083 ).result, 'SUCCESS')
2084 # test5 should run twice because two templates define it
James E. Blair3e98c022013-12-16 15:25:38 -08002085 test5_count = 0
2086 for job in self.worker.build_history:
James E. Blairaea6cf62013-12-16 15:38:12 -08002087 if job.name == 'layered-project-foo-test5':
James E. Blair3e98c022013-12-16 15:25:38 -08002088 test5_count += 1
2089 self.assertEqual(job.result, 'SUCCESS')
2090 self.assertEqual(test5_count, 2)
2091 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
James E. Blair1843a552013-07-03 14:19:52 -07002997 def test_json_status(self):
2998 "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
3007 f = urllib.urlopen("http://localhost:%s/status.json" % port)
3008 data = f.read()
3009
3010 self.worker.hold_jobs_in_build = False
3011 self.worker.release()
3012 self.waitUntilSettled()
3013
3014 data = json.loads(data)
3015 status_jobs = set()
3016 for p in data['pipelines']:
3017 for q in p['change_queues']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08003018 if q['dependent']:
3019 self.assertEqual(q['window'], 20)
3020 else:
3021 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07003022 for head in q['heads']:
3023 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08003024 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003025 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07003026 for job in change['jobs']:
3027 status_jobs.add(job['name'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003028 self.assertIn('project-merge', status_jobs)
3029 self.assertIn('project-test1', status_jobs)
3030 self.assertIn('project-test2', status_jobs)
James E. Blair1843a552013-07-03 14:19:52 -07003031
James E. Blairc3d428e2013-12-03 15:06:48 -08003032 def test_merging_queues(self):
3033 "Test that transitively-connected change queues are merged"
3034 self.config.set('zuul', 'layout_config',
3035 'tests/fixtures/layout-merge-queues.yaml')
3036 self.sched.reconfigure(self.config)
3037 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
3038
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003039 def test_node_label(self):
3040 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07003041 self.worker.registerFunction('build:node-project-test1:debian')
3042
3043 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
3044 A.addApproval('CRVW', 2)
3045 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3046 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07003047
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003048 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3049 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3050 'debian')
3051 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
James E. Blaircdccd972013-07-01 12:10:22 -07003052
3053 def test_live_reconfiguration(self):
3054 "Test that live reconfiguration works"
3055 self.worker.hold_jobs_in_build = True
3056 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3057 A.addApproval('CRVW', 2)
3058 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3059 self.waitUntilSettled()
3060
3061 self.sched.reconfigure(self.config)
3062
3063 self.worker.hold_jobs_in_build = False
3064 self.worker.release()
3065 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04003066 self.assertEqual(self.getJobFromHistory('project-merge').result,
3067 'SUCCESS')
3068 self.assertEqual(self.getJobFromHistory('project-test1').result,
3069 'SUCCESS')
3070 self.assertEqual(self.getJobFromHistory('project-test2').result,
3071 'SUCCESS')
3072 self.assertEqual(A.data['status'], 'MERGED')
3073 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07003074
James E. Blaire712d9f2013-07-31 11:40:11 -07003075 def test_live_reconfiguration_functions(self):
3076 "Test live reconfiguration with a custom function"
3077 self.worker.registerFunction('build:node-project-test1:debian')
3078 self.worker.registerFunction('build:node-project-test1:wheezy')
3079 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
3080 A.addApproval('CRVW', 2)
3081 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3082 self.waitUntilSettled()
3083
3084 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3085 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3086 'debian')
3087 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
3088
3089 self.config.set('zuul', 'layout_config',
3090 'tests/fixtures/layout-live-'
3091 'reconfiguration-functions.yaml')
3092 self.sched.reconfigure(self.config)
3093 self.worker.build_history = []
3094
3095 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
3096 B.addApproval('CRVW', 2)
3097 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3098 self.waitUntilSettled()
3099
3100 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
3101 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
3102 'wheezy')
3103 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
3104
James E. Blair287c06d2013-07-24 10:39:30 -07003105 def test_delayed_repo_init(self):
3106 self.config.set('zuul', 'layout_config',
3107 'tests/fixtures/layout-delayed-repo-init.yaml')
3108 self.sched.reconfigure(self.config)
3109
3110 self.init_repo("org/new-project")
3111 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
3112
3113 A.addApproval('CRVW', 2)
3114 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3115 self.waitUntilSettled()
3116 self.assertEqual(self.getJobFromHistory('project-merge').result,
3117 'SUCCESS')
3118 self.assertEqual(self.getJobFromHistory('project-test1').result,
3119 'SUCCESS')
3120 self.assertEqual(self.getJobFromHistory('project-test2').result,
3121 'SUCCESS')
3122 self.assertEqual(A.data['status'], 'MERGED')
3123 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003124
Clark Boylan6dbbc482013-10-18 10:57:31 -07003125 def test_repo_deleted(self):
3126 self.config.set('zuul', 'layout_config',
3127 'tests/fixtures/layout-repo-deleted.yaml')
3128 self.sched.reconfigure(self.config)
3129
3130 self.init_repo("org/delete-project")
3131 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
3132
3133 A.addApproval('CRVW', 2)
3134 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3135 self.waitUntilSettled()
3136 self.assertEqual(self.getJobFromHistory('project-merge').result,
3137 'SUCCESS')
3138 self.assertEqual(self.getJobFromHistory('project-test1').result,
3139 'SUCCESS')
3140 self.assertEqual(self.getJobFromHistory('project-test2').result,
3141 'SUCCESS')
3142 self.assertEqual(A.data['status'], 'MERGED')
3143 self.assertEqual(A.reported, 2)
3144
3145 # Delete org/new-project zuul repo. Should be recloned.
3146 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
3147
3148 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
3149
3150 B.addApproval('CRVW', 2)
3151 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3152 self.waitUntilSettled()
3153 self.assertEqual(self.getJobFromHistory('project-merge').result,
3154 'SUCCESS')
3155 self.assertEqual(self.getJobFromHistory('project-test1').result,
3156 'SUCCESS')
3157 self.assertEqual(self.getJobFromHistory('project-test2').result,
3158 'SUCCESS')
3159 self.assertEqual(B.data['status'], 'MERGED')
3160 self.assertEqual(B.reported, 2)
3161
James E. Blair63bb0ef2013-07-29 17:14:51 -07003162 def test_timer(self):
3163 "Test that a periodic job is triggered"
3164 self.worker.hold_jobs_in_build = True
3165 self.config.set('zuul', 'layout_config',
3166 'tests/fixtures/layout-timer.yaml')
3167 self.sched.reconfigure(self.config)
3168 self.registerJobs()
3169
3170 start = time.time()
3171 failed = True
3172 while ((time.time() - start) < 30):
3173 if len(self.builds) == 2:
3174 failed = False
3175 break
3176 else:
3177 time.sleep(1)
3178
3179 if failed:
3180 raise Exception("Expected jobs never ran")
3181
3182 self.waitUntilSettled()
3183 port = self.webapp.server.socket.getsockname()[1]
3184
3185 f = urllib.urlopen("http://localhost:%s/status.json" % port)
3186 data = f.read()
3187
3188 self.worker.hold_jobs_in_build = False
3189 self.worker.release()
3190 self.waitUntilSettled()
3191
3192 self.assertEqual(self.getJobFromHistory(
3193 'project-bitrot-stable-old').result, 'SUCCESS')
3194 self.assertEqual(self.getJobFromHistory(
3195 'project-bitrot-stable-older').result, 'SUCCESS')
3196
3197 data = json.loads(data)
3198 status_jobs = set()
3199 for p in data['pipelines']:
3200 for q in p['change_queues']:
3201 for head in q['heads']:
3202 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07003203 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003204 for job in change['jobs']:
3205 status_jobs.add(job['name'])
3206 self.assertIn('project-bitrot-stable-old', status_jobs)
3207 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003208
3209 def test_check_smtp_pool(self):
3210 self.config.set('zuul', 'layout_config',
3211 'tests/fixtures/layout-smtp.yaml')
3212 self.sched.reconfigure(self.config)
3213
3214 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3215 self.waitUntilSettled()
3216
3217 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3218 self.waitUntilSettled()
3219
James E. Blairff80a2f2013-12-27 13:24:06 -08003220 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003221
3222 # A.messages only holds what FakeGerrit places in it. Thus we
3223 # work on the knowledge of what the first message should be as
3224 # it is only configured to go to SMTP.
3225
3226 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003227 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003228 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003229 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003230 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003231 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003232
3233 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003234 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003235 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003236 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003237 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003238 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003239
James E. Blaire5910202013-12-27 09:50:31 -08003240 def test_timer_smtp(self):
3241 "Test that a periodic job is triggered"
3242 self.config.set('zuul', 'layout_config',
3243 'tests/fixtures/layout-timer-smtp.yaml')
3244 self.sched.reconfigure(self.config)
3245 self.registerJobs()
3246
3247 start = time.time()
3248 failed = True
3249 while ((time.time() - start) < 30):
3250 if len(self.history) == 2:
3251 failed = False
3252 break
3253 else:
3254 time.sleep(1)
3255
3256 if failed:
3257 raise Exception("Expected jobs never ran")
3258
3259 self.waitUntilSettled()
3260
3261 self.assertEqual(self.getJobFromHistory(
3262 'project-bitrot-stable-old').result, 'SUCCESS')
3263 self.assertEqual(self.getJobFromHistory(
3264 'project-bitrot-stable-older').result, 'SUCCESS')
3265
James E. Blairff80a2f2013-12-27 13:24:06 -08003266 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003267
3268 # A.messages only holds what FakeGerrit places in it. Thus we
3269 # work on the knowledge of what the first message should be as
3270 # it is only configured to go to SMTP.
3271
3272 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003273 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003274 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003275 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003276 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003277 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003278
James E. Blairad28e912013-11-27 10:43:22 -08003279 def test_client_enqueue(self):
3280 "Test that the RPC client can enqueue a change"
3281 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3282 A.addApproval('CRVW', 2)
3283 A.addApproval('APRV', 1)
3284
3285 client = zuul.rpcclient.RPCClient('127.0.0.1',
3286 self.gearman_server.port)
3287 r = client.enqueue(pipeline='gate',
3288 project='org/project',
3289 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003290 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003291 self.waitUntilSettled()
3292 self.assertEqual(self.getJobFromHistory('project-merge').result,
3293 'SUCCESS')
3294 self.assertEqual(self.getJobFromHistory('project-test1').result,
3295 'SUCCESS')
3296 self.assertEqual(self.getJobFromHistory('project-test2').result,
3297 'SUCCESS')
3298 self.assertEqual(A.data['status'], 'MERGED')
3299 self.assertEqual(A.reported, 2)
3300 self.assertEqual(r, True)
3301
3302 def test_client_enqueue_negative(self):
3303 "Test that the RPC client returns errors"
3304 client = zuul.rpcclient.RPCClient('127.0.0.1',
3305 self.gearman_server.port)
3306 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3307 "Invalid project"):
3308 r = client.enqueue(pipeline='gate',
3309 project='project-does-not-exist',
3310 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003311 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003312 client.shutdown()
3313 self.assertEqual(r, False)
3314
3315 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3316 "Invalid pipeline"):
3317 r = client.enqueue(pipeline='pipeline-does-not-exist',
3318 project='org/project',
3319 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003320 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003321 client.shutdown()
3322 self.assertEqual(r, False)
3323
3324 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3325 "Invalid trigger"):
3326 r = client.enqueue(pipeline='gate',
3327 project='org/project',
3328 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003329 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003330 client.shutdown()
3331 self.assertEqual(r, False)
3332
3333 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3334 "Invalid change"):
3335 r = client.enqueue(pipeline='gate',
3336 project='org/project',
3337 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003338 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003339 client.shutdown()
3340 self.assertEqual(r, False)
3341
3342 self.waitUntilSettled()
3343 self.assertEqual(len(self.history), 0)
3344 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003345
3346 def test_client_promote(self):
3347 "Test that the RPC client can promote a change"
3348 self.worker.hold_jobs_in_build = True
3349 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3350 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3351 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3352 A.addApproval('CRVW', 2)
3353 B.addApproval('CRVW', 2)
3354 C.addApproval('CRVW', 2)
3355
3356 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3357 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3358 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3359
3360 self.waitUntilSettled()
3361
Sean Daguef39b9ca2014-01-10 21:34:35 -05003362 items = self.sched.layout.pipelines['gate'].getAllItems()
3363 enqueue_times = {}
3364 for item in items:
3365 enqueue_times[str(item.change)] = item.enqueue_time
3366
James E. Blair36658cf2013-12-06 17:53:48 -08003367 client = zuul.rpcclient.RPCClient('127.0.0.1',
3368 self.gearman_server.port)
3369 r = client.promote(pipeline='gate',
3370 change_ids=['2,1', '3,1'])
3371
Sean Daguef39b9ca2014-01-10 21:34:35 -05003372 # ensure that enqueue times are durable
3373 items = self.sched.layout.pipelines['gate'].getAllItems()
3374 for item in items:
3375 self.assertEqual(
3376 enqueue_times[str(item.change)], item.enqueue_time)
3377
James E. Blair78acec92014-02-06 07:11:32 -08003378 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003379 self.worker.release('.*-merge')
3380 self.waitUntilSettled()
3381 self.worker.release('.*-merge')
3382 self.waitUntilSettled()
3383 self.worker.release('.*-merge')
3384 self.waitUntilSettled()
3385
3386 self.assertEqual(len(self.builds), 6)
3387 self.assertEqual(self.builds[0].name, 'project-test1')
3388 self.assertEqual(self.builds[1].name, 'project-test2')
3389 self.assertEqual(self.builds[2].name, 'project-test1')
3390 self.assertEqual(self.builds[3].name, 'project-test2')
3391 self.assertEqual(self.builds[4].name, 'project-test1')
3392 self.assertEqual(self.builds[5].name, 'project-test2')
3393
3394 self.assertTrue(self.job_has_changes(self.builds[0], B))
3395 self.assertFalse(self.job_has_changes(self.builds[0], A))
3396 self.assertFalse(self.job_has_changes(self.builds[0], C))
3397
3398 self.assertTrue(self.job_has_changes(self.builds[2], B))
3399 self.assertTrue(self.job_has_changes(self.builds[2], C))
3400 self.assertFalse(self.job_has_changes(self.builds[2], A))
3401
3402 self.assertTrue(self.job_has_changes(self.builds[4], B))
3403 self.assertTrue(self.job_has_changes(self.builds[4], C))
3404 self.assertTrue(self.job_has_changes(self.builds[4], A))
3405
3406 self.worker.release()
3407 self.waitUntilSettled()
3408
3409 self.assertEqual(A.data['status'], 'MERGED')
3410 self.assertEqual(A.reported, 2)
3411 self.assertEqual(B.data['status'], 'MERGED')
3412 self.assertEqual(B.reported, 2)
3413 self.assertEqual(C.data['status'], 'MERGED')
3414 self.assertEqual(C.reported, 2)
3415
3416 client.shutdown()
3417 self.assertEqual(r, True)
3418
3419 def test_client_promote_dependent(self):
3420 "Test that the RPC client can promote a dependent change"
3421 # C (depends on B) -> B -> A ; then promote C to get:
3422 # A -> C (depends on B) -> B
3423 self.worker.hold_jobs_in_build = True
3424 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3425 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3426 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3427
3428 C.setDependsOn(B, 1)
3429
3430 A.addApproval('CRVW', 2)
3431 B.addApproval('CRVW', 2)
3432 C.addApproval('CRVW', 2)
3433
3434 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3435 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3436 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3437
3438 self.waitUntilSettled()
3439
3440 client = zuul.rpcclient.RPCClient('127.0.0.1',
3441 self.gearman_server.port)
3442 r = client.promote(pipeline='gate',
3443 change_ids=['3,1'])
3444
James E. Blair78acec92014-02-06 07:11:32 -08003445 self.waitUntilSettled()
James E. Blair36658cf2013-12-06 17:53:48 -08003446 self.worker.release('.*-merge')
3447 self.waitUntilSettled()
3448 self.worker.release('.*-merge')
3449 self.waitUntilSettled()
3450 self.worker.release('.*-merge')
3451 self.waitUntilSettled()
3452
3453 self.assertEqual(len(self.builds), 6)
3454 self.assertEqual(self.builds[0].name, 'project-test1')
3455 self.assertEqual(self.builds[1].name, 'project-test2')
3456 self.assertEqual(self.builds[2].name, 'project-test1')
3457 self.assertEqual(self.builds[3].name, 'project-test2')
3458 self.assertEqual(self.builds[4].name, 'project-test1')
3459 self.assertEqual(self.builds[5].name, 'project-test2')
3460
3461 self.assertTrue(self.job_has_changes(self.builds[0], B))
3462 self.assertFalse(self.job_has_changes(self.builds[0], A))
3463 self.assertFalse(self.job_has_changes(self.builds[0], C))
3464
3465 self.assertTrue(self.job_has_changes(self.builds[2], B))
3466 self.assertTrue(self.job_has_changes(self.builds[2], C))
3467 self.assertFalse(self.job_has_changes(self.builds[2], A))
3468
3469 self.assertTrue(self.job_has_changes(self.builds[4], B))
3470 self.assertTrue(self.job_has_changes(self.builds[4], C))
3471 self.assertTrue(self.job_has_changes(self.builds[4], A))
3472
3473 self.worker.release()
3474 self.waitUntilSettled()
3475
3476 self.assertEqual(A.data['status'], 'MERGED')
3477 self.assertEqual(A.reported, 2)
3478 self.assertEqual(B.data['status'], 'MERGED')
3479 self.assertEqual(B.reported, 2)
3480 self.assertEqual(C.data['status'], 'MERGED')
3481 self.assertEqual(C.reported, 2)
3482
3483 client.shutdown()
3484 self.assertEqual(r, True)
3485
3486 def test_client_promote_negative(self):
3487 "Test that the RPC client returns errors for promotion"
3488 self.worker.hold_jobs_in_build = True
3489 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3490 A.addApproval('CRVW', 2)
3491 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3492 self.waitUntilSettled()
3493
3494 client = zuul.rpcclient.RPCClient('127.0.0.1',
3495 self.gearman_server.port)
3496
3497 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3498 r = client.promote(pipeline='nonexistent',
3499 change_ids=['2,1', '3,1'])
3500 client.shutdown()
3501 self.assertEqual(r, False)
3502
3503 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
3504 r = client.promote(pipeline='gate',
3505 change_ids=['4,1'])
3506 client.shutdown()
3507 self.assertEqual(r, False)
3508
3509 self.worker.hold_jobs_in_build = False
3510 self.worker.release()
3511 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003512
3513 def test_queue_rate_limiting(self):
3514 "Test that DependentPipelines are rate limited with dep across window"
3515 self.config.set('zuul', 'layout_config',
3516 'tests/fixtures/layout-rate-limit.yaml')
3517 self.sched.reconfigure(self.config)
3518 self.worker.hold_jobs_in_build = True
3519 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3520 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3521 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3522
3523 C.setDependsOn(B, 1)
3524 self.worker.addFailTest('project-test1', A)
3525
3526 A.addApproval('CRVW', 2)
3527 B.addApproval('CRVW', 2)
3528 C.addApproval('CRVW', 2)
3529
3530 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3531 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3532 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3533 self.waitUntilSettled()
3534
3535 # Only A and B will have their merge jobs queued because
3536 # window is 2.
3537 self.assertEqual(len(self.builds), 2)
3538 self.assertEqual(self.builds[0].name, 'project-merge')
3539 self.assertEqual(self.builds[1].name, 'project-merge')
3540
3541 self.worker.release('.*-merge')
3542 self.waitUntilSettled()
3543 self.worker.release('.*-merge')
3544 self.waitUntilSettled()
3545
3546 # Only A and B will have their test jobs queued because
3547 # window is 2.
3548 self.assertEqual(len(self.builds), 4)
3549 self.assertEqual(self.builds[0].name, 'project-test1')
3550 self.assertEqual(self.builds[1].name, 'project-test2')
3551 self.assertEqual(self.builds[2].name, 'project-test1')
3552 self.assertEqual(self.builds[3].name, 'project-test2')
3553
3554 self.worker.release('project-.*')
3555 self.waitUntilSettled()
3556
3557 queue = self.sched.layout.pipelines['gate'].queues[0]
3558 # A failed so window is reduced by 1 to 1.
3559 self.assertEqual(queue.window, 1)
3560 self.assertEqual(queue.window_floor, 1)
3561 self.assertEqual(A.data['status'], 'NEW')
3562
3563 # Gate is reset and only B's merge job is queued because
3564 # window shrunk to 1.
3565 self.assertEqual(len(self.builds), 1)
3566 self.assertEqual(self.builds[0].name, 'project-merge')
3567
3568 self.worker.release('.*-merge')
3569 self.waitUntilSettled()
3570
3571 # Only B's test jobs are queued because window is still 1.
3572 self.assertEqual(len(self.builds), 2)
3573 self.assertEqual(self.builds[0].name, 'project-test1')
3574 self.assertEqual(self.builds[1].name, 'project-test2')
3575
3576 self.worker.release('project-.*')
3577 self.waitUntilSettled()
3578
3579 # B was successfully merged so window is increased to 2.
3580 self.assertEqual(queue.window, 2)
3581 self.assertEqual(queue.window_floor, 1)
3582 self.assertEqual(B.data['status'], 'MERGED')
3583
3584 # Only C is left and its merge job is queued.
3585 self.assertEqual(len(self.builds), 1)
3586 self.assertEqual(self.builds[0].name, 'project-merge')
3587
3588 self.worker.release('.*-merge')
3589 self.waitUntilSettled()
3590
3591 # After successful merge job the test jobs for C are queued.
3592 self.assertEqual(len(self.builds), 2)
3593 self.assertEqual(self.builds[0].name, 'project-test1')
3594 self.assertEqual(self.builds[1].name, 'project-test2')
3595
3596 self.worker.release('project-.*')
3597 self.waitUntilSettled()
3598
3599 # C successfully merged so window is bumped to 3.
3600 self.assertEqual(queue.window, 3)
3601 self.assertEqual(queue.window_floor, 1)
3602 self.assertEqual(C.data['status'], 'MERGED')
3603
3604 def test_queue_rate_limiting_dependent(self):
3605 "Test that DependentPipelines are rate limited with dep in window"
3606 self.config.set('zuul', 'layout_config',
3607 'tests/fixtures/layout-rate-limit.yaml')
3608 self.sched.reconfigure(self.config)
3609 self.worker.hold_jobs_in_build = True
3610 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3611 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3612 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3613
3614 B.setDependsOn(A, 1)
3615
3616 self.worker.addFailTest('project-test1', A)
3617
3618 A.addApproval('CRVW', 2)
3619 B.addApproval('CRVW', 2)
3620 C.addApproval('CRVW', 2)
3621
3622 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3623 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3624 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3625 self.waitUntilSettled()
3626
3627 # Only A and B will have their merge jobs queued because
3628 # window is 2.
3629 self.assertEqual(len(self.builds), 2)
3630 self.assertEqual(self.builds[0].name, 'project-merge')
3631 self.assertEqual(self.builds[1].name, 'project-merge')
3632
3633 self.worker.release('.*-merge')
3634 self.waitUntilSettled()
3635 self.worker.release('.*-merge')
3636 self.waitUntilSettled()
3637
3638 # Only A and B will have their test jobs queued because
3639 # window is 2.
3640 self.assertEqual(len(self.builds), 4)
3641 self.assertEqual(self.builds[0].name, 'project-test1')
3642 self.assertEqual(self.builds[1].name, 'project-test2')
3643 self.assertEqual(self.builds[2].name, 'project-test1')
3644 self.assertEqual(self.builds[3].name, 'project-test2')
3645
3646 self.worker.release('project-.*')
3647 self.waitUntilSettled()
3648
3649 queue = self.sched.layout.pipelines['gate'].queues[0]
3650 # A failed so window is reduced by 1 to 1.
3651 self.assertEqual(queue.window, 1)
3652 self.assertEqual(queue.window_floor, 1)
3653 self.assertEqual(A.data['status'], 'NEW')
3654 self.assertEqual(B.data['status'], 'NEW')
3655
3656 # Gate is reset and only C's merge job is queued because
3657 # window shrunk to 1 and A and B were dequeued.
3658 self.assertEqual(len(self.builds), 1)
3659 self.assertEqual(self.builds[0].name, 'project-merge')
3660
3661 self.worker.release('.*-merge')
3662 self.waitUntilSettled()
3663
3664 # Only C's test jobs are queued because window is still 1.
3665 self.assertEqual(len(self.builds), 2)
3666 self.assertEqual(self.builds[0].name, 'project-test1')
3667 self.assertEqual(self.builds[1].name, 'project-test2')
3668
3669 self.worker.release('project-.*')
3670 self.waitUntilSettled()
3671
3672 # C was successfully merged so window is increased to 2.
3673 self.assertEqual(queue.window, 2)
3674 self.assertEqual(queue.window_floor, 1)
3675 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003676
3677 def test_worker_update_metadata(self):
3678 "Test if a worker can send back metadata about itself"
3679 self.worker.hold_jobs_in_build = True
3680
3681 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3682 A.addApproval('CRVW', 2)
3683 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3684 self.waitUntilSettled()
3685
3686 self.assertEqual(len(self.launcher.builds), 1)
3687
3688 self.log.debug('Current builds:')
3689 self.log.debug(self.launcher.builds)
3690
3691 start = time.time()
3692 while True:
3693 if time.time() - start > 10:
3694 raise Exception("Timeout waiting for gearman server to report "
3695 + "back to the client")
3696 build = self.launcher.builds.values()[0]
3697 if build.worker.name == "My Worker":
3698 break
3699 else:
3700 time.sleep(0)
3701
3702 self.log.debug(build)
3703 self.assertEqual("My Worker", build.worker.name)
3704 self.assertEqual("localhost", build.worker.hostname)
3705 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3706 self.assertEqual("zuul.example.org", build.worker.fqdn)
3707 self.assertEqual("FakeBuilder", build.worker.program)
3708 self.assertEqual("v1.1", build.worker.version)
3709 self.assertEqual({'something': 'else'}, build.worker.extra)
3710
3711 self.worker.hold_jobs_in_build = False
3712 self.worker.release()
3713 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003714
3715 def test_footer_message(self):
3716 "Test a pipeline's footer message is correctly added to the report."
3717 self.config.set('zuul', 'layout_config',
3718 'tests/fixtures/layout-footer-message.yaml')
3719 self.sched.reconfigure(self.config)
3720 self.registerJobs()
3721
3722 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3723 A.addApproval('CRVW', 2)
3724 self.worker.addFailTest('test1', A)
3725 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3726 self.waitUntilSettled()
3727
3728 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3729 B.addApproval('CRVW', 2)
3730 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3731 self.waitUntilSettled()
3732
3733 self.assertEqual(2, len(self.smtp_messages))
3734
3735 failure_body = """\
3736Build failed. For information on how to proceed, see \
3737http://wiki.example.org/Test_Failures
3738
3739- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3740- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3741
3742For CI problems and help debugging, contact ci@example.org"""
3743
3744 success_body = """\
3745Build succeeded.
3746
3747- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3748- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3749
3750For CI problems and help debugging, contact ci@example.org"""
3751
3752 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3753 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11003754
3755 def test_merge_failure_reporters(self):
3756 """Check that the config is set up correctly"""
3757
3758 self.config.set('zuul', 'layout_config',
3759 'tests/fixtures/layout-merge-failure.yaml')
3760 self.sched.reconfigure(self.config)
3761 self.registerJobs()
3762
3763 self.assertEqual(
3764 "Merge Failed.\n\nThis change was unable to be automatically "
3765 "merged with the current state of the repository. Please rebase "
3766 "your change and upload a new patchset.",
3767 self.sched.layout.pipelines['check'].merge_failure_message)
3768 self.assertEqual(
3769 "The merge failed! For more information...",
3770 self.sched.layout.pipelines['gate'].merge_failure_message)
3771
3772 self.assertEqual(
3773 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3774 self.assertEqual(
3775 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3776
3777 self.assertTrue(isinstance(
3778 self.sched.layout.pipelines['check'].merge_failure_actions[0].
3779 reporter, zuul.reporter.gerrit.Reporter))
3780
3781 self.assertTrue(
3782 (
3783 isinstance(self.sched.layout.pipelines['gate'].
3784 merge_failure_actions[0].reporter,
3785 zuul.reporter.smtp.Reporter) and
3786 isinstance(self.sched.layout.pipelines['gate'].
3787 merge_failure_actions[1].reporter,
3788 zuul.reporter.gerrit.Reporter)
3789 ) or (
3790 isinstance(self.sched.layout.pipelines['gate'].
3791 merge_failure_actions[0].reporter,
3792 zuul.reporter.gerrit.Reporter) and
3793 isinstance(self.sched.layout.pipelines['gate'].
3794 merge_failure_actions[1].reporter,
3795 zuul.reporter.smtp.Reporter)
3796 )
3797 )
3798
3799 def test_merge_failure_reports(self):
3800 """Check that when a change fails to merge the correct message is sent
3801 to the correct reporter"""
3802 self.config.set('zuul', 'layout_config',
3803 'tests/fixtures/layout-merge-failure.yaml')
3804 self.sched.reconfigure(self.config)
3805 self.registerJobs()
3806
3807 # Check a test failure isn't reported to SMTP
3808 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3809 A.addApproval('CRVW', 2)
3810 self.worker.addFailTest('project-test1', A)
3811 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3812 self.waitUntilSettled()
3813
3814 self.assertEqual(3, len(self.history)) # 3 jobs
3815 self.assertEqual(0, len(self.smtp_messages))
3816
3817 # Check a merge failure is reported to SMTP
3818 # B should be merged, but C will conflict with B
3819 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3820 B.addPatchset(['conflict'])
3821 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3822 C.addPatchset(['conflict'])
3823 B.addApproval('CRVW', 2)
3824 C.addApproval('CRVW', 2)
3825 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
3826 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
3827 self.waitUntilSettled()
3828
3829 self.assertEqual(6, len(self.history)) # A and B jobs
3830 self.assertEqual(1, len(self.smtp_messages))
3831 self.assertEqual('The merge failed! For more information...',
3832 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003833
3834 def test_swift_instructions(self):
3835 "Test that the correct swift instructions are sent to the workers"
3836 self.config.set('zuul', 'layout_config',
3837 'tests/fixtures/layout-swift.yaml')
3838 self.sched.reconfigure(self.config)
3839 self.registerJobs()
3840
3841 self.worker.hold_jobs_in_build = True
3842 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3843
3844 A.addApproval('CRVW', 2)
3845 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
3846 self.waitUntilSettled()
3847
3848 self.assertEqual(
3849 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
3850 "gate/test-merge/",
3851 self.builds[0].parameters['SWIFT_logs_URL'][:-32])
3852 self.assertEqual(5,
3853 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
3854 split('\n')))
3855 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
3856
3857 self.assertEqual(
3858 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
3859 "gate/test-test/",
3860 self.builds[1].parameters['SWIFT_logs_URL'][:-32])
3861 self.assertEqual(5,
3862 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
3863 split('\n')))
3864 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
3865
3866 self.assertEqual(
3867 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
3868 "gate/test-test/",
3869 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-32])
3870 self.assertEqual(5,
3871 len(self.builds[1].
3872 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
3873 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
3874
3875 self.worker.hold_jobs_in_build = False
3876 self.worker.release()
3877 self.waitUntilSettled()