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