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