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