blob: 6babdc02a96399cb2999b9cceea86ce0fabebd7c [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 = {
518 'full_url': 'https://server/job/%s/%s/' % (self.name, self.number),
519 'number': self.number,
520 'master': self.worker.worker_id,
521 }
522
523 self.job.sendWorkData(json.dumps(data))
524 self.job.sendWorkStatus(0, 100)
525
526 if self.worker.hold_jobs_in_build:
527 self._wait()
528 self.log.debug("Build %s continuing" % self.unique)
529
530 self.worker.lock.acquire()
531
532 result = 'SUCCESS'
533 if (('ZUUL_REF' in self.parameters) and
534 self.worker.shouldFailTest(self.name,
535 self.parameters['ZUUL_REF'])):
536 result = 'FAILURE'
537 if self.aborted:
538 result = 'ABORTED'
539
540 data = {'result': result}
541 changes = None
542 if 'ZUUL_CHANGE_IDS' in self.parameters:
543 changes = self.parameters['ZUUL_CHANGE_IDS']
544
545 self.worker.build_history.append(
546 BuildHistory(name=self.name, number=self.number,
547 result=result, changes=changes, node=self.node,
548 uuid=self.unique, description=self.description)
549 )
550
551 self.job.sendWorkComplete(json.dumps(data))
552 del self.worker.gearman_jobs[self.job.unique]
553 self.worker.running_builds.remove(self)
554 self.worker.lock.release()
555
556
557class FakeWorker(gear.Worker):
558 def __init__(self, worker_id):
559 super(FakeWorker, self).__init__(worker_id)
560 self.gearman_jobs = {}
561 self.build_history = []
562 self.running_builds = []
563 self.build_counter = 0
564 self.fail_tests = {}
565
566 self.hold_jobs_in_build = False
567 self.lock = threading.Lock()
568 self.__work_thread = threading.Thread(target=self.work)
569 self.__work_thread.start()
570
571 def handleJob(self, job):
572 parts = job.name.split(":")
573 cmd = parts[0]
574 name = parts[1]
575 if len(parts) > 2:
576 node = parts[2]
577 else:
578 node = None
579 if cmd == 'build':
580 self.handleBuild(job, name, node)
581 elif cmd == 'stop':
582 self.handleStop(job, name)
583 elif cmd == 'set_description':
584 self.handleSetDescription(job, name)
585
586 def handleBuild(self, job, name, node):
587 build = FakeBuild(self, job, self.build_counter, node)
588 job.build = build
589 self.gearman_jobs[job.unique] = job
590 self.build_counter += 1
591
592 self.running_builds.append(build)
593 build.start()
594
595 def handleStop(self, job, name):
596 self.log.debug("handle stop")
597 unique = job.arguments
598 for build in self.running_builds:
599 if build.unique == unique:
600 build.aborted = True
601 build.release()
602 job.sendWorkComplete()
603 return
604 job.sendWorkFail()
605
606 def handleSetDescription(self, job, name):
607 self.log.debug("handle set description")
608 parameters = json.loads(job.arguments)
609 unique = parameters['unique_id']
610 descr = parameters['html_description']
611 for build in self.running_builds:
612 if build.unique == unique:
613 build.description = descr
614 job.sendWorkComplete()
615 return
616 for build in self.build_history:
617 if build.uuid == unique:
618 build.description = descr
619 job.sendWorkComplete()
620 return
621 job.sendWorkFail()
622
623 def work(self):
624 while self.running:
625 try:
626 job = self.getJob()
627 except gear.InterruptedError:
628 continue
629 try:
630 self.handleJob(job)
631 except:
632 self.log.exception("Worker exception:")
633
634 def addFailTest(self, name, change):
635 l = self.fail_tests.get(name, [])
636 l.append(change)
637 self.fail_tests[name] = l
638
639 def shouldFailTest(self, name, ref):
640 l = self.fail_tests.get(name, [])
641 for change in l:
642 if ref_has_change(ref, change):
643 return True
644 return False
645
646 def release(self, regex=None):
647 builds = self.running_builds[:]
648 self.log.debug("releasing build %s (%s)" % (regex,
649 len(self.running_builds)))
650 for build in builds:
651 if not regex or re.match(regex, build.name):
652 self.log.debug("releasing build %s" %
653 (build.parameters['ZUUL_UUID']))
654 build.release()
655 else:
656 self.log.debug("not releasing build %s" %
657 (build.parameters['ZUUL_UUID']))
658 self.log.debug("done releasing builds %s (%s)" %
659 (regex, len(self.running_builds)))
660
661
662class FakeGearmanServer(gear.Server):
663 def __init__(self):
664 self.hold_jobs_in_queue = False
665 super(FakeGearmanServer, self).__init__(0)
666
667 def getJobForConnection(self, connection, peek=False):
668 for job in self.queue:
669 if not hasattr(job, 'waiting'):
670 if job.name.startswith('build:'):
671 job.waiting = self.hold_jobs_in_queue
672 else:
673 job.waiting = False
674 if job.waiting:
675 continue
676 if job.name in connection.functions:
677 if not peek:
678 self.queue.remove(job)
679 return job
680 return None
681
682 def release(self, regex=None):
683 released = False
684 queue = self.queue[:]
685 self.log.debug("releasing queued job %s (%s)" % (regex,
686 len(self.queue)))
687 for job in queue:
688 cmd, name = job.name.split(':')
689 if cmd != 'build':
690 continue
691 if not regex or re.match(regex, name):
692 self.log.debug("releasing queued job %s" %
693 job.unique)
694 job.waiting = False
695 released = True
696 else:
697 self.log.debug("not releasing queued job %s" %
698 job.unique)
699 if released:
700 self.wakeConnections()
701 self.log.debug("done releasing queued jobs %s (%s)" %
702 (regex, len(self.queue)))
703
704
James E. Blairb0fcae42012-07-17 11:12:10 -0700705class testScheduler(unittest.TestCase):
706 log = logging.getLogger("zuul.test")
707
708 def setUp(self):
James E. Blair1dbd5082012-08-23 15:12:15 -0700709 if os.path.exists(TEST_ROOT):
710 shutil.rmtree(TEST_ROOT)
711 os.makedirs(TEST_ROOT)
712 os.makedirs(UPSTREAM_ROOT)
713 os.makedirs(GIT_ROOT)
James E. Blair4886cc12012-07-18 15:39:41 -0700714
715 # For each project in config:
716 init_repo("org/project")
717 init_repo("org/project1")
718 init_repo("org/project2")
James E. Blair127bc182012-08-28 15:55:15 -0700719 init_repo("org/project3")
James E. Blair7f71c802012-08-22 13:04:32 -0700720 init_repo("org/one-job-project")
James E. Blair4ec821f2012-08-23 15:28:28 -0700721 init_repo("org/nonvoting-project")
Antoine Musso80edd5a2013-02-13 15:37:53 +0100722 init_repo("org/templated-project")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700723 init_repo("org/node-project")
James E. Blair412e5582013-04-22 15:50:12 -0700724
725 self.statsd = FakeStatsd()
726 os.environ['STATSD_HOST'] = 'localhost'
727 os.environ['STATSD_PORT'] = str(self.statsd.port)
728 self.statsd.start()
729 # the statsd client object is configured in the statsd module import
730 reload(statsd)
731 reload(zuul.scheduler)
732
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700733 self.gearman_server = FakeGearmanServer()
734
735 self.config = ConfigParser.ConfigParser()
736 cfg = StringIO()
737 CONFIG.write(cfg)
738 cfg.seek(0)
739 self.config.readfp(cfg)
740 self.config.set('gearman', 'port', str(self.gearman_server.port))
741
742 self.worker = FakeWorker('fake_worker')
743 self.worker.addServer('127.0.0.1', self.gearman_server.port)
744 self.gearman_server.worker = self.worker
745
James E. Blairb0fcae42012-07-17 11:12:10 -0700746 self.sched = zuul.scheduler.Scheduler()
747
James E. Blair8cc15a82012-08-01 11:17:57 -0700748 def URLOpenerFactory(*args, **kw):
749 args = [self.fake_gerrit] + list(args)
750 return FakeURLOpener(*args, **kw)
751
James E. Blair8cc15a82012-08-01 11:17:57 -0700752 urllib2.urlopen = URLOpenerFactory
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700753 self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched)
James E. Blairb0fcae42012-07-17 11:12:10 -0700754
755 zuul.lib.gerrit.Gerrit = FakeGerrit
756
James E. Blair4886cc12012-07-18 15:39:41 -0700757 self.gerrit = FakeGerritTrigger(self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700758 self.gerrit.replication_timeout = 1.5
759 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700760 self.fake_gerrit = self.gerrit.gerrit
761
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700762 self.sched.setLauncher(self.launcher)
James E. Blairb0fcae42012-07-17 11:12:10 -0700763 self.sched.setTrigger(self.gerrit)
764
765 self.sched.start()
766 self.sched.reconfigure(self.config)
767 self.sched.resume()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700768 self.launcher.gearman.waitForServer()
769 self.registerJobs()
James E. Blairb0fcae42012-07-17 11:12:10 -0700770
771 def tearDown(self):
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700772 self.launcher.stop()
773 self.worker.shutdown()
774 self.gearman_server.shutdown()
James E. Blairb0fcae42012-07-17 11:12:10 -0700775 self.gerrit.stop()
776 self.sched.stop()
777 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700778 self.statsd.stop()
779 self.statsd.join()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700780 threads = threading.enumerate()
781 if len(threads) > 1:
782 self.log.error("More than one thread is running: %s" % threads)
James E. Blair1dbd5082012-08-23 15:12:15 -0700783 #shutil.rmtree(TEST_ROOT)
James E. Blairb0fcae42012-07-17 11:12:10 -0700784
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700785 def registerJobs(self):
786 count = 0
787 for job in self.sched.jobs.keys():
788 self.worker.registerFunction('build:' + job)
789 count += 1
790 self.worker.registerFunction('stop:' + self.worker.worker_id)
791 count += 1
792
793 while len(self.gearman_server.functions) < count:
794 time.sleep(0)
795
796 def release(self, job):
797 if isinstance(job, FakeBuild):
798 job.release()
799 else:
800 job.waiting = False
801 self.log.debug("Queued job %s released" % job.unique)
802 self.gearman_server.wakeConnections()
803
804 def getParameter(self, job, name):
805 if isinstance(job, FakeBuild):
806 return job.parameters[name]
807 else:
808 parameters = json.loads(job.arguments)
809 return parameters[name]
810
811 def resetGearmanServer(self):
812 self.worker.setFunctions([])
813 while True:
814 done = True
815 for connection in self.gearman_server.active_connections:
816 if connection.functions:
817 done = False
818 if done:
819 break
820 time.sleep(0)
821 self.gearman_server.functions = set()
822
823 def haveAllBuildsReported(self):
824 # See if Zuul is waiting on a meta job to complete
825 if self.launcher.meta_jobs:
826 return False
827 # Find out if every build that the worker has completed has been
828 # reported back to Zuul. If it hasn't then that means a Gearman
829 # event is still in transit and the system is not stable.
830 for build in self.worker.build_history:
831 zbuild = self.launcher.builds.get(build.uuid)
832 if not zbuild:
833 # It has already been reported
834 continue
835 # It hasn't been reported yet.
836 return False
837 # Make sure that none of the worker connections are in GRAB_WAIT
838 for connection in self.worker.active_connections:
839 if connection.state == 'GRAB_WAIT':
840 return False
841 return True
842
843 def areAllBuildsWaiting(self):
844 ret = True
845
846 builds = self.launcher.builds.values()
847 for build in builds:
848 client_job = None
849 for conn in self.launcher.gearman.active_connections:
850 for j in conn.related_jobs.values():
851 if j.unique == build.uuid:
852 client_job = j
853 break
854 if not client_job:
855 self.log.debug("%s is not known to the gearman client" %
856 build)
857 ret = False
858 continue
859 if not client_job.handle:
860 self.log.debug("%s has no handle" % client_job)
861 ret = False
862 continue
863 server_job = self.gearman_server.jobs.get(client_job.handle)
864 if not server_job:
865 self.log.debug("%s is not known to the gearman server" %
866 client_job)
867 ret = False
868 continue
869 if not hasattr(server_job, 'waiting'):
870 self.log.debug("%s is being enqueued" % server_job)
871 ret = False
872 continue
873 if server_job.waiting:
874 continue
875 worker_job = self.worker.gearman_jobs.get(server_job.unique)
876 if worker_job:
877 if worker_job.build.isWaiting():
878 continue
879 else:
880 self.log.debug("%s is running" % worker_job)
881 ret = False
882 else:
883 self.log.debug("%s is unassigned" % server_job)
884 ret = False
885 return ret
886
James E. Blairb0fcae42012-07-17 11:12:10 -0700887 def waitUntilSettled(self):
888 self.log.debug("Waiting until settled...")
889 start = time.time()
890 while True:
891 if time.time() - start > 10:
892 print 'queue status:',
893 print self.sched.trigger_event_queue.empty(),
894 print self.sched.result_event_queue.empty(),
895 print self.fake_gerrit.event_queue.empty(),
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700896 print self.areAllBuildsWaiting()
James E. Blairb0fcae42012-07-17 11:12:10 -0700897 raise Exception("Timeout waiting for Zuul to settle")
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700898 # Make sure no new events show up while we're checking
899 self.worker.lock.acquire()
900 # have all build states propogated to zuul?
901 if self.haveAllBuildsReported():
902 # Join ensures that the queue is empty _and_ events have been
903 # processed
904 self.fake_gerrit.event_queue.join()
905 self.sched.trigger_event_queue.join()
906 self.sched.result_event_queue.join()
907 if (self.sched.trigger_event_queue.empty() and
908 self.sched.result_event_queue.empty() and
909 self.fake_gerrit.event_queue.empty() and
910 self.areAllBuildsWaiting()):
911 self.worker.lock.release()
912 self.log.debug("...settled.")
913 return
914 self.worker.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700915 self.sched.wake_event.wait(0.1)
916
James E. Blaird466dc42012-07-31 10:42:56 -0700917 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -0800918 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -0700919 return len(jobs)
920
James E. Blair4ca985f2013-05-30 12:27:43 -0700921 def getJobFromHistory(self, name):
922 history = self.worker.build_history
923 for job in history:
924 if job.name == name:
925 return job
926 raise Exception("Unable to find job %s in history" % name)
927
James E. Blaire0487072012-08-29 17:38:31 -0700928 def assertEmptyQueues(self):
929 # Make sure there are no orphaned jobs
930 for pipeline in self.sched.pipelines.values():
931 for queue in pipeline.queues:
932 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -0800933 print 'pipeline %s queue %s contents %s' % (
934 pipeline.name, queue.name, queue.queue)
James E. Blaire0487072012-08-29 17:38:31 -0700935 assert len(queue.queue) == 0
936 if len(queue.severed_heads) != 0:
937 print 'heads', queue.severed_heads
938 assert len(queue.severed_heads) == 0
939
James E. Blair412e5582013-04-22 15:50:12 -0700940 def assertReportedStat(self, key, value=None):
941 start = time.time()
942 while time.time() < (start + 5):
943 for stat in self.statsd.stats:
944 k, v = stat.split(':')
945 if key == k:
946 if value is None:
947 return
948 if value == v:
949 return
950 time.sleep(0.1)
951
952 pprint.pprint(self.statsd.stats)
953 raise Exception("Key %s not found in reported stats" % key)
954
James E. Blairb0fcae42012-07-17 11:12:10 -0700955 def test_jobs_launched(self):
956 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700957 builds = self.worker.running_builds
958 history = self.worker.build_history
959
James E. Blairb0fcae42012-07-17 11:12:10 -0700960 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -0700961 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700962 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
963 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -0700964 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
965 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
966 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blairb0fcae42012-07-17 11:12:10 -0700967 assert A.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700968 assert A.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700969 self.assertEmptyQueues()
James E. Blairb0fcae42012-07-17 11:12:10 -0700970
James E. Blair412e5582013-04-22 15:50:12 -0700971 self.assertReportedStat('gerrit.event.comment-added', '1|c')
972 self.assertReportedStat('zuul.pipeline.gate.current_changes', '1|g')
973 self.assertReportedStat('zuul.job.project-merge')
974 self.assertReportedStat('zuul.pipeline.gate.resident_time')
975 self.assertReportedStat('zuul.pipeline.gate.total_changes', '1|c')
976 self.assertReportedStat(
977 'zuul.pipeline.gate.org.project.resident_time')
978 self.assertReportedStat(
979 'zuul.pipeline.gate.org.project.total_changes', '1|c')
980
James E. Blair42f74822013-05-14 15:18:03 -0700981 def test_duplicate_pipelines(self):
982 "Test that a change matching multiple pipelines works"
James E. Blair1b4d9722013-05-21 10:32:04 -0700983 builds = self.worker.running_builds
984 history = self.worker.build_history
985
James E. Blair42f74822013-05-14 15:18:03 -0700986 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
987 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
988 self.waitUntilSettled()
James E. Blair42f74822013-05-14 15:18:03 -0700989
James E. Blair1b4d9722013-05-21 10:32:04 -0700990 print builds
James E. Blair42f74822013-05-14 15:18:03 -0700991 print A.messages
992
993 self.assertEmptyQueues()
994
James E. Blair1b4d9722013-05-21 10:32:04 -0700995 assert len(history) == 2
996 history[0].name == 'project-test1'
997 history[1].name == 'project-test1'
James E. Blair42f74822013-05-14 15:18:03 -0700998
999 assert len(A.messages) == 2
1000 if 'dup1/project-test1' in A.messages[0]:
1001 assert 'dup1/project-test1' in A.messages[0]
1002 assert 'dup2/project-test1' not in A.messages[0]
1003 assert 'dup1/project-test1' not in A.messages[1]
1004 assert 'dup2/project-test1' in A.messages[1]
1005 else:
1006 assert 'dup1/project-test1' in A.messages[1]
1007 assert 'dup2/project-test1' not in A.messages[1]
1008 assert 'dup1/project-test1' not in A.messages[0]
1009 assert 'dup2/project-test1' in A.messages[0]
1010
James E. Blairb0fcae42012-07-17 11:12:10 -07001011 def test_parallel_changes(self):
1012 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001013 builds = self.worker.running_builds
1014 history = self.worker.build_history
1015
1016 self.worker.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -07001017 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1018 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1019 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001020 A.addApproval('CRVW', 2)
1021 B.addApproval('CRVW', 2)
1022 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -07001023
1024 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1025 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1026 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1027
1028 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001029 assert len(builds) == 1
1030 assert builds[0].name == 'project-merge'
1031 assert job_has_changes(builds[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -07001032
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001033 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001034 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001035 assert len(builds) == 3
1036 assert builds[0].name == 'project-test1'
1037 assert job_has_changes(builds[0], A)
1038 assert builds[1].name == 'project-test2'
1039 assert job_has_changes(builds[1], A)
1040 assert builds[2].name == 'project-merge'
1041 assert job_has_changes(builds[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -07001042
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001043 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001044 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001045 assert len(builds) == 5
1046 assert builds[0].name == 'project-test1'
1047 assert job_has_changes(builds[0], A)
1048 assert builds[1].name == 'project-test2'
1049 assert job_has_changes(builds[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -07001050
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001051 assert builds[2].name == 'project-test1'
1052 assert job_has_changes(builds[2], A, B)
1053 assert builds[3].name == 'project-test2'
1054 assert job_has_changes(builds[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -07001055
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001056 assert builds[4].name == 'project-merge'
1057 assert job_has_changes(builds[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -07001058
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001059 self.worker.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -07001060 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001061 assert len(builds) == 6
1062 assert builds[0].name == 'project-test1'
1063 assert job_has_changes(builds[0], A)
1064 assert builds[1].name == 'project-test2'
1065 assert job_has_changes(builds[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -07001066
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001067 assert builds[2].name == 'project-test1'
1068 assert job_has_changes(builds[2], A, B)
1069 assert builds[3].name == 'project-test2'
1070 assert job_has_changes(builds[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -07001071
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001072 assert builds[4].name == 'project-test1'
1073 assert job_has_changes(builds[4], A, B, C)
1074 assert builds[5].name == 'project-test2'
1075 assert job_has_changes(builds[5], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -07001076
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001077 self.worker.hold_jobs_in_build = False
1078 self.worker.release()
James E. Blairb0fcae42012-07-17 11:12:10 -07001079 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001080 assert len(builds) == 0
James E. Blairb0fcae42012-07-17 11:12:10 -07001081
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001082 assert len(history) == 9
James E. Blairb0fcae42012-07-17 11:12:10 -07001083 assert A.data['status'] == 'MERGED'
1084 assert B.data['status'] == 'MERGED'
1085 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -07001086 assert A.reported == 2
1087 assert B.reported == 2
1088 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001089 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001090
1091 def test_failed_changes(self):
1092 "Test that a change behind a failed change is retested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001093 builds = self.worker.running_builds
1094 history = self.worker.build_history
1095
James E. Blairb02a3bb2012-07-30 17:49:55 -07001096 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1097 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -07001098 A.addApproval('CRVW', 2)
1099 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001100
1101 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1102 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1103
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001104 self.worker.addFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -07001105
1106 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001107 assert len(history) > 6
James E. Blairb02a3bb2012-07-30 17:49:55 -07001108 assert A.data['status'] == 'NEW'
1109 assert B.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -07001110 assert A.reported == 2
1111 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001112 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -07001113
1114 def test_independent_queues(self):
1115 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001116 builds = self.worker.running_builds
1117 history = self.worker.build_history
1118
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
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001134 assert len(builds) == 2
1135 assert builds[0].name == 'project-merge'
1136 assert job_has_changes(builds[0], A)
1137 assert builds[1].name == 'project1-merge'
1138 assert job_has_changes(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
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001149 assert len(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()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001153 assert len(builds) == 0
James E. Blairb02a3bb2012-07-30 17:49:55 -07001154
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001155 assert len(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. Blair1f4c2bb2013-04-26 08:40:46 -07001166 builds = self.worker.running_builds
1167 history = self.worker.build_history
James E. Blaird466dc42012-07-31 10:42:56 -07001168
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001169 self.worker.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -07001170 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1171 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1172 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001173 A.addApproval('CRVW', 2)
1174 B.addApproval('CRVW', 2)
1175 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001176
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001177 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001178
1179 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1180 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1181 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1182
1183 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -07001184
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001185 assert len(builds) == 1
1186 assert builds[0].name == 'project-merge'
1187 assert job_has_changes(builds[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -07001188
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()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001193 self.worker.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001194 self.waitUntilSettled()
1195
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001196 assert len(builds) == 6
1197 assert builds[0].name == 'project-test1'
1198 assert builds[1].name == 'project-test2'
1199 assert builds[2].name == 'project-test1'
1200 assert builds[3].name == 'project-test2'
1201 assert builds[4].name == 'project-test1'
1202 assert builds[5].name == 'project-test2'
James E. Blaird466dc42012-07-31 10:42:56 -07001203
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001204 self.release(builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001205 self.waitUntilSettled()
1206
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001207 assert len(builds) == 2 # project-test2, project-merge for B
1208 assert self.countJobResults(history, 'ABORTED') == 4
James E. Blaird466dc42012-07-31 10:42:56 -07001209
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001210 self.worker.hold_jobs_in_build = False
1211 self.worker.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001212 self.waitUntilSettled()
1213
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001214 assert len(builds) == 0
1215 assert len(history) == 15
James E. Blaird466dc42012-07-31 10:42:56 -07001216 assert A.data['status'] == 'NEW'
1217 assert B.data['status'] == 'MERGED'
1218 assert C.data['status'] == 'MERGED'
1219 assert A.reported == 2
1220 assert B.reported == 2
1221 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001222 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -07001223
1224 def test_failed_change_at_head_with_queue(self):
1225 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001226 builds = self.worker.running_builds
1227 history = self.worker.build_history
1228 queue = self.gearman_server.queue
James E. Blaird466dc42012-07-31 10:42:56 -07001229
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001230 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -07001231 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1232 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1233 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001234 A.addApproval('CRVW', 2)
1235 B.addApproval('CRVW', 2)
1236 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001237
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001238 self.worker.addFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001239
1240 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1241 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1242 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1243
1244 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001245 assert len(builds) == 0
James E. Blaird466dc42012-07-31 10:42:56 -07001246 assert len(queue) == 1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001247 assert queue[0].name == 'build:project-merge'
1248 assert job_has_changes(queue[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -07001249
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. Blair1f4c2bb2013-04-26 08:40:46 -07001252 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001253 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001254 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -07001255 self.waitUntilSettled()
1256
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001257 assert len(builds) == 0
James E. Blaird466dc42012-07-31 10:42:56 -07001258 assert len(queue) == 6
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001259 assert queue[0].name == 'build:project-test1'
1260 assert queue[1].name == 'build:project-test2'
1261 assert queue[2].name == 'build:project-test1'
1262 assert queue[3].name == 'build:project-test2'
1263 assert queue[4].name == 'build:project-test1'
1264 assert queue[5].name == 'build:project-test2'
James E. Blaird466dc42012-07-31 10:42:56 -07001265
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001266 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -07001267 self.waitUntilSettled()
1268
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001269 assert len(builds) == 0
1270 assert len(queue) == 2 # project-test2, project-merge for B
1271 assert self.countJobResults(history, 'ABORTED') == 0
James E. Blaird466dc42012-07-31 10:42:56 -07001272
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001273 self.gearman_server.hold_jobs_in_queue = False
1274 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -07001275 self.waitUntilSettled()
1276
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001277 assert len(builds) == 0
1278 assert len(history) == 11
James E. Blaird466dc42012-07-31 10:42:56 -07001279 assert A.data['status'] == 'NEW'
1280 assert B.data['status'] == 'MERGED'
1281 assert C.data['status'] == 'MERGED'
1282 assert A.reported == 2
1283 assert B.reported == 2
1284 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001285 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001286
1287 def test_patch_order(self):
1288 "Test that dependent patches are tested in the right order"
1289 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1290 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1291 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1292 A.addApproval('CRVW', 2)
1293 B.addApproval('CRVW', 2)
1294 C.addApproval('CRVW', 2)
1295
1296 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1297 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1298 M2.setMerged()
1299 M1.setMerged()
1300
1301 # C -> B -> A -> M1 -> M2
1302 # M2 is here to make sure it is never queried. If it is, it
1303 # means zuul is walking down the entire history of merged
1304 # changes.
1305
1306 C.setDependsOn(B, 1)
1307 B.setDependsOn(A, 1)
1308 A.setDependsOn(M1, 1)
1309 M1.setDependsOn(M2, 1)
1310
1311 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1312
1313 self.waitUntilSettled()
1314
1315 assert A.data['status'] == 'NEW'
1316 assert B.data['status'] == 'NEW'
1317 assert C.data['status'] == 'NEW'
1318
1319 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1320 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1321
1322 self.waitUntilSettled()
1323 assert M2.queried == 0
1324 assert A.data['status'] == 'MERGED'
1325 assert B.data['status'] == 'MERGED'
1326 assert C.data['status'] == 'MERGED'
1327 assert A.reported == 2
1328 assert B.reported == 2
1329 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001330 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001331
1332 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001333 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001334 # TODO: move to test_gerrit (this is a unit test!)
1335 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4aea70c2012-07-26 14:23:24 -07001336 a = self.sched.trigger.getChange(1, 2)
1337 mgr = self.sched.pipelines['gate'].manager
1338 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001339
1340 A.addApproval('CRVW', 2)
James E. Blair4aea70c2012-07-26 14:23:24 -07001341 a = self.sched.trigger.getChange(1, 2)
1342 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001343
1344 A.addApproval('APRV', 1)
James E. Blair4aea70c2012-07-26 14:23:24 -07001345 a = self.sched.trigger.getChange(1, 2)
1346 assert self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blaire0487072012-08-29 17:38:31 -07001347 self.assertEmptyQueues()
James E. Blair4886cc12012-07-18 15:39:41 -07001348
1349 def test_build_configuration(self):
1350 "Test that zuul merges the right commits for testing"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001351 builds = self.worker.running_builds
1352 history = self.worker.build_history
1353 queue = self.gearman_server.queue
1354
1355 self.gearman_server.hold_jobs_in_queue = True
James E. Blair4886cc12012-07-18 15:39:41 -07001356 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1357 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1358 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1359 A.addApproval('CRVW', 2)
1360 B.addApproval('CRVW', 2)
1361 C.addApproval('CRVW', 2)
1362 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1363 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1364 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1365 self.waitUntilSettled()
1366
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001367 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001368 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001369 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001370 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001371 self.gearman_server.release('.*-merge')
James E. Blair4886cc12012-07-18 15:39:41 -07001372 self.waitUntilSettled()
James E. Blair1dbd5082012-08-23 15:12:15 -07001373
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001374 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1375 self.gearman_server.hold_jobs_in_queue = False
1376 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001377 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001378
James E. Blair1dbd5082012-08-23 15:12:15 -07001379 path = os.path.join(GIT_ROOT, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001380 repo = git.Repo(path)
1381 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1382 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001383 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
1384 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001385 self.assertEmptyQueues()
James E. Blair973721f2012-08-15 10:19:43 -07001386
1387 def test_build_configuration_conflict(self):
1388 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001389 builds = self.worker.running_builds
1390 history = self.worker.build_history
1391 queue = self.gearman_server.queue
1392
1393 self.gearman_server.hold_jobs_in_queue = True
James E. Blair973721f2012-08-15 10:19:43 -07001394 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1395 A.addPatchset(['conflict'])
1396 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1397 B.addPatchset(['conflict'])
1398 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1399 A.addApproval('CRVW', 2)
1400 B.addApproval('CRVW', 2)
1401 C.addApproval('CRVW', 2)
1402 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1403 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1404 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1405 self.waitUntilSettled()
1406
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001407 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001408 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001409 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001410 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001411 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001412 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001413 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1414 self.gearman_server.hold_jobs_in_queue = False
1415 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001416 self.waitUntilSettled()
1417
1418 assert A.data['status'] == 'MERGED'
1419 assert B.data['status'] == 'NEW'
1420 assert C.data['status'] == 'MERGED'
1421 assert A.reported == 2
1422 assert B.reported == 2
1423 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001424 self.assertEmptyQueues()
James E. Blairdaabed22012-08-15 15:38:57 -07001425
1426 def test_post(self):
1427 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001428 builds = self.worker.running_builds
1429 history = self.worker.build_history
1430
Zhongyue Luo5d556072012-09-21 02:00:47 +09001431 e = {
1432 "type": "ref-updated",
1433 "submitter": {
1434 "name": "User Name",
1435 },
1436 "refUpdate": {
1437 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1438 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1439 "refName": "master",
1440 "project": "org/project",
1441 }
1442 }
James E. Blairdaabed22012-08-15 15:38:57 -07001443 self.fake_gerrit.addEvent(e)
1444 self.waitUntilSettled()
1445
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001446 job_names = [x.name for x in history]
1447 assert len(history) == 1
James E. Blairdaabed22012-08-15 15:38:57 -07001448 assert 'project-post' in job_names
James E. Blaire0487072012-08-29 17:38:31 -07001449 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001450
1451 def test_build_configuration_branch(self):
1452 "Test that the right commits are on alternate branches"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001453 builds = self.worker.running_builds
1454 history = self.worker.build_history
1455 queue = self.gearman_server.queue
1456
1457 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001458 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1459 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1460 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1461 A.addApproval('CRVW', 2)
1462 B.addApproval('CRVW', 2)
1463 C.addApproval('CRVW', 2)
1464 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1465 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1466 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1467 self.waitUntilSettled()
1468
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001469 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001470 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001471 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001472 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001473 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001474 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001475 ref = self.getParameter(queue[-1], 'ZUUL_REF')
1476 self.gearman_server.hold_jobs_in_queue = False
1477 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001478 self.waitUntilSettled()
1479
James E. Blair1dbd5082012-08-23 15:12:15 -07001480 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001481 repo = git.Repo(path)
1482 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1483 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001484 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
1485 assert repo_messages == correct_messages
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_branch_interaction(self):
1489 "Test that switching between branches works"
1490 self.test_build_configuration()
1491 self.test_build_configuration_branch()
1492 # C has been merged, undo that
James E. Blair1dbd5082012-08-23 15:12:15 -07001493 path = os.path.join(UPSTREAM_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001494 repo = git.Repo(path)
1495 repo.heads.master.commit = repo.commit('init')
1496 self.test_build_configuration()
James E. Blaire0487072012-08-29 17:38:31 -07001497 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001498
1499 def test_build_configuration_multi_branch(self):
1500 "Test that dependent changes on multiple branches are merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001501 builds = self.worker.running_builds
1502 history = self.worker.build_history
1503 queue = self.gearman_server.queue
1504
1505 self.gearman_server.hold_jobs_in_queue = True
James E. Blairc6294a52012-08-17 10:19:48 -07001506 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1507 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1508 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1509 A.addApproval('CRVW', 2)
1510 B.addApproval('CRVW', 2)
1511 C.addApproval('CRVW', 2)
1512 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1513 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1514 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1515 self.waitUntilSettled()
1516
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001517 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001518 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001519 ref_mp = self.getParameter(queue[-1], 'ZUUL_REF')
1520 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001521 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001522 self.gearman_server.release('.*-merge')
James E. Blairc6294a52012-08-17 10:19:48 -07001523 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001524 ref_master = self.getParameter(queue[-1], 'ZUUL_REF')
1525 self.gearman_server.hold_jobs_in_queue = False
1526 self.gearman_server.release()
James E. Blairc6294a52012-08-17 10:19:48 -07001527 self.waitUntilSettled()
1528
James E. Blair1dbd5082012-08-23 15:12:15 -07001529 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001530 repo = git.Repo(path)
1531
1532 repo_messages = [c.message.strip()
1533 for c in repo.iter_commits(ref_master)]
1534 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001535 correct_messages = ['initial commit', 'A-1', 'C-1']
1536 assert repo_messages == correct_messages
1537
1538 repo_messages = [c.message.strip()
1539 for c in repo.iter_commits(ref_mp)]
1540 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001541 correct_messages = ['initial commit', 'mp commit', 'B-1']
1542 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001543 self.assertEmptyQueues()
James E. Blair7f71c802012-08-22 13:04:32 -07001544
1545 def test_one_job_project(self):
1546 "Test that queueing works with one job"
1547 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1548 'master', 'A')
1549 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1550 'master', 'B')
1551 A.addApproval('CRVW', 2)
1552 B.addApproval('CRVW', 2)
1553 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1554 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1555 self.waitUntilSettled()
1556
James E. Blair7f71c802012-08-22 13:04:32 -07001557 assert A.data['status'] == 'MERGED'
1558 assert A.reported == 2
1559 assert B.data['status'] == 'MERGED'
1560 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001561 self.assertEmptyQueues()
James E. Blaircaec0c52012-08-22 14:52:22 -07001562
Antoine Musso80edd5a2013-02-13 15:37:53 +01001563 def test_job_from_templates_launched(self):
1564 "Test whether a job generated via a template can be launched"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001565 builds = self.worker.running_builds
1566 history = self.worker.build_history
1567
Antoine Musso80edd5a2013-02-13 15:37:53 +01001568 A = self.fake_gerrit.addFakeChange(
1569 'org/templated-project', 'master', 'A')
1570 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1571 self.waitUntilSettled()
Antoine Musso80edd5a2013-02-13 15:37:53 +01001572
James E. Blair4ca985f2013-05-30 12:27:43 -07001573 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1574 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
Antoine Musso80edd5a2013-02-13 15:37:53 +01001575
James E. Blaircaec0c52012-08-22 14:52:22 -07001576 def test_dependent_changes_dequeue(self):
1577 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001578 builds = self.worker.running_builds
1579 history = self.worker.build_history
1580
James E. Blaircaec0c52012-08-22 14:52:22 -07001581 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1582 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1583 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1584 A.addApproval('CRVW', 2)
1585 B.addApproval('CRVW', 2)
1586 C.addApproval('CRVW', 2)
1587
1588 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1589 M1.setMerged()
1590
1591 # C -> B -> A -> M1
1592
1593 C.setDependsOn(B, 1)
1594 B.setDependsOn(A, 1)
1595 A.setDependsOn(M1, 1)
1596
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001597 self.worker.addFailTest('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001598
1599 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1600 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1601 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1602
1603 self.waitUntilSettled()
1604
James E. Blairec590122012-08-22 15:19:31 -07001605 assert A.data['status'] == 'NEW'
1606 assert A.reported == 2
1607 assert B.data['status'] == 'NEW'
1608 assert B.reported == 2
1609 assert C.data['status'] == 'NEW'
1610 assert C.reported == 2
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001611 assert len(history) == 1
James E. Blaire0487072012-08-29 17:38:31 -07001612 self.assertEmptyQueues()
James E. Blairec590122012-08-22 15:19:31 -07001613
1614 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001615 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001616 # If it's dequeued more than once, we should see extra
1617 # aborted jobs.
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001618 builds = self.worker.running_builds
1619 history = self.worker.build_history
James E. Blairec590122012-08-22 15:19:31 -07001620
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001621 self.worker.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001622 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1623 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1624 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1625 A.addApproval('CRVW', 2)
1626 B.addApproval('CRVW', 2)
1627 C.addApproval('CRVW', 2)
1628
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001629 self.worker.addFailTest('project1-test1', A)
1630 self.worker.addFailTest('project1-test2', A)
1631 self.worker.addFailTest('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001632
1633 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1634 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1635 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1636
1637 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001638
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001639 assert len(builds) == 1
1640 assert builds[0].name == 'project1-merge'
1641 assert job_has_changes(builds[0], A)
James E. Blairec590122012-08-22 15:19:31 -07001642
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001643 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001644 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001645 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001646 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001647 self.worker.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001648 self.waitUntilSettled()
1649
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001650 assert len(builds) == 9
1651 assert builds[0].name == 'project1-test1'
1652 assert builds[1].name == 'project1-test2'
1653 assert builds[2].name == 'project1-project2-integration'
1654 assert builds[3].name == 'project1-test1'
1655 assert builds[4].name == 'project1-test2'
1656 assert builds[5].name == 'project1-project2-integration'
1657 assert builds[6].name == 'project1-test1'
1658 assert builds[7].name == 'project1-test2'
1659 assert builds[8].name == 'project1-project2-integration'
James E. Blairec590122012-08-22 15:19:31 -07001660
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001661 self.release(builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001662 self.waitUntilSettled()
1663
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001664 assert len(builds) == 3 # test2, integration, merge for B
1665 assert self.countJobResults(history, 'ABORTED') == 6
James E. Blairec590122012-08-22 15:19:31 -07001666
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001667 self.worker.hold_jobs_in_build = False
1668 self.worker.release()
James E. Blairec590122012-08-22 15:19:31 -07001669 self.waitUntilSettled()
1670
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001671 assert len(builds) == 0
1672 assert len(history) == 20
James E. Blaircaec0c52012-08-22 14:52:22 -07001673
1674 assert A.data['status'] == 'NEW'
James E. Blairec590122012-08-22 15:19:31 -07001675 assert B.data['status'] == 'MERGED'
1676 assert C.data['status'] == 'MERGED'
1677 assert A.reported == 2
1678 assert B.reported == 2
1679 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001680 self.assertEmptyQueues()
James E. Blair4ec821f2012-08-23 15:28:28 -07001681
1682 def test_nonvoting_job(self):
1683 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001684 builds = self.worker.running_builds
1685 history = self.worker.build_history
1686
James E. Blair4ec821f2012-08-23 15:28:28 -07001687 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1688 'master', 'A')
1689 A.addApproval('CRVW', 2)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001690 self.worker.addFailTest('nonvoting-project-test2', A)
James E. Blair4ec821f2012-08-23 15:28:28 -07001691 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1692
1693 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001694
1695 assert A.data['status'] == 'MERGED'
1696 assert A.reported == 2
James E. Blair4ca985f2013-05-30 12:27:43 -07001697 assert (self.getJobFromHistory('nonvoting-project-merge').result ==
1698 'SUCCESS')
1699 assert (self.getJobFromHistory('nonvoting-project-test1').result ==
1700 'SUCCESS')
1701 assert (self.getJobFromHistory('nonvoting-project-test2').result ==
1702 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001703 self.assertEmptyQueues()
1704
1705 def test_check_queue_success(self):
1706 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001707 builds = self.worker.running_builds
1708 history = self.worker.build_history
1709
James E. Blaire0487072012-08-29 17:38:31 -07001710 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1711 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1712
1713 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001714
1715 assert A.data['status'] == 'NEW'
1716 assert A.reported == 1
James E. Blair4ca985f2013-05-30 12:27:43 -07001717 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1718 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1719 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blaire0487072012-08-29 17:38:31 -07001720 self.assertEmptyQueues()
1721
1722 def test_check_queue_failure(self):
1723 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001724 builds = self.worker.running_builds
1725 history = self.worker.build_history
1726
James E. Blaire0487072012-08-29 17:38:31 -07001727 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001728 self.worker.addFailTest('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001729 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1730
1731 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001732
1733 assert A.data['status'] == 'NEW'
1734 assert A.reported == 1
James E. Blair4ca985f2013-05-30 12:27:43 -07001735 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1736 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1737 assert self.getJobFromHistory('project-test2').result == 'FAILURE'
James E. Blaire0487072012-08-29 17:38:31 -07001738 self.assertEmptyQueues()
James E. Blair127bc182012-08-28 15:55:15 -07001739
1740 def test_dependent_behind_dequeue(self):
1741 "test that dependent changes behind dequeued changes work"
1742 # This complicated test is a reproduction of a real life bug
1743 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001744
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001745 builds = self.worker.running_builds
1746 history = self.worker.build_history
1747
1748 self.worker.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001749 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1750 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1751 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1752 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1753 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1754 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1755 D.setDependsOn(C, 1)
1756 E.setDependsOn(D, 1)
1757 A.addApproval('CRVW', 2)
1758 B.addApproval('CRVW', 2)
1759 C.addApproval('CRVW', 2)
1760 D.addApproval('CRVW', 2)
1761 E.addApproval('CRVW', 2)
1762 F.addApproval('CRVW', 2)
1763
1764 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001765
1766 # Change object re-use in the gerrit trigger is hidden if
1767 # changes are added in quick succession; waiting makes it more
1768 # like real life.
1769 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1770 self.waitUntilSettled()
1771 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1772 self.waitUntilSettled()
1773
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001774 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001775 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001776 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001777 self.waitUntilSettled()
1778
1779 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1780 self.waitUntilSettled()
1781 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1782 self.waitUntilSettled()
1783 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1784 self.waitUntilSettled()
1785 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1786 self.waitUntilSettled()
1787
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001788 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001789 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001790 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001791 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001792 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001793 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001794 self.worker.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001795 self.waitUntilSettled()
1796
1797 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001798
1799 # Grab pointers to the jobs we want to release before
1800 # releasing any, because list indexes may change as
1801 # the jobs complete.
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001802 a, b, c = builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001803 a.release()
1804 b.release()
1805 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001806 self.waitUntilSettled()
1807
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001808 self.worker.hold_jobs_in_build = False
1809 self.worker.release()
James E. Blair127bc182012-08-28 15:55:15 -07001810 self.waitUntilSettled()
1811
James E. Blair127bc182012-08-28 15:55:15 -07001812 assert A.data['status'] == 'NEW'
1813 assert B.data['status'] == 'MERGED'
1814 assert C.data['status'] == 'MERGED'
1815 assert D.data['status'] == 'MERGED'
1816 assert E.data['status'] == 'MERGED'
1817 assert F.data['status'] == 'MERGED'
1818
1819 assert A.reported == 2
1820 assert B.reported == 2
1821 assert C.reported == 2
1822 assert D.reported == 2
1823 assert E.reported == 2
1824 assert F.reported == 2
1825
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001826 assert self.countJobResults(history, 'ABORTED') == 15
1827 assert len(history) == 44
James E. Blaire0487072012-08-29 17:38:31 -07001828 self.assertEmptyQueues()
James E. Blair05fed602012-09-07 12:45:24 -07001829
1830 def test_merger_repack(self):
1831 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001832 builds = self.worker.running_builds
1833 history = self.worker.build_history
1834
James E. Blair05fed602012-09-07 12:45:24 -07001835 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1836 A.addApproval('CRVW', 2)
1837 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1838 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001839 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1840 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1841 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blair05fed602012-09-07 12:45:24 -07001842 assert A.data['status'] == 'MERGED'
1843 assert A.reported == 2
1844 self.assertEmptyQueues()
James E. Blair4ca985f2013-05-30 12:27:43 -07001845 self.worker.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001846
1847 path = os.path.join(GIT_ROOT, "org/project")
1848 os.system('git --git-dir=%s/.git repack -afd' % path)
1849
1850 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1851 A.addApproval('CRVW', 2)
1852 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1853 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001854 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1855 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1856 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blair05fed602012-09-07 12:45:24 -07001857 assert A.data['status'] == 'MERGED'
1858 assert A.reported == 2
1859 self.assertEmptyQueues()
James E. Blair7ee88a22012-09-12 18:59:31 +02001860
James E. Blair4886f282012-11-15 09:27:33 -08001861 def test_merger_repack_large_change(self):
1862 "Test that the merger works with large changes after a repack"
1863 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001864 builds = self.worker.running_builds
1865 history = self.worker.build_history
1866
James E. Blair4886f282012-11-15 09:27:33 -08001867 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1868 A.addPatchset(large=True)
1869 path = os.path.join(UPSTREAM_ROOT, "org/project1")
1870 os.system('git --git-dir=%s/.git repack -afd' % path)
1871 path = os.path.join(GIT_ROOT, "org/project1")
1872 os.system('git --git-dir=%s/.git repack -afd' % path)
1873
1874 A.addApproval('CRVW', 2)
1875 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1876 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001877 assert self.getJobFromHistory('project1-merge').result == 'SUCCESS'
1878 assert self.getJobFromHistory('project1-test1').result == 'SUCCESS'
1879 assert self.getJobFromHistory('project1-test2').result == 'SUCCESS'
James E. Blair4886f282012-11-15 09:27:33 -08001880 assert A.data['status'] == 'MERGED'
1881 assert A.reported == 2
1882 self.assertEmptyQueues()
1883
James E. Blair7ee88a22012-09-12 18:59:31 +02001884 def test_nonexistent_job(self):
1885 "Test launching a job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001886 builds = self.worker.running_builds
1887 history = self.worker.build_history
1888
1889 # Set to the state immediately after a restart
1890 self.resetGearmanServer()
1891 self.launcher.negative_function_cache_ttl = 0
James E. Blair7ee88a22012-09-12 18:59:31 +02001892
1893 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1894 A.addApproval('CRVW', 2)
1895 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1896 # There may be a thread about to report a lost change
1897 while A.reported < 2:
1898 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001899 job_names = [x.name for x in history]
James E. Blair7ee88a22012-09-12 18:59:31 +02001900 assert not job_names
1901 assert A.data['status'] == 'NEW'
1902 assert A.reported == 2
1903 self.assertEmptyQueues()
1904
1905 # Make sure things still work:
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001906 self.registerJobs()
James E. Blair7ee88a22012-09-12 18:59:31 +02001907 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1908 A.addApproval('CRVW', 2)
1909 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1910 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07001911 assert self.getJobFromHistory('project-merge').result == 'SUCCESS'
1912 assert self.getJobFromHistory('project-test1').result == 'SUCCESS'
1913 assert self.getJobFromHistory('project-test2').result == 'SUCCESS'
James E. Blair7ee88a22012-09-12 18:59:31 +02001914 assert A.data['status'] == 'MERGED'
1915 assert A.reported == 2
1916 self.assertEmptyQueues()
James E. Blairf62d4282012-12-31 17:01:50 -08001917
1918 def test_single_nonexistent_post_job(self):
1919 "Test launching a single post job that doesn't exist"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001920 builds = self.worker.running_builds
1921 history = self.worker.build_history
James E. Blairf62d4282012-12-31 17:01:50 -08001922
1923 e = {
1924 "type": "ref-updated",
1925 "submitter": {
1926 "name": "User Name",
1927 },
1928 "refUpdate": {
1929 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1930 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1931 "refName": "master",
1932 "project": "org/project",
1933 }
1934 }
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001935 # Set to the state immediately after a restart
1936 self.resetGearmanServer()
1937 self.launcher.negative_function_cache_ttl = 0
1938
James E. Blairf62d4282012-12-31 17:01:50 -08001939 self.fake_gerrit.addEvent(e)
1940 self.waitUntilSettled()
1941
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001942 assert len(history) == 0
James E. Blairf62d4282012-12-31 17:01:50 -08001943 self.assertEmptyQueues()
James E. Blair2fa50962013-01-30 21:50:41 -08001944
1945 def test_new_patchset_dequeues_old(self):
1946 "Test that a new patchset causes the old to be dequeued"
1947 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001948 builds = self.worker.running_builds
1949 history = self.worker.build_history
James E. Blair2fa50962013-01-30 21:50:41 -08001950
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001951 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001952 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1953 M.setMerged()
1954
1955 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1956 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1957 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1958 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1959 A.addApproval('CRVW', 2)
1960 B.addApproval('CRVW', 2)
1961 C.addApproval('CRVW', 2)
1962 D.addApproval('CRVW', 2)
1963
1964 C.setDependsOn(B, 1)
1965 B.setDependsOn(A, 1)
1966 A.setDependsOn(M, 1)
1967
1968 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1969 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1970 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1971 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1972 self.waitUntilSettled()
1973
1974 B.addPatchset()
1975 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1976 self.waitUntilSettled()
1977
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001978 self.worker.hold_jobs_in_build = False
1979 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001980 self.waitUntilSettled()
1981
James E. Blair2fa50962013-01-30 21:50:41 -08001982 assert A.data['status'] == 'MERGED'
1983 assert A.reported == 2
1984 assert B.data['status'] == 'NEW'
1985 assert B.reported == 2
1986 assert C.data['status'] == 'NEW'
1987 assert C.reported == 2
1988 assert D.data['status'] == 'MERGED'
1989 assert D.reported == 2
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001990 assert len(history) == 9 # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001991 self.assertEmptyQueues()
1992
1993 def test_new_patchset_dequeues_old_on_head(self):
1994 "Test that a new patchset causes the old to be dequeued (at head)"
1995 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001996 builds = self.worker.running_builds
1997 history = self.worker.build_history
James E. Blair2fa50962013-01-30 21:50:41 -08001998
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001999 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002000 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2001 M.setMerged()
2002 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2003 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2004 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2005 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
2006 A.addApproval('CRVW', 2)
2007 B.addApproval('CRVW', 2)
2008 C.addApproval('CRVW', 2)
2009 D.addApproval('CRVW', 2)
2010
2011 C.setDependsOn(B, 1)
2012 B.setDependsOn(A, 1)
2013 A.setDependsOn(M, 1)
2014
2015 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2016 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2017 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2018 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2019 self.waitUntilSettled()
2020
2021 A.addPatchset()
2022 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2023 self.waitUntilSettled()
2024
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002025 self.worker.hold_jobs_in_build = False
2026 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002027 self.waitUntilSettled()
2028
James E. Blair2fa50962013-01-30 21:50:41 -08002029 assert A.data['status'] == 'NEW'
2030 assert A.reported == 2
2031 assert B.data['status'] == 'NEW'
2032 assert B.reported == 2
2033 assert C.data['status'] == 'NEW'
2034 assert C.reported == 2
2035 assert D.data['status'] == 'MERGED'
2036 assert D.reported == 2
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002037 assert len(history) == 7
James E. Blair2fa50962013-01-30 21:50:41 -08002038 self.assertEmptyQueues()
2039
2040 def test_new_patchset_dequeues_old_without_dependents(self):
2041 "Test that a new patchset causes only the old to be dequeued"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002042 builds = self.worker.running_builds
2043 history = self.worker.build_history
James E. Blair2fa50962013-01-30 21:50:41 -08002044
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002045 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002046 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2047 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2048 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2049 A.addApproval('CRVW', 2)
2050 B.addApproval('CRVW', 2)
2051 C.addApproval('CRVW', 2)
2052
2053 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2054 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2055 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2056 self.waitUntilSettled()
2057
2058 B.addPatchset()
2059 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2060 self.waitUntilSettled()
2061
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002062 self.worker.hold_jobs_in_build = False
2063 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002064 self.waitUntilSettled()
2065
James E. Blair2fa50962013-01-30 21:50:41 -08002066 assert A.data['status'] == 'MERGED'
2067 assert A.reported == 2
2068 assert B.data['status'] == 'NEW'
2069 assert B.reported == 2
2070 assert C.data['status'] == 'MERGED'
2071 assert C.reported == 2
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002072 assert len(history) == 9
James E. Blair2fa50962013-01-30 21:50:41 -08002073 self.assertEmptyQueues()
2074
2075 def test_new_patchset_dequeues_old_independent_queue(self):
2076 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002077 builds = self.worker.running_builds
2078 history = self.worker.build_history
James E. Blair2fa50962013-01-30 21:50:41 -08002079
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002080 self.worker.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002081 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2082 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2083 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2084 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2085 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2086 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2087 self.waitUntilSettled()
2088
2089 B.addPatchset()
2090 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2091 self.waitUntilSettled()
2092
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002093 self.worker.hold_jobs_in_build = False
2094 self.worker.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002095 self.waitUntilSettled()
2096
James E. Blair2fa50962013-01-30 21:50:41 -08002097 assert A.data['status'] == 'NEW'
2098 assert A.reported == 1
2099 assert B.data['status'] == 'NEW'
2100 assert B.reported == 1
2101 assert C.data['status'] == 'NEW'
2102 assert C.reported == 1
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002103 assert len(history) == 10
2104 assert self.countJobResults(history, 'ABORTED') == 1
James E. Blair2fa50962013-01-30 21:50:41 -08002105 self.assertEmptyQueues()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002106
2107 def test_zuul_refs(self):
2108 "Test that zuul refs exist and have the right changes"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002109 builds = self.worker.running_builds
2110 history = self.worker.build_history
James E. Blair7d0dedc2013-02-21 17:26:09 -08002111
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002112 self.worker.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002113 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2114 M1.setMerged()
2115 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2116 M2.setMerged()
2117
2118 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2119 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2120 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2121 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
2122 A.addApproval('CRVW', 2)
2123 B.addApproval('CRVW', 2)
2124 C.addApproval('CRVW', 2)
2125 D.addApproval('CRVW', 2)
2126 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2127 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2128 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
2129 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
2130
2131 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002132 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002133 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002134 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002135 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002136 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002137 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002138 self.worker.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002139 self.waitUntilSettled()
2140
James E. Blair7d0dedc2013-02-21 17:26:09 -08002141 a_zref = b_zref = c_zref = d_zref = None
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002142 for x in builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002143 if x.parameters['ZUUL_CHANGE'] == '3':
2144 a_zref = x.parameters['ZUUL_REF']
2145 if x.parameters['ZUUL_CHANGE'] == '4':
2146 b_zref = x.parameters['ZUUL_REF']
2147 if x.parameters['ZUUL_CHANGE'] == '5':
2148 c_zref = x.parameters['ZUUL_REF']
2149 if x.parameters['ZUUL_CHANGE'] == '6':
2150 d_zref = x.parameters['ZUUL_REF']
2151
2152 # There are... four... refs.
2153 assert a_zref is not None
2154 assert b_zref is not None
2155 assert c_zref is not None
2156 assert d_zref is not None
2157
2158 # And they should all be different
2159 refs = set([a_zref, b_zref, c_zref, d_zref])
2160 assert len(refs) == 4
2161
2162 # a ref should have a, not b, and should not be in project2
2163 assert ref_has_change(a_zref, A)
2164 assert not ref_has_change(a_zref, B)
2165 assert not ref_has_change(a_zref, M2)
2166
2167 # b ref should have a and b, and should not be in project2
2168 assert ref_has_change(b_zref, A)
2169 assert ref_has_change(b_zref, B)
2170 assert not ref_has_change(b_zref, M2)
2171
2172 # c ref should have a and b in 1, c in 2
2173 assert ref_has_change(c_zref, A)
2174 assert ref_has_change(c_zref, B)
2175 assert ref_has_change(c_zref, C)
2176 assert not ref_has_change(c_zref, D)
2177
2178 # d ref should have a and b in 1, c and d in 2
2179 assert ref_has_change(d_zref, A)
2180 assert ref_has_change(d_zref, B)
2181 assert ref_has_change(d_zref, C)
2182 assert ref_has_change(d_zref, D)
2183
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002184 self.worker.hold_jobs_in_build = False
2185 self.worker.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002186 self.waitUntilSettled()
2187
2188 assert A.data['status'] == 'MERGED'
2189 assert A.reported == 2
2190 assert B.data['status'] == 'MERGED'
2191 assert B.reported == 2
2192 assert C.data['status'] == 'MERGED'
2193 assert C.reported == 2
2194 assert D.data['status'] == 'MERGED'
2195 assert D.reported == 2
2196 self.assertEmptyQueues()
James E. Blair70c71582013-03-06 08:50:50 -08002197
James E. Blair412e5582013-04-22 15:50:12 -07002198 def test_statsd(self):
2199 "Test each of the statsd methods used in the scheduler"
2200 import extras
2201 statsd = extras.try_import('statsd.statsd')
2202 statsd.incr('test-incr')
2203 statsd.timing('test-timing', 3)
2204 statsd.gauge('test-guage', 12)
2205 self.assertReportedStat('test-incr', '1|c')
2206 self.assertReportedStat('test-timing', '3|ms')
2207 self.assertReportedStat('test-guage', '12|g')
2208
James E. Blair70c71582013-03-06 08:50:50 -08002209 def test_file_jobs(self):
2210 "Test that file jobs run only when appropriate"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002211 builds = self.worker.running_builds
2212 history = self.worker.build_history
2213
James E. Blair70c71582013-03-06 08:50:50 -08002214 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2215 A.addPatchset(['pip-requires'])
2216 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2217 A.addApproval('CRVW', 2)
2218 B.addApproval('CRVW', 2)
2219 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2220 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2221 self.waitUntilSettled()
2222
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002223 testfile_jobs = [x for x in history
James E. Blair70c71582013-03-06 08:50:50 -08002224 if x.name == 'project-testfile']
2225
2226 assert len(testfile_jobs) == 1
2227 assert testfile_jobs[0].changes == '1,2'
2228 assert A.data['status'] == 'MERGED'
2229 assert A.reported == 2
2230 assert B.data['status'] == 'MERGED'
2231 assert B.reported == 2
2232 self.assertEmptyQueues()
James E. Blair3c5e5b52013-04-26 11:17:03 -07002233
2234 def test_test_config(self):
2235 "Test that we can test the config"
2236 sched = zuul.scheduler.Scheduler()
2237 sched.testConfig(CONFIG.get('zuul', 'layout_config'))
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002238
2239 def test_build_description(self):
2240 "Test that build descriptions update"
2241 builds = self.worker.running_builds
2242 history = self.worker.build_history
2243
2244 self.worker.registerFunction('set_description:' +
2245 self.worker.worker_id)
2246
2247 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2248 A.addApproval('CRVW', 2)
2249 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2250 self.waitUntilSettled()
2251 desc = history[0].description
2252 self.log.debug("Description: %s" % desc)
2253 assert re.search("Branch.*master", desc)
2254 assert re.search("Pipeline.*gate", desc)
2255 assert re.search("project-merge.*SUCCESS", desc)
2256 assert re.search("project-test1.*SUCCESS", desc)
2257 assert re.search("project-test2.*SUCCESS", desc)
2258 assert re.search("Reported result.*SUCCESS", desc)
2259
2260 def test_node_label(self):
2261 "Test that a job runs on a specific node label"
2262 builds = self.worker.running_builds
2263 history = self.worker.build_history
2264
2265 self.worker.registerFunction('build:node-project-test1:debian')
2266
2267 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
2268 A.addApproval('CRVW', 2)
2269 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2270 self.waitUntilSettled()
James E. Blair4ca985f2013-05-30 12:27:43 -07002271
2272 assert self.getJobFromHistory('node-project-merge').node is None
2273 assert self.getJobFromHistory('node-project-test1').node == 'debian'
2274 assert self.getJobFromHistory('node-project-test2').node is None