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