James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1 | #!/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 | |
| 17 | import unittest |
| 18 | import ConfigParser |
| 19 | import os |
| 20 | import Queue |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 21 | import hashlib |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 22 | import logging |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 23 | import random |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 24 | import json |
| 25 | import threading |
| 26 | import time |
| 27 | import pprint |
| 28 | import re |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 29 | import urllib2 |
| 30 | import urlparse |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 31 | import shutil |
| 32 | import git |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 33 | |
| 34 | import zuul |
| 35 | import zuul.scheduler |
| 36 | import zuul.launcher.jenkins |
| 37 | import zuul.trigger.gerrit |
| 38 | |
| 39 | FIXTURE_DIR = os.path.join(os.path.dirname(__file__), |
| 40 | 'fixtures') |
| 41 | CONFIG = ConfigParser.ConfigParser() |
| 42 | CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf")) |
| 43 | |
| 44 | CONFIG.set('zuul', 'layout_config', |
| 45 | os.path.join(FIXTURE_DIR, "layout.yaml")) |
| 46 | |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 47 | TMP_ROOT = os.environ.get("ZUUL_TEST_ROOT", "/tmp") |
| 48 | TEST_ROOT = os.path.join(TMP_ROOT, "zuul-test") |
| 49 | UPSTREAM_ROOT = os.path.join(TEST_ROOT, "upstream") |
| 50 | GIT_ROOT = os.path.join(TEST_ROOT, "git") |
| 51 | |
| 52 | CONFIG.set('zuul', 'git_dir', GIT_ROOT) |
| 53 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 54 | logging.basicConfig(level=logging.DEBUG) |
| 55 | |
| 56 | |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 57 | def random_sha1(): |
| 58 | return hashlib.sha1(str(random.random())).hexdigest() |
| 59 | |
| 60 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 61 | class ChangeReference(git.Reference): |
| 62 | _common_path_default = "refs/changes" |
| 63 | _points_to_commits_only = True |
| 64 | |
| 65 | |
| 66 | def init_repo(project): |
| 67 | parts = project.split('/') |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 68 | path = os.path.join(UPSTREAM_ROOT, *parts[:-1]) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 69 | if not os.path.exists(path): |
| 70 | os.makedirs(path) |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 71 | path = os.path.join(UPSTREAM_ROOT, project) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 72 | repo = git.Repo.init(path) |
| 73 | |
| 74 | fn = os.path.join(path, 'README') |
| 75 | f = open(fn, 'w') |
| 76 | f.write("test\n") |
| 77 | f.close() |
| 78 | repo.index.add([fn]) |
| 79 | repo.index.commit('initial commit') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 80 | master = repo.create_head('master') |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 81 | repo.create_tag('init') |
| 82 | |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 83 | mp = repo.create_head('mp') |
| 84 | repo.head.reference = mp |
| 85 | f = open(fn, 'a') |
| 86 | f.write("test mp\n") |
| 87 | f.close() |
| 88 | repo.index.add([fn]) |
| 89 | repo.index.commit('mp commit') |
| 90 | |
| 91 | repo.head.reference = master |
| 92 | repo.head.reset(index=True, working_tree=True) |
| 93 | repo.git.clean('-x', '-f', '-d') |
| 94 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 95 | |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 96 | def add_fake_change_to_repo(project, branch, change_num, patchset, msg, fn): |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 97 | path = os.path.join(UPSTREAM_ROOT, project) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 98 | repo = git.Repo(path) |
| 99 | ref = ChangeReference.create(repo, '1/%s/%s' % (change_num, |
| 100 | patchset), |
| 101 | 'refs/tags/init') |
| 102 | repo.head.reference = ref |
| 103 | repo.head.reset(index=True, working_tree=True) |
| 104 | repo.git.clean('-x', '-f', '-d') |
| 105 | |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 106 | path = os.path.join(UPSTREAM_ROOT, project) |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 107 | fn = os.path.join(path, fn) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 108 | f = open(fn, 'w') |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 109 | f.write("test %s %s %s\n" % (branch, change_num, patchset)) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 110 | f.close() |
| 111 | repo.index.add([fn]) |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 112 | return repo.index.commit(msg) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 113 | |
| 114 | |
| 115 | def ref_has_change(ref, change): |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 116 | path = os.path.join(GIT_ROOT, change.project) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 117 | repo = git.Repo(path) |
| 118 | for commit in repo.iter_commits(ref): |
| 119 | if commit.message.strip() == ('%s-1' % change.subject): |
| 120 | return True |
| 121 | return False |
| 122 | |
| 123 | |
| 124 | def job_has_changes(*args): |
| 125 | job = args[0] |
| 126 | commits = args[1:] |
| 127 | project = job.parameters['ZUUL_PROJECT'] |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 128 | path = os.path.join(GIT_ROOT, project) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 129 | repo = git.Repo(path) |
| 130 | ref = job.parameters['ZUUL_REF'] |
| 131 | repo_messages = [c.message.strip() for c in repo.iter_commits(ref)] |
| 132 | commit_messages = ['%s-1' % commit.subject for commit in commits] |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 133 | for msg in commit_messages: |
| 134 | if msg not in repo_messages: |
| 135 | return False |
| 136 | return True |
| 137 | |
| 138 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 139 | class FakeChange(object): |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 140 | categories = {'APRV': ('Approved', -1, 1), |
| 141 | 'CRVW': ('Code-Review', -2, 2), |
| 142 | 'VRFY': ('Verified', -2, 2)} |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 143 | |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 144 | def __init__(self, gerrit, number, project, branch, subject, status='NEW'): |
| 145 | self.gerrit = gerrit |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 146 | self.reported = 0 |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 147 | self.queried = 0 |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 148 | self.patchsets = [] |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 149 | self.number = number |
| 150 | self.project = project |
| 151 | self.branch = branch |
| 152 | self.subject = subject |
| 153 | self.latest_patchset = 0 |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 154 | self.depends_on_change = None |
| 155 | self.needed_by_changes = [] |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 156 | self.fail_merge = False |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 157 | self.data = { |
| 158 | 'branch': branch, |
| 159 | 'comments': [], |
| 160 | 'commitMessage': subject, |
| 161 | 'createdOn': time.time(), |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 162 | 'id': 'I' + random_sha1(), |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 163 | 'lastUpdated': time.time(), |
| 164 | 'number': str(number), |
| 165 | 'open': True, |
| 166 | 'owner': {'email': 'user@example.com', |
| 167 | 'name': 'User Name', |
| 168 | 'username': 'username'}, |
| 169 | 'patchSets': self.patchsets, |
| 170 | 'project': project, |
| 171 | 'status': status, |
| 172 | 'subject': subject, |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 173 | 'submitRecords': [], |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 174 | 'url': 'https://hostname/%s' % number} |
| 175 | |
| 176 | self.addPatchset() |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 177 | self.data['submitRecords'] = self.getSubmitRecords() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 178 | |
| 179 | def addPatchset(self, files=None): |
| 180 | self.latest_patchset += 1 |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 181 | if files: |
| 182 | fn = files[0] |
| 183 | else: |
| 184 | fn = '%s-%s' % (self.branch, self.number) |
| 185 | msg = self.subject + '-' + str(self.latest_patchset) |
| 186 | c = add_fake_change_to_repo(self.project, self.branch, |
| 187 | self.number, self.latest_patchset, |
| 188 | msg, fn) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 189 | d = {'approvals': [], |
| 190 | 'createdOn': time.time(), |
| 191 | 'files': [{'file': '/COMMIT_MSG', |
| 192 | 'type': 'ADDED'}, |
| 193 | {'file': 'README', |
| 194 | 'type': 'MODIFIED'}], |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 195 | 'number': str(self.latest_patchset), |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 196 | 'ref': 'refs/changes/1/%s/%s' % (self.number, |
| 197 | self.latest_patchset), |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 198 | 'revision': c.hexsha, |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 199 | 'uploader': {'email': 'user@example.com', |
| 200 | 'name': 'User name', |
| 201 | 'username': 'user'}} |
| 202 | self.data['currentPatchSet'] = d |
| 203 | self.patchsets.append(d) |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 204 | self.data['submitRecords'] = self.getSubmitRecords() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 205 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 206 | def getPatchsetCreatedEvent(self, patchset): |
| 207 | event = {"type": "patchset-created", |
| 208 | "change": {"project": self.project, |
| 209 | "branch": self.branch, |
| 210 | "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af", |
| 211 | "number": str(self.number), |
| 212 | "subject": self.subject, |
| 213 | "owner": {"name": "User Name"}, |
| 214 | "url": "https://hostname/3"}, |
| 215 | "patchSet": self.patchsets[patchset - 1], |
| 216 | "uploader": {"name": "User Name"}} |
| 217 | return event |
| 218 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 219 | def addApproval(self, category, value): |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 220 | approval = {'description': self.categories[category][0], |
| 221 | 'type': category, |
| 222 | 'value': str(value)} |
| 223 | self.patchsets[-1]['approvals'].append(approval) |
| 224 | event = {'approvals': [approval], |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 225 | 'author': {'email': 'user@example.com', |
| 226 | 'name': 'User Name', |
| 227 | 'username': 'username'}, |
| 228 | 'change': {'branch': self.branch, |
| 229 | 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87', |
| 230 | 'number': str(self.number), |
| 231 | 'owner': {'email': 'user@example.com', |
| 232 | 'name': 'User Name', |
| 233 | 'username': 'username'}, |
| 234 | 'project': self.project, |
| 235 | 'subject': self.subject, |
| 236 | 'topic': 'master', |
| 237 | 'url': 'https://hostname/459'}, |
| 238 | 'comment': '', |
| 239 | 'patchSet': self.patchsets[-1], |
| 240 | 'type': 'comment-added'} |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 241 | self.data['submitRecords'] = self.getSubmitRecords() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 242 | return json.loads(json.dumps(event)) |
| 243 | |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 244 | def getSubmitRecords(self): |
| 245 | status = {} |
| 246 | for cat in self.categories.keys(): |
| 247 | status[cat] = 0 |
| 248 | |
| 249 | for a in self.patchsets[-1]['approvals']: |
| 250 | cur = status[a['type']] |
| 251 | cat_min, cat_max = self.categories[a['type']][1:] |
| 252 | new = int(a['value']) |
| 253 | if new == cat_min: |
| 254 | cur = new |
| 255 | elif abs(new) > abs(cur): |
| 256 | cur = new |
| 257 | status[a['type']] = cur |
| 258 | |
| 259 | labels = [] |
| 260 | ok = True |
| 261 | for typ, cat in self.categories.items(): |
| 262 | cur = status[typ] |
| 263 | cat_min, cat_max = cat[1:] |
| 264 | if cur == cat_min: |
| 265 | value = 'REJECT' |
| 266 | ok = False |
| 267 | elif cur == cat_max: |
| 268 | value = 'OK' |
| 269 | else: |
| 270 | value = 'NEED' |
| 271 | ok = False |
| 272 | labels.append({'label': cat[0], 'status': value}) |
| 273 | if ok: |
| 274 | return [{'status': 'OK'}] |
| 275 | return [{'status': 'NOT_READY', |
| 276 | 'labels': labels}] |
| 277 | |
| 278 | def setDependsOn(self, other, patchset): |
| 279 | self.depends_on_change = other |
| 280 | d = {'id': other.data['id'], |
| 281 | 'number': other.data['number'], |
| 282 | 'ref': other.patchsets[patchset - 1]['ref'] |
| 283 | } |
| 284 | self.data['dependsOn'] = [d] |
| 285 | |
| 286 | other.needed_by_changes.append(self) |
| 287 | needed = other.data.get('neededBy', []) |
| 288 | d = {'id': self.data['id'], |
| 289 | 'number': self.data['number'], |
| 290 | 'ref': self.patchsets[patchset - 1]['ref'], |
| 291 | 'revision': self.patchsets[patchset - 1]['revision'] |
| 292 | } |
| 293 | needed.append(d) |
| 294 | other.data['neededBy'] = needed |
| 295 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 296 | def query(self): |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 297 | self.queried += 1 |
| 298 | d = self.data.get('dependsOn') |
| 299 | if d: |
| 300 | d = d[0] |
| 301 | if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']): |
| 302 | d['isCurrentPatchSet'] = True |
| 303 | else: |
| 304 | d['isCurrentPatchSet'] = False |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 305 | return json.loads(json.dumps(self.data)) |
| 306 | |
| 307 | def setMerged(self): |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 308 | if (self.depends_on_change |
| 309 | and self.depends_on_change.data['status'] != 'MERGED'): |
| 310 | return |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 311 | if self.fail_merge: |
| 312 | return |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 313 | self.data['status'] = 'MERGED' |
| 314 | self.open = False |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 315 | |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 316 | path = os.path.join(UPSTREAM_ROOT, self.project) |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 317 | repo = git.Repo(path) |
| 318 | repo.heads[self.branch].commit = \ |
| 319 | repo.commit(self.patchsets[-1]['revision']) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 320 | |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 321 | def setReported(self): |
| 322 | self.reported += 1 |
| 323 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 324 | |
| 325 | class FakeGerrit(object): |
| 326 | def __init__(self, *args, **kw): |
| 327 | self.event_queue = Queue.Queue() |
| 328 | self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit') |
| 329 | self.change_number = 0 |
| 330 | self.changes = {} |
| 331 | |
| 332 | def addFakeChange(self, project, branch, subject): |
| 333 | self.change_number += 1 |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 334 | c = FakeChange(self, self.change_number, project, branch, subject) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 335 | self.changes[self.change_number] = c |
| 336 | return c |
| 337 | |
| 338 | def addEvent(self, data): |
| 339 | return self.event_queue.put(data) |
| 340 | |
| 341 | def getEvent(self): |
| 342 | return self.event_queue.get() |
| 343 | |
| 344 | def eventDone(self): |
| 345 | self.event_queue.task_done() |
| 346 | |
| 347 | def review(self, project, changeid, message, action): |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 348 | number, ps = changeid.split(',') |
| 349 | change = self.changes[int(number)] |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 350 | if 'submit' in action: |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 351 | change.setMerged() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 352 | if message: |
| 353 | change.setReported() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 354 | |
| 355 | def query(self, number): |
| 356 | change = self.changes[int(number)] |
| 357 | return change.query() |
| 358 | |
| 359 | def startWatching(self, *args, **kw): |
| 360 | pass |
| 361 | |
| 362 | |
| 363 | class FakeJenkinsEvent(object): |
| 364 | def __init__(self, name, number, parameters, phase, status=None): |
Jeremy Stanley | b8ff8a7 | 2012-09-25 16:10:02 +0000 | [diff] [blame^] | 365 | data = {'build': |
| 366 | {'full_url': 'https://server/job/%s/%s/' % (name, number), |
| 367 | 'number': number, |
| 368 | 'parameters': parameters, |
| 369 | 'phase': phase, |
| 370 | 'url': 'job/%s/%s/' % (name, number)}, |
| 371 | 'name': name, |
| 372 | 'url': 'job/%s/' % name} |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 373 | if status: |
| 374 | data['build']['status'] = status |
| 375 | self.body = json.dumps(data) |
| 376 | |
| 377 | |
| 378 | class FakeJenkinsJob(threading.Thread): |
| 379 | log = logging.getLogger("zuul.test") |
| 380 | |
| 381 | def __init__(self, jenkins, callback, name, number, parameters): |
| 382 | threading.Thread.__init__(self) |
| 383 | self.jenkins = jenkins |
| 384 | self.callback = callback |
| 385 | self.name = name |
| 386 | self.number = number |
| 387 | self.parameters = parameters |
| 388 | self.wait_condition = threading.Condition() |
| 389 | self.waiting = False |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 390 | self.aborted = False |
| 391 | self.canceled = False |
| 392 | self.created = time.time() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 393 | |
| 394 | def release(self): |
| 395 | self.wait_condition.acquire() |
| 396 | self.wait_condition.notify() |
| 397 | self.waiting = False |
| 398 | self.log.debug("Job %s released" % (self.parameters['UUID'])) |
| 399 | self.wait_condition.release() |
| 400 | |
| 401 | def isWaiting(self): |
| 402 | self.wait_condition.acquire() |
| 403 | if self.waiting: |
| 404 | ret = True |
| 405 | else: |
| 406 | ret = False |
| 407 | self.wait_condition.release() |
| 408 | return ret |
| 409 | |
| 410 | def _wait(self): |
| 411 | self.wait_condition.acquire() |
| 412 | self.waiting = True |
| 413 | self.log.debug("Job %s waiting" % (self.parameters['UUID'])) |
| 414 | self.wait_condition.wait() |
| 415 | self.wait_condition.release() |
| 416 | |
| 417 | def run(self): |
| 418 | self.jenkins.fakeEnqueue(self) |
| 419 | if self.jenkins.hold_jobs_in_queue: |
| 420 | self._wait() |
| 421 | self.jenkins.fakeDequeue(self) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 422 | if self.canceled: |
| 423 | self.jenkins.all_jobs.remove(self) |
| 424 | return |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 425 | self.callback.jenkins_endpoint(FakeJenkinsEvent( |
| 426 | self.name, self.number, self.parameters, |
| 427 | 'STARTED')) |
| 428 | if self.jenkins.hold_jobs_in_build: |
| 429 | self._wait() |
| 430 | self.log.debug("Job %s continuing" % (self.parameters['UUID'])) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 431 | |
| 432 | result = 'SUCCESS' |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 433 | if ('ZUUL_REF' in self.parameters) and self.jenkins.fakeShouldFailTest( |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 434 | self.name, |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 435 | self.parameters['ZUUL_REF']): |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 436 | result = 'FAILURE' |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 437 | if self.aborted: |
| 438 | result = 'ABORTED' |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 439 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 440 | self.jenkins.fakeAddHistory(name=self.name, number=self.number, |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 441 | result=result) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 442 | self.callback.jenkins_endpoint(FakeJenkinsEvent( |
| 443 | self.name, self.number, self.parameters, |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 444 | 'COMPLETED', result)) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 445 | self.callback.jenkins_endpoint(FakeJenkinsEvent( |
| 446 | self.name, self.number, self.parameters, |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 447 | 'FINISHED', result)) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 448 | self.jenkins.all_jobs.remove(self) |
| 449 | |
| 450 | |
| 451 | class FakeJenkins(object): |
| 452 | log = logging.getLogger("zuul.test") |
| 453 | |
| 454 | def __init__(self, *args, **kw): |
| 455 | self.queue = [] |
| 456 | self.all_jobs = [] |
| 457 | self.job_counter = {} |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 458 | self.queue_counter = 0 |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 459 | self.job_history = [] |
| 460 | self.hold_jobs_in_queue = False |
| 461 | self.hold_jobs_in_build = False |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 462 | self.fail_tests = {} |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 463 | self.nonexistent_jobs = [] |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 464 | |
| 465 | def fakeEnqueue(self, job): |
| 466 | self.queue.append(job) |
| 467 | |
| 468 | def fakeDequeue(self, job): |
| 469 | self.queue.remove(job) |
| 470 | |
| 471 | def fakeAddHistory(self, **kw): |
| 472 | self.job_history.append(kw) |
| 473 | |
| 474 | def fakeRelease(self, regex=None): |
| 475 | all_jobs = self.all_jobs[:] |
| 476 | self.log.debug("releasing jobs %s (%s)" % (regex, len(self.all_jobs))) |
| 477 | for job in all_jobs: |
| 478 | if not regex or re.match(regex, job.name): |
| 479 | self.log.debug("releasing job %s" % (job.parameters['UUID'])) |
| 480 | job.release() |
| 481 | else: |
| 482 | self.log.debug("not releasing job %s" % ( |
| 483 | job.parameters['UUID'])) |
| 484 | self.log.debug("done releasing jobs %s (%s)" % (regex, |
| 485 | len(self.all_jobs))) |
| 486 | |
| 487 | def fakeAllWaiting(self, regex=None): |
| 488 | all_jobs = self.all_jobs[:] |
| 489 | for job in all_jobs: |
| 490 | self.log.debug("job %s %s" % (job.parameters['UUID'], |
| 491 | job.isWaiting())) |
| 492 | if not job.isWaiting(): |
| 493 | return False |
| 494 | return True |
| 495 | |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 496 | def fakeAddFailTest(self, name, change): |
| 497 | l = self.fail_tests.get(name, []) |
| 498 | l.append(change) |
| 499 | self.fail_tests[name] = l |
| 500 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 501 | def fakeShouldFailTest(self, name, ref): |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 502 | l = self.fail_tests.get(name, []) |
| 503 | for change in l: |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 504 | if ref_has_change(ref, change): |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 505 | return True |
| 506 | return False |
| 507 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 508 | def build_job(self, name, parameters): |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 509 | if name in self.nonexistent_jobs: |
| 510 | raise Exception("Job does not exist") |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 511 | count = self.job_counter.get(name, 0) |
| 512 | count += 1 |
| 513 | self.job_counter[name] = count |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 514 | |
| 515 | queue_count = self.queue_counter |
| 516 | self.queue_counter += 1 |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 517 | job = FakeJenkinsJob(self, self.callback, name, count, parameters) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 518 | job.queue_id = queue_count |
| 519 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 520 | self.all_jobs.append(job) |
| 521 | job.start() |
| 522 | |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 523 | def stop_build(self, name, number): |
| 524 | for job in self.all_jobs: |
| 525 | if job.name == name and job.number == number: |
| 526 | job.aborted = True |
| 527 | job.release() |
| 528 | return |
| 529 | |
| 530 | def cancel_queue(self, id): |
| 531 | for job in self.queue: |
| 532 | if job.queue_id == id: |
| 533 | job.canceled = True |
| 534 | job.release() |
| 535 | return |
| 536 | |
| 537 | def get_queue_info(self): |
| 538 | items = [] |
| 539 | for job in self.queue: |
| 540 | paramstr = '' |
| 541 | paramlst = [] |
| 542 | d = {'actions': [{'parameters': paramlst}, |
| 543 | {'causes': [{'shortDescription': |
| 544 | 'Started by user Jenkins', |
| 545 | 'userId': 'jenkins', |
| 546 | 'userName': 'Jenkins'}]}], |
| 547 | 'blocked': False, |
| 548 | 'buildable': True, |
| 549 | 'buildableStartMilliseconds': (job.created * 1000) + 5, |
| 550 | 'id': job.queue_id, |
| 551 | 'inQueueSince': (job.created * 1000), |
| 552 | 'params': paramstr, |
| 553 | 'stuck': False, |
| 554 | 'task': {'color': 'blue', |
| 555 | 'name': job.name, |
| 556 | 'url': 'https://server/job/%s/' % job.name}, |
| 557 | 'why': 'Waiting for next available executor'} |
| 558 | for k, v in job.parameters.items(): |
| 559 | paramstr += "\n(StringParameterValue) %s='%s'" % (k, v) |
| 560 | pd = {'name': k, 'value': v} |
| 561 | paramlst.append(pd) |
| 562 | items.append(d) |
| 563 | return items |
| 564 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 565 | def set_build_description(self, *args, **kw): |
| 566 | pass |
| 567 | |
| 568 | |
| 569 | class FakeJenkinsCallback(zuul.launcher.jenkins.JenkinsCallback): |
| 570 | def start(self): |
| 571 | pass |
| 572 | |
| 573 | |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 574 | class FakeURLOpener(object): |
| 575 | def __init__(self, fake_gerrit, url): |
| 576 | self.fake_gerrit = fake_gerrit |
| 577 | self.url = url |
| 578 | |
| 579 | def read(self): |
| 580 | res = urlparse.urlparse(self.url) |
| 581 | path = res.path |
| 582 | project = '/'.join(path.split('/')[2:-2]) |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 583 | ret = '' |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 584 | path = os.path.join(UPSTREAM_ROOT, project) |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 585 | repo = git.Repo(path) |
| 586 | for ref in repo.refs: |
| 587 | ret += ref.object.hexsha + '\t' + ref.path + '\n' |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 588 | return ret |
| 589 | |
| 590 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 591 | class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit): |
| 592 | def getGitUrl(self, project): |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 593 | return os.path.join(UPSTREAM_ROOT, project.name) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 594 | |
| 595 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 596 | class testScheduler(unittest.TestCase): |
| 597 | log = logging.getLogger("zuul.test") |
| 598 | |
| 599 | def setUp(self): |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 600 | if os.path.exists(TEST_ROOT): |
| 601 | shutil.rmtree(TEST_ROOT) |
| 602 | os.makedirs(TEST_ROOT) |
| 603 | os.makedirs(UPSTREAM_ROOT) |
| 604 | os.makedirs(GIT_ROOT) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 605 | |
| 606 | # For each project in config: |
| 607 | init_repo("org/project") |
| 608 | init_repo("org/project1") |
| 609 | init_repo("org/project2") |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 610 | init_repo("org/project3") |
James E. Blair | 7f71c80 | 2012-08-22 13:04:32 -0700 | [diff] [blame] | 611 | init_repo("org/one-job-project") |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 612 | init_repo("org/nonvoting-project") |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 613 | self.config = CONFIG |
| 614 | self.sched = zuul.scheduler.Scheduler() |
| 615 | |
| 616 | def jenkinsFactory(*args, **kw): |
| 617 | self.fake_jenkins = FakeJenkins() |
| 618 | return self.fake_jenkins |
| 619 | |
| 620 | def jenkinsCallbackFactory(*args, **kw): |
| 621 | self.fake_jenkins_callback = FakeJenkinsCallback(*args, **kw) |
| 622 | return self.fake_jenkins_callback |
| 623 | |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 624 | def URLOpenerFactory(*args, **kw): |
| 625 | args = [self.fake_gerrit] + list(args) |
| 626 | return FakeURLOpener(*args, **kw) |
| 627 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 628 | zuul.launcher.jenkins.ExtendedJenkins = jenkinsFactory |
| 629 | zuul.launcher.jenkins.JenkinsCallback = jenkinsCallbackFactory |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 630 | urllib2.urlopen = URLOpenerFactory |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 631 | self.jenkins = zuul.launcher.jenkins.Jenkins(self.config, self.sched) |
| 632 | self.fake_jenkins.callback = self.fake_jenkins_callback |
| 633 | |
| 634 | zuul.lib.gerrit.Gerrit = FakeGerrit |
| 635 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 636 | self.gerrit = FakeGerritTrigger(self.config, self.sched) |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 637 | self.gerrit.replication_timeout = 1.5 |
| 638 | self.gerrit.replication_retry_interval = 0.5 |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 639 | self.fake_gerrit = self.gerrit.gerrit |
| 640 | |
| 641 | self.sched.setLauncher(self.jenkins) |
| 642 | self.sched.setTrigger(self.gerrit) |
| 643 | |
| 644 | self.sched.start() |
| 645 | self.sched.reconfigure(self.config) |
| 646 | self.sched.resume() |
| 647 | |
| 648 | def tearDown(self): |
| 649 | self.jenkins.stop() |
| 650 | self.gerrit.stop() |
| 651 | self.sched.stop() |
| 652 | self.sched.join() |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 653 | #shutil.rmtree(TEST_ROOT) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 654 | |
| 655 | def waitUntilSettled(self): |
| 656 | self.log.debug("Waiting until settled...") |
| 657 | start = time.time() |
| 658 | while True: |
| 659 | if time.time() - start > 10: |
| 660 | print 'queue status:', |
| 661 | print self.sched.trigger_event_queue.empty(), |
| 662 | print self.sched.result_event_queue.empty(), |
| 663 | print self.fake_gerrit.event_queue.empty(), |
| 664 | raise Exception("Timeout waiting for Zuul to settle") |
| 665 | self.fake_gerrit.event_queue.join() |
| 666 | self.sched.queue_lock.acquire() |
| 667 | if (self.sched.trigger_event_queue.empty() and |
| 668 | self.sched.result_event_queue.empty() and |
| 669 | self.fake_gerrit.event_queue.empty() and |
| 670 | self.fake_jenkins.fakeAllWaiting()): |
| 671 | self.sched.queue_lock.release() |
| 672 | self.log.debug("...settled.") |
| 673 | return |
| 674 | self.sched.queue_lock.release() |
| 675 | self.sched.wake_event.wait(0.1) |
| 676 | |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 677 | def countJobResults(self, jobs, result): |
| 678 | jobs = filter(lambda x: x['result'] == result, jobs) |
| 679 | return len(jobs) |
| 680 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 681 | def assertEmptyQueues(self): |
| 682 | # Make sure there are no orphaned jobs |
| 683 | for pipeline in self.sched.pipelines.values(): |
| 684 | for queue in pipeline.queues: |
| 685 | if len(queue.queue) != 0: |
| 686 | print 'queue', queue.queue |
| 687 | assert len(queue.queue) == 0 |
| 688 | if len(queue.severed_heads) != 0: |
| 689 | print 'heads', queue.severed_heads |
| 690 | assert len(queue.severed_heads) == 0 |
| 691 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 692 | def test_jobs_launched(self): |
| 693 | "Test that jobs are launched and a change is merged" |
| 694 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 695 | A.addApproval('CRVW', 2) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 696 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 697 | self.waitUntilSettled() |
| 698 | jobs = self.fake_jenkins.job_history |
| 699 | job_names = [x['name'] for x in jobs] |
| 700 | assert 'project-merge' in job_names |
| 701 | assert 'project-test1' in job_names |
| 702 | assert 'project-test2' in job_names |
| 703 | assert jobs[0]['result'] == 'SUCCESS' |
| 704 | assert jobs[1]['result'] == 'SUCCESS' |
| 705 | assert jobs[2]['result'] == 'SUCCESS' |
| 706 | assert A.data['status'] == 'MERGED' |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 707 | assert A.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 708 | self.assertEmptyQueues() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 709 | |
| 710 | def test_parallel_changes(self): |
| 711 | "Test that changes are tested in parallel and merged in series" |
| 712 | self.fake_jenkins.hold_jobs_in_build = True |
| 713 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 714 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 715 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 716 | A.addApproval('CRVW', 2) |
| 717 | B.addApproval('CRVW', 2) |
| 718 | C.addApproval('CRVW', 2) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 719 | |
| 720 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 721 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 722 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 723 | |
| 724 | self.waitUntilSettled() |
| 725 | jobs = self.fake_jenkins.all_jobs |
| 726 | assert len(jobs) == 1 |
| 727 | assert jobs[0].name == 'project-merge' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 728 | assert job_has_changes(jobs[0], A) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 729 | |
| 730 | self.fake_jenkins.fakeRelease('.*-merge') |
| 731 | self.waitUntilSettled() |
| 732 | assert len(jobs) == 3 |
| 733 | assert jobs[0].name == 'project-test1' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 734 | assert job_has_changes(jobs[0], A) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 735 | assert jobs[1].name == 'project-test2' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 736 | assert job_has_changes(jobs[1], A) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 737 | assert jobs[2].name == 'project-merge' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 738 | assert job_has_changes(jobs[2], A, B) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 739 | |
| 740 | self.fake_jenkins.fakeRelease('.*-merge') |
| 741 | self.waitUntilSettled() |
| 742 | assert len(jobs) == 5 |
| 743 | assert jobs[0].name == 'project-test1' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 744 | assert job_has_changes(jobs[0], A) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 745 | assert jobs[1].name == 'project-test2' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 746 | assert job_has_changes(jobs[1], A) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 747 | |
| 748 | assert jobs[2].name == 'project-test1' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 749 | assert job_has_changes(jobs[2], A, B) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 750 | assert jobs[3].name == 'project-test2' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 751 | assert job_has_changes(jobs[3], A, B) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 752 | |
| 753 | assert jobs[4].name == 'project-merge' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 754 | assert job_has_changes(jobs[4], A, B, C) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 755 | |
| 756 | self.fake_jenkins.fakeRelease('.*-merge') |
| 757 | self.waitUntilSettled() |
| 758 | assert len(jobs) == 6 |
| 759 | assert jobs[0].name == 'project-test1' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 760 | assert job_has_changes(jobs[0], A) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 761 | assert jobs[1].name == 'project-test2' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 762 | assert job_has_changes(jobs[1], A) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 763 | |
| 764 | assert jobs[2].name == 'project-test1' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 765 | assert job_has_changes(jobs[2], A, B) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 766 | assert jobs[3].name == 'project-test2' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 767 | assert job_has_changes(jobs[3], A, B) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 768 | |
| 769 | assert jobs[4].name == 'project-test1' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 770 | assert job_has_changes(jobs[4], A, B, C) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 771 | assert jobs[5].name == 'project-test2' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 772 | assert job_has_changes(jobs[5], A, B, C) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 773 | |
| 774 | self.fake_jenkins.hold_jobs_in_build = False |
| 775 | self.fake_jenkins.fakeRelease() |
| 776 | self.waitUntilSettled() |
| 777 | assert len(jobs) == 0 |
| 778 | |
| 779 | jobs = self.fake_jenkins.job_history |
| 780 | assert len(jobs) == 9 |
| 781 | assert A.data['status'] == 'MERGED' |
| 782 | assert B.data['status'] == 'MERGED' |
| 783 | assert C.data['status'] == 'MERGED' |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 784 | assert A.reported == 2 |
| 785 | assert B.reported == 2 |
| 786 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 787 | self.assertEmptyQueues() |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 788 | |
| 789 | def test_failed_changes(self): |
| 790 | "Test that a change behind a failed change is retested" |
| 791 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 792 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 793 | A.addApproval('CRVW', 2) |
| 794 | B.addApproval('CRVW', 2) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 795 | |
| 796 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 797 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 798 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 799 | self.fake_jenkins.fakeAddFailTest('project-test1', A) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 800 | |
| 801 | self.waitUntilSettled() |
| 802 | jobs = self.fake_jenkins.job_history |
| 803 | assert len(jobs) > 6 |
| 804 | assert A.data['status'] == 'NEW' |
| 805 | assert B.data['status'] == 'MERGED' |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 806 | assert A.reported == 2 |
| 807 | assert B.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 808 | self.assertEmptyQueues() |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 809 | |
| 810 | def test_independent_queues(self): |
| 811 | "Test that changes end up in the right queues" |
| 812 | self.fake_jenkins.hold_jobs_in_build = True |
Jeremy Stanley | b8ff8a7 | 2012-09-25 16:10:02 +0000 | [diff] [blame^] | 813 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 814 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 815 | C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 816 | A.addApproval('CRVW', 2) |
| 817 | B.addApproval('CRVW', 2) |
| 818 | C.addApproval('CRVW', 2) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 819 | |
| 820 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 821 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 822 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 823 | |
| 824 | jobs = self.fake_jenkins.all_jobs |
| 825 | self.waitUntilSettled() |
| 826 | |
| 827 | # There should be one merge job at the head of each queue running |
| 828 | assert len(jobs) == 2 |
| 829 | assert jobs[0].name == 'project-merge' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 830 | assert job_has_changes(jobs[0], A) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 831 | assert jobs[1].name == 'project1-merge' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 832 | assert job_has_changes(jobs[1], B) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 833 | |
| 834 | # Release the current merge jobs |
| 835 | self.fake_jenkins.fakeRelease('.*-merge') |
| 836 | self.waitUntilSettled() |
| 837 | # Release the merge job for project2 which is behind project1 |
| 838 | self.fake_jenkins.fakeRelease('.*-merge') |
| 839 | self.waitUntilSettled() |
| 840 | |
| 841 | # All the test jobs should be running: |
| 842 | # project1 (3) + project2 (3) + project (2) = 8 |
| 843 | assert len(jobs) == 8 |
| 844 | |
| 845 | self.fake_jenkins.fakeRelease() |
| 846 | self.waitUntilSettled() |
| 847 | assert len(jobs) == 0 |
| 848 | |
| 849 | jobs = self.fake_jenkins.job_history |
| 850 | assert len(jobs) == 11 |
| 851 | assert A.data['status'] == 'MERGED' |
| 852 | assert B.data['status'] == 'MERGED' |
| 853 | assert C.data['status'] == 'MERGED' |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 854 | assert A.reported == 2 |
| 855 | assert B.reported == 2 |
| 856 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 857 | self.assertEmptyQueues() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 858 | |
| 859 | def test_failed_change_at_head(self): |
| 860 | "Test that if a change at the head fails, jobs behind it are canceled" |
| 861 | self.fake_jenkins.hold_jobs_in_build = True |
| 862 | |
| 863 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 864 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 865 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 866 | A.addApproval('CRVW', 2) |
| 867 | B.addApproval('CRVW', 2) |
| 868 | C.addApproval('CRVW', 2) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 869 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 870 | self.fake_jenkins.fakeAddFailTest('project-test1', A) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 871 | |
| 872 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 873 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 874 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 875 | |
| 876 | self.waitUntilSettled() |
| 877 | jobs = self.fake_jenkins.all_jobs |
| 878 | finished_jobs = self.fake_jenkins.job_history |
| 879 | |
| 880 | assert len(jobs) == 1 |
| 881 | assert jobs[0].name == 'project-merge' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 882 | assert job_has_changes(jobs[0], A) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 883 | |
| 884 | self.fake_jenkins.fakeRelease('.*-merge') |
| 885 | self.waitUntilSettled() |
| 886 | self.fake_jenkins.fakeRelease('.*-merge') |
| 887 | self.waitUntilSettled() |
| 888 | self.fake_jenkins.fakeRelease('.*-merge') |
| 889 | self.waitUntilSettled() |
| 890 | |
| 891 | assert len(jobs) == 6 |
| 892 | assert jobs[0].name == 'project-test1' |
| 893 | assert jobs[1].name == 'project-test2' |
| 894 | assert jobs[2].name == 'project-test1' |
| 895 | assert jobs[3].name == 'project-test2' |
| 896 | assert jobs[4].name == 'project-test1' |
| 897 | assert jobs[5].name == 'project-test2' |
| 898 | |
| 899 | jobs[0].release() |
| 900 | self.waitUntilSettled() |
| 901 | |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 902 | assert len(jobs) == 2 # project-test2, project-merge for B |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 903 | assert self.countJobResults(finished_jobs, 'ABORTED') == 4 |
| 904 | |
| 905 | self.fake_jenkins.hold_jobs_in_build = False |
| 906 | self.fake_jenkins.fakeRelease() |
| 907 | self.waitUntilSettled() |
| 908 | |
| 909 | assert len(jobs) == 0 |
| 910 | assert len(finished_jobs) == 15 |
| 911 | assert A.data['status'] == 'NEW' |
| 912 | assert B.data['status'] == 'MERGED' |
| 913 | assert C.data['status'] == 'MERGED' |
| 914 | assert A.reported == 2 |
| 915 | assert B.reported == 2 |
| 916 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 917 | self.assertEmptyQueues() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 918 | |
| 919 | def test_failed_change_at_head_with_queue(self): |
| 920 | "Test that if a change at the head fails, queued jobs are canceled" |
| 921 | self.fake_jenkins.hold_jobs_in_queue = True |
| 922 | |
| 923 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 924 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 925 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 926 | A.addApproval('CRVW', 2) |
| 927 | B.addApproval('CRVW', 2) |
| 928 | C.addApproval('CRVW', 2) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 929 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 930 | self.fake_jenkins.fakeAddFailTest('project-test1', A) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 931 | |
| 932 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 933 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 934 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 935 | |
| 936 | self.waitUntilSettled() |
| 937 | jobs = self.fake_jenkins.all_jobs |
| 938 | finished_jobs = self.fake_jenkins.job_history |
| 939 | queue = self.fake_jenkins.queue |
| 940 | |
| 941 | assert len(jobs) == 1 |
| 942 | assert len(queue) == 1 |
| 943 | assert jobs[0].name == 'project-merge' |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 944 | assert job_has_changes(jobs[0], A) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 945 | |
| 946 | self.fake_jenkins.fakeRelease('.*-merge') |
| 947 | self.waitUntilSettled() |
| 948 | self.fake_jenkins.fakeRelease('.*-merge') |
| 949 | self.waitUntilSettled() |
| 950 | self.fake_jenkins.fakeRelease('.*-merge') |
| 951 | self.waitUntilSettled() |
| 952 | |
| 953 | assert len(jobs) == 6 |
| 954 | assert len(queue) == 6 |
| 955 | assert jobs[0].name == 'project-test1' |
| 956 | assert jobs[1].name == 'project-test2' |
| 957 | assert jobs[2].name == 'project-test1' |
| 958 | assert jobs[3].name == 'project-test2' |
| 959 | assert jobs[4].name == 'project-test1' |
| 960 | assert jobs[5].name == 'project-test2' |
| 961 | |
| 962 | jobs[0].release() |
| 963 | self.waitUntilSettled() |
| 964 | |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 965 | assert len(jobs) == 2 # project-test2, project-merge for B |
| 966 | assert len(queue) == 2 |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 967 | assert self.countJobResults(finished_jobs, 'ABORTED') == 0 |
| 968 | |
| 969 | self.fake_jenkins.hold_jobs_in_queue = False |
| 970 | self.fake_jenkins.fakeRelease() |
| 971 | self.waitUntilSettled() |
| 972 | |
| 973 | assert len(jobs) == 0 |
| 974 | assert len(finished_jobs) == 11 |
| 975 | assert A.data['status'] == 'NEW' |
| 976 | assert B.data['status'] == 'MERGED' |
| 977 | assert C.data['status'] == 'MERGED' |
| 978 | assert A.reported == 2 |
| 979 | assert B.reported == 2 |
| 980 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 981 | self.assertEmptyQueues() |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 982 | |
| 983 | def test_patch_order(self): |
| 984 | "Test that dependent patches are tested in the right order" |
| 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') |
| 988 | A.addApproval('CRVW', 2) |
| 989 | B.addApproval('CRVW', 2) |
| 990 | C.addApproval('CRVW', 2) |
| 991 | |
| 992 | M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2') |
| 993 | M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1') |
| 994 | M2.setMerged() |
| 995 | M1.setMerged() |
| 996 | |
| 997 | # C -> B -> A -> M1 -> M2 |
| 998 | # M2 is here to make sure it is never queried. If it is, it |
| 999 | # means zuul is walking down the entire history of merged |
| 1000 | # changes. |
| 1001 | |
| 1002 | C.setDependsOn(B, 1) |
| 1003 | B.setDependsOn(A, 1) |
| 1004 | A.setDependsOn(M1, 1) |
| 1005 | M1.setDependsOn(M2, 1) |
| 1006 | |
| 1007 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1008 | |
| 1009 | self.waitUntilSettled() |
| 1010 | |
| 1011 | assert A.data['status'] == 'NEW' |
| 1012 | assert B.data['status'] == 'NEW' |
| 1013 | assert C.data['status'] == 'NEW' |
| 1014 | |
| 1015 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1016 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1017 | |
| 1018 | self.waitUntilSettled() |
| 1019 | assert M2.queried == 0 |
| 1020 | assert A.data['status'] == 'MERGED' |
| 1021 | assert B.data['status'] == 'MERGED' |
| 1022 | assert C.data['status'] == 'MERGED' |
| 1023 | assert A.reported == 2 |
| 1024 | assert B.reported == 2 |
| 1025 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1026 | self.assertEmptyQueues() |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1027 | |
| 1028 | def test_can_merge(self): |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1029 | "Test whether a change is ready to merge" |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1030 | # TODO: move to test_gerrit (this is a unit test!) |
| 1031 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1032 | a = self.sched.trigger.getChange(1, 2) |
| 1033 | mgr = self.sched.pipelines['gate'].manager |
| 1034 | assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()) |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1035 | |
| 1036 | A.addApproval('CRVW', 2) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1037 | a = self.sched.trigger.getChange(1, 2) |
| 1038 | assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()) |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1039 | |
| 1040 | A.addApproval('APRV', 1) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1041 | a = self.sched.trigger.getChange(1, 2) |
| 1042 | assert self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1043 | self.assertEmptyQueues() |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1044 | |
| 1045 | def test_build_configuration(self): |
| 1046 | "Test that zuul merges the right commits for testing" |
| 1047 | self.fake_jenkins.hold_jobs_in_queue = True |
| 1048 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1049 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1050 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1051 | A.addApproval('CRVW', 2) |
| 1052 | B.addApproval('CRVW', 2) |
| 1053 | C.addApproval('CRVW', 2) |
| 1054 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1055 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1056 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1057 | self.waitUntilSettled() |
| 1058 | |
| 1059 | jobs = self.fake_jenkins.all_jobs |
| 1060 | |
| 1061 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1062 | self.waitUntilSettled() |
| 1063 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1064 | self.waitUntilSettled() |
| 1065 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1066 | self.waitUntilSettled() |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 1067 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1068 | ref = jobs[-1].parameters['ZUUL_REF'] |
| 1069 | self.fake_jenkins.hold_jobs_in_queue = False |
| 1070 | self.fake_jenkins.fakeRelease() |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1071 | self.waitUntilSettled() |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1072 | |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 1073 | path = os.path.join(GIT_ROOT, "org/project") |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1074 | repo = git.Repo(path) |
| 1075 | repo_messages = [c.message.strip() for c in repo.iter_commits(ref)] |
| 1076 | repo_messages.reverse() |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1077 | correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1'] |
| 1078 | assert repo_messages == correct_messages |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1079 | self.assertEmptyQueues() |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1080 | |
| 1081 | def test_build_configuration_conflict(self): |
| 1082 | "Test that merge conflicts are handled" |
| 1083 | self.fake_jenkins.hold_jobs_in_queue = True |
| 1084 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1085 | A.addPatchset(['conflict']) |
| 1086 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1087 | B.addPatchset(['conflict']) |
| 1088 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1089 | A.addApproval('CRVW', 2) |
| 1090 | B.addApproval('CRVW', 2) |
| 1091 | C.addApproval('CRVW', 2) |
| 1092 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1093 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1094 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1095 | self.waitUntilSettled() |
| 1096 | |
| 1097 | jobs = self.fake_jenkins.all_jobs |
| 1098 | |
| 1099 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1100 | self.waitUntilSettled() |
| 1101 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1102 | self.waitUntilSettled() |
| 1103 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1104 | self.waitUntilSettled() |
| 1105 | ref = jobs[-1].parameters['ZUUL_REF'] |
| 1106 | self.fake_jenkins.hold_jobs_in_queue = False |
| 1107 | self.fake_jenkins.fakeRelease() |
| 1108 | self.waitUntilSettled() |
| 1109 | |
| 1110 | assert A.data['status'] == 'MERGED' |
| 1111 | assert B.data['status'] == 'NEW' |
| 1112 | assert C.data['status'] == 'MERGED' |
| 1113 | assert A.reported == 2 |
| 1114 | assert B.reported == 2 |
| 1115 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1116 | self.assertEmptyQueues() |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 1117 | |
| 1118 | def test_post(self): |
| 1119 | "Test that post jobs run" |
Jeremy Stanley | b8ff8a7 | 2012-09-25 16:10:02 +0000 | [diff] [blame^] | 1120 | e = {"type": "ref-updated", |
| 1121 | "submitter": {"name": "User Name"}, |
| 1122 | "refUpdate": {"oldRev": |
| 1123 | "90f173846e3af9154517b88543ffbd1691f31366", |
| 1124 | "newRev": |
| 1125 | "d479a0bfcb34da57a31adb2a595c0cf687812543", |
| 1126 | "refName": "master", "project": "org/project"}} |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 1127 | self.fake_gerrit.addEvent(e) |
| 1128 | self.waitUntilSettled() |
| 1129 | |
| 1130 | jobs = self.fake_jenkins.job_history |
| 1131 | job_names = [x['name'] for x in jobs] |
| 1132 | assert len(jobs) == 1 |
| 1133 | assert 'project-post' in job_names |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1134 | self.assertEmptyQueues() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1135 | |
| 1136 | def test_build_configuration_branch(self): |
| 1137 | "Test that the right commits are on alternate branches" |
| 1138 | self.fake_jenkins.hold_jobs_in_queue = True |
| 1139 | A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A') |
| 1140 | B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B') |
| 1141 | C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C') |
| 1142 | A.addApproval('CRVW', 2) |
| 1143 | B.addApproval('CRVW', 2) |
| 1144 | C.addApproval('CRVW', 2) |
| 1145 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1146 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1147 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1148 | self.waitUntilSettled() |
| 1149 | |
| 1150 | jobs = self.fake_jenkins.all_jobs |
| 1151 | |
| 1152 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1153 | self.waitUntilSettled() |
| 1154 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1155 | self.waitUntilSettled() |
| 1156 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1157 | self.waitUntilSettled() |
| 1158 | ref = jobs[-1].parameters['ZUUL_REF'] |
| 1159 | self.fake_jenkins.hold_jobs_in_queue = False |
| 1160 | self.fake_jenkins.fakeRelease() |
| 1161 | self.waitUntilSettled() |
| 1162 | |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 1163 | path = os.path.join(GIT_ROOT, "org/project") |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1164 | repo = git.Repo(path) |
| 1165 | repo_messages = [c.message.strip() for c in repo.iter_commits(ref)] |
| 1166 | repo_messages.reverse() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1167 | correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1'] |
| 1168 | assert repo_messages == correct_messages |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1169 | self.assertEmptyQueues() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1170 | |
| 1171 | def test_build_configuration_branch_interaction(self): |
| 1172 | "Test that switching between branches works" |
| 1173 | self.test_build_configuration() |
| 1174 | self.test_build_configuration_branch() |
| 1175 | # C has been merged, undo that |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 1176 | path = os.path.join(UPSTREAM_ROOT, "org/project") |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1177 | repo = git.Repo(path) |
| 1178 | repo.heads.master.commit = repo.commit('init') |
| 1179 | self.test_build_configuration() |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1180 | self.assertEmptyQueues() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1181 | |
| 1182 | def test_build_configuration_multi_branch(self): |
| 1183 | "Test that dependent changes on multiple branches are merged" |
| 1184 | self.fake_jenkins.hold_jobs_in_queue = True |
| 1185 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1186 | B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B') |
| 1187 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1188 | A.addApproval('CRVW', 2) |
| 1189 | B.addApproval('CRVW', 2) |
| 1190 | C.addApproval('CRVW', 2) |
| 1191 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1192 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1193 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1194 | self.waitUntilSettled() |
| 1195 | |
| 1196 | jobs = self.fake_jenkins.all_jobs |
| 1197 | |
| 1198 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1199 | self.waitUntilSettled() |
| 1200 | ref_mp = jobs[-1].parameters['ZUUL_REF'] |
| 1201 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1202 | self.waitUntilSettled() |
| 1203 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1204 | self.waitUntilSettled() |
| 1205 | ref_master = jobs[-1].parameters['ZUUL_REF'] |
| 1206 | self.fake_jenkins.hold_jobs_in_queue = False |
| 1207 | self.fake_jenkins.fakeRelease() |
| 1208 | self.waitUntilSettled() |
| 1209 | |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 1210 | path = os.path.join(GIT_ROOT, "org/project") |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1211 | repo = git.Repo(path) |
| 1212 | |
| 1213 | repo_messages = [c.message.strip() |
| 1214 | for c in repo.iter_commits(ref_master)] |
| 1215 | repo_messages.reverse() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1216 | correct_messages = ['initial commit', 'A-1', 'C-1'] |
| 1217 | assert repo_messages == correct_messages |
| 1218 | |
| 1219 | repo_messages = [c.message.strip() |
| 1220 | for c in repo.iter_commits(ref_mp)] |
| 1221 | repo_messages.reverse() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1222 | correct_messages = ['initial commit', 'mp commit', 'B-1'] |
| 1223 | assert repo_messages == correct_messages |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1224 | self.assertEmptyQueues() |
James E. Blair | 7f71c80 | 2012-08-22 13:04:32 -0700 | [diff] [blame] | 1225 | |
| 1226 | def test_one_job_project(self): |
| 1227 | "Test that queueing works with one job" |
| 1228 | A = self.fake_gerrit.addFakeChange('org/one-job-project', |
| 1229 | 'master', 'A') |
| 1230 | B = self.fake_gerrit.addFakeChange('org/one-job-project', |
| 1231 | 'master', 'B') |
| 1232 | A.addApproval('CRVW', 2) |
| 1233 | B.addApproval('CRVW', 2) |
| 1234 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1235 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1236 | self.waitUntilSettled() |
| 1237 | |
| 1238 | jobs = self.fake_jenkins.all_jobs |
| 1239 | finished_jobs = self.fake_jenkins.job_history |
James E. Blair | 7f71c80 | 2012-08-22 13:04:32 -0700 | [diff] [blame] | 1240 | |
| 1241 | assert A.data['status'] == 'MERGED' |
| 1242 | assert A.reported == 2 |
| 1243 | assert B.data['status'] == 'MERGED' |
| 1244 | assert B.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1245 | self.assertEmptyQueues() |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1246 | |
| 1247 | def test_dependent_changes_dequeue(self): |
| 1248 | "Test that dependent patches are not needlessly tested" |
| 1249 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1250 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1251 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1252 | A.addApproval('CRVW', 2) |
| 1253 | B.addApproval('CRVW', 2) |
| 1254 | C.addApproval('CRVW', 2) |
| 1255 | |
| 1256 | M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1') |
| 1257 | M1.setMerged() |
| 1258 | |
| 1259 | # C -> B -> A -> M1 |
| 1260 | |
| 1261 | C.setDependsOn(B, 1) |
| 1262 | B.setDependsOn(A, 1) |
| 1263 | A.setDependsOn(M1, 1) |
| 1264 | |
| 1265 | self.fake_jenkins.fakeAddFailTest('project-merge', A) |
| 1266 | |
| 1267 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1268 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1269 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1270 | |
| 1271 | self.waitUntilSettled() |
| 1272 | |
| 1273 | jobs = self.fake_jenkins.all_jobs |
| 1274 | finished_jobs = self.fake_jenkins.job_history |
| 1275 | |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1276 | for x in jobs: |
| 1277 | print x |
| 1278 | for x in finished_jobs: |
| 1279 | print x |
| 1280 | |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1281 | assert A.data['status'] == 'NEW' |
| 1282 | assert A.reported == 2 |
| 1283 | assert B.data['status'] == 'NEW' |
| 1284 | assert B.reported == 2 |
| 1285 | assert C.data['status'] == 'NEW' |
| 1286 | assert C.reported == 2 |
| 1287 | assert len(finished_jobs) == 1 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1288 | self.assertEmptyQueues() |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1289 | |
| 1290 | def test_head_is_dequeued_once(self): |
| 1291 | "Test that if a change at the head fails it is dequeud only once" |
| 1292 | # If it's dequeued more than once, we should see extra |
| 1293 | # aborted jobs. |
| 1294 | self.fake_jenkins.hold_jobs_in_build = True |
| 1295 | |
| 1296 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 1297 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 1298 | C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C') |
| 1299 | A.addApproval('CRVW', 2) |
| 1300 | B.addApproval('CRVW', 2) |
| 1301 | C.addApproval('CRVW', 2) |
| 1302 | |
| 1303 | self.fake_jenkins.fakeAddFailTest('project1-test1', A) |
| 1304 | self.fake_jenkins.fakeAddFailTest('project1-test2', A) |
| 1305 | self.fake_jenkins.fakeAddFailTest('project1-project2-integration', A) |
| 1306 | |
| 1307 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1308 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1309 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1310 | |
| 1311 | self.waitUntilSettled() |
| 1312 | jobs = self.fake_jenkins.all_jobs |
| 1313 | finished_jobs = self.fake_jenkins.job_history |
| 1314 | |
| 1315 | assert len(jobs) == 1 |
| 1316 | assert jobs[0].name == 'project1-merge' |
| 1317 | assert job_has_changes(jobs[0], A) |
| 1318 | |
| 1319 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1320 | self.waitUntilSettled() |
| 1321 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1322 | self.waitUntilSettled() |
| 1323 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1324 | self.waitUntilSettled() |
| 1325 | |
| 1326 | assert len(jobs) == 9 |
| 1327 | assert jobs[0].name == 'project1-test1' |
| 1328 | assert jobs[1].name == 'project1-test2' |
| 1329 | assert jobs[2].name == 'project1-project2-integration' |
| 1330 | assert jobs[3].name == 'project1-test1' |
| 1331 | assert jobs[4].name == 'project1-test2' |
| 1332 | assert jobs[5].name == 'project1-project2-integration' |
| 1333 | assert jobs[6].name == 'project1-test1' |
| 1334 | assert jobs[7].name == 'project1-test2' |
| 1335 | assert jobs[8].name == 'project1-project2-integration' |
| 1336 | |
| 1337 | jobs[0].release() |
| 1338 | self.waitUntilSettled() |
| 1339 | |
| 1340 | assert len(jobs) == 3 # test2, integration, merge for B |
| 1341 | assert self.countJobResults(finished_jobs, 'ABORTED') == 6 |
| 1342 | |
| 1343 | self.fake_jenkins.hold_jobs_in_build = False |
| 1344 | self.fake_jenkins.fakeRelease() |
| 1345 | self.waitUntilSettled() |
| 1346 | |
| 1347 | assert len(jobs) == 0 |
| 1348 | assert len(finished_jobs) == 20 |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1349 | |
| 1350 | assert A.data['status'] == 'NEW' |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1351 | assert B.data['status'] == 'MERGED' |
| 1352 | assert C.data['status'] == 'MERGED' |
| 1353 | assert A.reported == 2 |
| 1354 | assert B.reported == 2 |
| 1355 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1356 | self.assertEmptyQueues() |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 1357 | |
| 1358 | def test_nonvoting_job(self): |
| 1359 | "Test that non-voting jobs don't vote." |
| 1360 | A = self.fake_gerrit.addFakeChange('org/nonvoting-project', |
| 1361 | 'master', 'A') |
| 1362 | A.addApproval('CRVW', 2) |
| 1363 | self.fake_jenkins.fakeAddFailTest('nonvoting-project-test2', A) |
| 1364 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1365 | |
| 1366 | self.waitUntilSettled() |
| 1367 | jobs = self.fake_jenkins.all_jobs |
| 1368 | finished_jobs = self.fake_jenkins.job_history |
| 1369 | |
| 1370 | assert A.data['status'] == 'MERGED' |
| 1371 | assert A.reported == 2 |
| 1372 | assert finished_jobs[0]['result'] == 'SUCCESS' |
| 1373 | assert finished_jobs[1]['result'] == 'SUCCESS' |
| 1374 | assert finished_jobs[2]['result'] == 'FAILURE' |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1375 | self.assertEmptyQueues() |
| 1376 | |
| 1377 | def test_check_queue_success(self): |
| 1378 | "Test successful check queue jobs." |
| 1379 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1380 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1381 | |
| 1382 | self.waitUntilSettled() |
| 1383 | jobs = self.fake_jenkins.all_jobs |
| 1384 | finished_jobs = self.fake_jenkins.job_history |
| 1385 | |
| 1386 | assert A.data['status'] == 'NEW' |
| 1387 | assert A.reported == 1 |
| 1388 | assert finished_jobs[0]['result'] == 'SUCCESS' |
| 1389 | assert finished_jobs[1]['result'] == 'SUCCESS' |
| 1390 | assert finished_jobs[2]['result'] == 'SUCCESS' |
| 1391 | self.assertEmptyQueues() |
| 1392 | |
| 1393 | def test_check_queue_failure(self): |
| 1394 | "Test failed check queue jobs." |
| 1395 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1396 | self.fake_jenkins.fakeAddFailTest('project-test2', A) |
| 1397 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1398 | |
| 1399 | self.waitUntilSettled() |
| 1400 | jobs = self.fake_jenkins.all_jobs |
| 1401 | finished_jobs = self.fake_jenkins.job_history |
| 1402 | |
| 1403 | assert A.data['status'] == 'NEW' |
| 1404 | assert A.reported == 1 |
| 1405 | assert finished_jobs[0]['result'] == 'SUCCESS' |
| 1406 | assert finished_jobs[1]['result'] == 'SUCCESS' |
| 1407 | assert finished_jobs[2]['result'] == 'FAILURE' |
| 1408 | self.assertEmptyQueues() |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1409 | |
| 1410 | def test_dependent_behind_dequeue(self): |
| 1411 | "test that dependent changes behind dequeued changes work" |
| 1412 | # This complicated test is a reproduction of a real life bug |
| 1413 | self.sched.reconfigure(self.config) |
| 1414 | self.fake_jenkins.hold_jobs_in_build = True |
| 1415 | |
| 1416 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 1417 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 1418 | C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C') |
| 1419 | D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D') |
| 1420 | E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E') |
| 1421 | F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F') |
| 1422 | D.setDependsOn(C, 1) |
| 1423 | E.setDependsOn(D, 1) |
| 1424 | A.addApproval('CRVW', 2) |
| 1425 | B.addApproval('CRVW', 2) |
| 1426 | C.addApproval('CRVW', 2) |
| 1427 | D.addApproval('CRVW', 2) |
| 1428 | E.addApproval('CRVW', 2) |
| 1429 | F.addApproval('CRVW', 2) |
| 1430 | |
| 1431 | A.fail_merge = True |
| 1432 | jobs = self.fake_jenkins.all_jobs |
| 1433 | finished_jobs = self.fake_jenkins.job_history |
| 1434 | |
| 1435 | # Change object re-use in the gerrit trigger is hidden if |
| 1436 | # changes are added in quick succession; waiting makes it more |
| 1437 | # like real life. |
| 1438 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1439 | self.waitUntilSettled() |
| 1440 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1441 | self.waitUntilSettled() |
| 1442 | |
| 1443 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1444 | self.waitUntilSettled() |
| 1445 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1446 | self.waitUntilSettled() |
| 1447 | |
| 1448 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1449 | self.waitUntilSettled() |
| 1450 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 1451 | self.waitUntilSettled() |
| 1452 | self.fake_gerrit.addEvent(E.addApproval('APRV', 1)) |
| 1453 | self.waitUntilSettled() |
| 1454 | self.fake_gerrit.addEvent(F.addApproval('APRV', 1)) |
| 1455 | self.waitUntilSettled() |
| 1456 | |
| 1457 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1458 | self.waitUntilSettled() |
| 1459 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1460 | self.waitUntilSettled() |
| 1461 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1462 | self.waitUntilSettled() |
| 1463 | self.fake_jenkins.fakeRelease('.*-merge') |
| 1464 | self.waitUntilSettled() |
| 1465 | |
| 1466 | # all jobs running |
| 1467 | jobs[0].release() |
| 1468 | jobs[1].release() |
| 1469 | jobs[2].release() |
| 1470 | self.waitUntilSettled() |
| 1471 | |
| 1472 | self.fake_jenkins.hold_jobs_in_build = False |
| 1473 | self.fake_jenkins.fakeRelease() |
| 1474 | self.waitUntilSettled() |
| 1475 | |
| 1476 | for x in jobs: |
| 1477 | print x |
| 1478 | for x in finished_jobs: |
| 1479 | print x |
| 1480 | print self.sched.formatStatusHTML() |
| 1481 | |
| 1482 | assert A.data['status'] == 'NEW' |
| 1483 | assert B.data['status'] == 'MERGED' |
| 1484 | assert C.data['status'] == 'MERGED' |
| 1485 | assert D.data['status'] == 'MERGED' |
| 1486 | assert E.data['status'] == 'MERGED' |
| 1487 | assert F.data['status'] == 'MERGED' |
| 1488 | |
| 1489 | assert A.reported == 2 |
| 1490 | assert B.reported == 2 |
| 1491 | assert C.reported == 2 |
| 1492 | assert D.reported == 2 |
| 1493 | assert E.reported == 2 |
| 1494 | assert F.reported == 2 |
| 1495 | |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1496 | assert self.countJobResults(finished_jobs, 'ABORTED') == 15 |
| 1497 | assert len(finished_jobs) == 44 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1498 | self.assertEmptyQueues() |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 1499 | |
| 1500 | def test_merger_repack(self): |
| 1501 | "Test that the merger works after a repack" |
| 1502 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1503 | A.addApproval('CRVW', 2) |
| 1504 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1505 | self.waitUntilSettled() |
| 1506 | jobs = self.fake_jenkins.job_history |
| 1507 | job_names = [x['name'] for x in jobs] |
| 1508 | assert 'project-merge' in job_names |
| 1509 | assert 'project-test1' in job_names |
| 1510 | assert 'project-test2' in job_names |
| 1511 | assert jobs[0]['result'] == 'SUCCESS' |
| 1512 | assert jobs[1]['result'] == 'SUCCESS' |
| 1513 | assert jobs[2]['result'] == 'SUCCESS' |
| 1514 | assert A.data['status'] == 'MERGED' |
| 1515 | assert A.reported == 2 |
| 1516 | self.assertEmptyQueues() |
| 1517 | |
| 1518 | path = os.path.join(GIT_ROOT, "org/project") |
| 1519 | os.system('git --git-dir=%s/.git repack -afd' % path) |
| 1520 | |
| 1521 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1522 | A.addApproval('CRVW', 2) |
| 1523 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1524 | self.waitUntilSettled() |
| 1525 | jobs = self.fake_jenkins.job_history |
| 1526 | job_names = [x['name'] for x in jobs] |
| 1527 | assert 'project-merge' in job_names |
| 1528 | assert 'project-test1' in job_names |
| 1529 | assert 'project-test2' in job_names |
| 1530 | assert jobs[0]['result'] == 'SUCCESS' |
| 1531 | assert jobs[1]['result'] == 'SUCCESS' |
| 1532 | assert jobs[2]['result'] == 'SUCCESS' |
| 1533 | assert A.data['status'] == 'MERGED' |
| 1534 | assert A.reported == 2 |
| 1535 | self.assertEmptyQueues() |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 1536 | |
| 1537 | def test_nonexistent_job(self): |
| 1538 | "Test launching a job that doesn't exist" |
| 1539 | self.fake_jenkins.nonexistent_jobs.append('project-merge') |
| 1540 | self.jenkins.launch_retry_timeout = 0.1 |
| 1541 | |
| 1542 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1543 | A.addApproval('CRVW', 2) |
| 1544 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1545 | # There may be a thread about to report a lost change |
| 1546 | while A.reported < 2: |
| 1547 | self.waitUntilSettled() |
| 1548 | jobs = self.fake_jenkins.job_history |
| 1549 | job_names = [x['name'] for x in jobs] |
| 1550 | assert not job_names |
| 1551 | assert A.data['status'] == 'NEW' |
| 1552 | assert A.reported == 2 |
| 1553 | self.assertEmptyQueues() |
| 1554 | |
| 1555 | # Make sure things still work: |
| 1556 | self.fake_jenkins.nonexistent_jobs = [] |
| 1557 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1558 | A.addApproval('CRVW', 2) |
| 1559 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1560 | self.waitUntilSettled() |
| 1561 | jobs = self.fake_jenkins.job_history |
| 1562 | job_names = [x['name'] for x in jobs] |
| 1563 | assert 'project-merge' in job_names |
| 1564 | assert 'project-test1' in job_names |
| 1565 | assert 'project-test2' in job_names |
| 1566 | assert jobs[0]['result'] == 'SUCCESS' |
| 1567 | assert jobs[1]['result'] == 'SUCCESS' |
| 1568 | assert jobs[2]['result'] == 'SUCCESS' |
| 1569 | assert A.data['status'] == 'MERGED' |
| 1570 | assert A.reported == 2 |
| 1571 | self.assertEmptyQueues() |