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