blob: 557809998b34ba60b3bc6d3c0c3e14a4bc326740 [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. Blair4886cc12012-07-18 15:39:41 -070036import git
James E. Blairb0fcae42012-07-17 11:12:10 -070037
James E. Blairb0fcae42012-07-17 11:12:10 -070038import zuul.scheduler
39import zuul.launcher.jenkins
40import zuul.trigger.gerrit
41
42FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
43 'fixtures')
44CONFIG = ConfigParser.ConfigParser()
45CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf"))
46
47CONFIG.set('zuul', 'layout_config',
48 os.path.join(FIXTURE_DIR, "layout.yaml"))
49
James E. Blair1dbd5082012-08-23 15:12:15 -070050TMP_ROOT = os.environ.get("ZUUL_TEST_ROOT", "/tmp")
51TEST_ROOT = os.path.join(TMP_ROOT, "zuul-test")
52UPSTREAM_ROOT = os.path.join(TEST_ROOT, "upstream")
53GIT_ROOT = os.path.join(TEST_ROOT, "git")
54
55CONFIG.set('zuul', 'git_dir', GIT_ROOT)
56
James E. Blairb0fcae42012-07-17 11:12:10 -070057logging.basicConfig(level=logging.DEBUG)
58
59
James E. Blair8cc15a82012-08-01 11:17:57 -070060def random_sha1():
61 return hashlib.sha1(str(random.random())).hexdigest()
62
63
James E. Blair4886cc12012-07-18 15:39:41 -070064class ChangeReference(git.Reference):
65 _common_path_default = "refs/changes"
66 _points_to_commits_only = True
67
68
69def init_repo(project):
70 parts = project.split('/')
James E. Blair1dbd5082012-08-23 15:12:15 -070071 path = os.path.join(UPSTREAM_ROOT, *parts[:-1])
James E. Blair4886cc12012-07-18 15:39:41 -070072 if not os.path.exists(path):
73 os.makedirs(path)
James E. Blair1dbd5082012-08-23 15:12:15 -070074 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -070075 repo = git.Repo.init(path)
76
77 fn = os.path.join(path, 'README')
78 f = open(fn, 'w')
79 f.write("test\n")
80 f.close()
81 repo.index.add([fn])
82 repo.index.commit('initial commit')
James E. Blairc6294a52012-08-17 10:19:48 -070083 master = repo.create_head('master')
James E. Blair4886cc12012-07-18 15:39:41 -070084 repo.create_tag('init')
85
James E. Blairc6294a52012-08-17 10:19:48 -070086 mp = repo.create_head('mp')
87 repo.head.reference = mp
88 f = open(fn, 'a')
89 f.write("test mp\n")
90 f.close()
91 repo.index.add([fn])
92 repo.index.commit('mp commit')
93
94 repo.head.reference = master
95 repo.head.reset(index=True, working_tree=True)
96 repo.git.clean('-x', '-f', '-d')
97
James E. Blair4886cc12012-07-18 15:39:41 -070098
James E. Blair4886f282012-11-15 09:27:33 -080099def add_fake_change_to_repo(project, branch, change_num, patchset, msg, fn,
100 large):
James E. Blair1dbd5082012-08-23 15:12:15 -0700101 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -0700102 repo = git.Repo(path)
103 ref = ChangeReference.create(repo, '1/%s/%s' % (change_num,
104 patchset),
105 'refs/tags/init')
106 repo.head.reference = ref
107 repo.head.reset(index=True, working_tree=True)
108 repo.git.clean('-x', '-f', '-d')
109
James E. Blair1dbd5082012-08-23 15:12:15 -0700110 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blair4886f282012-11-15 09:27:33 -0800111 if not large:
112 fn = os.path.join(path, fn)
113 f = open(fn, 'w')
114 f.write("test %s %s %s\n" % (branch, change_num, patchset))
115 f.close()
116 repo.index.add([fn])
117 else:
118 for fni in range(100):
119 fn = os.path.join(path, str(fni))
120 f = open(fn, 'w')
121 for ci in range(4096):
122 f.write(random.choice(string.printable))
123 f.close()
124 repo.index.add([fn])
125
James E. Blairdaabed22012-08-15 15:38:57 -0700126 return repo.index.commit(msg)
James E. Blair4886cc12012-07-18 15:39:41 -0700127
128
129def ref_has_change(ref, change):
James E. Blair1dbd5082012-08-23 15:12:15 -0700130 path = os.path.join(GIT_ROOT, change.project)
James E. Blair4886cc12012-07-18 15:39:41 -0700131 repo = git.Repo(path)
132 for commit in repo.iter_commits(ref):
133 if commit.message.strip() == ('%s-1' % change.subject):
134 return True
135 return False
136
137
138def job_has_changes(*args):
139 job = args[0]
140 commits = args[1:]
141 project = job.parameters['ZUUL_PROJECT']
James E. Blair1dbd5082012-08-23 15:12:15 -0700142 path = os.path.join(GIT_ROOT, project)
James E. Blair4886cc12012-07-18 15:39:41 -0700143 repo = git.Repo(path)
144 ref = job.parameters['ZUUL_REF']
James E. Blair81515ad2012-10-01 18:29:08 -0700145 sha = job.parameters['ZUUL_COMMIT']
James E. Blair4886cc12012-07-18 15:39:41 -0700146 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
James E. Blair81515ad2012-10-01 18:29:08 -0700147 repo_shas = [c.hexsha for c in repo.iter_commits(ref)]
James E. Blair4886cc12012-07-18 15:39:41 -0700148 commit_messages = ['%s-1' % commit.subject for commit in commits]
James E. Blair4886cc12012-07-18 15:39:41 -0700149 for msg in commit_messages:
150 if msg not in repo_messages:
151 return False
James E. Blair81515ad2012-10-01 18:29:08 -0700152 if repo_shas[0] != sha:
153 return False
James E. Blair4886cc12012-07-18 15:39:41 -0700154 return True
155
156
James E. Blairb0fcae42012-07-17 11:12:10 -0700157class FakeChange(object):
James E. Blair8c803f82012-07-31 16:25:42 -0700158 categories = {'APRV': ('Approved', -1, 1),
159 'CRVW': ('Code-Review', -2, 2),
160 'VRFY': ('Verified', -2, 2)}
James E. Blairb0fcae42012-07-17 11:12:10 -0700161
James E. Blair8cc15a82012-08-01 11:17:57 -0700162 def __init__(self, gerrit, number, project, branch, subject, status='NEW'):
163 self.gerrit = gerrit
James E. Blaird466dc42012-07-31 10:42:56 -0700164 self.reported = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700165 self.queried = 0
James E. Blairb0fcae42012-07-17 11:12:10 -0700166 self.patchsets = []
James E. Blairb0fcae42012-07-17 11:12:10 -0700167 self.number = number
168 self.project = project
169 self.branch = branch
170 self.subject = subject
171 self.latest_patchset = 0
James E. Blair8c803f82012-07-31 16:25:42 -0700172 self.depends_on_change = None
173 self.needed_by_changes = []
James E. Blair127bc182012-08-28 15:55:15 -0700174 self.fail_merge = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700175 self.data = {
176 'branch': branch,
177 'comments': [],
178 'commitMessage': subject,
179 'createdOn': time.time(),
James E. Blair8cc15a82012-08-01 11:17:57 -0700180 'id': 'I' + random_sha1(),
James E. Blairb0fcae42012-07-17 11:12:10 -0700181 'lastUpdated': time.time(),
182 'number': str(number),
183 'open': True,
184 'owner': {'email': 'user@example.com',
185 'name': 'User Name',
186 'username': 'username'},
187 'patchSets': self.patchsets,
188 'project': project,
189 'status': status,
190 'subject': subject,
James E. Blair8c803f82012-07-31 16:25:42 -0700191 'submitRecords': [],
James E. Blairb0fcae42012-07-17 11:12:10 -0700192 'url': 'https://hostname/%s' % number}
193
194 self.addPatchset()
James E. Blair8c803f82012-07-31 16:25:42 -0700195 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700196
James E. Blair70c71582013-03-06 08:50:50 -0800197 def addPatchset(self, files=[], large=False):
James E. Blairb0fcae42012-07-17 11:12:10 -0700198 self.latest_patchset += 1
James E. Blairdaabed22012-08-15 15:38:57 -0700199 if files:
200 fn = files[0]
201 else:
202 fn = '%s-%s' % (self.branch, self.number)
203 msg = self.subject + '-' + str(self.latest_patchset)
204 c = add_fake_change_to_repo(self.project, self.branch,
205 self.number, self.latest_patchset,
James E. Blair4886f282012-11-15 09:27:33 -0800206 msg, fn, large)
James E. Blair70c71582013-03-06 08:50:50 -0800207 ps_files = [{'file': '/COMMIT_MSG',
208 'type': 'ADDED'},
209 {'file': 'README',
210 'type': 'MODIFIED'}]
211 for f in files:
212 ps_files.append({'file': f, 'type': 'ADDED'})
James E. Blairb0fcae42012-07-17 11:12:10 -0700213 d = {'approvals': [],
214 'createdOn': time.time(),
James E. Blair70c71582013-03-06 08:50:50 -0800215 'files': ps_files,
James E. Blair8c803f82012-07-31 16:25:42 -0700216 'number': str(self.latest_patchset),
James E. Blairb0fcae42012-07-17 11:12:10 -0700217 'ref': 'refs/changes/1/%s/%s' % (self.number,
218 self.latest_patchset),
James E. Blairdaabed22012-08-15 15:38:57 -0700219 'revision': c.hexsha,
James E. Blairb0fcae42012-07-17 11:12:10 -0700220 'uploader': {'email': 'user@example.com',
221 'name': 'User name',
222 'username': 'user'}}
223 self.data['currentPatchSet'] = d
224 self.patchsets.append(d)
James E. Blair8c803f82012-07-31 16:25:42 -0700225 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700226
James E. Blaire0487072012-08-29 17:38:31 -0700227 def getPatchsetCreatedEvent(self, patchset):
228 event = {"type": "patchset-created",
229 "change": {"project": self.project,
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800230 "branch": self.branch,
231 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
232 "number": str(self.number),
233 "subject": self.subject,
234 "owner": {"name": "User Name"},
235 "url": "https://hostname/3"},
James E. Blaire0487072012-08-29 17:38:31 -0700236 "patchSet": self.patchsets[patchset - 1],
237 "uploader": {"name": "User Name"}}
238 return event
239
James E. Blairb0fcae42012-07-17 11:12:10 -0700240 def addApproval(self, category, value):
James E. Blair8c803f82012-07-31 16:25:42 -0700241 approval = {'description': self.categories[category][0],
242 'type': category,
243 'value': str(value)}
244 self.patchsets[-1]['approvals'].append(approval)
245 event = {'approvals': [approval],
James E. Blairb0fcae42012-07-17 11:12:10 -0700246 'author': {'email': 'user@example.com',
247 'name': 'User Name',
248 'username': 'username'},
249 'change': {'branch': self.branch,
250 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
251 'number': str(self.number),
252 'owner': {'email': 'user@example.com',
253 'name': 'User Name',
254 'username': 'username'},
255 'project': self.project,
256 'subject': self.subject,
257 'topic': 'master',
258 'url': 'https://hostname/459'},
259 'comment': '',
260 'patchSet': self.patchsets[-1],
261 'type': 'comment-added'}
James E. Blair8c803f82012-07-31 16:25:42 -0700262 self.data['submitRecords'] = self.getSubmitRecords()
James E. Blairb0fcae42012-07-17 11:12:10 -0700263 return json.loads(json.dumps(event))
264
James E. Blair8c803f82012-07-31 16:25:42 -0700265 def getSubmitRecords(self):
266 status = {}
267 for cat in self.categories.keys():
268 status[cat] = 0
269
270 for a in self.patchsets[-1]['approvals']:
271 cur = status[a['type']]
272 cat_min, cat_max = self.categories[a['type']][1:]
273 new = int(a['value'])
274 if new == cat_min:
275 cur = new
276 elif abs(new) > abs(cur):
277 cur = new
278 status[a['type']] = cur
279
280 labels = []
281 ok = True
282 for typ, cat in self.categories.items():
283 cur = status[typ]
284 cat_min, cat_max = cat[1:]
285 if cur == cat_min:
286 value = 'REJECT'
287 ok = False
288 elif cur == cat_max:
289 value = 'OK'
290 else:
291 value = 'NEED'
292 ok = False
293 labels.append({'label': cat[0], 'status': value})
294 if ok:
295 return [{'status': 'OK'}]
296 return [{'status': 'NOT_READY',
297 'labels': labels}]
298
299 def setDependsOn(self, other, patchset):
300 self.depends_on_change = other
301 d = {'id': other.data['id'],
302 'number': other.data['number'],
303 'ref': other.patchsets[patchset - 1]['ref']
304 }
305 self.data['dependsOn'] = [d]
306
307 other.needed_by_changes.append(self)
308 needed = other.data.get('neededBy', [])
309 d = {'id': self.data['id'],
310 'number': self.data['number'],
311 'ref': self.patchsets[patchset - 1]['ref'],
312 'revision': self.patchsets[patchset - 1]['revision']
313 }
314 needed.append(d)
315 other.data['neededBy'] = needed
316
James E. Blairb0fcae42012-07-17 11:12:10 -0700317 def query(self):
James E. Blair8c803f82012-07-31 16:25:42 -0700318 self.queried += 1
319 d = self.data.get('dependsOn')
320 if d:
321 d = d[0]
322 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
323 d['isCurrentPatchSet'] = True
324 else:
325 d['isCurrentPatchSet'] = False
James E. Blairb0fcae42012-07-17 11:12:10 -0700326 return json.loads(json.dumps(self.data))
327
328 def setMerged(self):
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800329 if (self.depends_on_change and
330 self.depends_on_change.data['status'] != 'MERGED'):
James E. Blaircaec0c52012-08-22 14:52:22 -0700331 return
James E. Blair127bc182012-08-28 15:55:15 -0700332 if self.fail_merge:
333 return
James E. Blairb0fcae42012-07-17 11:12:10 -0700334 self.data['status'] = 'MERGED'
335 self.open = False
James E. Blairdaabed22012-08-15 15:38:57 -0700336
James E. Blair1dbd5082012-08-23 15:12:15 -0700337 path = os.path.join(UPSTREAM_ROOT, self.project)
James E. Blairdaabed22012-08-15 15:38:57 -0700338 repo = git.Repo(path)
339 repo.heads[self.branch].commit = \
340 repo.commit(self.patchsets[-1]['revision'])
James E. Blairb0fcae42012-07-17 11:12:10 -0700341
James E. Blaird466dc42012-07-31 10:42:56 -0700342 def setReported(self):
343 self.reported += 1
344
James E. Blairb0fcae42012-07-17 11:12:10 -0700345
346class FakeGerrit(object):
347 def __init__(self, *args, **kw):
348 self.event_queue = Queue.Queue()
349 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
350 self.change_number = 0
351 self.changes = {}
352
353 def addFakeChange(self, project, branch, subject):
354 self.change_number += 1
James E. Blair8cc15a82012-08-01 11:17:57 -0700355 c = FakeChange(self, self.change_number, project, branch, subject)
James E. Blairb0fcae42012-07-17 11:12:10 -0700356 self.changes[self.change_number] = c
357 return c
358
359 def addEvent(self, data):
360 return self.event_queue.put(data)
361
362 def getEvent(self):
363 return self.event_queue.get()
364
365 def eventDone(self):
366 self.event_queue.task_done()
367
368 def review(self, project, changeid, message, action):
James E. Blaird466dc42012-07-31 10:42:56 -0700369 number, ps = changeid.split(',')
370 change = self.changes[int(number)]
James E. Blairb0fcae42012-07-17 11:12:10 -0700371 if 'submit' in action:
James E. Blairb0fcae42012-07-17 11:12:10 -0700372 change.setMerged()
James E. Blaird466dc42012-07-31 10:42:56 -0700373 if message:
374 change.setReported()
James E. Blairb0fcae42012-07-17 11:12:10 -0700375
376 def query(self, number):
377 change = self.changes[int(number)]
378 return change.query()
379
380 def startWatching(self, *args, **kw):
381 pass
382
383
384class FakeJenkinsEvent(object):
385 def __init__(self, name, number, parameters, phase, status=None):
Zhongyue Luo5d556072012-09-21 02:00:47 +0900386 data = {
387 'build': {
388 'full_url': 'https://server/job/%s/%s/' % (name, number),
389 'number': number,
390 'parameters': parameters,
391 'phase': phase,
392 'url': 'job/%s/%s/' % (name, number),
393 },
394 'name': name,
395 'url': 'job/%s/' % name,
396 }
James E. Blairb0fcae42012-07-17 11:12:10 -0700397 if status:
398 data['build']['status'] = status
399 self.body = json.dumps(data)
400
401
402class FakeJenkinsJob(threading.Thread):
403 log = logging.getLogger("zuul.test")
404
405 def __init__(self, jenkins, callback, name, number, parameters):
406 threading.Thread.__init__(self)
407 self.jenkins = jenkins
408 self.callback = callback
409 self.name = name
410 self.number = number
411 self.parameters = parameters
412 self.wait_condition = threading.Condition()
413 self.waiting = False
James E. Blaird466dc42012-07-31 10:42:56 -0700414 self.aborted = False
415 self.canceled = False
416 self.created = time.time()
James E. Blairb0fcae42012-07-17 11:12:10 -0700417
418 def release(self):
419 self.wait_condition.acquire()
420 self.wait_condition.notify()
421 self.waiting = False
422 self.log.debug("Job %s released" % (self.parameters['UUID']))
423 self.wait_condition.release()
424
425 def isWaiting(self):
426 self.wait_condition.acquire()
427 if self.waiting:
428 ret = True
429 else:
430 ret = False
431 self.wait_condition.release()
432 return ret
433
434 def _wait(self):
435 self.wait_condition.acquire()
436 self.waiting = True
437 self.log.debug("Job %s waiting" % (self.parameters['UUID']))
438 self.wait_condition.wait()
439 self.wait_condition.release()
440
441 def run(self):
442 self.jenkins.fakeEnqueue(self)
443 if self.jenkins.hold_jobs_in_queue:
444 self._wait()
445 self.jenkins.fakeDequeue(self)
James E. Blaird466dc42012-07-31 10:42:56 -0700446 if self.canceled:
447 self.jenkins.all_jobs.remove(self)
448 return
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800449 self.callback.jenkins_endpoint(FakeJenkinsEvent(self.name,
450 self.number,
451 self.parameters,
452 'STARTED'))
James E. Blairb0fcae42012-07-17 11:12:10 -0700453 if self.jenkins.hold_jobs_in_build:
454 self._wait()
455 self.log.debug("Job %s continuing" % (self.parameters['UUID']))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700456
457 result = 'SUCCESS'
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800458 if (('ZUUL_REF' in self.parameters) and
459 self.jenkins.fakeShouldFailTest(self.name,
460 self.parameters['ZUUL_REF'])):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700461 result = 'FAILURE'
James E. Blaird466dc42012-07-31 10:42:56 -0700462 if self.aborted:
463 result = 'ABORTED'
James E. Blairb02a3bb2012-07-30 17:49:55 -0700464
James E. Blair0018a6c2013-02-27 14:11:45 -0800465 changes = None
466 if 'ZUUL_CHANGE_IDS' in self.parameters:
467 changes = self.parameters['ZUUL_CHANGE_IDS']
468
James E. Blairb0fcae42012-07-17 11:12:10 -0700469 self.jenkins.fakeAddHistory(name=self.name, number=self.number,
James E. Blair0018a6c2013-02-27 14:11:45 -0800470 result=result, changes=changes)
James E. Blairff791972013-01-09 11:45:43 -0800471 self.jenkins.lock.acquire()
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800472 self.callback.jenkins_endpoint(FakeJenkinsEvent(self.name,
473 self.number,
474 self.parameters,
475 'COMPLETED',
476 result))
477 self.callback.jenkins_endpoint(FakeJenkinsEvent(self.name,
478 self.number,
479 self.parameters,
480 'FINISHED',
481 result))
James E. Blairb0fcae42012-07-17 11:12:10 -0700482 self.jenkins.all_jobs.remove(self)
James E. Blairff791972013-01-09 11:45:43 -0800483 self.jenkins.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700484
485
486class FakeJenkins(object):
487 log = logging.getLogger("zuul.test")
488
489 def __init__(self, *args, **kw):
490 self.queue = []
491 self.all_jobs = []
492 self.job_counter = {}
James E. Blaird466dc42012-07-31 10:42:56 -0700493 self.queue_counter = 0
James E. Blairb0fcae42012-07-17 11:12:10 -0700494 self.job_history = []
495 self.hold_jobs_in_queue = False
496 self.hold_jobs_in_build = False
James E. Blairb02a3bb2012-07-30 17:49:55 -0700497 self.fail_tests = {}
James E. Blair7ee88a22012-09-12 18:59:31 +0200498 self.nonexistent_jobs = []
James E. Blairff791972013-01-09 11:45:43 -0800499 self.lock = threading.Lock()
James E. Blairb0fcae42012-07-17 11:12:10 -0700500
501 def fakeEnqueue(self, job):
502 self.queue.append(job)
503
504 def fakeDequeue(self, job):
505 self.queue.remove(job)
506
James E. Blair0018a6c2013-02-27 14:11:45 -0800507 class FakeJobHistory(object):
508 def __init__(self, **kw):
509 self.__dict__.update(kw)
510
511 def __repr__(self):
512 return ("<Completed job, result: %s name: %s #%s changes: %s>" %
513 (self.result, self.name, self.number, self.changes))
514
James E. Blairb0fcae42012-07-17 11:12:10 -0700515 def fakeAddHistory(self, **kw):
James E. Blair0018a6c2013-02-27 14:11:45 -0800516 self.job_history.append(self.FakeJobHistory(**kw))
James E. Blairb0fcae42012-07-17 11:12:10 -0700517
518 def fakeRelease(self, regex=None):
519 all_jobs = self.all_jobs[:]
520 self.log.debug("releasing jobs %s (%s)" % (regex, len(self.all_jobs)))
521 for job in all_jobs:
522 if not regex or re.match(regex, job.name):
523 self.log.debug("releasing job %s" % (job.parameters['UUID']))
524 job.release()
525 else:
Zhongyue Luoaa85ebf2012-09-21 16:38:33 +0800526 self.log.debug("not releasing job %s" %
527 (job.parameters['UUID']))
James E. Blairb0fcae42012-07-17 11:12:10 -0700528 self.log.debug("done releasing jobs %s (%s)" % (regex,
529 len(self.all_jobs)))
530
531 def fakeAllWaiting(self, regex=None):
James E. Blair4aa1ad62012-10-05 12:39:26 -0700532 all_jobs = self.all_jobs[:] + self.queue[:]
James E. Blairb0fcae42012-07-17 11:12:10 -0700533 for job in all_jobs:
534 self.log.debug("job %s %s" % (job.parameters['UUID'],
535 job.isWaiting()))
536 if not job.isWaiting():
537 return False
538 return True
539
James E. Blairb02a3bb2012-07-30 17:49:55 -0700540 def fakeAddFailTest(self, name, change):
541 l = self.fail_tests.get(name, [])
542 l.append(change)
543 self.fail_tests[name] = l
544
James E. Blair4886cc12012-07-18 15:39:41 -0700545 def fakeShouldFailTest(self, name, ref):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700546 l = self.fail_tests.get(name, [])
547 for change in l:
James E. Blair4886cc12012-07-18 15:39:41 -0700548 if ref_has_change(ref, change):
James E. Blairb02a3bb2012-07-30 17:49:55 -0700549 return True
550 return False
551
James E. Blairb0fcae42012-07-17 11:12:10 -0700552 def build_job(self, name, parameters):
James E. Blair7ee88a22012-09-12 18:59:31 +0200553 if name in self.nonexistent_jobs:
554 raise Exception("Job does not exist")
James E. Blairb0fcae42012-07-17 11:12:10 -0700555 count = self.job_counter.get(name, 0)
556 count += 1
557 self.job_counter[name] = count
James E. Blaird466dc42012-07-31 10:42:56 -0700558
559 queue_count = self.queue_counter
560 self.queue_counter += 1
James E. Blairb0fcae42012-07-17 11:12:10 -0700561 job = FakeJenkinsJob(self, self.callback, name, count, parameters)
James E. Blaird466dc42012-07-31 10:42:56 -0700562 job.queue_id = queue_count
563
James E. Blairb0fcae42012-07-17 11:12:10 -0700564 self.all_jobs.append(job)
565 job.start()
566
James E. Blaird466dc42012-07-31 10:42:56 -0700567 def stop_build(self, name, number):
568 for job in self.all_jobs:
569 if job.name == name and job.number == number:
570 job.aborted = True
571 job.release()
572 return
573
574 def cancel_queue(self, id):
575 for job in self.queue:
576 if job.queue_id == id:
577 job.canceled = True
578 job.release()
579 return
580
581 def get_queue_info(self):
582 items = []
James E. Blair1490eba2013-03-06 19:14:00 -0800583 for job in self.queue[:]:
584 self.log.debug("Queue info: %s %s" % (job.name,
585 job.parameters['UUID']))
James E. Blaird466dc42012-07-31 10:42:56 -0700586 paramstr = ''
587 paramlst = []
588 d = {'actions': [{'parameters': paramlst},
589 {'causes': [{'shortDescription':
590 'Started by user Jenkins',
591 'userId': 'jenkins',
592 'userName': 'Jenkins'}]}],
593 'blocked': False,
594 'buildable': True,
595 'buildableStartMilliseconds': (job.created * 1000) + 5,
596 'id': job.queue_id,
597 'inQueueSince': (job.created * 1000),
598 'params': paramstr,
599 'stuck': False,
600 'task': {'color': 'blue',
601 'name': job.name,
602 'url': 'https://server/job/%s/' % job.name},
603 'why': 'Waiting for next available executor'}
604 for k, v in job.parameters.items():
605 paramstr += "\n(StringParameterValue) %s='%s'" % (k, v)
606 pd = {'name': k, 'value': v}
607 paramlst.append(pd)
608 items.append(d)
609 return items
610
James E. Blairb0fcae42012-07-17 11:12:10 -0700611 def set_build_description(self, *args, **kw):
612 pass
613
614
615class FakeJenkinsCallback(zuul.launcher.jenkins.JenkinsCallback):
616 def start(self):
617 pass
618
619
James E. Blair8cc15a82012-08-01 11:17:57 -0700620class FakeURLOpener(object):
621 def __init__(self, fake_gerrit, url):
622 self.fake_gerrit = fake_gerrit
623 self.url = url
624
625 def read(self):
626 res = urlparse.urlparse(self.url)
627 path = res.path
628 project = '/'.join(path.split('/')[2:-2])
James E. Blair35956c52012-09-17 22:13:36 +0200629 ret = '001e# service=git-upload-pack\n'
630 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
631 'multi_ack thin-pack side-band side-band-64k ofs-delta '
632 'shallow no-progress include-tag multi_ack_detailed no-done\n')
James E. Blair1dbd5082012-08-23 15:12:15 -0700633 path = os.path.join(UPSTREAM_ROOT, project)
James E. Blairdaabed22012-08-15 15:38:57 -0700634 repo = git.Repo(path)
635 for ref in repo.refs:
James E. Blair35956c52012-09-17 22:13:36 +0200636 r = ref.object.hexsha + ' ' + ref.path + '\n'
637 ret += '%04x%s' % (len(r) + 4, r)
638 ret += '0000'
James E. Blair8cc15a82012-08-01 11:17:57 -0700639 return ret
640
641
James E. Blair4886cc12012-07-18 15:39:41 -0700642class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit):
643 def getGitUrl(self, project):
James E. Blair1dbd5082012-08-23 15:12:15 -0700644 return os.path.join(UPSTREAM_ROOT, project.name)
James E. Blair4886cc12012-07-18 15:39:41 -0700645
646
James E. Blair412e5582013-04-22 15:50:12 -0700647class FakeStatsd(threading.Thread):
648 def __init__(self):
649 threading.Thread.__init__(self)
650 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
651 self.sock.bind(('', 0))
652 self.port = self.sock.getsockname()[1]
653 self.wake_read, self.wake_write = os.pipe()
654 self.stats = []
655
656 def run(self):
657 while True:
658 poll = select.poll()
659 poll.register(self.sock, select.POLLIN)
660 poll.register(self.wake_read, select.POLLIN)
661 ret = poll.poll()
662 for (fd, event) in ret:
663 if fd == self.sock.fileno():
664 data = self.sock.recvfrom(1024)
665 if not data:
666 return
667 self.stats.append(data[0])
668 if fd == self.wake_read:
669 return
670
671 def stop(self):
672 os.write(self.wake_write, '1\n')
673
674
James E. Blairb0fcae42012-07-17 11:12:10 -0700675class testScheduler(unittest.TestCase):
676 log = logging.getLogger("zuul.test")
677
678 def setUp(self):
James E. Blair1dbd5082012-08-23 15:12:15 -0700679 if os.path.exists(TEST_ROOT):
680 shutil.rmtree(TEST_ROOT)
681 os.makedirs(TEST_ROOT)
682 os.makedirs(UPSTREAM_ROOT)
683 os.makedirs(GIT_ROOT)
James E. Blair4886cc12012-07-18 15:39:41 -0700684
685 # For each project in config:
686 init_repo("org/project")
687 init_repo("org/project1")
688 init_repo("org/project2")
James E. Blair127bc182012-08-28 15:55:15 -0700689 init_repo("org/project3")
James E. Blair7f71c802012-08-22 13:04:32 -0700690 init_repo("org/one-job-project")
James E. Blair4ec821f2012-08-23 15:28:28 -0700691 init_repo("org/nonvoting-project")
James E. Blairb0fcae42012-07-17 11:12:10 -0700692 self.config = CONFIG
James E. Blair412e5582013-04-22 15:50:12 -0700693
694 self.statsd = FakeStatsd()
695 os.environ['STATSD_HOST'] = 'localhost'
696 os.environ['STATSD_PORT'] = str(self.statsd.port)
697 self.statsd.start()
698 # the statsd client object is configured in the statsd module import
699 reload(statsd)
700 reload(zuul.scheduler)
701
James E. Blairb0fcae42012-07-17 11:12:10 -0700702 self.sched = zuul.scheduler.Scheduler()
703
704 def jenkinsFactory(*args, **kw):
705 self.fake_jenkins = FakeJenkins()
706 return self.fake_jenkins
707
708 def jenkinsCallbackFactory(*args, **kw):
709 self.fake_jenkins_callback = FakeJenkinsCallback(*args, **kw)
710 return self.fake_jenkins_callback
711
James E. Blair8cc15a82012-08-01 11:17:57 -0700712 def URLOpenerFactory(*args, **kw):
713 args = [self.fake_gerrit] + list(args)
714 return FakeURLOpener(*args, **kw)
715
James E. Blairb0fcae42012-07-17 11:12:10 -0700716 zuul.launcher.jenkins.ExtendedJenkins = jenkinsFactory
717 zuul.launcher.jenkins.JenkinsCallback = jenkinsCallbackFactory
James E. Blair8cc15a82012-08-01 11:17:57 -0700718 urllib2.urlopen = URLOpenerFactory
James E. Blairb0fcae42012-07-17 11:12:10 -0700719 self.jenkins = zuul.launcher.jenkins.Jenkins(self.config, self.sched)
720 self.fake_jenkins.callback = self.fake_jenkins_callback
721
722 zuul.lib.gerrit.Gerrit = FakeGerrit
723
James E. Blair4886cc12012-07-18 15:39:41 -0700724 self.gerrit = FakeGerritTrigger(self.config, self.sched)
James E. Blair8cc15a82012-08-01 11:17:57 -0700725 self.gerrit.replication_timeout = 1.5
726 self.gerrit.replication_retry_interval = 0.5
James E. Blairb0fcae42012-07-17 11:12:10 -0700727 self.fake_gerrit = self.gerrit.gerrit
728
729 self.sched.setLauncher(self.jenkins)
730 self.sched.setTrigger(self.gerrit)
731
732 self.sched.start()
733 self.sched.reconfigure(self.config)
734 self.sched.resume()
735
736 def tearDown(self):
737 self.jenkins.stop()
738 self.gerrit.stop()
739 self.sched.stop()
740 self.sched.join()
James E. Blair412e5582013-04-22 15:50:12 -0700741 self.statsd.stop()
742 self.statsd.join()
James E. Blair1dbd5082012-08-23 15:12:15 -0700743 #shutil.rmtree(TEST_ROOT)
James E. Blairb0fcae42012-07-17 11:12:10 -0700744
745 def waitUntilSettled(self):
746 self.log.debug("Waiting until settled...")
747 start = time.time()
748 while True:
749 if time.time() - start > 10:
750 print 'queue status:',
751 print self.sched.trigger_event_queue.empty(),
752 print self.sched.result_event_queue.empty(),
753 print self.fake_gerrit.event_queue.empty(),
754 raise Exception("Timeout waiting for Zuul to settle")
James E. Blairff791972013-01-09 11:45:43 -0800755 # Make sure our fake jenkins doesn't end any jobs
756 # (and therefore, emit events) while we're checking
757 self.fake_jenkins.lock.acquire()
758 # Join ensures that the queue is empty _and_ events have been
759 # processed
James E. Blairb0fcae42012-07-17 11:12:10 -0700760 self.fake_gerrit.event_queue.join()
James E. Blairff791972013-01-09 11:45:43 -0800761 self.sched.trigger_event_queue.join()
762 self.sched.result_event_queue.join()
James E. Blairb0fcae42012-07-17 11:12:10 -0700763 if (self.sched.trigger_event_queue.empty() and
764 self.sched.result_event_queue.empty() and
765 self.fake_gerrit.event_queue.empty() and
766 self.fake_jenkins.fakeAllWaiting()):
James E. Blairff791972013-01-09 11:45:43 -0800767 self.fake_jenkins.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700768 self.log.debug("...settled.")
769 return
James E. Blairff791972013-01-09 11:45:43 -0800770 self.fake_jenkins.lock.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700771 self.sched.wake_event.wait(0.1)
772
James E. Blaird466dc42012-07-31 10:42:56 -0700773 def countJobResults(self, jobs, result):
James E. Blair0018a6c2013-02-27 14:11:45 -0800774 jobs = filter(lambda x: x.result == result, jobs)
James E. Blaird466dc42012-07-31 10:42:56 -0700775 return len(jobs)
776
James E. Blaire0487072012-08-29 17:38:31 -0700777 def assertEmptyQueues(self):
778 # Make sure there are no orphaned jobs
779 for pipeline in self.sched.pipelines.values():
780 for queue in pipeline.queues:
781 if len(queue.queue) != 0:
James E. Blairf62d4282012-12-31 17:01:50 -0800782 print 'pipeline %s queue %s contents %s' % (
783 pipeline.name, queue.name, queue.queue)
James E. Blaire0487072012-08-29 17:38:31 -0700784 assert len(queue.queue) == 0
785 if len(queue.severed_heads) != 0:
786 print 'heads', queue.severed_heads
787 assert len(queue.severed_heads) == 0
788
James E. Blair412e5582013-04-22 15:50:12 -0700789 def assertReportedStat(self, key, value=None):
790 start = time.time()
791 while time.time() < (start + 5):
792 for stat in self.statsd.stats:
793 k, v = stat.split(':')
794 if key == k:
795 if value is None:
796 return
797 if value == v:
798 return
799 time.sleep(0.1)
800
801 pprint.pprint(self.statsd.stats)
802 raise Exception("Key %s not found in reported stats" % key)
803
James E. Blairb0fcae42012-07-17 11:12:10 -0700804 def test_jobs_launched(self):
805 "Test that jobs are launched and a change is merged"
806 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8c803f82012-07-31 16:25:42 -0700807 A.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700808 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
809 self.waitUntilSettled()
810 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -0800811 job_names = [x.name for x in jobs]
James E. Blairb0fcae42012-07-17 11:12:10 -0700812 assert 'project-merge' in job_names
813 assert 'project-test1' in job_names
814 assert 'project-test2' in job_names
James E. Blair0018a6c2013-02-27 14:11:45 -0800815 assert jobs[0].result == 'SUCCESS'
816 assert jobs[1].result == 'SUCCESS'
817 assert jobs[2].result == 'SUCCESS'
James E. Blairb0fcae42012-07-17 11:12:10 -0700818 assert A.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700819 assert A.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700820 self.assertEmptyQueues()
James E. Blairb0fcae42012-07-17 11:12:10 -0700821
James E. Blair412e5582013-04-22 15:50:12 -0700822 self.assertReportedStat('gerrit.event.comment-added', '1|c')
823 self.assertReportedStat('zuul.pipeline.gate.current_changes', '1|g')
824 self.assertReportedStat('zuul.job.project-merge')
825 self.assertReportedStat('zuul.pipeline.gate.resident_time')
826 self.assertReportedStat('zuul.pipeline.gate.total_changes', '1|c')
827 self.assertReportedStat(
828 'zuul.pipeline.gate.org.project.resident_time')
829 self.assertReportedStat(
830 'zuul.pipeline.gate.org.project.total_changes', '1|c')
831
James E. Blairb0fcae42012-07-17 11:12:10 -0700832 def test_parallel_changes(self):
833 "Test that changes are tested in parallel and merged in series"
834 self.fake_jenkins.hold_jobs_in_build = True
835 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
836 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
837 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700838 A.addApproval('CRVW', 2)
839 B.addApproval('CRVW', 2)
840 C.addApproval('CRVW', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700841
842 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
843 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
844 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
845
846 self.waitUntilSettled()
847 jobs = self.fake_jenkins.all_jobs
848 assert len(jobs) == 1
849 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700850 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700851
852 self.fake_jenkins.fakeRelease('.*-merge')
853 self.waitUntilSettled()
854 assert len(jobs) == 3
855 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700856 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700857 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700858 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700859 assert jobs[2].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700860 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700861
862 self.fake_jenkins.fakeRelease('.*-merge')
863 self.waitUntilSettled()
864 assert len(jobs) == 5
865 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700866 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700867 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700868 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700869
870 assert jobs[2].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700871 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700872 assert jobs[3].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700873 assert job_has_changes(jobs[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700874
875 assert jobs[4].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700876 assert job_has_changes(jobs[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700877
878 self.fake_jenkins.fakeRelease('.*-merge')
879 self.waitUntilSettled()
880 assert len(jobs) == 6
881 assert jobs[0].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700882 assert job_has_changes(jobs[0], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700883 assert jobs[1].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700884 assert job_has_changes(jobs[1], A)
James E. Blairb0fcae42012-07-17 11:12:10 -0700885
886 assert jobs[2].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700887 assert job_has_changes(jobs[2], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700888 assert jobs[3].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700889 assert job_has_changes(jobs[3], A, B)
James E. Blairb0fcae42012-07-17 11:12:10 -0700890
891 assert jobs[4].name == 'project-test1'
James E. Blair4886cc12012-07-18 15:39:41 -0700892 assert job_has_changes(jobs[4], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700893 assert jobs[5].name == 'project-test2'
James E. Blair4886cc12012-07-18 15:39:41 -0700894 assert job_has_changes(jobs[5], A, B, C)
James E. Blairb0fcae42012-07-17 11:12:10 -0700895
896 self.fake_jenkins.hold_jobs_in_build = False
897 self.fake_jenkins.fakeRelease()
898 self.waitUntilSettled()
899 assert len(jobs) == 0
900
901 jobs = self.fake_jenkins.job_history
902 assert len(jobs) == 9
903 assert A.data['status'] == 'MERGED'
904 assert B.data['status'] == 'MERGED'
905 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700906 assert A.reported == 2
907 assert B.reported == 2
908 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700909 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700910
911 def test_failed_changes(self):
912 "Test that a change behind a failed change is retested"
913 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
914 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8c803f82012-07-31 16:25:42 -0700915 A.addApproval('CRVW', 2)
916 B.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700917
918 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
919 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
920
James E. Blair4886cc12012-07-18 15:39:41 -0700921 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700922
923 self.waitUntilSettled()
924 jobs = self.fake_jenkins.job_history
925 assert len(jobs) > 6
926 assert A.data['status'] == 'NEW'
927 assert B.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700928 assert A.reported == 2
929 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700930 self.assertEmptyQueues()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700931
932 def test_independent_queues(self):
933 "Test that changes end up in the right queues"
934 self.fake_jenkins.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900935 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700936 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
937 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700938 A.addApproval('CRVW', 2)
939 B.addApproval('CRVW', 2)
940 C.addApproval('CRVW', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700941
942 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
943 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
944 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
945
946 jobs = self.fake_jenkins.all_jobs
947 self.waitUntilSettled()
948
949 # There should be one merge job at the head of each queue running
950 assert len(jobs) == 2
951 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700952 assert job_has_changes(jobs[0], A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700953 assert jobs[1].name == 'project1-merge'
James E. Blair4886cc12012-07-18 15:39:41 -0700954 assert job_has_changes(jobs[1], B)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700955
956 # Release the current merge jobs
957 self.fake_jenkins.fakeRelease('.*-merge')
958 self.waitUntilSettled()
959 # Release the merge job for project2 which is behind project1
960 self.fake_jenkins.fakeRelease('.*-merge')
961 self.waitUntilSettled()
962
963 # All the test jobs should be running:
964 # project1 (3) + project2 (3) + project (2) = 8
965 assert len(jobs) == 8
966
967 self.fake_jenkins.fakeRelease()
968 self.waitUntilSettled()
969 assert len(jobs) == 0
970
971 jobs = self.fake_jenkins.job_history
972 assert len(jobs) == 11
973 assert A.data['status'] == 'MERGED'
974 assert B.data['status'] == 'MERGED'
975 assert C.data['status'] == 'MERGED'
James E. Blaird466dc42012-07-31 10:42:56 -0700976 assert A.reported == 2
977 assert B.reported == 2
978 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -0700979 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -0700980
981 def test_failed_change_at_head(self):
982 "Test that if a change at the head fails, jobs behind it are canceled"
983 self.fake_jenkins.hold_jobs_in_build = True
984
985 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
986 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
987 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -0700988 A.addApproval('CRVW', 2)
989 B.addApproval('CRVW', 2)
990 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700991
James E. Blair4886cc12012-07-18 15:39:41 -0700992 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700993
994 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
995 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
996 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
997
998 self.waitUntilSettled()
999 jobs = self.fake_jenkins.all_jobs
1000 finished_jobs = self.fake_jenkins.job_history
1001
1002 assert len(jobs) == 1
1003 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -07001004 assert job_has_changes(jobs[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -07001005
1006 self.fake_jenkins.fakeRelease('.*-merge')
1007 self.waitUntilSettled()
1008 self.fake_jenkins.fakeRelease('.*-merge')
1009 self.waitUntilSettled()
1010 self.fake_jenkins.fakeRelease('.*-merge')
1011 self.waitUntilSettled()
1012
1013 assert len(jobs) == 6
1014 assert jobs[0].name == 'project-test1'
1015 assert jobs[1].name == 'project-test2'
1016 assert jobs[2].name == 'project-test1'
1017 assert jobs[3].name == 'project-test2'
1018 assert jobs[4].name == 'project-test1'
1019 assert jobs[5].name == 'project-test2'
1020
1021 jobs[0].release()
1022 self.waitUntilSettled()
1023
James E. Blairec590122012-08-22 15:19:31 -07001024 assert len(jobs) == 2 # project-test2, project-merge for B
James E. Blaird466dc42012-07-31 10:42:56 -07001025 assert self.countJobResults(finished_jobs, 'ABORTED') == 4
1026
1027 self.fake_jenkins.hold_jobs_in_build = False
1028 self.fake_jenkins.fakeRelease()
1029 self.waitUntilSettled()
1030
James E. Blair0018a6c2013-02-27 14:11:45 -08001031 for x in jobs:
1032 print x
1033 for x in finished_jobs:
1034 print x
1035
James E. Blaird466dc42012-07-31 10:42:56 -07001036 assert len(jobs) == 0
1037 assert len(finished_jobs) == 15
1038 assert A.data['status'] == 'NEW'
1039 assert B.data['status'] == 'MERGED'
1040 assert C.data['status'] == 'MERGED'
1041 assert A.reported == 2
1042 assert B.reported == 2
1043 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001044 self.assertEmptyQueues()
James E. Blaird466dc42012-07-31 10:42:56 -07001045
1046 def test_failed_change_at_head_with_queue(self):
1047 "Test that if a change at the head fails, queued jobs are canceled"
1048 self.fake_jenkins.hold_jobs_in_queue = True
1049
1050 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1051 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1052 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8c803f82012-07-31 16:25:42 -07001053 A.addApproval('CRVW', 2)
1054 B.addApproval('CRVW', 2)
1055 C.addApproval('CRVW', 2)
James E. Blaird466dc42012-07-31 10:42:56 -07001056
James E. Blair4886cc12012-07-18 15:39:41 -07001057 self.fake_jenkins.fakeAddFailTest('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -07001058
1059 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1060 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1061 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1062
1063 self.waitUntilSettled()
1064 jobs = self.fake_jenkins.all_jobs
1065 finished_jobs = self.fake_jenkins.job_history
1066 queue = self.fake_jenkins.queue
1067
1068 assert len(jobs) == 1
1069 assert len(queue) == 1
1070 assert jobs[0].name == 'project-merge'
James E. Blair4886cc12012-07-18 15:39:41 -07001071 assert job_has_changes(jobs[0], A)
James E. Blaird466dc42012-07-31 10:42:56 -07001072
1073 self.fake_jenkins.fakeRelease('.*-merge')
1074 self.waitUntilSettled()
1075 self.fake_jenkins.fakeRelease('.*-merge')
1076 self.waitUntilSettled()
1077 self.fake_jenkins.fakeRelease('.*-merge')
1078 self.waitUntilSettled()
1079
1080 assert len(jobs) == 6
1081 assert len(queue) == 6
1082 assert jobs[0].name == 'project-test1'
1083 assert jobs[1].name == 'project-test2'
1084 assert jobs[2].name == 'project-test1'
1085 assert jobs[3].name == 'project-test2'
1086 assert jobs[4].name == 'project-test1'
1087 assert jobs[5].name == 'project-test2'
1088
1089 jobs[0].release()
1090 self.waitUntilSettled()
1091
James E. Blairec590122012-08-22 15:19:31 -07001092 assert len(jobs) == 2 # project-test2, project-merge for B
1093 assert len(queue) == 2
James E. Blaird466dc42012-07-31 10:42:56 -07001094 assert self.countJobResults(finished_jobs, 'ABORTED') == 0
1095
1096 self.fake_jenkins.hold_jobs_in_queue = False
1097 self.fake_jenkins.fakeRelease()
1098 self.waitUntilSettled()
1099
1100 assert len(jobs) == 0
1101 assert len(finished_jobs) == 11
1102 assert A.data['status'] == 'NEW'
1103 assert B.data['status'] == 'MERGED'
1104 assert C.data['status'] == 'MERGED'
1105 assert A.reported == 2
1106 assert B.reported == 2
1107 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001108 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001109
1110 def test_patch_order(self):
1111 "Test that dependent patches are tested in the right order"
1112 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1113 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1114 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1115 A.addApproval('CRVW', 2)
1116 B.addApproval('CRVW', 2)
1117 C.addApproval('CRVW', 2)
1118
1119 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
1120 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1121 M2.setMerged()
1122 M1.setMerged()
1123
1124 # C -> B -> A -> M1 -> M2
1125 # M2 is here to make sure it is never queried. If it is, it
1126 # means zuul is walking down the entire history of merged
1127 # changes.
1128
1129 C.setDependsOn(B, 1)
1130 B.setDependsOn(A, 1)
1131 A.setDependsOn(M1, 1)
1132 M1.setDependsOn(M2, 1)
1133
1134 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1135
1136 self.waitUntilSettled()
1137
1138 assert A.data['status'] == 'NEW'
1139 assert B.data['status'] == 'NEW'
1140 assert C.data['status'] == 'NEW'
1141
1142 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1143 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1144
1145 self.waitUntilSettled()
1146 assert M2.queried == 0
1147 assert A.data['status'] == 'MERGED'
1148 assert B.data['status'] == 'MERGED'
1149 assert C.data['status'] == 'MERGED'
1150 assert A.reported == 2
1151 assert B.reported == 2
1152 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001153 self.assertEmptyQueues()
James E. Blair8c803f82012-07-31 16:25:42 -07001154
1155 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001156 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001157 # TODO: move to test_gerrit (this is a unit test!)
1158 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4aea70c2012-07-26 14:23:24 -07001159 a = self.sched.trigger.getChange(1, 2)
1160 mgr = self.sched.pipelines['gate'].manager
1161 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001162
1163 A.addApproval('CRVW', 2)
James E. Blair4aea70c2012-07-26 14:23:24 -07001164 a = self.sched.trigger.getChange(1, 2)
1165 assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blair8c803f82012-07-31 16:25:42 -07001166
1167 A.addApproval('APRV', 1)
James E. Blair4aea70c2012-07-26 14:23:24 -07001168 a = self.sched.trigger.getChange(1, 2)
1169 assert self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds())
James E. Blaire0487072012-08-29 17:38:31 -07001170 self.assertEmptyQueues()
James E. Blair4886cc12012-07-18 15:39:41 -07001171
1172 def test_build_configuration(self):
1173 "Test that zuul merges the right commits for testing"
1174 self.fake_jenkins.hold_jobs_in_queue = True
1175 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1176 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1177 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1178 A.addApproval('CRVW', 2)
1179 B.addApproval('CRVW', 2)
1180 C.addApproval('CRVW', 2)
1181 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1182 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1183 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1184 self.waitUntilSettled()
1185
1186 jobs = self.fake_jenkins.all_jobs
1187
1188 self.fake_jenkins.fakeRelease('.*-merge')
1189 self.waitUntilSettled()
1190 self.fake_jenkins.fakeRelease('.*-merge')
1191 self.waitUntilSettled()
1192 self.fake_jenkins.fakeRelease('.*-merge')
1193 self.waitUntilSettled()
James E. Blair1dbd5082012-08-23 15:12:15 -07001194
James E. Blair4886cc12012-07-18 15:39:41 -07001195 ref = jobs[-1].parameters['ZUUL_REF']
1196 self.fake_jenkins.hold_jobs_in_queue = False
1197 self.fake_jenkins.fakeRelease()
James E. Blair973721f2012-08-15 10:19:43 -07001198 self.waitUntilSettled()
James E. Blair4886cc12012-07-18 15:39:41 -07001199
James E. Blair1dbd5082012-08-23 15:12:15 -07001200 path = os.path.join(GIT_ROOT, "org/project")
James E. Blair4886cc12012-07-18 15:39:41 -07001201 repo = git.Repo(path)
1202 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1203 repo_messages.reverse()
James E. Blair4886cc12012-07-18 15:39:41 -07001204 correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1']
1205 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001206 self.assertEmptyQueues()
James E. Blair973721f2012-08-15 10:19:43 -07001207
1208 def test_build_configuration_conflict(self):
1209 "Test that merge conflicts are handled"
1210 self.fake_jenkins.hold_jobs_in_queue = True
1211 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1212 A.addPatchset(['conflict'])
1213 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1214 B.addPatchset(['conflict'])
1215 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1216 A.addApproval('CRVW', 2)
1217 B.addApproval('CRVW', 2)
1218 C.addApproval('CRVW', 2)
1219 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1220 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1221 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1222 self.waitUntilSettled()
1223
1224 jobs = self.fake_jenkins.all_jobs
1225
1226 self.fake_jenkins.fakeRelease('.*-merge')
1227 self.waitUntilSettled()
1228 self.fake_jenkins.fakeRelease('.*-merge')
1229 self.waitUntilSettled()
1230 self.fake_jenkins.fakeRelease('.*-merge')
1231 self.waitUntilSettled()
1232 ref = jobs[-1].parameters['ZUUL_REF']
1233 self.fake_jenkins.hold_jobs_in_queue = False
1234 self.fake_jenkins.fakeRelease()
1235 self.waitUntilSettled()
1236
1237 assert A.data['status'] == 'MERGED'
1238 assert B.data['status'] == 'NEW'
1239 assert C.data['status'] == 'MERGED'
1240 assert A.reported == 2
1241 assert B.reported == 2
1242 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001243 self.assertEmptyQueues()
James E. Blairdaabed22012-08-15 15:38:57 -07001244
1245 def test_post(self):
1246 "Test that post jobs run"
Zhongyue Luo5d556072012-09-21 02:00:47 +09001247 e = {
1248 "type": "ref-updated",
1249 "submitter": {
1250 "name": "User Name",
1251 },
1252 "refUpdate": {
1253 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1254 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1255 "refName": "master",
1256 "project": "org/project",
1257 }
1258 }
James E. Blairdaabed22012-08-15 15:38:57 -07001259 self.fake_gerrit.addEvent(e)
1260 self.waitUntilSettled()
1261
1262 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001263 job_names = [x.name for x in jobs]
James E. Blairdaabed22012-08-15 15:38:57 -07001264 assert len(jobs) == 1
1265 assert 'project-post' in job_names
James E. Blaire0487072012-08-29 17:38:31 -07001266 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001267
1268 def test_build_configuration_branch(self):
1269 "Test that the right commits are on alternate branches"
1270 self.fake_jenkins.hold_jobs_in_queue = True
1271 A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A')
1272 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1273 C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C')
1274 A.addApproval('CRVW', 2)
1275 B.addApproval('CRVW', 2)
1276 C.addApproval('CRVW', 2)
1277 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1278 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1279 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1280 self.waitUntilSettled()
1281
1282 jobs = self.fake_jenkins.all_jobs
1283
1284 self.fake_jenkins.fakeRelease('.*-merge')
1285 self.waitUntilSettled()
1286 self.fake_jenkins.fakeRelease('.*-merge')
1287 self.waitUntilSettled()
1288 self.fake_jenkins.fakeRelease('.*-merge')
1289 self.waitUntilSettled()
1290 ref = jobs[-1].parameters['ZUUL_REF']
1291 self.fake_jenkins.hold_jobs_in_queue = False
1292 self.fake_jenkins.fakeRelease()
1293 self.waitUntilSettled()
1294
James E. Blair1dbd5082012-08-23 15:12:15 -07001295 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001296 repo = git.Repo(path)
1297 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
1298 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001299 correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1']
1300 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001301 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001302
1303 def test_build_configuration_branch_interaction(self):
1304 "Test that switching between branches works"
1305 self.test_build_configuration()
1306 self.test_build_configuration_branch()
1307 # C has been merged, undo that
James E. Blair1dbd5082012-08-23 15:12:15 -07001308 path = os.path.join(UPSTREAM_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001309 repo = git.Repo(path)
1310 repo.heads.master.commit = repo.commit('init')
1311 self.test_build_configuration()
James E. Blaire0487072012-08-29 17:38:31 -07001312 self.assertEmptyQueues()
James E. Blairc6294a52012-08-17 10:19:48 -07001313
1314 def test_build_configuration_multi_branch(self):
1315 "Test that dependent changes on multiple branches are merged"
1316 self.fake_jenkins.hold_jobs_in_queue = True
1317 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1318 B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B')
1319 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1320 A.addApproval('CRVW', 2)
1321 B.addApproval('CRVW', 2)
1322 C.addApproval('CRVW', 2)
1323 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1324 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1325 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1326 self.waitUntilSettled()
1327
1328 jobs = self.fake_jenkins.all_jobs
1329
1330 self.fake_jenkins.fakeRelease('.*-merge')
1331 self.waitUntilSettled()
1332 ref_mp = jobs[-1].parameters['ZUUL_REF']
1333 self.fake_jenkins.fakeRelease('.*-merge')
1334 self.waitUntilSettled()
1335 self.fake_jenkins.fakeRelease('.*-merge')
1336 self.waitUntilSettled()
1337 ref_master = jobs[-1].parameters['ZUUL_REF']
1338 self.fake_jenkins.hold_jobs_in_queue = False
1339 self.fake_jenkins.fakeRelease()
1340 self.waitUntilSettled()
1341
James E. Blair1dbd5082012-08-23 15:12:15 -07001342 path = os.path.join(GIT_ROOT, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001343 repo = git.Repo(path)
1344
1345 repo_messages = [c.message.strip()
1346 for c in repo.iter_commits(ref_master)]
1347 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001348 correct_messages = ['initial commit', 'A-1', 'C-1']
1349 assert repo_messages == correct_messages
1350
1351 repo_messages = [c.message.strip()
1352 for c in repo.iter_commits(ref_mp)]
1353 repo_messages.reverse()
James E. Blairc6294a52012-08-17 10:19:48 -07001354 correct_messages = ['initial commit', 'mp commit', 'B-1']
1355 assert repo_messages == correct_messages
James E. Blaire0487072012-08-29 17:38:31 -07001356 self.assertEmptyQueues()
James E. Blair7f71c802012-08-22 13:04:32 -07001357
1358 def test_one_job_project(self):
1359 "Test that queueing works with one job"
1360 A = self.fake_gerrit.addFakeChange('org/one-job-project',
1361 'master', 'A')
1362 B = self.fake_gerrit.addFakeChange('org/one-job-project',
1363 'master', 'B')
1364 A.addApproval('CRVW', 2)
1365 B.addApproval('CRVW', 2)
1366 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1367 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1368 self.waitUntilSettled()
1369
1370 jobs = self.fake_jenkins.all_jobs
1371 finished_jobs = self.fake_jenkins.job_history
James E. Blair7f71c802012-08-22 13:04:32 -07001372
1373 assert A.data['status'] == 'MERGED'
1374 assert A.reported == 2
1375 assert B.data['status'] == 'MERGED'
1376 assert B.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001377 self.assertEmptyQueues()
James E. Blaircaec0c52012-08-22 14:52:22 -07001378
1379 def test_dependent_changes_dequeue(self):
1380 "Test that dependent patches are not needlessly tested"
1381 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1382 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1383 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1384 A.addApproval('CRVW', 2)
1385 B.addApproval('CRVW', 2)
1386 C.addApproval('CRVW', 2)
1387
1388 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1389 M1.setMerged()
1390
1391 # C -> B -> A -> M1
1392
1393 C.setDependsOn(B, 1)
1394 B.setDependsOn(A, 1)
1395 A.setDependsOn(M1, 1)
1396
1397 self.fake_jenkins.fakeAddFailTest('project-merge', A)
1398
1399 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1400 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1401 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1402
1403 self.waitUntilSettled()
1404
1405 jobs = self.fake_jenkins.all_jobs
1406 finished_jobs = self.fake_jenkins.job_history
1407
James E. Blair127bc182012-08-28 15:55:15 -07001408 for x in jobs:
1409 print x
1410 for x in finished_jobs:
1411 print x
1412
James E. Blairec590122012-08-22 15:19:31 -07001413 assert A.data['status'] == 'NEW'
1414 assert A.reported == 2
1415 assert B.data['status'] == 'NEW'
1416 assert B.reported == 2
1417 assert C.data['status'] == 'NEW'
1418 assert C.reported == 2
1419 assert len(finished_jobs) == 1
James E. Blaire0487072012-08-29 17:38:31 -07001420 self.assertEmptyQueues()
James E. Blairec590122012-08-22 15:19:31 -07001421
1422 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001423 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001424 # If it's dequeued more than once, we should see extra
1425 # aborted jobs.
1426 self.fake_jenkins.hold_jobs_in_build = True
1427
1428 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1429 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1430 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
1431 A.addApproval('CRVW', 2)
1432 B.addApproval('CRVW', 2)
1433 C.addApproval('CRVW', 2)
1434
1435 self.fake_jenkins.fakeAddFailTest('project1-test1', A)
1436 self.fake_jenkins.fakeAddFailTest('project1-test2', A)
1437 self.fake_jenkins.fakeAddFailTest('project1-project2-integration', A)
1438
1439 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1440 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1441 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1442
1443 self.waitUntilSettled()
1444 jobs = self.fake_jenkins.all_jobs
1445 finished_jobs = self.fake_jenkins.job_history
1446
1447 assert len(jobs) == 1
1448 assert jobs[0].name == 'project1-merge'
1449 assert job_has_changes(jobs[0], A)
1450
1451 self.fake_jenkins.fakeRelease('.*-merge')
1452 self.waitUntilSettled()
1453 self.fake_jenkins.fakeRelease('.*-merge')
1454 self.waitUntilSettled()
1455 self.fake_jenkins.fakeRelease('.*-merge')
1456 self.waitUntilSettled()
1457
1458 assert len(jobs) == 9
1459 assert jobs[0].name == 'project1-test1'
1460 assert jobs[1].name == 'project1-test2'
1461 assert jobs[2].name == 'project1-project2-integration'
1462 assert jobs[3].name == 'project1-test1'
1463 assert jobs[4].name == 'project1-test2'
1464 assert jobs[5].name == 'project1-project2-integration'
1465 assert jobs[6].name == 'project1-test1'
1466 assert jobs[7].name == 'project1-test2'
1467 assert jobs[8].name == 'project1-project2-integration'
1468
1469 jobs[0].release()
1470 self.waitUntilSettled()
1471
1472 assert len(jobs) == 3 # test2, integration, merge for B
1473 assert self.countJobResults(finished_jobs, 'ABORTED') == 6
1474
1475 self.fake_jenkins.hold_jobs_in_build = False
1476 self.fake_jenkins.fakeRelease()
1477 self.waitUntilSettled()
1478
1479 assert len(jobs) == 0
1480 assert len(finished_jobs) == 20
James E. Blaircaec0c52012-08-22 14:52:22 -07001481
1482 assert A.data['status'] == 'NEW'
James E. Blairec590122012-08-22 15:19:31 -07001483 assert B.data['status'] == 'MERGED'
1484 assert C.data['status'] == 'MERGED'
1485 assert A.reported == 2
1486 assert B.reported == 2
1487 assert C.reported == 2
James E. Blaire0487072012-08-29 17:38:31 -07001488 self.assertEmptyQueues()
James E. Blair4ec821f2012-08-23 15:28:28 -07001489
1490 def test_nonvoting_job(self):
1491 "Test that non-voting jobs don't vote."
1492 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1493 'master', 'A')
1494 A.addApproval('CRVW', 2)
1495 self.fake_jenkins.fakeAddFailTest('nonvoting-project-test2', A)
1496 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1497
1498 self.waitUntilSettled()
1499 jobs = self.fake_jenkins.all_jobs
1500 finished_jobs = self.fake_jenkins.job_history
1501
1502 assert A.data['status'] == 'MERGED'
1503 assert A.reported == 2
James E. Blair0018a6c2013-02-27 14:11:45 -08001504 assert finished_jobs[0].result == 'SUCCESS'
1505 assert finished_jobs[1].result == 'SUCCESS'
1506 assert finished_jobs[2].result == 'FAILURE'
James E. Blaire0487072012-08-29 17:38:31 -07001507 self.assertEmptyQueues()
1508
1509 def test_check_queue_success(self):
1510 "Test successful check queue jobs."
1511 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1512 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1513
1514 self.waitUntilSettled()
1515 jobs = self.fake_jenkins.all_jobs
1516 finished_jobs = self.fake_jenkins.job_history
1517
1518 assert A.data['status'] == 'NEW'
1519 assert A.reported == 1
James E. Blair0018a6c2013-02-27 14:11:45 -08001520 assert finished_jobs[0].result == 'SUCCESS'
1521 assert finished_jobs[1].result == 'SUCCESS'
1522 assert finished_jobs[2].result == 'SUCCESS'
James E. Blaire0487072012-08-29 17:38:31 -07001523 self.assertEmptyQueues()
1524
1525 def test_check_queue_failure(self):
1526 "Test failed check queue jobs."
1527 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1528 self.fake_jenkins.fakeAddFailTest('project-test2', A)
1529 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1530
1531 self.waitUntilSettled()
1532 jobs = self.fake_jenkins.all_jobs
1533 finished_jobs = self.fake_jenkins.job_history
1534
1535 assert A.data['status'] == 'NEW'
1536 assert A.reported == 1
James E. Blair0018a6c2013-02-27 14:11:45 -08001537 assert finished_jobs[0].result == 'SUCCESS'
1538 assert finished_jobs[1].result == 'SUCCESS'
1539 assert finished_jobs[2].result == 'FAILURE'
James E. Blaire0487072012-08-29 17:38:31 -07001540 self.assertEmptyQueues()
James E. Blair127bc182012-08-28 15:55:15 -07001541
1542 def test_dependent_behind_dequeue(self):
1543 "test that dependent changes behind dequeued changes work"
1544 # This complicated test is a reproduction of a real life bug
1545 self.sched.reconfigure(self.config)
1546 self.fake_jenkins.hold_jobs_in_build = True
1547
1548 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1549 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1550 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1551 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1552 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1553 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1554 D.setDependsOn(C, 1)
1555 E.setDependsOn(D, 1)
1556 A.addApproval('CRVW', 2)
1557 B.addApproval('CRVW', 2)
1558 C.addApproval('CRVW', 2)
1559 D.addApproval('CRVW', 2)
1560 E.addApproval('CRVW', 2)
1561 F.addApproval('CRVW', 2)
1562
1563 A.fail_merge = True
1564 jobs = self.fake_jenkins.all_jobs
1565 finished_jobs = self.fake_jenkins.job_history
1566
1567 # Change object re-use in the gerrit trigger is hidden if
1568 # changes are added in quick succession; waiting makes it more
1569 # like real life.
1570 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1571 self.waitUntilSettled()
1572 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1573 self.waitUntilSettled()
1574
1575 self.fake_jenkins.fakeRelease('.*-merge')
1576 self.waitUntilSettled()
1577 self.fake_jenkins.fakeRelease('.*-merge')
1578 self.waitUntilSettled()
1579
1580 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1581 self.waitUntilSettled()
1582 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1583 self.waitUntilSettled()
1584 self.fake_gerrit.addEvent(E.addApproval('APRV', 1))
1585 self.waitUntilSettled()
1586 self.fake_gerrit.addEvent(F.addApproval('APRV', 1))
1587 self.waitUntilSettled()
1588
1589 self.fake_jenkins.fakeRelease('.*-merge')
1590 self.waitUntilSettled()
1591 self.fake_jenkins.fakeRelease('.*-merge')
1592 self.waitUntilSettled()
1593 self.fake_jenkins.fakeRelease('.*-merge')
1594 self.waitUntilSettled()
1595 self.fake_jenkins.fakeRelease('.*-merge')
1596 self.waitUntilSettled()
1597
James E. Blair4aa1ad62012-10-05 12:39:26 -07001598 for x in jobs:
1599 print x
James E. Blair127bc182012-08-28 15:55:15 -07001600 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001601
1602 # Grab pointers to the jobs we want to release before
1603 # releasing any, because list indexes may change as
1604 # the jobs complete.
1605 a, b, c = jobs[:3]
1606 a.release()
1607 b.release()
1608 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001609 self.waitUntilSettled()
1610
1611 self.fake_jenkins.hold_jobs_in_build = False
1612 self.fake_jenkins.fakeRelease()
1613 self.waitUntilSettled()
1614
1615 for x in jobs:
1616 print x
1617 for x in finished_jobs:
1618 print x
1619 print self.sched.formatStatusHTML()
1620
1621 assert A.data['status'] == 'NEW'
1622 assert B.data['status'] == 'MERGED'
1623 assert C.data['status'] == 'MERGED'
1624 assert D.data['status'] == 'MERGED'
1625 assert E.data['status'] == 'MERGED'
1626 assert F.data['status'] == 'MERGED'
1627
1628 assert A.reported == 2
1629 assert B.reported == 2
1630 assert C.reported == 2
1631 assert D.reported == 2
1632 assert E.reported == 2
1633 assert F.reported == 2
1634
James E. Blair127bc182012-08-28 15:55:15 -07001635 assert self.countJobResults(finished_jobs, 'ABORTED') == 15
1636 assert len(finished_jobs) == 44
James E. Blaire0487072012-08-29 17:38:31 -07001637 self.assertEmptyQueues()
James E. Blair05fed602012-09-07 12:45:24 -07001638
1639 def test_merger_repack(self):
1640 "Test that the merger works after a repack"
1641 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1642 A.addApproval('CRVW', 2)
1643 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1644 self.waitUntilSettled()
1645 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001646 job_names = [x.name for x in jobs]
James E. Blair05fed602012-09-07 12:45:24 -07001647 assert 'project-merge' in job_names
1648 assert 'project-test1' in job_names
1649 assert 'project-test2' in job_names
James E. Blair0018a6c2013-02-27 14:11:45 -08001650 assert jobs[0].result == 'SUCCESS'
1651 assert jobs[1].result == 'SUCCESS'
1652 assert jobs[2].result == 'SUCCESS'
James E. Blair05fed602012-09-07 12:45:24 -07001653 assert A.data['status'] == 'MERGED'
1654 assert A.reported == 2
1655 self.assertEmptyQueues()
1656
1657 path = os.path.join(GIT_ROOT, "org/project")
1658 os.system('git --git-dir=%s/.git repack -afd' % path)
1659
1660 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1661 A.addApproval('CRVW', 2)
1662 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1663 self.waitUntilSettled()
1664 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001665 job_names = [x.name for x in jobs]
James E. Blair05fed602012-09-07 12:45:24 -07001666 assert 'project-merge' in job_names
1667 assert 'project-test1' in job_names
1668 assert 'project-test2' in job_names
James E. Blair0018a6c2013-02-27 14:11:45 -08001669 assert jobs[0].result == 'SUCCESS'
1670 assert jobs[1].result == 'SUCCESS'
1671 assert jobs[2].result == 'SUCCESS'
James E. Blair05fed602012-09-07 12:45:24 -07001672 assert A.data['status'] == 'MERGED'
1673 assert A.reported == 2
1674 self.assertEmptyQueues()
James E. Blair7ee88a22012-09-12 18:59:31 +02001675
James E. Blair4886f282012-11-15 09:27:33 -08001676 def test_merger_repack_large_change(self):
1677 "Test that the merger works with large changes after a repack"
1678 # https://bugs.launchpad.net/zuul/+bug/1078946
1679 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1680 A.addPatchset(large=True)
1681 path = os.path.join(UPSTREAM_ROOT, "org/project1")
1682 os.system('git --git-dir=%s/.git repack -afd' % path)
1683 path = os.path.join(GIT_ROOT, "org/project1")
1684 os.system('git --git-dir=%s/.git repack -afd' % path)
1685
1686 A.addApproval('CRVW', 2)
1687 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1688 self.waitUntilSettled()
1689 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001690 job_names = [x.name for x in jobs]
James E. Blair4886f282012-11-15 09:27:33 -08001691 assert 'project1-merge' in job_names
1692 assert 'project1-test1' in job_names
1693 assert 'project1-test2' in job_names
James E. Blair0018a6c2013-02-27 14:11:45 -08001694 assert jobs[0].result == 'SUCCESS'
1695 assert jobs[1].result == 'SUCCESS'
1696 assert jobs[2].result == 'SUCCESS'
James E. Blair4886f282012-11-15 09:27:33 -08001697 assert A.data['status'] == 'MERGED'
1698 assert A.reported == 2
1699 self.assertEmptyQueues()
1700
James E. Blair7ee88a22012-09-12 18:59:31 +02001701 def test_nonexistent_job(self):
1702 "Test launching a job that doesn't exist"
1703 self.fake_jenkins.nonexistent_jobs.append('project-merge')
1704 self.jenkins.launch_retry_timeout = 0.1
1705
1706 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1707 A.addApproval('CRVW', 2)
1708 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1709 # There may be a thread about to report a lost change
1710 while A.reported < 2:
1711 self.waitUntilSettled()
1712 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001713 job_names = [x.name for x in jobs]
James E. Blair7ee88a22012-09-12 18:59:31 +02001714 assert not job_names
1715 assert A.data['status'] == 'NEW'
1716 assert A.reported == 2
1717 self.assertEmptyQueues()
1718
1719 # Make sure things still work:
1720 self.fake_jenkins.nonexistent_jobs = []
1721 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1722 A.addApproval('CRVW', 2)
1723 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1724 self.waitUntilSettled()
1725 jobs = self.fake_jenkins.job_history
James E. Blair0018a6c2013-02-27 14:11:45 -08001726 job_names = [x.name for x in jobs]
James E. Blair7ee88a22012-09-12 18:59:31 +02001727 assert 'project-merge' in job_names
1728 assert 'project-test1' in job_names
1729 assert 'project-test2' in job_names
James E. Blair0018a6c2013-02-27 14:11:45 -08001730 assert jobs[0].result == 'SUCCESS'
1731 assert jobs[1].result == 'SUCCESS'
1732 assert jobs[2].result == 'SUCCESS'
James E. Blair7ee88a22012-09-12 18:59:31 +02001733 assert A.data['status'] == 'MERGED'
1734 assert A.reported == 2
1735 self.assertEmptyQueues()
James E. Blairf62d4282012-12-31 17:01:50 -08001736
1737 def test_single_nonexistent_post_job(self):
1738 "Test launching a single post job that doesn't exist"
1739 self.fake_jenkins.nonexistent_jobs.append('project-post')
1740 self.jenkins.launch_retry_timeout = 0.1
1741
1742 e = {
1743 "type": "ref-updated",
1744 "submitter": {
1745 "name": "User Name",
1746 },
1747 "refUpdate": {
1748 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1749 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1750 "refName": "master",
1751 "project": "org/project",
1752 }
1753 }
1754 self.fake_gerrit.addEvent(e)
1755 self.waitUntilSettled()
1756
1757 jobs = self.fake_jenkins.job_history
1758 assert len(jobs) == 0
1759 self.assertEmptyQueues()
James E. Blair2fa50962013-01-30 21:50:41 -08001760
1761 def test_new_patchset_dequeues_old(self):
1762 "Test that a new patchset causes the old to be dequeued"
1763 # D -> C (depends on B) -> B (depends on A) -> A -> M
1764 self.fake_jenkins.hold_jobs_in_build = True
1765
1766 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1767 M.setMerged()
1768
1769 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1770 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1771 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1772 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1773 A.addApproval('CRVW', 2)
1774 B.addApproval('CRVW', 2)
1775 C.addApproval('CRVW', 2)
1776 D.addApproval('CRVW', 2)
1777
1778 C.setDependsOn(B, 1)
1779 B.setDependsOn(A, 1)
1780 A.setDependsOn(M, 1)
1781
1782 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1783 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1784 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1785 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1786 self.waitUntilSettled()
1787
1788 B.addPatchset()
1789 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1790 self.waitUntilSettled()
1791
1792 self.fake_jenkins.hold_jobs_in_build = False
1793 self.fake_jenkins.fakeRelease()
1794 self.waitUntilSettled()
1795
1796 jobs = self.fake_jenkins.all_jobs
1797 finished_jobs = self.fake_jenkins.job_history
1798
1799 for x in jobs:
1800 print x
1801 for x in finished_jobs:
1802 print x
1803
1804 assert A.data['status'] == 'MERGED'
1805 assert A.reported == 2
1806 assert B.data['status'] == 'NEW'
1807 assert B.reported == 2
1808 assert C.data['status'] == 'NEW'
1809 assert C.reported == 2
1810 assert D.data['status'] == 'MERGED'
1811 assert D.reported == 2
1812 assert len(finished_jobs) == 9 # 3 each for A, B, D.
1813 self.assertEmptyQueues()
1814
1815 def test_new_patchset_dequeues_old_on_head(self):
1816 "Test that a new patchset causes the old to be dequeued (at head)"
1817 # D -> C (depends on B) -> B (depends on A) -> A -> M
1818 self.fake_jenkins.hold_jobs_in_build = True
1819
1820 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1821 M.setMerged()
1822 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1823 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1824 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1825 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1826 A.addApproval('CRVW', 2)
1827 B.addApproval('CRVW', 2)
1828 C.addApproval('CRVW', 2)
1829 D.addApproval('CRVW', 2)
1830
1831 C.setDependsOn(B, 1)
1832 B.setDependsOn(A, 1)
1833 A.setDependsOn(M, 1)
1834
1835 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1836 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1837 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1838 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1839 self.waitUntilSettled()
1840
1841 A.addPatchset()
1842 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1843 self.waitUntilSettled()
1844
1845 self.fake_jenkins.hold_jobs_in_build = False
1846 self.fake_jenkins.fakeRelease()
1847 self.waitUntilSettled()
1848
1849 jobs = self.fake_jenkins.all_jobs
1850 finished_jobs = self.fake_jenkins.job_history
1851
1852 for x in jobs:
1853 print x
1854 for x in finished_jobs:
1855 print x
1856
1857 assert A.data['status'] == 'NEW'
1858 assert A.reported == 2
1859 assert B.data['status'] == 'NEW'
1860 assert B.reported == 2
1861 assert C.data['status'] == 'NEW'
1862 assert C.reported == 2
1863 assert D.data['status'] == 'MERGED'
1864 assert D.reported == 2
1865 assert len(finished_jobs) == 7
1866 self.assertEmptyQueues()
1867
1868 def test_new_patchset_dequeues_old_without_dependents(self):
1869 "Test that a new patchset causes only the old to be dequeued"
1870 self.fake_jenkins.hold_jobs_in_build = True
1871
1872 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1873 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1874 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1875 A.addApproval('CRVW', 2)
1876 B.addApproval('CRVW', 2)
1877 C.addApproval('CRVW', 2)
1878
1879 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1880 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1881 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1882 self.waitUntilSettled()
1883
1884 B.addPatchset()
1885 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1886 self.waitUntilSettled()
1887
1888 self.fake_jenkins.hold_jobs_in_build = False
1889 self.fake_jenkins.fakeRelease()
1890 self.waitUntilSettled()
1891
1892 jobs = self.fake_jenkins.all_jobs
1893 finished_jobs = self.fake_jenkins.job_history
1894
1895 for x in jobs:
1896 print x
1897 for x in finished_jobs:
1898 print x
1899
1900 assert A.data['status'] == 'MERGED'
1901 assert A.reported == 2
1902 assert B.data['status'] == 'NEW'
1903 assert B.reported == 2
1904 assert C.data['status'] == 'MERGED'
1905 assert C.reported == 2
1906 assert len(finished_jobs) == 9
1907 self.assertEmptyQueues()
1908
1909 def test_new_patchset_dequeues_old_independent_queue(self):
1910 "Test that a new patchset causes the old to be dequeued (independent)"
1911 self.fake_jenkins.hold_jobs_in_build = True
1912
1913 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1914 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1915 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1916 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1917 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1918 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1919 self.waitUntilSettled()
1920
1921 B.addPatchset()
1922 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1923 self.waitUntilSettled()
1924
1925 self.fake_jenkins.hold_jobs_in_build = False
1926 self.fake_jenkins.fakeRelease()
1927 self.waitUntilSettled()
1928
1929 jobs = self.fake_jenkins.all_jobs
1930 finished_jobs = self.fake_jenkins.job_history
1931
1932 for x in jobs:
1933 print x
1934 for x in finished_jobs:
1935 print x
1936
1937 assert A.data['status'] == 'NEW'
1938 assert A.reported == 1
1939 assert B.data['status'] == 'NEW'
1940 assert B.reported == 1
1941 assert C.data['status'] == 'NEW'
1942 assert C.reported == 1
1943 assert len(finished_jobs) == 10
1944 assert self.countJobResults(finished_jobs, 'ABORTED') == 1
1945 self.assertEmptyQueues()
James E. Blair7d0dedc2013-02-21 17:26:09 -08001946
1947 def test_zuul_refs(self):
1948 "Test that zuul refs exist and have the right changes"
1949 self.fake_jenkins.hold_jobs_in_build = True
1950
1951 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1952 M1.setMerged()
1953 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1954 M2.setMerged()
1955
1956 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1957 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1958 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1959 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1960 A.addApproval('CRVW', 2)
1961 B.addApproval('CRVW', 2)
1962 C.addApproval('CRVW', 2)
1963 D.addApproval('CRVW', 2)
1964 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
1965 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
1966 self.fake_gerrit.addEvent(C.addApproval('APRV', 1))
1967 self.fake_gerrit.addEvent(D.addApproval('APRV', 1))
1968
1969 self.waitUntilSettled()
1970 self.fake_jenkins.fakeRelease('.*-merge')
1971 self.waitUntilSettled()
1972 self.fake_jenkins.fakeRelease('.*-merge')
1973 self.waitUntilSettled()
1974 self.fake_jenkins.fakeRelease('.*-merge')
1975 self.waitUntilSettled()
1976 self.fake_jenkins.fakeRelease('.*-merge')
1977 self.waitUntilSettled()
1978
1979 jobs = self.fake_jenkins.all_jobs
1980 finished_jobs = self.fake_jenkins.job_history
1981
1982 a_zref = b_zref = c_zref = d_zref = None
1983 for x in jobs:
1984 if x.parameters['ZUUL_CHANGE'] == '3':
1985 a_zref = x.parameters['ZUUL_REF']
1986 if x.parameters['ZUUL_CHANGE'] == '4':
1987 b_zref = x.parameters['ZUUL_REF']
1988 if x.parameters['ZUUL_CHANGE'] == '5':
1989 c_zref = x.parameters['ZUUL_REF']
1990 if x.parameters['ZUUL_CHANGE'] == '6':
1991 d_zref = x.parameters['ZUUL_REF']
1992
1993 # There are... four... refs.
1994 assert a_zref is not None
1995 assert b_zref is not None
1996 assert c_zref is not None
1997 assert d_zref is not None
1998
1999 # And they should all be different
2000 refs = set([a_zref, b_zref, c_zref, d_zref])
2001 assert len(refs) == 4
2002
2003 # a ref should have a, not b, and should not be in project2
2004 assert ref_has_change(a_zref, A)
2005 assert not ref_has_change(a_zref, B)
2006 assert not ref_has_change(a_zref, M2)
2007
2008 # b ref should have a and b, and should not be in project2
2009 assert ref_has_change(b_zref, A)
2010 assert ref_has_change(b_zref, B)
2011 assert not ref_has_change(b_zref, M2)
2012
2013 # c ref should have a and b in 1, c in 2
2014 assert ref_has_change(c_zref, A)
2015 assert ref_has_change(c_zref, B)
2016 assert ref_has_change(c_zref, C)
2017 assert not ref_has_change(c_zref, D)
2018
2019 # d ref should have a and b in 1, c and d in 2
2020 assert ref_has_change(d_zref, A)
2021 assert ref_has_change(d_zref, B)
2022 assert ref_has_change(d_zref, C)
2023 assert ref_has_change(d_zref, D)
2024
2025 self.fake_jenkins.hold_jobs_in_build = False
2026 self.fake_jenkins.fakeRelease()
2027 self.waitUntilSettled()
2028
2029 assert A.data['status'] == 'MERGED'
2030 assert A.reported == 2
2031 assert B.data['status'] == 'MERGED'
2032 assert B.reported == 2
2033 assert C.data['status'] == 'MERGED'
2034 assert C.reported == 2
2035 assert D.data['status'] == 'MERGED'
2036 assert D.reported == 2
2037 self.assertEmptyQueues()
James E. Blair70c71582013-03-06 08:50:50 -08002038
James E. Blair412e5582013-04-22 15:50:12 -07002039 def test_statsd(self):
2040 "Test each of the statsd methods used in the scheduler"
2041 import extras
2042 statsd = extras.try_import('statsd.statsd')
2043 statsd.incr('test-incr')
2044 statsd.timing('test-timing', 3)
2045 statsd.gauge('test-guage', 12)
2046 self.assertReportedStat('test-incr', '1|c')
2047 self.assertReportedStat('test-timing', '3|ms')
2048 self.assertReportedStat('test-guage', '12|g')
2049
James E. Blair70c71582013-03-06 08:50:50 -08002050 def test_file_jobs(self):
2051 "Test that file jobs run only when appropriate"
2052 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2053 A.addPatchset(['pip-requires'])
2054 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2055 A.addApproval('CRVW', 2)
2056 B.addApproval('CRVW', 2)
2057 self.fake_gerrit.addEvent(A.addApproval('APRV', 1))
2058 self.fake_gerrit.addEvent(B.addApproval('APRV', 1))
2059 self.waitUntilSettled()
2060
2061 jobs = self.fake_jenkins.all_jobs
2062 finished_jobs = self.fake_jenkins.job_history
2063
2064 testfile_jobs = [x for x in finished_jobs
2065 if x.name == 'project-testfile']
2066
2067 assert len(testfile_jobs) == 1
2068 assert testfile_jobs[0].changes == '1,2'
2069 assert A.data['status'] == 'MERGED'
2070 assert A.reported == 2
2071 assert B.data['status'] == 'MERGED'
2072 assert B.reported == 2
2073 self.assertEmptyQueues()