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 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 17 | import ConfigParser |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 18 | from cStringIO import StringIO |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 19 | import hashlib |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 20 | import json |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 21 | import logging |
| 22 | import os |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 23 | import pprint |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 24 | import Queue |
| 25 | import random |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 26 | import re |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 27 | import select |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 28 | import shutil |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 29 | import socket |
James E. Blair | 4886f28 | 2012-11-15 09:27:33 -0800 | [diff] [blame] | 30 | import string |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 31 | import subprocess |
| 32 | import tempfile |
| 33 | import threading |
| 34 | import time |
James E. Blair | 1843a55 | 2013-07-03 14:19:52 -0700 | [diff] [blame^] | 35 | import urllib |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 36 | import urllib2 |
| 37 | import urlparse |
| 38 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 39 | import git |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 40 | import gear |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 41 | import fixtures |
| 42 | import statsd |
| 43 | import testtools |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 44 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 45 | import zuul.scheduler |
James E. Blair | 1843a55 | 2013-07-03 14:19:52 -0700 | [diff] [blame^] | 46 | import zuul.webapp |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 47 | import zuul.launcher.gearman |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 48 | import zuul.trigger.gerrit |
| 49 | |
| 50 | FIXTURE_DIR = os.path.join(os.path.dirname(__file__), |
| 51 | 'fixtures') |
| 52 | CONFIG = ConfigParser.ConfigParser() |
| 53 | CONFIG.read(os.path.join(FIXTURE_DIR, "zuul.conf")) |
| 54 | |
| 55 | CONFIG.set('zuul', 'layout_config', |
| 56 | os.path.join(FIXTURE_DIR, "layout.yaml")) |
| 57 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 58 | logging.basicConfig(level=logging.DEBUG, |
| 59 | format='%(asctime)s %(name)-32s ' |
| 60 | '%(levelname)-8s %(message)s') |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 61 | |
| 62 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 63 | def repack_repo(path): |
| 64 | output = subprocess.Popen( |
| 65 | ['git', '--git-dir=%s/.git' % path, 'repack', '-afd'], |
| 66 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 67 | out = output.communicate() |
| 68 | if output.returncode: |
| 69 | raise Exception("git repack returned %d" % output.returncode) |
| 70 | return out |
| 71 | |
| 72 | |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 73 | def random_sha1(): |
| 74 | return hashlib.sha1(str(random.random())).hexdigest() |
| 75 | |
| 76 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 77 | class ChangeReference(git.Reference): |
| 78 | _common_path_default = "refs/changes" |
| 79 | _points_to_commits_only = True |
| 80 | |
| 81 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 82 | class FakeChange(object): |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 83 | categories = {'APRV': ('Approved', -1, 1), |
| 84 | 'CRVW': ('Code-Review', -2, 2), |
| 85 | 'VRFY': ('Verified', -2, 2)} |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 86 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 87 | def __init__(self, gerrit, number, project, branch, subject, |
| 88 | status='NEW', upstream_root=None): |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 89 | self.gerrit = gerrit |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 90 | self.reported = 0 |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 91 | self.queried = 0 |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 92 | self.patchsets = [] |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 93 | self.number = number |
| 94 | self.project = project |
| 95 | self.branch = branch |
| 96 | self.subject = subject |
| 97 | self.latest_patchset = 0 |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 98 | self.depends_on_change = None |
| 99 | self.needed_by_changes = [] |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 100 | self.fail_merge = False |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 101 | self.messages = [] |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 102 | self.data = { |
| 103 | 'branch': branch, |
| 104 | 'comments': [], |
| 105 | 'commitMessage': subject, |
| 106 | 'createdOn': time.time(), |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 107 | 'id': 'I' + random_sha1(), |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 108 | 'lastUpdated': time.time(), |
| 109 | 'number': str(number), |
| 110 | 'open': True, |
| 111 | 'owner': {'email': 'user@example.com', |
| 112 | 'name': 'User Name', |
| 113 | 'username': 'username'}, |
| 114 | 'patchSets': self.patchsets, |
| 115 | 'project': project, |
| 116 | 'status': status, |
| 117 | 'subject': subject, |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 118 | 'submitRecords': [], |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 119 | 'url': 'https://hostname/%s' % number} |
| 120 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 121 | self.upstream_root = upstream_root |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 122 | self.addPatchset() |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 123 | self.data['submitRecords'] = self.getSubmitRecords() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 124 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 125 | def add_fake_change_to_repo(self, msg, fn, large): |
| 126 | path = os.path.join(self.upstream_root, self.project) |
| 127 | repo = git.Repo(path) |
| 128 | ref = ChangeReference.create(repo, '1/%s/%s' % (self.number, |
| 129 | self.latest_patchset), |
| 130 | 'refs/tags/init') |
| 131 | repo.head.reference = ref |
| 132 | repo.head.reset(index=True, working_tree=True) |
| 133 | repo.git.clean('-x', '-f', '-d') |
| 134 | |
| 135 | path = os.path.join(self.upstream_root, self.project) |
| 136 | if not large: |
| 137 | fn = os.path.join(path, fn) |
| 138 | f = open(fn, 'w') |
| 139 | f.write("test %s %s %s\n" % |
| 140 | (self.branch, self.number, self.latest_patchset)) |
| 141 | f.close() |
| 142 | repo.index.add([fn]) |
| 143 | else: |
| 144 | for fni in range(100): |
| 145 | fn = os.path.join(path, str(fni)) |
| 146 | f = open(fn, 'w') |
| 147 | for ci in range(4096): |
| 148 | f.write(random.choice(string.printable)) |
| 149 | f.close() |
| 150 | repo.index.add([fn]) |
| 151 | |
| 152 | return repo.index.commit(msg) |
| 153 | |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 154 | def addPatchset(self, files=[], large=False): |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 155 | self.latest_patchset += 1 |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 156 | if files: |
| 157 | fn = files[0] |
| 158 | else: |
| 159 | fn = '%s-%s' % (self.branch, self.number) |
| 160 | msg = self.subject + '-' + str(self.latest_patchset) |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 161 | c = self.add_fake_change_to_repo(msg, fn, large) |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 162 | ps_files = [{'file': '/COMMIT_MSG', |
| 163 | 'type': 'ADDED'}, |
| 164 | {'file': 'README', |
| 165 | 'type': 'MODIFIED'}] |
| 166 | for f in files: |
| 167 | ps_files.append({'file': f, 'type': 'ADDED'}) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 168 | d = {'approvals': [], |
| 169 | 'createdOn': time.time(), |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 170 | 'files': ps_files, |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 171 | 'number': str(self.latest_patchset), |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 172 | 'ref': 'refs/changes/1/%s/%s' % (self.number, |
| 173 | self.latest_patchset), |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 174 | 'revision': c.hexsha, |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 175 | 'uploader': {'email': 'user@example.com', |
| 176 | 'name': 'User name', |
| 177 | 'username': 'user'}} |
| 178 | self.data['currentPatchSet'] = d |
| 179 | self.patchsets.append(d) |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 180 | self.data['submitRecords'] = self.getSubmitRecords() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 181 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 182 | def getPatchsetCreatedEvent(self, patchset): |
| 183 | event = {"type": "patchset-created", |
| 184 | "change": {"project": self.project, |
Zhongyue Luo | aa85ebf | 2012-09-21 16:38:33 +0800 | [diff] [blame] | 185 | "branch": self.branch, |
| 186 | "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af", |
| 187 | "number": str(self.number), |
| 188 | "subject": self.subject, |
| 189 | "owner": {"name": "User Name"}, |
| 190 | "url": "https://hostname/3"}, |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 191 | "patchSet": self.patchsets[patchset - 1], |
| 192 | "uploader": {"name": "User Name"}} |
| 193 | return event |
| 194 | |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 195 | def getChangeRestoredEvent(self): |
| 196 | event = {"type": "change-restored", |
| 197 | "change": {"project": self.project, |
| 198 | "branch": self.branch, |
| 199 | "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af", |
| 200 | "number": str(self.number), |
| 201 | "subject": self.subject, |
| 202 | "owner": {"name": "User Name"}, |
| 203 | "url": "https://hostname/3"}, |
| 204 | "restorer": {"name": "User Name"}, |
| 205 | "reason": ""} |
| 206 | return event |
| 207 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 208 | def addApproval(self, category, value): |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 209 | approval = {'description': self.categories[category][0], |
| 210 | 'type': category, |
| 211 | 'value': str(value)} |
| 212 | self.patchsets[-1]['approvals'].append(approval) |
| 213 | event = {'approvals': [approval], |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 214 | 'author': {'email': 'user@example.com', |
| 215 | 'name': 'User Name', |
| 216 | 'username': 'username'}, |
| 217 | 'change': {'branch': self.branch, |
| 218 | 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87', |
| 219 | 'number': str(self.number), |
| 220 | 'owner': {'email': 'user@example.com', |
| 221 | 'name': 'User Name', |
| 222 | 'username': 'username'}, |
| 223 | 'project': self.project, |
| 224 | 'subject': self.subject, |
| 225 | 'topic': 'master', |
| 226 | 'url': 'https://hostname/459'}, |
| 227 | 'comment': '', |
| 228 | 'patchSet': self.patchsets[-1], |
| 229 | 'type': 'comment-added'} |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 230 | self.data['submitRecords'] = self.getSubmitRecords() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 231 | return json.loads(json.dumps(event)) |
| 232 | |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 233 | def getSubmitRecords(self): |
| 234 | status = {} |
| 235 | for cat in self.categories.keys(): |
| 236 | status[cat] = 0 |
| 237 | |
| 238 | for a in self.patchsets[-1]['approvals']: |
| 239 | cur = status[a['type']] |
| 240 | cat_min, cat_max = self.categories[a['type']][1:] |
| 241 | new = int(a['value']) |
| 242 | if new == cat_min: |
| 243 | cur = new |
| 244 | elif abs(new) > abs(cur): |
| 245 | cur = new |
| 246 | status[a['type']] = cur |
| 247 | |
| 248 | labels = [] |
| 249 | ok = True |
| 250 | for typ, cat in self.categories.items(): |
| 251 | cur = status[typ] |
| 252 | cat_min, cat_max = cat[1:] |
| 253 | if cur == cat_min: |
| 254 | value = 'REJECT' |
| 255 | ok = False |
| 256 | elif cur == cat_max: |
| 257 | value = 'OK' |
| 258 | else: |
| 259 | value = 'NEED' |
| 260 | ok = False |
| 261 | labels.append({'label': cat[0], 'status': value}) |
| 262 | if ok: |
| 263 | return [{'status': 'OK'}] |
| 264 | return [{'status': 'NOT_READY', |
| 265 | 'labels': labels}] |
| 266 | |
| 267 | def setDependsOn(self, other, patchset): |
| 268 | self.depends_on_change = other |
| 269 | d = {'id': other.data['id'], |
| 270 | 'number': other.data['number'], |
| 271 | 'ref': other.patchsets[patchset - 1]['ref'] |
| 272 | } |
| 273 | self.data['dependsOn'] = [d] |
| 274 | |
| 275 | other.needed_by_changes.append(self) |
| 276 | needed = other.data.get('neededBy', []) |
| 277 | d = {'id': self.data['id'], |
| 278 | 'number': self.data['number'], |
| 279 | 'ref': self.patchsets[patchset - 1]['ref'], |
| 280 | 'revision': self.patchsets[patchset - 1]['revision'] |
| 281 | } |
| 282 | needed.append(d) |
| 283 | other.data['neededBy'] = needed |
| 284 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 285 | def query(self): |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 286 | self.queried += 1 |
| 287 | d = self.data.get('dependsOn') |
| 288 | if d: |
| 289 | d = d[0] |
| 290 | if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']): |
| 291 | d['isCurrentPatchSet'] = True |
| 292 | else: |
| 293 | d['isCurrentPatchSet'] = False |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 294 | return json.loads(json.dumps(self.data)) |
| 295 | |
| 296 | def setMerged(self): |
Zhongyue Luo | aa85ebf | 2012-09-21 16:38:33 +0800 | [diff] [blame] | 297 | if (self.depends_on_change and |
| 298 | self.depends_on_change.data['status'] != 'MERGED'): |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 299 | return |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 300 | if self.fail_merge: |
| 301 | return |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 302 | self.data['status'] = 'MERGED' |
| 303 | self.open = False |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 304 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 305 | path = os.path.join(self.upstream_root, self.project) |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 306 | repo = git.Repo(path) |
| 307 | repo.heads[self.branch].commit = \ |
| 308 | repo.commit(self.patchsets[-1]['revision']) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 309 | |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 310 | def setReported(self): |
| 311 | self.reported += 1 |
| 312 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 313 | |
| 314 | class FakeGerrit(object): |
| 315 | def __init__(self, *args, **kw): |
| 316 | self.event_queue = Queue.Queue() |
| 317 | self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit') |
| 318 | self.change_number = 0 |
| 319 | self.changes = {} |
| 320 | |
| 321 | def addFakeChange(self, project, branch, subject): |
| 322 | self.change_number += 1 |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 323 | c = FakeChange(self, self.change_number, project, branch, subject, |
| 324 | upstream_root=self.upstream_root) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 325 | self.changes[self.change_number] = c |
| 326 | return c |
| 327 | |
| 328 | def addEvent(self, data): |
| 329 | return self.event_queue.put(data) |
| 330 | |
| 331 | def getEvent(self): |
| 332 | return self.event_queue.get() |
| 333 | |
| 334 | def eventDone(self): |
| 335 | self.event_queue.task_done() |
| 336 | |
| 337 | def review(self, project, changeid, message, action): |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 338 | number, ps = changeid.split(',') |
| 339 | change = self.changes[int(number)] |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 340 | change.messages.append(message) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 341 | if 'submit' in action: |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 342 | change.setMerged() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 343 | if message: |
| 344 | change.setReported() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 345 | |
| 346 | def query(self, number): |
| 347 | change = self.changes[int(number)] |
| 348 | return change.query() |
| 349 | |
| 350 | def startWatching(self, *args, **kw): |
| 351 | pass |
| 352 | |
| 353 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 354 | class BuildHistory(object): |
| 355 | def __init__(self, **kw): |
| 356 | self.__dict__.update(kw) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 357 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 358 | def __repr__(self): |
| 359 | return ("<Completed build, result: %s name: %s #%s changes: %s>" % |
| 360 | (self.result, self.name, self.number, self.changes)) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 361 | |
| 362 | |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 363 | class FakeURLOpener(object): |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 364 | def __init__(self, upstream_root, fake_gerrit, url): |
| 365 | self.upstream_root = upstream_root |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 366 | self.fake_gerrit = fake_gerrit |
| 367 | self.url = url |
| 368 | |
| 369 | def read(self): |
| 370 | res = urlparse.urlparse(self.url) |
| 371 | path = res.path |
| 372 | project = '/'.join(path.split('/')[2:-2]) |
James E. Blair | 35956c5 | 2012-09-17 22:13:36 +0200 | [diff] [blame] | 373 | ret = '001e# service=git-upload-pack\n' |
| 374 | ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00' |
| 375 | 'multi_ack thin-pack side-band side-band-64k ofs-delta ' |
| 376 | 'shallow no-progress include-tag multi_ack_detailed no-done\n') |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 377 | path = os.path.join(self.upstream_root, project) |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 378 | repo = git.Repo(path) |
| 379 | for ref in repo.refs: |
James E. Blair | 35956c5 | 2012-09-17 22:13:36 +0200 | [diff] [blame] | 380 | r = ref.object.hexsha + ' ' + ref.path + '\n' |
| 381 | ret += '%04x%s' % (len(r) + 4, r) |
| 382 | ret += '0000' |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 383 | return ret |
| 384 | |
| 385 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 386 | class FakeGerritTrigger(zuul.trigger.gerrit.Gerrit): |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 387 | def __init__(self, upstream_root, *args): |
| 388 | super(FakeGerritTrigger, self).__init__(*args) |
| 389 | self.upstream_root = upstream_root |
| 390 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 391 | def getGitUrl(self, project): |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 392 | return os.path.join(self.upstream_root, project.name) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 393 | |
| 394 | |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 395 | class FakeStatsd(threading.Thread): |
| 396 | def __init__(self): |
| 397 | threading.Thread.__init__(self) |
James E. Blair | 8a6f0c2 | 2013-07-01 12:31:34 -0400 | [diff] [blame] | 398 | self.daemon = True |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 399 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 400 | self.sock.bind(('', 0)) |
| 401 | self.port = self.sock.getsockname()[1] |
| 402 | self.wake_read, self.wake_write = os.pipe() |
| 403 | self.stats = [] |
| 404 | |
| 405 | def run(self): |
| 406 | while True: |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 407 | poll = select.poll() |
| 408 | poll.register(self.sock, select.POLLIN) |
| 409 | poll.register(self.wake_read, select.POLLIN) |
| 410 | ret = poll.poll() |
| 411 | for (fd, event) in ret: |
| 412 | if fd == self.sock.fileno(): |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 413 | data = self.sock.recvfrom(1024) |
| 414 | if not data: |
| 415 | return |
| 416 | self.stats.append(data[0]) |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 417 | if fd == self.wake_read: |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 418 | return |
| 419 | |
| 420 | def stop(self): |
| 421 | os.write(self.wake_write, '1\n') |
| 422 | |
| 423 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 424 | class FakeBuild(threading.Thread): |
| 425 | log = logging.getLogger("zuul.test") |
| 426 | |
| 427 | def __init__(self, worker, job, number, node): |
| 428 | threading.Thread.__init__(self) |
James E. Blair | 8a6f0c2 | 2013-07-01 12:31:34 -0400 | [diff] [blame] | 429 | self.daemon = True |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 430 | self.worker = worker |
| 431 | self.job = job |
| 432 | self.name = job.name.split(':')[1] |
| 433 | self.number = number |
| 434 | self.node = node |
| 435 | self.parameters = json.loads(job.arguments) |
| 436 | self.unique = self.parameters['ZUUL_UUID'] |
| 437 | self.wait_condition = threading.Condition() |
| 438 | self.waiting = False |
| 439 | self.aborted = False |
| 440 | self.created = time.time() |
| 441 | self.description = '' |
| 442 | |
| 443 | def release(self): |
| 444 | self.wait_condition.acquire() |
| 445 | self.wait_condition.notify() |
| 446 | self.waiting = False |
| 447 | self.log.debug("Build %s released" % self.unique) |
| 448 | self.wait_condition.release() |
| 449 | |
| 450 | def isWaiting(self): |
| 451 | self.wait_condition.acquire() |
| 452 | if self.waiting: |
| 453 | ret = True |
| 454 | else: |
| 455 | ret = False |
| 456 | self.wait_condition.release() |
| 457 | return ret |
| 458 | |
| 459 | def _wait(self): |
| 460 | self.wait_condition.acquire() |
| 461 | self.waiting = True |
| 462 | self.log.debug("Build %s waiting" % self.unique) |
| 463 | self.wait_condition.wait() |
| 464 | self.wait_condition.release() |
| 465 | |
| 466 | def run(self): |
| 467 | data = { |
James E. Blair | 3c483cf | 2013-06-04 16:30:43 -0700 | [diff] [blame] | 468 | 'url': 'https://server/job/%s/%s/' % (self.name, self.number), |
| 469 | 'name': self.name, |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 470 | 'number': self.number, |
James E. Blair | 3c483cf | 2013-06-04 16:30:43 -0700 | [diff] [blame] | 471 | 'manager': self.worker.worker_id, |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | self.job.sendWorkData(json.dumps(data)) |
| 475 | self.job.sendWorkStatus(0, 100) |
| 476 | |
| 477 | if self.worker.hold_jobs_in_build: |
| 478 | self._wait() |
| 479 | self.log.debug("Build %s continuing" % self.unique) |
| 480 | |
| 481 | self.worker.lock.acquire() |
| 482 | |
| 483 | result = 'SUCCESS' |
| 484 | if (('ZUUL_REF' in self.parameters) and |
| 485 | self.worker.shouldFailTest(self.name, |
| 486 | self.parameters['ZUUL_REF'])): |
| 487 | result = 'FAILURE' |
| 488 | if self.aborted: |
| 489 | result = 'ABORTED' |
| 490 | |
| 491 | data = {'result': result} |
| 492 | changes = None |
| 493 | if 'ZUUL_CHANGE_IDS' in self.parameters: |
| 494 | changes = self.parameters['ZUUL_CHANGE_IDS'] |
| 495 | |
| 496 | self.worker.build_history.append( |
| 497 | BuildHistory(name=self.name, number=self.number, |
| 498 | result=result, changes=changes, node=self.node, |
| 499 | uuid=self.unique, description=self.description) |
| 500 | ) |
| 501 | |
| 502 | self.job.sendWorkComplete(json.dumps(data)) |
| 503 | del self.worker.gearman_jobs[self.job.unique] |
| 504 | self.worker.running_builds.remove(self) |
| 505 | self.worker.lock.release() |
| 506 | |
| 507 | |
| 508 | class FakeWorker(gear.Worker): |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 509 | def __init__(self, worker_id, test): |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 510 | super(FakeWorker, self).__init__(worker_id) |
| 511 | self.gearman_jobs = {} |
| 512 | self.build_history = [] |
| 513 | self.running_builds = [] |
| 514 | self.build_counter = 0 |
| 515 | self.fail_tests = {} |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 516 | self.test = test |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 517 | |
| 518 | self.hold_jobs_in_build = False |
| 519 | self.lock = threading.Lock() |
| 520 | self.__work_thread = threading.Thread(target=self.work) |
James E. Blair | 8a6f0c2 | 2013-07-01 12:31:34 -0400 | [diff] [blame] | 521 | self.__work_thread.daemon = True |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 522 | self.__work_thread.start() |
| 523 | |
| 524 | def handleJob(self, job): |
| 525 | parts = job.name.split(":") |
| 526 | cmd = parts[0] |
| 527 | name = parts[1] |
| 528 | if len(parts) > 2: |
| 529 | node = parts[2] |
| 530 | else: |
| 531 | node = None |
| 532 | if cmd == 'build': |
| 533 | self.handleBuild(job, name, node) |
| 534 | elif cmd == 'stop': |
| 535 | self.handleStop(job, name) |
| 536 | elif cmd == 'set_description': |
| 537 | self.handleSetDescription(job, name) |
| 538 | |
| 539 | def handleBuild(self, job, name, node): |
| 540 | build = FakeBuild(self, job, self.build_counter, node) |
| 541 | job.build = build |
| 542 | self.gearman_jobs[job.unique] = job |
| 543 | self.build_counter += 1 |
| 544 | |
| 545 | self.running_builds.append(build) |
| 546 | build.start() |
| 547 | |
| 548 | def handleStop(self, job, name): |
| 549 | self.log.debug("handle stop") |
James E. Blair | 3c483cf | 2013-06-04 16:30:43 -0700 | [diff] [blame] | 550 | parameters = json.loads(job.arguments) |
| 551 | name = parameters['name'] |
| 552 | number = parameters['number'] |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 553 | for build in self.running_builds: |
James E. Blair | 3c483cf | 2013-06-04 16:30:43 -0700 | [diff] [blame] | 554 | if build.name == name and build.number == number: |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 555 | build.aborted = True |
| 556 | build.release() |
| 557 | job.sendWorkComplete() |
| 558 | return |
| 559 | job.sendWorkFail() |
| 560 | |
| 561 | def handleSetDescription(self, job, name): |
| 562 | self.log.debug("handle set description") |
| 563 | parameters = json.loads(job.arguments) |
James E. Blair | 3c483cf | 2013-06-04 16:30:43 -0700 | [diff] [blame] | 564 | name = parameters['name'] |
| 565 | number = parameters['number'] |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 566 | descr = parameters['html_description'] |
| 567 | for build in self.running_builds: |
James E. Blair | 3c483cf | 2013-06-04 16:30:43 -0700 | [diff] [blame] | 568 | if build.name == name and build.number == number: |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 569 | build.description = descr |
| 570 | job.sendWorkComplete() |
| 571 | return |
| 572 | for build in self.build_history: |
James E. Blair | 3c483cf | 2013-06-04 16:30:43 -0700 | [diff] [blame] | 573 | if build.name == name and build.number == number: |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 574 | build.description = descr |
| 575 | job.sendWorkComplete() |
| 576 | return |
| 577 | job.sendWorkFail() |
| 578 | |
| 579 | def work(self): |
| 580 | while self.running: |
| 581 | try: |
| 582 | job = self.getJob() |
| 583 | except gear.InterruptedError: |
| 584 | continue |
| 585 | try: |
| 586 | self.handleJob(job) |
| 587 | except: |
| 588 | self.log.exception("Worker exception:") |
| 589 | |
| 590 | def addFailTest(self, name, change): |
| 591 | l = self.fail_tests.get(name, []) |
| 592 | l.append(change) |
| 593 | self.fail_tests[name] = l |
| 594 | |
| 595 | def shouldFailTest(self, name, ref): |
| 596 | l = self.fail_tests.get(name, []) |
| 597 | for change in l: |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 598 | if self.test.ref_has_change(ref, change): |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 599 | return True |
| 600 | return False |
| 601 | |
| 602 | def release(self, regex=None): |
| 603 | builds = self.running_builds[:] |
| 604 | self.log.debug("releasing build %s (%s)" % (regex, |
| 605 | len(self.running_builds))) |
| 606 | for build in builds: |
| 607 | if not regex or re.match(regex, build.name): |
| 608 | self.log.debug("releasing build %s" % |
| 609 | (build.parameters['ZUUL_UUID'])) |
| 610 | build.release() |
| 611 | else: |
| 612 | self.log.debug("not releasing build %s" % |
| 613 | (build.parameters['ZUUL_UUID'])) |
| 614 | self.log.debug("done releasing builds %s (%s)" % |
| 615 | (regex, len(self.running_builds))) |
| 616 | |
| 617 | |
| 618 | class FakeGearmanServer(gear.Server): |
| 619 | def __init__(self): |
| 620 | self.hold_jobs_in_queue = False |
| 621 | super(FakeGearmanServer, self).__init__(0) |
| 622 | |
| 623 | def getJobForConnection(self, connection, peek=False): |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 624 | for queue in [self.high_queue, self.normal_queue, self.low_queue]: |
| 625 | for job in queue: |
| 626 | if not hasattr(job, 'waiting'): |
| 627 | if job.name.startswith('build:'): |
| 628 | job.waiting = self.hold_jobs_in_queue |
| 629 | else: |
| 630 | job.waiting = False |
| 631 | if job.waiting: |
| 632 | continue |
| 633 | if job.name in connection.functions: |
| 634 | if not peek: |
| 635 | queue.remove(job) |
James E. Blair | e281901 | 2013-06-28 17:17:26 -0400 | [diff] [blame] | 636 | connection.related_jobs[job.handle] = job |
| 637 | job.worker_connection = connection |
| 638 | job.running = True |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 639 | return job |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 640 | return None |
| 641 | |
| 642 | def release(self, regex=None): |
| 643 | released = False |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 644 | qlen = (len(self.high_queue) + len(self.normal_queue) + |
| 645 | len(self.low_queue)) |
| 646 | self.log.debug("releasing queued job %s (%s)" % (regex, qlen)) |
| 647 | for queue in [self.high_queue, self.normal_queue, self.low_queue]: |
| 648 | queue = queue[:] |
| 649 | for job in queue: |
| 650 | cmd, name = job.name.split(':') |
| 651 | if cmd != 'build': |
| 652 | continue |
| 653 | if not regex or re.match(regex, name): |
| 654 | self.log.debug("releasing queued job %s" % |
| 655 | job.unique) |
| 656 | job.waiting = False |
| 657 | released = True |
| 658 | else: |
| 659 | self.log.debug("not releasing queued job %s" % |
| 660 | job.unique) |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 661 | if released: |
| 662 | self.wakeConnections() |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 663 | qlen = (len(self.high_queue) + len(self.normal_queue) + |
| 664 | len(self.low_queue)) |
| 665 | self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen)) |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 666 | |
| 667 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 668 | class TestScheduler(testtools.TestCase): |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 669 | log = logging.getLogger("zuul.test") |
| 670 | |
| 671 | def setUp(self): |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 672 | super(TestScheduler, self).setUp() |
| 673 | test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0) |
| 674 | try: |
| 675 | test_timeout = int(test_timeout) |
| 676 | except ValueError: |
| 677 | # If timeout value is invalid do not set a timeout. |
| 678 | test_timeout = 0 |
| 679 | if test_timeout > 0: |
| 680 | self.useFixture(fixtures.Timeout(test_timeout, gentle=True)) |
| 681 | |
| 682 | if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or |
| 683 | os.environ.get('OS_STDOUT_CAPTURE') == '1'): |
| 684 | stdout = self.useFixture(fixtures.StringStream('stdout')).stream |
| 685 | self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) |
| 686 | if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or |
| 687 | os.environ.get('OS_STDERR_CAPTURE') == '1'): |
| 688 | stderr = self.useFixture(fixtures.StringStream('stderr')).stream |
| 689 | self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) |
| 690 | self.useFixture(fixtures.NestedTempfile()) |
Monty Taylor | e6a6c40 | 2013-07-02 09:25:55 -0700 | [diff] [blame] | 691 | if (os.environ.get('OS_LOG_CAPTURE') == 'True' or |
| 692 | os.environ.get('OS_LOG_CAPTURE') == '1'): |
James E. Blair | e281901 | 2013-06-28 17:17:26 -0400 | [diff] [blame] | 693 | self.useFixture(fixtures.FakeLogger( |
| 694 | level=logging.DEBUG, |
| 695 | format='%(asctime)s %(name)-32s ' |
| 696 | '%(levelname)-8s %(message)s')) |
| 697 | tmp_root = tempfile.mkdtemp(dir=os.environ.get("ZUUL_TEST_ROOT", |
| 698 | '/tmp')) |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 699 | self.test_root = os.path.join(tmp_root, "zuul-test") |
| 700 | self.upstream_root = os.path.join(self.test_root, "upstream") |
| 701 | self.git_root = os.path.join(self.test_root, "git") |
| 702 | |
| 703 | CONFIG.set('zuul', 'git_dir', self.git_root) |
| 704 | if os.path.exists(self.test_root): |
| 705 | shutil.rmtree(self.test_root) |
| 706 | os.makedirs(self.test_root) |
| 707 | os.makedirs(self.upstream_root) |
| 708 | os.makedirs(self.git_root) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 709 | |
| 710 | # For each project in config: |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 711 | self.init_repo("org/project") |
| 712 | self.init_repo("org/project1") |
| 713 | self.init_repo("org/project2") |
| 714 | self.init_repo("org/project3") |
| 715 | self.init_repo("org/one-job-project") |
| 716 | self.init_repo("org/nonvoting-project") |
| 717 | self.init_repo("org/templated-project") |
| 718 | self.init_repo("org/node-project") |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 719 | |
| 720 | self.statsd = FakeStatsd() |
| 721 | os.environ['STATSD_HOST'] = 'localhost' |
| 722 | os.environ['STATSD_PORT'] = str(self.statsd.port) |
| 723 | self.statsd.start() |
| 724 | # the statsd client object is configured in the statsd module import |
| 725 | reload(statsd) |
| 726 | reload(zuul.scheduler) |
| 727 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 728 | self.gearman_server = FakeGearmanServer() |
| 729 | |
| 730 | self.config = ConfigParser.ConfigParser() |
| 731 | cfg = StringIO() |
| 732 | CONFIG.write(cfg) |
| 733 | cfg.seek(0) |
| 734 | self.config.readfp(cfg) |
| 735 | self.config.set('gearman', 'port', str(self.gearman_server.port)) |
| 736 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 737 | self.worker = FakeWorker('fake_worker', self) |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 738 | self.worker.addServer('127.0.0.1', self.gearman_server.port) |
| 739 | self.gearman_server.worker = self.worker |
| 740 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 741 | self.sched = zuul.scheduler.Scheduler() |
| 742 | |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 743 | def URLOpenerFactory(*args, **kw): |
| 744 | args = [self.fake_gerrit] + list(args) |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 745 | return FakeURLOpener(self.upstream_root, *args, **kw) |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 746 | |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 747 | urllib2.urlopen = URLOpenerFactory |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 748 | self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 749 | |
| 750 | zuul.lib.gerrit.Gerrit = FakeGerrit |
| 751 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 752 | self.gerrit = FakeGerritTrigger( |
| 753 | self.upstream_root, self.config, self.sched) |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 754 | self.gerrit.replication_timeout = 1.5 |
| 755 | self.gerrit.replication_retry_interval = 0.5 |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 756 | self.fake_gerrit = self.gerrit.gerrit |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 757 | self.fake_gerrit.upstream_root = self.upstream_root |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 758 | |
James E. Blair | 1843a55 | 2013-07-03 14:19:52 -0700 | [diff] [blame^] | 759 | self.webapp = zuul.webapp.WebApp(self.sched, port=0) |
| 760 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 761 | self.sched.setLauncher(self.launcher) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 762 | self.sched.setTrigger(self.gerrit) |
| 763 | |
| 764 | self.sched.start() |
| 765 | self.sched.reconfigure(self.config) |
| 766 | self.sched.resume() |
James E. Blair | 1843a55 | 2013-07-03 14:19:52 -0700 | [diff] [blame^] | 767 | self.webapp.start() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 768 | self.launcher.gearman.waitForServer() |
| 769 | self.registerJobs() |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 770 | self.builds = self.worker.running_builds |
| 771 | self.history = self.worker.build_history |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 772 | |
| 773 | def tearDown(self): |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 774 | self.launcher.stop() |
| 775 | self.worker.shutdown() |
| 776 | self.gearman_server.shutdown() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 777 | self.gerrit.stop() |
| 778 | self.sched.stop() |
| 779 | self.sched.join() |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 780 | self.statsd.stop() |
| 781 | self.statsd.join() |
James E. Blair | 1843a55 | 2013-07-03 14:19:52 -0700 | [diff] [blame^] | 782 | self.webapp.stop() |
| 783 | self.webapp.join() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 784 | threads = threading.enumerate() |
| 785 | if len(threads) > 1: |
| 786 | self.log.error("More than one thread is running: %s" % threads) |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 787 | super(TestScheduler, self).tearDown() |
| 788 | |
| 789 | def init_repo(self, project): |
| 790 | parts = project.split('/') |
| 791 | path = os.path.join(self.upstream_root, *parts[:-1]) |
| 792 | if not os.path.exists(path): |
| 793 | os.makedirs(path) |
| 794 | path = os.path.join(self.upstream_root, project) |
| 795 | repo = git.Repo.init(path) |
| 796 | |
| 797 | repo.config_writer().set_value('user', 'email', 'user@example.com') |
| 798 | repo.config_writer().set_value('user', 'name', 'User Name') |
| 799 | repo.config_writer().write() |
| 800 | |
| 801 | fn = os.path.join(path, 'README') |
| 802 | f = open(fn, 'w') |
| 803 | f.write("test\n") |
| 804 | f.close() |
| 805 | repo.index.add([fn]) |
| 806 | repo.index.commit('initial commit') |
| 807 | master = repo.create_head('master') |
| 808 | repo.create_tag('init') |
| 809 | |
| 810 | mp = repo.create_head('mp') |
| 811 | repo.head.reference = mp |
| 812 | f = open(fn, 'a') |
| 813 | f.write("test mp\n") |
| 814 | f.close() |
| 815 | repo.index.add([fn]) |
| 816 | repo.index.commit('mp commit') |
| 817 | |
| 818 | repo.head.reference = master |
| 819 | repo.head.reset(index=True, working_tree=True) |
| 820 | repo.git.clean('-x', '-f', '-d') |
| 821 | |
| 822 | def ref_has_change(self, ref, change): |
| 823 | path = os.path.join(self.git_root, change.project) |
| 824 | repo = git.Repo(path) |
| 825 | for commit in repo.iter_commits(ref): |
| 826 | if commit.message.strip() == ('%s-1' % change.subject): |
| 827 | return True |
| 828 | return False |
| 829 | |
| 830 | def job_has_changes(self, *args): |
| 831 | job = args[0] |
| 832 | commits = args[1:] |
| 833 | if isinstance(job, FakeBuild): |
| 834 | parameters = job.parameters |
| 835 | else: |
| 836 | parameters = json.loads(job.arguments) |
| 837 | project = parameters['ZUUL_PROJECT'] |
| 838 | path = os.path.join(self.git_root, project) |
| 839 | repo = git.Repo(path) |
| 840 | ref = parameters['ZUUL_REF'] |
| 841 | sha = parameters['ZUUL_COMMIT'] |
| 842 | repo_messages = [c.message.strip() for c in repo.iter_commits(ref)] |
| 843 | repo_shas = [c.hexsha for c in repo.iter_commits(ref)] |
| 844 | commit_messages = ['%s-1' % commit.subject for commit in commits] |
| 845 | for msg in commit_messages: |
| 846 | if msg not in repo_messages: |
| 847 | return False |
| 848 | if repo_shas[0] != sha: |
| 849 | return False |
| 850 | return True |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 851 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 852 | def registerJobs(self): |
| 853 | count = 0 |
| 854 | for job in self.sched.jobs.keys(): |
| 855 | self.worker.registerFunction('build:' + job) |
| 856 | count += 1 |
| 857 | self.worker.registerFunction('stop:' + self.worker.worker_id) |
| 858 | count += 1 |
| 859 | |
| 860 | while len(self.gearman_server.functions) < count: |
| 861 | time.sleep(0) |
| 862 | |
| 863 | def release(self, job): |
| 864 | if isinstance(job, FakeBuild): |
| 865 | job.release() |
| 866 | else: |
| 867 | job.waiting = False |
| 868 | self.log.debug("Queued job %s released" % job.unique) |
| 869 | self.gearman_server.wakeConnections() |
| 870 | |
| 871 | def getParameter(self, job, name): |
| 872 | if isinstance(job, FakeBuild): |
| 873 | return job.parameters[name] |
| 874 | else: |
| 875 | parameters = json.loads(job.arguments) |
| 876 | return parameters[name] |
| 877 | |
| 878 | def resetGearmanServer(self): |
| 879 | self.worker.setFunctions([]) |
| 880 | while True: |
| 881 | done = True |
| 882 | for connection in self.gearman_server.active_connections: |
| 883 | if connection.functions: |
| 884 | done = False |
| 885 | if done: |
| 886 | break |
| 887 | time.sleep(0) |
| 888 | self.gearman_server.functions = set() |
| 889 | |
| 890 | def haveAllBuildsReported(self): |
| 891 | # See if Zuul is waiting on a meta job to complete |
| 892 | if self.launcher.meta_jobs: |
| 893 | return False |
| 894 | # Find out if every build that the worker has completed has been |
| 895 | # reported back to Zuul. If it hasn't then that means a Gearman |
| 896 | # event is still in transit and the system is not stable. |
| 897 | for build in self.worker.build_history: |
| 898 | zbuild = self.launcher.builds.get(build.uuid) |
| 899 | if not zbuild: |
| 900 | # It has already been reported |
| 901 | continue |
| 902 | # It hasn't been reported yet. |
| 903 | return False |
| 904 | # Make sure that none of the worker connections are in GRAB_WAIT |
| 905 | for connection in self.worker.active_connections: |
| 906 | if connection.state == 'GRAB_WAIT': |
| 907 | return False |
| 908 | return True |
| 909 | |
| 910 | def areAllBuildsWaiting(self): |
| 911 | ret = True |
| 912 | |
| 913 | builds = self.launcher.builds.values() |
| 914 | for build in builds: |
| 915 | client_job = None |
| 916 | for conn in self.launcher.gearman.active_connections: |
| 917 | for j in conn.related_jobs.values(): |
| 918 | if j.unique == build.uuid: |
| 919 | client_job = j |
| 920 | break |
| 921 | if not client_job: |
| 922 | self.log.debug("%s is not known to the gearman client" % |
| 923 | build) |
| 924 | ret = False |
| 925 | continue |
| 926 | if not client_job.handle: |
| 927 | self.log.debug("%s has no handle" % client_job) |
| 928 | ret = False |
| 929 | continue |
| 930 | server_job = self.gearman_server.jobs.get(client_job.handle) |
| 931 | if not server_job: |
| 932 | self.log.debug("%s is not known to the gearman server" % |
| 933 | client_job) |
| 934 | ret = False |
| 935 | continue |
| 936 | if not hasattr(server_job, 'waiting'): |
| 937 | self.log.debug("%s is being enqueued" % server_job) |
| 938 | ret = False |
| 939 | continue |
| 940 | if server_job.waiting: |
| 941 | continue |
| 942 | worker_job = self.worker.gearman_jobs.get(server_job.unique) |
| 943 | if worker_job: |
| 944 | if worker_job.build.isWaiting(): |
| 945 | continue |
| 946 | else: |
| 947 | self.log.debug("%s is running" % worker_job) |
| 948 | ret = False |
| 949 | else: |
| 950 | self.log.debug("%s is unassigned" % server_job) |
| 951 | ret = False |
| 952 | return ret |
| 953 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 954 | def waitUntilSettled(self): |
| 955 | self.log.debug("Waiting until settled...") |
| 956 | start = time.time() |
| 957 | while True: |
| 958 | if time.time() - start > 10: |
| 959 | print 'queue status:', |
| 960 | print self.sched.trigger_event_queue.empty(), |
| 961 | print self.sched.result_event_queue.empty(), |
| 962 | print self.fake_gerrit.event_queue.empty(), |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 963 | print self.areAllBuildsWaiting() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 964 | raise Exception("Timeout waiting for Zuul to settle") |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 965 | # Make sure no new events show up while we're checking |
| 966 | self.worker.lock.acquire() |
| 967 | # have all build states propogated to zuul? |
| 968 | if self.haveAllBuildsReported(): |
| 969 | # Join ensures that the queue is empty _and_ events have been |
| 970 | # processed |
| 971 | self.fake_gerrit.event_queue.join() |
| 972 | self.sched.trigger_event_queue.join() |
| 973 | self.sched.result_event_queue.join() |
| 974 | if (self.sched.trigger_event_queue.empty() and |
| 975 | self.sched.result_event_queue.empty() and |
| 976 | self.fake_gerrit.event_queue.empty() and |
| 977 | self.areAllBuildsWaiting()): |
| 978 | self.worker.lock.release() |
| 979 | self.log.debug("...settled.") |
| 980 | return |
| 981 | self.worker.lock.release() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 982 | self.sched.wake_event.wait(0.1) |
| 983 | |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 984 | def countJobResults(self, jobs, result): |
James E. Blair | 0018a6c | 2013-02-27 14:11:45 -0800 | [diff] [blame] | 985 | jobs = filter(lambda x: x.result == result, jobs) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 986 | return len(jobs) |
| 987 | |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 988 | def getJobFromHistory(self, name): |
| 989 | history = self.worker.build_history |
| 990 | for job in history: |
| 991 | if job.name == name: |
| 992 | return job |
| 993 | raise Exception("Unable to find job %s in history" % name) |
| 994 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 995 | def assertEmptyQueues(self): |
| 996 | # Make sure there are no orphaned jobs |
| 997 | for pipeline in self.sched.pipelines.values(): |
| 998 | for queue in pipeline.queues: |
| 999 | if len(queue.queue) != 0: |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 1000 | print 'pipeline %s queue %s contents %s' % ( |
| 1001 | pipeline.name, queue.name, queue.queue) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1002 | assert len(queue.queue) == 0 |
| 1003 | if len(queue.severed_heads) != 0: |
| 1004 | print 'heads', queue.severed_heads |
| 1005 | assert len(queue.severed_heads) == 0 |
| 1006 | |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 1007 | def assertReportedStat(self, key, value=None): |
| 1008 | start = time.time() |
| 1009 | while time.time() < (start + 5): |
| 1010 | for stat in self.statsd.stats: |
| 1011 | k, v = stat.split(':') |
| 1012 | if key == k: |
| 1013 | if value is None: |
| 1014 | return |
| 1015 | if value == v: |
| 1016 | return |
| 1017 | time.sleep(0.1) |
| 1018 | |
| 1019 | pprint.pprint(self.statsd.stats) |
| 1020 | raise Exception("Key %s not found in reported stats" % key) |
| 1021 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1022 | def test_jobs_launched(self): |
| 1023 | "Test that jobs are launched and a change is merged" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1024 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1025 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1026 | A.addApproval('CRVW', 2) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1027 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1028 | self.waitUntilSettled() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1029 | assert self.getJobFromHistory('project-merge').result == 'SUCCESS' |
| 1030 | assert self.getJobFromHistory('project-test1').result == 'SUCCESS' |
| 1031 | assert self.getJobFromHistory('project-test2').result == 'SUCCESS' |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1032 | assert A.data['status'] == 'MERGED' |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1033 | assert A.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1034 | self.assertEmptyQueues() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1035 | |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 1036 | self.assertReportedStat('gerrit.event.comment-added', '1|c') |
| 1037 | self.assertReportedStat('zuul.pipeline.gate.current_changes', '1|g') |
| 1038 | self.assertReportedStat('zuul.job.project-merge') |
| 1039 | self.assertReportedStat('zuul.pipeline.gate.resident_time') |
| 1040 | self.assertReportedStat('zuul.pipeline.gate.total_changes', '1|c') |
| 1041 | self.assertReportedStat( |
| 1042 | 'zuul.pipeline.gate.org.project.resident_time') |
| 1043 | self.assertReportedStat( |
| 1044 | 'zuul.pipeline.gate.org.project.total_changes', '1|c') |
| 1045 | |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 1046 | def test_duplicate_pipelines(self): |
| 1047 | "Test that a change matching multiple pipelines works" |
James E. Blair | 1b4d972 | 2013-05-21 10:32:04 -0700 | [diff] [blame] | 1048 | |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 1049 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1050 | self.fake_gerrit.addEvent(A.getChangeRestoredEvent()) |
| 1051 | self.waitUntilSettled() |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 1052 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1053 | print self.builds |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 1054 | print A.messages |
| 1055 | |
| 1056 | self.assertEmptyQueues() |
| 1057 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1058 | assert len(self.history) == 2 |
| 1059 | self.history[0].name == 'project-test1' |
| 1060 | self.history[1].name == 'project-test1' |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 1061 | |
| 1062 | assert len(A.messages) == 2 |
| 1063 | if 'dup1/project-test1' in A.messages[0]: |
| 1064 | assert 'dup1/project-test1' in A.messages[0] |
| 1065 | assert 'dup2/project-test1' not in A.messages[0] |
| 1066 | assert 'dup1/project-test1' not in A.messages[1] |
| 1067 | assert 'dup2/project-test1' in A.messages[1] |
| 1068 | else: |
| 1069 | assert 'dup1/project-test1' in A.messages[1] |
| 1070 | assert 'dup2/project-test1' not in A.messages[1] |
| 1071 | assert 'dup1/project-test1' not in A.messages[0] |
| 1072 | assert 'dup2/project-test1' in A.messages[0] |
| 1073 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1074 | def test_parallel_changes(self): |
| 1075 | "Test that changes are tested in parallel and merged in series" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1076 | |
| 1077 | self.worker.hold_jobs_in_build = True |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1078 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1079 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1080 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1081 | A.addApproval('CRVW', 2) |
| 1082 | B.addApproval('CRVW', 2) |
| 1083 | C.addApproval('CRVW', 2) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1084 | |
| 1085 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1086 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1087 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1088 | |
| 1089 | self.waitUntilSettled() |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1090 | assert len(self.builds) == 1 |
| 1091 | assert self.builds[0].name == 'project-merge' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1092 | assert self.job_has_changes(self.builds[0], A) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1093 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1094 | self.worker.release('.*-merge') |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1095 | self.waitUntilSettled() |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1096 | assert len(self.builds) == 3 |
| 1097 | assert self.builds[0].name == 'project-test1' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1098 | assert self.job_has_changes(self.builds[0], A) |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1099 | assert self.builds[1].name == 'project-test2' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1100 | assert self.job_has_changes(self.builds[1], A) |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1101 | assert self.builds[2].name == 'project-merge' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1102 | assert self.job_has_changes(self.builds[2], A, B) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1103 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1104 | self.worker.release('.*-merge') |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1105 | self.waitUntilSettled() |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1106 | assert len(self.builds) == 5 |
| 1107 | assert self.builds[0].name == 'project-test1' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1108 | assert self.job_has_changes(self.builds[0], A) |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1109 | assert self.builds[1].name == 'project-test2' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1110 | assert self.job_has_changes(self.builds[1], A) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1111 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1112 | assert self.builds[2].name == 'project-test1' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1113 | assert self.job_has_changes(self.builds[2], A, B) |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1114 | assert self.builds[3].name == 'project-test2' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1115 | assert self.job_has_changes(self.builds[3], A, B) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1116 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1117 | assert self.builds[4].name == 'project-merge' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1118 | assert self.job_has_changes(self.builds[4], A, B, C) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1119 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1120 | self.worker.release('.*-merge') |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1121 | self.waitUntilSettled() |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1122 | assert len(self.builds) == 6 |
| 1123 | assert self.builds[0].name == 'project-test1' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1124 | assert self.job_has_changes(self.builds[0], A) |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1125 | assert self.builds[1].name == 'project-test2' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1126 | assert self.job_has_changes(self.builds[1], A) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1127 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1128 | assert self.builds[2].name == 'project-test1' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1129 | assert self.job_has_changes(self.builds[2], A, B) |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1130 | assert self.builds[3].name == 'project-test2' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1131 | assert self.job_has_changes(self.builds[3], A, B) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1132 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1133 | assert self.builds[4].name == 'project-test1' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1134 | assert self.job_has_changes(self.builds[4], A, B, C) |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1135 | assert self.builds[5].name == 'project-test2' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1136 | assert self.job_has_changes(self.builds[5], A, B, C) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1137 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1138 | self.worker.hold_jobs_in_build = False |
| 1139 | self.worker.release() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1140 | self.waitUntilSettled() |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1141 | assert len(self.builds) == 0 |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1142 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1143 | assert len(self.history) == 9 |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1144 | assert A.data['status'] == 'MERGED' |
| 1145 | assert B.data['status'] == 'MERGED' |
| 1146 | assert C.data['status'] == 'MERGED' |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1147 | assert A.reported == 2 |
| 1148 | assert B.reported == 2 |
| 1149 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1150 | self.assertEmptyQueues() |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1151 | |
| 1152 | def test_failed_changes(self): |
| 1153 | "Test that a change behind a failed change is retested" |
James E. Blair | e281901 | 2013-06-28 17:17:26 -0400 | [diff] [blame] | 1154 | self.worker.hold_jobs_in_build = True |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1155 | |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1156 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1157 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1158 | A.addApproval('CRVW', 2) |
| 1159 | B.addApproval('CRVW', 2) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1160 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1161 | self.worker.addFailTest('project-test1', A) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1162 | |
James E. Blair | e281901 | 2013-06-28 17:17:26 -0400 | [diff] [blame] | 1163 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1164 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1165 | self.waitUntilSettled() |
James E. Blair | e281901 | 2013-06-28 17:17:26 -0400 | [diff] [blame] | 1166 | |
| 1167 | self.worker.release('.*-merge') |
| 1168 | self.waitUntilSettled() |
| 1169 | |
| 1170 | self.worker.hold_jobs_in_build = False |
| 1171 | self.worker.release() |
| 1172 | |
| 1173 | self.waitUntilSettled() |
| 1174 | # It's certain that the merge job for change 2 will run, but |
| 1175 | # the test1 and test2 jobs may or may not run. |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1176 | assert len(self.history) > 6 |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1177 | assert A.data['status'] == 'NEW' |
| 1178 | assert B.data['status'] == 'MERGED' |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1179 | assert A.reported == 2 |
| 1180 | assert B.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1181 | self.assertEmptyQueues() |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1182 | |
| 1183 | def test_independent_queues(self): |
| 1184 | "Test that changes end up in the right queues" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1185 | |
| 1186 | self.worker.hold_jobs_in_build = True |
Zhongyue Luo | 5d55607 | 2012-09-21 02:00:47 +0900 | [diff] [blame] | 1187 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1188 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 1189 | C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1190 | A.addApproval('CRVW', 2) |
| 1191 | B.addApproval('CRVW', 2) |
| 1192 | C.addApproval('CRVW', 2) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1193 | |
| 1194 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1195 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1196 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1197 | |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1198 | self.waitUntilSettled() |
| 1199 | |
| 1200 | # There should be one merge job at the head of each queue running |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1201 | assert len(self.builds) == 2 |
| 1202 | assert self.builds[0].name == 'project-merge' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1203 | assert self.job_has_changes(self.builds[0], A) |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1204 | assert self.builds[1].name == 'project1-merge' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1205 | assert self.job_has_changes(self.builds[1], B) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1206 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1207 | # Release the current merge builds |
| 1208 | self.worker.release('.*-merge') |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1209 | self.waitUntilSettled() |
| 1210 | # Release the merge job for project2 which is behind project1 |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1211 | self.worker.release('.*-merge') |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1212 | self.waitUntilSettled() |
| 1213 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1214 | # All the test builds should be running: |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1215 | # project1 (3) + project2 (3) + project (2) = 8 |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1216 | assert len(self.builds) == 8 |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1217 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1218 | self.worker.release() |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1219 | self.waitUntilSettled() |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1220 | assert len(self.builds) == 0 |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1221 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1222 | assert len(self.history) == 11 |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1223 | assert A.data['status'] == 'MERGED' |
| 1224 | assert B.data['status'] == 'MERGED' |
| 1225 | assert C.data['status'] == 'MERGED' |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1226 | assert A.reported == 2 |
| 1227 | assert B.reported == 2 |
| 1228 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1229 | self.assertEmptyQueues() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1230 | |
| 1231 | def test_failed_change_at_head(self): |
| 1232 | "Test that if a change at the head fails, jobs behind it are canceled" |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1233 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1234 | self.worker.hold_jobs_in_build = True |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1235 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1236 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1237 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1238 | A.addApproval('CRVW', 2) |
| 1239 | B.addApproval('CRVW', 2) |
| 1240 | C.addApproval('CRVW', 2) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1241 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1242 | self.worker.addFailTest('project-test1', A) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1243 | |
| 1244 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1245 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1246 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1247 | |
| 1248 | self.waitUntilSettled() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1249 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1250 | assert len(self.builds) == 1 |
| 1251 | assert self.builds[0].name == 'project-merge' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1252 | assert self.job_has_changes(self.builds[0], A) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1253 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1254 | self.worker.release('.*-merge') |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1255 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1256 | self.worker.release('.*-merge') |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1257 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1258 | self.worker.release('.*-merge') |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1259 | self.waitUntilSettled() |
| 1260 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1261 | assert len(self.builds) == 6 |
| 1262 | assert self.builds[0].name == 'project-test1' |
| 1263 | assert self.builds[1].name == 'project-test2' |
| 1264 | assert self.builds[2].name == 'project-test1' |
| 1265 | assert self.builds[3].name == 'project-test2' |
| 1266 | assert self.builds[4].name == 'project-test1' |
| 1267 | assert self.builds[5].name == 'project-test2' |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1268 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1269 | self.release(self.builds[0]) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1270 | self.waitUntilSettled() |
| 1271 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1272 | assert len(self.builds) == 2 # project-test2, project-merge for B |
| 1273 | assert self.countJobResults(self.history, 'ABORTED') == 4 |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1274 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1275 | self.worker.hold_jobs_in_build = False |
| 1276 | self.worker.release() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1277 | self.waitUntilSettled() |
| 1278 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1279 | assert len(self.builds) == 0 |
| 1280 | assert len(self.history) == 15 |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1281 | assert A.data['status'] == 'NEW' |
| 1282 | assert B.data['status'] == 'MERGED' |
| 1283 | assert C.data['status'] == 'MERGED' |
| 1284 | assert A.reported == 2 |
| 1285 | assert B.reported == 2 |
| 1286 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1287 | self.assertEmptyQueues() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1288 | |
| 1289 | def test_failed_change_at_head_with_queue(self): |
| 1290 | "Test that if a change at the head fails, queued jobs are canceled" |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1291 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1292 | self.gearman_server.hold_jobs_in_queue = True |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1293 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1294 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1295 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1296 | A.addApproval('CRVW', 2) |
| 1297 | B.addApproval('CRVW', 2) |
| 1298 | C.addApproval('CRVW', 2) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1299 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1300 | self.worker.addFailTest('project-test1', A) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1301 | |
| 1302 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1303 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1304 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1305 | |
| 1306 | self.waitUntilSettled() |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 1307 | queue = self.gearman_server.getQueue() |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1308 | assert len(self.builds) == 0 |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1309 | assert len(queue) == 1 |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1310 | assert queue[0].name == 'build:project-merge' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1311 | assert self.job_has_changes(queue[0], A) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1312 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1313 | self.gearman_server.release('.*-merge') |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1314 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1315 | self.gearman_server.release('.*-merge') |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1316 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1317 | self.gearman_server.release('.*-merge') |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1318 | self.waitUntilSettled() |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 1319 | queue = self.gearman_server.getQueue() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1320 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1321 | assert len(self.builds) == 0 |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1322 | assert len(queue) == 6 |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1323 | assert queue[0].name == 'build:project-test1' |
| 1324 | assert queue[1].name == 'build:project-test2' |
| 1325 | assert queue[2].name == 'build:project-test1' |
| 1326 | assert queue[3].name == 'build:project-test2' |
| 1327 | assert queue[4].name == 'build:project-test1' |
| 1328 | assert queue[5].name == 'build:project-test2' |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1329 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1330 | self.release(queue[0]) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1331 | self.waitUntilSettled() |
| 1332 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1333 | assert len(self.builds) == 0 |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 1334 | queue = self.gearman_server.getQueue() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1335 | assert len(queue) == 2 # project-test2, project-merge for B |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1336 | assert self.countJobResults(self.history, 'ABORTED') == 0 |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1337 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1338 | self.gearman_server.hold_jobs_in_queue = False |
| 1339 | self.gearman_server.release() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1340 | self.waitUntilSettled() |
| 1341 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1342 | assert len(self.builds) == 0 |
| 1343 | assert len(self.history) == 11 |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1344 | assert A.data['status'] == 'NEW' |
| 1345 | assert B.data['status'] == 'MERGED' |
| 1346 | assert C.data['status'] == 'MERGED' |
| 1347 | assert A.reported == 2 |
| 1348 | assert B.reported == 2 |
| 1349 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1350 | self.assertEmptyQueues() |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1351 | |
| 1352 | def test_patch_order(self): |
| 1353 | "Test that dependent patches are tested in the right order" |
| 1354 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1355 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1356 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1357 | A.addApproval('CRVW', 2) |
| 1358 | B.addApproval('CRVW', 2) |
| 1359 | C.addApproval('CRVW', 2) |
| 1360 | |
| 1361 | M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2') |
| 1362 | M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1') |
| 1363 | M2.setMerged() |
| 1364 | M1.setMerged() |
| 1365 | |
| 1366 | # C -> B -> A -> M1 -> M2 |
| 1367 | # M2 is here to make sure it is never queried. If it is, it |
| 1368 | # means zuul is walking down the entire history of merged |
| 1369 | # changes. |
| 1370 | |
| 1371 | C.setDependsOn(B, 1) |
| 1372 | B.setDependsOn(A, 1) |
| 1373 | A.setDependsOn(M1, 1) |
| 1374 | M1.setDependsOn(M2, 1) |
| 1375 | |
| 1376 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1377 | |
| 1378 | self.waitUntilSettled() |
| 1379 | |
| 1380 | assert A.data['status'] == 'NEW' |
| 1381 | assert B.data['status'] == 'NEW' |
| 1382 | assert C.data['status'] == 'NEW' |
| 1383 | |
| 1384 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1385 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1386 | |
| 1387 | self.waitUntilSettled() |
| 1388 | assert M2.queried == 0 |
| 1389 | assert A.data['status'] == 'MERGED' |
| 1390 | assert B.data['status'] == 'MERGED' |
| 1391 | assert C.data['status'] == 'MERGED' |
| 1392 | assert A.reported == 2 |
| 1393 | assert B.reported == 2 |
| 1394 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1395 | self.assertEmptyQueues() |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1396 | |
| 1397 | def test_can_merge(self): |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1398 | "Test whether a change is ready to merge" |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1399 | # TODO: move to test_gerrit (this is a unit test!) |
| 1400 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1401 | a = self.sched.trigger.getChange(1, 2) |
| 1402 | mgr = self.sched.pipelines['gate'].manager |
| 1403 | assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()) |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1404 | |
| 1405 | A.addApproval('CRVW', 2) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1406 | a = self.sched.trigger.getChange(1, 2) |
| 1407 | assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()) |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1408 | |
| 1409 | A.addApproval('APRV', 1) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1410 | a = self.sched.trigger.getChange(1, 2) |
| 1411 | assert self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1412 | self.assertEmptyQueues() |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1413 | |
| 1414 | def test_build_configuration(self): |
| 1415 | "Test that zuul merges the right commits for testing" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1416 | |
| 1417 | self.gearman_server.hold_jobs_in_queue = True |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1418 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1419 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1420 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1421 | A.addApproval('CRVW', 2) |
| 1422 | B.addApproval('CRVW', 2) |
| 1423 | C.addApproval('CRVW', 2) |
| 1424 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1425 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1426 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1427 | self.waitUntilSettled() |
| 1428 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1429 | self.gearman_server.release('.*-merge') |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1430 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1431 | self.gearman_server.release('.*-merge') |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1432 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1433 | self.gearman_server.release('.*-merge') |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1434 | self.waitUntilSettled() |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 1435 | queue = self.gearman_server.getQueue() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1436 | ref = self.getParameter(queue[-1], 'ZUUL_REF') |
| 1437 | self.gearman_server.hold_jobs_in_queue = False |
| 1438 | self.gearman_server.release() |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1439 | self.waitUntilSettled() |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1440 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1441 | path = os.path.join(self.git_root, "org/project") |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1442 | repo = git.Repo(path) |
| 1443 | repo_messages = [c.message.strip() for c in repo.iter_commits(ref)] |
| 1444 | repo_messages.reverse() |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1445 | correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1'] |
| 1446 | assert repo_messages == correct_messages |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1447 | self.assertEmptyQueues() |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1448 | |
| 1449 | def test_build_configuration_conflict(self): |
| 1450 | "Test that merge conflicts are handled" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1451 | |
| 1452 | self.gearman_server.hold_jobs_in_queue = True |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1453 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1454 | A.addPatchset(['conflict']) |
| 1455 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1456 | B.addPatchset(['conflict']) |
| 1457 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1458 | A.addApproval('CRVW', 2) |
| 1459 | B.addApproval('CRVW', 2) |
| 1460 | C.addApproval('CRVW', 2) |
| 1461 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1462 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1463 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1464 | self.waitUntilSettled() |
| 1465 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1466 | self.gearman_server.release('.*-merge') |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1467 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1468 | self.gearman_server.release('.*-merge') |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1469 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1470 | self.gearman_server.release('.*-merge') |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1471 | self.waitUntilSettled() |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 1472 | queue = self.gearman_server.getQueue() |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1473 | self.getParameter(queue[-1], 'ZUUL_REF') |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1474 | self.gearman_server.hold_jobs_in_queue = False |
| 1475 | self.gearman_server.release() |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1476 | self.waitUntilSettled() |
| 1477 | |
| 1478 | assert A.data['status'] == 'MERGED' |
| 1479 | assert B.data['status'] == 'NEW' |
| 1480 | assert C.data['status'] == 'MERGED' |
| 1481 | assert A.reported == 2 |
| 1482 | assert B.reported == 2 |
| 1483 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1484 | self.assertEmptyQueues() |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 1485 | |
| 1486 | def test_post(self): |
| 1487 | "Test that post jobs run" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1488 | |
Zhongyue Luo | 5d55607 | 2012-09-21 02:00:47 +0900 | [diff] [blame] | 1489 | e = { |
| 1490 | "type": "ref-updated", |
| 1491 | "submitter": { |
| 1492 | "name": "User Name", |
| 1493 | }, |
| 1494 | "refUpdate": { |
| 1495 | "oldRev": "90f173846e3af9154517b88543ffbd1691f31366", |
| 1496 | "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543", |
| 1497 | "refName": "master", |
| 1498 | "project": "org/project", |
| 1499 | } |
| 1500 | } |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 1501 | self.fake_gerrit.addEvent(e) |
| 1502 | self.waitUntilSettled() |
| 1503 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1504 | job_names = [x.name for x in self.history] |
| 1505 | assert len(self.history) == 1 |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 1506 | assert 'project-post' in job_names |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1507 | self.assertEmptyQueues() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1508 | |
| 1509 | def test_build_configuration_branch(self): |
| 1510 | "Test that the right commits are on alternate branches" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1511 | |
| 1512 | self.gearman_server.hold_jobs_in_queue = True |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1513 | A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A') |
| 1514 | B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B') |
| 1515 | C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C') |
| 1516 | A.addApproval('CRVW', 2) |
| 1517 | B.addApproval('CRVW', 2) |
| 1518 | C.addApproval('CRVW', 2) |
| 1519 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1520 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1521 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1522 | self.waitUntilSettled() |
| 1523 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1524 | self.gearman_server.release('.*-merge') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1525 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1526 | self.gearman_server.release('.*-merge') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1527 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1528 | self.gearman_server.release('.*-merge') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1529 | self.waitUntilSettled() |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 1530 | queue = self.gearman_server.getQueue() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1531 | ref = self.getParameter(queue[-1], 'ZUUL_REF') |
| 1532 | self.gearman_server.hold_jobs_in_queue = False |
| 1533 | self.gearman_server.release() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1534 | self.waitUntilSettled() |
| 1535 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1536 | path = os.path.join(self.git_root, "org/project") |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1537 | repo = git.Repo(path) |
| 1538 | repo_messages = [c.message.strip() for c in repo.iter_commits(ref)] |
| 1539 | repo_messages.reverse() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1540 | correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1'] |
| 1541 | assert repo_messages == correct_messages |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1542 | self.assertEmptyQueues() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1543 | |
| 1544 | def test_build_configuration_branch_interaction(self): |
| 1545 | "Test that switching between branches works" |
| 1546 | self.test_build_configuration() |
| 1547 | self.test_build_configuration_branch() |
| 1548 | # C has been merged, undo that |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1549 | path = os.path.join(self.upstream_root, "org/project") |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1550 | repo = git.Repo(path) |
| 1551 | repo.heads.master.commit = repo.commit('init') |
| 1552 | self.test_build_configuration() |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1553 | self.assertEmptyQueues() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1554 | |
| 1555 | def test_build_configuration_multi_branch(self): |
| 1556 | "Test that dependent changes on multiple branches are merged" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1557 | |
| 1558 | self.gearman_server.hold_jobs_in_queue = True |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1559 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1560 | B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B') |
| 1561 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1562 | A.addApproval('CRVW', 2) |
| 1563 | B.addApproval('CRVW', 2) |
| 1564 | C.addApproval('CRVW', 2) |
| 1565 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1566 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1567 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1568 | self.waitUntilSettled() |
| 1569 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1570 | self.gearman_server.release('.*-merge') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1571 | self.waitUntilSettled() |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 1572 | queue = self.gearman_server.getQueue() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1573 | ref_mp = self.getParameter(queue[-1], 'ZUUL_REF') |
| 1574 | self.gearman_server.release('.*-merge') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1575 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1576 | self.gearman_server.release('.*-merge') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1577 | self.waitUntilSettled() |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 1578 | queue = self.gearman_server.getQueue() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1579 | ref_master = self.getParameter(queue[-1], 'ZUUL_REF') |
| 1580 | self.gearman_server.hold_jobs_in_queue = False |
| 1581 | self.gearman_server.release() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1582 | self.waitUntilSettled() |
| 1583 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1584 | path = os.path.join(self.git_root, "org/project") |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1585 | repo = git.Repo(path) |
| 1586 | |
| 1587 | repo_messages = [c.message.strip() |
| 1588 | for c in repo.iter_commits(ref_master)] |
| 1589 | repo_messages.reverse() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1590 | correct_messages = ['initial commit', 'A-1', 'C-1'] |
| 1591 | assert repo_messages == correct_messages |
| 1592 | |
| 1593 | repo_messages = [c.message.strip() |
| 1594 | for c in repo.iter_commits(ref_mp)] |
| 1595 | repo_messages.reverse() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1596 | correct_messages = ['initial commit', 'mp commit', 'B-1'] |
| 1597 | assert repo_messages == correct_messages |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1598 | self.assertEmptyQueues() |
James E. Blair | 7f71c80 | 2012-08-22 13:04:32 -0700 | [diff] [blame] | 1599 | |
| 1600 | def test_one_job_project(self): |
| 1601 | "Test that queueing works with one job" |
| 1602 | A = self.fake_gerrit.addFakeChange('org/one-job-project', |
| 1603 | 'master', 'A') |
| 1604 | B = self.fake_gerrit.addFakeChange('org/one-job-project', |
| 1605 | 'master', 'B') |
| 1606 | A.addApproval('CRVW', 2) |
| 1607 | B.addApproval('CRVW', 2) |
| 1608 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1609 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1610 | self.waitUntilSettled() |
| 1611 | |
James E. Blair | 7f71c80 | 2012-08-22 13:04:32 -0700 | [diff] [blame] | 1612 | assert A.data['status'] == 'MERGED' |
| 1613 | assert A.reported == 2 |
| 1614 | assert B.data['status'] == 'MERGED' |
| 1615 | assert B.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1616 | self.assertEmptyQueues() |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1617 | |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 1618 | def test_job_from_templates_launched(self): |
| 1619 | "Test whether a job generated via a template can be launched" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1620 | |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 1621 | A = self.fake_gerrit.addFakeChange( |
| 1622 | 'org/templated-project', 'master', 'A') |
| 1623 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1624 | self.waitUntilSettled() |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 1625 | |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1626 | assert self.getJobFromHistory('project-test1').result == 'SUCCESS' |
| 1627 | assert self.getJobFromHistory('project-test2').result == 'SUCCESS' |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 1628 | |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1629 | def test_dependent_changes_dequeue(self): |
| 1630 | "Test that dependent patches are not needlessly tested" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1631 | |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1632 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1633 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1634 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1635 | A.addApproval('CRVW', 2) |
| 1636 | B.addApproval('CRVW', 2) |
| 1637 | C.addApproval('CRVW', 2) |
| 1638 | |
| 1639 | M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1') |
| 1640 | M1.setMerged() |
| 1641 | |
| 1642 | # C -> B -> A -> M1 |
| 1643 | |
| 1644 | C.setDependsOn(B, 1) |
| 1645 | B.setDependsOn(A, 1) |
| 1646 | A.setDependsOn(M1, 1) |
| 1647 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1648 | self.worker.addFailTest('project-merge', A) |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1649 | |
| 1650 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1651 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1652 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1653 | |
| 1654 | self.waitUntilSettled() |
| 1655 | |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1656 | assert A.data['status'] == 'NEW' |
| 1657 | assert A.reported == 2 |
| 1658 | assert B.data['status'] == 'NEW' |
| 1659 | assert B.reported == 2 |
| 1660 | assert C.data['status'] == 'NEW' |
| 1661 | assert C.reported == 2 |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1662 | assert len(self.history) == 1 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1663 | self.assertEmptyQueues() |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1664 | |
| 1665 | def test_head_is_dequeued_once(self): |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 1666 | "Test that if a change at the head fails it is dequeued only once" |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1667 | # If it's dequeued more than once, we should see extra |
| 1668 | # aborted jobs. |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1669 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1670 | self.worker.hold_jobs_in_build = True |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1671 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 1672 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 1673 | C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C') |
| 1674 | A.addApproval('CRVW', 2) |
| 1675 | B.addApproval('CRVW', 2) |
| 1676 | C.addApproval('CRVW', 2) |
| 1677 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1678 | self.worker.addFailTest('project1-test1', A) |
| 1679 | self.worker.addFailTest('project1-test2', A) |
| 1680 | self.worker.addFailTest('project1-project2-integration', A) |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1681 | |
| 1682 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1683 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1684 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1685 | |
| 1686 | self.waitUntilSettled() |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1687 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1688 | assert len(self.builds) == 1 |
| 1689 | assert self.builds[0].name == 'project1-merge' |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1690 | assert self.job_has_changes(self.builds[0], A) |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1691 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1692 | self.worker.release('.*-merge') |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1693 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1694 | self.worker.release('.*-merge') |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1695 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1696 | self.worker.release('.*-merge') |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1697 | self.waitUntilSettled() |
| 1698 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1699 | assert len(self.builds) == 9 |
| 1700 | assert self.builds[0].name == 'project1-test1' |
| 1701 | assert self.builds[1].name == 'project1-test2' |
| 1702 | assert self.builds[2].name == 'project1-project2-integration' |
| 1703 | assert self.builds[3].name == 'project1-test1' |
| 1704 | assert self.builds[4].name == 'project1-test2' |
| 1705 | assert self.builds[5].name == 'project1-project2-integration' |
| 1706 | assert self.builds[6].name == 'project1-test1' |
| 1707 | assert self.builds[7].name == 'project1-test2' |
| 1708 | assert self.builds[8].name == 'project1-project2-integration' |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1709 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1710 | self.release(self.builds[0]) |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1711 | self.waitUntilSettled() |
| 1712 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1713 | assert len(self.builds) == 3 # test2, integration, merge for B |
| 1714 | assert self.countJobResults(self.history, 'ABORTED') == 6 |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1715 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1716 | self.worker.hold_jobs_in_build = False |
| 1717 | self.worker.release() |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1718 | self.waitUntilSettled() |
| 1719 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1720 | assert len(self.builds) == 0 |
| 1721 | assert len(self.history) == 20 |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1722 | |
| 1723 | assert A.data['status'] == 'NEW' |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1724 | assert B.data['status'] == 'MERGED' |
| 1725 | assert C.data['status'] == 'MERGED' |
| 1726 | assert A.reported == 2 |
| 1727 | assert B.reported == 2 |
| 1728 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1729 | self.assertEmptyQueues() |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 1730 | |
| 1731 | def test_nonvoting_job(self): |
| 1732 | "Test that non-voting jobs don't vote." |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1733 | |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 1734 | A = self.fake_gerrit.addFakeChange('org/nonvoting-project', |
| 1735 | 'master', 'A') |
| 1736 | A.addApproval('CRVW', 2) |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1737 | self.worker.addFailTest('nonvoting-project-test2', A) |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 1738 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1739 | |
| 1740 | self.waitUntilSettled() |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 1741 | |
| 1742 | assert A.data['status'] == 'MERGED' |
| 1743 | assert A.reported == 2 |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1744 | assert (self.getJobFromHistory('nonvoting-project-merge').result == |
| 1745 | 'SUCCESS') |
| 1746 | assert (self.getJobFromHistory('nonvoting-project-test1').result == |
| 1747 | 'SUCCESS') |
| 1748 | assert (self.getJobFromHistory('nonvoting-project-test2').result == |
| 1749 | 'FAILURE') |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1750 | self.assertEmptyQueues() |
| 1751 | |
| 1752 | def test_check_queue_success(self): |
| 1753 | "Test successful check queue jobs." |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1754 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1755 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1756 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1757 | |
| 1758 | self.waitUntilSettled() |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1759 | |
| 1760 | assert A.data['status'] == 'NEW' |
| 1761 | assert A.reported == 1 |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1762 | assert self.getJobFromHistory('project-merge').result == 'SUCCESS' |
| 1763 | assert self.getJobFromHistory('project-test1').result == 'SUCCESS' |
| 1764 | assert self.getJobFromHistory('project-test2').result == 'SUCCESS' |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1765 | self.assertEmptyQueues() |
| 1766 | |
| 1767 | def test_check_queue_failure(self): |
| 1768 | "Test failed check queue jobs." |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1769 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1770 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1771 | self.worker.addFailTest('project-test2', A) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1772 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1773 | |
| 1774 | self.waitUntilSettled() |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1775 | |
| 1776 | assert A.data['status'] == 'NEW' |
| 1777 | assert A.reported == 1 |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1778 | assert self.getJobFromHistory('project-merge').result == 'SUCCESS' |
| 1779 | assert self.getJobFromHistory('project-test1').result == 'SUCCESS' |
| 1780 | assert self.getJobFromHistory('project-test2').result == 'FAILURE' |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1781 | self.assertEmptyQueues() |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1782 | |
| 1783 | def test_dependent_behind_dequeue(self): |
| 1784 | "test that dependent changes behind dequeued changes work" |
| 1785 | # This complicated test is a reproduction of a real life bug |
| 1786 | self.sched.reconfigure(self.config) |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1787 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1788 | self.worker.hold_jobs_in_build = True |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1789 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 1790 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 1791 | C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C') |
| 1792 | D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D') |
| 1793 | E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E') |
| 1794 | F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F') |
| 1795 | D.setDependsOn(C, 1) |
| 1796 | E.setDependsOn(D, 1) |
| 1797 | A.addApproval('CRVW', 2) |
| 1798 | B.addApproval('CRVW', 2) |
| 1799 | C.addApproval('CRVW', 2) |
| 1800 | D.addApproval('CRVW', 2) |
| 1801 | E.addApproval('CRVW', 2) |
| 1802 | F.addApproval('CRVW', 2) |
| 1803 | |
| 1804 | A.fail_merge = True |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1805 | |
| 1806 | # Change object re-use in the gerrit trigger is hidden if |
| 1807 | # changes are added in quick succession; waiting makes it more |
| 1808 | # like real life. |
| 1809 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1810 | self.waitUntilSettled() |
| 1811 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1812 | self.waitUntilSettled() |
| 1813 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1814 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1815 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1816 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1817 | self.waitUntilSettled() |
| 1818 | |
| 1819 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1820 | self.waitUntilSettled() |
| 1821 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 1822 | self.waitUntilSettled() |
| 1823 | self.fake_gerrit.addEvent(E.addApproval('APRV', 1)) |
| 1824 | self.waitUntilSettled() |
| 1825 | self.fake_gerrit.addEvent(F.addApproval('APRV', 1)) |
| 1826 | self.waitUntilSettled() |
| 1827 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1828 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1829 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1830 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1831 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1832 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1833 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1834 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1835 | self.waitUntilSettled() |
| 1836 | |
| 1837 | # all jobs running |
James E. Blair | e955e06 | 2012-10-08 09:49:03 -0700 | [diff] [blame] | 1838 | |
| 1839 | # Grab pointers to the jobs we want to release before |
| 1840 | # releasing any, because list indexes may change as |
| 1841 | # the jobs complete. |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1842 | a, b, c = self.builds[:3] |
James E. Blair | e955e06 | 2012-10-08 09:49:03 -0700 | [diff] [blame] | 1843 | a.release() |
| 1844 | b.release() |
| 1845 | c.release() |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1846 | self.waitUntilSettled() |
| 1847 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1848 | self.worker.hold_jobs_in_build = False |
| 1849 | self.worker.release() |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1850 | self.waitUntilSettled() |
| 1851 | |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1852 | assert A.data['status'] == 'NEW' |
| 1853 | assert B.data['status'] == 'MERGED' |
| 1854 | assert C.data['status'] == 'MERGED' |
| 1855 | assert D.data['status'] == 'MERGED' |
| 1856 | assert E.data['status'] == 'MERGED' |
| 1857 | assert F.data['status'] == 'MERGED' |
| 1858 | |
| 1859 | assert A.reported == 2 |
| 1860 | assert B.reported == 2 |
| 1861 | assert C.reported == 2 |
| 1862 | assert D.reported == 2 |
| 1863 | assert E.reported == 2 |
| 1864 | assert F.reported == 2 |
| 1865 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1866 | assert self.countJobResults(self.history, 'ABORTED') == 15 |
| 1867 | assert len(self.history) == 44 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1868 | self.assertEmptyQueues() |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 1869 | |
| 1870 | def test_merger_repack(self): |
| 1871 | "Test that the merger works after a repack" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1872 | |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 1873 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1874 | A.addApproval('CRVW', 2) |
| 1875 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1876 | self.waitUntilSettled() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1877 | assert self.getJobFromHistory('project-merge').result == 'SUCCESS' |
| 1878 | assert self.getJobFromHistory('project-test1').result == 'SUCCESS' |
| 1879 | assert self.getJobFromHistory('project-test2').result == 'SUCCESS' |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 1880 | assert A.data['status'] == 'MERGED' |
| 1881 | assert A.reported == 2 |
| 1882 | self.assertEmptyQueues() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1883 | self.worker.build_history = [] |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 1884 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1885 | path = os.path.join(self.git_root, "org/project") |
| 1886 | print repack_repo(path) |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 1887 | |
| 1888 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1889 | A.addApproval('CRVW', 2) |
| 1890 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1891 | self.waitUntilSettled() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1892 | assert self.getJobFromHistory('project-merge').result == 'SUCCESS' |
| 1893 | assert self.getJobFromHistory('project-test1').result == 'SUCCESS' |
| 1894 | assert self.getJobFromHistory('project-test2').result == 'SUCCESS' |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 1895 | assert A.data['status'] == 'MERGED' |
| 1896 | assert A.reported == 2 |
| 1897 | self.assertEmptyQueues() |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 1898 | |
James E. Blair | 4886f28 | 2012-11-15 09:27:33 -0800 | [diff] [blame] | 1899 | def test_merger_repack_large_change(self): |
| 1900 | "Test that the merger works with large changes after a repack" |
| 1901 | # https://bugs.launchpad.net/zuul/+bug/1078946 |
| 1902 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 1903 | A.addPatchset(large=True) |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1904 | path = os.path.join(self.upstream_root, "org/project1") |
| 1905 | print repack_repo(path) |
| 1906 | path = os.path.join(self.git_root, "org/project1") |
| 1907 | print repack_repo(path) |
James E. Blair | 4886f28 | 2012-11-15 09:27:33 -0800 | [diff] [blame] | 1908 | |
| 1909 | A.addApproval('CRVW', 2) |
| 1910 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1911 | self.waitUntilSettled() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1912 | assert self.getJobFromHistory('project1-merge').result == 'SUCCESS' |
| 1913 | assert self.getJobFromHistory('project1-test1').result == 'SUCCESS' |
| 1914 | assert self.getJobFromHistory('project1-test2').result == 'SUCCESS' |
James E. Blair | 4886f28 | 2012-11-15 09:27:33 -0800 | [diff] [blame] | 1915 | assert A.data['status'] == 'MERGED' |
| 1916 | assert A.reported == 2 |
| 1917 | self.assertEmptyQueues() |
| 1918 | |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 1919 | def test_nonexistent_job(self): |
| 1920 | "Test launching a job that doesn't exist" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1921 | # Set to the state immediately after a restart |
| 1922 | self.resetGearmanServer() |
| 1923 | self.launcher.negative_function_cache_ttl = 0 |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 1924 | |
| 1925 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1926 | A.addApproval('CRVW', 2) |
| 1927 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1928 | # There may be a thread about to report a lost change |
| 1929 | while A.reported < 2: |
| 1930 | self.waitUntilSettled() |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1931 | job_names = [x.name for x in self.history] |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 1932 | assert not job_names |
| 1933 | assert A.data['status'] == 'NEW' |
| 1934 | assert A.reported == 2 |
| 1935 | self.assertEmptyQueues() |
| 1936 | |
| 1937 | # Make sure things still work: |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1938 | self.registerJobs() |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 1939 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1940 | A.addApproval('CRVW', 2) |
| 1941 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1942 | self.waitUntilSettled() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1943 | assert self.getJobFromHistory('project-merge').result == 'SUCCESS' |
| 1944 | assert self.getJobFromHistory('project-test1').result == 'SUCCESS' |
| 1945 | assert self.getJobFromHistory('project-test2').result == 'SUCCESS' |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 1946 | assert A.data['status'] == 'MERGED' |
| 1947 | assert A.reported == 2 |
| 1948 | self.assertEmptyQueues() |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 1949 | |
| 1950 | def test_single_nonexistent_post_job(self): |
| 1951 | "Test launching a single post job that doesn't exist" |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 1952 | e = { |
| 1953 | "type": "ref-updated", |
| 1954 | "submitter": { |
| 1955 | "name": "User Name", |
| 1956 | }, |
| 1957 | "refUpdate": { |
| 1958 | "oldRev": "90f173846e3af9154517b88543ffbd1691f31366", |
| 1959 | "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543", |
| 1960 | "refName": "master", |
| 1961 | "project": "org/project", |
| 1962 | } |
| 1963 | } |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1964 | # Set to the state immediately after a restart |
| 1965 | self.resetGearmanServer() |
| 1966 | self.launcher.negative_function_cache_ttl = 0 |
| 1967 | |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 1968 | self.fake_gerrit.addEvent(e) |
| 1969 | self.waitUntilSettled() |
| 1970 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1971 | assert len(self.history) == 0 |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 1972 | self.assertEmptyQueues() |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 1973 | |
| 1974 | def test_new_patchset_dequeues_old(self): |
| 1975 | "Test that a new patchset causes the old to be dequeued" |
| 1976 | # D -> C (depends on B) -> B (depends on A) -> A -> M |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1977 | self.worker.hold_jobs_in_build = True |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 1978 | M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M') |
| 1979 | M.setMerged() |
| 1980 | |
| 1981 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1982 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1983 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1984 | D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D') |
| 1985 | A.addApproval('CRVW', 2) |
| 1986 | B.addApproval('CRVW', 2) |
| 1987 | C.addApproval('CRVW', 2) |
| 1988 | D.addApproval('CRVW', 2) |
| 1989 | |
| 1990 | C.setDependsOn(B, 1) |
| 1991 | B.setDependsOn(A, 1) |
| 1992 | A.setDependsOn(M, 1) |
| 1993 | |
| 1994 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1995 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1996 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1997 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 1998 | self.waitUntilSettled() |
| 1999 | |
| 2000 | B.addPatchset() |
| 2001 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2)) |
| 2002 | self.waitUntilSettled() |
| 2003 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2004 | self.worker.hold_jobs_in_build = False |
| 2005 | self.worker.release() |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2006 | self.waitUntilSettled() |
| 2007 | |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2008 | assert A.data['status'] == 'MERGED' |
| 2009 | assert A.reported == 2 |
| 2010 | assert B.data['status'] == 'NEW' |
| 2011 | assert B.reported == 2 |
| 2012 | assert C.data['status'] == 'NEW' |
| 2013 | assert C.reported == 2 |
| 2014 | assert D.data['status'] == 'MERGED' |
| 2015 | assert D.reported == 2 |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 2016 | assert len(self.history) == 9 # 3 each for A, B, D. |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2017 | self.assertEmptyQueues() |
| 2018 | |
| 2019 | def test_new_patchset_dequeues_old_on_head(self): |
| 2020 | "Test that a new patchset causes the old to be dequeued (at head)" |
| 2021 | # D -> C (depends on B) -> B (depends on A) -> A -> M |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2022 | self.worker.hold_jobs_in_build = True |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2023 | M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M') |
| 2024 | M.setMerged() |
| 2025 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2026 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2027 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 2028 | D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D') |
| 2029 | A.addApproval('CRVW', 2) |
| 2030 | B.addApproval('CRVW', 2) |
| 2031 | C.addApproval('CRVW', 2) |
| 2032 | D.addApproval('CRVW', 2) |
| 2033 | |
| 2034 | C.setDependsOn(B, 1) |
| 2035 | B.setDependsOn(A, 1) |
| 2036 | A.setDependsOn(M, 1) |
| 2037 | |
| 2038 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2039 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2040 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2041 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 2042 | self.waitUntilSettled() |
| 2043 | |
| 2044 | A.addPatchset() |
| 2045 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2)) |
| 2046 | self.waitUntilSettled() |
| 2047 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2048 | self.worker.hold_jobs_in_build = False |
| 2049 | self.worker.release() |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2050 | self.waitUntilSettled() |
| 2051 | |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2052 | assert A.data['status'] == 'NEW' |
| 2053 | assert A.reported == 2 |
| 2054 | assert B.data['status'] == 'NEW' |
| 2055 | assert B.reported == 2 |
| 2056 | assert C.data['status'] == 'NEW' |
| 2057 | assert C.reported == 2 |
| 2058 | assert D.data['status'] == 'MERGED' |
| 2059 | assert D.reported == 2 |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 2060 | assert len(self.history) == 7 |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2061 | self.assertEmptyQueues() |
| 2062 | |
| 2063 | def test_new_patchset_dequeues_old_without_dependents(self): |
| 2064 | "Test that a new patchset causes only the old to be dequeued" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2065 | self.worker.hold_jobs_in_build = True |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2066 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2067 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2068 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 2069 | A.addApproval('CRVW', 2) |
| 2070 | B.addApproval('CRVW', 2) |
| 2071 | C.addApproval('CRVW', 2) |
| 2072 | |
| 2073 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2074 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2075 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2076 | self.waitUntilSettled() |
| 2077 | |
| 2078 | B.addPatchset() |
| 2079 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2)) |
| 2080 | self.waitUntilSettled() |
| 2081 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2082 | self.worker.hold_jobs_in_build = False |
| 2083 | self.worker.release() |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2084 | self.waitUntilSettled() |
| 2085 | |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2086 | assert A.data['status'] == 'MERGED' |
| 2087 | assert A.reported == 2 |
| 2088 | assert B.data['status'] == 'NEW' |
| 2089 | assert B.reported == 2 |
| 2090 | assert C.data['status'] == 'MERGED' |
| 2091 | assert C.reported == 2 |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 2092 | assert len(self.history) == 9 |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2093 | self.assertEmptyQueues() |
| 2094 | |
| 2095 | def test_new_patchset_dequeues_old_independent_queue(self): |
| 2096 | "Test that a new patchset causes the old to be dequeued (independent)" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2097 | self.worker.hold_jobs_in_build = True |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2098 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2099 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2100 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 2101 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 2102 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 2103 | self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1)) |
| 2104 | self.waitUntilSettled() |
| 2105 | |
| 2106 | B.addPatchset() |
| 2107 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2)) |
| 2108 | self.waitUntilSettled() |
| 2109 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2110 | self.worker.hold_jobs_in_build = False |
| 2111 | self.worker.release() |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2112 | self.waitUntilSettled() |
| 2113 | |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2114 | assert A.data['status'] == 'NEW' |
| 2115 | assert A.reported == 1 |
| 2116 | assert B.data['status'] == 'NEW' |
| 2117 | assert B.reported == 1 |
| 2118 | assert C.data['status'] == 'NEW' |
| 2119 | assert C.reported == 1 |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 2120 | assert len(self.history) == 10 |
| 2121 | assert self.countJobResults(self.history, 'ABORTED') == 1 |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2122 | self.assertEmptyQueues() |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2123 | |
| 2124 | def test_zuul_refs(self): |
| 2125 | "Test that zuul refs exist and have the right changes" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2126 | self.worker.hold_jobs_in_build = True |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2127 | M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1') |
| 2128 | M1.setMerged() |
| 2129 | M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2') |
| 2130 | M2.setMerged() |
| 2131 | |
| 2132 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 2133 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 2134 | C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C') |
| 2135 | D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D') |
| 2136 | A.addApproval('CRVW', 2) |
| 2137 | B.addApproval('CRVW', 2) |
| 2138 | C.addApproval('CRVW', 2) |
| 2139 | D.addApproval('CRVW', 2) |
| 2140 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2141 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2142 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2143 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 2144 | |
| 2145 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2146 | self.worker.release('.*-merge') |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2147 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2148 | self.worker.release('.*-merge') |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2149 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2150 | self.worker.release('.*-merge') |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2151 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2152 | self.worker.release('.*-merge') |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2153 | self.waitUntilSettled() |
| 2154 | |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2155 | a_zref = b_zref = c_zref = d_zref = None |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 2156 | for x in self.builds: |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2157 | if x.parameters['ZUUL_CHANGE'] == '3': |
| 2158 | a_zref = x.parameters['ZUUL_REF'] |
| 2159 | if x.parameters['ZUUL_CHANGE'] == '4': |
| 2160 | b_zref = x.parameters['ZUUL_REF'] |
| 2161 | if x.parameters['ZUUL_CHANGE'] == '5': |
| 2162 | c_zref = x.parameters['ZUUL_REF'] |
| 2163 | if x.parameters['ZUUL_CHANGE'] == '6': |
| 2164 | d_zref = x.parameters['ZUUL_REF'] |
| 2165 | |
| 2166 | # There are... four... refs. |
| 2167 | assert a_zref is not None |
| 2168 | assert b_zref is not None |
| 2169 | assert c_zref is not None |
| 2170 | assert d_zref is not None |
| 2171 | |
| 2172 | # And they should all be different |
| 2173 | refs = set([a_zref, b_zref, c_zref, d_zref]) |
| 2174 | assert len(refs) == 4 |
| 2175 | |
| 2176 | # a ref should have a, not b, and should not be in project2 |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 2177 | assert self.ref_has_change(a_zref, A) |
| 2178 | assert not self.ref_has_change(a_zref, B) |
| 2179 | assert not self.ref_has_change(a_zref, M2) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2180 | |
| 2181 | # b ref should have a and b, and should not be in project2 |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 2182 | assert self.ref_has_change(b_zref, A) |
| 2183 | assert self.ref_has_change(b_zref, B) |
| 2184 | assert not self.ref_has_change(b_zref, M2) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2185 | |
| 2186 | # c ref should have a and b in 1, c in 2 |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 2187 | assert self.ref_has_change(c_zref, A) |
| 2188 | assert self.ref_has_change(c_zref, B) |
| 2189 | assert self.ref_has_change(c_zref, C) |
| 2190 | assert not self.ref_has_change(c_zref, D) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2191 | |
| 2192 | # d ref should have a and b in 1, c and d in 2 |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 2193 | assert self.ref_has_change(d_zref, A) |
| 2194 | assert self.ref_has_change(d_zref, B) |
| 2195 | assert self.ref_has_change(d_zref, C) |
| 2196 | assert self.ref_has_change(d_zref, D) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2197 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2198 | self.worker.hold_jobs_in_build = False |
| 2199 | self.worker.release() |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2200 | self.waitUntilSettled() |
| 2201 | |
| 2202 | assert A.data['status'] == 'MERGED' |
| 2203 | assert A.reported == 2 |
| 2204 | assert B.data['status'] == 'MERGED' |
| 2205 | assert B.reported == 2 |
| 2206 | assert C.data['status'] == 'MERGED' |
| 2207 | assert C.reported == 2 |
| 2208 | assert D.data['status'] == 'MERGED' |
| 2209 | assert D.reported == 2 |
| 2210 | self.assertEmptyQueues() |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 2211 | |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 2212 | def test_statsd(self): |
| 2213 | "Test each of the statsd methods used in the scheduler" |
| 2214 | import extras |
| 2215 | statsd = extras.try_import('statsd.statsd') |
| 2216 | statsd.incr('test-incr') |
| 2217 | statsd.timing('test-timing', 3) |
| 2218 | statsd.gauge('test-guage', 12) |
| 2219 | self.assertReportedStat('test-incr', '1|c') |
| 2220 | self.assertReportedStat('test-timing', '3|ms') |
| 2221 | self.assertReportedStat('test-guage', '12|g') |
| 2222 | |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 2223 | def test_file_jobs(self): |
| 2224 | "Test that file jobs run only when appropriate" |
| 2225 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2226 | A.addPatchset(['pip-requires']) |
| 2227 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2228 | A.addApproval('CRVW', 2) |
| 2229 | B.addApproval('CRVW', 2) |
| 2230 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2231 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2232 | self.waitUntilSettled() |
| 2233 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 2234 | testfile_jobs = [x for x in self.history |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 2235 | if x.name == 'project-testfile'] |
| 2236 | |
| 2237 | assert len(testfile_jobs) == 1 |
| 2238 | assert testfile_jobs[0].changes == '1,2' |
| 2239 | assert A.data['status'] == 'MERGED' |
| 2240 | assert A.reported == 2 |
| 2241 | assert B.data['status'] == 'MERGED' |
| 2242 | assert B.reported == 2 |
| 2243 | self.assertEmptyQueues() |
James E. Blair | 3c5e5b5 | 2013-04-26 11:17:03 -0700 | [diff] [blame] | 2244 | |
| 2245 | def test_test_config(self): |
| 2246 | "Test that we can test the config" |
| 2247 | sched = zuul.scheduler.Scheduler() |
| 2248 | sched.testConfig(CONFIG.get('zuul', 'layout_config')) |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2249 | |
| 2250 | def test_build_description(self): |
| 2251 | "Test that build descriptions update" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2252 | self.worker.registerFunction('set_description:' + |
| 2253 | self.worker.worker_id) |
| 2254 | |
| 2255 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2256 | A.addApproval('CRVW', 2) |
| 2257 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2258 | self.waitUntilSettled() |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 2259 | desc = self.history[0].description |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2260 | self.log.debug("Description: %s" % desc) |
| 2261 | assert re.search("Branch.*master", desc) |
| 2262 | assert re.search("Pipeline.*gate", desc) |
| 2263 | assert re.search("project-merge.*SUCCESS", desc) |
| 2264 | assert re.search("project-test1.*SUCCESS", desc) |
| 2265 | assert re.search("project-test2.*SUCCESS", desc) |
| 2266 | assert re.search("Reported result.*SUCCESS", desc) |
| 2267 | |
James E. Blair | 1843a55 | 2013-07-03 14:19:52 -0700 | [diff] [blame^] | 2268 | def test_json_status(self): |
| 2269 | "Test that we can retrieve JSON status info" |
| 2270 | self.worker.hold_jobs_in_build = True |
| 2271 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2272 | A.addApproval('CRVW', 2) |
| 2273 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2274 | self.waitUntilSettled() |
| 2275 | |
| 2276 | port = self.webapp.server.socket.getsockname()[1] |
| 2277 | |
| 2278 | f = urllib.urlopen("http://localhost:%s/status.json" % port) |
| 2279 | data = f.read() |
| 2280 | |
| 2281 | self.worker.hold_jobs_in_build = False |
| 2282 | self.worker.release() |
| 2283 | self.waitUntilSettled() |
| 2284 | |
| 2285 | data = json.loads(data) |
| 2286 | status_jobs = set() |
| 2287 | for p in data['pipelines']: |
| 2288 | for q in p['change_queues']: |
| 2289 | for head in q['heads']: |
| 2290 | for change in head: |
| 2291 | assert change['id'] == '1,1' |
| 2292 | for job in change['jobs']: |
| 2293 | status_jobs.add(job['name']) |
| 2294 | assert 'project-merge' in status_jobs |
| 2295 | assert 'project-test1' in status_jobs |
| 2296 | assert 'project-test2' in status_jobs |
| 2297 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2298 | def test_node_label(self): |
| 2299 | "Test that a job runs on a specific node label" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2300 | self.worker.registerFunction('build:node-project-test1:debian') |
| 2301 | |
| 2302 | A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A') |
| 2303 | A.addApproval('CRVW', 2) |
| 2304 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2305 | self.waitUntilSettled() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 2306 | |
| 2307 | assert self.getJobFromHistory('node-project-merge').node is None |
| 2308 | assert self.getJobFromHistory('node-project-test1').node == 'debian' |
| 2309 | assert self.getJobFromHistory('node-project-test2').node is None |