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 | 18c6444 | 2014-03-18 10:14:45 -0700 | [diff] [blame^] | 798 | self.init_repo("org/noop-project") |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 799 | |
| 800 | self.statsd = FakeStatsd() |
| 801 | os.environ['STATSD_HOST'] = 'localhost' |
| 802 | os.environ['STATSD_PORT'] = str(self.statsd.port) |
| 803 | self.statsd.start() |
| 804 | # the statsd client object is configured in the statsd module import |
| 805 | reload(statsd) |
| 806 | reload(zuul.scheduler) |
| 807 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 808 | self.gearman_server = FakeGearmanServer() |
| 809 | |
| 810 | self.config = ConfigParser.ConfigParser() |
| 811 | cfg = StringIO() |
| 812 | CONFIG.write(cfg) |
| 813 | cfg.seek(0) |
| 814 | self.config.readfp(cfg) |
| 815 | self.config.set('gearman', 'port', str(self.gearman_server.port)) |
| 816 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 817 | self.worker = FakeWorker('fake_worker', self) |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 818 | self.worker.addServer('127.0.0.1', self.gearman_server.port) |
| 819 | self.gearman_server.worker = self.worker |
| 820 | |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 821 | self.merge_server = zuul.merger.server.MergeServer(self.config) |
| 822 | self.merge_server.start() |
| 823 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 824 | self.sched = zuul.scheduler.Scheduler() |
| 825 | |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 826 | def URLOpenerFactory(*args, **kw): |
| 827 | args = [self.fake_gerrit] + list(args) |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 828 | return FakeURLOpener(self.upstream_root, *args, **kw) |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 829 | |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 830 | urllib2.urlopen = URLOpenerFactory |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 831 | self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched) |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 832 | self.merge_client = zuul.merger.client.MergeClient( |
| 833 | self.config, self.sched) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 834 | |
James E. Blair | ff80a2f | 2013-12-27 13:24:06 -0800 | [diff] [blame] | 835 | self.smtp_messages = [] |
| 836 | |
| 837 | def FakeSMTPFactory(*args, **kw): |
| 838 | args = [self.smtp_messages] + list(args) |
| 839 | return FakeSMTP(*args, **kw) |
| 840 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 841 | zuul.lib.gerrit.Gerrit = FakeGerrit |
James E. Blair | ff80a2f | 2013-12-27 13:24:06 -0800 | [diff] [blame] | 842 | self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTPFactory)) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 843 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 844 | self.gerrit = FakeGerritTrigger( |
| 845 | self.upstream_root, self.config, self.sched) |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 846 | self.gerrit.replication_timeout = 1.5 |
| 847 | self.gerrit.replication_retry_interval = 0.5 |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 848 | self.fake_gerrit = self.gerrit.gerrit |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 849 | self.fake_gerrit.upstream_root = self.upstream_root |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 850 | |
James E. Blair | 1843a55 | 2013-07-03 14:19:52 -0700 | [diff] [blame] | 851 | self.webapp = zuul.webapp.WebApp(self.sched, port=0) |
James E. Blair | ad28e91 | 2013-11-27 10:43:22 -0800 | [diff] [blame] | 852 | self.rpc = zuul.rpclistener.RPCListener(self.config, self.sched) |
James E. Blair | 1843a55 | 2013-07-03 14:19:52 -0700 | [diff] [blame] | 853 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 854 | self.sched.setLauncher(self.launcher) |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 855 | self.sched.setMerger(self.merge_client) |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 856 | self.sched.registerTrigger(self.gerrit) |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 857 | self.timer = zuul.trigger.timer.Timer(self.config, self.sched) |
| 858 | self.sched.registerTrigger(self.timer) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 859 | |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 860 | self.sched.registerReporter( |
| 861 | zuul.reporter.gerrit.Reporter(self.gerrit)) |
Joshua Hesketh | 5fea867 | 2013-08-19 17:32:01 +1000 | [diff] [blame] | 862 | self.smtp_reporter = zuul.reporter.smtp.Reporter( |
| 863 | self.config.get('smtp', 'default_from'), |
| 864 | self.config.get('smtp', 'default_to'), |
| 865 | self.config.get('smtp', 'server')) |
| 866 | self.sched.registerReporter(self.smtp_reporter) |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 867 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 868 | self.sched.start() |
| 869 | self.sched.reconfigure(self.config) |
| 870 | self.sched.resume() |
James E. Blair | 1843a55 | 2013-07-03 14:19:52 -0700 | [diff] [blame] | 871 | self.webapp.start() |
James E. Blair | ad28e91 | 2013-11-27 10:43:22 -0800 | [diff] [blame] | 872 | self.rpc.start() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 873 | self.launcher.gearman.waitForServer() |
| 874 | self.registerJobs() |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 875 | self.builds = self.worker.running_builds |
| 876 | self.history = self.worker.build_history |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 877 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 878 | self.addCleanup(self.assertFinalState) |
| 879 | self.addCleanup(self.shutdown) |
| 880 | |
| 881 | def assertFinalState(self): |
| 882 | # Make sure that the change cache is cleared |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 883 | self.assertEqual(len(self.gerrit._change_cache.keys()), 0) |
Clark Boylan | 4ba48d9 | 2013-11-11 18:03:53 -0800 | [diff] [blame] | 884 | # Make sure that git.Repo objects have been garbage collected. |
| 885 | repos = [] |
| 886 | gc.collect() |
| 887 | for obj in gc.get_objects(): |
| 888 | if isinstance(obj, git.Repo): |
| 889 | repos.append(obj) |
| 890 | self.assertEqual(len(repos), 0) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 891 | self.assertEmptyQueues() |
| 892 | |
| 893 | def shutdown(self): |
| 894 | self.log.debug("Shutting down after tests") |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 895 | self.launcher.stop() |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 896 | self.merge_server.stop() |
| 897 | self.merge_server.join() |
| 898 | self.merge_client.stop() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 899 | self.worker.shutdown() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 900 | self.gerrit.stop() |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 901 | self.timer.stop() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 902 | self.sched.stop() |
| 903 | self.sched.join() |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 904 | self.statsd.stop() |
| 905 | self.statsd.join() |
James E. Blair | 1843a55 | 2013-07-03 14:19:52 -0700 | [diff] [blame] | 906 | self.webapp.stop() |
| 907 | self.webapp.join() |
James E. Blair | ad28e91 | 2013-11-27 10:43:22 -0800 | [diff] [blame] | 908 | self.rpc.stop() |
| 909 | self.rpc.join() |
James E. Blair | 491b317 | 2014-03-10 15:05:36 -0700 | [diff] [blame] | 910 | self.gearman_server.shutdown() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 911 | threads = threading.enumerate() |
| 912 | if len(threads) > 1: |
| 913 | self.log.error("More than one thread is running: %s" % threads) |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 914 | super(TestScheduler, self).tearDown() |
| 915 | |
| 916 | def init_repo(self, project): |
| 917 | parts = project.split('/') |
| 918 | path = os.path.join(self.upstream_root, *parts[:-1]) |
| 919 | if not os.path.exists(path): |
| 920 | os.makedirs(path) |
| 921 | path = os.path.join(self.upstream_root, project) |
| 922 | repo = git.Repo.init(path) |
| 923 | |
| 924 | repo.config_writer().set_value('user', 'email', 'user@example.com') |
| 925 | repo.config_writer().set_value('user', 'name', 'User Name') |
| 926 | repo.config_writer().write() |
| 927 | |
| 928 | fn = os.path.join(path, 'README') |
| 929 | f = open(fn, 'w') |
| 930 | f.write("test\n") |
| 931 | f.close() |
| 932 | repo.index.add([fn]) |
| 933 | repo.index.commit('initial commit') |
| 934 | master = repo.create_head('master') |
| 935 | repo.create_tag('init') |
| 936 | |
| 937 | mp = repo.create_head('mp') |
| 938 | repo.head.reference = mp |
| 939 | f = open(fn, 'a') |
| 940 | f.write("test mp\n") |
| 941 | f.close() |
| 942 | repo.index.add([fn]) |
| 943 | repo.index.commit('mp commit') |
| 944 | |
| 945 | repo.head.reference = master |
| 946 | repo.head.reset(index=True, working_tree=True) |
| 947 | repo.git.clean('-x', '-f', '-d') |
| 948 | |
| 949 | def ref_has_change(self, ref, change): |
| 950 | path = os.path.join(self.git_root, change.project) |
| 951 | repo = git.Repo(path) |
| 952 | for commit in repo.iter_commits(ref): |
| 953 | if commit.message.strip() == ('%s-1' % change.subject): |
| 954 | return True |
| 955 | return False |
| 956 | |
| 957 | def job_has_changes(self, *args): |
| 958 | job = args[0] |
| 959 | commits = args[1:] |
| 960 | if isinstance(job, FakeBuild): |
| 961 | parameters = job.parameters |
| 962 | else: |
| 963 | parameters = json.loads(job.arguments) |
| 964 | project = parameters['ZUUL_PROJECT'] |
| 965 | path = os.path.join(self.git_root, project) |
| 966 | repo = git.Repo(path) |
| 967 | ref = parameters['ZUUL_REF'] |
| 968 | sha = parameters['ZUUL_COMMIT'] |
| 969 | repo_messages = [c.message.strip() for c in repo.iter_commits(ref)] |
| 970 | repo_shas = [c.hexsha for c in repo.iter_commits(ref)] |
| 971 | commit_messages = ['%s-1' % commit.subject for commit in commits] |
James E. Blair | ac2c324 | 2014-01-24 13:38:51 -0800 | [diff] [blame] | 972 | self.log.debug("Checking if job %s has changes; commit_messages %s;" |
| 973 | " repo_messages %s; sha %s" % (job, commit_messages, |
| 974 | repo_messages, sha)) |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 975 | for msg in commit_messages: |
| 976 | if msg not in repo_messages: |
James E. Blair | ac2c324 | 2014-01-24 13:38:51 -0800 | [diff] [blame] | 977 | self.log.debug(" messages do not match") |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 978 | return False |
| 979 | if repo_shas[0] != sha: |
James E. Blair | ac2c324 | 2014-01-24 13:38:51 -0800 | [diff] [blame] | 980 | self.log.debug(" sha does not match") |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 981 | return False |
James E. Blair | ac2c324 | 2014-01-24 13:38:51 -0800 | [diff] [blame] | 982 | self.log.debug(" OK") |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 983 | return True |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 984 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 985 | def registerJobs(self): |
| 986 | count = 0 |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 987 | for job in self.sched.layout.jobs.keys(): |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 988 | self.worker.registerFunction('build:' + job) |
| 989 | count += 1 |
| 990 | self.worker.registerFunction('stop:' + self.worker.worker_id) |
| 991 | count += 1 |
| 992 | |
| 993 | while len(self.gearman_server.functions) < count: |
| 994 | time.sleep(0) |
| 995 | |
| 996 | def release(self, job): |
| 997 | if isinstance(job, FakeBuild): |
| 998 | job.release() |
| 999 | else: |
| 1000 | job.waiting = False |
| 1001 | self.log.debug("Queued job %s released" % job.unique) |
| 1002 | self.gearman_server.wakeConnections() |
| 1003 | |
| 1004 | def getParameter(self, job, name): |
| 1005 | if isinstance(job, FakeBuild): |
| 1006 | return job.parameters[name] |
| 1007 | else: |
| 1008 | parameters = json.loads(job.arguments) |
| 1009 | return parameters[name] |
| 1010 | |
| 1011 | def resetGearmanServer(self): |
| 1012 | self.worker.setFunctions([]) |
| 1013 | while True: |
| 1014 | done = True |
| 1015 | for connection in self.gearman_server.active_connections: |
James E. Blair | ad28e91 | 2013-11-27 10:43:22 -0800 | [diff] [blame] | 1016 | if (connection.functions and |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 1017 | connection.client_id not in ['Zuul RPC Listener', |
| 1018 | 'Zuul Merger']): |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1019 | done = False |
| 1020 | if done: |
| 1021 | break |
| 1022 | time.sleep(0) |
| 1023 | self.gearman_server.functions = set() |
James E. Blair | ad28e91 | 2013-11-27 10:43:22 -0800 | [diff] [blame] | 1024 | self.rpc.register() |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 1025 | self.merge_server.register() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1026 | |
| 1027 | def haveAllBuildsReported(self): |
| 1028 | # See if Zuul is waiting on a meta job to complete |
| 1029 | if self.launcher.meta_jobs: |
| 1030 | return False |
| 1031 | # Find out if every build that the worker has completed has been |
| 1032 | # reported back to Zuul. If it hasn't then that means a Gearman |
| 1033 | # event is still in transit and the system is not stable. |
| 1034 | for build in self.worker.build_history: |
| 1035 | zbuild = self.launcher.builds.get(build.uuid) |
| 1036 | if not zbuild: |
| 1037 | # It has already been reported |
| 1038 | continue |
| 1039 | # It hasn't been reported yet. |
| 1040 | return False |
| 1041 | # Make sure that none of the worker connections are in GRAB_WAIT |
| 1042 | for connection in self.worker.active_connections: |
| 1043 | if connection.state == 'GRAB_WAIT': |
| 1044 | return False |
| 1045 | return True |
| 1046 | |
| 1047 | def areAllBuildsWaiting(self): |
| 1048 | ret = True |
| 1049 | |
| 1050 | builds = self.launcher.builds.values() |
| 1051 | for build in builds: |
| 1052 | client_job = None |
| 1053 | for conn in self.launcher.gearman.active_connections: |
| 1054 | for j in conn.related_jobs.values(): |
| 1055 | if j.unique == build.uuid: |
| 1056 | client_job = j |
| 1057 | break |
| 1058 | if not client_job: |
| 1059 | self.log.debug("%s is not known to the gearman client" % |
| 1060 | build) |
| 1061 | ret = False |
| 1062 | continue |
| 1063 | if not client_job.handle: |
| 1064 | self.log.debug("%s has no handle" % client_job) |
| 1065 | ret = False |
| 1066 | continue |
| 1067 | server_job = self.gearman_server.jobs.get(client_job.handle) |
| 1068 | if not server_job: |
| 1069 | self.log.debug("%s is not known to the gearman server" % |
| 1070 | client_job) |
| 1071 | ret = False |
| 1072 | continue |
| 1073 | if not hasattr(server_job, 'waiting'): |
| 1074 | self.log.debug("%s is being enqueued" % server_job) |
| 1075 | ret = False |
| 1076 | continue |
| 1077 | if server_job.waiting: |
| 1078 | continue |
| 1079 | worker_job = self.worker.gearman_jobs.get(server_job.unique) |
| 1080 | if worker_job: |
| 1081 | if worker_job.build.isWaiting(): |
| 1082 | continue |
| 1083 | else: |
| 1084 | self.log.debug("%s is running" % worker_job) |
| 1085 | ret = False |
| 1086 | else: |
| 1087 | self.log.debug("%s is unassigned" % server_job) |
| 1088 | ret = False |
| 1089 | return ret |
| 1090 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1091 | def waitUntilSettled(self): |
| 1092 | self.log.debug("Waiting until settled...") |
| 1093 | start = time.time() |
| 1094 | while True: |
| 1095 | if time.time() - start > 10: |
| 1096 | print 'queue status:', |
| 1097 | print self.sched.trigger_event_queue.empty(), |
| 1098 | print self.sched.result_event_queue.empty(), |
| 1099 | print self.fake_gerrit.event_queue.empty(), |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1100 | print self.areAllBuildsWaiting() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1101 | raise Exception("Timeout waiting for Zuul to settle") |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1102 | # Make sure no new events show up while we're checking |
| 1103 | self.worker.lock.acquire() |
| 1104 | # have all build states propogated to zuul? |
| 1105 | if self.haveAllBuildsReported(): |
| 1106 | # Join ensures that the queue is empty _and_ events have been |
| 1107 | # processed |
| 1108 | self.fake_gerrit.event_queue.join() |
| 1109 | self.sched.trigger_event_queue.join() |
| 1110 | self.sched.result_event_queue.join() |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 1111 | self.sched.run_handler_lock.acquire() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1112 | if (self.sched.trigger_event_queue.empty() and |
| 1113 | self.sched.result_event_queue.empty() and |
| 1114 | self.fake_gerrit.event_queue.empty() and |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 1115 | not self.merge_client.build_sets and |
James E. Blair | 9c2985a | 2014-03-10 10:59:05 -0700 | [diff] [blame] | 1116 | self.haveAllBuildsReported() and |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1117 | self.areAllBuildsWaiting()): |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 1118 | self.sched.run_handler_lock.release() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1119 | self.worker.lock.release() |
| 1120 | self.log.debug("...settled.") |
| 1121 | return |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 1122 | self.sched.run_handler_lock.release() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1123 | self.worker.lock.release() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1124 | self.sched.wake_event.wait(0.1) |
| 1125 | |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1126 | def countJobResults(self, jobs, result): |
James E. Blair | 0018a6c | 2013-02-27 14:11:45 -0800 | [diff] [blame] | 1127 | jobs = filter(lambda x: x.result == result, jobs) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1128 | return len(jobs) |
| 1129 | |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1130 | def getJobFromHistory(self, name): |
| 1131 | history = self.worker.build_history |
| 1132 | for job in history: |
| 1133 | if job.name == name: |
| 1134 | return job |
| 1135 | raise Exception("Unable to find job %s in history" % name) |
| 1136 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1137 | def assertEmptyQueues(self): |
| 1138 | # Make sure there are no orphaned jobs |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 1139 | for pipeline in self.sched.layout.pipelines.values(): |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1140 | for queue in pipeline.queues: |
| 1141 | if len(queue.queue) != 0: |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 1142 | print 'pipeline %s queue %s contents %s' % ( |
| 1143 | pipeline.name, queue.name, queue.queue) |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1144 | self.assertEqual(len(queue.queue), 0) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1145 | |
James E. Blair | 66eeebf | 2013-07-27 17:44:32 -0700 | [diff] [blame] | 1146 | def assertReportedStat(self, key, value=None, kind=None): |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 1147 | start = time.time() |
| 1148 | while time.time() < (start + 5): |
| 1149 | for stat in self.statsd.stats: |
James E. Blair | 66eeebf | 2013-07-27 17:44:32 -0700 | [diff] [blame] | 1150 | pprint.pprint(self.statsd.stats) |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 1151 | k, v = stat.split(':') |
| 1152 | if key == k: |
James E. Blair | 66eeebf | 2013-07-27 17:44:32 -0700 | [diff] [blame] | 1153 | if value is None and kind is None: |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 1154 | return |
James E. Blair | 66eeebf | 2013-07-27 17:44:32 -0700 | [diff] [blame] | 1155 | elif value: |
| 1156 | if value == v: |
| 1157 | return |
| 1158 | elif kind: |
| 1159 | if v.endswith('|' + kind): |
| 1160 | return |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 1161 | time.sleep(0.1) |
| 1162 | |
| 1163 | pprint.pprint(self.statsd.stats) |
| 1164 | raise Exception("Key %s not found in reported stats" % key) |
| 1165 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1166 | def test_jobs_launched(self): |
| 1167 | "Test that jobs are launched and a change is merged" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1168 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1169 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1170 | A.addApproval('CRVW', 2) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1171 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1172 | self.waitUntilSettled() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1173 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
| 1174 | 'SUCCESS') |
| 1175 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 1176 | 'SUCCESS') |
| 1177 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 1178 | 'SUCCESS') |
| 1179 | self.assertEqual(A.data['status'], 'MERGED') |
| 1180 | self.assertEqual(A.reported, 2) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1181 | |
James E. Blair | 66eeebf | 2013-07-27 17:44:32 -0700 | [diff] [blame] | 1182 | self.assertReportedStat('gerrit.event.comment-added', value='1|c') |
| 1183 | self.assertReportedStat('zuul.pipeline.gate.current_changes', |
| 1184 | value='1|g') |
| 1185 | self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS', |
| 1186 | kind='ms') |
| 1187 | self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS', |
| 1188 | value='1|c') |
| 1189 | self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms') |
| 1190 | self.assertReportedStat('zuul.pipeline.gate.total_changes', |
| 1191 | value='1|c') |
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.resident_time', kind='ms') |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 1194 | self.assertReportedStat( |
James E. Blair | 66eeebf | 2013-07-27 17:44:32 -0700 | [diff] [blame] | 1195 | 'zuul.pipeline.gate.org.project.total_changes', value='1|c') |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 1196 | |
James E. Blair | 3cb1070 | 2013-08-24 08:56:03 -0700 | [diff] [blame] | 1197 | def test_initial_pipeline_gauges(self): |
| 1198 | "Test that each pipeline reported its length on start" |
| 1199 | pipeline_names = self.sched.layout.pipelines.keys() |
| 1200 | self.assertNotEqual(len(pipeline_names), 0) |
| 1201 | for name in pipeline_names: |
| 1202 | self.assertReportedStat('zuul.pipeline.%s.current_changes' % name, |
| 1203 | value='0|g') |
| 1204 | |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 1205 | def test_duplicate_pipelines(self): |
| 1206 | "Test that a change matching multiple pipelines works" |
James E. Blair | 1b4d972 | 2013-05-21 10:32:04 -0700 | [diff] [blame] | 1207 | |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 1208 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1209 | self.fake_gerrit.addEvent(A.getChangeRestoredEvent()) |
| 1210 | self.waitUntilSettled() |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 1211 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1212 | self.assertEqual(len(self.history), 2) |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1213 | self.history[0].name == 'project-test1' |
| 1214 | self.history[1].name == 'project-test1' |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 1215 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1216 | self.assertEqual(len(A.messages), 2) |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 1217 | if 'dup1/project-test1' in A.messages[0]: |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1218 | self.assertIn('dup1/project-test1', A.messages[0]) |
| 1219 | self.assertNotIn('dup2/project-test1', A.messages[0]) |
| 1220 | self.assertNotIn('dup1/project-test1', A.messages[1]) |
| 1221 | self.assertIn('dup2/project-test1', A.messages[1]) |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 1222 | else: |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1223 | self.assertIn('dup1/project-test1', A.messages[1]) |
| 1224 | self.assertNotIn('dup2/project-test1', A.messages[1]) |
| 1225 | self.assertNotIn('dup1/project-test1', A.messages[0]) |
| 1226 | self.assertIn('dup2/project-test1', A.messages[0]) |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 1227 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1228 | def test_parallel_changes(self): |
| 1229 | "Test that changes are tested in parallel and merged in series" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1230 | |
| 1231 | self.worker.hold_jobs_in_build = True |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1232 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1233 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1234 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1235 | A.addApproval('CRVW', 2) |
| 1236 | B.addApproval('CRVW', 2) |
| 1237 | C.addApproval('CRVW', 2) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1238 | |
| 1239 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1240 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1241 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1242 | |
| 1243 | self.waitUntilSettled() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1244 | self.assertEqual(len(self.builds), 1) |
| 1245 | self.assertEqual(self.builds[0].name, 'project-merge') |
| 1246 | self.assertTrue(self.job_has_changes(self.builds[0], A)) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1247 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1248 | self.worker.release('.*-merge') |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1249 | self.waitUntilSettled() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1250 | self.assertEqual(len(self.builds), 3) |
| 1251 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 1252 | self.assertTrue(self.job_has_changes(self.builds[0], A)) |
| 1253 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 1254 | self.assertTrue(self.job_has_changes(self.builds[1], A)) |
| 1255 | self.assertEqual(self.builds[2].name, 'project-merge') |
| 1256 | self.assertTrue(self.job_has_changes(self.builds[2], A, B)) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1257 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1258 | self.worker.release('.*-merge') |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1259 | self.waitUntilSettled() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1260 | self.assertEqual(len(self.builds), 5) |
| 1261 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 1262 | self.assertTrue(self.job_has_changes(self.builds[0], A)) |
| 1263 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 1264 | self.assertTrue(self.job_has_changes(self.builds[1], A)) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1265 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1266 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 1267 | self.assertTrue(self.job_has_changes(self.builds[2], A, B)) |
| 1268 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 1269 | self.assertTrue(self.job_has_changes(self.builds[3], A, B)) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1270 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1271 | self.assertEqual(self.builds[4].name, 'project-merge') |
| 1272 | 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] | 1273 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1274 | self.worker.release('.*-merge') |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1275 | self.waitUntilSettled() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1276 | self.assertEqual(len(self.builds), 6) |
| 1277 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 1278 | self.assertTrue(self.job_has_changes(self.builds[0], A)) |
| 1279 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 1280 | self.assertTrue(self.job_has_changes(self.builds[1], A)) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1281 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1282 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 1283 | self.assertTrue(self.job_has_changes(self.builds[2], A, B)) |
| 1284 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 1285 | self.assertTrue(self.job_has_changes(self.builds[3], A, B)) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1286 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1287 | self.assertEqual(self.builds[4].name, 'project-test1') |
| 1288 | self.assertTrue(self.job_has_changes(self.builds[4], A, B, C)) |
| 1289 | self.assertEqual(self.builds[5].name, 'project-test2') |
| 1290 | 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] | 1291 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1292 | self.worker.hold_jobs_in_build = False |
| 1293 | self.worker.release() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1294 | self.waitUntilSettled() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1295 | self.assertEqual(len(self.builds), 0) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1296 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1297 | self.assertEqual(len(self.history), 9) |
| 1298 | self.assertEqual(A.data['status'], 'MERGED') |
| 1299 | self.assertEqual(B.data['status'], 'MERGED') |
| 1300 | self.assertEqual(C.data['status'], 'MERGED') |
| 1301 | self.assertEqual(A.reported, 2) |
| 1302 | self.assertEqual(B.reported, 2) |
| 1303 | self.assertEqual(C.reported, 2) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1304 | |
| 1305 | def test_failed_changes(self): |
| 1306 | "Test that a change behind a failed change is retested" |
James E. Blair | e281901 | 2013-06-28 17:17:26 -0400 | [diff] [blame] | 1307 | self.worker.hold_jobs_in_build = True |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1308 | |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1309 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1310 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1311 | A.addApproval('CRVW', 2) |
| 1312 | B.addApproval('CRVW', 2) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1313 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1314 | self.worker.addFailTest('project-test1', A) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1315 | |
James E. Blair | e281901 | 2013-06-28 17:17:26 -0400 | [diff] [blame] | 1316 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1317 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1318 | self.waitUntilSettled() |
James E. Blair | e281901 | 2013-06-28 17:17:26 -0400 | [diff] [blame] | 1319 | |
| 1320 | self.worker.release('.*-merge') |
| 1321 | self.waitUntilSettled() |
| 1322 | |
| 1323 | self.worker.hold_jobs_in_build = False |
| 1324 | self.worker.release() |
| 1325 | |
| 1326 | self.waitUntilSettled() |
| 1327 | # It's certain that the merge job for change 2 will run, but |
| 1328 | # the test1 and test2 jobs may or may not run. |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1329 | self.assertTrue(len(self.history) > 6) |
| 1330 | self.assertEqual(A.data['status'], 'NEW') |
| 1331 | self.assertEqual(B.data['status'], 'MERGED') |
| 1332 | self.assertEqual(A.reported, 2) |
| 1333 | self.assertEqual(B.reported, 2) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1334 | |
| 1335 | def test_independent_queues(self): |
| 1336 | "Test that changes end up in the right queues" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1337 | |
| 1338 | self.worker.hold_jobs_in_build = True |
Zhongyue Luo | 5d55607 | 2012-09-21 02:00:47 +0900 | [diff] [blame] | 1339 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1340 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 1341 | C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1342 | A.addApproval('CRVW', 2) |
| 1343 | B.addApproval('CRVW', 2) |
| 1344 | C.addApproval('CRVW', 2) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1345 | |
| 1346 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1347 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1348 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1349 | |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1350 | self.waitUntilSettled() |
| 1351 | |
| 1352 | # 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] | 1353 | self.assertEqual(len(self.builds), 2) |
| 1354 | self.assertEqual(self.builds[0].name, 'project-merge') |
| 1355 | self.assertTrue(self.job_has_changes(self.builds[0], A)) |
| 1356 | self.assertEqual(self.builds[1].name, 'project1-merge') |
| 1357 | self.assertTrue(self.job_has_changes(self.builds[1], B)) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1358 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1359 | # Release the current merge builds |
| 1360 | self.worker.release('.*-merge') |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1361 | self.waitUntilSettled() |
| 1362 | # Release the merge job for project2 which is behind project1 |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1363 | self.worker.release('.*-merge') |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1364 | self.waitUntilSettled() |
| 1365 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1366 | # All the test builds should be running: |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1367 | # project1 (3) + project2 (3) + project (2) = 8 |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1368 | self.assertEqual(len(self.builds), 8) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1369 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1370 | self.worker.release() |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1371 | self.waitUntilSettled() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1372 | self.assertEqual(len(self.builds), 0) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1373 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1374 | self.assertEqual(len(self.history), 11) |
| 1375 | self.assertEqual(A.data['status'], 'MERGED') |
| 1376 | self.assertEqual(B.data['status'], 'MERGED') |
| 1377 | self.assertEqual(C.data['status'], 'MERGED') |
| 1378 | self.assertEqual(A.reported, 2) |
| 1379 | self.assertEqual(B.reported, 2) |
| 1380 | self.assertEqual(C.reported, 2) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1381 | |
| 1382 | def test_failed_change_at_head(self): |
| 1383 | "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] | 1384 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1385 | self.worker.hold_jobs_in_build = True |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1386 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1387 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1388 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1389 | A.addApproval('CRVW', 2) |
| 1390 | B.addApproval('CRVW', 2) |
| 1391 | C.addApproval('CRVW', 2) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1392 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1393 | self.worker.addFailTest('project-test1', A) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1394 | |
| 1395 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1396 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1397 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1398 | |
| 1399 | self.waitUntilSettled() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1400 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1401 | self.assertEqual(len(self.builds), 1) |
| 1402 | self.assertEqual(self.builds[0].name, 'project-merge') |
| 1403 | self.assertTrue(self.job_has_changes(self.builds[0], A)) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1404 | |
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() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1409 | self.worker.release('.*-merge') |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1410 | self.waitUntilSettled() |
| 1411 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1412 | self.assertEqual(len(self.builds), 6) |
| 1413 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 1414 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 1415 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 1416 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 1417 | self.assertEqual(self.builds[4].name, 'project-test1') |
| 1418 | self.assertEqual(self.builds[5].name, 'project-test2') |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1419 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1420 | self.release(self.builds[0]) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1421 | self.waitUntilSettled() |
| 1422 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1423 | # project-test2, project-merge for B |
| 1424 | self.assertEqual(len(self.builds), 2) |
| 1425 | self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1426 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1427 | self.worker.hold_jobs_in_build = False |
| 1428 | self.worker.release() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1429 | self.waitUntilSettled() |
| 1430 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1431 | self.assertEqual(len(self.builds), 0) |
| 1432 | self.assertEqual(len(self.history), 15) |
| 1433 | self.assertEqual(A.data['status'], 'NEW') |
| 1434 | self.assertEqual(B.data['status'], 'MERGED') |
| 1435 | self.assertEqual(C.data['status'], 'MERGED') |
| 1436 | self.assertEqual(A.reported, 2) |
| 1437 | self.assertEqual(B.reported, 2) |
| 1438 | self.assertEqual(C.reported, 2) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1439 | |
James E. Blair | 0aac487 | 2013-08-23 14:02:38 -0700 | [diff] [blame] | 1440 | def test_failed_change_in_middle(self): |
| 1441 | "Test a failed change in the middle of the queue" |
| 1442 | |
| 1443 | self.worker.hold_jobs_in_build = True |
| 1444 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1445 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1446 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1447 | A.addApproval('CRVW', 2) |
| 1448 | B.addApproval('CRVW', 2) |
| 1449 | C.addApproval('CRVW', 2) |
| 1450 | |
| 1451 | self.worker.addFailTest('project-test1', B) |
| 1452 | |
| 1453 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1454 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1455 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1456 | |
| 1457 | self.waitUntilSettled() |
| 1458 | |
| 1459 | self.worker.release('.*-merge') |
| 1460 | self.waitUntilSettled() |
| 1461 | self.worker.release('.*-merge') |
| 1462 | self.waitUntilSettled() |
| 1463 | self.worker.release('.*-merge') |
| 1464 | self.waitUntilSettled() |
| 1465 | |
| 1466 | self.assertEqual(len(self.builds), 6) |
| 1467 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 1468 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 1469 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 1470 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 1471 | self.assertEqual(self.builds[4].name, 'project-test1') |
| 1472 | self.assertEqual(self.builds[5].name, 'project-test2') |
| 1473 | |
| 1474 | self.release(self.builds[2]) |
| 1475 | self.waitUntilSettled() |
| 1476 | |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 1477 | # project-test1 and project-test2 for A |
| 1478 | # project-test2 for B |
| 1479 | # project-merge for C (without B) |
| 1480 | self.assertEqual(len(self.builds), 4) |
James E. Blair | 0aac487 | 2013-08-23 14:02:38 -0700 | [diff] [blame] | 1481 | self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2) |
| 1482 | |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 1483 | self.worker.release('.*-merge') |
| 1484 | self.waitUntilSettled() |
| 1485 | |
| 1486 | # project-test1 and project-test2 for A |
| 1487 | # project-test2 for B |
| 1488 | # project-test1 and project-test2 for C |
| 1489 | self.assertEqual(len(self.builds), 5) |
| 1490 | |
James E. Blair | 0aac487 | 2013-08-23 14:02:38 -0700 | [diff] [blame] | 1491 | items = self.sched.layout.pipelines['gate'].getAllItems() |
| 1492 | builds = items[0].current_build_set.getBuilds() |
| 1493 | self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1) |
| 1494 | self.assertEqual(self.countJobResults(builds, None), 2) |
| 1495 | builds = items[1].current_build_set.getBuilds() |
| 1496 | self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1) |
| 1497 | self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1) |
| 1498 | self.assertEqual(self.countJobResults(builds, None), 1) |
| 1499 | builds = items[2].current_build_set.getBuilds() |
| 1500 | self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1) |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 1501 | self.assertEqual(self.countJobResults(builds, None), 2) |
James E. Blair | 0aac487 | 2013-08-23 14:02:38 -0700 | [diff] [blame] | 1502 | |
| 1503 | self.worker.hold_jobs_in_build = False |
| 1504 | self.worker.release() |
| 1505 | self.waitUntilSettled() |
| 1506 | |
| 1507 | self.assertEqual(len(self.builds), 0) |
| 1508 | self.assertEqual(len(self.history), 12) |
| 1509 | self.assertEqual(A.data['status'], 'MERGED') |
| 1510 | self.assertEqual(B.data['status'], 'NEW') |
| 1511 | self.assertEqual(C.data['status'], 'MERGED') |
| 1512 | self.assertEqual(A.reported, 2) |
| 1513 | self.assertEqual(B.reported, 2) |
| 1514 | self.assertEqual(C.reported, 2) |
| 1515 | |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1516 | def test_failed_change_at_head_with_queue(self): |
| 1517 | "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] | 1518 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1519 | self.gearman_server.hold_jobs_in_queue = True |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1520 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1521 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1522 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1523 | A.addApproval('CRVW', 2) |
| 1524 | B.addApproval('CRVW', 2) |
| 1525 | C.addApproval('CRVW', 2) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1526 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1527 | self.worker.addFailTest('project-test1', A) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1528 | |
| 1529 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1530 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1531 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1532 | |
| 1533 | self.waitUntilSettled() |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 1534 | queue = self.gearman_server.getQueue() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1535 | self.assertEqual(len(self.builds), 0) |
| 1536 | self.assertEqual(len(queue), 1) |
| 1537 | self.assertEqual(queue[0].name, 'build:project-merge') |
| 1538 | self.assertTrue(self.job_has_changes(queue[0], A)) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1539 | |
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 | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1544 | self.gearman_server.release('.*-merge') |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1545 | self.waitUntilSettled() |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 1546 | queue = self.gearman_server.getQueue() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1547 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1548 | self.assertEqual(len(self.builds), 0) |
| 1549 | self.assertEqual(len(queue), 6) |
| 1550 | self.assertEqual(queue[0].name, 'build:project-test1') |
| 1551 | self.assertEqual(queue[1].name, 'build:project-test2') |
| 1552 | self.assertEqual(queue[2].name, 'build:project-test1') |
| 1553 | self.assertEqual(queue[3].name, 'build:project-test2') |
| 1554 | self.assertEqual(queue[4].name, 'build:project-test1') |
| 1555 | self.assertEqual(queue[5].name, 'build:project-test2') |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1556 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1557 | self.release(queue[0]) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1558 | self.waitUntilSettled() |
| 1559 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1560 | self.assertEqual(len(self.builds), 0) |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 1561 | queue = self.gearman_server.getQueue() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1562 | self.assertEqual(len(queue), 2) # project-test2, project-merge for B |
| 1563 | self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1564 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1565 | self.gearman_server.hold_jobs_in_queue = False |
| 1566 | self.gearman_server.release() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1567 | self.waitUntilSettled() |
| 1568 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1569 | self.assertEqual(len(self.builds), 0) |
| 1570 | self.assertEqual(len(self.history), 11) |
| 1571 | self.assertEqual(A.data['status'], 'NEW') |
| 1572 | self.assertEqual(B.data['status'], 'MERGED') |
| 1573 | self.assertEqual(C.data['status'], 'MERGED') |
| 1574 | self.assertEqual(A.reported, 2) |
| 1575 | self.assertEqual(B.reported, 2) |
| 1576 | self.assertEqual(C.reported, 2) |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1577 | |
James E. Blair | fef7163 | 2013-09-23 11:15:47 -0700 | [diff] [blame] | 1578 | def test_two_failed_changes_at_head(self): |
| 1579 | "Test that changes are reparented correctly if 2 fail at head" |
| 1580 | |
| 1581 | self.worker.hold_jobs_in_build = True |
| 1582 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1583 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1584 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1585 | A.addApproval('CRVW', 2) |
| 1586 | B.addApproval('CRVW', 2) |
| 1587 | C.addApproval('CRVW', 2) |
| 1588 | |
| 1589 | self.worker.addFailTest('project-test1', A) |
| 1590 | self.worker.addFailTest('project-test1', B) |
| 1591 | |
| 1592 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1593 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1594 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1595 | self.waitUntilSettled() |
| 1596 | |
| 1597 | self.worker.release('.*-merge') |
| 1598 | self.waitUntilSettled() |
| 1599 | self.worker.release('.*-merge') |
| 1600 | self.waitUntilSettled() |
| 1601 | self.worker.release('.*-merge') |
| 1602 | self.waitUntilSettled() |
| 1603 | |
| 1604 | self.assertEqual(len(self.builds), 6) |
| 1605 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 1606 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 1607 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 1608 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 1609 | self.assertEqual(self.builds[4].name, 'project-test1') |
| 1610 | self.assertEqual(self.builds[5].name, 'project-test2') |
| 1611 | |
| 1612 | self.assertTrue(self.job_has_changes(self.builds[0], A)) |
| 1613 | self.assertTrue(self.job_has_changes(self.builds[2], A)) |
| 1614 | self.assertTrue(self.job_has_changes(self.builds[2], B)) |
| 1615 | self.assertTrue(self.job_has_changes(self.builds[4], A)) |
| 1616 | self.assertTrue(self.job_has_changes(self.builds[4], B)) |
| 1617 | self.assertTrue(self.job_has_changes(self.builds[4], C)) |
| 1618 | |
| 1619 | # Fail change B first |
| 1620 | self.release(self.builds[2]) |
| 1621 | self.waitUntilSettled() |
| 1622 | |
| 1623 | # restart of C after B failure |
| 1624 | self.worker.release('.*-merge') |
| 1625 | self.waitUntilSettled() |
| 1626 | |
| 1627 | self.assertEqual(len(self.builds), 5) |
| 1628 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 1629 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 1630 | self.assertEqual(self.builds[2].name, 'project-test2') |
| 1631 | self.assertEqual(self.builds[3].name, 'project-test1') |
| 1632 | self.assertEqual(self.builds[4].name, 'project-test2') |
| 1633 | |
| 1634 | self.assertTrue(self.job_has_changes(self.builds[1], A)) |
| 1635 | self.assertTrue(self.job_has_changes(self.builds[2], A)) |
| 1636 | self.assertTrue(self.job_has_changes(self.builds[2], B)) |
| 1637 | self.assertTrue(self.job_has_changes(self.builds[4], A)) |
| 1638 | self.assertFalse(self.job_has_changes(self.builds[4], B)) |
| 1639 | self.assertTrue(self.job_has_changes(self.builds[4], C)) |
| 1640 | |
| 1641 | # Finish running all passing jobs for change A |
| 1642 | self.release(self.builds[1]) |
| 1643 | self.waitUntilSettled() |
| 1644 | # Fail and report change A |
| 1645 | self.release(self.builds[0]) |
| 1646 | self.waitUntilSettled() |
| 1647 | |
| 1648 | # restart of B,C after A failure |
| 1649 | self.worker.release('.*-merge') |
| 1650 | self.waitUntilSettled() |
| 1651 | self.worker.release('.*-merge') |
| 1652 | self.waitUntilSettled() |
| 1653 | |
| 1654 | self.assertEqual(len(self.builds), 4) |
| 1655 | self.assertEqual(self.builds[0].name, 'project-test1') # B |
| 1656 | self.assertEqual(self.builds[1].name, 'project-test2') # B |
| 1657 | self.assertEqual(self.builds[2].name, 'project-test1') # C |
| 1658 | self.assertEqual(self.builds[3].name, 'project-test2') # C |
| 1659 | |
| 1660 | self.assertFalse(self.job_has_changes(self.builds[1], A)) |
| 1661 | self.assertTrue(self.job_has_changes(self.builds[1], B)) |
| 1662 | self.assertFalse(self.job_has_changes(self.builds[1], C)) |
| 1663 | |
| 1664 | self.assertFalse(self.job_has_changes(self.builds[2], A)) |
| 1665 | # After A failed and B and C restarted, B should be back in |
| 1666 | # C's tests because it has not failed yet. |
| 1667 | self.assertTrue(self.job_has_changes(self.builds[2], B)) |
| 1668 | self.assertTrue(self.job_has_changes(self.builds[2], C)) |
| 1669 | |
| 1670 | self.worker.hold_jobs_in_build = False |
| 1671 | self.worker.release() |
| 1672 | self.waitUntilSettled() |
| 1673 | |
| 1674 | self.assertEqual(len(self.builds), 0) |
| 1675 | self.assertEqual(len(self.history), 21) |
| 1676 | self.assertEqual(A.data['status'], 'NEW') |
| 1677 | self.assertEqual(B.data['status'], 'NEW') |
| 1678 | self.assertEqual(C.data['status'], 'MERGED') |
| 1679 | self.assertEqual(A.reported, 2) |
| 1680 | self.assertEqual(B.reported, 2) |
| 1681 | self.assertEqual(C.reported, 2) |
| 1682 | |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1683 | def test_patch_order(self): |
| 1684 | "Test that dependent patches are tested in the right order" |
| 1685 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1686 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1687 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1688 | A.addApproval('CRVW', 2) |
| 1689 | B.addApproval('CRVW', 2) |
| 1690 | C.addApproval('CRVW', 2) |
| 1691 | |
| 1692 | M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2') |
| 1693 | M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1') |
| 1694 | M2.setMerged() |
| 1695 | M1.setMerged() |
| 1696 | |
| 1697 | # C -> B -> A -> M1 -> M2 |
| 1698 | # M2 is here to make sure it is never queried. If it is, it |
| 1699 | # means zuul is walking down the entire history of merged |
| 1700 | # changes. |
| 1701 | |
| 1702 | C.setDependsOn(B, 1) |
| 1703 | B.setDependsOn(A, 1) |
| 1704 | A.setDependsOn(M1, 1) |
| 1705 | M1.setDependsOn(M2, 1) |
| 1706 | |
| 1707 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1708 | |
| 1709 | self.waitUntilSettled() |
| 1710 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1711 | self.assertEqual(A.data['status'], 'NEW') |
| 1712 | self.assertEqual(B.data['status'], 'NEW') |
| 1713 | self.assertEqual(C.data['status'], 'NEW') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1714 | |
| 1715 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1716 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1717 | |
| 1718 | self.waitUntilSettled() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1719 | self.assertEqual(M2.queried, 0) |
| 1720 | self.assertEqual(A.data['status'], 'MERGED') |
| 1721 | self.assertEqual(B.data['status'], 'MERGED') |
| 1722 | self.assertEqual(C.data['status'], 'MERGED') |
| 1723 | self.assertEqual(A.reported, 2) |
| 1724 | self.assertEqual(B.reported, 2) |
| 1725 | self.assertEqual(C.reported, 2) |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1726 | |
James E. Blair | 0e933c5 | 2013-07-11 10:18:52 -0700 | [diff] [blame] | 1727 | def test_trigger_cache(self): |
| 1728 | "Test that the trigger cache operates correctly" |
| 1729 | self.worker.hold_jobs_in_build = True |
| 1730 | |
| 1731 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1732 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1733 | X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X') |
| 1734 | A.addApproval('CRVW', 2) |
| 1735 | B.addApproval('CRVW', 2) |
| 1736 | |
| 1737 | M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1') |
| 1738 | M1.setMerged() |
| 1739 | |
| 1740 | B.setDependsOn(A, 1) |
| 1741 | A.setDependsOn(M1, 1) |
| 1742 | |
| 1743 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1744 | self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1)) |
| 1745 | |
| 1746 | self.waitUntilSettled() |
| 1747 | |
| 1748 | for build in self.builds: |
| 1749 | if build.parameters['ZUUL_PIPELINE'] == 'check': |
| 1750 | build.release() |
| 1751 | self.waitUntilSettled() |
| 1752 | for build in self.builds: |
| 1753 | if build.parameters['ZUUL_PIPELINE'] == 'check': |
| 1754 | build.release() |
| 1755 | self.waitUntilSettled() |
| 1756 | |
| 1757 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1758 | self.waitUntilSettled() |
| 1759 | |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 1760 | self.log.debug("len %s " % self.gerrit._change_cache.keys()) |
James E. Blair | 0e933c5 | 2013-07-11 10:18:52 -0700 | [diff] [blame] | 1761 | # there should still be changes in the cache |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 1762 | self.assertNotEqual(len(self.gerrit._change_cache.keys()), 0) |
James E. Blair | 0e933c5 | 2013-07-11 10:18:52 -0700 | [diff] [blame] | 1763 | |
| 1764 | self.worker.hold_jobs_in_build = False |
| 1765 | self.worker.release() |
| 1766 | self.waitUntilSettled() |
| 1767 | |
| 1768 | self.assertEqual(A.data['status'], 'MERGED') |
| 1769 | self.assertEqual(B.data['status'], 'MERGED') |
| 1770 | self.assertEqual(A.queried, 2) # Initial and isMerged |
| 1771 | self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged |
| 1772 | |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1773 | def test_can_merge(self): |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1774 | "Test whether a change is ready to merge" |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1775 | # TODO: move to test_gerrit (this is a unit test!) |
| 1776 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 1777 | trigger = self.sched.layout.pipelines['gate'].trigger |
| 1778 | a = self.sched.triggers['gerrit'].getChange(1, 2) |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 1779 | mgr = self.sched.layout.pipelines['gate'].manager |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 1780 | self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds())) |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1781 | |
| 1782 | A.addApproval('CRVW', 2) |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 1783 | a = trigger.getChange(1, 2, refresh=True) |
| 1784 | self.assertFalse(trigger.canMerge(a, mgr.getSubmitAllowNeeds())) |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1785 | |
| 1786 | A.addApproval('APRV', 1) |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 1787 | a = trigger.getChange(1, 2, refresh=True) |
| 1788 | self.assertTrue(trigger.canMerge(a, mgr.getSubmitAllowNeeds())) |
| 1789 | trigger.maintainCache([]) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1790 | |
| 1791 | def test_build_configuration(self): |
| 1792 | "Test that zuul merges the right commits for testing" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1793 | |
| 1794 | self.gearman_server.hold_jobs_in_queue = True |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1795 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1796 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1797 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1798 | A.addApproval('CRVW', 2) |
| 1799 | B.addApproval('CRVW', 2) |
| 1800 | C.addApproval('CRVW', 2) |
| 1801 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1802 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1803 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1804 | self.waitUntilSettled() |
| 1805 | |
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 | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1810 | self.gearman_server.release('.*-merge') |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1811 | self.waitUntilSettled() |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 1812 | queue = self.gearman_server.getQueue() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1813 | ref = self.getParameter(queue[-1], 'ZUUL_REF') |
| 1814 | self.gearman_server.hold_jobs_in_queue = False |
| 1815 | self.gearman_server.release() |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1816 | self.waitUntilSettled() |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1817 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1818 | path = os.path.join(self.git_root, "org/project") |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1819 | repo = git.Repo(path) |
| 1820 | repo_messages = [c.message.strip() for c in repo.iter_commits(ref)] |
| 1821 | repo_messages.reverse() |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1822 | correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1'] |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1823 | self.assertEqual(repo_messages, correct_messages) |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1824 | |
| 1825 | def test_build_configuration_conflict(self): |
| 1826 | "Test that merge conflicts are handled" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1827 | |
| 1828 | self.gearman_server.hold_jobs_in_queue = True |
James E. Blair | 6736beb | 2013-07-11 15:18:15 -0700 | [diff] [blame] | 1829 | A = self.fake_gerrit.addFakeChange('org/conflict-project', |
| 1830 | 'master', 'A') |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1831 | A.addPatchset(['conflict']) |
James E. Blair | 6736beb | 2013-07-11 15:18:15 -0700 | [diff] [blame] | 1832 | B = self.fake_gerrit.addFakeChange('org/conflict-project', |
| 1833 | 'master', 'B') |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1834 | B.addPatchset(['conflict']) |
James E. Blair | 6736beb | 2013-07-11 15:18:15 -0700 | [diff] [blame] | 1835 | C = self.fake_gerrit.addFakeChange('org/conflict-project', |
| 1836 | 'master', 'C') |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1837 | A.addApproval('CRVW', 2) |
| 1838 | B.addApproval('CRVW', 2) |
| 1839 | C.addApproval('CRVW', 2) |
| 1840 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1841 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1842 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1843 | self.waitUntilSettled() |
| 1844 | |
James E. Blair | 6736beb | 2013-07-11 15:18:15 -0700 | [diff] [blame] | 1845 | self.assertEqual(A.reported, 1) |
| 1846 | self.assertEqual(B.reported, 1) |
| 1847 | self.assertEqual(C.reported, 1) |
| 1848 | |
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 | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1853 | self.gearman_server.release('.*-merge') |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1854 | self.waitUntilSettled() |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 1855 | |
| 1856 | self.assertEqual(len(self.history), 2) # A and C merge jobs |
| 1857 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1858 | self.gearman_server.hold_jobs_in_queue = False |
| 1859 | self.gearman_server.release() |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1860 | self.waitUntilSettled() |
| 1861 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1862 | self.assertEqual(A.data['status'], 'MERGED') |
| 1863 | self.assertEqual(B.data['status'], 'NEW') |
| 1864 | self.assertEqual(C.data['status'], 'MERGED') |
| 1865 | self.assertEqual(A.reported, 2) |
| 1866 | self.assertEqual(B.reported, 2) |
| 1867 | self.assertEqual(C.reported, 2) |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 1868 | self.assertEqual(len(self.history), 6) |
James E. Blair | 6736beb | 2013-07-11 15:18:15 -0700 | [diff] [blame] | 1869 | |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 1870 | def test_post(self): |
| 1871 | "Test that post jobs run" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1872 | |
Zhongyue Luo | 5d55607 | 2012-09-21 02:00:47 +0900 | [diff] [blame] | 1873 | e = { |
| 1874 | "type": "ref-updated", |
| 1875 | "submitter": { |
| 1876 | "name": "User Name", |
| 1877 | }, |
| 1878 | "refUpdate": { |
| 1879 | "oldRev": "90f173846e3af9154517b88543ffbd1691f31366", |
| 1880 | "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543", |
| 1881 | "refName": "master", |
| 1882 | "project": "org/project", |
| 1883 | } |
| 1884 | } |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 1885 | self.fake_gerrit.addEvent(e) |
| 1886 | self.waitUntilSettled() |
| 1887 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 1888 | job_names = [x.name for x in self.history] |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1889 | self.assertEqual(len(self.history), 1) |
| 1890 | self.assertIn('project-post', job_names) |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1891 | |
| 1892 | def test_build_configuration_branch(self): |
| 1893 | "Test that the right commits are on alternate branches" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1894 | |
| 1895 | self.gearman_server.hold_jobs_in_queue = True |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1896 | A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A') |
| 1897 | B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B') |
| 1898 | C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C') |
| 1899 | A.addApproval('CRVW', 2) |
| 1900 | B.addApproval('CRVW', 2) |
| 1901 | C.addApproval('CRVW', 2) |
| 1902 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1903 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1904 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1905 | self.waitUntilSettled() |
| 1906 | |
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 | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1911 | self.gearman_server.release('.*-merge') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1912 | self.waitUntilSettled() |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 1913 | queue = self.gearman_server.getQueue() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1914 | ref = self.getParameter(queue[-1], 'ZUUL_REF') |
| 1915 | self.gearman_server.hold_jobs_in_queue = False |
| 1916 | self.gearman_server.release() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1917 | self.waitUntilSettled() |
| 1918 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1919 | path = os.path.join(self.git_root, "org/project") |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1920 | repo = git.Repo(path) |
| 1921 | repo_messages = [c.message.strip() for c in repo.iter_commits(ref)] |
| 1922 | repo_messages.reverse() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1923 | correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1'] |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1924 | self.assertEqual(repo_messages, correct_messages) |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1925 | |
| 1926 | def test_build_configuration_branch_interaction(self): |
| 1927 | "Test that switching between branches works" |
| 1928 | self.test_build_configuration() |
| 1929 | self.test_build_configuration_branch() |
| 1930 | # C has been merged, undo that |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1931 | path = os.path.join(self.upstream_root, "org/project") |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1932 | repo = git.Repo(path) |
| 1933 | repo.heads.master.commit = repo.commit('init') |
| 1934 | self.test_build_configuration() |
| 1935 | |
| 1936 | def test_build_configuration_multi_branch(self): |
| 1937 | "Test that dependent changes on multiple branches are merged" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1938 | |
| 1939 | self.gearman_server.hold_jobs_in_queue = True |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1940 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1941 | B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B') |
| 1942 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1943 | A.addApproval('CRVW', 2) |
| 1944 | B.addApproval('CRVW', 2) |
| 1945 | C.addApproval('CRVW', 2) |
| 1946 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1947 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1948 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1949 | self.waitUntilSettled() |
James E. Blair | bb1fe50 | 2014-03-04 10:15:06 -0800 | [diff] [blame] | 1950 | queue = self.gearman_server.getQueue() |
| 1951 | job_A = None |
| 1952 | for job in queue: |
| 1953 | if 'project-merge' in job.name: |
| 1954 | job_A = job |
| 1955 | ref_A = self.getParameter(job_A, 'ZUUL_REF') |
| 1956 | commit_A = self.getParameter(job_A, 'ZUUL_COMMIT') |
| 1957 | self.log.debug("Got Zuul ref for change A: %s" % ref_A) |
| 1958 | self.log.debug("Got Zuul commit for change A: %s" % commit_A) |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1959 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1960 | self.gearman_server.release('.*-merge') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1961 | self.waitUntilSettled() |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 1962 | queue = self.gearman_server.getQueue() |
James E. Blair | d320d7e | 2013-07-30 16:36:20 -0700 | [diff] [blame] | 1963 | job_B = None |
| 1964 | for job in queue: |
| 1965 | if 'project-merge' in job.name: |
| 1966 | job_B = job |
| 1967 | ref_B = self.getParameter(job_B, 'ZUUL_REF') |
James E. Blair | bb1fe50 | 2014-03-04 10:15:06 -0800 | [diff] [blame] | 1968 | commit_B = self.getParameter(job_B, 'ZUUL_COMMIT') |
James E. Blair | f750aa0 | 2013-07-15 14:11:24 -0700 | [diff] [blame] | 1969 | self.log.debug("Got Zuul ref for change B: %s" % ref_B) |
James E. Blair | bb1fe50 | 2014-03-04 10:15:06 -0800 | [diff] [blame] | 1970 | self.log.debug("Got Zuul commit for change B: %s" % commit_B) |
| 1971 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1972 | self.gearman_server.release('.*-merge') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1973 | self.waitUntilSettled() |
James E. Blair | 701c5b4 | 2013-06-06 09:34:59 -0700 | [diff] [blame] | 1974 | queue = self.gearman_server.getQueue() |
James E. Blair | d320d7e | 2013-07-30 16:36:20 -0700 | [diff] [blame] | 1975 | for job in queue: |
| 1976 | if 'project-merge' in job.name: |
| 1977 | job_C = job |
| 1978 | ref_C = self.getParameter(job_C, 'ZUUL_REF') |
James E. Blair | bb1fe50 | 2014-03-04 10:15:06 -0800 | [diff] [blame] | 1979 | commit_C = self.getParameter(job_C, 'ZUUL_COMMIT') |
James E. Blair | f750aa0 | 2013-07-15 14:11:24 -0700 | [diff] [blame] | 1980 | self.log.debug("Got Zuul ref for change C: %s" % ref_C) |
James E. Blair | bb1fe50 | 2014-03-04 10:15:06 -0800 | [diff] [blame] | 1981 | self.log.debug("Got Zuul commit for change C: %s" % commit_C) |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1982 | self.gearman_server.hold_jobs_in_queue = False |
| 1983 | self.gearman_server.release() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1984 | self.waitUntilSettled() |
| 1985 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 1986 | path = os.path.join(self.git_root, "org/project") |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1987 | repo = git.Repo(path) |
| 1988 | |
| 1989 | repo_messages = [c.message.strip() |
James E. Blair | f750aa0 | 2013-07-15 14:11:24 -0700 | [diff] [blame] | 1990 | for c in repo.iter_commits(ref_C)] |
James E. Blair | bb1fe50 | 2014-03-04 10:15:06 -0800 | [diff] [blame] | 1991 | repo_shas = [c.hexsha for c in repo.iter_commits(ref_C)] |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1992 | repo_messages.reverse() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1993 | correct_messages = ['initial commit', 'A-1', 'C-1'] |
James E. Blair | bb1fe50 | 2014-03-04 10:15:06 -0800 | [diff] [blame] | 1994 | # Ensure the right commits are in the history for this ref |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 1995 | self.assertEqual(repo_messages, correct_messages) |
James E. Blair | bb1fe50 | 2014-03-04 10:15:06 -0800 | [diff] [blame] | 1996 | # Ensure ZUUL_REF -> ZUUL_COMMIT |
| 1997 | self.assertEqual(repo_shas[0], commit_C) |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1998 | |
| 1999 | repo_messages = [c.message.strip() |
James E. Blair | f750aa0 | 2013-07-15 14:11:24 -0700 | [diff] [blame] | 2000 | for c in repo.iter_commits(ref_B)] |
James E. Blair | bb1fe50 | 2014-03-04 10:15:06 -0800 | [diff] [blame] | 2001 | repo_shas = [c.hexsha for c in repo.iter_commits(ref_B)] |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 2002 | repo_messages.reverse() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 2003 | correct_messages = ['initial commit', 'mp commit', 'B-1'] |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2004 | self.assertEqual(repo_messages, correct_messages) |
James E. Blair | bb1fe50 | 2014-03-04 10:15:06 -0800 | [diff] [blame] | 2005 | self.assertEqual(repo_shas[0], commit_B) |
| 2006 | |
| 2007 | repo_messages = [c.message.strip() |
| 2008 | for c in repo.iter_commits(ref_A)] |
| 2009 | repo_shas = [c.hexsha for c in repo.iter_commits(ref_A)] |
| 2010 | repo_messages.reverse() |
| 2011 | correct_messages = ['initial commit', 'A-1'] |
| 2012 | self.assertEqual(repo_messages, correct_messages) |
| 2013 | self.assertEqual(repo_shas[0], commit_A) |
| 2014 | |
| 2015 | self.assertNotEqual(ref_A, ref_B, ref_C) |
| 2016 | self.assertNotEqual(commit_A, commit_B, commit_C) |
James E. Blair | 7f71c80 | 2012-08-22 13:04:32 -0700 | [diff] [blame] | 2017 | |
| 2018 | def test_one_job_project(self): |
| 2019 | "Test that queueing works with one job" |
| 2020 | A = self.fake_gerrit.addFakeChange('org/one-job-project', |
| 2021 | 'master', 'A') |
| 2022 | B = self.fake_gerrit.addFakeChange('org/one-job-project', |
| 2023 | 'master', 'B') |
| 2024 | A.addApproval('CRVW', 2) |
| 2025 | B.addApproval('CRVW', 2) |
| 2026 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2027 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2028 | self.waitUntilSettled() |
| 2029 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2030 | self.assertEqual(A.data['status'], 'MERGED') |
| 2031 | self.assertEqual(A.reported, 2) |
| 2032 | self.assertEqual(B.data['status'], 'MERGED') |
| 2033 | self.assertEqual(B.reported, 2) |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 2034 | |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 2035 | def test_job_from_templates_launched(self): |
| 2036 | "Test whether a job generated via a template can be launched" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2037 | |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 2038 | A = self.fake_gerrit.addFakeChange( |
| 2039 | 'org/templated-project', 'master', 'A') |
| 2040 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 2041 | self.waitUntilSettled() |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 2042 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2043 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 2044 | 'SUCCESS') |
| 2045 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 2046 | 'SUCCESS') |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 2047 | |
James E. Blair | 3e98c02 | 2013-12-16 15:25:38 -0800 | [diff] [blame] | 2048 | def test_layered_templates(self): |
| 2049 | "Test whether a job generated via a template can be launched" |
| 2050 | |
| 2051 | A = self.fake_gerrit.addFakeChange( |
| 2052 | 'org/layered-project', 'master', 'A') |
| 2053 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 2054 | self.waitUntilSettled() |
| 2055 | |
| 2056 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 2057 | 'SUCCESS') |
| 2058 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 2059 | 'SUCCESS') |
James E. Blair | aea6cf6 | 2013-12-16 15:38:12 -0800 | [diff] [blame] | 2060 | self.assertEqual(self.getJobFromHistory('layered-project-test3' |
| 2061 | ).result, 'SUCCESS') |
| 2062 | self.assertEqual(self.getJobFromHistory('layered-project-test4' |
| 2063 | ).result, 'SUCCESS') |
| 2064 | # test5 should run twice because two templates define it |
James E. Blair | 3e98c02 | 2013-12-16 15:25:38 -0800 | [diff] [blame] | 2065 | test5_count = 0 |
| 2066 | for job in self.worker.build_history: |
James E. Blair | aea6cf6 | 2013-12-16 15:38:12 -0800 | [diff] [blame] | 2067 | if job.name == 'layered-project-foo-test5': |
James E. Blair | 3e98c02 | 2013-12-16 15:25:38 -0800 | [diff] [blame] | 2068 | test5_count += 1 |
| 2069 | self.assertEqual(job.result, 'SUCCESS') |
| 2070 | self.assertEqual(test5_count, 2) |
| 2071 | self.assertEqual(self.getJobFromHistory('project-test6').result, |
| 2072 | 'SUCCESS') |
| 2073 | |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 2074 | def test_dependent_changes_dequeue(self): |
| 2075 | "Test that dependent patches are not needlessly tested" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2076 | |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 2077 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2078 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2079 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 2080 | A.addApproval('CRVW', 2) |
| 2081 | B.addApproval('CRVW', 2) |
| 2082 | C.addApproval('CRVW', 2) |
| 2083 | |
| 2084 | M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1') |
| 2085 | M1.setMerged() |
| 2086 | |
| 2087 | # C -> B -> A -> M1 |
| 2088 | |
| 2089 | C.setDependsOn(B, 1) |
| 2090 | B.setDependsOn(A, 1) |
| 2091 | A.setDependsOn(M1, 1) |
| 2092 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2093 | self.worker.addFailTest('project-merge', A) |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 2094 | |
| 2095 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2096 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2097 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2098 | |
| 2099 | self.waitUntilSettled() |
| 2100 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2101 | self.assertEqual(A.data['status'], 'NEW') |
| 2102 | self.assertEqual(A.reported, 2) |
| 2103 | self.assertEqual(B.data['status'], 'NEW') |
| 2104 | self.assertEqual(B.reported, 2) |
| 2105 | self.assertEqual(C.data['status'], 'NEW') |
| 2106 | self.assertEqual(C.reported, 2) |
| 2107 | self.assertEqual(len(self.history), 1) |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 2108 | |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 2109 | def test_failing_dependent_changes(self): |
| 2110 | "Test that failing dependent patches are taken out of stream" |
| 2111 | self.worker.hold_jobs_in_build = True |
| 2112 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2113 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2114 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 2115 | D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D') |
| 2116 | E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E') |
| 2117 | A.addApproval('CRVW', 2) |
| 2118 | B.addApproval('CRVW', 2) |
| 2119 | C.addApproval('CRVW', 2) |
| 2120 | D.addApproval('CRVW', 2) |
| 2121 | E.addApproval('CRVW', 2) |
| 2122 | |
| 2123 | # E, D -> C -> B, A |
| 2124 | |
| 2125 | D.setDependsOn(C, 1) |
| 2126 | C.setDependsOn(B, 1) |
| 2127 | |
| 2128 | self.worker.addFailTest('project-test1', B) |
| 2129 | |
| 2130 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2131 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 2132 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2133 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2134 | self.fake_gerrit.addEvent(E.addApproval('APRV', 1)) |
| 2135 | |
| 2136 | self.waitUntilSettled() |
| 2137 | self.worker.release('.*-merge') |
| 2138 | self.waitUntilSettled() |
| 2139 | self.worker.release('.*-merge') |
| 2140 | self.waitUntilSettled() |
| 2141 | self.worker.release('.*-merge') |
| 2142 | self.waitUntilSettled() |
| 2143 | self.worker.release('.*-merge') |
| 2144 | self.waitUntilSettled() |
| 2145 | self.worker.release('.*-merge') |
| 2146 | self.waitUntilSettled() |
| 2147 | |
| 2148 | self.worker.hold_jobs_in_build = False |
| 2149 | for build in self.builds: |
| 2150 | if build.parameters['ZUUL_CHANGE'] != '1': |
| 2151 | build.release() |
| 2152 | self.waitUntilSettled() |
| 2153 | |
| 2154 | self.worker.release() |
| 2155 | self.waitUntilSettled() |
| 2156 | |
| 2157 | self.assertEqual(A.data['status'], 'MERGED') |
| 2158 | self.assertEqual(A.reported, 2) |
| 2159 | self.assertEqual(B.data['status'], 'NEW') |
| 2160 | self.assertEqual(B.reported, 2) |
| 2161 | self.assertEqual(C.data['status'], 'NEW') |
| 2162 | self.assertEqual(C.reported, 2) |
| 2163 | self.assertEqual(D.data['status'], 'NEW') |
| 2164 | self.assertEqual(D.reported, 2) |
| 2165 | self.assertEqual(E.data['status'], 'MERGED') |
| 2166 | self.assertEqual(E.reported, 2) |
| 2167 | self.assertEqual(len(self.history), 18) |
| 2168 | |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 2169 | def test_head_is_dequeued_once(self): |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2170 | "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] | 2171 | # If it's dequeued more than once, we should see extra |
| 2172 | # aborted jobs. |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 2173 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2174 | self.worker.hold_jobs_in_build = True |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 2175 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 2176 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 2177 | C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C') |
| 2178 | A.addApproval('CRVW', 2) |
| 2179 | B.addApproval('CRVW', 2) |
| 2180 | C.addApproval('CRVW', 2) |
| 2181 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2182 | self.worker.addFailTest('project1-test1', A) |
| 2183 | self.worker.addFailTest('project1-test2', A) |
| 2184 | self.worker.addFailTest('project1-project2-integration', A) |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 2185 | |
| 2186 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2187 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2188 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2189 | |
| 2190 | self.waitUntilSettled() |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 2191 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2192 | self.assertEqual(len(self.builds), 1) |
| 2193 | self.assertEqual(self.builds[0].name, 'project1-merge') |
| 2194 | self.assertTrue(self.job_has_changes(self.builds[0], A)) |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 2195 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2196 | self.worker.release('.*-merge') |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 2197 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2198 | self.worker.release('.*-merge') |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 2199 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2200 | self.worker.release('.*-merge') |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 2201 | self.waitUntilSettled() |
| 2202 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2203 | self.assertEqual(len(self.builds), 9) |
| 2204 | self.assertEqual(self.builds[0].name, 'project1-test1') |
| 2205 | self.assertEqual(self.builds[1].name, 'project1-test2') |
| 2206 | self.assertEqual(self.builds[2].name, 'project1-project2-integration') |
| 2207 | self.assertEqual(self.builds[3].name, 'project1-test1') |
| 2208 | self.assertEqual(self.builds[4].name, 'project1-test2') |
| 2209 | self.assertEqual(self.builds[5].name, 'project1-project2-integration') |
| 2210 | self.assertEqual(self.builds[6].name, 'project1-test1') |
| 2211 | self.assertEqual(self.builds[7].name, 'project1-test2') |
| 2212 | self.assertEqual(self.builds[8].name, 'project1-project2-integration') |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 2213 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 2214 | self.release(self.builds[0]) |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 2215 | self.waitUntilSettled() |
| 2216 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2217 | self.assertEqual(len(self.builds), 3) # test2,integration, merge for B |
| 2218 | self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6) |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 2219 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2220 | self.worker.hold_jobs_in_build = False |
| 2221 | self.worker.release() |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 2222 | self.waitUntilSettled() |
| 2223 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2224 | self.assertEqual(len(self.builds), 0) |
| 2225 | self.assertEqual(len(self.history), 20) |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 2226 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2227 | self.assertEqual(A.data['status'], 'NEW') |
| 2228 | self.assertEqual(B.data['status'], 'MERGED') |
| 2229 | self.assertEqual(C.data['status'], 'MERGED') |
| 2230 | self.assertEqual(A.reported, 2) |
| 2231 | self.assertEqual(B.reported, 2) |
| 2232 | self.assertEqual(C.reported, 2) |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 2233 | |
| 2234 | def test_nonvoting_job(self): |
| 2235 | "Test that non-voting jobs don't vote." |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2236 | |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 2237 | A = self.fake_gerrit.addFakeChange('org/nonvoting-project', |
| 2238 | 'master', 'A') |
| 2239 | A.addApproval('CRVW', 2) |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2240 | self.worker.addFailTest('nonvoting-project-test2', A) |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 2241 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2242 | |
| 2243 | self.waitUntilSettled() |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 2244 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2245 | self.assertEqual(A.data['status'], 'MERGED') |
| 2246 | self.assertEqual(A.reported, 2) |
| 2247 | self.assertEqual( |
| 2248 | self.getJobFromHistory('nonvoting-project-merge').result, |
| 2249 | 'SUCCESS') |
| 2250 | self.assertEqual( |
| 2251 | self.getJobFromHistory('nonvoting-project-test1').result, |
| 2252 | 'SUCCESS') |
| 2253 | self.assertEqual( |
| 2254 | self.getJobFromHistory('nonvoting-project-test2').result, |
| 2255 | 'FAILURE') |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 2256 | |
| 2257 | def test_check_queue_success(self): |
| 2258 | "Test successful check queue jobs." |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2259 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 2260 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2261 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 2262 | |
| 2263 | self.waitUntilSettled() |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 2264 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2265 | self.assertEqual(A.data['status'], 'NEW') |
| 2266 | self.assertEqual(A.reported, 1) |
| 2267 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
| 2268 | 'SUCCESS') |
| 2269 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 2270 | 'SUCCESS') |
| 2271 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 2272 | 'SUCCESS') |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 2273 | |
| 2274 | def test_check_queue_failure(self): |
| 2275 | "Test failed check queue jobs." |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2276 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 2277 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2278 | self.worker.addFailTest('project-test2', A) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 2279 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 2280 | |
| 2281 | self.waitUntilSettled() |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 2282 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2283 | self.assertEqual(A.data['status'], 'NEW') |
| 2284 | self.assertEqual(A.reported, 1) |
| 2285 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
James E. Blair | 78e31b3 | 2013-07-09 09:11:34 -0700 | [diff] [blame] | 2286 | 'SUCCESS') |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2287 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 2288 | 'SUCCESS') |
| 2289 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 2290 | 'FAILURE') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 2291 | |
| 2292 | def test_dependent_behind_dequeue(self): |
| 2293 | "test that dependent changes behind dequeued changes work" |
| 2294 | # This complicated test is a reproduction of a real life bug |
| 2295 | self.sched.reconfigure(self.config) |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 2296 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2297 | self.worker.hold_jobs_in_build = True |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 2298 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 2299 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 2300 | C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C') |
| 2301 | D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D') |
| 2302 | E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E') |
| 2303 | F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F') |
| 2304 | D.setDependsOn(C, 1) |
| 2305 | E.setDependsOn(D, 1) |
| 2306 | A.addApproval('CRVW', 2) |
| 2307 | B.addApproval('CRVW', 2) |
| 2308 | C.addApproval('CRVW', 2) |
| 2309 | D.addApproval('CRVW', 2) |
| 2310 | E.addApproval('CRVW', 2) |
| 2311 | F.addApproval('CRVW', 2) |
| 2312 | |
| 2313 | A.fail_merge = True |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 2314 | |
| 2315 | # Change object re-use in the gerrit trigger is hidden if |
| 2316 | # changes are added in quick succession; waiting makes it more |
| 2317 | # like real life. |
| 2318 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2319 | self.waitUntilSettled() |
| 2320 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2321 | self.waitUntilSettled() |
| 2322 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2323 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 2324 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2325 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 2326 | self.waitUntilSettled() |
| 2327 | |
| 2328 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2329 | self.waitUntilSettled() |
| 2330 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 2331 | self.waitUntilSettled() |
| 2332 | self.fake_gerrit.addEvent(E.addApproval('APRV', 1)) |
| 2333 | self.waitUntilSettled() |
| 2334 | self.fake_gerrit.addEvent(F.addApproval('APRV', 1)) |
| 2335 | self.waitUntilSettled() |
| 2336 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2337 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 2338 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2339 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 2340 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2341 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 2342 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2343 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 2344 | self.waitUntilSettled() |
| 2345 | |
| 2346 | # all jobs running |
James E. Blair | e955e06 | 2012-10-08 09:49:03 -0700 | [diff] [blame] | 2347 | |
| 2348 | # Grab pointers to the jobs we want to release before |
| 2349 | # releasing any, because list indexes may change as |
| 2350 | # the jobs complete. |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 2351 | a, b, c = self.builds[:3] |
James E. Blair | e955e06 | 2012-10-08 09:49:03 -0700 | [diff] [blame] | 2352 | a.release() |
| 2353 | b.release() |
| 2354 | c.release() |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 2355 | self.waitUntilSettled() |
| 2356 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2357 | self.worker.hold_jobs_in_build = False |
| 2358 | self.worker.release() |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 2359 | self.waitUntilSettled() |
| 2360 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2361 | self.assertEqual(A.data['status'], 'NEW') |
| 2362 | self.assertEqual(B.data['status'], 'MERGED') |
| 2363 | self.assertEqual(C.data['status'], 'MERGED') |
| 2364 | self.assertEqual(D.data['status'], 'MERGED') |
| 2365 | self.assertEqual(E.data['status'], 'MERGED') |
| 2366 | self.assertEqual(F.data['status'], 'MERGED') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 2367 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2368 | self.assertEqual(A.reported, 2) |
| 2369 | self.assertEqual(B.reported, 2) |
| 2370 | self.assertEqual(C.reported, 2) |
| 2371 | self.assertEqual(D.reported, 2) |
| 2372 | self.assertEqual(E.reported, 2) |
| 2373 | self.assertEqual(F.reported, 2) |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 2374 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2375 | self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15) |
| 2376 | self.assertEqual(len(self.history), 44) |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 2377 | |
| 2378 | def test_merger_repack(self): |
| 2379 | "Test that the merger works after a repack" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2380 | |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 2381 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2382 | A.addApproval('CRVW', 2) |
| 2383 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2384 | self.waitUntilSettled() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2385 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
| 2386 | 'SUCCESS') |
| 2387 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 2388 | 'SUCCESS') |
| 2389 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 2390 | 'SUCCESS') |
| 2391 | self.assertEqual(A.data['status'], 'MERGED') |
| 2392 | self.assertEqual(A.reported, 2) |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 2393 | self.assertEmptyQueues() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 2394 | self.worker.build_history = [] |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 2395 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 2396 | path = os.path.join(self.git_root, "org/project") |
| 2397 | print repack_repo(path) |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 2398 | |
| 2399 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2400 | A.addApproval('CRVW', 2) |
| 2401 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2402 | self.waitUntilSettled() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2403 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
| 2404 | 'SUCCESS') |
| 2405 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 2406 | 'SUCCESS') |
| 2407 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 2408 | 'SUCCESS') |
| 2409 | self.assertEqual(A.data['status'], 'MERGED') |
| 2410 | self.assertEqual(A.reported, 2) |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 2411 | |
James E. Blair | 4886f28 | 2012-11-15 09:27:33 -0800 | [diff] [blame] | 2412 | def test_merger_repack_large_change(self): |
| 2413 | "Test that the merger works with large changes after a repack" |
| 2414 | # https://bugs.launchpad.net/zuul/+bug/1078946 |
James E. Blair | ac2c324 | 2014-01-24 13:38:51 -0800 | [diff] [blame] | 2415 | # This test assumes the repo is already cloned; make sure it is |
| 2416 | url = self.sched.triggers['gerrit'].getGitUrl( |
| 2417 | self.sched.layout.projects['org/project1']) |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 2418 | self.merge_server.merger.addProject('org/project1', url) |
James E. Blair | 4886f28 | 2012-11-15 09:27:33 -0800 | [diff] [blame] | 2419 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 2420 | A.addPatchset(large=True) |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 2421 | path = os.path.join(self.upstream_root, "org/project1") |
| 2422 | print repack_repo(path) |
| 2423 | path = os.path.join(self.git_root, "org/project1") |
| 2424 | print repack_repo(path) |
James E. Blair | 4886f28 | 2012-11-15 09:27:33 -0800 | [diff] [blame] | 2425 | |
| 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('project1-merge').result, |
| 2430 | 'SUCCESS') |
| 2431 | self.assertEqual(self.getJobFromHistory('project1-test1').result, |
| 2432 | 'SUCCESS') |
| 2433 | self.assertEqual(self.getJobFromHistory('project1-test2').result, |
| 2434 | 'SUCCESS') |
| 2435 | self.assertEqual(A.data['status'], 'MERGED') |
| 2436 | self.assertEqual(A.reported, 2) |
James E. Blair | 4886f28 | 2012-11-15 09:27:33 -0800 | [diff] [blame] | 2437 | |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 2438 | def test_nonexistent_job(self): |
| 2439 | "Test launching a job that doesn't exist" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2440 | # Set to the state immediately after a restart |
| 2441 | self.resetGearmanServer() |
| 2442 | self.launcher.negative_function_cache_ttl = 0 |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 2443 | |
| 2444 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2445 | A.addApproval('CRVW', 2) |
| 2446 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2447 | # There may be a thread about to report a lost change |
| 2448 | while A.reported < 2: |
| 2449 | self.waitUntilSettled() |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 2450 | job_names = [x.name for x in self.history] |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2451 | self.assertFalse(job_names) |
| 2452 | self.assertEqual(A.data['status'], 'NEW') |
| 2453 | self.assertEqual(A.reported, 2) |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 2454 | self.assertEmptyQueues() |
| 2455 | |
| 2456 | # Make sure things still work: |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2457 | self.registerJobs() |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 2458 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2459 | A.addApproval('CRVW', 2) |
| 2460 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2461 | self.waitUntilSettled() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2462 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
| 2463 | 'SUCCESS') |
| 2464 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 2465 | 'SUCCESS') |
| 2466 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 2467 | 'SUCCESS') |
| 2468 | self.assertEqual(A.data['status'], 'MERGED') |
| 2469 | self.assertEqual(A.reported, 2) |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 2470 | |
| 2471 | def test_single_nonexistent_post_job(self): |
| 2472 | "Test launching a single post job that doesn't exist" |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 2473 | e = { |
| 2474 | "type": "ref-updated", |
| 2475 | "submitter": { |
| 2476 | "name": "User Name", |
| 2477 | }, |
| 2478 | "refUpdate": { |
| 2479 | "oldRev": "90f173846e3af9154517b88543ffbd1691f31366", |
| 2480 | "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543", |
| 2481 | "refName": "master", |
| 2482 | "project": "org/project", |
| 2483 | } |
| 2484 | } |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2485 | # Set to the state immediately after a restart |
| 2486 | self.resetGearmanServer() |
| 2487 | self.launcher.negative_function_cache_ttl = 0 |
| 2488 | |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 2489 | self.fake_gerrit.addEvent(e) |
| 2490 | self.waitUntilSettled() |
| 2491 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2492 | self.assertEqual(len(self.history), 0) |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2493 | |
| 2494 | def test_new_patchset_dequeues_old(self): |
| 2495 | "Test that a new patchset causes the old to be dequeued" |
| 2496 | # D -> C (depends on B) -> B (depends on A) -> A -> M |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2497 | self.worker.hold_jobs_in_build = True |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2498 | M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M') |
| 2499 | M.setMerged() |
| 2500 | |
| 2501 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2502 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2503 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 2504 | D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D') |
| 2505 | A.addApproval('CRVW', 2) |
| 2506 | B.addApproval('CRVW', 2) |
| 2507 | C.addApproval('CRVW', 2) |
| 2508 | D.addApproval('CRVW', 2) |
| 2509 | |
| 2510 | C.setDependsOn(B, 1) |
| 2511 | B.setDependsOn(A, 1) |
| 2512 | A.setDependsOn(M, 1) |
| 2513 | |
| 2514 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2515 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2516 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2517 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 2518 | self.waitUntilSettled() |
| 2519 | |
| 2520 | B.addPatchset() |
| 2521 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2)) |
| 2522 | self.waitUntilSettled() |
| 2523 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2524 | self.worker.hold_jobs_in_build = False |
| 2525 | self.worker.release() |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2526 | self.waitUntilSettled() |
| 2527 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2528 | self.assertEqual(A.data['status'], 'MERGED') |
| 2529 | self.assertEqual(A.reported, 2) |
| 2530 | self.assertEqual(B.data['status'], 'NEW') |
| 2531 | self.assertEqual(B.reported, 2) |
| 2532 | self.assertEqual(C.data['status'], 'NEW') |
| 2533 | self.assertEqual(C.reported, 2) |
| 2534 | self.assertEqual(D.data['status'], 'MERGED') |
| 2535 | self.assertEqual(D.reported, 2) |
| 2536 | 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] | 2537 | |
Arx Cruz | b1b010d | 2013-10-28 19:49:59 -0200 | [diff] [blame] | 2538 | def test_zuul_url_return(self): |
| 2539 | "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] | 2540 | self.assertTrue(self.sched.config.has_option('merger', 'zuul_url')) |
Arx Cruz | b1b010d | 2013-10-28 19:49:59 -0200 | [diff] [blame] | 2541 | self.worker.hold_jobs_in_build = True |
| 2542 | |
| 2543 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2544 | A.addApproval('CRVW', 2) |
| 2545 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2546 | self.waitUntilSettled() |
| 2547 | |
| 2548 | self.assertEqual(len(self.builds), 1) |
| 2549 | for build in self.builds: |
| 2550 | self.assertTrue('ZUUL_URL' in build.parameters) |
| 2551 | |
| 2552 | self.worker.hold_jobs_in_build = False |
| 2553 | self.worker.release() |
| 2554 | self.waitUntilSettled() |
| 2555 | |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2556 | def test_new_patchset_dequeues_old_on_head(self): |
| 2557 | "Test that a new patchset causes the old to be dequeued (at head)" |
| 2558 | # D -> C (depends on B) -> B (depends on A) -> A -> M |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2559 | self.worker.hold_jobs_in_build = True |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2560 | M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M') |
| 2561 | M.setMerged() |
| 2562 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2563 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2564 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 2565 | D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D') |
| 2566 | A.addApproval('CRVW', 2) |
| 2567 | B.addApproval('CRVW', 2) |
| 2568 | C.addApproval('CRVW', 2) |
| 2569 | D.addApproval('CRVW', 2) |
| 2570 | |
| 2571 | C.setDependsOn(B, 1) |
| 2572 | B.setDependsOn(A, 1) |
| 2573 | A.setDependsOn(M, 1) |
| 2574 | |
| 2575 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2576 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2577 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2578 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 2579 | self.waitUntilSettled() |
| 2580 | |
| 2581 | A.addPatchset() |
| 2582 | self.fake_gerrit.addEvent(A.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'], 'NEW') |
| 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'], 'NEW') |
| 2594 | self.assertEqual(C.reported, 2) |
| 2595 | self.assertEqual(D.data['status'], 'MERGED') |
| 2596 | self.assertEqual(D.reported, 2) |
| 2597 | self.assertEqual(len(self.history), 7) |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2598 | |
| 2599 | def test_new_patchset_dequeues_old_without_dependents(self): |
| 2600 | "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] | 2601 | self.worker.hold_jobs_in_build = True |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2602 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2603 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2604 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 2605 | A.addApproval('CRVW', 2) |
| 2606 | B.addApproval('CRVW', 2) |
| 2607 | C.addApproval('CRVW', 2) |
| 2608 | |
| 2609 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2610 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2611 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2612 | self.waitUntilSettled() |
| 2613 | |
| 2614 | B.addPatchset() |
| 2615 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2)) |
| 2616 | self.waitUntilSettled() |
| 2617 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2618 | self.worker.hold_jobs_in_build = False |
| 2619 | self.worker.release() |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2620 | self.waitUntilSettled() |
| 2621 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2622 | self.assertEqual(A.data['status'], 'MERGED') |
| 2623 | self.assertEqual(A.reported, 2) |
| 2624 | self.assertEqual(B.data['status'], 'NEW') |
| 2625 | self.assertEqual(B.reported, 2) |
| 2626 | self.assertEqual(C.data['status'], 'MERGED') |
| 2627 | self.assertEqual(C.reported, 2) |
| 2628 | self.assertEqual(len(self.history), 9) |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2629 | |
| 2630 | def test_new_patchset_dequeues_old_independent_queue(self): |
| 2631 | "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] | 2632 | self.worker.hold_jobs_in_build = True |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2633 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2634 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2635 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 2636 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 2637 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 2638 | self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1)) |
| 2639 | self.waitUntilSettled() |
| 2640 | |
| 2641 | B.addPatchset() |
| 2642 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2)) |
| 2643 | self.waitUntilSettled() |
| 2644 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2645 | self.worker.hold_jobs_in_build = False |
| 2646 | self.worker.release() |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2647 | self.waitUntilSettled() |
| 2648 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2649 | self.assertEqual(A.data['status'], 'NEW') |
| 2650 | self.assertEqual(A.reported, 1) |
| 2651 | self.assertEqual(B.data['status'], 'NEW') |
| 2652 | self.assertEqual(B.reported, 1) |
| 2653 | self.assertEqual(C.data['status'], 'NEW') |
| 2654 | self.assertEqual(C.reported, 1) |
| 2655 | self.assertEqual(len(self.history), 10) |
| 2656 | self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2657 | |
James E. Blair | 18c6444 | 2014-03-18 10:14:45 -0700 | [diff] [blame^] | 2658 | def test_noop_job(self): |
| 2659 | "Test that the internal noop job works" |
| 2660 | A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A') |
| 2661 | A.addApproval('CRVW', 2) |
| 2662 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2663 | self.waitUntilSettled() |
| 2664 | |
| 2665 | self.assertEqual(len(self.gearman_server.getQueue()), 0) |
| 2666 | self.assertTrue(self.sched._areAllBuildsComplete()) |
| 2667 | self.assertEqual(len(self.history), 0) |
| 2668 | self.assertEqual(A.data['status'], 'MERGED') |
| 2669 | self.assertEqual(A.reported, 2) |
| 2670 | |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2671 | def test_zuul_refs(self): |
| 2672 | "Test that zuul refs exist and have the right changes" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2673 | self.worker.hold_jobs_in_build = True |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2674 | M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1') |
| 2675 | M1.setMerged() |
| 2676 | M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2') |
| 2677 | M2.setMerged() |
| 2678 | |
| 2679 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 2680 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 2681 | C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C') |
| 2682 | D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D') |
| 2683 | A.addApproval('CRVW', 2) |
| 2684 | B.addApproval('CRVW', 2) |
| 2685 | C.addApproval('CRVW', 2) |
| 2686 | D.addApproval('CRVW', 2) |
| 2687 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2688 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2689 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2690 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 2691 | |
| 2692 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2693 | self.worker.release('.*-merge') |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2694 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2695 | self.worker.release('.*-merge') |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2696 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2697 | self.worker.release('.*-merge') |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2698 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2699 | self.worker.release('.*-merge') |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2700 | self.waitUntilSettled() |
| 2701 | |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2702 | a_zref = b_zref = c_zref = d_zref = None |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 2703 | for x in self.builds: |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2704 | if x.parameters['ZUUL_CHANGE'] == '3': |
| 2705 | a_zref = x.parameters['ZUUL_REF'] |
| 2706 | if x.parameters['ZUUL_CHANGE'] == '4': |
| 2707 | b_zref = x.parameters['ZUUL_REF'] |
| 2708 | if x.parameters['ZUUL_CHANGE'] == '5': |
| 2709 | c_zref = x.parameters['ZUUL_REF'] |
| 2710 | if x.parameters['ZUUL_CHANGE'] == '6': |
| 2711 | d_zref = x.parameters['ZUUL_REF'] |
| 2712 | |
| 2713 | # There are... four... refs. |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2714 | self.assertIsNotNone(a_zref) |
| 2715 | self.assertIsNotNone(b_zref) |
| 2716 | self.assertIsNotNone(c_zref) |
| 2717 | self.assertIsNotNone(d_zref) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2718 | |
| 2719 | # And they should all be different |
| 2720 | refs = set([a_zref, b_zref, c_zref, d_zref]) |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2721 | self.assertEqual(len(refs), 4) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2722 | |
| 2723 | # 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] | 2724 | self.assertTrue(self.ref_has_change(a_zref, A)) |
| 2725 | self.assertFalse(self.ref_has_change(a_zref, B)) |
| 2726 | self.assertFalse(self.ref_has_change(a_zref, M2)) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2727 | |
| 2728 | # 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] | 2729 | self.assertTrue(self.ref_has_change(b_zref, A)) |
| 2730 | self.assertTrue(self.ref_has_change(b_zref, B)) |
| 2731 | self.assertFalse(self.ref_has_change(b_zref, M2)) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2732 | |
| 2733 | # c ref should have a and b in 1, c in 2 |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2734 | self.assertTrue(self.ref_has_change(c_zref, A)) |
| 2735 | self.assertTrue(self.ref_has_change(c_zref, B)) |
| 2736 | self.assertTrue(self.ref_has_change(c_zref, C)) |
| 2737 | self.assertFalse(self.ref_has_change(c_zref, D)) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2738 | |
| 2739 | # 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] | 2740 | self.assertTrue(self.ref_has_change(d_zref, A)) |
| 2741 | self.assertTrue(self.ref_has_change(d_zref, B)) |
| 2742 | self.assertTrue(self.ref_has_change(d_zref, C)) |
| 2743 | self.assertTrue(self.ref_has_change(d_zref, D)) |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2744 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2745 | self.worker.hold_jobs_in_build = False |
| 2746 | self.worker.release() |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2747 | self.waitUntilSettled() |
| 2748 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2749 | self.assertEqual(A.data['status'], 'MERGED') |
| 2750 | self.assertEqual(A.reported, 2) |
| 2751 | self.assertEqual(B.data['status'], 'MERGED') |
| 2752 | self.assertEqual(B.reported, 2) |
| 2753 | self.assertEqual(C.data['status'], 'MERGED') |
| 2754 | self.assertEqual(C.reported, 2) |
| 2755 | self.assertEqual(D.data['status'], 'MERGED') |
| 2756 | self.assertEqual(D.reported, 2) |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 2757 | |
James E. Blair | c053d02 | 2014-01-22 14:57:33 -0800 | [diff] [blame] | 2758 | def test_required_approval_check_and_gate(self): |
| 2759 | "Test required-approval triggers both check and gate" |
| 2760 | self.config.set('zuul', 'layout_config', |
| 2761 | 'tests/fixtures/layout-require-approval.yaml') |
| 2762 | self.sched.reconfigure(self.config) |
| 2763 | self.registerJobs() |
| 2764 | |
| 2765 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2766 | A.addApproval('CRVW', 2) |
| 2767 | # Add a too-old +1 |
| 2768 | A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60) |
| 2769 | |
| 2770 | aprv = A.addApproval('APRV', 1) |
| 2771 | self.fake_gerrit.addEvent(aprv) |
| 2772 | self.waitUntilSettled() |
| 2773 | # Should have run a check job |
| 2774 | self.assertEqual(len(self.history), 1) |
| 2775 | self.assertEqual(self.history[0].name, 'project-check') |
| 2776 | |
| 2777 | # Report the result of that check job (overrides previous vrfy) |
| 2778 | # Skynet alert: this should trigger a gate job now that |
| 2779 | # all reqs are met |
| 2780 | self.fake_gerrit.addEvent(A.addApproval('VRFY', 1)) |
| 2781 | self.waitUntilSettled() |
| 2782 | self.assertEqual(len(self.history), 2) |
| 2783 | self.assertEqual(self.history[1].name, 'project-gate') |
| 2784 | |
| 2785 | def test_required_approval_newer(self): |
| 2786 | "Test required-approval newer trigger parameter" |
| 2787 | self.config.set('zuul', 'layout_config', |
| 2788 | 'tests/fixtures/layout-require-approval.yaml') |
| 2789 | self.sched.reconfigure(self.config) |
| 2790 | self.registerJobs() |
| 2791 | |
| 2792 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2793 | A.addApproval('CRVW', 2) |
| 2794 | aprv = A.addApproval('APRV', 1) |
| 2795 | self.fake_gerrit.addEvent(aprv) |
| 2796 | self.waitUntilSettled() |
| 2797 | # No +1 from Jenkins so should not be enqueued |
| 2798 | self.assertEqual(len(self.history), 0) |
| 2799 | |
| 2800 | # Add a too-old +1, should trigger check but not gate |
| 2801 | A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60) |
| 2802 | self.fake_gerrit.addEvent(aprv) |
| 2803 | self.waitUntilSettled() |
| 2804 | self.assertEqual(len(self.history), 1) |
| 2805 | self.assertEqual(self.history[0].name, 'project-check') |
| 2806 | |
| 2807 | # Add a recent +1 |
| 2808 | self.fake_gerrit.addEvent(A.addApproval('VRFY', 1)) |
| 2809 | self.fake_gerrit.addEvent(aprv) |
| 2810 | self.waitUntilSettled() |
| 2811 | self.assertEqual(len(self.history), 2) |
| 2812 | self.assertEqual(self.history[1].name, 'project-gate') |
| 2813 | |
| 2814 | def test_required_approval_older(self): |
| 2815 | "Test required-approval older trigger parameter" |
| 2816 | self.config.set('zuul', 'layout_config', |
| 2817 | 'tests/fixtures/layout-require-approval.yaml') |
| 2818 | self.sched.reconfigure(self.config) |
| 2819 | self.registerJobs() |
| 2820 | |
| 2821 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2822 | crvw = A.addApproval('CRVW', 2) |
| 2823 | self.fake_gerrit.addEvent(crvw) |
| 2824 | self.waitUntilSettled() |
| 2825 | # No +1 from Jenkins so should not be enqueued |
| 2826 | self.assertEqual(len(self.history), 0) |
| 2827 | |
| 2828 | # Add an old +1 and trigger check with a comment |
| 2829 | A.addApproval('VRFY', 1, granted_on=time.time() - 72 * 60 * 60) |
| 2830 | self.fake_gerrit.addEvent(crvw) |
| 2831 | self.waitUntilSettled() |
| 2832 | self.assertEqual(len(self.history), 1) |
| 2833 | self.assertEqual(self.history[0].name, 'project-check') |
| 2834 | |
| 2835 | # Add a recent +1 and make sure nothing changes |
| 2836 | A.addApproval('VRFY', 1) |
| 2837 | self.fake_gerrit.addEvent(crvw) |
| 2838 | self.waitUntilSettled() |
| 2839 | self.assertEqual(len(self.history), 1) |
| 2840 | |
| 2841 | # The last thing we did was query a change then do nothing |
| 2842 | # with a pipeline, so it will be in the cache; clean it up so |
| 2843 | # it does not fail the test. |
| 2844 | for pipeline in self.sched.layout.pipelines.values(): |
| 2845 | pipeline.trigger.maintainCache([]) |
| 2846 | |
James E. Blair | 4a28a88 | 2013-08-23 15:17:33 -0700 | [diff] [blame] | 2847 | def test_rerun_on_error(self): |
| 2848 | "Test that if a worker fails to run a job, it is run again" |
| 2849 | self.worker.hold_jobs_in_build = True |
| 2850 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2851 | A.addApproval('CRVW', 2) |
| 2852 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2853 | self.waitUntilSettled() |
| 2854 | |
| 2855 | self.builds[0].run_error = True |
| 2856 | self.worker.hold_jobs_in_build = False |
| 2857 | self.worker.release() |
| 2858 | self.waitUntilSettled() |
| 2859 | self.assertEqual(self.countJobResults(self.history, 'RUN_ERROR'), 1) |
| 2860 | self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3) |
| 2861 | |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 2862 | def test_statsd(self): |
| 2863 | "Test each of the statsd methods used in the scheduler" |
| 2864 | import extras |
| 2865 | statsd = extras.try_import('statsd.statsd') |
| 2866 | statsd.incr('test-incr') |
| 2867 | statsd.timing('test-timing', 3) |
| 2868 | statsd.gauge('test-guage', 12) |
| 2869 | self.assertReportedStat('test-incr', '1|c') |
| 2870 | self.assertReportedStat('test-timing', '3|ms') |
| 2871 | self.assertReportedStat('test-guage', '12|g') |
| 2872 | |
James E. Blair | dad5225 | 2014-02-07 16:59:17 -0800 | [diff] [blame] | 2873 | def test_stuck_job_cleanup(self): |
| 2874 | "Test that pending jobs are cleaned up if removed from layout" |
James E. Blair | 18c6444 | 2014-03-18 10:14:45 -0700 | [diff] [blame^] | 2875 | # This job won't be registered at startup because it is not in |
| 2876 | # the standard layout, but we need it to already be registerd |
| 2877 | # for when we reconfigure, as that is when Zuul will attempt |
| 2878 | # to run the new job. |
| 2879 | self.worker.registerFunction('build:gate-noop') |
James E. Blair | dad5225 | 2014-02-07 16:59:17 -0800 | [diff] [blame] | 2880 | self.gearman_server.hold_jobs_in_queue = True |
| 2881 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2882 | A.addApproval('CRVW', 2) |
| 2883 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2884 | self.waitUntilSettled() |
| 2885 | self.assertEqual(len(self.gearman_server.getQueue()), 1) |
| 2886 | |
| 2887 | self.config.set('zuul', 'layout_config', |
| 2888 | 'tests/fixtures/layout-no-jobs.yaml') |
| 2889 | self.sched.reconfigure(self.config) |
| 2890 | self.waitUntilSettled() |
| 2891 | |
James E. Blair | 18c6444 | 2014-03-18 10:14:45 -0700 | [diff] [blame^] | 2892 | self.gearman_server.release('gate-noop') |
James E. Blair | dad5225 | 2014-02-07 16:59:17 -0800 | [diff] [blame] | 2893 | self.waitUntilSettled() |
| 2894 | self.assertEqual(len(self.gearman_server.getQueue()), 0) |
| 2895 | self.assertTrue(self.sched._areAllBuildsComplete()) |
| 2896 | |
| 2897 | self.assertEqual(len(self.history), 1) |
James E. Blair | 18c6444 | 2014-03-18 10:14:45 -0700 | [diff] [blame^] | 2898 | self.assertEqual(self.history[0].name, 'gate-noop') |
James E. Blair | dad5225 | 2014-02-07 16:59:17 -0800 | [diff] [blame] | 2899 | self.assertEqual(self.history[0].result, 'SUCCESS') |
| 2900 | |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 2901 | def test_file_jobs(self): |
| 2902 | "Test that file jobs run only when appropriate" |
| 2903 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2904 | A.addPatchset(['pip-requires']) |
| 2905 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2906 | A.addApproval('CRVW', 2) |
| 2907 | B.addApproval('CRVW', 2) |
| 2908 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2909 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2910 | self.waitUntilSettled() |
| 2911 | |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 2912 | testfile_jobs = [x for x in self.history |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 2913 | if x.name == 'project-testfile'] |
| 2914 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2915 | self.assertEqual(len(testfile_jobs), 1) |
| 2916 | self.assertEqual(testfile_jobs[0].changes, '1,2') |
| 2917 | self.assertEqual(A.data['status'], 'MERGED') |
| 2918 | self.assertEqual(A.reported, 2) |
| 2919 | self.assertEqual(B.data['status'], 'MERGED') |
| 2920 | self.assertEqual(B.reported, 2) |
James E. Blair | 3c5e5b5 | 2013-04-26 11:17:03 -0700 | [diff] [blame] | 2921 | |
| 2922 | def test_test_config(self): |
| 2923 | "Test that we can test the config" |
| 2924 | sched = zuul.scheduler.Scheduler() |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 2925 | sched.registerTrigger(None, 'gerrit') |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 2926 | sched.registerTrigger(None, 'timer') |
James E. Blair | 3c5e5b5 | 2013-04-26 11:17:03 -0700 | [diff] [blame] | 2927 | sched.testConfig(CONFIG.get('zuul', 'layout_config')) |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2928 | |
| 2929 | def test_build_description(self): |
| 2930 | "Test that build descriptions update" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2931 | self.worker.registerFunction('set_description:' + |
| 2932 | self.worker.worker_id) |
| 2933 | |
| 2934 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2935 | A.addApproval('CRVW', 2) |
| 2936 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2937 | self.waitUntilSettled() |
Monty Taylor | 6bef8ef | 2013-06-02 08:17:12 -0400 | [diff] [blame] | 2938 | desc = self.history[0].description |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2939 | self.log.debug("Description: %s" % desc) |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 2940 | self.assertTrue(re.search("Branch.*master", desc)) |
| 2941 | self.assertTrue(re.search("Pipeline.*gate", desc)) |
| 2942 | self.assertTrue(re.search("project-merge.*SUCCESS", desc)) |
| 2943 | self.assertTrue(re.search("project-test1.*SUCCESS", desc)) |
| 2944 | self.assertTrue(re.search("project-test2.*SUCCESS", desc)) |
| 2945 | self.assertTrue(re.search("Reported result.*SUCCESS", desc)) |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2946 | |
James E. Blair | 64ed6f2 | 2013-07-10 14:07:23 -0700 | [diff] [blame] | 2947 | def test_queue_precedence(self): |
| 2948 | "Test that queue precedence works" |
| 2949 | |
| 2950 | self.gearman_server.hold_jobs_in_queue = True |
James E. Blair | 8de58bd | 2013-07-18 16:23:33 -0700 | [diff] [blame] | 2951 | self.worker.hold_jobs_in_build = True |
James E. Blair | 64ed6f2 | 2013-07-10 14:07:23 -0700 | [diff] [blame] | 2952 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2953 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 2954 | A.addApproval('CRVW', 2) |
| 2955 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2956 | |
| 2957 | self.waitUntilSettled() |
| 2958 | self.gearman_server.hold_jobs_in_queue = False |
| 2959 | self.gearman_server.release() |
| 2960 | self.waitUntilSettled() |
| 2961 | |
James E. Blair | 8de58bd | 2013-07-18 16:23:33 -0700 | [diff] [blame] | 2962 | # Run one build at a time to ensure non-race order: |
| 2963 | for x in range(6): |
| 2964 | self.release(self.builds[0]) |
| 2965 | self.waitUntilSettled() |
| 2966 | self.worker.hold_jobs_in_build = False |
| 2967 | self.waitUntilSettled() |
| 2968 | |
James E. Blair | 64ed6f2 | 2013-07-10 14:07:23 -0700 | [diff] [blame] | 2969 | self.log.debug(self.history) |
| 2970 | self.assertEqual(self.history[0].pipeline, 'gate') |
| 2971 | self.assertEqual(self.history[1].pipeline, 'check') |
| 2972 | self.assertEqual(self.history[2].pipeline, 'gate') |
| 2973 | self.assertEqual(self.history[3].pipeline, 'gate') |
| 2974 | self.assertEqual(self.history[4].pipeline, 'check') |
| 2975 | self.assertEqual(self.history[5].pipeline, 'check') |
| 2976 | |
James E. Blair | 1843a55 | 2013-07-03 14:19:52 -0700 | [diff] [blame] | 2977 | def test_json_status(self): |
| 2978 | "Test that we can retrieve JSON status info" |
| 2979 | self.worker.hold_jobs_in_build = True |
| 2980 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2981 | A.addApproval('CRVW', 2) |
| 2982 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2983 | self.waitUntilSettled() |
| 2984 | |
| 2985 | port = self.webapp.server.socket.getsockname()[1] |
| 2986 | |
| 2987 | f = urllib.urlopen("http://localhost:%s/status.json" % port) |
| 2988 | data = f.read() |
| 2989 | |
| 2990 | self.worker.hold_jobs_in_build = False |
| 2991 | self.worker.release() |
| 2992 | self.waitUntilSettled() |
| 2993 | |
| 2994 | data = json.loads(data) |
| 2995 | status_jobs = set() |
| 2996 | for p in data['pipelines']: |
| 2997 | for q in p['change_queues']: |
Clark Boylan | af2476f | 2014-01-23 14:47:36 -0800 | [diff] [blame] | 2998 | if q['dependent']: |
| 2999 | self.assertEqual(q['window'], 20) |
| 3000 | else: |
| 3001 | self.assertEqual(q['window'], 0) |
James E. Blair | 1843a55 | 2013-07-03 14:19:52 -0700 | [diff] [blame] | 3002 | for head in q['heads']: |
| 3003 | for change in head: |
Clark Boylan | af2476f | 2014-01-23 14:47:36 -0800 | [diff] [blame] | 3004 | self.assertTrue(change['active']) |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 3005 | self.assertEqual(change['id'], '1,1') |
James E. Blair | 1843a55 | 2013-07-03 14:19:52 -0700 | [diff] [blame] | 3006 | for job in change['jobs']: |
| 3007 | status_jobs.add(job['name']) |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 3008 | self.assertIn('project-merge', status_jobs) |
| 3009 | self.assertIn('project-test1', status_jobs) |
| 3010 | self.assertIn('project-test2', status_jobs) |
James E. Blair | 1843a55 | 2013-07-03 14:19:52 -0700 | [diff] [blame] | 3011 | |
James E. Blair | c3d428e | 2013-12-03 15:06:48 -0800 | [diff] [blame] | 3012 | def test_merging_queues(self): |
| 3013 | "Test that transitively-connected change queues are merged" |
| 3014 | self.config.set('zuul', 'layout_config', |
| 3015 | 'tests/fixtures/layout-merge-queues.yaml') |
| 3016 | self.sched.reconfigure(self.config) |
| 3017 | self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1) |
| 3018 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 3019 | def test_node_label(self): |
| 3020 | "Test that a job runs on a specific node label" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 3021 | self.worker.registerFunction('build:node-project-test1:debian') |
| 3022 | |
| 3023 | A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A') |
| 3024 | A.addApproval('CRVW', 2) |
| 3025 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 3026 | self.waitUntilSettled() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 3027 | |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 3028 | self.assertIsNone(self.getJobFromHistory('node-project-merge').node) |
| 3029 | self.assertEqual(self.getJobFromHistory('node-project-test1').node, |
| 3030 | 'debian') |
| 3031 | self.assertIsNone(self.getJobFromHistory('node-project-test2').node) |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 3032 | |
| 3033 | def test_live_reconfiguration(self): |
| 3034 | "Test that live reconfiguration works" |
| 3035 | self.worker.hold_jobs_in_build = True |
| 3036 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 3037 | A.addApproval('CRVW', 2) |
| 3038 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 3039 | self.waitUntilSettled() |
| 3040 | |
| 3041 | self.sched.reconfigure(self.config) |
| 3042 | |
| 3043 | self.worker.hold_jobs_in_build = False |
| 3044 | self.worker.release() |
| 3045 | self.waitUntilSettled() |
Monty Taylor | 98f0f3e | 2013-07-06 16:02:31 -0400 | [diff] [blame] | 3046 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
| 3047 | 'SUCCESS') |
| 3048 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 3049 | 'SUCCESS') |
| 3050 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 3051 | 'SUCCESS') |
| 3052 | self.assertEqual(A.data['status'], 'MERGED') |
| 3053 | self.assertEqual(A.reported, 2) |
James E. Blair | 287c06d | 2013-07-24 10:39:30 -0700 | [diff] [blame] | 3054 | |
James E. Blair | e712d9f | 2013-07-31 11:40:11 -0700 | [diff] [blame] | 3055 | def test_live_reconfiguration_functions(self): |
| 3056 | "Test live reconfiguration with a custom function" |
| 3057 | self.worker.registerFunction('build:node-project-test1:debian') |
| 3058 | self.worker.registerFunction('build:node-project-test1:wheezy') |
| 3059 | A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A') |
| 3060 | A.addApproval('CRVW', 2) |
| 3061 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 3062 | self.waitUntilSettled() |
| 3063 | |
| 3064 | self.assertIsNone(self.getJobFromHistory('node-project-merge').node) |
| 3065 | self.assertEqual(self.getJobFromHistory('node-project-test1').node, |
| 3066 | 'debian') |
| 3067 | self.assertIsNone(self.getJobFromHistory('node-project-test2').node) |
| 3068 | |
| 3069 | self.config.set('zuul', 'layout_config', |
| 3070 | 'tests/fixtures/layout-live-' |
| 3071 | 'reconfiguration-functions.yaml') |
| 3072 | self.sched.reconfigure(self.config) |
| 3073 | self.worker.build_history = [] |
| 3074 | |
| 3075 | B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B') |
| 3076 | B.addApproval('CRVW', 2) |
| 3077 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 3078 | self.waitUntilSettled() |
| 3079 | |
| 3080 | self.assertIsNone(self.getJobFromHistory('node-project-merge').node) |
| 3081 | self.assertEqual(self.getJobFromHistory('node-project-test1').node, |
| 3082 | 'wheezy') |
| 3083 | self.assertIsNone(self.getJobFromHistory('node-project-test2').node) |
| 3084 | |
James E. Blair | 287c06d | 2013-07-24 10:39:30 -0700 | [diff] [blame] | 3085 | def test_delayed_repo_init(self): |
| 3086 | self.config.set('zuul', 'layout_config', |
| 3087 | 'tests/fixtures/layout-delayed-repo-init.yaml') |
| 3088 | self.sched.reconfigure(self.config) |
| 3089 | |
| 3090 | self.init_repo("org/new-project") |
| 3091 | A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A') |
| 3092 | |
| 3093 | A.addApproval('CRVW', 2) |
| 3094 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 3095 | self.waitUntilSettled() |
| 3096 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
| 3097 | 'SUCCESS') |
| 3098 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 3099 | 'SUCCESS') |
| 3100 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 3101 | 'SUCCESS') |
| 3102 | self.assertEqual(A.data['status'], 'MERGED') |
| 3103 | self.assertEqual(A.reported, 2) |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 3104 | |
Clark Boylan | 6dbbc48 | 2013-10-18 10:57:31 -0700 | [diff] [blame] | 3105 | def test_repo_deleted(self): |
| 3106 | self.config.set('zuul', 'layout_config', |
| 3107 | 'tests/fixtures/layout-repo-deleted.yaml') |
| 3108 | self.sched.reconfigure(self.config) |
| 3109 | |
| 3110 | self.init_repo("org/delete-project") |
| 3111 | A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A') |
| 3112 | |
| 3113 | A.addApproval('CRVW', 2) |
| 3114 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 3115 | self.waitUntilSettled() |
| 3116 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
| 3117 | 'SUCCESS') |
| 3118 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 3119 | 'SUCCESS') |
| 3120 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 3121 | 'SUCCESS') |
| 3122 | self.assertEqual(A.data['status'], 'MERGED') |
| 3123 | self.assertEqual(A.reported, 2) |
| 3124 | |
| 3125 | # Delete org/new-project zuul repo. Should be recloned. |
| 3126 | shutil.rmtree(os.path.join(self.git_root, "org/delete-project")) |
| 3127 | |
| 3128 | B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B') |
| 3129 | |
| 3130 | B.addApproval('CRVW', 2) |
| 3131 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 3132 | self.waitUntilSettled() |
| 3133 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
| 3134 | 'SUCCESS') |
| 3135 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 3136 | 'SUCCESS') |
| 3137 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 3138 | 'SUCCESS') |
| 3139 | self.assertEqual(B.data['status'], 'MERGED') |
| 3140 | self.assertEqual(B.reported, 2) |
| 3141 | |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 3142 | def test_timer(self): |
| 3143 | "Test that a periodic job is triggered" |
| 3144 | self.worker.hold_jobs_in_build = True |
| 3145 | self.config.set('zuul', 'layout_config', |
| 3146 | 'tests/fixtures/layout-timer.yaml') |
| 3147 | self.sched.reconfigure(self.config) |
| 3148 | self.registerJobs() |
| 3149 | |
| 3150 | start = time.time() |
| 3151 | failed = True |
| 3152 | while ((time.time() - start) < 30): |
| 3153 | if len(self.builds) == 2: |
| 3154 | failed = False |
| 3155 | break |
| 3156 | else: |
| 3157 | time.sleep(1) |
| 3158 | |
| 3159 | if failed: |
| 3160 | raise Exception("Expected jobs never ran") |
| 3161 | |
| 3162 | self.waitUntilSettled() |
| 3163 | port = self.webapp.server.socket.getsockname()[1] |
| 3164 | |
| 3165 | f = urllib.urlopen("http://localhost:%s/status.json" % port) |
| 3166 | data = f.read() |
| 3167 | |
| 3168 | self.worker.hold_jobs_in_build = False |
| 3169 | self.worker.release() |
| 3170 | self.waitUntilSettled() |
| 3171 | |
| 3172 | self.assertEqual(self.getJobFromHistory( |
| 3173 | 'project-bitrot-stable-old').result, 'SUCCESS') |
| 3174 | self.assertEqual(self.getJobFromHistory( |
| 3175 | 'project-bitrot-stable-older').result, 'SUCCESS') |
| 3176 | |
| 3177 | data = json.loads(data) |
| 3178 | status_jobs = set() |
| 3179 | for p in data['pipelines']: |
| 3180 | for q in p['change_queues']: |
| 3181 | for head in q['heads']: |
| 3182 | for change in head: |
Alex Gaynor | ddb9ef3 | 2013-09-16 21:04:58 -0700 | [diff] [blame] | 3183 | self.assertEqual(change['id'], None) |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 3184 | for job in change['jobs']: |
| 3185 | status_jobs.add(job['name']) |
| 3186 | self.assertIn('project-bitrot-stable-old', status_jobs) |
| 3187 | self.assertIn('project-bitrot-stable-older', status_jobs) |
Joshua Hesketh | 5fea867 | 2013-08-19 17:32:01 +1000 | [diff] [blame] | 3188 | |
| 3189 | def test_check_smtp_pool(self): |
| 3190 | self.config.set('zuul', 'layout_config', |
| 3191 | 'tests/fixtures/layout-smtp.yaml') |
| 3192 | self.sched.reconfigure(self.config) |
| 3193 | |
| 3194 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 3195 | self.waitUntilSettled() |
| 3196 | |
| 3197 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 3198 | self.waitUntilSettled() |
| 3199 | |
James E. Blair | ff80a2f | 2013-12-27 13:24:06 -0800 | [diff] [blame] | 3200 | self.assertEqual(len(self.smtp_messages), 2) |
Joshua Hesketh | 5fea867 | 2013-08-19 17:32:01 +1000 | [diff] [blame] | 3201 | |
| 3202 | # A.messages only holds what FakeGerrit places in it. Thus we |
| 3203 | # work on the knowledge of what the first message should be as |
| 3204 | # it is only configured to go to SMTP. |
| 3205 | |
| 3206 | self.assertEqual('zuul@example.com', |
James E. Blair | ff80a2f | 2013-12-27 13:24:06 -0800 | [diff] [blame] | 3207 | self.smtp_messages[0]['from_email']) |
Joshua Hesketh | 5fea867 | 2013-08-19 17:32:01 +1000 | [diff] [blame] | 3208 | self.assertEqual(['you@example.com'], |
James E. Blair | ff80a2f | 2013-12-27 13:24:06 -0800 | [diff] [blame] | 3209 | self.smtp_messages[0]['to_email']) |
Joshua Hesketh | 5fea867 | 2013-08-19 17:32:01 +1000 | [diff] [blame] | 3210 | self.assertEqual('Starting check jobs.', |
James E. Blair | ff80a2f | 2013-12-27 13:24:06 -0800 | [diff] [blame] | 3211 | self.smtp_messages[0]['body']) |
Joshua Hesketh | 5fea867 | 2013-08-19 17:32:01 +1000 | [diff] [blame] | 3212 | |
| 3213 | self.assertEqual('zuul_from@example.com', |
James E. Blair | ff80a2f | 2013-12-27 13:24:06 -0800 | [diff] [blame] | 3214 | self.smtp_messages[1]['from_email']) |
Joshua Hesketh | 5fea867 | 2013-08-19 17:32:01 +1000 | [diff] [blame] | 3215 | self.assertEqual(['alternative_me@example.com'], |
James E. Blair | ff80a2f | 2013-12-27 13:24:06 -0800 | [diff] [blame] | 3216 | self.smtp_messages[1]['to_email']) |
Joshua Hesketh | 5fea867 | 2013-08-19 17:32:01 +1000 | [diff] [blame] | 3217 | self.assertEqual(A.messages[0], |
James E. Blair | ff80a2f | 2013-12-27 13:24:06 -0800 | [diff] [blame] | 3218 | self.smtp_messages[1]['body']) |
James E. Blair | ad28e91 | 2013-11-27 10:43:22 -0800 | [diff] [blame] | 3219 | |
James E. Blair | e591020 | 2013-12-27 09:50:31 -0800 | [diff] [blame] | 3220 | def test_timer_smtp(self): |
| 3221 | "Test that a periodic job is triggered" |
| 3222 | self.config.set('zuul', 'layout_config', |
| 3223 | 'tests/fixtures/layout-timer-smtp.yaml') |
| 3224 | self.sched.reconfigure(self.config) |
| 3225 | self.registerJobs() |
| 3226 | |
| 3227 | start = time.time() |
| 3228 | failed = True |
| 3229 | while ((time.time() - start) < 30): |
| 3230 | if len(self.history) == 2: |
| 3231 | failed = False |
| 3232 | break |
| 3233 | else: |
| 3234 | time.sleep(1) |
| 3235 | |
| 3236 | if failed: |
| 3237 | raise Exception("Expected jobs never ran") |
| 3238 | |
| 3239 | self.waitUntilSettled() |
| 3240 | |
| 3241 | self.assertEqual(self.getJobFromHistory( |
| 3242 | 'project-bitrot-stable-old').result, 'SUCCESS') |
| 3243 | self.assertEqual(self.getJobFromHistory( |
| 3244 | 'project-bitrot-stable-older').result, 'SUCCESS') |
| 3245 | |
James E. Blair | ff80a2f | 2013-12-27 13:24:06 -0800 | [diff] [blame] | 3246 | self.assertEqual(len(self.smtp_messages), 1) |
James E. Blair | e591020 | 2013-12-27 09:50:31 -0800 | [diff] [blame] | 3247 | |
| 3248 | # A.messages only holds what FakeGerrit places in it. Thus we |
| 3249 | # work on the knowledge of what the first message should be as |
| 3250 | # it is only configured to go to SMTP. |
| 3251 | |
| 3252 | self.assertEqual('zuul_from@example.com', |
James E. Blair | ff80a2f | 2013-12-27 13:24:06 -0800 | [diff] [blame] | 3253 | self.smtp_messages[0]['from_email']) |
James E. Blair | e591020 | 2013-12-27 09:50:31 -0800 | [diff] [blame] | 3254 | self.assertEqual(['alternative_me@example.com'], |
James E. Blair | ff80a2f | 2013-12-27 13:24:06 -0800 | [diff] [blame] | 3255 | self.smtp_messages[0]['to_email']) |
James E. Blair | e591020 | 2013-12-27 09:50:31 -0800 | [diff] [blame] | 3256 | self.assertIn('Subject: Periodic check for org/project succeeded', |
James E. Blair | ff80a2f | 2013-12-27 13:24:06 -0800 | [diff] [blame] | 3257 | self.smtp_messages[0]['headers']) |
James E. Blair | e591020 | 2013-12-27 09:50:31 -0800 | [diff] [blame] | 3258 | |
James E. Blair | ad28e91 | 2013-11-27 10:43:22 -0800 | [diff] [blame] | 3259 | def test_client_enqueue(self): |
| 3260 | "Test that the RPC client can enqueue a change" |
| 3261 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 3262 | A.addApproval('CRVW', 2) |
| 3263 | A.addApproval('APRV', 1) |
| 3264 | |
| 3265 | client = zuul.rpcclient.RPCClient('127.0.0.1', |
| 3266 | self.gearman_server.port) |
| 3267 | r = client.enqueue(pipeline='gate', |
| 3268 | project='org/project', |
| 3269 | trigger='gerrit', |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 3270 | change='1,1') |
James E. Blair | ad28e91 | 2013-11-27 10:43:22 -0800 | [diff] [blame] | 3271 | self.waitUntilSettled() |
| 3272 | self.assertEqual(self.getJobFromHistory('project-merge').result, |
| 3273 | 'SUCCESS') |
| 3274 | self.assertEqual(self.getJobFromHistory('project-test1').result, |
| 3275 | 'SUCCESS') |
| 3276 | self.assertEqual(self.getJobFromHistory('project-test2').result, |
| 3277 | 'SUCCESS') |
| 3278 | self.assertEqual(A.data['status'], 'MERGED') |
| 3279 | self.assertEqual(A.reported, 2) |
| 3280 | self.assertEqual(r, True) |
| 3281 | |
| 3282 | def test_client_enqueue_negative(self): |
| 3283 | "Test that the RPC client returns errors" |
| 3284 | client = zuul.rpcclient.RPCClient('127.0.0.1', |
| 3285 | self.gearman_server.port) |
| 3286 | with testtools.ExpectedException(zuul.rpcclient.RPCFailure, |
| 3287 | "Invalid project"): |
| 3288 | r = client.enqueue(pipeline='gate', |
| 3289 | project='project-does-not-exist', |
| 3290 | trigger='gerrit', |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 3291 | change='1,1') |
James E. Blair | ad28e91 | 2013-11-27 10:43:22 -0800 | [diff] [blame] | 3292 | client.shutdown() |
| 3293 | self.assertEqual(r, False) |
| 3294 | |
| 3295 | with testtools.ExpectedException(zuul.rpcclient.RPCFailure, |
| 3296 | "Invalid pipeline"): |
| 3297 | r = client.enqueue(pipeline='pipeline-does-not-exist', |
| 3298 | project='org/project', |
| 3299 | trigger='gerrit', |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 3300 | change='1,1') |
James E. Blair | ad28e91 | 2013-11-27 10:43:22 -0800 | [diff] [blame] | 3301 | client.shutdown() |
| 3302 | self.assertEqual(r, False) |
| 3303 | |
| 3304 | with testtools.ExpectedException(zuul.rpcclient.RPCFailure, |
| 3305 | "Invalid trigger"): |
| 3306 | r = client.enqueue(pipeline='gate', |
| 3307 | project='org/project', |
| 3308 | trigger='trigger-does-not-exist', |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 3309 | change='1,1') |
James E. Blair | ad28e91 | 2013-11-27 10:43:22 -0800 | [diff] [blame] | 3310 | client.shutdown() |
| 3311 | self.assertEqual(r, False) |
| 3312 | |
| 3313 | with testtools.ExpectedException(zuul.rpcclient.RPCFailure, |
| 3314 | "Invalid change"): |
| 3315 | r = client.enqueue(pipeline='gate', |
| 3316 | project='org/project', |
| 3317 | trigger='gerrit', |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 3318 | change='1,1') |
James E. Blair | ad28e91 | 2013-11-27 10:43:22 -0800 | [diff] [blame] | 3319 | client.shutdown() |
| 3320 | self.assertEqual(r, False) |
| 3321 | |
| 3322 | self.waitUntilSettled() |
| 3323 | self.assertEqual(len(self.history), 0) |
| 3324 | self.assertEqual(len(self.builds), 0) |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 3325 | |
| 3326 | def test_client_promote(self): |
| 3327 | "Test that the RPC client can promote a change" |
| 3328 | self.worker.hold_jobs_in_build = True |
| 3329 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 3330 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 3331 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 3332 | A.addApproval('CRVW', 2) |
| 3333 | B.addApproval('CRVW', 2) |
| 3334 | C.addApproval('CRVW', 2) |
| 3335 | |
| 3336 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 3337 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 3338 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 3339 | |
| 3340 | self.waitUntilSettled() |
| 3341 | |
Sean Dague | f39b9ca | 2014-01-10 21:34:35 -0500 | [diff] [blame] | 3342 | items = self.sched.layout.pipelines['gate'].getAllItems() |
| 3343 | enqueue_times = {} |
| 3344 | for item in items: |
| 3345 | enqueue_times[str(item.change)] = item.enqueue_time |
| 3346 | |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 3347 | client = zuul.rpcclient.RPCClient('127.0.0.1', |
| 3348 | self.gearman_server.port) |
| 3349 | r = client.promote(pipeline='gate', |
| 3350 | change_ids=['2,1', '3,1']) |
| 3351 | |
Sean Dague | f39b9ca | 2014-01-10 21:34:35 -0500 | [diff] [blame] | 3352 | # ensure that enqueue times are durable |
| 3353 | items = self.sched.layout.pipelines['gate'].getAllItems() |
| 3354 | for item in items: |
| 3355 | self.assertEqual( |
| 3356 | enqueue_times[str(item.change)], item.enqueue_time) |
| 3357 | |
James E. Blair | 78acec9 | 2014-02-06 07:11:32 -0800 | [diff] [blame] | 3358 | self.waitUntilSettled() |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 3359 | self.worker.release('.*-merge') |
| 3360 | self.waitUntilSettled() |
| 3361 | self.worker.release('.*-merge') |
| 3362 | self.waitUntilSettled() |
| 3363 | self.worker.release('.*-merge') |
| 3364 | self.waitUntilSettled() |
| 3365 | |
| 3366 | self.assertEqual(len(self.builds), 6) |
| 3367 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 3368 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 3369 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 3370 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 3371 | self.assertEqual(self.builds[4].name, 'project-test1') |
| 3372 | self.assertEqual(self.builds[5].name, 'project-test2') |
| 3373 | |
| 3374 | self.assertTrue(self.job_has_changes(self.builds[0], B)) |
| 3375 | self.assertFalse(self.job_has_changes(self.builds[0], A)) |
| 3376 | self.assertFalse(self.job_has_changes(self.builds[0], C)) |
| 3377 | |
| 3378 | self.assertTrue(self.job_has_changes(self.builds[2], B)) |
| 3379 | self.assertTrue(self.job_has_changes(self.builds[2], C)) |
| 3380 | self.assertFalse(self.job_has_changes(self.builds[2], A)) |
| 3381 | |
| 3382 | self.assertTrue(self.job_has_changes(self.builds[4], B)) |
| 3383 | self.assertTrue(self.job_has_changes(self.builds[4], C)) |
| 3384 | self.assertTrue(self.job_has_changes(self.builds[4], A)) |
| 3385 | |
| 3386 | self.worker.release() |
| 3387 | self.waitUntilSettled() |
| 3388 | |
| 3389 | self.assertEqual(A.data['status'], 'MERGED') |
| 3390 | self.assertEqual(A.reported, 2) |
| 3391 | self.assertEqual(B.data['status'], 'MERGED') |
| 3392 | self.assertEqual(B.reported, 2) |
| 3393 | self.assertEqual(C.data['status'], 'MERGED') |
| 3394 | self.assertEqual(C.reported, 2) |
| 3395 | |
| 3396 | client.shutdown() |
| 3397 | self.assertEqual(r, True) |
| 3398 | |
| 3399 | def test_client_promote_dependent(self): |
| 3400 | "Test that the RPC client can promote a dependent change" |
| 3401 | # C (depends on B) -> B -> A ; then promote C to get: |
| 3402 | # A -> C (depends on B) -> B |
| 3403 | self.worker.hold_jobs_in_build = True |
| 3404 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 3405 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 3406 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 3407 | |
| 3408 | C.setDependsOn(B, 1) |
| 3409 | |
| 3410 | A.addApproval('CRVW', 2) |
| 3411 | B.addApproval('CRVW', 2) |
| 3412 | C.addApproval('CRVW', 2) |
| 3413 | |
| 3414 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 3415 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 3416 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 3417 | |
| 3418 | self.waitUntilSettled() |
| 3419 | |
| 3420 | client = zuul.rpcclient.RPCClient('127.0.0.1', |
| 3421 | self.gearman_server.port) |
| 3422 | r = client.promote(pipeline='gate', |
| 3423 | change_ids=['3,1']) |
| 3424 | |
James E. Blair | 78acec9 | 2014-02-06 07:11:32 -0800 | [diff] [blame] | 3425 | self.waitUntilSettled() |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 3426 | self.worker.release('.*-merge') |
| 3427 | self.waitUntilSettled() |
| 3428 | self.worker.release('.*-merge') |
| 3429 | self.waitUntilSettled() |
| 3430 | self.worker.release('.*-merge') |
| 3431 | self.waitUntilSettled() |
| 3432 | |
| 3433 | self.assertEqual(len(self.builds), 6) |
| 3434 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 3435 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 3436 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 3437 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 3438 | self.assertEqual(self.builds[4].name, 'project-test1') |
| 3439 | self.assertEqual(self.builds[5].name, 'project-test2') |
| 3440 | |
| 3441 | self.assertTrue(self.job_has_changes(self.builds[0], B)) |
| 3442 | self.assertFalse(self.job_has_changes(self.builds[0], A)) |
| 3443 | self.assertFalse(self.job_has_changes(self.builds[0], C)) |
| 3444 | |
| 3445 | self.assertTrue(self.job_has_changes(self.builds[2], B)) |
| 3446 | self.assertTrue(self.job_has_changes(self.builds[2], C)) |
| 3447 | self.assertFalse(self.job_has_changes(self.builds[2], A)) |
| 3448 | |
| 3449 | self.assertTrue(self.job_has_changes(self.builds[4], B)) |
| 3450 | self.assertTrue(self.job_has_changes(self.builds[4], C)) |
| 3451 | self.assertTrue(self.job_has_changes(self.builds[4], A)) |
| 3452 | |
| 3453 | self.worker.release() |
| 3454 | self.waitUntilSettled() |
| 3455 | |
| 3456 | self.assertEqual(A.data['status'], 'MERGED') |
| 3457 | self.assertEqual(A.reported, 2) |
| 3458 | self.assertEqual(B.data['status'], 'MERGED') |
| 3459 | self.assertEqual(B.reported, 2) |
| 3460 | self.assertEqual(C.data['status'], 'MERGED') |
| 3461 | self.assertEqual(C.reported, 2) |
| 3462 | |
| 3463 | client.shutdown() |
| 3464 | self.assertEqual(r, True) |
| 3465 | |
| 3466 | def test_client_promote_negative(self): |
| 3467 | "Test that the RPC client returns errors for promotion" |
| 3468 | self.worker.hold_jobs_in_build = True |
| 3469 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 3470 | A.addApproval('CRVW', 2) |
| 3471 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 3472 | self.waitUntilSettled() |
| 3473 | |
| 3474 | client = zuul.rpcclient.RPCClient('127.0.0.1', |
| 3475 | self.gearman_server.port) |
| 3476 | |
| 3477 | with testtools.ExpectedException(zuul.rpcclient.RPCFailure): |
| 3478 | r = client.promote(pipeline='nonexistent', |
| 3479 | change_ids=['2,1', '3,1']) |
| 3480 | client.shutdown() |
| 3481 | self.assertEqual(r, False) |
| 3482 | |
| 3483 | with testtools.ExpectedException(zuul.rpcclient.RPCFailure): |
| 3484 | r = client.promote(pipeline='gate', |
| 3485 | change_ids=['4,1']) |
| 3486 | client.shutdown() |
| 3487 | self.assertEqual(r, False) |
| 3488 | |
| 3489 | self.worker.hold_jobs_in_build = False |
| 3490 | self.worker.release() |
| 3491 | self.waitUntilSettled() |
Clark Boylan | 7603a37 | 2014-01-21 11:43:20 -0800 | [diff] [blame] | 3492 | |
| 3493 | def test_queue_rate_limiting(self): |
| 3494 | "Test that DependentPipelines are rate limited with dep across window" |
| 3495 | self.config.set('zuul', 'layout_config', |
| 3496 | 'tests/fixtures/layout-rate-limit.yaml') |
| 3497 | self.sched.reconfigure(self.config) |
| 3498 | self.worker.hold_jobs_in_build = True |
| 3499 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 3500 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 3501 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 3502 | |
| 3503 | C.setDependsOn(B, 1) |
| 3504 | self.worker.addFailTest('project-test1', A) |
| 3505 | |
| 3506 | A.addApproval('CRVW', 2) |
| 3507 | B.addApproval('CRVW', 2) |
| 3508 | C.addApproval('CRVW', 2) |
| 3509 | |
| 3510 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 3511 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 3512 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 3513 | self.waitUntilSettled() |
| 3514 | |
| 3515 | # Only A and B will have their merge jobs queued because |
| 3516 | # window is 2. |
| 3517 | self.assertEqual(len(self.builds), 2) |
| 3518 | self.assertEqual(self.builds[0].name, 'project-merge') |
| 3519 | self.assertEqual(self.builds[1].name, 'project-merge') |
| 3520 | |
| 3521 | self.worker.release('.*-merge') |
| 3522 | self.waitUntilSettled() |
| 3523 | self.worker.release('.*-merge') |
| 3524 | self.waitUntilSettled() |
| 3525 | |
| 3526 | # Only A and B will have their test jobs queued because |
| 3527 | # window is 2. |
| 3528 | self.assertEqual(len(self.builds), 4) |
| 3529 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 3530 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 3531 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 3532 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 3533 | |
| 3534 | self.worker.release('project-.*') |
| 3535 | self.waitUntilSettled() |
| 3536 | |
| 3537 | queue = self.sched.layout.pipelines['gate'].queues[0] |
| 3538 | # A failed so window is reduced by 1 to 1. |
| 3539 | self.assertEqual(queue.window, 1) |
| 3540 | self.assertEqual(queue.window_floor, 1) |
| 3541 | self.assertEqual(A.data['status'], 'NEW') |
| 3542 | |
| 3543 | # Gate is reset and only B's merge job is queued because |
| 3544 | # window shrunk to 1. |
| 3545 | self.assertEqual(len(self.builds), 1) |
| 3546 | self.assertEqual(self.builds[0].name, 'project-merge') |
| 3547 | |
| 3548 | self.worker.release('.*-merge') |
| 3549 | self.waitUntilSettled() |
| 3550 | |
| 3551 | # Only B's test jobs are queued because window is still 1. |
| 3552 | self.assertEqual(len(self.builds), 2) |
| 3553 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 3554 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 3555 | |
| 3556 | self.worker.release('project-.*') |
| 3557 | self.waitUntilSettled() |
| 3558 | |
| 3559 | # B was successfully merged so window is increased to 2. |
| 3560 | self.assertEqual(queue.window, 2) |
| 3561 | self.assertEqual(queue.window_floor, 1) |
| 3562 | self.assertEqual(B.data['status'], 'MERGED') |
| 3563 | |
| 3564 | # Only C is left and its merge job is queued. |
| 3565 | self.assertEqual(len(self.builds), 1) |
| 3566 | self.assertEqual(self.builds[0].name, 'project-merge') |
| 3567 | |
| 3568 | self.worker.release('.*-merge') |
| 3569 | self.waitUntilSettled() |
| 3570 | |
| 3571 | # After successful merge job the test jobs for C are queued. |
| 3572 | self.assertEqual(len(self.builds), 2) |
| 3573 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 3574 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 3575 | |
| 3576 | self.worker.release('project-.*') |
| 3577 | self.waitUntilSettled() |
| 3578 | |
| 3579 | # C successfully merged so window is bumped to 3. |
| 3580 | self.assertEqual(queue.window, 3) |
| 3581 | self.assertEqual(queue.window_floor, 1) |
| 3582 | self.assertEqual(C.data['status'], 'MERGED') |
| 3583 | |
| 3584 | def test_queue_rate_limiting_dependent(self): |
| 3585 | "Test that DependentPipelines are rate limited with dep in window" |
| 3586 | self.config.set('zuul', 'layout_config', |
| 3587 | 'tests/fixtures/layout-rate-limit.yaml') |
| 3588 | self.sched.reconfigure(self.config) |
| 3589 | self.worker.hold_jobs_in_build = True |
| 3590 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 3591 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 3592 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 3593 | |
| 3594 | B.setDependsOn(A, 1) |
| 3595 | |
| 3596 | self.worker.addFailTest('project-test1', A) |
| 3597 | |
| 3598 | A.addApproval('CRVW', 2) |
| 3599 | B.addApproval('CRVW', 2) |
| 3600 | C.addApproval('CRVW', 2) |
| 3601 | |
| 3602 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 3603 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 3604 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 3605 | self.waitUntilSettled() |
| 3606 | |
| 3607 | # Only A and B will have their merge jobs queued because |
| 3608 | # window is 2. |
| 3609 | self.assertEqual(len(self.builds), 2) |
| 3610 | self.assertEqual(self.builds[0].name, 'project-merge') |
| 3611 | self.assertEqual(self.builds[1].name, 'project-merge') |
| 3612 | |
| 3613 | self.worker.release('.*-merge') |
| 3614 | self.waitUntilSettled() |
| 3615 | self.worker.release('.*-merge') |
| 3616 | self.waitUntilSettled() |
| 3617 | |
| 3618 | # Only A and B will have their test jobs queued because |
| 3619 | # window is 2. |
| 3620 | self.assertEqual(len(self.builds), 4) |
| 3621 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 3622 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 3623 | self.assertEqual(self.builds[2].name, 'project-test1') |
| 3624 | self.assertEqual(self.builds[3].name, 'project-test2') |
| 3625 | |
| 3626 | self.worker.release('project-.*') |
| 3627 | self.waitUntilSettled() |
| 3628 | |
| 3629 | queue = self.sched.layout.pipelines['gate'].queues[0] |
| 3630 | # A failed so window is reduced by 1 to 1. |
| 3631 | self.assertEqual(queue.window, 1) |
| 3632 | self.assertEqual(queue.window_floor, 1) |
| 3633 | self.assertEqual(A.data['status'], 'NEW') |
| 3634 | self.assertEqual(B.data['status'], 'NEW') |
| 3635 | |
| 3636 | # Gate is reset and only C's merge job is queued because |
| 3637 | # window shrunk to 1 and A and B were dequeued. |
| 3638 | self.assertEqual(len(self.builds), 1) |
| 3639 | self.assertEqual(self.builds[0].name, 'project-merge') |
| 3640 | |
| 3641 | self.worker.release('.*-merge') |
| 3642 | self.waitUntilSettled() |
| 3643 | |
| 3644 | # Only C's test jobs are queued because window is still 1. |
| 3645 | self.assertEqual(len(self.builds), 2) |
| 3646 | self.assertEqual(self.builds[0].name, 'project-test1') |
| 3647 | self.assertEqual(self.builds[1].name, 'project-test2') |
| 3648 | |
| 3649 | self.worker.release('project-.*') |
| 3650 | self.waitUntilSettled() |
| 3651 | |
| 3652 | # C was successfully merged so window is increased to 2. |
| 3653 | self.assertEqual(queue.window, 2) |
| 3654 | self.assertEqual(queue.window_floor, 1) |
| 3655 | self.assertEqual(C.data['status'], 'MERGED') |
Joshua Hesketh | ba8776a | 2014-01-12 14:35:40 +0800 | [diff] [blame] | 3656 | |
| 3657 | def test_worker_update_metadata(self): |
| 3658 | "Test if a worker can send back metadata about itself" |
| 3659 | self.worker.hold_jobs_in_build = True |
| 3660 | |
| 3661 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 3662 | A.addApproval('CRVW', 2) |
| 3663 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 3664 | self.waitUntilSettled() |
| 3665 | |
| 3666 | self.assertEqual(len(self.launcher.builds), 1) |
| 3667 | |
| 3668 | self.log.debug('Current builds:') |
| 3669 | self.log.debug(self.launcher.builds) |
| 3670 | |
| 3671 | start = time.time() |
| 3672 | while True: |
| 3673 | if time.time() - start > 10: |
| 3674 | raise Exception("Timeout waiting for gearman server to report " |
| 3675 | + "back to the client") |
| 3676 | build = self.launcher.builds.values()[0] |
| 3677 | if build.worker.name == "My Worker": |
| 3678 | break |
| 3679 | else: |
| 3680 | time.sleep(0) |
| 3681 | |
| 3682 | self.log.debug(build) |
| 3683 | self.assertEqual("My Worker", build.worker.name) |
| 3684 | self.assertEqual("localhost", build.worker.hostname) |
| 3685 | self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips) |
| 3686 | self.assertEqual("zuul.example.org", build.worker.fqdn) |
| 3687 | self.assertEqual("FakeBuilder", build.worker.program) |
| 3688 | self.assertEqual("v1.1", build.worker.version) |
| 3689 | self.assertEqual({'something': 'else'}, build.worker.extra) |
| 3690 | |
| 3691 | self.worker.hold_jobs_in_build = False |
| 3692 | self.worker.release() |
| 3693 | self.waitUntilSettled() |
Joshua Hesketh | 3979e3e | 2014-03-04 11:21:10 +1100 | [diff] [blame] | 3694 | |
| 3695 | def test_footer_message(self): |
| 3696 | "Test a pipeline's footer message is correctly added to the report." |
| 3697 | self.config.set('zuul', 'layout_config', |
| 3698 | 'tests/fixtures/layout-footer-message.yaml') |
| 3699 | self.sched.reconfigure(self.config) |
| 3700 | self.registerJobs() |
| 3701 | |
| 3702 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 3703 | A.addApproval('CRVW', 2) |
| 3704 | self.worker.addFailTest('test1', A) |
| 3705 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 3706 | self.waitUntilSettled() |
| 3707 | |
| 3708 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 3709 | B.addApproval('CRVW', 2) |
| 3710 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 3711 | self.waitUntilSettled() |
| 3712 | |
| 3713 | self.assertEqual(2, len(self.smtp_messages)) |
| 3714 | |
| 3715 | failure_body = """\ |
| 3716 | Build failed. For information on how to proceed, see \ |
| 3717 | http://wiki.example.org/Test_Failures |
| 3718 | |
| 3719 | - test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s |
| 3720 | - test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s |
| 3721 | |
| 3722 | For CI problems and help debugging, contact ci@example.org""" |
| 3723 | |
| 3724 | success_body = """\ |
| 3725 | Build succeeded. |
| 3726 | |
| 3727 | - test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s |
| 3728 | - test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s |
| 3729 | |
| 3730 | For CI problems and help debugging, contact ci@example.org""" |
| 3731 | |
| 3732 | self.assertEqual(failure_body, self.smtp_messages[0]['body']) |
| 3733 | self.assertEqual(success_body, self.smtp_messages[1]['body']) |