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