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, |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 475 | } |
| 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']) |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 504 | ) |
| 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 | f750aa0 | 2013-07-15 14:11:24 -0700 | [diff] [blame] | 1655 | ref_B = self.getParameter(queue[-1], 'ZUUL_REF') |
| 1656 | 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] | 1657 | self.gearman_server.release('.*-merge') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1658 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1659 | self.gearman_server.release('.*-merge') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1660 | self.waitUntilSettled() |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 1661 | queue = self.gearman_server.getQueue() |
James E. Blair | f750aa0 | 2013-07-15 14:11:24 -0700 | [diff] [blame] | 1662 | ref_C = self.getParameter(queue[-1], 'ZUUL_REF') |
| 1663 | 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] | 1664 | self.gearman_server.hold_jobs_in_queue = False |
| 1665 | self.gearman_server.release() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1666 | self.waitUntilSettled() |
| 1667 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1668 | path = os.path.join(self.git_root, "org/project") |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1669 | repo = git.Repo(path) |
| 1670 | |
| 1671 | repo_messages = [c.message.strip() |
James E. Blair | f750aa0 | 2013-07-15 14:11:24 -0700 | [diff] [blame] | 1672 | for c in repo.iter_commits(ref_C)] |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1673 | repo_messages.reverse() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1674 | correct_messages = ['initial commit', 'A-1', 'C-1'] |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1675 | self.assertEqual(repo_messages, correct_messages) |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1676 | |
| 1677 | repo_messages = [c.message.strip() |
James E. Blair | f750aa0 | 2013-07-15 14:11:24 -0700 | [diff] [blame] | 1678 | for c in repo.iter_commits(ref_B)] |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1679 | repo_messages.reverse() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1680 | correct_messages = ['initial commit', 'mp commit', 'B-1'] |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1681 | self.assertEqual(repo_messages, correct_messages) |
James E. Blair | 7f71c80 | 2012-08-22 13:04:32 -0700 | [diff] [blame] | 1682 | |
| 1683 | def test_one_job_project(self): |
| 1684 | "Test that queueing works with one job" |
| 1685 | A = self.fake_gerrit.addFakeChange('org/one-job-project', |
| 1686 | 'master', 'A') |
| 1687 | B = self.fake_gerrit.addFakeChange('org/one-job-project', |
| 1688 | 'master', 'B') |
| 1689 | A.addApproval('CRVW', 2) |
| 1690 | B.addApproval('CRVW', 2) |
| 1691 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1692 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1693 | self.waitUntilSettled() |
| 1694 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1695 | self.assertEqual(A.data['status'], 'MERGED') |
| 1696 | self.assertEqual(A.reported, 2) |
| 1697 | self.assertEqual(B.data['status'], 'MERGED') |
| 1698 | self.assertEqual(B.reported, 2) |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1699 | |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 1700 | def test_job_from_templates_launched(self): |
| 1701 | "Test whether a job generated via a template can be launched" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1702 | |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 1703 | A = self.fake_gerrit.addFakeChange( |
| 1704 | 'org/templated-project', 'master', 'A') |
| 1705 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1706 | self.waitUntilSettled() |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 1707 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1708 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 1709 | 'SUCCESS') |
| 1710 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 1711 | 'SUCCESS') |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 1712 | |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1713 | def test_dependent_changes_dequeue(self): |
| 1714 | "Test that dependent patches are not needlessly tested" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1715 | |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1716 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1717 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1718 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1719 | A.addApproval('CRVW', 2) |
| 1720 | B.addApproval('CRVW', 2) |
| 1721 | C.addApproval('CRVW', 2) |
| 1722 | |
| 1723 | M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1') |
| 1724 | M1.setMerged() |
| 1725 | |
| 1726 | # C -> B -> A -> M1 |
| 1727 | |
| 1728 | C.setDependsOn(B, 1) |
| 1729 | B.setDependsOn(A, 1) |
| 1730 | A.setDependsOn(M1, 1) |
| 1731 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1732 | self.worker.addFailTest('project-merge', A) |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1733 | |
| 1734 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1735 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1736 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1737 | |
| 1738 | self.waitUntilSettled() |
| 1739 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1740 | self.assertEqual(A.data['status'], 'NEW') |
| 1741 | self.assertEqual(A.reported, 2) |
| 1742 | self.assertEqual(B.data['status'], 'NEW') |
| 1743 | self.assertEqual(B.reported, 2) |
| 1744 | self.assertEqual(C.data['status'], 'NEW') |
| 1745 | self.assertEqual(C.reported, 2) |
| 1746 | self.assertEqual(len(self.history), 1) |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1747 | |
| 1748 | def test_head_is_dequeued_once(self): |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 1749 | "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] | 1750 | # If it's dequeued more than once, we should see extra |
| 1751 | # aborted jobs. |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1752 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1753 | self.worker.hold_jobs_in_build = True |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1754 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 1755 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 1756 | C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C') |
| 1757 | A.addApproval('CRVW', 2) |
| 1758 | B.addApproval('CRVW', 2) |
| 1759 | C.addApproval('CRVW', 2) |
| 1760 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1761 | self.worker.addFailTest('project1-test1', A) |
| 1762 | self.worker.addFailTest('project1-test2', A) |
| 1763 | self.worker.addFailTest('project1-project2-integration', A) |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1764 | |
| 1765 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1766 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1767 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1768 | |
| 1769 | self.waitUntilSettled() |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1770 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1771 | self.assertEqual(len(self.builds), 1) |
| 1772 | self.assertEqual(self.builds[0].name, 'project1-merge') |
| 1773 | self.assertTrue(self.job_has_changes(self.builds[0], A)) |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1774 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1775 | self.worker.release('.*-merge') |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1776 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1777 | self.worker.release('.*-merge') |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1778 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1779 | self.worker.release('.*-merge') |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1780 | self.waitUntilSettled() |
| 1781 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1782 | self.assertEqual(len(self.builds), 9) |
| 1783 | self.assertEqual(self.builds[0].name, 'project1-test1') |
| 1784 | self.assertEqual(self.builds[1].name, 'project1-test2') |
| 1785 | self.assertEqual(self.builds[2].name, 'project1-project2-integration') |
| 1786 | self.assertEqual(self.builds[3].name, 'project1-test1') |
| 1787 | self.assertEqual(self.builds[4].name, 'project1-test2') |
| 1788 | self.assertEqual(self.builds[5].name, 'project1-project2-integration') |
| 1789 | self.assertEqual(self.builds[6].name, 'project1-test1') |
| 1790 | self.assertEqual(self.builds[7].name, 'project1-test2') |
| 1791 | self.assertEqual(self.builds[8].name, 'project1-project2-integration') |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1792 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1793 | self.release(self.builds[0]) |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1794 | self.waitUntilSettled() |
| 1795 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1796 | self.assertEqual(len(self.builds), 3) # test2,integration, merge for B |
| 1797 | self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6) |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1798 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1799 | self.worker.hold_jobs_in_build = False |
| 1800 | self.worker.release() |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1801 | self.waitUntilSettled() |
| 1802 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1803 | self.assertEqual(len(self.builds), 0) |
| 1804 | self.assertEqual(len(self.history), 20) |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1805 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1806 | self.assertEqual(A.data['status'], 'NEW') |
| 1807 | self.assertEqual(B.data['status'], 'MERGED') |
| 1808 | self.assertEqual(C.data['status'], 'MERGED') |
| 1809 | self.assertEqual(A.reported, 2) |
| 1810 | self.assertEqual(B.reported, 2) |
| 1811 | self.assertEqual(C.reported, 2) |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 1812 | |
| 1813 | def test_nonvoting_job(self): |
| 1814 | "Test that non-voting jobs don't vote." |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1815 | |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 1816 | A = self.fake_gerrit.addFakeChange('org/nonvoting-project', |
| 1817 | 'master', 'A') |
| 1818 | A.addApproval('CRVW', 2) |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1819 | self.worker.addFailTest('nonvoting-project-test2', A) |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 1820 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1821 | |
| 1822 | self.waitUntilSettled() |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 1823 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1824 | self.assertEqual(A.data['status'], 'MERGED') |
| 1825 | self.assertEqual(A.reported, 2) |
| 1826 | self.assertEqual( |
| 1827 | self.getJobFromHistory('nonvoting-project-merge').result, |
| 1828 | 'SUCCESS') |
| 1829 | self.assertEqual( |
| 1830 | self.getJobFromHistory('nonvoting-project-test1').result, |
| 1831 | 'SUCCESS') |
| 1832 | self.assertEqual( |
| 1833 | self.getJobFromHistory('nonvoting-project-test2').result, |
| 1834 | 'FAILURE') |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1835 | |
| 1836 | def test_check_queue_success(self): |
| 1837 | "Test successful check queue jobs." |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1838 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1839 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1840 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1841 | |
| 1842 | self.waitUntilSettled() |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1843 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1844 | self.assertEqual(A.data['status'], 'NEW') |
| 1845 | self.assertEqual(A.reported, 1) |
| 1846 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
| 1847 | 'SUCCESS') |
| 1848 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 1849 | 'SUCCESS') |
| 1850 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 1851 | 'SUCCESS') |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1852 | |
| 1853 | def test_check_queue_failure(self): |
| 1854 | "Test failed check queue jobs." |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1855 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1856 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1857 | self.worker.addFailTest('project-test2', A) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1858 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1859 | |
| 1860 | self.waitUntilSettled() |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1861 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1862 | self.assertEqual(A.data['status'], 'NEW') |
| 1863 | self.assertEqual(A.reported, 1) |
| 1864 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
James E. Blair | 78e31b3 | 2013-07-09 09:11:34 -0700 | [diff] [blame] | 1865 | 'SUCCESS') |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1866 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 1867 | 'SUCCESS') |
| 1868 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 1869 | 'FAILURE') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1870 | |
| 1871 | def test_dependent_behind_dequeue(self): |
| 1872 | "test that dependent changes behind dequeued changes work" |
| 1873 | # This complicated test is a reproduction of a real life bug |
| 1874 | self.sched.reconfigure(self.config) |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1875 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1876 | self.worker.hold_jobs_in_build = True |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1877 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 1878 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 1879 | C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C') |
| 1880 | D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D') |
| 1881 | E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E') |
| 1882 | F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F') |
| 1883 | D.setDependsOn(C, 1) |
| 1884 | E.setDependsOn(D, 1) |
| 1885 | A.addApproval('CRVW', 2) |
| 1886 | B.addApproval('CRVW', 2) |
| 1887 | C.addApproval('CRVW', 2) |
| 1888 | D.addApproval('CRVW', 2) |
| 1889 | E.addApproval('CRVW', 2) |
| 1890 | F.addApproval('CRVW', 2) |
| 1891 | |
| 1892 | A.fail_merge = True |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1893 | |
| 1894 | # Change object re-use in the gerrit trigger is hidden if |
| 1895 | # changes are added in quick succession; waiting makes it more |
| 1896 | # like real life. |
| 1897 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1898 | self.waitUntilSettled() |
| 1899 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1900 | self.waitUntilSettled() |
| 1901 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1902 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1903 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1904 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1905 | self.waitUntilSettled() |
| 1906 | |
| 1907 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1908 | self.waitUntilSettled() |
| 1909 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 1910 | self.waitUntilSettled() |
| 1911 | self.fake_gerrit.addEvent(E.addApproval('APRV', 1)) |
| 1912 | self.waitUntilSettled() |
| 1913 | self.fake_gerrit.addEvent(F.addApproval('APRV', 1)) |
| 1914 | self.waitUntilSettled() |
| 1915 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1916 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1917 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1918 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1919 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1920 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1921 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1922 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1923 | self.waitUntilSettled() |
| 1924 | |
| 1925 | # all jobs running |
James E. Blair | e955e06 | 2012-10-08 09:49:03 -0700 | [diff] [blame] | 1926 | |
| 1927 | # Grab pointers to the jobs we want to release before |
| 1928 | # releasing any, because list indexes may change as |
| 1929 | # the jobs complete. |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1930 | a, b, c = self.builds[:3] |
James E. Blair | e955e06 | 2012-10-08 09:49:03 -0700 | [diff] [blame] | 1931 | a.release() |
| 1932 | b.release() |
| 1933 | c.release() |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1934 | self.waitUntilSettled() |
| 1935 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1936 | self.worker.hold_jobs_in_build = False |
| 1937 | self.worker.release() |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1938 | self.waitUntilSettled() |
| 1939 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1940 | self.assertEqual(A.data['status'], 'NEW') |
| 1941 | self.assertEqual(B.data['status'], 'MERGED') |
| 1942 | self.assertEqual(C.data['status'], 'MERGED') |
| 1943 | self.assertEqual(D.data['status'], 'MERGED') |
| 1944 | self.assertEqual(E.data['status'], 'MERGED') |
| 1945 | self.assertEqual(F.data['status'], 'MERGED') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1946 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1947 | self.assertEqual(A.reported, 2) |
| 1948 | self.assertEqual(B.reported, 2) |
| 1949 | self.assertEqual(C.reported, 2) |
| 1950 | self.assertEqual(D.reported, 2) |
| 1951 | self.assertEqual(E.reported, 2) |
| 1952 | self.assertEqual(F.reported, 2) |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1953 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1954 | self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15) |
| 1955 | self.assertEqual(len(self.history), 44) |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 1956 | |
| 1957 | def test_merger_repack(self): |
| 1958 | "Test that the merger works after a repack" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1959 | |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 1960 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1961 | A.addApproval('CRVW', 2) |
| 1962 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1963 | self.waitUntilSettled() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1964 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
| 1965 | 'SUCCESS') |
| 1966 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 1967 | 'SUCCESS') |
| 1968 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 1969 | 'SUCCESS') |
| 1970 | self.assertEqual(A.data['status'], 'MERGED') |
| 1971 | self.assertEqual(A.reported, 2) |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 1972 | self.assertEmptyQueues() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1973 | self.worker.build_history = [] |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 1974 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1975 | path = os.path.join(self.git_root, "org/project") |
| 1976 | print repack_repo(path) |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 1977 | |
| 1978 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1979 | A.addApproval('CRVW', 2) |
| 1980 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1981 | self.waitUntilSettled() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1982 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
| 1983 | 'SUCCESS') |
| 1984 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 1985 | 'SUCCESS') |
| 1986 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 1987 | 'SUCCESS') |
| 1988 | self.assertEqual(A.data['status'], 'MERGED') |
| 1989 | self.assertEqual(A.reported, 2) |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 1990 | |
James E. Blair | 4886f28 | 2012-11-15 09:27:33 -0800 | [diff] [blame] | 1991 | def test_merger_repack_large_change(self): |
| 1992 | "Test that the merger works with large changes after a repack" |
| 1993 | # https://bugs.launchpad.net/zuul/+bug/1078946 |
| 1994 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 1995 | A.addPatchset(large=True) |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1996 | path = os.path.join(self.upstream_root, "org/project1") |
| 1997 | print repack_repo(path) |
| 1998 | path = os.path.join(self.git_root, "org/project1") |
| 1999 | print repack_repo(path) |
James E. Blair | 4886f28 | 2012-11-15 09:27:33 -0800 | [diff] [blame] | 2000 | |
| 2001 | A.addApproval('CRVW', 2) |
| 2002 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2003 | self.waitUntilSettled() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2004 | self.assertEqual(self.getJobFromHistory('project1-merge').result, |
| 2005 | 'SUCCESS') |
| 2006 | self.assertEqual(self.getJobFromHistory('project1-test1').result, |
| 2007 | 'SUCCESS') |
| 2008 | self.assertEqual(self.getJobFromHistory('project1-test2').result, |
| 2009 | 'SUCCESS') |
| 2010 | self.assertEqual(A.data['status'], 'MERGED') |
| 2011 | self.assertEqual(A.reported, 2) |
James E. Blair | 4886f28 | 2012-11-15 09:27:33 -0800 | [diff] [blame] | 2012 | |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 2013 | def test_nonexistent_job(self): |
| 2014 | "Test launching a job that doesn't exist" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2015 | # Set to the state immediately after a restart |
| 2016 | self.resetGearmanServer() |
| 2017 | self.launcher.negative_function_cache_ttl = 0 |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 2018 | |
| 2019 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2020 | A.addApproval('CRVW', 2) |
| 2021 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2022 | # There may be a thread about to report a lost change |
| 2023 | while A.reported < 2: |
| 2024 | self.waitUntilSettled() |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 2025 | job_names = [x.name for x in self.history] |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2026 | self.assertFalse(job_names) |
| 2027 | self.assertEqual(A.data['status'], 'NEW') |
| 2028 | self.assertEqual(A.reported, 2) |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 2029 | self.assertEmptyQueues() |
| 2030 | |
| 2031 | # Make sure things still work: |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2032 | self.registerJobs() |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 2033 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2034 | A.addApproval('CRVW', 2) |
| 2035 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2036 | self.waitUntilSettled() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2037 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
| 2038 | 'SUCCESS') |
| 2039 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 2040 | 'SUCCESS') |
| 2041 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 2042 | 'SUCCESS') |
| 2043 | self.assertEqual(A.data['status'], 'MERGED') |
| 2044 | self.assertEqual(A.reported, 2) |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 2045 | |
| 2046 | def test_single_nonexistent_post_job(self): |
| 2047 | "Test launching a single post job that doesn't exist" |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 2048 | e = { |
| 2049 | "type": "ref-updated", |
| 2050 | "submitter": { |
| 2051 | "name": "User Name", |
| 2052 | }, |
| 2053 | "refUpdate": { |
| 2054 | "oldRev": "90f173846e3af9154517b88543ffbd1691f31366", |
| 2055 | "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543", |
| 2056 | "refName": "master", |
| 2057 | "project": "org/project", |
| 2058 | } |
| 2059 | } |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2060 | # Set to the state immediately after a restart |
| 2061 | self.resetGearmanServer() |
| 2062 | self.launcher.negative_function_cache_ttl = 0 |
| 2063 | |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 2064 | self.fake_gerrit.addEvent(e) |
| 2065 | self.waitUntilSettled() |
| 2066 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2067 | self.assertEqual(len(self.history), 0) |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2068 | |
| 2069 | def test_new_patchset_dequeues_old(self): |
| 2070 | "Test that a new patchset causes the old to be dequeued" |
| 2071 | # D -> C (depends on B) -> B (depends on A) -> A -> M |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2072 | self.worker.hold_jobs_in_build = True |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2073 | M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M') |
| 2074 | M.setMerged() |
| 2075 | |
| 2076 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2077 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2078 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 2079 | D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D') |
| 2080 | A.addApproval('CRVW', 2) |
| 2081 | B.addApproval('CRVW', 2) |
| 2082 | C.addApproval('CRVW', 2) |
| 2083 | D.addApproval('CRVW', 2) |
| 2084 | |
| 2085 | C.setDependsOn(B, 1) |
| 2086 | B.setDependsOn(A, 1) |
| 2087 | A.setDependsOn(M, 1) |
| 2088 | |
| 2089 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2090 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2091 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2092 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 2093 | self.waitUntilSettled() |
| 2094 | |
| 2095 | B.addPatchset() |
| 2096 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2)) |
| 2097 | self.waitUntilSettled() |
| 2098 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2099 | self.worker.hold_jobs_in_build = False |
| 2100 | self.worker.release() |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2101 | self.waitUntilSettled() |
| 2102 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2103 | self.assertEqual(A.data['status'], 'MERGED') |
| 2104 | self.assertEqual(A.reported, 2) |
| 2105 | self.assertEqual(B.data['status'], 'NEW') |
| 2106 | self.assertEqual(B.reported, 2) |
| 2107 | self.assertEqual(C.data['status'], 'NEW') |
| 2108 | self.assertEqual(C.reported, 2) |
| 2109 | self.assertEqual(D.data['status'], 'MERGED') |
| 2110 | self.assertEqual(D.reported, 2) |
| 2111 | 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] | 2112 | |
| 2113 | def test_new_patchset_dequeues_old_on_head(self): |
| 2114 | "Test that a new patchset causes the old to be dequeued (at head)" |
| 2115 | # D -> C (depends on B) -> B (depends on A) -> A -> M |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2116 | self.worker.hold_jobs_in_build = True |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2117 | M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M') |
| 2118 | M.setMerged() |
| 2119 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2120 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2121 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 2122 | D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D') |
| 2123 | A.addApproval('CRVW', 2) |
| 2124 | B.addApproval('CRVW', 2) |
| 2125 | C.addApproval('CRVW', 2) |
| 2126 | D.addApproval('CRVW', 2) |
| 2127 | |
| 2128 | C.setDependsOn(B, 1) |
| 2129 | B.setDependsOn(A, 1) |
| 2130 | A.setDependsOn(M, 1) |
| 2131 | |
| 2132 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2133 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2134 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2135 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 2136 | self.waitUntilSettled() |
| 2137 | |
| 2138 | A.addPatchset() |
| 2139 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2)) |
| 2140 | self.waitUntilSettled() |
| 2141 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2142 | self.worker.hold_jobs_in_build = False |
| 2143 | self.worker.release() |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2144 | self.waitUntilSettled() |
| 2145 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2146 | self.assertEqual(A.data['status'], 'NEW') |
| 2147 | self.assertEqual(A.reported, 2) |
| 2148 | self.assertEqual(B.data['status'], 'NEW') |
| 2149 | self.assertEqual(B.reported, 2) |
| 2150 | self.assertEqual(C.data['status'], 'NEW') |
| 2151 | self.assertEqual(C.reported, 2) |
| 2152 | self.assertEqual(D.data['status'], 'MERGED') |
| 2153 | self.assertEqual(D.reported, 2) |
| 2154 | self.assertEqual(len(self.history), 7) |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2155 | |
| 2156 | def test_new_patchset_dequeues_old_without_dependents(self): |
| 2157 | "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] | 2158 | self.worker.hold_jobs_in_build = True |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2159 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2160 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2161 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 2162 | A.addApproval('CRVW', 2) |
| 2163 | B.addApproval('CRVW', 2) |
| 2164 | C.addApproval('CRVW', 2) |
| 2165 | |
| 2166 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2167 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2168 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2169 | self.waitUntilSettled() |
| 2170 | |
| 2171 | B.addPatchset() |
| 2172 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2)) |
| 2173 | self.waitUntilSettled() |
| 2174 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2175 | self.worker.hold_jobs_in_build = False |
| 2176 | self.worker.release() |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2177 | self.waitUntilSettled() |
| 2178 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2179 | self.assertEqual(A.data['status'], 'MERGED') |
| 2180 | self.assertEqual(A.reported, 2) |
| 2181 | self.assertEqual(B.data['status'], 'NEW') |
| 2182 | self.assertEqual(B.reported, 2) |
| 2183 | self.assertEqual(C.data['status'], 'MERGED') |
| 2184 | self.assertEqual(C.reported, 2) |
| 2185 | self.assertEqual(len(self.history), 9) |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2186 | |
| 2187 | def test_new_patchset_dequeues_old_independent_queue(self): |
| 2188 | "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] | 2189 | self.worker.hold_jobs_in_build = True |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2190 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2191 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2192 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 2193 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 2194 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 2195 | self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1)) |
| 2196 | self.waitUntilSettled() |
| 2197 | |
| 2198 | B.addPatchset() |
| 2199 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2)) |
| 2200 | self.waitUntilSettled() |
| 2201 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2202 | self.worker.hold_jobs_in_build = False |
| 2203 | self.worker.release() |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2204 | self.waitUntilSettled() |
| 2205 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2206 | self.assertEqual(A.data['status'], 'NEW') |
| 2207 | self.assertEqual(A.reported, 1) |
| 2208 | self.assertEqual(B.data['status'], 'NEW') |
| 2209 | self.assertEqual(B.reported, 1) |
| 2210 | self.assertEqual(C.data['status'], 'NEW') |
| 2211 | self.assertEqual(C.reported, 1) |
| 2212 | self.assertEqual(len(self.history), 10) |
| 2213 | self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2214 | |
| 2215 | def test_zuul_refs(self): |
| 2216 | "Test that zuul refs exist and have the right changes" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2217 | self.worker.hold_jobs_in_build = True |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2218 | M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1') |
| 2219 | M1.setMerged() |
| 2220 | M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2') |
| 2221 | M2.setMerged() |
| 2222 | |
| 2223 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 2224 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 2225 | C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C') |
| 2226 | D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D') |
| 2227 | A.addApproval('CRVW', 2) |
| 2228 | B.addApproval('CRVW', 2) |
| 2229 | C.addApproval('CRVW', 2) |
| 2230 | D.addApproval('CRVW', 2) |
| 2231 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2232 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2233 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2234 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 2235 | |
| 2236 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2237 | self.worker.release('.*-merge') |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2238 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2239 | self.worker.release('.*-merge') |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2240 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2241 | self.worker.release('.*-merge') |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2242 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2243 | self.worker.release('.*-merge') |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2244 | self.waitUntilSettled() |
| 2245 | |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2246 | a_zref = b_zref = c_zref = d_zref = None |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 2247 | for x in self.builds: |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2248 | if x.parameters['ZUUL_CHANGE'] == '3': |
| 2249 | a_zref = x.parameters['ZUUL_REF'] |
| 2250 | if x.parameters['ZUUL_CHANGE'] == '4': |
| 2251 | b_zref = x.parameters['ZUUL_REF'] |
| 2252 | if x.parameters['ZUUL_CHANGE'] == '5': |
| 2253 | c_zref = x.parameters['ZUUL_REF'] |
| 2254 | if x.parameters['ZUUL_CHANGE'] == '6': |
| 2255 | d_zref = x.parameters['ZUUL_REF'] |
| 2256 | |
| 2257 | # There are... four... refs. |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2258 | self.assertIsNotNone(a_zref) |
| 2259 | self.assertIsNotNone(b_zref) |
| 2260 | self.assertIsNotNone(c_zref) |
| 2261 | self.assertIsNotNone(d_zref) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2262 | |
| 2263 | # And they should all be different |
| 2264 | refs = set([a_zref, b_zref, c_zref, d_zref]) |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2265 | self.assertEqual(len(refs), 4) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2266 | |
| 2267 | # 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] | 2268 | self.assertTrue(self.ref_has_change(a_zref, A)) |
| 2269 | self.assertFalse(self.ref_has_change(a_zref, B)) |
| 2270 | self.assertFalse(self.ref_has_change(a_zref, M2)) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2271 | |
| 2272 | # 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] | 2273 | self.assertTrue(self.ref_has_change(b_zref, A)) |
| 2274 | self.assertTrue(self.ref_has_change(b_zref, B)) |
| 2275 | self.assertFalse(self.ref_has_change(b_zref, M2)) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2276 | |
| 2277 | # c ref should have a and b in 1, c in 2 |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2278 | self.assertTrue(self.ref_has_change(c_zref, A)) |
| 2279 | self.assertTrue(self.ref_has_change(c_zref, B)) |
| 2280 | self.assertTrue(self.ref_has_change(c_zref, C)) |
| 2281 | self.assertFalse(self.ref_has_change(c_zref, D)) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2282 | |
| 2283 | # 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] | 2284 | self.assertTrue(self.ref_has_change(d_zref, A)) |
| 2285 | self.assertTrue(self.ref_has_change(d_zref, B)) |
| 2286 | self.assertTrue(self.ref_has_change(d_zref, C)) |
| 2287 | self.assertTrue(self.ref_has_change(d_zref, D)) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2288 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2289 | self.worker.hold_jobs_in_build = False |
| 2290 | self.worker.release() |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2291 | self.waitUntilSettled() |
| 2292 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2293 | self.assertEqual(A.data['status'], 'MERGED') |
| 2294 | self.assertEqual(A.reported, 2) |
| 2295 | self.assertEqual(B.data['status'], 'MERGED') |
| 2296 | self.assertEqual(B.reported, 2) |
| 2297 | self.assertEqual(C.data['status'], 'MERGED') |
| 2298 | self.assertEqual(C.reported, 2) |
| 2299 | self.assertEqual(D.data['status'], 'MERGED') |
| 2300 | self.assertEqual(D.reported, 2) |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 2301 | |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 2302 | def test_statsd(self): |
| 2303 | "Test each of the statsd methods used in the scheduler" |
| 2304 | import extras |
| 2305 | statsd = extras.try_import('statsd.statsd') |
| 2306 | statsd.incr('test-incr') |
| 2307 | statsd.timing('test-timing', 3) |
| 2308 | statsd.gauge('test-guage', 12) |
| 2309 | self.assertReportedStat('test-incr', '1|c') |
| 2310 | self.assertReportedStat('test-timing', '3|ms') |
| 2311 | self.assertReportedStat('test-guage', '12|g') |
| 2312 | |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 2313 | def test_file_jobs(self): |
| 2314 | "Test that file jobs run only when appropriate" |
| 2315 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2316 | A.addPatchset(['pip-requires']) |
| 2317 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2318 | A.addApproval('CRVW', 2) |
| 2319 | B.addApproval('CRVW', 2) |
| 2320 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2321 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2322 | self.waitUntilSettled() |
| 2323 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 2324 | testfile_jobs = [x for x in self.history |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 2325 | if x.name == 'project-testfile'] |
| 2326 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2327 | self.assertEqual(len(testfile_jobs), 1) |
| 2328 | self.assertEqual(testfile_jobs[0].changes, '1,2') |
| 2329 | self.assertEqual(A.data['status'], 'MERGED') |
| 2330 | self.assertEqual(A.reported, 2) |
| 2331 | self.assertEqual(B.data['status'], 'MERGED') |
| 2332 | self.assertEqual(B.reported, 2) |
James E. Blair | 3c5e5b5 | 2013-04-26 11:17:03 -0700 | [diff] [blame] | 2333 | |
| 2334 | def test_test_config(self): |
| 2335 | "Test that we can test the config" |
| 2336 | sched = zuul.scheduler.Scheduler() |
| 2337 | sched.testConfig(CONFIG.get('zuul', 'layout_config')) |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2338 | |
| 2339 | def test_build_description(self): |
| 2340 | "Test that build descriptions update" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2341 | self.worker.registerFunction('set_description:' + |
| 2342 | self.worker.worker_id) |
| 2343 | |
| 2344 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2345 | A.addApproval('CRVW', 2) |
| 2346 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2347 | self.waitUntilSettled() |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 2348 | desc = self.history[0].description |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2349 | self.log.debug("Description: %s" % desc) |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2350 | self.assertTrue(re.search("Branch.*master", desc)) |
| 2351 | self.assertTrue(re.search("Pipeline.*gate", desc)) |
| 2352 | self.assertTrue(re.search("project-merge.*SUCCESS", desc)) |
| 2353 | self.assertTrue(re.search("project-test1.*SUCCESS", desc)) |
| 2354 | self.assertTrue(re.search("project-test2.*SUCCESS", desc)) |
| 2355 | self.assertTrue(re.search("Reported result.*SUCCESS", desc)) |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2356 | |
James E. Blair | 64ed6f2 | 2013-07-10 14:07:23 -0700 | [diff] [blame] | 2357 | def test_queue_precedence(self): |
| 2358 | "Test that queue precedence works" |
| 2359 | |
| 2360 | self.gearman_server.hold_jobs_in_queue = True |
James E. Blair | 8de58bd | 2013-07-18 16:23:33 -0700 | [diff] [blame] | 2361 | self.worker.hold_jobs_in_build = True |
James E. Blair | 64ed6f2 | 2013-07-10 14:07:23 -0700 | [diff] [blame] | 2362 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2363 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 2364 | A.addApproval('CRVW', 2) |
| 2365 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2366 | |
| 2367 | self.waitUntilSettled() |
| 2368 | self.gearman_server.hold_jobs_in_queue = False |
| 2369 | self.gearman_server.release() |
| 2370 | self.waitUntilSettled() |
| 2371 | |
James E. Blair | 8de58bd | 2013-07-18 16:23:33 -0700 | [diff] [blame] | 2372 | # Run one build at a time to ensure non-race order: |
| 2373 | for x in range(6): |
| 2374 | self.release(self.builds[0]) |
| 2375 | self.waitUntilSettled() |
| 2376 | self.worker.hold_jobs_in_build = False |
| 2377 | self.waitUntilSettled() |
| 2378 | |
James E. Blair | 64ed6f2 | 2013-07-10 14:07:23 -0700 | [diff] [blame] | 2379 | self.log.debug(self.history) |
| 2380 | self.assertEqual(self.history[0].pipeline, 'gate') |
| 2381 | self.assertEqual(self.history[1].pipeline, 'check') |
| 2382 | self.assertEqual(self.history[2].pipeline, 'gate') |
| 2383 | self.assertEqual(self.history[3].pipeline, 'gate') |
| 2384 | self.assertEqual(self.history[4].pipeline, 'check') |
| 2385 | self.assertEqual(self.history[5].pipeline, 'check') |
| 2386 | |
James E. Blair | 1843a55 | 2013-07-03 14:19:52 -0700 | [diff] [blame] | 2387 | def test_json_status(self): |
| 2388 | "Test that we can retrieve JSON status info" |
| 2389 | self.worker.hold_jobs_in_build = True |
| 2390 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2391 | A.addApproval('CRVW', 2) |
| 2392 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2393 | self.waitUntilSettled() |
| 2394 | |
| 2395 | port = self.webapp.server.socket.getsockname()[1] |
| 2396 | |
| 2397 | f = urllib.urlopen("http://localhost:%s/status.json" % port) |
| 2398 | data = f.read() |
| 2399 | |
| 2400 | self.worker.hold_jobs_in_build = False |
| 2401 | self.worker.release() |
| 2402 | self.waitUntilSettled() |
| 2403 | |
| 2404 | data = json.loads(data) |
| 2405 | status_jobs = set() |
| 2406 | for p in data['pipelines']: |
| 2407 | for q in p['change_queues']: |
| 2408 | for head in q['heads']: |
| 2409 | for change in head: |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2410 | self.assertEqual(change['id'], '1,1') |
James E. Blair | 1843a55 | 2013-07-03 14:19:52 -0700 | [diff] [blame] | 2411 | for job in change['jobs']: |
| 2412 | status_jobs.add(job['name']) |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2413 | self.assertIn('project-merge', status_jobs) |
| 2414 | self.assertIn('project-test1', status_jobs) |
| 2415 | self.assertIn('project-test2', status_jobs) |
James E. Blair | 1843a55 | 2013-07-03 14:19:52 -0700 | [diff] [blame] | 2416 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2417 | def test_node_label(self): |
| 2418 | "Test that a job runs on a specific node label" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2419 | self.worker.registerFunction('build:node-project-test1:debian') |
| 2420 | |
| 2421 | A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A') |
| 2422 | A.addApproval('CRVW', 2) |
| 2423 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2424 | self.waitUntilSettled() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 2425 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2426 | self.assertIsNone(self.getJobFromHistory('node-project-merge').node) |
| 2427 | self.assertEqual(self.getJobFromHistory('node-project-test1').node, |
| 2428 | 'debian') |
| 2429 | self.assertIsNone(self.getJobFromHistory('node-project-test2').node) |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 2430 | |
| 2431 | def test_live_reconfiguration(self): |
| 2432 | "Test that live reconfiguration works" |
| 2433 | self.worker.hold_jobs_in_build = True |
| 2434 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2435 | A.addApproval('CRVW', 2) |
| 2436 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2437 | self.waitUntilSettled() |
| 2438 | |
| 2439 | self.sched.reconfigure(self.config) |
| 2440 | |
| 2441 | self.worker.hold_jobs_in_build = False |
| 2442 | self.worker.release() |
| 2443 | self.waitUntilSettled() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2444 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
| 2445 | 'SUCCESS') |
| 2446 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 2447 | 'SUCCESS') |
| 2448 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 2449 | 'SUCCESS') |
| 2450 | self.assertEqual(A.data['status'], 'MERGED') |
| 2451 | self.assertEqual(A.reported, 2) |
James E. Blair | 287c06d | 2013-07-24 10:39:30 -0700 | [diff] [blame^] | 2452 | |
| 2453 | def test_delayed_repo_init(self): |
| 2454 | self.config.set('zuul', 'layout_config', |
| 2455 | 'tests/fixtures/layout-delayed-repo-init.yaml') |
| 2456 | self.sched.reconfigure(self.config) |
| 2457 | |
| 2458 | self.init_repo("org/new-project") |
| 2459 | A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A') |
| 2460 | |
| 2461 | A.addApproval('CRVW', 2) |
| 2462 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2463 | self.waitUntilSettled() |
| 2464 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
| 2465 | 'SUCCESS') |
| 2466 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 2467 | 'SUCCESS') |
| 2468 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 2469 | 'SUCCESS') |
| 2470 | self.assertEqual(A.data['status'], 'MERGED') |
| 2471 | self.assertEqual(A.reported, 2) |