blob: a880b74937d7c4afebde82be008b7725eb42c35a [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
17import unittest
18import ConfigParser
19import os
20import Queue
James E. Blair8cc15a82012-08-01 11:17:57 -070021import hashlib
James E. Blairb0fcae42012-07-17 11:12:10 -070022import logging
James E. Blair8cc15a82012-08-01 11:17:57 -070023import random
James E. Blairb0fcae42012-07-17 11:12:10 -070024import json
25import threading
26import time
27import pprint
28import re
James E. Blair8cc15a82012-08-01 11:17:57 -070029import urllib2
30import urlparse
James E. Blair412e5582013-04-22 15:50:12 -070031import select
32import statsd
James E. Blair4886cc12012-07-18 15:39:41 -070033import shutil
James E. Blair412e5582013-04-22 15:50:12 -070034import socket
James E. Blair4886f282012-11-15 09:27:33 -080035import string
James E. Blair1f4c2bb2013-04-26 08:40:46 -070036from cStringIO import StringIO
James E. Blair4886cc12012-07-18 15:39:41 -070037import git
James E. Blair1f4c2bb2013-04-26 08:40:46 -070038import gear
James E. Blairb0fcae42012-07-17 11:12:10 -070039
James E. Blairb0fcae42012-07-17 11:12:10 -070040import zuul.scheduler
James E. Blair1f4c2bb2013-04-26 08:40:46 -070041import zuul.launcher.gearman
James E. Blairb0fcae42012-07-17 11:12:10 -070042import zuul.trigger.gerrit
43
44FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
45 'fixtures')
46CONFIG = ConfigParser.ConfigParser()
47CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
48
49CONFIG.set('zuul', 'layout_config',
50 os.path.join(FIXTURE_DIR, "layout.yaml"))
51
James E. Blair1dbd5082012-08-23 15:12:15 -070052TMP_ROOT = os.environ.get("ZUUL_TEST_ROOT", "/tmp")
53TEST_ROOT = os.path.join(TMP_ROOT, "zuul-test")
54UPSTREAM_ROOT = os.path.join(TEST_ROOT, "upstream")
55GIT_ROOT = os.path.join(TEST_ROOT, "git")
56
57CONFIG.set('zuul', 'git_dir', GIT_ROOT)
58
James E. Blair1f4c2bb2013-04-26 08:40:46 -070059logging.basicConfig(level=logging.DEBUG,
60 format='%(asctime)s %(name)-32s '
61 '%(levelname)-8s %(message)s')
James E. Blairb0fcae42012-07-17 11:12:10 -070062
63
James E. Blair8cc15a82012-08-01 11:17:57 -070064def random_sha1():
65 return hashlib.sha1(str(random.random())).hexdigest()
66
67
James E. Blair4886cc12012-07-18 15:39:41 -070068class ChangeReference(git.Reference):
69 _common_path_default = "refs/changes"
70 _points_to_commits_only = True
71
72
73def init_repo(project):
74 parts = project.split('/')
James E. Blair1dbd5082012-08-23 15:12:15 -070075 path = os.path.join(UPSTREAM_ROOT, *parts[:-1])
James E. Blair4886cc12012-07-18 15:39:41 -070076 if not os.path.exists(path):
77 os.makedirs(path)
James E. Blair1dbd5082012-08-23 15:12:15 -070078 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -070079 repo = git.Repo.init(path)
80
Paul Belangerb67aba12013-05-13 19:22:14 -040081 repo.config_writer().set_value('user', 'email', 'user@example.com')
82 repo.config_writer().set_value('user', 'name', 'User Name')
83 repo.config_writer().write()
84
James E. Blair4886cc12012-07-18 15:39:41 -070085 fn = os.path.join(path, 'README')
86 f = open(fn, 'w')
87 f.write("test\n")
88 f.close()
89 repo.index.add([fn])
90 repo.index.commit('initial commit')
James E. Blairc6294a52012-08-17 10:19:48 -070091 master = repo.create_head('master')
James E. Blair4886cc12012-07-18 15:39:41 -070092 repo.create_tag('init')
93
James E. Blairc6294a52012-08-17 10:19:48 -070094 mp = repo.create_head('mp')
95 repo.head.reference = mp
96 f = open(fn, 'a')
97 f.write("test mp\n")
98 f.close()
99 repo.index.add([fn])
100 repo.index.commit('mp commit')
101
102 repo.head.reference = master
103 repo.head.reset(index=True, working_tree=True)
104 repo.git.clean('-x', '-f', '-d')
105
James E. Blair4886cc12012-07-18 15:39:41 -0700106
James E. Blair4886f282012-11-15 09:27:33 -0800107def add_fake_change_to_repo(project, branch, change_num, patchset, msg, fn,
108 large):
James E. Blair1dbd5082012-08-23 15:12:15 -0700109 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -0700110 repo = git.Repo(path)
111 ref = ChangeReference.create(repo, '1/%s/%s' % (change_num,
112 patchset),
113 'refs/tags/init')
114 repo.head.reference = ref
115 repo.head.reset(index=True, working_tree=True)
116 repo.git.clean('-x', '-f', '-d')
117
James E. Blair1dbd5082012-08-23 15:12:15 -0700118 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair4886f282012-11-15 09:27:33 -0800119 if not large:
120 fn = os.path.join(path, fn)
121 f = open(fn, 'w')
122 f.write("test %s %s %s\n" % (branch, change_num, patchset))
123 f.close()
124 repo.index.add([fn])
125 else:
126 for fni in range(100):
127 fn = os.path.join(path, str(fni))
128 f = open(fn, 'w')
129 for ci in range(4096):
130 f.write(random.choice(string.printable))
131 f.close()
132 repo.index.add([fn])
133
James E. Blairdaabed22012-08-15 15:38:57 -0700134 return repo.index.commit(msg)
James E. Blair4886cc12012-07-18 15:39:41 -0700135
136
137def ref_has_change(ref, change):
James E. Blair1dbd5082012-08-23 15:12:15 -0700138 path = os.path.join(GIT_ROOT, change.project)
James E. Blair4886cc12012-07-18 15:39:41 -0700139 repo = git.Repo(path)
140 for commit in repo.iter_commits(ref):
141 if commit.message.strip() == ('%s-1' % change.subject):
142 return True
143 return False
144
145
146def job_has_changes(*args):
147 job = args[0]
148 commits = args[1:]
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700149 if isinstance(job, FakeBuild):
150 parameters = job.parameters
151 else:
152 parameters = json.loads(job.arguments)
153 project = parameters['ZUUL_PROJECT']
James E. Blair1dbd5082012-08-23 15:12:15 -0700154 path = os.path.join(GIT_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -0700155 repo = git.Repo(path)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700156 ref = parameters['ZUUL_REF']
157 sha = parameters['ZUUL_COMMIT']
James E. Blair4886cc12012-07-18 15:39:41 -0700158 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
James E. Blair81515ad2012-10-01 18:29:08 -0700159 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
James E. Blair4886cc12012-07-18 15:39:41 -0700160 commit_messages = ['%s-1' % commit.subject for commit in commits]
James E. Blair4886cc12012-07-18 15:39:41 -0700161 for msg in commit_messages:
162 if msg not in repo_messages:
163 return False
James E. Blair81515ad2012-10-01 18:29:08 -0700164 if repo_shas[0] != sha:
165 return False
James E. Blair4886cc12012-07-18 15:39:41 -0700166 return True
167
168
James E. Blairb0fcae42012-07-17 11:12:10 -0700169class FakeChange(object):
James E. Blair8c803f82012-07-31 16:25:42 -0700170 categories = {'APRV': ('Approved', -1, 1),
171 'CRVW': ('Code-Review', -2, 2),
172 'VRFY': ('Verified', -2, 2)}
James E. Blairb0fcae42012-07-17 11:12:10 -0700173
James E. Blair8cc15a82012-08-01 11:17:57 -0700174 def __init__(self, gerrit, number, project, branch, subject, status='NEW'):
175 self.gerrit = gerrit
James E. Blaird466dc42012-07-31 10:42:56 -0700176 self.reported = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700177 self.queried = 0
James E. Blairb0fcae42012-07-17 11:12:10 -0700178 self.patchsets = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700179 self.number = number
180 self.project = project
181 self.branch = branch
182 self.subject = subject
183 self.latest_patchset = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700184 self.depends_on_change = None
185 self.needed_by_changes = []
James E. Blair127bc182012-08-28 15:55:15 -0700186 self.fail_merge = False
James E. Blair42f74822013-05-14 15:18:03 -0700187 self.messages = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700188 self.data = {
189 'branch': branch,
190 'comments': [],
191 'commitMessage': subject,
192 'createdOn': time.time(),
James E. Blair8cc15a82012-08-01 11:17:57 -0700193 'id': 'I' + random_sha1(),
James E. Blairb0fcae42012-07-17 11:12:10 -0700194 'lastUpdated': time.time(),
195 'number': str(number),
196 'open': True,
197 'owner': {'email': 'user@example.com',
198 'name': 'User Name',
199 'username': 'username'},
200 'patchSets': self.patchsets,
201 'project': project,
202 'status': status,
203 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700204 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700205 'url': 'https://hostname/%s' % number}
206
207 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700208 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700209
James E. Blair70c71582013-03-06 08:50:50 -0800210 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700211 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700212 if files:
213 fn = files[0]
214 else:
215 fn = '%s-%s' % (self.branch, self.number)
216 msg = self.subject + '-' + str(self.latest_patchset)
217 c = add_fake_change_to_repo(self.project, self.branch,
218 self.number, self.latest_patchset,
James E. Blair4886f282012-11-15 09:27:33 -0800219 msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800220 ps_files = [{'file': '/COMMIT_MSG',
221 'type': 'ADDED'},
222 {'file': 'README',
223 'type': 'MODIFIED'}]
224 for f in files:
225 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700226 d = {'approvals': [],
227 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800228 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700229 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700230 'ref': 'refs/changes/1/%s/%s' % (self.number,
231 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700232 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700233 'uploader': {'email': 'user@example.com',
234 'name': 'User name',
235 'username': 'user'}}
236 self.data['currentPatchSet'] = d
237 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700238 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700239
James E. Blaire0487072012-08-29 17:38:31 -0700240 def getPatchsetCreatedEvent(self, patchset):
241 event = {"type": "patchset-created",
242 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800243 "branch": self.branch,
244 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
245 "number": str(self.number),
246 "subject": self.subject,
247 "owner": {"name": "User Name"},
248 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700249 "patchSet": self.patchsets[patchset - 1],
250 "uploader": {"name": "User Name"}}
251 return event
252
James E. Blair42f74822013-05-14 15:18:03 -0700253 def getChangeRestoredEvent(self):
254 event = {"type": "change-restored",
255 "change": {"project": self.project,
256 "branch": self.branch,
257 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
258 "number": str(self.number),
259 "subject": self.subject,
260 "owner": {"name": "User Name"},
261 "url": "https://hostname/3"},
262 "restorer": {"name": "User Name"},
263 "reason": ""}
264 return event
265
James E. Blairb0fcae42012-07-17 11:12:10 -0700266 def addApproval(self, category, value):
James E. Blair8c803f82012-07-31 16:25:42 -0700267 approval = {'description': self.categories[category][0],
268 'type': category,
269 'value': str(value)}
270 self.patchsets[-1]['approvals'].append(approval)
271 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700272 'author': {'email': 'user@example.com',
273 'name': 'User Name',
274 'username': 'username'},
275 'change': {'branch': self.branch,
276 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
277 'number': str(self.number),
278 'owner': {'email': 'user@example.com',
279 'name': 'User Name',
280 'username': 'username'},
281 'project': self.project,
282 'subject': self.subject,
283 'topic': 'master',
284 'url': 'https://hostname/459'},
285 'comment': '',
286 'patchSet': self.patchsets[-1],
287 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700288 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700289 return json.loads(json.dumps(event))
290
James E. Blair8c803f82012-07-31 16:25:42 -0700291 def getSubmitRecords(self):
292 status = {}
293 for cat in self.categories.keys():
294 status[cat] = 0
295
296 for a in self.patchsets[-1]['approvals']:
297 cur = status[a['type']]
298 cat_min, cat_max = self.categories[a['type']][1:]
299 new = int(a['value'])
300 if new == cat_min:
301 cur = new
302 elif abs(new) > abs(cur):
303 cur = new
304 status[a['type']] = cur
305
306 labels = []
307 ok = True
308 for typ, cat in self.categories.items():
309 cur = status[typ]
310 cat_min, cat_max = cat[1:]
311 if cur == cat_min:
312 value = 'REJECT'
313 ok = False
314 elif cur == cat_max:
315 value = 'OK'
316 else:
317 value = 'NEED'
318 ok = False
319 labels.append({'label': cat[0], 'status': value})
320 if ok:
321 return [{'status': 'OK'}]
322 return [{'status': 'NOT_READY',
323 'labels': labels}]
324
325 def setDependsOn(self, other, patchset):
326 self.depends_on_change = other
327 d = {'id': other.data['id'],
328 'number': other.data['number'],
329 'ref': other.patchsets[patchset - 1]['ref']
330 }
331 self.data['dependsOn'] = [d]
332
333 other.needed_by_changes.append(self)
334 needed = other.data.get('neededBy', [])
335 d = {'id': self.data['id'],
336 'number': self.data['number'],
337 'ref': self.patchsets[patchset - 1]['ref'],
338 'revision': self.patchsets[patchset - 1]['revision']
339 }
340 needed.append(d)
341 other.data['neededBy'] = needed
342
James E. Blairb0fcae42012-07-17 11:12:10 -0700343 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700344 self.queried += 1
345 d = self.data.get('dependsOn')
346 if d:
347 d = d[0]
348 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
349 d['isCurrentPatchSet'] = True
350 else:
351 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700352 return json.loads(json.dumps(self.data))
353
354 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800355 if (self.depends_on_change and
356 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700357 return
James E. Blair127bc182012-08-28 15:55:15 -0700358 if self.fail_merge:
359 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700360 self.data['status'] = 'MERGED'
361 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700362
James E. Blair1dbd5082012-08-23 15:12:15 -0700363 path = os.path.join(UPSTREAM_ROOT, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700364 repo = git.Repo(path)
365 repo.heads[self.branch].commit = \
366 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700367
James E. Blaird466dc42012-07-31 10:42:56 -0700368 def setReported(self):
369 self.reported += 1
370
James E. Blairb0fcae42012-07-17 11:12:10 -0700371
372class FakeGerrit(object):
373 def __init__(self, *args, **kw):
374 self.event_queue = Queue.Queue()
375 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
376 self.change_number = 0
377 self.changes = {}
378
379 def addFakeChange(self, project, branch, subject):
380 self.change_number += 1
James E. Blair8cc15a82012-08-01 11:17:57 -0700381 c = FakeChange(self, self.change_number, project, branch, subject)
James E. Blairb0fcae42012-07-17 11:12:10 -0700382 self.changes[self.change_number] = c
383 return c
384
385 def addEvent(self, data):
386 return self.event_queue.put(data)
387
388 def getEvent(self):
389 return self.event_queue.get()
390
391 def eventDone(self):
392 self.event_queue.task_done()
393
394 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700395 number, ps = changeid.split(',')
396 change = self.changes[int(number)]
James E. Blair42f74822013-05-14 15:18:03 -0700397 change.messages.append(message)
James E. Blairb0fcae42012-07-17 11:12:10 -0700398 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700399 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700400 if message:
401 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700402
403 def query(self, number):
404 change = self.changes[int(number)]
405 return change.query()
406
407 def startWatching(self, *args, **kw):
408 pass
409
410
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700411class BuildHistory(object):
412 def __init__(self, **kw):
413 self.__dict__.update(kw)
James E. Blairb0fcae42012-07-17 11:12:10 -0700414
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700415 def __repr__(self):
416 return ("<Completed build, result: %s name: %s #%s changes: %s>" %
417 (self.result, self.name, self.number, self.changes))
James E. Blairb0fcae42012-07-17 11:12:10 -0700418
419
James E. Blair8cc15a82012-08-01 11:17:57 -0700420class FakeURLOpener(object):
421 def __init__(self, fake_gerrit, url):
422 self.fake_gerrit = fake_gerrit
423 self.url = url
424
425 def read(self):
426 res = urlparse.urlparse(self.url)
427 path = res.path
428 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200429 ret = '001e# service=git-upload-pack\n'
430 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
431 'multi_ack thin-pack side-band side-band-64k ofs-delta '
432 'shallow no-progress include-tag multi_ack_detailed no-done\n')
James E. Blair1dbd5082012-08-23 15:12:15 -0700433 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700434 repo = git.Repo(path)
435 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200436 r = ref.object.hexsha + ' ' + ref.path + '\n'
437 ret += '%04x%s' % (len(r) + 4, r)
438 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700439 return ret
440
441
James E. Blair4886cc12012-07-18 15:39:41 -0700442class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
443 def getGitUrl(self, project):
James E. Blair1dbd5082012-08-23 15:12:15 -0700444 return os.path.join(UPSTREAM_ROOT, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700445
446
James E. Blair412e5582013-04-22 15:50:12 -0700447class FakeStatsd(threading.Thread):
448 def __init__(self):
449 threading.Thread.__init__(self)
450 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
451 self.sock.bind(('', 0))
452 self.port = self.sock.getsockname()[1]
453 self.wake_read, self.wake_write = os.pipe()
454 self.stats = []
455
456 def run(self):
457 while True:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700458 poll = select.poll()
459 poll.register(self.sock, select.POLLIN)
460 poll.register(self.wake_read, select.POLLIN)
461 ret = poll.poll()
462 for (fd, event) in ret:
463 if fd == self.sock.fileno():
James E. Blair412e5582013-04-22 15:50:12 -0700464 data = self.sock.recvfrom(1024)
465 if not data:
466 return
467 self.stats.append(data[0])
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700468 if fd == self.wake_read:
James E. Blair412e5582013-04-22 15:50:12 -0700469 return
470
471 def stop(self):
472 os.write(self.wake_write, '1\n')
473
474
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700475class FakeBuild(threading.Thread):
476 log = logging.getLogger("zuul.test")
477
478 def __init__(self, worker, job, number, node):
479 threading.Thread.__init__(self)
480 self.worker = worker
481 self.job = job
482 self.name = job.name.split(':')[1]
483 self.number = number
484 self.node = node
485 self.parameters = json.loads(job.arguments)
486 self.unique = self.parameters['ZUUL_UUID']
487 self.wait_condition = threading.Condition()
488 self.waiting = False
489 self.aborted = False
490 self.created = time.time()
491 self.description = ''
492
493 def release(self):
494 self.wait_condition.acquire()
495 self.wait_condition.notify()
496 self.waiting = False
497 self.log.debug("Build %s released" % self.unique)
498 self.wait_condition.release()
499
500 def isWaiting(self):
501 self.wait_condition.acquire()
502 if self.waiting:
503 ret = True
504 else:
505 ret = False
506 self.wait_condition.release()
507 return ret
508
509 def _wait(self):
510 self.wait_condition.acquire()
511 self.waiting = True
512 self.log.debug("Build %s waiting" % self.unique)
513 self.wait_condition.wait()
514 self.wait_condition.release()
515
516 def run(self):
517 data = {
James E. Blair3c483cf2013-06-04 16:30:43 -0700518 'url': 'https://server/job/%s/%s/' % (self.name, self.number),
519 'name': self.name,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700520 'number': self.number,
James E. Blair3c483cf2013-06-04 16:30:43 -0700521 'manager': self.worker.worker_id,
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700522 }
523
524 self.job.sendWorkData(json.dumps(data))
525 self.job.sendWorkStatus(0, 100)
526
527 if self.worker.hold_jobs_in_build:
528 self._wait()
529 self.log.debug("Build %s continuing" % self.unique)
530
531 self.worker.lock.acquire()
532
533 result = 'SUCCESS'
534 if (('ZUUL_REF' in self.parameters) and
535 self.worker.shouldFailTest(self.name,
536 self.parameters['ZUUL_REF'])):
537 result = 'FAILURE'
538 if self.aborted:
539 result = 'ABORTED'
540
541 data = {'result': result}
542 changes = None
543 if 'ZUUL_CHANGE_IDS' in self.parameters:
544 changes = self.parameters['ZUUL_CHANGE_IDS']
545
546 self.worker.build_history.append(
547 BuildHistory(name=self.name, number=self.number,
548 result=result, changes=changes, node=self.node,
549 uuid=self.unique, description=self.description)
550 )
551
552 self.job.sendWorkComplete(json.dumps(data))
553 del self.worker.gearman_jobs[self.job.unique]
554 self.worker.running_builds.remove(self)
555 self.worker.lock.release()
556
557
558class FakeWorker(gear.Worker):
559 def __init__(self, worker_id):
560 super(FakeWorker, self).__init__(worker_id)
561 self.gearman_jobs = {}
562 self.build_history = []
563 self.running_builds = []
564 self.build_counter = 0
565 self.fail_tests = {}
566
567 self.hold_jobs_in_build = False
568 self.lock = threading.Lock()
569 self.__work_thread = threading.Thread(target=self.work)
570 self.__work_thread.start()
571
572 def handleJob(self, job):
573 parts = job.name.split(":")
574 cmd = parts[0]
575 name = parts[1]
576 if len(parts) > 2:
577 node = parts[2]
578 else:
579 node = None
580 if cmd == 'build':
581 self.handleBuild(job, name, node)
582 elif cmd == 'stop':
583 self.handleStop(job, name)
584 elif cmd == 'set_description':
585 self.handleSetDescription(job, name)
586
587 def handleBuild(self, job, name, node):
588 build = FakeBuild(self, job, self.build_counter, node)
589 job.build = build
590 self.gearman_jobs[job.unique] = job
591 self.build_counter += 1
592
593 self.running_builds.append(build)
594 build.start()
595
596 def handleStop(self, job, name):
597 self.log.debug("handle stop")
James E. Blair3c483cf2013-06-04 16:30:43 -0700598 parameters = json.loads(job.arguments)
599 name = parameters['name']
600 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700601 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700602 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700603 build.aborted = True
604 build.release()
605 job.sendWorkComplete()
606 return
607 job.sendWorkFail()
608
609 def handleSetDescription(self, job, name):
610 self.log.debug("handle set description")
611 parameters = json.loads(job.arguments)
James E. Blair3c483cf2013-06-04 16:30:43 -0700612 name = parameters['name']
613 number = parameters['number']
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700614 descr = parameters['html_description']
615 for build in self.running_builds:
James E. Blair3c483cf2013-06-04 16:30:43 -0700616 if build.name == name and build.number == number:
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700617 build.description = descr
618 job.sendWorkComplete()
619 return
620 for build in self.build_history:
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 job.sendWorkFail()
626
627 def work(self):
628 while self.running:
629 try:
630 job = self.getJob()
631 except gear.InterruptedError:
632 continue
633 try:
634 self.handleJob(job)
635 except:
636 self.log.exception("Worker exception:")
637
638 def addFailTest(self, name, change):
639 l = self.fail_tests.get(name, [])
640 l.append(change)
641 self.fail_tests[name] = l
642
643 def shouldFailTest(self, name, ref):
644 l = self.fail_tests.get(name, [])
645 for change in l:
646 if ref_has_change(ref, change):
647 return True
648 return False
649
650 def release(self, regex=None):
651 builds = self.running_builds[:]
652 self.log.debug("releasing build %s (%s)" % (regex,
653 len(self.running_builds)))
654 for build in builds:
655 if not regex or re.match(regex, build.name):
656 self.log.debug("releasing build %s" %
657 (build.parameters['ZUUL_UUID']))
658 build.release()
659 else:
660 self.log.debug("not releasing build %s" %
661 (build.parameters['ZUUL_UUID']))
662 self.log.debug("done releasing builds %s (%s)" %
663 (regex, len(self.running_builds)))
664
665
666class FakeGearmanServer(gear.Server):
667 def __init__(self):
668 self.hold_jobs_in_queue = False
669 super(FakeGearmanServer, self).__init__(0)
670
671 def getJobForConnection(self, connection, peek=False):
James E. Blair701c5b42013-06-06 09:34:59 -0700672 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
673 for job in queue:
674 if not hasattr(job, 'waiting'):
675 if job.name.startswith('build:'):
676 job.waiting = self.hold_jobs_in_queue
677 else:
678 job.waiting = False
679 if job.waiting:
680 continue
681 if job.name in connection.functions:
682 if not peek:
683 queue.remove(job)
684 return job
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700685 return None
686
687 def release(self, regex=None):
688 released = False
James E. Blair701c5b42013-06-06 09:34:59 -0700689 qlen = (len(self.high_queue) + len(self.normal_queue) +
690 len(self.low_queue))
691 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
692 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
693 queue = queue[:]
694 for job in queue:
695 cmd, name = job.name.split(':')
696 if cmd != 'build':
697 continue
698 if not regex or re.match(regex, name):
699 self.log.debug("releasing queued job %s" %
700 job.unique)
701 job.waiting = False
702 released = True
703 else:
704 self.log.debug("not releasing queued job %s" %
705 job.unique)
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700706 if released:
707 self.wakeConnections()
James E. Blair701c5b42013-06-06 09:34:59 -0700708 qlen = (len(self.high_queue) + len(self.normal_queue) +
709 len(self.low_queue))
710 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700711
712
James E. Blairb0fcae42012-07-17 11:12:10 -0700713class testScheduler(unittest.TestCase):
714 log = logging.getLogger("zuul.test")
715
716 def setUp(self):
James E. Blair1dbd5082012-08-23 15:12:15 -0700717 if os.path.exists(TEST_ROOT):
718 shutil.rmtree(TEST_ROOT)
719 os.makedirs(TEST_ROOT)
720 os.makedirs(UPSTREAM_ROOT)
721 os.makedirs(GIT_ROOT)
James E. Blair4886cc12012-07-18 15:39:41 -0700722
723 # For each project in config:
724 init_repo("org/project")
725 init_repo("org/project1")
726 init_repo("org/project2")
James E. Blair127bc182012-08-28 15:55:15 -0700727 init_repo("org/project3")
James E. Blair7f71c802012-08-22 13:04:32 -0700728 init_repo("org/one-job-project")
James E. Blair4ec821f2012-08-23 15:28:28 -0700729 init_repo("org/nonvoting-project")
Antoine Musso80edd5a2013-02-13 15:37:53 +0100730 init_repo("org/templated-project")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700731 init_repo("org/node-project")
James E. Blair412e5582013-04-22 15:50:12 -0700732
733 self.statsd = FakeStatsd()
734 os.environ['STATSD_HOST'] = 'localhost'
735 os.environ['STATSD_PORT'] = str(self.statsd.port)
736 self.statsd.start()
737 # the statsd client object is configured in the statsd module import
738 reload(statsd)
739 reload(zuul.scheduler)
740
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700741 self.gearman_server = FakeGearmanServer()
742
743 self.config = ConfigParser.ConfigParser()
744 cfg = StringIO()
745 CONFIG.write(cfg)
746 cfg.seek(0)
747 self.config.readfp(cfg)
748 self.config.set('gearman', 'port', str(self.gearman_server.port))
749
750 self.worker = FakeWorker('fake_worker')
751 self.worker.addServer('127.0.0.1', self.gearman_server.port)
752 self.gearman_server.worker = self.worker
753
James E. Blairb0fcae42012-07-17 11:12:10 -0700754 self.sched = zuul.scheduler.Scheduler()
755
James E. Blair8cc15a82012-08-01 11:17:57 -0700756 def URLOpenerFactory(*args, **kw):
757 args = [self.fake_gerrit] + list(args)
758 return FakeURLOpener(*args, **kw)
759
James E. Blair8cc15a82012-08-01 11:17:57 -0700760 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700761 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700762
763 zuul.lib.gerrit.Gerrit = FakeGerrit
764
James E. Blair4886cc12012-07-18 15:39:41 -0700765 self.gerrit = FakeGerritTrigger(self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700766 self.gerrit.replication_timeout = 1.5
767 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700768 self.fake_gerrit = self.gerrit.gerrit
769
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700770 self.sched.setLauncher(self.launcher)
James E. Blairb0fcae42012-07-17 11:12:10 -0700771 self.sched.setTrigger(self.gerrit)
772
773 self.sched.start()
774 self.sched.reconfigure(self.config)
775 self.sched.resume()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700776 self.launcher.gearman.waitForServer()
777 self.registerJobs()
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400778 self.builds = self.worker.running_builds
779 self.history = self.worker.build_history
James E. Blairb0fcae42012-07-17 11:12:10 -0700780
781 def tearDown(self):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700782 self.launcher.stop()
783 self.worker.shutdown()
784 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700785 self.gerrit.stop()
786 self.sched.stop()
787 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700788 self.statsd.stop()
789 self.statsd.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700790 threads = threading.enumerate()
791 if len(threads) > 1:
792 self.log.error("More than one thread is running: %s" % threads)
James E. Blair1dbd5082012-08-23 15:12:15 -0700793 #shutil.rmtree(TEST_ROOT)
James E. Blairb0fcae42012-07-17 11:12:10 -0700794
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700795 def registerJobs(self):
796 count = 0
797 for job in self.sched.jobs.keys():
798 self.worker.registerFunction('build:' + job)
799 count += 1
800 self.worker.registerFunction('stop:' + self.worker.worker_id)
801 count += 1
802
803 while len(self.gearman_server.functions) < count:
804 time.sleep(0)
805
806 def release(self, job):
807 if isinstance(job, FakeBuild):
808 job.release()
809 else:
810 job.waiting = False
811 self.log.debug("Queued job %s released" % job.unique)
812 self.gearman_server.wakeConnections()
813
814 def getParameter(self, job, name):
815 if isinstance(job, FakeBuild):
816 return job.parameters[name]
817 else:
818 parameters = json.loads(job.arguments)
819 return parameters[name]
820
821 def resetGearmanServer(self):
822 self.worker.setFunctions([])
823 while True:
824 done = True
825 for connection in self.gearman_server.active_connections:
826 if connection.functions:
827 done = False
828 if done:
829 break
830 time.sleep(0)
831 self.gearman_server.functions = set()
832
833 def haveAllBuildsReported(self):
834 # See if Zuul is waiting on a meta job to complete
835 if self.launcher.meta_jobs:
836 return False
837 # Find out if every build that the worker has completed has been
838 # reported back to Zuul. If it hasn't then that means a Gearman
839 # event is still in transit and the system is not stable.
840 for build in self.worker.build_history:
841 zbuild = self.launcher.builds.get(build.uuid)
842 if not zbuild:
843 # It has already been reported
844 continue
845 # It hasn't been reported yet.
846 return False
847 # Make sure that none of the worker connections are in GRAB_WAIT
848 for connection in self.worker.active_connections:
849 if connection.state == 'GRAB_WAIT':
850 return False
851 return True
852
853 def areAllBuildsWaiting(self):
854 ret = True
855
856 builds = self.launcher.builds.values()
857 for build in builds:
858 client_job = None
859 for conn in self.launcher.gearman.active_connections:
860 for j in conn.related_jobs.values():
861 if j.unique == build.uuid:
862 client_job = j
863 break
864 if not client_job:
865 self.log.debug("%s is not known to the gearman client" %
866 build)
867 ret = False
868 continue
869 if not client_job.handle:
870 self.log.debug("%s has no handle" % client_job)
871 ret = False
872 continue
873 server_job = self.gearman_server.jobs.get(client_job.handle)
874 if not server_job:
875 self.log.debug("%s is not known to the gearman server" %
876 client_job)
877 ret = False
878 continue
879 if not hasattr(server_job, 'waiting'):
880 self.log.debug("%s is being enqueued" % server_job)
881 ret = False
882 continue
883 if server_job.waiting:
884 continue
885 worker_job = self.worker.gearman_jobs.get(server_job.unique)
886 if worker_job:
887 if worker_job.build.isWaiting():
888 continue
889 else:
890 self.log.debug("%s is running" % worker_job)
891 ret = False
892 else:
893 self.log.debug("%s is unassigned" % server_job)
894 ret = False
895 return ret
896
James E. Blairb0fcae42012-07-17 11:12:10 -0700897 def waitUntilSettled(self):
898 self.log.debug("Waiting until settled...")
899 start = time.time()
900 while True:
901 if time.time() - start > 10:
902 print 'queue status:',
903 print self.sched.trigger_event_queue.empty(),
904 print self.sched.result_event_queue.empty(),
905 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700906 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -0700907 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700908 # Make sure no new events show up while we're checking
909 self.worker.lock.acquire()
910 # have all build states propogated to zuul?
911 if self.haveAllBuildsReported():
912 # Join ensures that the queue is empty _and_ events have been
913 # processed
914 self.fake_gerrit.event_queue.join()
915 self.sched.trigger_event_queue.join()
916 self.sched.result_event_queue.join()
917 if (self.sched.trigger_event_queue.empty() and
918 self.sched.result_event_queue.empty() and
919 self.fake_gerrit.event_queue.empty() and
920 self.areAllBuildsWaiting()):
921 self.worker.lock.release()
922 self.log.debug("...settled.")
923 return
924 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700925 self.sched.wake_event.wait(0.1)
926
James E. Blaird466dc42012-07-31 10:42:56 -0700927 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -0800928 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -0700929 return len(jobs)
930
James E. Blair4ca985f2013-05-30 12:27:43 -0700931 def getJobFromHistory(self, name):
932 history = self.worker.build_history
933 for job in history:
934 if job.name == name:
935 return job
936 raise Exception("Unable to find job %s in history" % name)
937
James E. Blaire0487072012-08-29 17:38:31 -0700938 def assertEmptyQueues(self):
939 # Make sure there are no orphaned jobs
940 for pipeline in self.sched.pipelines.values():
941 for queue in pipeline.queues:
942 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -0800943 print 'pipeline %s queue %s contents %s' % (
944 pipeline.name, queue.name, queue.queue)
James E. Blaire0487072012-08-29 17:38:31 -0700945 assert len(queue.queue) == 0
946 if len(queue.severed_heads) != 0:
947 print 'heads', queue.severed_heads
948 assert len(queue.severed_heads) == 0
949
James E. Blair412e5582013-04-22 15:50:12 -0700950 def assertReportedStat(self, key, value=None):
951 start = time.time()
952 while time.time() < (start + 5):
953 for stat in self.statsd.stats:
954 k, v = stat.split(':')
955 if key == k:
956 if value is None:
957 return
958 if value == v:
959 return
960 time.sleep(0.1)
961
962 pprint.pprint(self.statsd.stats)
963 raise Exception("Key %s not found in reported stats" % key)
964
James E. Blairb0fcae42012-07-17 11:12:10 -0700965 def test_jobs_launched(self):
966 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700967
James E. Blairb0fcae42012-07-17 11:12:10 -0700968 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -0700969 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700970 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
971 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -0700972 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
973 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
974 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blairb0fcae42012-07-17 11:12:10 -0700975 assert A.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700976 assert A.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700977 self.assertEmptyQueues()
James E. Blairb0fcae42012-07-17 11:12:10 -0700978
James E. Blair412e5582013-04-22 15:50:12 -0700979 self.assertReportedStat('gerrit.event.comment-added', '1|c')
980 self.assertReportedStat('zuul.pipeline.gate.current_changes', '1|g')
981 self.assertReportedStat('zuul.job.project-merge')
982 self.assertReportedStat('zuul.pipeline.gate.resident_time')
983 self.assertReportedStat('zuul.pipeline.gate.total_changes', '1|c')
984 self.assertReportedStat(
985 'zuul.pipeline.gate.org.project.resident_time')
986 self.assertReportedStat(
987 'zuul.pipeline.gate.org.project.total_changes', '1|c')
988
James E. Blair42f74822013-05-14 15:18:03 -0700989 def test_duplicate_pipelines(self):
990 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -0700991
James E. Blair42f74822013-05-14 15:18:03 -0700992 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
993 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
994 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -0700995
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400996 print self.builds
James E. Blair42f74822013-05-14 15:18:03 -0700997 print A.messages
998
999 self.assertEmptyQueues()
1000
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001001 assert len(self.history) == 2
1002 self.history[0].name == 'project-test1'
1003 self.history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -07001004
1005 assert len(A.messages) == 2
1006 if 'dup1/project-test1' in A.messages[0]:
1007 assert 'dup1/project-test1' in A.messages[0]
1008 assert 'dup2/project-test1' not in A.messages[0]
1009 assert 'dup1/project-test1' not in A.messages[1]
1010 assert 'dup2/project-test1' in A.messages[1]
1011 else:
1012 assert 'dup1/project-test1' in A.messages[1]
1013 assert 'dup2/project-test1' not in A.messages[1]
1014 assert 'dup1/project-test1' not in A.messages[0]
1015 assert 'dup2/project-test1' in A.messages[0]
1016
James E. Blairb0fcae42012-07-17 11:12:10 -07001017 def test_parallel_changes(self):
1018 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001019
1020 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001021 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1022 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1023 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001024 A.addApproval('CRVW', 2)
1025 B.addApproval('CRVW', 2)
1026 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001027
1028 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1029 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1030 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1031
1032 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001033 assert len(self.builds) == 1
1034 assert self.builds[0].name == 'project-merge'
1035 assert job_has_changes(self.builds[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -07001036
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001037 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001038 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001039 assert len(self.builds) == 3
1040 assert self.builds[0].name == 'project-test1'
1041 assert job_has_changes(self.builds[0], A)
1042 assert self.builds[1].name == 'project-test2'
1043 assert job_has_changes(self.builds[1], A)
1044 assert self.builds[2].name == 'project-merge'
1045 assert job_has_changes(self.builds[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -07001046
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001047 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001048 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001049 assert len(self.builds) == 5
1050 assert self.builds[0].name == 'project-test1'
1051 assert job_has_changes(self.builds[0], A)
1052 assert self.builds[1].name == 'project-test2'
1053 assert job_has_changes(self.builds[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -07001054
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001055 assert self.builds[2].name == 'project-test1'
1056 assert job_has_changes(self.builds[2], A, B)
1057 assert self.builds[3].name == 'project-test2'
1058 assert job_has_changes(self.builds[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -07001059
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001060 assert self.builds[4].name == 'project-merge'
1061 assert job_has_changes(self.builds[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -07001062
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001063 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001064 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001065 assert len(self.builds) == 6
1066 assert self.builds[0].name == 'project-test1'
1067 assert job_has_changes(self.builds[0], A)
1068 assert self.builds[1].name == 'project-test2'
1069 assert job_has_changes(self.builds[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -07001070
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001071 assert self.builds[2].name == 'project-test1'
1072 assert job_has_changes(self.builds[2], A, B)
1073 assert self.builds[3].name == 'project-test2'
1074 assert job_has_changes(self.builds[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -07001075
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001076 assert self.builds[4].name == 'project-test1'
1077 assert job_has_changes(self.builds[4], A, B, C)
1078 assert self.builds[5].name == 'project-test2'
1079 assert job_has_changes(self.builds[5], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -07001080
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001081 self.worker.hold_jobs_in_build = False
1082 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001083 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001084 assert len(self.builds) == 0
James E. Blairb0fcae42012-07-17 11:12:10 -07001085
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001086 assert len(self.history) == 9
James E. Blairb0fcae42012-07-17 11:12:10 -07001087 assert A.data['status'] == 'MERGED'
1088 assert B.data['status'] == 'MERGED'
1089 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -07001090 assert A.reported == 2
1091 assert B.reported == 2
1092 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001093 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001094
1095 def test_failed_changes(self):
1096 "Test that a change behind a failed change is retested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001097
James E. Blairb02a3bb2012-07-30 17:49:55 -07001098 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1099 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001100 A.addApproval('CRVW', 2)
1101 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001102
1103 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1104 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1105
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001106 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001107
1108 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001109 assert len(self.history) > 6
James E. Blairb02a3bb2012-07-30 17:49:55 -07001110 assert A.data['status'] == 'NEW'
1111 assert B.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -07001112 assert A.reported == 2
1113 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001114 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001115
1116 def test_independent_queues(self):
1117 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001118
1119 self.worker.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +09001120 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001121 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1122 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001123 A.addApproval('CRVW', 2)
1124 B.addApproval('CRVW', 2)
1125 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001126
1127 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1128 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1129 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1130
James E. Blairb02a3bb2012-07-30 17:49:55 -07001131 self.waitUntilSettled()
1132
1133 # There should be one merge job at the head of each queue running
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001134 assert len(self.builds) == 2
1135 assert self.builds[0].name == 'project-merge'
1136 assert job_has_changes(self.builds[0], A)
1137 assert self.builds[1].name == 'project1-merge'
1138 assert job_has_changes(self.builds[1], B)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001139
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001140 # Release the current merge builds
1141 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001142 self.waitUntilSettled()
1143 # Release the merge job for project2 which is behind project1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001144 self.worker.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -07001145 self.waitUntilSettled()
1146
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001147 # All the test builds should be running:
James E. Blairb02a3bb2012-07-30 17:49:55 -07001148 # project1 (3) + project2 (3) + project (2) = 8
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001149 assert len(self.builds) == 8
James E. Blairb02a3bb2012-07-30 17:49:55 -07001150
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001151 self.worker.release()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001152 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001153 assert len(self.builds) == 0
James E. Blairb02a3bb2012-07-30 17:49:55 -07001154
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001155 assert len(self.history) == 11
James E. Blairb02a3bb2012-07-30 17:49:55 -07001156 assert A.data['status'] == 'MERGED'
1157 assert B.data['status'] == 'MERGED'
1158 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -07001159 assert A.reported == 2
1160 assert B.reported == 2
1161 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001162 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -07001163
1164 def test_failed_change_at_head(self):
1165 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001166
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001167 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001168 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1169 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1170 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001171 A.addApproval('CRVW', 2)
1172 B.addApproval('CRVW', 2)
1173 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001174
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001175 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001176
1177 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1178 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1179 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1180
1181 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001182
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001183 assert len(self.builds) == 1
1184 assert self.builds[0].name == 'project-merge'
1185 assert job_has_changes(self.builds[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -07001186
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001187 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001188 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001189 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001190 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001191 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001192 self.waitUntilSettled()
1193
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001194 assert len(self.builds) == 6
1195 assert self.builds[0].name == 'project-test1'
1196 assert self.builds[1].name == 'project-test2'
1197 assert self.builds[2].name == 'project-test1'
1198 assert self.builds[3].name == 'project-test2'
1199 assert self.builds[4].name == 'project-test1'
1200 assert self.builds[5].name == 'project-test2'
James E. Blaird466dc42012-07-31 10:42:56 -07001201
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001202 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001203 self.waitUntilSettled()
1204
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001205 assert len(self.builds) == 2 # project-test2, project-merge for B
1206 assert self.countJobResults(self.history, 'ABORTED') == 4
James E. Blaird466dc42012-07-31 10:42:56 -07001207
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001208 self.worker.hold_jobs_in_build = False
1209 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001210 self.waitUntilSettled()
1211
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001212 assert len(self.builds) == 0
1213 assert len(self.history) == 15
James E. Blaird466dc42012-07-31 10:42:56 -07001214 assert A.data['status'] == 'NEW'
1215 assert B.data['status'] == 'MERGED'
1216 assert C.data['status'] == 'MERGED'
1217 assert A.reported == 2
1218 assert B.reported == 2
1219 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001220 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -07001221
1222 def test_failed_change_at_head_with_queue(self):
1223 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -07001224
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001225 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001226 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1227 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1228 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001229 A.addApproval('CRVW', 2)
1230 B.addApproval('CRVW', 2)
1231 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001232
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001233 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001234
1235 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1236 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1237 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1238
1239 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001240 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001241 assert len(self.builds) == 0
James E. Blaird466dc42012-07-31 10:42:56 -07001242 assert len(queue) == 1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001243 assert queue[0].name == 'build:project-merge'
1244 assert job_has_changes(queue[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -07001245
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001246 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001247 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001248 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001249 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001250 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001251 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001252 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -07001253
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001254 assert len(self.builds) == 0
James E. Blaird466dc42012-07-31 10:42:56 -07001255 assert len(queue) == 6
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001256 assert queue[0].name == 'build:project-test1'
1257 assert queue[1].name == 'build:project-test2'
1258 assert queue[2].name == 'build:project-test1'
1259 assert queue[3].name == 'build:project-test2'
1260 assert queue[4].name == 'build:project-test1'
1261 assert queue[5].name == 'build:project-test2'
James E. Blaird466dc42012-07-31 10:42:56 -07001262
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001263 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001264 self.waitUntilSettled()
1265
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001266 assert len(self.builds) == 0
James E. Blair701c5b42013-06-06 09:34:59 -07001267 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001268 assert len(queue) == 2 # project-test2, project-merge for B
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001269 assert self.countJobResults(self.history, 'ABORTED') == 0
James E. Blaird466dc42012-07-31 10:42:56 -07001270
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001271 self.gearman_server.hold_jobs_in_queue = False
1272 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001273 self.waitUntilSettled()
1274
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001275 assert len(self.builds) == 0
1276 assert len(self.history) == 11
James E. Blaird466dc42012-07-31 10:42:56 -07001277 assert A.data['status'] == 'NEW'
1278 assert B.data['status'] == 'MERGED'
1279 assert C.data['status'] == 'MERGED'
1280 assert A.reported == 2
1281 assert B.reported == 2
1282 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001283 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001284
1285 def test_patch_order(self):
1286 "Test that dependent patches are tested in the right order"
1287 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1288 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1289 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1290 A.addApproval('CRVW', 2)
1291 B.addApproval('CRVW', 2)
1292 C.addApproval('CRVW', 2)
1293
1294 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1295 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1296 M2.setMerged()
1297 M1.setMerged()
1298
1299 # C -> B -> A -> M1 -> M2
1300 # M2 is here to make sure it is never queried. If it is, it
1301 # means zuul is walking down the entire history of merged
1302 # changes.
1303
1304 C.setDependsOn(B, 1)
1305 B.setDependsOn(A, 1)
1306 A.setDependsOn(M1, 1)
1307 M1.setDependsOn(M2, 1)
1308
1309 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1310
1311 self.waitUntilSettled()
1312
1313 assert A.data['status'] == 'NEW'
1314 assert B.data['status'] == 'NEW'
1315 assert C.data['status'] == 'NEW'
1316
1317 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1318 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1319
1320 self.waitUntilSettled()
1321 assert M2.queried == 0
1322 assert A.data['status'] == 'MERGED'
1323 assert B.data['status'] == 'MERGED'
1324 assert C.data['status'] == 'MERGED'
1325 assert A.reported == 2
1326 assert B.reported == 2
1327 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001328 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001329
1330 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001331 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001332 # TODO: move to test_gerrit (this is a unit test!)
1333 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4aea70c2012-07-26 14:23:24 -07001334 a = self.sched.trigger.getChange(1, 2)
1335 mgr = self.sched.pipelines['gate'].manager
1336 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001337
1338 A.addApproval('CRVW', 2)
James E. Blair4aea70c2012-07-26 14:23:24 -07001339 a = self.sched.trigger.getChange(1, 2)
1340 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001341
1342 A.addApproval('APRV', 1)
James E. Blair4aea70c2012-07-26 14:23:24 -07001343 a = self.sched.trigger.getChange(1, 2)
1344 assert self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blaire0487072012-08-29 17:38:31 -07001345 self.assertEmptyQueues()
James E. Blair4886cc12012-07-18 15:39:41 -07001346
1347 def test_build_configuration(self):
1348 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001349
1350 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001351 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1352 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1353 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1354 A.addApproval('CRVW', 2)
1355 B.addApproval('CRVW', 2)
1356 C.addApproval('CRVW', 2)
1357 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1358 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1359 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1360 self.waitUntilSettled()
1361
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001362 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001363 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001364 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001365 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001366 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001367 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001368 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001369 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1370 self.gearman_server.hold_jobs_in_queue = False
1371 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001372 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001373
James E. Blair1dbd5082012-08-23 15:12:15 -07001374 path = os.path.join(GIT_ROOT, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001375 repo = git.Repo(path)
1376 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1377 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001378 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
1379 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001380 self.assertEmptyQueues()
James E. Blair973721f2012-08-15 10:19:43 -07001381
1382 def test_build_configuration_conflict(self):
1383 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001384
1385 self.gearman_server.hold_jobs_in_queue = True
James E. Blair973721f2012-08-15 10:19:43 -07001386 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1387 A.addPatchset(['conflict'])
1388 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1389 B.addPatchset(['conflict'])
1390 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1391 A.addApproval('CRVW', 2)
1392 B.addApproval('CRVW', 2)
1393 C.addApproval('CRVW', 2)
1394 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1395 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1396 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1397 self.waitUntilSettled()
1398
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001399 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001400 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001401 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001402 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001403 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001404 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001405 queue = self.gearman_server.getQueue()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001406 self.getParameter(queue[-1], 'ZUUL_REF')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001407 self.gearman_server.hold_jobs_in_queue = False
1408 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001409 self.waitUntilSettled()
1410
1411 assert A.data['status'] == 'MERGED'
1412 assert B.data['status'] == 'NEW'
1413 assert C.data['status'] == 'MERGED'
1414 assert A.reported == 2
1415 assert B.reported == 2
1416 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001417 self.assertEmptyQueues()
James E. Blairdaabed22012-08-15 15:38:57 -07001418
1419 def test_post(self):
1420 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001421
Zhongyue Luo5d556072012-09-21 02:00:47 +09001422 e = {
1423 "type": "ref-updated",
1424 "submitter": {
1425 "name": "User Name",
1426 },
1427 "refUpdate": {
1428 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1429 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1430 "refName": "master",
1431 "project": "org/project",
1432 }
1433 }
James E. Blairdaabed22012-08-15 15:38:57 -07001434 self.fake_gerrit.addEvent(e)
1435 self.waitUntilSettled()
1436
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001437 job_names = [x.name for x in self.history]
1438 assert len(self.history) == 1
James E. Blairdaabed22012-08-15 15:38:57 -07001439 assert 'project-post' in job_names
James E. Blaire0487072012-08-29 17:38:31 -07001440 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001441
1442 def test_build_configuration_branch(self):
1443 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001444
1445 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001446 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1447 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1448 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1449 A.addApproval('CRVW', 2)
1450 B.addApproval('CRVW', 2)
1451 C.addApproval('CRVW', 2)
1452 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1453 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1454 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1455 self.waitUntilSettled()
1456
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001457 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001458 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001459 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001460 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001461 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001462 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001463 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001464 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1465 self.gearman_server.hold_jobs_in_queue = False
1466 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001467 self.waitUntilSettled()
1468
James E. Blair1dbd5082012-08-23 15:12:15 -07001469 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001470 repo = git.Repo(path)
1471 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1472 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001473 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
1474 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001475 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001476
1477 def test_build_configuration_branch_interaction(self):
1478 "Test that switching between branches works"
1479 self.test_build_configuration()
1480 self.test_build_configuration_branch()
1481 # C has been merged, undo that
James E. Blair1dbd5082012-08-23 15:12:15 -07001482 path = os.path.join(UPSTREAM_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001483 repo = git.Repo(path)
1484 repo.heads.master.commit = repo.commit('init')
1485 self.test_build_configuration()
James E. Blaire0487072012-08-29 17:38:31 -07001486 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001487
1488 def test_build_configuration_multi_branch(self):
1489 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001490
1491 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001492 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1493 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1494 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1495 A.addApproval('CRVW', 2)
1496 B.addApproval('CRVW', 2)
1497 C.addApproval('CRVW', 2)
1498 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1499 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1500 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1501 self.waitUntilSettled()
1502
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001503 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001504 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001505 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001506 ref_mp = self.getParameter(queue[-1], 'ZUUL_REF')
1507 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001508 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001509 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001510 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -07001511 queue = self.gearman_server.getQueue()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001512 ref_master = self.getParameter(queue[-1], 'ZUUL_REF')
1513 self.gearman_server.hold_jobs_in_queue = False
1514 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001515 self.waitUntilSettled()
1516
James E. Blair1dbd5082012-08-23 15:12:15 -07001517 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001518 repo = git.Repo(path)
1519
1520 repo_messages = [c.message.strip()
1521 for c in repo.iter_commits(ref_master)]
1522 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001523 correct_messages = ['initial commit', 'A-1', 'C-1']
1524 assert repo_messages == correct_messages
1525
1526 repo_messages = [c.message.strip()
1527 for c in repo.iter_commits(ref_mp)]
1528 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001529 correct_messages = ['initial commit', 'mp commit', 'B-1']
1530 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001531 self.assertEmptyQueues()
James E. Blair7f71c802012-08-22 13:04:32 -07001532
1533 def test_one_job_project(self):
1534 "Test that queueing works with one job"
1535 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1536 'master', 'A')
1537 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1538 'master', 'B')
1539 A.addApproval('CRVW', 2)
1540 B.addApproval('CRVW', 2)
1541 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1542 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1543 self.waitUntilSettled()
1544
James E. Blair7f71c802012-08-22 13:04:32 -07001545 assert A.data['status'] == 'MERGED'
1546 assert A.reported == 2
1547 assert B.data['status'] == 'MERGED'
1548 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001549 self.assertEmptyQueues()
James E. Blaircaec0c52012-08-22 14:52:22 -07001550
Antoine Musso80edd5a2013-02-13 15:37:53 +01001551 def test_job_from_templates_launched(self):
1552 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001553
Antoine Musso80edd5a2013-02-13 15:37:53 +01001554 A = self.fake_gerrit.addFakeChange(
1555 'org/templated-project', 'master', 'A')
1556 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1557 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001558
James E. Blair4ca985f2013-05-30 12:27:43 -07001559 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1560 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
Antoine Musso80edd5a2013-02-13 15:37:53 +01001561
James E. Blaircaec0c52012-08-22 14:52:22 -07001562 def test_dependent_changes_dequeue(self):
1563 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001564
James E. Blaircaec0c52012-08-22 14:52:22 -07001565 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1566 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1567 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1568 A.addApproval('CRVW', 2)
1569 B.addApproval('CRVW', 2)
1570 C.addApproval('CRVW', 2)
1571
1572 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1573 M1.setMerged()
1574
1575 # C -> B -> A -> M1
1576
1577 C.setDependsOn(B, 1)
1578 B.setDependsOn(A, 1)
1579 A.setDependsOn(M1, 1)
1580
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001581 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001582
1583 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1584 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1585 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1586
1587 self.waitUntilSettled()
1588
James E. Blairec590122012-08-22 15:19:31 -07001589 assert A.data['status'] == 'NEW'
1590 assert A.reported == 2
1591 assert B.data['status'] == 'NEW'
1592 assert B.reported == 2
1593 assert C.data['status'] == 'NEW'
1594 assert C.reported == 2
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001595 assert len(self.history) == 1
James E. Blaire0487072012-08-29 17:38:31 -07001596 self.assertEmptyQueues()
James E. Blairec590122012-08-22 15:19:31 -07001597
1598 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001599 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001600 # If it's dequeued more than once, we should see extra
1601 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001602
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001603 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001604 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1605 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1606 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1607 A.addApproval('CRVW', 2)
1608 B.addApproval('CRVW', 2)
1609 C.addApproval('CRVW', 2)
1610
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001611 self.worker.addFailTest('project1-test1', A)
1612 self.worker.addFailTest('project1-test2', A)
1613 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001614
1615 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1616 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1617 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1618
1619 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001620
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001621 assert len(self.builds) == 1
1622 assert self.builds[0].name == 'project1-merge'
1623 assert job_has_changes(self.builds[0], A)
James E. Blairec590122012-08-22 15:19:31 -07001624
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001625 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001626 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001627 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001628 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001629 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001630 self.waitUntilSettled()
1631
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001632 assert len(self.builds) == 9
1633 assert self.builds[0].name == 'project1-test1'
1634 assert self.builds[1].name == 'project1-test2'
1635 assert self.builds[2].name == 'project1-project2-integration'
1636 assert self.builds[3].name == 'project1-test1'
1637 assert self.builds[4].name == 'project1-test2'
1638 assert self.builds[5].name == 'project1-project2-integration'
1639 assert self.builds[6].name == 'project1-test1'
1640 assert self.builds[7].name == 'project1-test2'
1641 assert self.builds[8].name == 'project1-project2-integration'
James E. Blairec590122012-08-22 15:19:31 -07001642
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001643 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001644 self.waitUntilSettled()
1645
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001646 assert len(self.builds) == 3 # test2, integration, merge for B
1647 assert self.countJobResults(self.history, 'ABORTED') == 6
James E. Blairec590122012-08-22 15:19:31 -07001648
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001649 self.worker.hold_jobs_in_build = False
1650 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001651 self.waitUntilSettled()
1652
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001653 assert len(self.builds) == 0
1654 assert len(self.history) == 20
James E. Blaircaec0c52012-08-22 14:52:22 -07001655
1656 assert A.data['status'] == 'NEW'
James E. Blairec590122012-08-22 15:19:31 -07001657 assert B.data['status'] == 'MERGED'
1658 assert C.data['status'] == 'MERGED'
1659 assert A.reported == 2
1660 assert B.reported == 2
1661 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001662 self.assertEmptyQueues()
James E. Blair4ec821f2012-08-23 15:28:28 -07001663
1664 def test_nonvoting_job(self):
1665 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001666
James E. Blair4ec821f2012-08-23 15:28:28 -07001667 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1668 'master', 'A')
1669 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001670 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001671 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1672
1673 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001674
1675 assert A.data['status'] == 'MERGED'
1676 assert A.reported == 2
James E. Blair4ca985f2013-05-30 12:27:43 -07001677 assert (self.getJobFromHistory('nonvoting-project-merge').result ==
1678 'SUCCESS')
1679 assert (self.getJobFromHistory('nonvoting-project-test1').result ==
1680 'SUCCESS')
1681 assert (self.getJobFromHistory('nonvoting-project-test2').result ==
1682 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001683 self.assertEmptyQueues()
1684
1685 def test_check_queue_success(self):
1686 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001687
James E. Blaire0487072012-08-29 17:38:31 -07001688 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1689 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1690
1691 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001692
1693 assert A.data['status'] == 'NEW'
1694 assert A.reported == 1
James E. Blair4ca985f2013-05-30 12:27:43 -07001695 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1696 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1697 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blaire0487072012-08-29 17:38:31 -07001698 self.assertEmptyQueues()
1699
1700 def test_check_queue_failure(self):
1701 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001702
James E. Blaire0487072012-08-29 17:38:31 -07001703 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001704 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001705 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1706
1707 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001708
1709 assert A.data['status'] == 'NEW'
1710 assert A.reported == 1
James E. Blair4ca985f2013-05-30 12:27:43 -07001711 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1712 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1713 assert self.getJobFromHistory('project-test2').result == 'FAILURE'
James E. Blaire0487072012-08-29 17:38:31 -07001714 self.assertEmptyQueues()
James E. Blair127bc182012-08-28 15:55:15 -07001715
1716 def test_dependent_behind_dequeue(self):
1717 "test that dependent changes behind dequeued changes work"
1718 # This complicated test is a reproduction of a real life bug
1719 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001720
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001721 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001722 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1723 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1724 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1725 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1726 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1727 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1728 D.setDependsOn(C, 1)
1729 E.setDependsOn(D, 1)
1730 A.addApproval('CRVW', 2)
1731 B.addApproval('CRVW', 2)
1732 C.addApproval('CRVW', 2)
1733 D.addApproval('CRVW', 2)
1734 E.addApproval('CRVW', 2)
1735 F.addApproval('CRVW', 2)
1736
1737 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001738
1739 # Change object re-use in the gerrit trigger is hidden if
1740 # changes are added in quick succession; waiting makes it more
1741 # like real life.
1742 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1743 self.waitUntilSettled()
1744 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1745 self.waitUntilSettled()
1746
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001747 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001748 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001749 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001750 self.waitUntilSettled()
1751
1752 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1753 self.waitUntilSettled()
1754 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1755 self.waitUntilSettled()
1756 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1757 self.waitUntilSettled()
1758 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1759 self.waitUntilSettled()
1760
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001761 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001762 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001763 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001764 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001765 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001766 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001767 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001768 self.waitUntilSettled()
1769
1770 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001771
1772 # Grab pointers to the jobs we want to release before
1773 # releasing any, because list indexes may change as
1774 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001775 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001776 a.release()
1777 b.release()
1778 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001779 self.waitUntilSettled()
1780
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001781 self.worker.hold_jobs_in_build = False
1782 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07001783 self.waitUntilSettled()
1784
James E. Blair127bc182012-08-28 15:55:15 -07001785 assert A.data['status'] == 'NEW'
1786 assert B.data['status'] == 'MERGED'
1787 assert C.data['status'] == 'MERGED'
1788 assert D.data['status'] == 'MERGED'
1789 assert E.data['status'] == 'MERGED'
1790 assert F.data['status'] == 'MERGED'
1791
1792 assert A.reported == 2
1793 assert B.reported == 2
1794 assert C.reported == 2
1795 assert D.reported == 2
1796 assert E.reported == 2
1797 assert F.reported == 2
1798
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001799 assert self.countJobResults(self.history, 'ABORTED') == 15
1800 assert len(self.history) == 44
James E. Blaire0487072012-08-29 17:38:31 -07001801 self.assertEmptyQueues()
James E. Blair05fed602012-09-07 12:45:24 -07001802
1803 def test_merger_repack(self):
1804 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001805
James E. Blair05fed602012-09-07 12:45:24 -07001806 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1807 A.addApproval('CRVW', 2)
1808 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1809 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001810 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1811 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1812 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blair05fed602012-09-07 12:45:24 -07001813 assert A.data['status'] == 'MERGED'
1814 assert A.reported == 2
1815 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07001816 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001817
1818 path = os.path.join(GIT_ROOT, "org/project")
1819 os.system('git --git-dir=%s/.git repack -afd' % path)
1820
1821 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1822 A.addApproval('CRVW', 2)
1823 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1824 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001825 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1826 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1827 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blair05fed602012-09-07 12:45:24 -07001828 assert A.data['status'] == 'MERGED'
1829 assert A.reported == 2
1830 self.assertEmptyQueues()
James E. Blair7ee88a22012-09-12 18:59:31 +02001831
James E. Blair4886f282012-11-15 09:27:33 -08001832 def test_merger_repack_large_change(self):
1833 "Test that the merger works with large changes after a repack"
1834 # https://bugs.launchpad.net/zuul/+bug/1078946
1835 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1836 A.addPatchset(large=True)
1837 path = os.path.join(UPSTREAM_ROOT, "org/project1")
1838 os.system('git --git-dir=%s/.git repack -afd' % path)
1839 path = os.path.join(GIT_ROOT, "org/project1")
1840 os.system('git --git-dir=%s/.git repack -afd' % path)
1841
1842 A.addApproval('CRVW', 2)
1843 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1844 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001845 assert self.getJobFromHistory('project1-merge').result == 'SUCCESS'
1846 assert self.getJobFromHistory('project1-test1').result == 'SUCCESS'
1847 assert self.getJobFromHistory('project1-test2').result == 'SUCCESS'
James E. Blair4886f282012-11-15 09:27:33 -08001848 assert A.data['status'] == 'MERGED'
1849 assert A.reported == 2
1850 self.assertEmptyQueues()
1851
James E. Blair7ee88a22012-09-12 18:59:31 +02001852 def test_nonexistent_job(self):
1853 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001854 # Set to the state immediately after a restart
1855 self.resetGearmanServer()
1856 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02001857
1858 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1859 A.addApproval('CRVW', 2)
1860 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1861 # There may be a thread about to report a lost change
1862 while A.reported < 2:
1863 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001864 job_names = [x.name for x in self.history]
James E. Blair7ee88a22012-09-12 18:59:31 +02001865 assert not job_names
1866 assert A.data['status'] == 'NEW'
1867 assert A.reported == 2
1868 self.assertEmptyQueues()
1869
1870 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001871 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02001872 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1873 A.addApproval('CRVW', 2)
1874 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1875 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001876 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1877 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1878 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blair7ee88a22012-09-12 18:59:31 +02001879 assert A.data['status'] == 'MERGED'
1880 assert A.reported == 2
1881 self.assertEmptyQueues()
James E. Blairf62d4282012-12-31 17:01:50 -08001882
1883 def test_single_nonexistent_post_job(self):
1884 "Test launching a single post job that doesn't exist"
James E. Blairf62d4282012-12-31 17:01:50 -08001885 e = {
1886 "type": "ref-updated",
1887 "submitter": {
1888 "name": "User Name",
1889 },
1890 "refUpdate": {
1891 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1892 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1893 "refName": "master",
1894 "project": "org/project",
1895 }
1896 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001897 # Set to the state immediately after a restart
1898 self.resetGearmanServer()
1899 self.launcher.negative_function_cache_ttl = 0
1900
James E. Blairf62d4282012-12-31 17:01:50 -08001901 self.fake_gerrit.addEvent(e)
1902 self.waitUntilSettled()
1903
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001904 assert len(self.history) == 0
James E. Blairf62d4282012-12-31 17:01:50 -08001905 self.assertEmptyQueues()
James E. Blair2fa50962013-01-30 21:50:41 -08001906
1907 def test_new_patchset_dequeues_old(self):
1908 "Test that a new patchset causes the old to be dequeued"
1909 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001910 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001911 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1912 M.setMerged()
1913
1914 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1915 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1916 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1917 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1918 A.addApproval('CRVW', 2)
1919 B.addApproval('CRVW', 2)
1920 C.addApproval('CRVW', 2)
1921 D.addApproval('CRVW', 2)
1922
1923 C.setDependsOn(B, 1)
1924 B.setDependsOn(A, 1)
1925 A.setDependsOn(M, 1)
1926
1927 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1928 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1929 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1930 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1931 self.waitUntilSettled()
1932
1933 B.addPatchset()
1934 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1935 self.waitUntilSettled()
1936
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001937 self.worker.hold_jobs_in_build = False
1938 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001939 self.waitUntilSettled()
1940
James E. Blair2fa50962013-01-30 21:50:41 -08001941 assert A.data['status'] == 'MERGED'
1942 assert A.reported == 2
1943 assert B.data['status'] == 'NEW'
1944 assert B.reported == 2
1945 assert C.data['status'] == 'NEW'
1946 assert C.reported == 2
1947 assert D.data['status'] == 'MERGED'
1948 assert D.reported == 2
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001949 assert len(self.history) == 9 # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001950 self.assertEmptyQueues()
1951
1952 def test_new_patchset_dequeues_old_on_head(self):
1953 "Test that a new patchset causes the old to be dequeued (at head)"
1954 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001955 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001956 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1957 M.setMerged()
1958 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1959 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1960 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1961 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1962 A.addApproval('CRVW', 2)
1963 B.addApproval('CRVW', 2)
1964 C.addApproval('CRVW', 2)
1965 D.addApproval('CRVW', 2)
1966
1967 C.setDependsOn(B, 1)
1968 B.setDependsOn(A, 1)
1969 A.setDependsOn(M, 1)
1970
1971 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1972 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1973 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1974 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1975 self.waitUntilSettled()
1976
1977 A.addPatchset()
1978 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1979 self.waitUntilSettled()
1980
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001981 self.worker.hold_jobs_in_build = False
1982 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001983 self.waitUntilSettled()
1984
James E. Blair2fa50962013-01-30 21:50:41 -08001985 assert A.data['status'] == 'NEW'
1986 assert A.reported == 2
1987 assert B.data['status'] == 'NEW'
1988 assert B.reported == 2
1989 assert C.data['status'] == 'NEW'
1990 assert C.reported == 2
1991 assert D.data['status'] == 'MERGED'
1992 assert D.reported == 2
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001993 assert len(self.history) == 7
James E. Blair2fa50962013-01-30 21:50:41 -08001994 self.assertEmptyQueues()
1995
1996 def test_new_patchset_dequeues_old_without_dependents(self):
1997 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001998 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001999 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2000 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2001 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2002 A.addApproval('CRVW', 2)
2003 B.addApproval('CRVW', 2)
2004 C.addApproval('CRVW', 2)
2005
2006 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2007 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2008 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2009 self.waitUntilSettled()
2010
2011 B.addPatchset()
2012 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2013 self.waitUntilSettled()
2014
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002015 self.worker.hold_jobs_in_build = False
2016 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002017 self.waitUntilSettled()
2018
James E. Blair2fa50962013-01-30 21:50:41 -08002019 assert A.data['status'] == 'MERGED'
2020 assert A.reported == 2
2021 assert B.data['status'] == 'NEW'
2022 assert B.reported == 2
2023 assert C.data['status'] == 'MERGED'
2024 assert C.reported == 2
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002025 assert len(self.history) == 9
James E. Blair2fa50962013-01-30 21:50:41 -08002026 self.assertEmptyQueues()
2027
2028 def test_new_patchset_dequeues_old_independent_queue(self):
2029 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002030 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002031 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2032 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2033 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2034 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2035 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2036 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2037 self.waitUntilSettled()
2038
2039 B.addPatchset()
2040 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2041 self.waitUntilSettled()
2042
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002043 self.worker.hold_jobs_in_build = False
2044 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002045 self.waitUntilSettled()
2046
James E. Blair2fa50962013-01-30 21:50:41 -08002047 assert A.data['status'] == 'NEW'
2048 assert A.reported == 1
2049 assert B.data['status'] == 'NEW'
2050 assert B.reported == 1
2051 assert C.data['status'] == 'NEW'
2052 assert C.reported == 1
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002053 assert len(self.history) == 10
2054 assert self.countJobResults(self.history, 'ABORTED') == 1
James E. Blair2fa50962013-01-30 21:50:41 -08002055 self.assertEmptyQueues()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002056
2057 def test_zuul_refs(self):
2058 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002059 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002060 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2061 M1.setMerged()
2062 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2063 M2.setMerged()
2064
2065 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2066 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2067 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2068 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2069 A.addApproval('CRVW', 2)
2070 B.addApproval('CRVW', 2)
2071 C.addApproval('CRVW', 2)
2072 D.addApproval('CRVW', 2)
2073 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2074 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2075 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2076 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2077
2078 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002079 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002080 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002081 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002082 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002083 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002084 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002085 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002086 self.waitUntilSettled()
2087
James E. Blair7d0dedc2013-02-21 17:26:09 -08002088 a_zref = b_zref = c_zref = d_zref = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002089 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002090 if x.parameters['ZUUL_CHANGE'] == '3':
2091 a_zref = x.parameters['ZUUL_REF']
2092 if x.parameters['ZUUL_CHANGE'] == '4':
2093 b_zref = x.parameters['ZUUL_REF']
2094 if x.parameters['ZUUL_CHANGE'] == '5':
2095 c_zref = x.parameters['ZUUL_REF']
2096 if x.parameters['ZUUL_CHANGE'] == '6':
2097 d_zref = x.parameters['ZUUL_REF']
2098
2099 # There are... four... refs.
2100 assert a_zref is not None
2101 assert b_zref is not None
2102 assert c_zref is not None
2103 assert d_zref is not None
2104
2105 # And they should all be different
2106 refs = set([a_zref, b_zref, c_zref, d_zref])
2107 assert len(refs) == 4
2108
2109 # a ref should have a, not b, and should not be in project2
2110 assert ref_has_change(a_zref, A)
2111 assert not ref_has_change(a_zref, B)
2112 assert not ref_has_change(a_zref, M2)
2113
2114 # b ref should have a and b, and should not be in project2
2115 assert ref_has_change(b_zref, A)
2116 assert ref_has_change(b_zref, B)
2117 assert not ref_has_change(b_zref, M2)
2118
2119 # c ref should have a and b in 1, c in 2
2120 assert ref_has_change(c_zref, A)
2121 assert ref_has_change(c_zref, B)
2122 assert ref_has_change(c_zref, C)
2123 assert not ref_has_change(c_zref, D)
2124
2125 # d ref should have a and b in 1, c and d in 2
2126 assert ref_has_change(d_zref, A)
2127 assert ref_has_change(d_zref, B)
2128 assert ref_has_change(d_zref, C)
2129 assert ref_has_change(d_zref, D)
2130
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002131 self.worker.hold_jobs_in_build = False
2132 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002133 self.waitUntilSettled()
2134
2135 assert A.data['status'] == 'MERGED'
2136 assert A.reported == 2
2137 assert B.data['status'] == 'MERGED'
2138 assert B.reported == 2
2139 assert C.data['status'] == 'MERGED'
2140 assert C.reported == 2
2141 assert D.data['status'] == 'MERGED'
2142 assert D.reported == 2
2143 self.assertEmptyQueues()
James E. Blair70c71582013-03-06 08:50:50 -08002144
James E. Blair412e5582013-04-22 15:50:12 -07002145 def test_statsd(self):
2146 "Test each of the statsd methods used in the scheduler"
2147 import extras
2148 statsd = extras.try_import('statsd.statsd')
2149 statsd.incr('test-incr')
2150 statsd.timing('test-timing', 3)
2151 statsd.gauge('test-guage', 12)
2152 self.assertReportedStat('test-incr', '1|c')
2153 self.assertReportedStat('test-timing', '3|ms')
2154 self.assertReportedStat('test-guage', '12|g')
2155
James E. Blair70c71582013-03-06 08:50:50 -08002156 def test_file_jobs(self):
2157 "Test that file jobs run only when appropriate"
2158 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2159 A.addPatchset(['pip-requires'])
2160 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2161 A.addApproval('CRVW', 2)
2162 B.addApproval('CRVW', 2)
2163 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2164 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2165 self.waitUntilSettled()
2166
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002167 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002168 if x.name == 'project-testfile']
2169
2170 assert len(testfile_jobs) == 1
2171 assert testfile_jobs[0].changes == '1,2'
2172 assert A.data['status'] == 'MERGED'
2173 assert A.reported == 2
2174 assert B.data['status'] == 'MERGED'
2175 assert B.reported == 2
2176 self.assertEmptyQueues()
James E. Blair3c5e5b52013-04-26 11:17:03 -07002177
2178 def test_test_config(self):
2179 "Test that we can test the config"
2180 sched = zuul.scheduler.Scheduler()
2181 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002182
2183 def test_build_description(self):
2184 "Test that build descriptions update"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002185 self.worker.registerFunction('set_description:' +
2186 self.worker.worker_id)
2187
2188 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2189 A.addApproval('CRVW', 2)
2190 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2191 self.waitUntilSettled()
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002192 desc = self.history[0].description
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002193 self.log.debug("Description: %s" % desc)
2194 assert re.search("Branch.*master", desc)
2195 assert re.search("Pipeline.*gate", desc)
2196 assert re.search("project-merge.*SUCCESS", desc)
2197 assert re.search("project-test1.*SUCCESS", desc)
2198 assert re.search("project-test2.*SUCCESS", desc)
2199 assert re.search("Reported result.*SUCCESS", desc)
2200
2201 def test_node_label(self):
2202 "Test that a job runs on a specific node label"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002203 self.worker.registerFunction('build:node-project-test1:debian')
2204
2205 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2206 A.addApproval('CRVW', 2)
2207 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2208 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002209
2210 assert self.getJobFromHistory('node-project-merge').node is None
2211 assert self.getJobFromHistory('node-project-test1').node == 'debian'
2212 assert self.getJobFromHistory('node-project-test2').node is None