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 = { |
| 518 | 'full_url': 'https://server/job/%s/%s/' % (self.name, self.number), |
| 519 | 'number': self.number, |
| 520 | 'master': self.worker.worker_id, |
| 521 | } |
| 522 | |
| 523 | self.job.sendWorkData(json.dumps(data)) |
| 524 | self.job.sendWorkStatus(0, 100) |
| 525 | |
| 526 | if self.worker.hold_jobs_in_build: |
| 527 | self._wait() |
| 528 | self.log.debug("Build %s continuing" % self.unique) |
| 529 | |
| 530 | self.worker.lock.acquire() |
| 531 | |
| 532 | result = 'SUCCESS' |
| 533 | if (('ZUUL_REF' in self.parameters) and |
| 534 | self.worker.shouldFailTest(self.name, |
| 535 | self.parameters['ZUUL_REF'])): |
| 536 | result = 'FAILURE' |
| 537 | if self.aborted: |
| 538 | result = 'ABORTED' |
| 539 | |
| 540 | data = {'result': result} |
| 541 | changes = None |
| 542 | if 'ZUUL_CHANGE_IDS' in self.parameters: |
| 543 | changes = self.parameters['ZUUL_CHANGE_IDS'] |
| 544 | |
| 545 | self.worker.build_history.append( |
| 546 | BuildHistory(name=self.name, number=self.number, |
| 547 | result=result, changes=changes, node=self.node, |
| 548 | uuid=self.unique, description=self.description) |
| 549 | ) |
| 550 | |
| 551 | self.job.sendWorkComplete(json.dumps(data)) |
| 552 | del self.worker.gearman_jobs[self.job.unique] |
| 553 | self.worker.running_builds.remove(self) |
| 554 | self.worker.lock.release() |
| 555 | |
| 556 | |
| 557 | class FakeWorker(gear.Worker): |
| 558 | def __init__(self, worker_id): |
| 559 | super(FakeWorker, self).__init__(worker_id) |
| 560 | self.gearman_jobs = {} |
| 561 | self.build_history = [] |
| 562 | self.running_builds = [] |
| 563 | self.build_counter = 0 |
| 564 | self.fail_tests = {} |
| 565 | |
| 566 | self.hold_jobs_in_build = False |
| 567 | self.lock = threading.Lock() |
| 568 | self.__work_thread = threading.Thread(target=self.work) |
| 569 | self.__work_thread.start() |
| 570 | |
| 571 | def handleJob(self, job): |
| 572 | parts = job.name.split(":") |
| 573 | cmd = parts[0] |
| 574 | name = parts[1] |
| 575 | if len(parts) > 2: |
| 576 | node = parts[2] |
| 577 | else: |
| 578 | node = None |
| 579 | if cmd == 'build': |
| 580 | self.handleBuild(job, name, node) |
| 581 | elif cmd == 'stop': |
| 582 | self.handleStop(job, name) |
| 583 | elif cmd == 'set_description': |
| 584 | self.handleSetDescription(job, name) |
| 585 | |
| 586 | def handleBuild(self, job, name, node): |
| 587 | build = FakeBuild(self, job, self.build_counter, node) |
| 588 | job.build = build |
| 589 | self.gearman_jobs[job.unique] = job |
| 590 | self.build_counter += 1 |
| 591 | |
| 592 | self.running_builds.append(build) |
| 593 | build.start() |
| 594 | |
| 595 | def handleStop(self, job, name): |
| 596 | self.log.debug("handle stop") |
| 597 | unique = job.arguments |
| 598 | for build in self.running_builds: |
| 599 | if build.unique == unique: |
| 600 | build.aborted = True |
| 601 | build.release() |
| 602 | job.sendWorkComplete() |
| 603 | return |
| 604 | job.sendWorkFail() |
| 605 | |
| 606 | def handleSetDescription(self, job, name): |
| 607 | self.log.debug("handle set description") |
| 608 | parameters = json.loads(job.arguments) |
| 609 | unique = parameters['unique_id'] |
| 610 | descr = parameters['html_description'] |
| 611 | for build in self.running_builds: |
| 612 | if build.unique == unique: |
| 613 | build.description = descr |
| 614 | job.sendWorkComplete() |
| 615 | return |
| 616 | for build in self.build_history: |
| 617 | if build.uuid == unique: |
| 618 | build.description = descr |
| 619 | job.sendWorkComplete() |
| 620 | return |
| 621 | job.sendWorkFail() |
| 622 | |
| 623 | def work(self): |
| 624 | while self.running: |
| 625 | try: |
| 626 | job = self.getJob() |
| 627 | except gear.InterruptedError: |
| 628 | continue |
| 629 | try: |
| 630 | self.handleJob(job) |
| 631 | except: |
| 632 | self.log.exception("Worker exception:") |
| 633 | |
| 634 | def addFailTest(self, name, change): |
| 635 | l = self.fail_tests.get(name, []) |
| 636 | l.append(change) |
| 637 | self.fail_tests[name] = l |
| 638 | |
| 639 | def shouldFailTest(self, name, ref): |
| 640 | l = self.fail_tests.get(name, []) |
| 641 | for change in l: |
| 642 | if ref_has_change(ref, change): |
| 643 | return True |
| 644 | return False |
| 645 | |
| 646 | def release(self, regex=None): |
| 647 | builds = self.running_builds[:] |
| 648 | self.log.debug("releasing build %s (%s)" % (regex, |
| 649 | len(self.running_builds))) |
| 650 | for build in builds: |
| 651 | if not regex or re.match(regex, build.name): |
| 652 | self.log.debug("releasing build %s" % |
| 653 | (build.parameters['ZUUL_UUID'])) |
| 654 | build.release() |
| 655 | else: |
| 656 | self.log.debug("not releasing build %s" % |
| 657 | (build.parameters['ZUUL_UUID'])) |
| 658 | self.log.debug("done releasing builds %s (%s)" % |
| 659 | (regex, len(self.running_builds))) |
| 660 | |
| 661 | |
| 662 | class FakeGearmanServer(gear.Server): |
| 663 | def __init__(self): |
| 664 | self.hold_jobs_in_queue = False |
| 665 | super(FakeGearmanServer, self).__init__(0) |
| 666 | |
| 667 | def getJobForConnection(self, connection, peek=False): |
| 668 | for job in self.queue: |
| 669 | if not hasattr(job, 'waiting'): |
| 670 | if job.name.startswith('build:'): |
| 671 | job.waiting = self.hold_jobs_in_queue |
| 672 | else: |
| 673 | job.waiting = False |
| 674 | if job.waiting: |
| 675 | continue |
| 676 | if job.name in connection.functions: |
| 677 | if not peek: |
| 678 | self.queue.remove(job) |
| 679 | return job |
| 680 | return None |
| 681 | |
| 682 | def release(self, regex=None): |
| 683 | released = False |
| 684 | queue = self.queue[:] |
| 685 | self.log.debug("releasing queued job %s (%s)" % (regex, |
| 686 | len(self.queue))) |
| 687 | for job in queue: |
| 688 | cmd, name = job.name.split(':') |
| 689 | if cmd != 'build': |
| 690 | continue |
| 691 | if not regex or re.match(regex, name): |
| 692 | self.log.debug("releasing queued job %s" % |
| 693 | job.unique) |
| 694 | job.waiting = False |
| 695 | released = True |
| 696 | else: |
| 697 | self.log.debug("not releasing queued job %s" % |
| 698 | job.unique) |
| 699 | if released: |
| 700 | self.wakeConnections() |
| 701 | self.log.debug("done releasing queued jobs %s (%s)" % |
| 702 | (regex, len(self.queue))) |
| 703 | |
| 704 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 705 | class testScheduler(unittest.TestCase): |
| 706 | log = logging.getLogger("zuul.test") |
| 707 | |
| 708 | def setUp(self): |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 709 | if os.path.exists(TEST_ROOT): |
| 710 | shutil.rmtree(TEST_ROOT) |
| 711 | os.makedirs(TEST_ROOT) |
| 712 | os.makedirs(UPSTREAM_ROOT) |
| 713 | os.makedirs(GIT_ROOT) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 714 | |
| 715 | # For each project in config: |
| 716 | init_repo("org/project") |
| 717 | init_repo("org/project1") |
| 718 | init_repo("org/project2") |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 719 | init_repo("org/project3") |
James E. Blair | 7f71c80 | 2012-08-22 13:04:32 -0700 | [diff] [blame] | 720 | init_repo("org/one-job-project") |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 721 | init_repo("org/nonvoting-project") |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 722 | init_repo("org/templated-project") |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 723 | init_repo("org/node-project") |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 724 | |
| 725 | self.statsd = FakeStatsd() |
| 726 | os.environ['STATSD_HOST'] = 'localhost' |
| 727 | os.environ['STATSD_PORT'] = str(self.statsd.port) |
| 728 | self.statsd.start() |
| 729 | # the statsd client object is configured in the statsd module import |
| 730 | reload(statsd) |
| 731 | reload(zuul.scheduler) |
| 732 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 733 | self.gearman_server = FakeGearmanServer() |
| 734 | |
| 735 | self.config = ConfigParser.ConfigParser() |
| 736 | cfg = StringIO() |
| 737 | CONFIG.write(cfg) |
| 738 | cfg.seek(0) |
| 739 | self.config.readfp(cfg) |
| 740 | self.config.set('gearman', 'port', str(self.gearman_server.port)) |
| 741 | |
| 742 | self.worker = FakeWorker('fake_worker') |
| 743 | self.worker.addServer('127.0.0.1', self.gearman_server.port) |
| 744 | self.gearman_server.worker = self.worker |
| 745 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 746 | self.sched = zuul.scheduler.Scheduler() |
| 747 | |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 748 | def URLOpenerFactory(*args, **kw): |
| 749 | args = [self.fake_gerrit] + list(args) |
| 750 | return FakeURLOpener(*args, **kw) |
| 751 | |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 752 | urllib2.urlopen = URLOpenerFactory |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 753 | self.launcher = zuul.launcher.gearman.Gearman(self.config, self.sched) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 754 | |
| 755 | zuul.lib.gerrit.Gerrit = FakeGerrit |
| 756 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 757 | self.gerrit = FakeGerritTrigger(self.config, self.sched) |
James E. Blair | 8cc15a8 | 2012-08-01 11:17:57 -0700 | [diff] [blame] | 758 | self.gerrit.replication_timeout = 1.5 |
| 759 | self.gerrit.replication_retry_interval = 0.5 |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 760 | self.fake_gerrit = self.gerrit.gerrit |
| 761 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 762 | self.sched.setLauncher(self.launcher) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 763 | self.sched.setTrigger(self.gerrit) |
| 764 | |
| 765 | self.sched.start() |
| 766 | self.sched.reconfigure(self.config) |
| 767 | self.sched.resume() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 768 | self.launcher.gearman.waitForServer() |
| 769 | self.registerJobs() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 770 | |
| 771 | def tearDown(self): |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 772 | self.launcher.stop() |
| 773 | self.worker.shutdown() |
| 774 | self.gearman_server.shutdown() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 775 | self.gerrit.stop() |
| 776 | self.sched.stop() |
| 777 | self.sched.join() |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 778 | self.statsd.stop() |
| 779 | self.statsd.join() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 780 | threads = threading.enumerate() |
| 781 | if len(threads) > 1: |
| 782 | self.log.error("More than one thread is running: %s" % threads) |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 783 | #shutil.rmtree(TEST_ROOT) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 784 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 785 | def registerJobs(self): |
| 786 | count = 0 |
| 787 | for job in self.sched.jobs.keys(): |
| 788 | self.worker.registerFunction('build:' + job) |
| 789 | count += 1 |
| 790 | self.worker.registerFunction('stop:' + self.worker.worker_id) |
| 791 | count += 1 |
| 792 | |
| 793 | while len(self.gearman_server.functions) < count: |
| 794 | time.sleep(0) |
| 795 | |
| 796 | def release(self, job): |
| 797 | if isinstance(job, FakeBuild): |
| 798 | job.release() |
| 799 | else: |
| 800 | job.waiting = False |
| 801 | self.log.debug("Queued job %s released" % job.unique) |
| 802 | self.gearman_server.wakeConnections() |
| 803 | |
| 804 | def getParameter(self, job, name): |
| 805 | if isinstance(job, FakeBuild): |
| 806 | return job.parameters[name] |
| 807 | else: |
| 808 | parameters = json.loads(job.arguments) |
| 809 | return parameters[name] |
| 810 | |
| 811 | def resetGearmanServer(self): |
| 812 | self.worker.setFunctions([]) |
| 813 | while True: |
| 814 | done = True |
| 815 | for connection in self.gearman_server.active_connections: |
| 816 | if connection.functions: |
| 817 | done = False |
| 818 | if done: |
| 819 | break |
| 820 | time.sleep(0) |
| 821 | self.gearman_server.functions = set() |
| 822 | |
| 823 | def haveAllBuildsReported(self): |
| 824 | # See if Zuul is waiting on a meta job to complete |
| 825 | if self.launcher.meta_jobs: |
| 826 | return False |
| 827 | # Find out if every build that the worker has completed has been |
| 828 | # reported back to Zuul. If it hasn't then that means a Gearman |
| 829 | # event is still in transit and the system is not stable. |
| 830 | for build in self.worker.build_history: |
| 831 | zbuild = self.launcher.builds.get(build.uuid) |
| 832 | if not zbuild: |
| 833 | # It has already been reported |
| 834 | continue |
| 835 | # It hasn't been reported yet. |
| 836 | return False |
| 837 | # Make sure that none of the worker connections are in GRAB_WAIT |
| 838 | for connection in self.worker.active_connections: |
| 839 | if connection.state == 'GRAB_WAIT': |
| 840 | return False |
| 841 | return True |
| 842 | |
| 843 | def areAllBuildsWaiting(self): |
| 844 | ret = True |
| 845 | |
| 846 | builds = self.launcher.builds.values() |
| 847 | for build in builds: |
| 848 | client_job = None |
| 849 | for conn in self.launcher.gearman.active_connections: |
| 850 | for j in conn.related_jobs.values(): |
| 851 | if j.unique == build.uuid: |
| 852 | client_job = j |
| 853 | break |
| 854 | if not client_job: |
| 855 | self.log.debug("%s is not known to the gearman client" % |
| 856 | build) |
| 857 | ret = False |
| 858 | continue |
| 859 | if not client_job.handle: |
| 860 | self.log.debug("%s has no handle" % client_job) |
| 861 | ret = False |
| 862 | continue |
| 863 | server_job = self.gearman_server.jobs.get(client_job.handle) |
| 864 | if not server_job: |
| 865 | self.log.debug("%s is not known to the gearman server" % |
| 866 | client_job) |
| 867 | ret = False |
| 868 | continue |
| 869 | if not hasattr(server_job, 'waiting'): |
| 870 | self.log.debug("%s is being enqueued" % server_job) |
| 871 | ret = False |
| 872 | continue |
| 873 | if server_job.waiting: |
| 874 | continue |
| 875 | worker_job = self.worker.gearman_jobs.get(server_job.unique) |
| 876 | if worker_job: |
| 877 | if worker_job.build.isWaiting(): |
| 878 | continue |
| 879 | else: |
| 880 | self.log.debug("%s is running" % worker_job) |
| 881 | ret = False |
| 882 | else: |
| 883 | self.log.debug("%s is unassigned" % server_job) |
| 884 | ret = False |
| 885 | return ret |
| 886 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 887 | def waitUntilSettled(self): |
| 888 | self.log.debug("Waiting until settled...") |
| 889 | start = time.time() |
| 890 | while True: |
| 891 | if time.time() - start > 10: |
| 892 | print 'queue status:', |
| 893 | print self.sched.trigger_event_queue.empty(), |
| 894 | print self.sched.result_event_queue.empty(), |
| 895 | print self.fake_gerrit.event_queue.empty(), |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 896 | print self.areAllBuildsWaiting() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 897 | raise Exception("Timeout waiting for Zuul to settle") |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 898 | # Make sure no new events show up while we're checking |
| 899 | self.worker.lock.acquire() |
| 900 | # have all build states propogated to zuul? |
| 901 | if self.haveAllBuildsReported(): |
| 902 | # Join ensures that the queue is empty _and_ events have been |
| 903 | # processed |
| 904 | self.fake_gerrit.event_queue.join() |
| 905 | self.sched.trigger_event_queue.join() |
| 906 | self.sched.result_event_queue.join() |
| 907 | if (self.sched.trigger_event_queue.empty() and |
| 908 | self.sched.result_event_queue.empty() and |
| 909 | self.fake_gerrit.event_queue.empty() and |
| 910 | self.areAllBuildsWaiting()): |
| 911 | self.worker.lock.release() |
| 912 | self.log.debug("...settled.") |
| 913 | return |
| 914 | self.worker.lock.release() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 915 | self.sched.wake_event.wait(0.1) |
| 916 | |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 917 | def countJobResults(self, jobs, result): |
James E. Blair | 0018a6c | 2013-02-27 14:11:45 -0800 | [diff] [blame] | 918 | jobs = filter(lambda x: x.result == result, jobs) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 919 | return len(jobs) |
| 920 | |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 921 | def getJobFromHistory(self, name): |
| 922 | history = self.worker.build_history |
| 923 | for job in history: |
| 924 | if job.name == name: |
| 925 | return job |
| 926 | raise Exception("Unable to find job %s in history" % name) |
| 927 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 928 | def assertEmptyQueues(self): |
| 929 | # Make sure there are no orphaned jobs |
| 930 | for pipeline in self.sched.pipelines.values(): |
| 931 | for queue in pipeline.queues: |
| 932 | if len(queue.queue) != 0: |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 933 | print 'pipeline %s queue %s contents %s' % ( |
| 934 | pipeline.name, queue.name, queue.queue) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 935 | assert len(queue.queue) == 0 |
| 936 | if len(queue.severed_heads) != 0: |
| 937 | print 'heads', queue.severed_heads |
| 938 | assert len(queue.severed_heads) == 0 |
| 939 | |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 940 | def assertReportedStat(self, key, value=None): |
| 941 | start = time.time() |
| 942 | while time.time() < (start + 5): |
| 943 | for stat in self.statsd.stats: |
| 944 | k, v = stat.split(':') |
| 945 | if key == k: |
| 946 | if value is None: |
| 947 | return |
| 948 | if value == v: |
| 949 | return |
| 950 | time.sleep(0.1) |
| 951 | |
| 952 | pprint.pprint(self.statsd.stats) |
| 953 | raise Exception("Key %s not found in reported stats" % key) |
| 954 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 955 | def test_jobs_launched(self): |
| 956 | "Test that jobs are launched and a change is merged" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 957 | builds = self.worker.running_builds |
| 958 | history = self.worker.build_history |
| 959 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 960 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 961 | A.addApproval('CRVW', 2) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 962 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 963 | self.waitUntilSettled() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 964 | assert self.getJobFromHistory('project-merge').result == 'SUCCESS' |
| 965 | assert self.getJobFromHistory('project-test1').result == 'SUCCESS' |
| 966 | assert self.getJobFromHistory('project-test2').result == 'SUCCESS' |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 967 | assert A.data['status'] == 'MERGED' |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 968 | assert A.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 969 | self.assertEmptyQueues() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 970 | |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 971 | self.assertReportedStat('gerrit.event.comment-added', '1|c') |
| 972 | self.assertReportedStat('zuul.pipeline.gate.current_changes', '1|g') |
| 973 | self.assertReportedStat('zuul.job.project-merge') |
| 974 | self.assertReportedStat('zuul.pipeline.gate.resident_time') |
| 975 | self.assertReportedStat('zuul.pipeline.gate.total_changes', '1|c') |
| 976 | self.assertReportedStat( |
| 977 | 'zuul.pipeline.gate.org.project.resident_time') |
| 978 | self.assertReportedStat( |
| 979 | 'zuul.pipeline.gate.org.project.total_changes', '1|c') |
| 980 | |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 981 | def test_duplicate_pipelines(self): |
| 982 | "Test that a change matching multiple pipelines works" |
James E. Blair | 1b4d972 | 2013-05-21 10:32:04 -0700 | [diff] [blame] | 983 | builds = self.worker.running_builds |
| 984 | history = self.worker.build_history |
| 985 | |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 986 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 987 | self.fake_gerrit.addEvent(A.getChangeRestoredEvent()) |
| 988 | self.waitUntilSettled() |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 989 | |
James E. Blair | 1b4d972 | 2013-05-21 10:32:04 -0700 | [diff] [blame] | 990 | print builds |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 991 | print A.messages |
| 992 | |
| 993 | self.assertEmptyQueues() |
| 994 | |
James E. Blair | 1b4d972 | 2013-05-21 10:32:04 -0700 | [diff] [blame] | 995 | assert len(history) == 2 |
| 996 | history[0].name == 'project-test1' |
| 997 | history[1].name == 'project-test1' |
James E. Blair | 42f7482 | 2013-05-14 15:18:03 -0700 | [diff] [blame] | 998 | |
| 999 | assert len(A.messages) == 2 |
| 1000 | if 'dup1/project-test1' in A.messages[0]: |
| 1001 | assert 'dup1/project-test1' in A.messages[0] |
| 1002 | assert 'dup2/project-test1' not in A.messages[0] |
| 1003 | assert 'dup1/project-test1' not in A.messages[1] |
| 1004 | assert 'dup2/project-test1' in A.messages[1] |
| 1005 | else: |
| 1006 | assert 'dup1/project-test1' in A.messages[1] |
| 1007 | assert 'dup2/project-test1' not in A.messages[1] |
| 1008 | assert 'dup1/project-test1' not in A.messages[0] |
| 1009 | assert 'dup2/project-test1' in A.messages[0] |
| 1010 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1011 | def test_parallel_changes(self): |
| 1012 | "Test that changes are tested in parallel and merged in series" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1013 | builds = self.worker.running_builds |
| 1014 | history = self.worker.build_history |
| 1015 | |
| 1016 | self.worker.hold_jobs_in_build = True |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1017 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1018 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1019 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1020 | A.addApproval('CRVW', 2) |
| 1021 | B.addApproval('CRVW', 2) |
| 1022 | C.addApproval('CRVW', 2) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1023 | |
| 1024 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1025 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1026 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1027 | |
| 1028 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1029 | assert len(builds) == 1 |
| 1030 | assert builds[0].name == 'project-merge' |
| 1031 | assert job_has_changes(builds[0], A) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1032 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1033 | self.worker.release('.*-merge') |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1034 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1035 | assert len(builds) == 3 |
| 1036 | assert builds[0].name == 'project-test1' |
| 1037 | assert job_has_changes(builds[0], A) |
| 1038 | assert builds[1].name == 'project-test2' |
| 1039 | assert job_has_changes(builds[1], A) |
| 1040 | assert builds[2].name == 'project-merge' |
| 1041 | assert job_has_changes(builds[2], A, B) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1042 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1043 | self.worker.release('.*-merge') |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1044 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1045 | assert len(builds) == 5 |
| 1046 | assert builds[0].name == 'project-test1' |
| 1047 | assert job_has_changes(builds[0], A) |
| 1048 | assert builds[1].name == 'project-test2' |
| 1049 | assert job_has_changes(builds[1], A) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1050 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1051 | assert builds[2].name == 'project-test1' |
| 1052 | assert job_has_changes(builds[2], A, B) |
| 1053 | assert builds[3].name == 'project-test2' |
| 1054 | assert job_has_changes(builds[3], A, B) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1055 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1056 | assert builds[4].name == 'project-merge' |
| 1057 | assert job_has_changes(builds[4], A, B, C) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1058 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1059 | self.worker.release('.*-merge') |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1060 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1061 | assert len(builds) == 6 |
| 1062 | assert builds[0].name == 'project-test1' |
| 1063 | assert job_has_changes(builds[0], A) |
| 1064 | assert builds[1].name == 'project-test2' |
| 1065 | assert job_has_changes(builds[1], A) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1066 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1067 | assert builds[2].name == 'project-test1' |
| 1068 | assert job_has_changes(builds[2], A, B) |
| 1069 | assert builds[3].name == 'project-test2' |
| 1070 | assert job_has_changes(builds[3], A, B) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1071 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1072 | assert builds[4].name == 'project-test1' |
| 1073 | assert job_has_changes(builds[4], A, B, C) |
| 1074 | assert builds[5].name == 'project-test2' |
| 1075 | assert job_has_changes(builds[5], A, B, C) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1076 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1077 | self.worker.hold_jobs_in_build = False |
| 1078 | self.worker.release() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1079 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1080 | assert len(builds) == 0 |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1081 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1082 | assert len(history) == 9 |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1083 | assert A.data['status'] == 'MERGED' |
| 1084 | assert B.data['status'] == 'MERGED' |
| 1085 | assert C.data['status'] == 'MERGED' |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1086 | assert A.reported == 2 |
| 1087 | assert B.reported == 2 |
| 1088 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1089 | self.assertEmptyQueues() |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1090 | |
| 1091 | def test_failed_changes(self): |
| 1092 | "Test that a change behind a failed change is retested" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1093 | builds = self.worker.running_builds |
| 1094 | history = self.worker.build_history |
| 1095 | |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1096 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1097 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1098 | A.addApproval('CRVW', 2) |
| 1099 | B.addApproval('CRVW', 2) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1100 | |
| 1101 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1102 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1103 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1104 | self.worker.addFailTest('project-test1', A) |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1105 | |
| 1106 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1107 | assert len(history) > 6 |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1108 | assert A.data['status'] == 'NEW' |
| 1109 | assert B.data['status'] == 'MERGED' |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1110 | assert A.reported == 2 |
| 1111 | assert B.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1112 | self.assertEmptyQueues() |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1113 | |
| 1114 | def test_independent_queues(self): |
| 1115 | "Test that changes end up in the right queues" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1116 | builds = self.worker.running_builds |
| 1117 | history = self.worker.build_history |
| 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 |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1134 | assert len(builds) == 2 |
| 1135 | assert builds[0].name == 'project-merge' |
| 1136 | assert job_has_changes(builds[0], A) |
| 1137 | assert builds[1].name == 'project1-merge' |
| 1138 | assert job_has_changes(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 |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1149 | assert len(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() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1153 | assert len(builds) == 0 |
James E. Blair | b02a3bb | 2012-07-30 17:49:55 -0700 | [diff] [blame] | 1154 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1155 | assert len(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 | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1166 | builds = self.worker.running_builds |
| 1167 | history = self.worker.build_history |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1168 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1169 | self.worker.hold_jobs_in_build = True |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1170 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1171 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1172 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1173 | A.addApproval('CRVW', 2) |
| 1174 | B.addApproval('CRVW', 2) |
| 1175 | C.addApproval('CRVW', 2) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1176 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1177 | self.worker.addFailTest('project-test1', A) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1178 | |
| 1179 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1180 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1181 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1182 | |
| 1183 | self.waitUntilSettled() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1184 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1185 | assert len(builds) == 1 |
| 1186 | assert builds[0].name == 'project-merge' |
| 1187 | assert job_has_changes(builds[0], A) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1188 | |
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() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1193 | self.worker.release('.*-merge') |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1194 | self.waitUntilSettled() |
| 1195 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1196 | assert len(builds) == 6 |
| 1197 | assert builds[0].name == 'project-test1' |
| 1198 | assert builds[1].name == 'project-test2' |
| 1199 | assert builds[2].name == 'project-test1' |
| 1200 | assert builds[3].name == 'project-test2' |
| 1201 | assert builds[4].name == 'project-test1' |
| 1202 | assert builds[5].name == 'project-test2' |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1203 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1204 | self.release(builds[0]) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1205 | self.waitUntilSettled() |
| 1206 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1207 | assert len(builds) == 2 # project-test2, project-merge for B |
| 1208 | assert self.countJobResults(history, 'ABORTED') == 4 |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1209 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1210 | self.worker.hold_jobs_in_build = False |
| 1211 | self.worker.release() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1212 | self.waitUntilSettled() |
| 1213 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1214 | assert len(builds) == 0 |
| 1215 | assert len(history) == 15 |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1216 | assert A.data['status'] == 'NEW' |
| 1217 | assert B.data['status'] == 'MERGED' |
| 1218 | assert C.data['status'] == 'MERGED' |
| 1219 | assert A.reported == 2 |
| 1220 | assert B.reported == 2 |
| 1221 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1222 | self.assertEmptyQueues() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1223 | |
| 1224 | def test_failed_change_at_head_with_queue(self): |
| 1225 | "Test that if a change at the head fails, queued jobs are canceled" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1226 | builds = self.worker.running_builds |
| 1227 | history = self.worker.build_history |
| 1228 | queue = self.gearman_server.queue |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1229 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1230 | self.gearman_server.hold_jobs_in_queue = True |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1231 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1232 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1233 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1234 | A.addApproval('CRVW', 2) |
| 1235 | B.addApproval('CRVW', 2) |
| 1236 | C.addApproval('CRVW', 2) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1237 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1238 | self.worker.addFailTest('project-test1', A) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1239 | |
| 1240 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1241 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1242 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1243 | |
| 1244 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1245 | assert len(builds) == 0 |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1246 | assert len(queue) == 1 |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1247 | assert queue[0].name == 'build:project-merge' |
| 1248 | assert job_has_changes(queue[0], A) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1249 | |
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 | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1252 | self.gearman_server.release('.*-merge') |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1253 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1254 | self.gearman_server.release('.*-merge') |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1255 | self.waitUntilSettled() |
| 1256 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1257 | assert len(builds) == 0 |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1258 | assert len(queue) == 6 |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1259 | assert queue[0].name == 'build:project-test1' |
| 1260 | assert queue[1].name == 'build:project-test2' |
| 1261 | assert queue[2].name == 'build:project-test1' |
| 1262 | assert queue[3].name == 'build:project-test2' |
| 1263 | assert queue[4].name == 'build:project-test1' |
| 1264 | assert queue[5].name == 'build:project-test2' |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1265 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1266 | self.release(queue[0]) |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1267 | self.waitUntilSettled() |
| 1268 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1269 | assert len(builds) == 0 |
| 1270 | assert len(queue) == 2 # project-test2, project-merge for B |
| 1271 | assert self.countJobResults(history, 'ABORTED') == 0 |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1272 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1273 | self.gearman_server.hold_jobs_in_queue = False |
| 1274 | self.gearman_server.release() |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1275 | self.waitUntilSettled() |
| 1276 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1277 | assert len(builds) == 0 |
| 1278 | assert len(history) == 11 |
James E. Blair | d466dc4 | 2012-07-31 10:42:56 -0700 | [diff] [blame] | 1279 | assert A.data['status'] == 'NEW' |
| 1280 | assert B.data['status'] == 'MERGED' |
| 1281 | assert C.data['status'] == 'MERGED' |
| 1282 | assert A.reported == 2 |
| 1283 | assert B.reported == 2 |
| 1284 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1285 | self.assertEmptyQueues() |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1286 | |
| 1287 | def test_patch_order(self): |
| 1288 | "Test that dependent patches are tested in the right order" |
| 1289 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1290 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1291 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1292 | A.addApproval('CRVW', 2) |
| 1293 | B.addApproval('CRVW', 2) |
| 1294 | C.addApproval('CRVW', 2) |
| 1295 | |
| 1296 | M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2') |
| 1297 | M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1') |
| 1298 | M2.setMerged() |
| 1299 | M1.setMerged() |
| 1300 | |
| 1301 | # C -> B -> A -> M1 -> M2 |
| 1302 | # M2 is here to make sure it is never queried. If it is, it |
| 1303 | # means zuul is walking down the entire history of merged |
| 1304 | # changes. |
| 1305 | |
| 1306 | C.setDependsOn(B, 1) |
| 1307 | B.setDependsOn(A, 1) |
| 1308 | A.setDependsOn(M1, 1) |
| 1309 | M1.setDependsOn(M2, 1) |
| 1310 | |
| 1311 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1312 | |
| 1313 | self.waitUntilSettled() |
| 1314 | |
| 1315 | assert A.data['status'] == 'NEW' |
| 1316 | assert B.data['status'] == 'NEW' |
| 1317 | assert C.data['status'] == 'NEW' |
| 1318 | |
| 1319 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1320 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1321 | |
| 1322 | self.waitUntilSettled() |
| 1323 | assert M2.queried == 0 |
| 1324 | assert A.data['status'] == 'MERGED' |
| 1325 | assert B.data['status'] == 'MERGED' |
| 1326 | assert C.data['status'] == 'MERGED' |
| 1327 | assert A.reported == 2 |
| 1328 | assert B.reported == 2 |
| 1329 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1330 | self.assertEmptyQueues() |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1331 | |
| 1332 | def test_can_merge(self): |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1333 | "Test whether a change is ready to merge" |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1334 | # TODO: move to test_gerrit (this is a unit test!) |
| 1335 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1336 | a = self.sched.trigger.getChange(1, 2) |
| 1337 | mgr = self.sched.pipelines['gate'].manager |
| 1338 | assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()) |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1339 | |
| 1340 | A.addApproval('CRVW', 2) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1341 | a = self.sched.trigger.getChange(1, 2) |
| 1342 | assert not self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()) |
James E. Blair | 8c803f8 | 2012-07-31 16:25:42 -0700 | [diff] [blame] | 1343 | |
| 1344 | A.addApproval('APRV', 1) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1345 | a = self.sched.trigger.getChange(1, 2) |
| 1346 | assert self.sched.trigger.canMerge(a, mgr.getSubmitAllowNeeds()) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1347 | self.assertEmptyQueues() |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1348 | |
| 1349 | def test_build_configuration(self): |
| 1350 | "Test that zuul merges the right commits for testing" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1351 | builds = self.worker.running_builds |
| 1352 | history = self.worker.build_history |
| 1353 | queue = self.gearman_server.queue |
| 1354 | |
| 1355 | self.gearman_server.hold_jobs_in_queue = True |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1356 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1357 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1358 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1359 | A.addApproval('CRVW', 2) |
| 1360 | B.addApproval('CRVW', 2) |
| 1361 | C.addApproval('CRVW', 2) |
| 1362 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1363 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1364 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1365 | self.waitUntilSettled() |
| 1366 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1367 | self.gearman_server.release('.*-merge') |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1368 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1369 | self.gearman_server.release('.*-merge') |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1370 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1371 | self.gearman_server.release('.*-merge') |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1372 | self.waitUntilSettled() |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 1373 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1374 | ref = self.getParameter(queue[-1], 'ZUUL_REF') |
| 1375 | self.gearman_server.hold_jobs_in_queue = False |
| 1376 | self.gearman_server.release() |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1377 | self.waitUntilSettled() |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1378 | |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 1379 | path = os.path.join(GIT_ROOT, "org/project") |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1380 | repo = git.Repo(path) |
| 1381 | repo_messages = [c.message.strip() for c in repo.iter_commits(ref)] |
| 1382 | repo_messages.reverse() |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1383 | correct_messages = ['initial commit', 'A-1', 'B-1', 'C-1'] |
| 1384 | assert repo_messages == correct_messages |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1385 | self.assertEmptyQueues() |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1386 | |
| 1387 | def test_build_configuration_conflict(self): |
| 1388 | "Test that merge conflicts are handled" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1389 | builds = self.worker.running_builds |
| 1390 | history = self.worker.build_history |
| 1391 | queue = self.gearman_server.queue |
| 1392 | |
| 1393 | self.gearman_server.hold_jobs_in_queue = True |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1394 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1395 | A.addPatchset(['conflict']) |
| 1396 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1397 | B.addPatchset(['conflict']) |
| 1398 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1399 | A.addApproval('CRVW', 2) |
| 1400 | B.addApproval('CRVW', 2) |
| 1401 | C.addApproval('CRVW', 2) |
| 1402 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1403 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1404 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1405 | self.waitUntilSettled() |
| 1406 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1407 | self.gearman_server.release('.*-merge') |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1408 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1409 | self.gearman_server.release('.*-merge') |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1410 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1411 | self.gearman_server.release('.*-merge') |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1412 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1413 | ref = self.getParameter(queue[-1], 'ZUUL_REF') |
| 1414 | self.gearman_server.hold_jobs_in_queue = False |
| 1415 | self.gearman_server.release() |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1416 | self.waitUntilSettled() |
| 1417 | |
| 1418 | assert A.data['status'] == 'MERGED' |
| 1419 | assert B.data['status'] == 'NEW' |
| 1420 | assert C.data['status'] == 'MERGED' |
| 1421 | assert A.reported == 2 |
| 1422 | assert B.reported == 2 |
| 1423 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1424 | self.assertEmptyQueues() |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 1425 | |
| 1426 | def test_post(self): |
| 1427 | "Test that post jobs run" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1428 | builds = self.worker.running_builds |
| 1429 | history = self.worker.build_history |
| 1430 | |
Zhongyue Luo | 5d55607 | 2012-09-21 02:00:47 +0900 | [diff] [blame] | 1431 | e = { |
| 1432 | "type": "ref-updated", |
| 1433 | "submitter": { |
| 1434 | "name": "User Name", |
| 1435 | }, |
| 1436 | "refUpdate": { |
| 1437 | "oldRev": "90f173846e3af9154517b88543ffbd1691f31366", |
| 1438 | "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543", |
| 1439 | "refName": "master", |
| 1440 | "project": "org/project", |
| 1441 | } |
| 1442 | } |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 1443 | self.fake_gerrit.addEvent(e) |
| 1444 | self.waitUntilSettled() |
| 1445 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1446 | job_names = [x.name for x in history] |
| 1447 | assert len(history) == 1 |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 1448 | assert 'project-post' in job_names |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1449 | self.assertEmptyQueues() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1450 | |
| 1451 | def test_build_configuration_branch(self): |
| 1452 | "Test that the right commits are on alternate branches" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1453 | builds = self.worker.running_builds |
| 1454 | history = self.worker.build_history |
| 1455 | queue = self.gearman_server.queue |
| 1456 | |
| 1457 | self.gearman_server.hold_jobs_in_queue = True |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1458 | A = self.fake_gerrit.addFakeChange('org/project', 'mp', 'A') |
| 1459 | B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B') |
| 1460 | C = self.fake_gerrit.addFakeChange('org/project', 'mp', 'C') |
| 1461 | A.addApproval('CRVW', 2) |
| 1462 | B.addApproval('CRVW', 2) |
| 1463 | C.addApproval('CRVW', 2) |
| 1464 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1465 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1466 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1467 | self.waitUntilSettled() |
| 1468 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1469 | self.gearman_server.release('.*-merge') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1470 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1471 | self.gearman_server.release('.*-merge') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1472 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1473 | self.gearman_server.release('.*-merge') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1474 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1475 | ref = self.getParameter(queue[-1], 'ZUUL_REF') |
| 1476 | self.gearman_server.hold_jobs_in_queue = False |
| 1477 | self.gearman_server.release() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1478 | self.waitUntilSettled() |
| 1479 | |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 1480 | path = os.path.join(GIT_ROOT, "org/project") |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1481 | repo = git.Repo(path) |
| 1482 | repo_messages = [c.message.strip() for c in repo.iter_commits(ref)] |
| 1483 | repo_messages.reverse() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1484 | correct_messages = ['initial commit', 'mp commit', 'A-1', 'B-1', 'C-1'] |
| 1485 | assert repo_messages == correct_messages |
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_branch_interaction(self): |
| 1489 | "Test that switching between branches works" |
| 1490 | self.test_build_configuration() |
| 1491 | self.test_build_configuration_branch() |
| 1492 | # C has been merged, undo that |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 1493 | path = os.path.join(UPSTREAM_ROOT, "org/project") |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1494 | repo = git.Repo(path) |
| 1495 | repo.heads.master.commit = repo.commit('init') |
| 1496 | self.test_build_configuration() |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1497 | self.assertEmptyQueues() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1498 | |
| 1499 | def test_build_configuration_multi_branch(self): |
| 1500 | "Test that dependent changes on multiple branches are merged" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1501 | builds = self.worker.running_builds |
| 1502 | history = self.worker.build_history |
| 1503 | queue = self.gearman_server.queue |
| 1504 | |
| 1505 | self.gearman_server.hold_jobs_in_queue = True |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1506 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1507 | B = self.fake_gerrit.addFakeChange('org/project', 'mp', 'B') |
| 1508 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1509 | A.addApproval('CRVW', 2) |
| 1510 | B.addApproval('CRVW', 2) |
| 1511 | C.addApproval('CRVW', 2) |
| 1512 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1513 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1514 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1515 | self.waitUntilSettled() |
| 1516 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1517 | self.gearman_server.release('.*-merge') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1518 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1519 | ref_mp = self.getParameter(queue[-1], 'ZUUL_REF') |
| 1520 | self.gearman_server.release('.*-merge') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1521 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1522 | self.gearman_server.release('.*-merge') |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1523 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1524 | ref_master = self.getParameter(queue[-1], 'ZUUL_REF') |
| 1525 | self.gearman_server.hold_jobs_in_queue = False |
| 1526 | self.gearman_server.release() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1527 | self.waitUntilSettled() |
| 1528 | |
James E. Blair | 1dbd508 | 2012-08-23 15:12:15 -0700 | [diff] [blame] | 1529 | path = os.path.join(GIT_ROOT, "org/project") |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1530 | repo = git.Repo(path) |
| 1531 | |
| 1532 | repo_messages = [c.message.strip() |
| 1533 | for c in repo.iter_commits(ref_master)] |
| 1534 | repo_messages.reverse() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1535 | correct_messages = ['initial commit', 'A-1', 'C-1'] |
| 1536 | assert repo_messages == correct_messages |
| 1537 | |
| 1538 | repo_messages = [c.message.strip() |
| 1539 | for c in repo.iter_commits(ref_mp)] |
| 1540 | repo_messages.reverse() |
James E. Blair | c6294a5 | 2012-08-17 10:19:48 -0700 | [diff] [blame] | 1541 | correct_messages = ['initial commit', 'mp commit', 'B-1'] |
| 1542 | assert repo_messages == correct_messages |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1543 | self.assertEmptyQueues() |
James E. Blair | 7f71c80 | 2012-08-22 13:04:32 -0700 | [diff] [blame] | 1544 | |
| 1545 | def test_one_job_project(self): |
| 1546 | "Test that queueing works with one job" |
| 1547 | A = self.fake_gerrit.addFakeChange('org/one-job-project', |
| 1548 | 'master', 'A') |
| 1549 | B = self.fake_gerrit.addFakeChange('org/one-job-project', |
| 1550 | 'master', 'B') |
| 1551 | A.addApproval('CRVW', 2) |
| 1552 | B.addApproval('CRVW', 2) |
| 1553 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1554 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1555 | self.waitUntilSettled() |
| 1556 | |
James E. Blair | 7f71c80 | 2012-08-22 13:04:32 -0700 | [diff] [blame] | 1557 | assert A.data['status'] == 'MERGED' |
| 1558 | assert A.reported == 2 |
| 1559 | assert B.data['status'] == 'MERGED' |
| 1560 | assert B.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1561 | self.assertEmptyQueues() |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1562 | |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 1563 | def test_job_from_templates_launched(self): |
| 1564 | "Test whether a job generated via a template can be launched" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1565 | builds = self.worker.running_builds |
| 1566 | history = self.worker.build_history |
| 1567 | |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 1568 | A = self.fake_gerrit.addFakeChange( |
| 1569 | 'org/templated-project', 'master', 'A') |
| 1570 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1571 | self.waitUntilSettled() |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 1572 | |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1573 | assert self.getJobFromHistory('project-test1').result == 'SUCCESS' |
| 1574 | assert self.getJobFromHistory('project-test2').result == 'SUCCESS' |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 1575 | |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1576 | def test_dependent_changes_dequeue(self): |
| 1577 | "Test that dependent patches are not needlessly tested" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1578 | builds = self.worker.running_builds |
| 1579 | history = self.worker.build_history |
| 1580 | |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1581 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1582 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1583 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1584 | A.addApproval('CRVW', 2) |
| 1585 | B.addApproval('CRVW', 2) |
| 1586 | C.addApproval('CRVW', 2) |
| 1587 | |
| 1588 | M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1') |
| 1589 | M1.setMerged() |
| 1590 | |
| 1591 | # C -> B -> A -> M1 |
| 1592 | |
| 1593 | C.setDependsOn(B, 1) |
| 1594 | B.setDependsOn(A, 1) |
| 1595 | A.setDependsOn(M1, 1) |
| 1596 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1597 | self.worker.addFailTest('project-merge', A) |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1598 | |
| 1599 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1600 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1601 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1602 | |
| 1603 | self.waitUntilSettled() |
| 1604 | |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1605 | assert A.data['status'] == 'NEW' |
| 1606 | assert A.reported == 2 |
| 1607 | assert B.data['status'] == 'NEW' |
| 1608 | assert B.reported == 2 |
| 1609 | assert C.data['status'] == 'NEW' |
| 1610 | assert C.reported == 2 |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1611 | assert len(history) == 1 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1612 | self.assertEmptyQueues() |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1613 | |
| 1614 | def test_head_is_dequeued_once(self): |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 1615 | "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] | 1616 | # If it's dequeued more than once, we should see extra |
| 1617 | # aborted jobs. |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1618 | builds = self.worker.running_builds |
| 1619 | history = self.worker.build_history |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1620 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1621 | self.worker.hold_jobs_in_build = True |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1622 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 1623 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 1624 | C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C') |
| 1625 | A.addApproval('CRVW', 2) |
| 1626 | B.addApproval('CRVW', 2) |
| 1627 | C.addApproval('CRVW', 2) |
| 1628 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1629 | self.worker.addFailTest('project1-test1', A) |
| 1630 | self.worker.addFailTest('project1-test2', A) |
| 1631 | self.worker.addFailTest('project1-project2-integration', A) |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1632 | |
| 1633 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1634 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1635 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1636 | |
| 1637 | self.waitUntilSettled() |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1638 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1639 | assert len(builds) == 1 |
| 1640 | assert builds[0].name == 'project1-merge' |
| 1641 | assert job_has_changes(builds[0], A) |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1642 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1643 | self.worker.release('.*-merge') |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1644 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1645 | self.worker.release('.*-merge') |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1646 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1647 | self.worker.release('.*-merge') |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1648 | self.waitUntilSettled() |
| 1649 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1650 | assert len(builds) == 9 |
| 1651 | assert builds[0].name == 'project1-test1' |
| 1652 | assert builds[1].name == 'project1-test2' |
| 1653 | assert builds[2].name == 'project1-project2-integration' |
| 1654 | assert builds[3].name == 'project1-test1' |
| 1655 | assert builds[4].name == 'project1-test2' |
| 1656 | assert builds[5].name == 'project1-project2-integration' |
| 1657 | assert builds[6].name == 'project1-test1' |
| 1658 | assert builds[7].name == 'project1-test2' |
| 1659 | assert builds[8].name == 'project1-project2-integration' |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1660 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1661 | self.release(builds[0]) |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1662 | self.waitUntilSettled() |
| 1663 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1664 | assert len(builds) == 3 # test2, integration, merge for B |
| 1665 | assert self.countJobResults(history, 'ABORTED') == 6 |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1666 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1667 | self.worker.hold_jobs_in_build = False |
| 1668 | self.worker.release() |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1669 | self.waitUntilSettled() |
| 1670 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1671 | assert len(builds) == 0 |
| 1672 | assert len(history) == 20 |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1673 | |
| 1674 | assert A.data['status'] == 'NEW' |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1675 | assert B.data['status'] == 'MERGED' |
| 1676 | assert C.data['status'] == 'MERGED' |
| 1677 | assert A.reported == 2 |
| 1678 | assert B.reported == 2 |
| 1679 | assert C.reported == 2 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1680 | self.assertEmptyQueues() |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 1681 | |
| 1682 | def test_nonvoting_job(self): |
| 1683 | "Test that non-voting jobs don't vote." |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1684 | builds = self.worker.running_builds |
| 1685 | history = self.worker.build_history |
| 1686 | |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 1687 | A = self.fake_gerrit.addFakeChange('org/nonvoting-project', |
| 1688 | 'master', 'A') |
| 1689 | A.addApproval('CRVW', 2) |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1690 | self.worker.addFailTest('nonvoting-project-test2', A) |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 1691 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1692 | |
| 1693 | self.waitUntilSettled() |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 1694 | |
| 1695 | assert A.data['status'] == 'MERGED' |
| 1696 | assert A.reported == 2 |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1697 | assert (self.getJobFromHistory('nonvoting-project-merge').result == |
| 1698 | 'SUCCESS') |
| 1699 | assert (self.getJobFromHistory('nonvoting-project-test1').result == |
| 1700 | 'SUCCESS') |
| 1701 | assert (self.getJobFromHistory('nonvoting-project-test2').result == |
| 1702 | 'FAILURE') |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1703 | self.assertEmptyQueues() |
| 1704 | |
| 1705 | def test_check_queue_success(self): |
| 1706 | "Test successful check queue jobs." |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1707 | builds = self.worker.running_builds |
| 1708 | history = self.worker.build_history |
| 1709 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1710 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1711 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1712 | |
| 1713 | self.waitUntilSettled() |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1714 | |
| 1715 | assert A.data['status'] == 'NEW' |
| 1716 | assert A.reported == 1 |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1717 | assert self.getJobFromHistory('project-merge').result == 'SUCCESS' |
| 1718 | assert self.getJobFromHistory('project-test1').result == 'SUCCESS' |
| 1719 | assert self.getJobFromHistory('project-test2').result == 'SUCCESS' |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1720 | self.assertEmptyQueues() |
| 1721 | |
| 1722 | def test_check_queue_failure(self): |
| 1723 | "Test failed check queue jobs." |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1724 | builds = self.worker.running_builds |
| 1725 | history = self.worker.build_history |
| 1726 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1727 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1728 | self.worker.addFailTest('project-test2', A) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1729 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 1730 | |
| 1731 | self.waitUntilSettled() |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1732 | |
| 1733 | assert A.data['status'] == 'NEW' |
| 1734 | assert A.reported == 1 |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1735 | assert self.getJobFromHistory('project-merge').result == 'SUCCESS' |
| 1736 | assert self.getJobFromHistory('project-test1').result == 'SUCCESS' |
| 1737 | assert self.getJobFromHistory('project-test2').result == 'FAILURE' |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1738 | self.assertEmptyQueues() |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1739 | |
| 1740 | def test_dependent_behind_dequeue(self): |
| 1741 | "test that dependent changes behind dequeued changes work" |
| 1742 | # This complicated test is a reproduction of a real life bug |
| 1743 | self.sched.reconfigure(self.config) |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1744 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1745 | builds = self.worker.running_builds |
| 1746 | history = self.worker.build_history |
| 1747 | |
| 1748 | self.worker.hold_jobs_in_build = True |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1749 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 1750 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 1751 | C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C') |
| 1752 | D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D') |
| 1753 | E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E') |
| 1754 | F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F') |
| 1755 | D.setDependsOn(C, 1) |
| 1756 | E.setDependsOn(D, 1) |
| 1757 | A.addApproval('CRVW', 2) |
| 1758 | B.addApproval('CRVW', 2) |
| 1759 | C.addApproval('CRVW', 2) |
| 1760 | D.addApproval('CRVW', 2) |
| 1761 | E.addApproval('CRVW', 2) |
| 1762 | F.addApproval('CRVW', 2) |
| 1763 | |
| 1764 | A.fail_merge = True |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1765 | |
| 1766 | # Change object re-use in the gerrit trigger is hidden if |
| 1767 | # changes are added in quick succession; waiting makes it more |
| 1768 | # like real life. |
| 1769 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1770 | self.waitUntilSettled() |
| 1771 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1772 | self.waitUntilSettled() |
| 1773 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1774 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1775 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1776 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1777 | self.waitUntilSettled() |
| 1778 | |
| 1779 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1780 | self.waitUntilSettled() |
| 1781 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 1782 | self.waitUntilSettled() |
| 1783 | self.fake_gerrit.addEvent(E.addApproval('APRV', 1)) |
| 1784 | self.waitUntilSettled() |
| 1785 | self.fake_gerrit.addEvent(F.addApproval('APRV', 1)) |
| 1786 | self.waitUntilSettled() |
| 1787 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1788 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1789 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1790 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1791 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1792 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1793 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1794 | self.worker.release('.*-merge') |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1795 | self.waitUntilSettled() |
| 1796 | |
| 1797 | # all jobs running |
James E. Blair | e955e06 | 2012-10-08 09:49:03 -0700 | [diff] [blame] | 1798 | |
| 1799 | # Grab pointers to the jobs we want to release before |
| 1800 | # releasing any, because list indexes may change as |
| 1801 | # the jobs complete. |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1802 | a, b, c = builds[:3] |
James E. Blair | e955e06 | 2012-10-08 09:49:03 -0700 | [diff] [blame] | 1803 | a.release() |
| 1804 | b.release() |
| 1805 | c.release() |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1806 | self.waitUntilSettled() |
| 1807 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1808 | self.worker.hold_jobs_in_build = False |
| 1809 | self.worker.release() |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1810 | self.waitUntilSettled() |
| 1811 | |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1812 | assert A.data['status'] == 'NEW' |
| 1813 | assert B.data['status'] == 'MERGED' |
| 1814 | assert C.data['status'] == 'MERGED' |
| 1815 | assert D.data['status'] == 'MERGED' |
| 1816 | assert E.data['status'] == 'MERGED' |
| 1817 | assert F.data['status'] == 'MERGED' |
| 1818 | |
| 1819 | assert A.reported == 2 |
| 1820 | assert B.reported == 2 |
| 1821 | assert C.reported == 2 |
| 1822 | assert D.reported == 2 |
| 1823 | assert E.reported == 2 |
| 1824 | assert F.reported == 2 |
| 1825 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1826 | assert self.countJobResults(history, 'ABORTED') == 15 |
| 1827 | assert len(history) == 44 |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1828 | self.assertEmptyQueues() |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 1829 | |
| 1830 | def test_merger_repack(self): |
| 1831 | "Test that the merger works after a repack" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1832 | builds = self.worker.running_builds |
| 1833 | history = self.worker.build_history |
| 1834 | |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 1835 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1836 | A.addApproval('CRVW', 2) |
| 1837 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1838 | self.waitUntilSettled() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1839 | assert self.getJobFromHistory('project-merge').result == 'SUCCESS' |
| 1840 | assert self.getJobFromHistory('project-test1').result == 'SUCCESS' |
| 1841 | assert self.getJobFromHistory('project-test2').result == 'SUCCESS' |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 1842 | assert A.data['status'] == 'MERGED' |
| 1843 | assert A.reported == 2 |
| 1844 | self.assertEmptyQueues() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1845 | self.worker.build_history = [] |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 1846 | |
| 1847 | path = os.path.join(GIT_ROOT, "org/project") |
| 1848 | os.system('git --git-dir=%s/.git repack -afd' % path) |
| 1849 | |
| 1850 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1851 | A.addApproval('CRVW', 2) |
| 1852 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1853 | self.waitUntilSettled() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1854 | assert self.getJobFromHistory('project-merge').result == 'SUCCESS' |
| 1855 | assert self.getJobFromHistory('project-test1').result == 'SUCCESS' |
| 1856 | assert self.getJobFromHistory('project-test2').result == 'SUCCESS' |
James E. Blair | 05fed60 | 2012-09-07 12:45:24 -0700 | [diff] [blame] | 1857 | assert A.data['status'] == 'MERGED' |
| 1858 | assert A.reported == 2 |
| 1859 | self.assertEmptyQueues() |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 1860 | |
James E. Blair | 4886f28 | 2012-11-15 09:27:33 -0800 | [diff] [blame] | 1861 | def test_merger_repack_large_change(self): |
| 1862 | "Test that the merger works with large changes after a repack" |
| 1863 | # https://bugs.launchpad.net/zuul/+bug/1078946 |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1864 | builds = self.worker.running_builds |
| 1865 | history = self.worker.build_history |
| 1866 | |
James E. Blair | 4886f28 | 2012-11-15 09:27:33 -0800 | [diff] [blame] | 1867 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 1868 | A.addPatchset(large=True) |
| 1869 | path = os.path.join(UPSTREAM_ROOT, "org/project1") |
| 1870 | os.system('git --git-dir=%s/.git repack -afd' % path) |
| 1871 | path = os.path.join(GIT_ROOT, "org/project1") |
| 1872 | os.system('git --git-dir=%s/.git repack -afd' % path) |
| 1873 | |
| 1874 | A.addApproval('CRVW', 2) |
| 1875 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1876 | self.waitUntilSettled() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1877 | assert self.getJobFromHistory('project1-merge').result == 'SUCCESS' |
| 1878 | assert self.getJobFromHistory('project1-test1').result == 'SUCCESS' |
| 1879 | assert self.getJobFromHistory('project1-test2').result == 'SUCCESS' |
James E. Blair | 4886f28 | 2012-11-15 09:27:33 -0800 | [diff] [blame] | 1880 | assert A.data['status'] == 'MERGED' |
| 1881 | assert A.reported == 2 |
| 1882 | self.assertEmptyQueues() |
| 1883 | |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 1884 | def test_nonexistent_job(self): |
| 1885 | "Test launching a job that doesn't exist" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1886 | builds = self.worker.running_builds |
| 1887 | history = self.worker.build_history |
| 1888 | |
| 1889 | # Set to the state immediately after a restart |
| 1890 | self.resetGearmanServer() |
| 1891 | self.launcher.negative_function_cache_ttl = 0 |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 1892 | |
| 1893 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1894 | A.addApproval('CRVW', 2) |
| 1895 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1896 | # There may be a thread about to report a lost change |
| 1897 | while A.reported < 2: |
| 1898 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1899 | job_names = [x.name for x in history] |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 1900 | assert not job_names |
| 1901 | assert A.data['status'] == 'NEW' |
| 1902 | assert A.reported == 2 |
| 1903 | self.assertEmptyQueues() |
| 1904 | |
| 1905 | # Make sure things still work: |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1906 | self.registerJobs() |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 1907 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1908 | A.addApproval('CRVW', 2) |
| 1909 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1910 | self.waitUntilSettled() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 1911 | assert self.getJobFromHistory('project-merge').result == 'SUCCESS' |
| 1912 | assert self.getJobFromHistory('project-test1').result == 'SUCCESS' |
| 1913 | assert self.getJobFromHistory('project-test2').result == 'SUCCESS' |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 1914 | assert A.data['status'] == 'MERGED' |
| 1915 | assert A.reported == 2 |
| 1916 | self.assertEmptyQueues() |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 1917 | |
| 1918 | def test_single_nonexistent_post_job(self): |
| 1919 | "Test launching a single post job that doesn't exist" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1920 | builds = self.worker.running_builds |
| 1921 | history = self.worker.build_history |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 1922 | |
| 1923 | e = { |
| 1924 | "type": "ref-updated", |
| 1925 | "submitter": { |
| 1926 | "name": "User Name", |
| 1927 | }, |
| 1928 | "refUpdate": { |
| 1929 | "oldRev": "90f173846e3af9154517b88543ffbd1691f31366", |
| 1930 | "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543", |
| 1931 | "refName": "master", |
| 1932 | "project": "org/project", |
| 1933 | } |
| 1934 | } |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1935 | # Set to the state immediately after a restart |
| 1936 | self.resetGearmanServer() |
| 1937 | self.launcher.negative_function_cache_ttl = 0 |
| 1938 | |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 1939 | self.fake_gerrit.addEvent(e) |
| 1940 | self.waitUntilSettled() |
| 1941 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1942 | assert len(history) == 0 |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 1943 | self.assertEmptyQueues() |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 1944 | |
| 1945 | def test_new_patchset_dequeues_old(self): |
| 1946 | "Test that a new patchset causes the old to be dequeued" |
| 1947 | # D -> C (depends on B) -> B (depends on A) -> A -> M |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1948 | builds = self.worker.running_builds |
| 1949 | history = self.worker.build_history |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 1950 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1951 | self.worker.hold_jobs_in_build = True |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 1952 | M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M') |
| 1953 | M.setMerged() |
| 1954 | |
| 1955 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 1956 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 1957 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 1958 | D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D') |
| 1959 | A.addApproval('CRVW', 2) |
| 1960 | B.addApproval('CRVW', 2) |
| 1961 | C.addApproval('CRVW', 2) |
| 1962 | D.addApproval('CRVW', 2) |
| 1963 | |
| 1964 | C.setDependsOn(B, 1) |
| 1965 | B.setDependsOn(A, 1) |
| 1966 | A.setDependsOn(M, 1) |
| 1967 | |
| 1968 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 1969 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 1970 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 1971 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 1972 | self.waitUntilSettled() |
| 1973 | |
| 1974 | B.addPatchset() |
| 1975 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2)) |
| 1976 | self.waitUntilSettled() |
| 1977 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1978 | self.worker.hold_jobs_in_build = False |
| 1979 | self.worker.release() |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 1980 | self.waitUntilSettled() |
| 1981 | |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 1982 | assert A.data['status'] == 'MERGED' |
| 1983 | assert A.reported == 2 |
| 1984 | assert B.data['status'] == 'NEW' |
| 1985 | assert B.reported == 2 |
| 1986 | assert C.data['status'] == 'NEW' |
| 1987 | assert C.reported == 2 |
| 1988 | assert D.data['status'] == 'MERGED' |
| 1989 | assert D.reported == 2 |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1990 | assert len(history) == 9 # 3 each for A, B, D. |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 1991 | self.assertEmptyQueues() |
| 1992 | |
| 1993 | def test_new_patchset_dequeues_old_on_head(self): |
| 1994 | "Test that a new patchset causes the old to be dequeued (at head)" |
| 1995 | # D -> C (depends on B) -> B (depends on A) -> A -> M |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1996 | builds = self.worker.running_builds |
| 1997 | history = self.worker.build_history |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 1998 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 1999 | self.worker.hold_jobs_in_build = True |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2000 | M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M') |
| 2001 | M.setMerged() |
| 2002 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2003 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2004 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 2005 | D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D') |
| 2006 | A.addApproval('CRVW', 2) |
| 2007 | B.addApproval('CRVW', 2) |
| 2008 | C.addApproval('CRVW', 2) |
| 2009 | D.addApproval('CRVW', 2) |
| 2010 | |
| 2011 | C.setDependsOn(B, 1) |
| 2012 | B.setDependsOn(A, 1) |
| 2013 | A.setDependsOn(M, 1) |
| 2014 | |
| 2015 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2016 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2017 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2018 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 2019 | self.waitUntilSettled() |
| 2020 | |
| 2021 | A.addPatchset() |
| 2022 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2)) |
| 2023 | self.waitUntilSettled() |
| 2024 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2025 | self.worker.hold_jobs_in_build = False |
| 2026 | self.worker.release() |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2027 | self.waitUntilSettled() |
| 2028 | |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2029 | assert A.data['status'] == 'NEW' |
| 2030 | assert A.reported == 2 |
| 2031 | assert B.data['status'] == 'NEW' |
| 2032 | assert B.reported == 2 |
| 2033 | assert C.data['status'] == 'NEW' |
| 2034 | assert C.reported == 2 |
| 2035 | assert D.data['status'] == 'MERGED' |
| 2036 | assert D.reported == 2 |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2037 | assert len(history) == 7 |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2038 | self.assertEmptyQueues() |
| 2039 | |
| 2040 | def test_new_patchset_dequeues_old_without_dependents(self): |
| 2041 | "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] | 2042 | builds = self.worker.running_builds |
| 2043 | history = self.worker.build_history |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2044 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2045 | self.worker.hold_jobs_in_build = True |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2046 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2047 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2048 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 2049 | A.addApproval('CRVW', 2) |
| 2050 | B.addApproval('CRVW', 2) |
| 2051 | C.addApproval('CRVW', 2) |
| 2052 | |
| 2053 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2054 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2055 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2056 | self.waitUntilSettled() |
| 2057 | |
| 2058 | B.addPatchset() |
| 2059 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2)) |
| 2060 | self.waitUntilSettled() |
| 2061 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2062 | self.worker.hold_jobs_in_build = False |
| 2063 | self.worker.release() |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2064 | self.waitUntilSettled() |
| 2065 | |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2066 | assert A.data['status'] == 'MERGED' |
| 2067 | assert A.reported == 2 |
| 2068 | assert B.data['status'] == 'NEW' |
| 2069 | assert B.reported == 2 |
| 2070 | assert C.data['status'] == 'MERGED' |
| 2071 | assert C.reported == 2 |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2072 | assert len(history) == 9 |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2073 | self.assertEmptyQueues() |
| 2074 | |
| 2075 | def test_new_patchset_dequeues_old_independent_queue(self): |
| 2076 | "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] | 2077 | builds = self.worker.running_builds |
| 2078 | history = self.worker.build_history |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2079 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2080 | self.worker.hold_jobs_in_build = True |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2081 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2082 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2083 | C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C') |
| 2084 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 2085 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 2086 | self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1)) |
| 2087 | self.waitUntilSettled() |
| 2088 | |
| 2089 | B.addPatchset() |
| 2090 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2)) |
| 2091 | self.waitUntilSettled() |
| 2092 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2093 | self.worker.hold_jobs_in_build = False |
| 2094 | self.worker.release() |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2095 | self.waitUntilSettled() |
| 2096 | |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2097 | assert A.data['status'] == 'NEW' |
| 2098 | assert A.reported == 1 |
| 2099 | assert B.data['status'] == 'NEW' |
| 2100 | assert B.reported == 1 |
| 2101 | assert C.data['status'] == 'NEW' |
| 2102 | assert C.reported == 1 |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2103 | assert len(history) == 10 |
| 2104 | assert self.countJobResults(history, 'ABORTED') == 1 |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2105 | self.assertEmptyQueues() |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2106 | |
| 2107 | def test_zuul_refs(self): |
| 2108 | "Test that zuul refs exist and have the right changes" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2109 | builds = self.worker.running_builds |
| 2110 | history = self.worker.build_history |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2111 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2112 | self.worker.hold_jobs_in_build = True |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2113 | M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1') |
| 2114 | M1.setMerged() |
| 2115 | M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2') |
| 2116 | M2.setMerged() |
| 2117 | |
| 2118 | A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A') |
| 2119 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 2120 | C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C') |
| 2121 | D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D') |
| 2122 | A.addApproval('CRVW', 2) |
| 2123 | B.addApproval('CRVW', 2) |
| 2124 | C.addApproval('CRVW', 2) |
| 2125 | D.addApproval('CRVW', 2) |
| 2126 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2127 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2128 | self.fake_gerrit.addEvent(C.addApproval('APRV', 1)) |
| 2129 | self.fake_gerrit.addEvent(D.addApproval('APRV', 1)) |
| 2130 | |
| 2131 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2132 | self.worker.release('.*-merge') |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2133 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2134 | self.worker.release('.*-merge') |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2135 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2136 | self.worker.release('.*-merge') |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2137 | self.waitUntilSettled() |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2138 | self.worker.release('.*-merge') |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2139 | self.waitUntilSettled() |
| 2140 | |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2141 | a_zref = b_zref = c_zref = d_zref = None |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2142 | for x in builds: |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2143 | if x.parameters['ZUUL_CHANGE'] == '3': |
| 2144 | a_zref = x.parameters['ZUUL_REF'] |
| 2145 | if x.parameters['ZUUL_CHANGE'] == '4': |
| 2146 | b_zref = x.parameters['ZUUL_REF'] |
| 2147 | if x.parameters['ZUUL_CHANGE'] == '5': |
| 2148 | c_zref = x.parameters['ZUUL_REF'] |
| 2149 | if x.parameters['ZUUL_CHANGE'] == '6': |
| 2150 | d_zref = x.parameters['ZUUL_REF'] |
| 2151 | |
| 2152 | # There are... four... refs. |
| 2153 | assert a_zref is not None |
| 2154 | assert b_zref is not None |
| 2155 | assert c_zref is not None |
| 2156 | assert d_zref is not None |
| 2157 | |
| 2158 | # And they should all be different |
| 2159 | refs = set([a_zref, b_zref, c_zref, d_zref]) |
| 2160 | assert len(refs) == 4 |
| 2161 | |
| 2162 | # a ref should have a, not b, and should not be in project2 |
| 2163 | assert ref_has_change(a_zref, A) |
| 2164 | assert not ref_has_change(a_zref, B) |
| 2165 | assert not ref_has_change(a_zref, M2) |
| 2166 | |
| 2167 | # b ref should have a and b, and should not be in project2 |
| 2168 | assert ref_has_change(b_zref, A) |
| 2169 | assert ref_has_change(b_zref, B) |
| 2170 | assert not ref_has_change(b_zref, M2) |
| 2171 | |
| 2172 | # c ref should have a and b in 1, c in 2 |
| 2173 | assert ref_has_change(c_zref, A) |
| 2174 | assert ref_has_change(c_zref, B) |
| 2175 | assert ref_has_change(c_zref, C) |
| 2176 | assert not ref_has_change(c_zref, D) |
| 2177 | |
| 2178 | # d ref should have a and b in 1, c and d in 2 |
| 2179 | assert ref_has_change(d_zref, A) |
| 2180 | assert ref_has_change(d_zref, B) |
| 2181 | assert ref_has_change(d_zref, C) |
| 2182 | assert ref_has_change(d_zref, D) |
| 2183 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2184 | self.worker.hold_jobs_in_build = False |
| 2185 | self.worker.release() |
James E. Blair | 7d0dedc | 2013-02-21 17:26:09 -0800 | [diff] [blame] | 2186 | self.waitUntilSettled() |
| 2187 | |
| 2188 | assert A.data['status'] == 'MERGED' |
| 2189 | assert A.reported == 2 |
| 2190 | assert B.data['status'] == 'MERGED' |
| 2191 | assert B.reported == 2 |
| 2192 | assert C.data['status'] == 'MERGED' |
| 2193 | assert C.reported == 2 |
| 2194 | assert D.data['status'] == 'MERGED' |
| 2195 | assert D.reported == 2 |
| 2196 | self.assertEmptyQueues() |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 2197 | |
James E. Blair | 412e558 | 2013-04-22 15:50:12 -0700 | [diff] [blame] | 2198 | def test_statsd(self): |
| 2199 | "Test each of the statsd methods used in the scheduler" |
| 2200 | import extras |
| 2201 | statsd = extras.try_import('statsd.statsd') |
| 2202 | statsd.incr('test-incr') |
| 2203 | statsd.timing('test-timing', 3) |
| 2204 | statsd.gauge('test-guage', 12) |
| 2205 | self.assertReportedStat('test-incr', '1|c') |
| 2206 | self.assertReportedStat('test-timing', '3|ms') |
| 2207 | self.assertReportedStat('test-guage', '12|g') |
| 2208 | |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 2209 | def test_file_jobs(self): |
| 2210 | "Test that file jobs run only when appropriate" |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2211 | builds = self.worker.running_builds |
| 2212 | history = self.worker.build_history |
| 2213 | |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 2214 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2215 | A.addPatchset(['pip-requires']) |
| 2216 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 2217 | A.addApproval('CRVW', 2) |
| 2218 | B.addApproval('CRVW', 2) |
| 2219 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2220 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 2221 | self.waitUntilSettled() |
| 2222 | |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2223 | testfile_jobs = [x for x in history |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 2224 | if x.name == 'project-testfile'] |
| 2225 | |
| 2226 | assert len(testfile_jobs) == 1 |
| 2227 | assert testfile_jobs[0].changes == '1,2' |
| 2228 | assert A.data['status'] == 'MERGED' |
| 2229 | assert A.reported == 2 |
| 2230 | assert B.data['status'] == 'MERGED' |
| 2231 | assert B.reported == 2 |
| 2232 | self.assertEmptyQueues() |
James E. Blair | 3c5e5b5 | 2013-04-26 11:17:03 -0700 | [diff] [blame] | 2233 | |
| 2234 | def test_test_config(self): |
| 2235 | "Test that we can test the config" |
| 2236 | sched = zuul.scheduler.Scheduler() |
| 2237 | sched.testConfig(CONFIG.get('zuul', 'layout_config')) |
James E. Blair | 1f4c2bb | 2013-04-26 08:40:46 -0700 | [diff] [blame] | 2238 | |
| 2239 | def test_build_description(self): |
| 2240 | "Test that build descriptions update" |
| 2241 | builds = self.worker.running_builds |
| 2242 | history = self.worker.build_history |
| 2243 | |
| 2244 | self.worker.registerFunction('set_description:' + |
| 2245 | self.worker.worker_id) |
| 2246 | |
| 2247 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 2248 | A.addApproval('CRVW', 2) |
| 2249 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2250 | self.waitUntilSettled() |
| 2251 | desc = history[0].description |
| 2252 | self.log.debug("Description: %s" % desc) |
| 2253 | assert re.search("Branch.*master", desc) |
| 2254 | assert re.search("Pipeline.*gate", desc) |
| 2255 | assert re.search("project-merge.*SUCCESS", desc) |
| 2256 | assert re.search("project-test1.*SUCCESS", desc) |
| 2257 | assert re.search("project-test2.*SUCCESS", desc) |
| 2258 | assert re.search("Reported result.*SUCCESS", desc) |
| 2259 | |
| 2260 | def test_node_label(self): |
| 2261 | "Test that a job runs on a specific node label" |
| 2262 | builds = self.worker.running_builds |
| 2263 | history = self.worker.build_history |
| 2264 | |
| 2265 | self.worker.registerFunction('build:node-project-test1:debian') |
| 2266 | |
| 2267 | A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A') |
| 2268 | A.addApproval('CRVW', 2) |
| 2269 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 2270 | self.waitUntilSettled() |
James E. Blair | 4ca985f | 2013-05-30 12:27:43 -0700 | [diff] [blame] | 2271 | |
| 2272 | assert self.getJobFromHistory('node-project-merge').node is None |
| 2273 | assert self.getJobFromHistory('node-project-test1').node == 'debian' |
| 2274 | assert self.getJobFromHistory('node-project-test2').node is None |