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