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