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