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