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