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