Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright 2012 Hewlett-Packard Development Company, L.P. |
James E. Blair | 498059b | 2016-12-20 13:50:13 -0800 | [diff] [blame] | 4 | # Copyright 2016 Red Hat, Inc. |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
Christian Berendt | ffba5df | 2014-06-07 21:30:22 +0200 | [diff] [blame] | 18 | from six.moves import configparser as ConfigParser |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 19 | import gc |
| 20 | import hashlib |
| 21 | import json |
| 22 | import logging |
| 23 | import os |
Christian Berendt | 12d4d72 | 2014-06-07 21:03:45 +0200 | [diff] [blame] | 24 | from six.moves import queue as Queue |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 25 | from six.moves import urllib |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 26 | import random |
| 27 | import re |
| 28 | import select |
| 29 | import shutil |
Monty Taylor | 74fa386 | 2016-06-02 07:39:49 +0300 | [diff] [blame] | 30 | from six.moves import reload_module |
James E. Blair | 1c236df | 2017-02-01 14:07:24 -0800 | [diff] [blame] | 31 | from six import StringIO |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 32 | import socket |
| 33 | import string |
| 34 | import subprocess |
| 35 | import swiftclient |
James E. Blair | 1c236df | 2017-02-01 14:07:24 -0800 | [diff] [blame] | 36 | import sys |
James E. Blair | f84026c | 2015-12-08 16:11:46 -0800 | [diff] [blame] | 37 | import tempfile |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 38 | import threading |
| 39 | import time |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 40 | |
| 41 | import git |
| 42 | import gear |
| 43 | import fixtures |
James E. Blair | 498059b | 2016-12-20 13:50:13 -0800 | [diff] [blame] | 44 | import kazoo.client |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 45 | import kazoo.exceptions |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 46 | import statsd |
| 47 | import testtools |
James E. Blair | 1c236df | 2017-02-01 14:07:24 -0800 | [diff] [blame] | 48 | import testtools.content |
| 49 | import testtools.content_type |
Clint Byrum | 3343e3e | 2016-11-15 16:05:03 -0800 | [diff] [blame] | 50 | from git.exc import NoSuchPathError |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 51 | |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 52 | import zuul.driver.gerrit.gerritsource as gerritsource |
| 53 | import zuul.driver.gerrit.gerritconnection as gerritconnection |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 54 | import zuul.scheduler |
| 55 | import zuul.webapp |
| 56 | import zuul.rpclistener |
Joshua Hesketh | 0c54b2a | 2016-04-11 21:23:33 +1000 | [diff] [blame] | 57 | import zuul.launcher.server |
| 58 | import zuul.launcher.client |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 59 | import zuul.lib.swift |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 60 | import zuul.lib.connections |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 61 | import zuul.merger.client |
James E. Blair | 879dafb | 2015-07-17 14:04:49 -0700 | [diff] [blame] | 62 | import zuul.merger.merger |
| 63 | import zuul.merger.server |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 64 | import zuul.nodepool |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 65 | import zuul.zk |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 66 | |
| 67 | FIXTURE_DIR = os.path.join(os.path.dirname(__file__), |
| 68 | 'fixtures') |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 69 | USE_TEMPDIR = True |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 70 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 71 | |
| 72 | def repack_repo(path): |
| 73 | cmd = ['git', '--git-dir=%s/.git' % path, 'repack', '-afd'] |
| 74 | output = subprocess.Popen(cmd, close_fds=True, |
| 75 | stdout=subprocess.PIPE, |
| 76 | stderr=subprocess.PIPE) |
| 77 | out = output.communicate() |
| 78 | if output.returncode: |
| 79 | raise Exception("git repack returned %d" % output.returncode) |
| 80 | return out |
| 81 | |
| 82 | |
| 83 | def random_sha1(): |
| 84 | return hashlib.sha1(str(random.random())).hexdigest() |
| 85 | |
| 86 | |
James E. Blair | a190f3b | 2015-01-05 14:56:54 -0800 | [diff] [blame] | 87 | def iterate_timeout(max_seconds, purpose): |
| 88 | start = time.time() |
| 89 | count = 0 |
| 90 | while (time.time() < start + max_seconds): |
| 91 | count += 1 |
| 92 | yield count |
| 93 | time.sleep(0) |
| 94 | raise Exception("Timeout waiting for %s" % purpose) |
| 95 | |
| 96 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 97 | class ChangeReference(git.Reference): |
| 98 | _common_path_default = "refs/changes" |
| 99 | _points_to_commits_only = True |
| 100 | |
| 101 | |
| 102 | class FakeChange(object): |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 103 | categories = {'approved': ('Approved', -1, 1), |
| 104 | 'code-review': ('Code-Review', -2, 2), |
| 105 | 'verified': ('Verified', -2, 2)} |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 106 | |
| 107 | def __init__(self, gerrit, number, project, branch, subject, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 108 | status='NEW', upstream_root=None, files={}): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 109 | self.gerrit = gerrit |
| 110 | self.reported = 0 |
| 111 | self.queried = 0 |
| 112 | self.patchsets = [] |
| 113 | self.number = number |
| 114 | self.project = project |
| 115 | self.branch = branch |
| 116 | self.subject = subject |
| 117 | self.latest_patchset = 0 |
| 118 | self.depends_on_change = None |
| 119 | self.needed_by_changes = [] |
| 120 | self.fail_merge = False |
| 121 | self.messages = [] |
| 122 | self.data = { |
| 123 | 'branch': branch, |
| 124 | 'comments': [], |
| 125 | 'commitMessage': subject, |
| 126 | 'createdOn': time.time(), |
| 127 | 'id': 'I' + random_sha1(), |
| 128 | 'lastUpdated': time.time(), |
| 129 | 'number': str(number), |
| 130 | 'open': status == 'NEW', |
| 131 | 'owner': {'email': 'user@example.com', |
| 132 | 'name': 'User Name', |
| 133 | 'username': 'username'}, |
| 134 | 'patchSets': self.patchsets, |
| 135 | 'project': project, |
| 136 | 'status': status, |
| 137 | 'subject': subject, |
| 138 | 'submitRecords': [], |
| 139 | 'url': 'https://hostname/%s' % number} |
| 140 | |
| 141 | self.upstream_root = upstream_root |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 142 | self.addPatchset(files=files) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 143 | self.data['submitRecords'] = self.getSubmitRecords() |
| 144 | self.open = status == 'NEW' |
| 145 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 146 | def addFakeChangeToRepo(self, msg, files, large): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 147 | path = os.path.join(self.upstream_root, self.project) |
| 148 | repo = git.Repo(path) |
| 149 | ref = ChangeReference.create(repo, '1/%s/%s' % (self.number, |
| 150 | self.latest_patchset), |
| 151 | 'refs/tags/init') |
| 152 | repo.head.reference = ref |
James E. Blair | 879dafb | 2015-07-17 14:04:49 -0700 | [diff] [blame] | 153 | zuul.merger.merger.reset_repo_to_head(repo) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 154 | repo.git.clean('-x', '-f', '-d') |
| 155 | |
| 156 | path = os.path.join(self.upstream_root, self.project) |
| 157 | if not large: |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 158 | for fn, content in files.items(): |
| 159 | fn = os.path.join(path, fn) |
| 160 | with open(fn, 'w') as f: |
| 161 | f.write(content) |
| 162 | repo.index.add([fn]) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 163 | else: |
| 164 | for fni in range(100): |
| 165 | fn = os.path.join(path, str(fni)) |
| 166 | f = open(fn, 'w') |
| 167 | for ci in range(4096): |
| 168 | f.write(random.choice(string.printable)) |
| 169 | f.close() |
| 170 | repo.index.add([fn]) |
| 171 | |
| 172 | r = repo.index.commit(msg) |
| 173 | repo.head.reference = 'master' |
James E. Blair | 879dafb | 2015-07-17 14:04:49 -0700 | [diff] [blame] | 174 | zuul.merger.merger.reset_repo_to_head(repo) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 175 | repo.git.clean('-x', '-f', '-d') |
| 176 | repo.heads['master'].checkout() |
| 177 | return r |
| 178 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 179 | def addPatchset(self, files=None, large=False): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 180 | self.latest_patchset += 1 |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 181 | if not files: |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 182 | fn = '%s-%s' % (self.branch.replace('/', '_'), self.number) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 183 | data = ("test %s %s %s\n" % |
| 184 | (self.branch, self.number, self.latest_patchset)) |
| 185 | files = {fn: data} |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 186 | msg = self.subject + '-' + str(self.latest_patchset) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 187 | c = self.addFakeChangeToRepo(msg, files, large) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 188 | ps_files = [{'file': '/COMMIT_MSG', |
| 189 | 'type': 'ADDED'}, |
| 190 | {'file': 'README', |
| 191 | 'type': 'MODIFIED'}] |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 192 | for f in files.keys(): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 193 | ps_files.append({'file': f, 'type': 'ADDED'}) |
| 194 | d = {'approvals': [], |
| 195 | 'createdOn': time.time(), |
| 196 | 'files': ps_files, |
| 197 | 'number': str(self.latest_patchset), |
| 198 | 'ref': 'refs/changes/1/%s/%s' % (self.number, |
| 199 | self.latest_patchset), |
| 200 | 'revision': c.hexsha, |
| 201 | 'uploader': {'email': 'user@example.com', |
| 202 | 'name': 'User name', |
| 203 | 'username': 'user'}} |
| 204 | self.data['currentPatchSet'] = d |
| 205 | self.patchsets.append(d) |
| 206 | self.data['submitRecords'] = self.getSubmitRecords() |
| 207 | |
| 208 | def getPatchsetCreatedEvent(self, patchset): |
| 209 | event = {"type": "patchset-created", |
| 210 | "change": {"project": self.project, |
| 211 | "branch": self.branch, |
| 212 | "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af", |
| 213 | "number": str(self.number), |
| 214 | "subject": self.subject, |
| 215 | "owner": {"name": "User Name"}, |
| 216 | "url": "https://hostname/3"}, |
| 217 | "patchSet": self.patchsets[patchset - 1], |
| 218 | "uploader": {"name": "User Name"}} |
| 219 | return event |
| 220 | |
| 221 | def getChangeRestoredEvent(self): |
| 222 | event = {"type": "change-restored", |
| 223 | "change": {"project": self.project, |
| 224 | "branch": self.branch, |
| 225 | "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af", |
| 226 | "number": str(self.number), |
| 227 | "subject": self.subject, |
| 228 | "owner": {"name": "User Name"}, |
| 229 | "url": "https://hostname/3"}, |
| 230 | "restorer": {"name": "User Name"}, |
Antoine Musso | bd86a31 | 2014-01-08 14:51:33 +0100 | [diff] [blame] | 231 | "patchSet": self.patchsets[-1], |
| 232 | "reason": ""} |
| 233 | return event |
| 234 | |
| 235 | def getChangeAbandonedEvent(self): |
| 236 | event = {"type": "change-abandoned", |
| 237 | "change": {"project": self.project, |
| 238 | "branch": self.branch, |
| 239 | "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af", |
| 240 | "number": str(self.number), |
| 241 | "subject": self.subject, |
| 242 | "owner": {"name": "User Name"}, |
| 243 | "url": "https://hostname/3"}, |
| 244 | "abandoner": {"name": "User Name"}, |
| 245 | "patchSet": self.patchsets[-1], |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 246 | "reason": ""} |
| 247 | return event |
| 248 | |
| 249 | def getChangeCommentEvent(self, patchset): |
| 250 | event = {"type": "comment-added", |
| 251 | "change": {"project": self.project, |
| 252 | "branch": self.branch, |
| 253 | "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af", |
| 254 | "number": str(self.number), |
| 255 | "subject": self.subject, |
| 256 | "owner": {"name": "User Name"}, |
| 257 | "url": "https://hostname/3"}, |
| 258 | "patchSet": self.patchsets[patchset - 1], |
| 259 | "author": {"name": "User Name"}, |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 260 | "approvals": [{"type": "code-review", |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 261 | "description": "Code-Review", |
| 262 | "value": "0"}], |
| 263 | "comment": "This is a comment"} |
| 264 | return event |
| 265 | |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 266 | def addApproval(self, category, value, username='reviewer_john', |
| 267 | granted_on=None, message=''): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 268 | if not granted_on: |
| 269 | granted_on = time.time() |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 270 | approval = { |
| 271 | 'description': self.categories[category][0], |
| 272 | 'type': category, |
| 273 | 'value': str(value), |
| 274 | 'by': { |
| 275 | 'username': username, |
| 276 | 'email': username + '@example.com', |
| 277 | }, |
| 278 | 'grantedOn': int(granted_on) |
| 279 | } |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 280 | for i, x in enumerate(self.patchsets[-1]['approvals'][:]): |
| 281 | if x['by']['username'] == username and x['type'] == category: |
| 282 | del self.patchsets[-1]['approvals'][i] |
| 283 | self.patchsets[-1]['approvals'].append(approval) |
| 284 | event = {'approvals': [approval], |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 285 | 'author': {'email': 'author@example.com', |
| 286 | 'name': 'Patchset Author', |
| 287 | 'username': 'author_phil'}, |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 288 | 'change': {'branch': self.branch, |
| 289 | 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87', |
| 290 | 'number': str(self.number), |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 291 | 'owner': {'email': 'owner@example.com', |
| 292 | 'name': 'Change Owner', |
| 293 | 'username': 'owner_jane'}, |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 294 | 'project': self.project, |
| 295 | 'subject': self.subject, |
| 296 | 'topic': 'master', |
| 297 | 'url': 'https://hostname/459'}, |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 298 | 'comment': message, |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 299 | 'patchSet': self.patchsets[-1], |
| 300 | 'type': 'comment-added'} |
| 301 | self.data['submitRecords'] = self.getSubmitRecords() |
| 302 | return json.loads(json.dumps(event)) |
| 303 | |
| 304 | def getSubmitRecords(self): |
| 305 | status = {} |
| 306 | for cat in self.categories.keys(): |
| 307 | status[cat] = 0 |
| 308 | |
| 309 | for a in self.patchsets[-1]['approvals']: |
| 310 | cur = status[a['type']] |
| 311 | cat_min, cat_max = self.categories[a['type']][1:] |
| 312 | new = int(a['value']) |
| 313 | if new == cat_min: |
| 314 | cur = new |
| 315 | elif abs(new) > abs(cur): |
| 316 | cur = new |
| 317 | status[a['type']] = cur |
| 318 | |
| 319 | labels = [] |
| 320 | ok = True |
| 321 | for typ, cat in self.categories.items(): |
| 322 | cur = status[typ] |
| 323 | cat_min, cat_max = cat[1:] |
| 324 | if cur == cat_min: |
| 325 | value = 'REJECT' |
| 326 | ok = False |
| 327 | elif cur == cat_max: |
| 328 | value = 'OK' |
| 329 | else: |
| 330 | value = 'NEED' |
| 331 | ok = False |
| 332 | labels.append({'label': cat[0], 'status': value}) |
| 333 | if ok: |
| 334 | return [{'status': 'OK'}] |
| 335 | return [{'status': 'NOT_READY', |
| 336 | 'labels': labels}] |
| 337 | |
| 338 | def setDependsOn(self, other, patchset): |
| 339 | self.depends_on_change = other |
| 340 | d = {'id': other.data['id'], |
| 341 | 'number': other.data['number'], |
| 342 | 'ref': other.patchsets[patchset - 1]['ref'] |
| 343 | } |
| 344 | self.data['dependsOn'] = [d] |
| 345 | |
| 346 | other.needed_by_changes.append(self) |
| 347 | needed = other.data.get('neededBy', []) |
| 348 | d = {'id': self.data['id'], |
| 349 | 'number': self.data['number'], |
| 350 | 'ref': self.patchsets[patchset - 1]['ref'], |
| 351 | 'revision': self.patchsets[patchset - 1]['revision'] |
| 352 | } |
| 353 | needed.append(d) |
| 354 | other.data['neededBy'] = needed |
| 355 | |
| 356 | def query(self): |
| 357 | self.queried += 1 |
| 358 | d = self.data.get('dependsOn') |
| 359 | if d: |
| 360 | d = d[0] |
| 361 | if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']): |
| 362 | d['isCurrentPatchSet'] = True |
| 363 | else: |
| 364 | d['isCurrentPatchSet'] = False |
| 365 | return json.loads(json.dumps(self.data)) |
| 366 | |
| 367 | def setMerged(self): |
| 368 | if (self.depends_on_change and |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 369 | self.depends_on_change.data['status'] != 'MERGED'): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 370 | return |
| 371 | if self.fail_merge: |
| 372 | return |
| 373 | self.data['status'] = 'MERGED' |
| 374 | self.open = False |
| 375 | |
| 376 | path = os.path.join(self.upstream_root, self.project) |
| 377 | repo = git.Repo(path) |
| 378 | repo.heads[self.branch].commit = \ |
| 379 | repo.commit(self.patchsets[-1]['revision']) |
| 380 | |
| 381 | def setReported(self): |
| 382 | self.reported += 1 |
| 383 | |
| 384 | |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 385 | class FakeGerritConnection(gerritconnection.GerritConnection): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 386 | """A Fake Gerrit connection for use in tests. |
| 387 | |
| 388 | This subclasses |
| 389 | :py:class:`~zuul.connection.gerrit.GerritConnection` to add the |
| 390 | ability for tests to add changes to the fake Gerrit it represents. |
| 391 | """ |
| 392 | |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 393 | log = logging.getLogger("zuul.test.FakeGerritConnection") |
James E. Blair | 96698e2 | 2015-04-02 07:48:21 -0700 | [diff] [blame] | 394 | |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 395 | def __init__(self, driver, connection_name, connection_config, |
James E. Blair | 7fc8daa | 2016-08-08 15:37:15 -0700 | [diff] [blame] | 396 | changes_db=None, upstream_root=None): |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 397 | super(FakeGerritConnection, self).__init__(driver, connection_name, |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 398 | connection_config) |
| 399 | |
James E. Blair | 7fc8daa | 2016-08-08 15:37:15 -0700 | [diff] [blame] | 400 | self.event_queue = Queue.Queue() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 401 | self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit') |
| 402 | self.change_number = 0 |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 403 | self.changes = changes_db |
James E. Blair | f8ff993 | 2014-08-15 15:24:24 -0700 | [diff] [blame] | 404 | self.queries = [] |
Jan Hruban | 6b71aff | 2015-10-22 16:58:08 +0200 | [diff] [blame] | 405 | self.upstream_root = upstream_root |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 406 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 407 | def addFakeChange(self, project, branch, subject, status='NEW', |
| 408 | files=None): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 409 | """Add a change to the fake Gerrit.""" |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 410 | self.change_number += 1 |
| 411 | c = FakeChange(self, self.change_number, project, branch, subject, |
| 412 | upstream_root=self.upstream_root, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 413 | status=status, files=files) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 414 | self.changes[self.change_number] = c |
| 415 | return c |
| 416 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 417 | def review(self, project, changeid, message, action): |
| 418 | number, ps = changeid.split(',') |
| 419 | change = self.changes[int(number)] |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 420 | |
| 421 | # Add the approval back onto the change (ie simulate what gerrit would |
| 422 | # do). |
| 423 | # Usually when zuul leaves a review it'll create a feedback loop where |
| 424 | # zuul's review enters another gerrit event (which is then picked up by |
| 425 | # zuul). However, we can't mimic this behaviour (by adding this |
| 426 | # approval event into the queue) as it stops jobs from checking what |
| 427 | # happens before this event is triggered. If a job needs to see what |
| 428 | # happens they can add their own verified event into the queue. |
| 429 | # Nevertheless, we can update change with the new review in gerrit. |
| 430 | |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 431 | for cat in action.keys(): |
| 432 | if cat != 'submit': |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 433 | change.addApproval(cat, action[cat], username=self.user) |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 434 | |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 435 | # TODOv3(jeblair): can this be removed? |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 436 | if 'label' in action: |
| 437 | parts = action['label'].split('=') |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 438 | change.addApproval(parts[0], parts[2], username=self.user) |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 439 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 440 | change.messages.append(message) |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 441 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 442 | if 'submit' in action: |
| 443 | change.setMerged() |
| 444 | if message: |
| 445 | change.setReported() |
| 446 | |
| 447 | def query(self, number): |
| 448 | change = self.changes.get(int(number)) |
| 449 | if change: |
| 450 | return change.query() |
| 451 | return {} |
| 452 | |
James E. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 453 | def simpleQuery(self, query): |
James E. Blair | 96698e2 | 2015-04-02 07:48:21 -0700 | [diff] [blame] | 454 | self.log.debug("simpleQuery: %s" % query) |
James E. Blair | f8ff993 | 2014-08-15 15:24:24 -0700 | [diff] [blame] | 455 | self.queries.append(query) |
James E. Blair | 5ee2425 | 2014-12-30 10:12:29 -0800 | [diff] [blame] | 456 | if query.startswith('change:'): |
| 457 | # Query a specific changeid |
| 458 | changeid = query[len('change:'):] |
| 459 | l = [change.query() for change in self.changes.values() |
| 460 | if change.data['id'] == changeid] |
James E. Blair | 96698e2 | 2015-04-02 07:48:21 -0700 | [diff] [blame] | 461 | elif query.startswith('message:'): |
| 462 | # Query the content of a commit message |
| 463 | msg = query[len('message:'):].strip() |
| 464 | l = [change.query() for change in self.changes.values() |
| 465 | if msg in change.data['commitMessage']] |
James E. Blair | 5ee2425 | 2014-12-30 10:12:29 -0800 | [diff] [blame] | 466 | else: |
| 467 | # Query all open changes |
| 468 | l = [change.query() for change in self.changes.values()] |
James E. Blair | f8ff993 | 2014-08-15 15:24:24 -0700 | [diff] [blame] | 469 | return l |
James E. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 470 | |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 471 | def _start_watcher_thread(self, *args, **kw): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 472 | pass |
| 473 | |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 474 | def getGitUrl(self, project): |
| 475 | return os.path.join(self.upstream_root, project.name) |
| 476 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 477 | |
| 478 | class BuildHistory(object): |
| 479 | def __init__(self, **kw): |
| 480 | self.__dict__.update(kw) |
| 481 | |
| 482 | def __repr__(self): |
James E. Blair | 1730297 | 2016-08-10 16:11:42 -0700 | [diff] [blame] | 483 | return ("<Completed build, result: %s name: %s uuid: %s changes: %s>" % |
| 484 | (self.result, self.name, self.uuid, self.changes)) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 485 | |
| 486 | |
| 487 | class FakeURLOpener(object): |
Jan Hruban | 6b71aff | 2015-10-22 16:58:08 +0200 | [diff] [blame] | 488 | def __init__(self, upstream_root, url): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 489 | self.upstream_root = upstream_root |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 490 | self.url = url |
| 491 | |
| 492 | def read(self): |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 493 | res = urllib.parse.urlparse(self.url) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 494 | path = res.path |
| 495 | project = '/'.join(path.split('/')[2:-2]) |
| 496 | ret = '001e# service=git-upload-pack\n' |
| 497 | ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00' |
| 498 | 'multi_ack thin-pack side-band side-band-64k ofs-delta ' |
| 499 | 'shallow no-progress include-tag multi_ack_detailed no-done\n') |
| 500 | path = os.path.join(self.upstream_root, project) |
| 501 | repo = git.Repo(path) |
| 502 | for ref in repo.refs: |
| 503 | r = ref.object.hexsha + ' ' + ref.path + '\n' |
| 504 | ret += '%04x%s' % (len(r) + 4, r) |
| 505 | ret += '0000' |
| 506 | return ret |
| 507 | |
| 508 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 509 | class FakeStatsd(threading.Thread): |
| 510 | def __init__(self): |
| 511 | threading.Thread.__init__(self) |
| 512 | self.daemon = True |
| 513 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 514 | self.sock.bind(('', 0)) |
| 515 | self.port = self.sock.getsockname()[1] |
| 516 | self.wake_read, self.wake_write = os.pipe() |
| 517 | self.stats = [] |
| 518 | |
| 519 | def run(self): |
| 520 | while True: |
| 521 | poll = select.poll() |
| 522 | poll.register(self.sock, select.POLLIN) |
| 523 | poll.register(self.wake_read, select.POLLIN) |
| 524 | ret = poll.poll() |
| 525 | for (fd, event) in ret: |
| 526 | if fd == self.sock.fileno(): |
| 527 | data = self.sock.recvfrom(1024) |
| 528 | if not data: |
| 529 | return |
| 530 | self.stats.append(data[0]) |
| 531 | if fd == self.wake_read: |
| 532 | return |
| 533 | |
| 534 | def stop(self): |
| 535 | os.write(self.wake_write, '1\n') |
| 536 | |
| 537 | |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 538 | class FakeBuild(object): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 539 | log = logging.getLogger("zuul.test") |
| 540 | |
James E. Blair | 34776ee | 2016-08-25 13:53:54 -0700 | [diff] [blame] | 541 | def __init__(self, launch_server, job): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 542 | self.daemon = True |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 543 | self.launch_server = launch_server |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 544 | self.job = job |
James E. Blair | ab7132b | 2016-08-05 12:36:22 -0700 | [diff] [blame] | 545 | self.jobdir = None |
James E. Blair | 1730297 | 2016-08-10 16:11:42 -0700 | [diff] [blame] | 546 | self.uuid = job.unique |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 547 | self.parameters = json.loads(job.arguments) |
James E. Blair | 34776ee | 2016-08-25 13:53:54 -0700 | [diff] [blame] | 548 | # TODOv3(jeblair): self.node is really "the image of the node |
| 549 | # assigned". We should rename it (self.node_image?) if we |
| 550 | # keep using it like this, or we may end up exposing more of |
| 551 | # the complexity around multi-node jobs here |
| 552 | # (self.nodes[0].image?) |
| 553 | self.node = None |
| 554 | if len(self.parameters.get('nodes')) == 1: |
| 555 | self.node = self.parameters['nodes'][0]['image'] |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 556 | self.unique = self.parameters['ZUUL_UUID'] |
Jamie Lennox | d2e3733 | 2016-12-05 15:26:19 +1100 | [diff] [blame] | 557 | self.pipeline = self.parameters['ZUUL_PIPELINE'] |
| 558 | self.project = self.parameters['ZUUL_PROJECT'] |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 559 | self.name = self.parameters['job'] |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 560 | self.wait_condition = threading.Condition() |
| 561 | self.waiting = False |
| 562 | self.aborted = False |
Paul Belanger | 71d9817 | 2016-11-08 10:56:31 -0500 | [diff] [blame] | 563 | self.requeue = False |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 564 | self.created = time.time() |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 565 | self.changes = None |
| 566 | if 'ZUUL_CHANGE_IDS' in self.parameters: |
| 567 | self.changes = self.parameters['ZUUL_CHANGE_IDS'] |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 568 | |
James E. Blair | 3158e28 | 2016-08-19 09:34:11 -0700 | [diff] [blame] | 569 | def __repr__(self): |
| 570 | waiting = '' |
| 571 | if self.waiting: |
| 572 | waiting = ' [waiting]' |
Jamie Lennox | d2e3733 | 2016-12-05 15:26:19 +1100 | [diff] [blame] | 573 | return '<FakeBuild %s:%s %s%s>' % (self.pipeline, self.name, |
| 574 | self.changes, waiting) |
James E. Blair | 3158e28 | 2016-08-19 09:34:11 -0700 | [diff] [blame] | 575 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 576 | def release(self): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 577 | """Release this build.""" |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 578 | self.wait_condition.acquire() |
| 579 | self.wait_condition.notify() |
| 580 | self.waiting = False |
| 581 | self.log.debug("Build %s released" % self.unique) |
| 582 | self.wait_condition.release() |
| 583 | |
| 584 | def isWaiting(self): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 585 | """Return whether this build is being held. |
| 586 | |
| 587 | :returns: Whether the build is being held. |
| 588 | :rtype: bool |
| 589 | """ |
| 590 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 591 | self.wait_condition.acquire() |
| 592 | if self.waiting: |
| 593 | ret = True |
| 594 | else: |
| 595 | ret = False |
| 596 | self.wait_condition.release() |
| 597 | return ret |
| 598 | |
| 599 | def _wait(self): |
| 600 | self.wait_condition.acquire() |
| 601 | self.waiting = True |
| 602 | self.log.debug("Build %s waiting" % self.unique) |
| 603 | self.wait_condition.wait() |
| 604 | self.wait_condition.release() |
| 605 | |
| 606 | def run(self): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 607 | self.log.debug('Running build %s' % self.unique) |
| 608 | |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 609 | if self.launch_server.hold_jobs_in_build: |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 610 | self.log.debug('Holding build %s' % self.unique) |
| 611 | self._wait() |
| 612 | self.log.debug("Build %s continuing" % self.unique) |
| 613 | |
James E. Blair | 412fba8 | 2017-01-26 15:00:50 -0800 | [diff] [blame] | 614 | result = (RecordingAnsibleJob.RESULT_NORMAL, 0) # Success |
James E. Blair | a5dba23 | 2016-08-08 15:53:24 -0700 | [diff] [blame] | 615 | if (('ZUUL_REF' in self.parameters) and self.shouldFail()): |
James E. Blair | 412fba8 | 2017-01-26 15:00:50 -0800 | [diff] [blame] | 616 | result = (RecordingAnsibleJob.RESULT_NORMAL, 1) # Failure |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 617 | if self.aborted: |
James E. Blair | 412fba8 | 2017-01-26 15:00:50 -0800 | [diff] [blame] | 618 | result = (RecordingAnsibleJob.RESULT_ABORTED, None) |
Paul Belanger | 71d9817 | 2016-11-08 10:56:31 -0500 | [diff] [blame] | 619 | if self.requeue: |
James E. Blair | 412fba8 | 2017-01-26 15:00:50 -0800 | [diff] [blame] | 620 | result = (RecordingAnsibleJob.RESULT_UNREACHABLE, None) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 621 | |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 622 | return result |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 623 | |
James E. Blair | a5dba23 | 2016-08-08 15:53:24 -0700 | [diff] [blame] | 624 | def shouldFail(self): |
| 625 | changes = self.launch_server.fail_tests.get(self.name, []) |
| 626 | for change in changes: |
| 627 | if self.hasChanges(change): |
| 628 | return True |
| 629 | return False |
| 630 | |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 631 | def hasChanges(self, *changes): |
| 632 | """Return whether this build has certain changes in its git repos. |
| 633 | |
| 634 | :arg FakeChange changes: One or more changes (varargs) that |
| 635 | are expected to be present (in order) in the git repository of |
| 636 | the active project. |
| 637 | |
| 638 | :returns: Whether the build has the indicated changes. |
| 639 | :rtype: bool |
| 640 | |
| 641 | """ |
Clint Byrum | 3343e3e | 2016-11-15 16:05:03 -0800 | [diff] [blame] | 642 | for change in changes: |
| 643 | path = os.path.join(self.jobdir.git_root, change.project) |
| 644 | try: |
| 645 | repo = git.Repo(path) |
| 646 | except NoSuchPathError as e: |
| 647 | self.log.debug('%s' % e) |
| 648 | return False |
| 649 | ref = self.parameters['ZUUL_REF'] |
| 650 | repo_messages = [c.message.strip() for c in repo.iter_commits(ref)] |
| 651 | commit_message = '%s-1' % change.subject |
| 652 | self.log.debug("Checking if build %s has changes; commit_message " |
| 653 | "%s; repo_messages %s" % (self, commit_message, |
| 654 | repo_messages)) |
| 655 | if commit_message not in repo_messages: |
James E. Blair | 962220f | 2016-08-03 11:22:38 -0700 | [diff] [blame] | 656 | self.log.debug(" messages do not match") |
| 657 | return False |
| 658 | self.log.debug(" OK") |
| 659 | return True |
| 660 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 661 | |
Joshua Hesketh | 0c54b2a | 2016-04-11 21:23:33 +1000 | [diff] [blame] | 662 | class RecordingLaunchServer(zuul.launcher.server.LaunchServer): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 663 | """An Ansible launcher to be used in tests. |
| 664 | |
| 665 | :ivar bool hold_jobs_in_build: If true, when jobs are launched |
| 666 | they will report that they have started but then pause until |
| 667 | released before reporting completion. This attribute may be |
| 668 | changed at any time and will take effect for subsequently |
| 669 | launched builds, but previously held builds will still need to |
| 670 | be explicitly released. |
| 671 | |
| 672 | """ |
James E. Blair | f5dbd00 | 2015-12-23 15:26:17 -0800 | [diff] [blame] | 673 | def __init__(self, *args, **kw): |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 674 | self._run_ansible = kw.pop('_run_ansible', False) |
James E. Blair | a92cbc8 | 2017-01-23 14:56:49 -0800 | [diff] [blame] | 675 | self._test_root = kw.pop('_test_root', False) |
James E. Blair | f5dbd00 | 2015-12-23 15:26:17 -0800 | [diff] [blame] | 676 | super(RecordingLaunchServer, self).__init__(*args, **kw) |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 677 | self.hold_jobs_in_build = False |
| 678 | self.lock = threading.Lock() |
| 679 | self.running_builds = [] |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 680 | self.build_history = [] |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 681 | self.fail_tests = {} |
James E. Blair | ab7132b | 2016-08-05 12:36:22 -0700 | [diff] [blame] | 682 | self.job_builds = {} |
James E. Blair | f5dbd00 | 2015-12-23 15:26:17 -0800 | [diff] [blame] | 683 | |
James E. Blair | a5dba23 | 2016-08-08 15:53:24 -0700 | [diff] [blame] | 684 | def failJob(self, name, change): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 685 | """Instruct the launcher to report matching builds as failures. |
| 686 | |
| 687 | :arg str name: The name of the job to fail. |
James E. Blair | a5dba23 | 2016-08-08 15:53:24 -0700 | [diff] [blame] | 688 | :arg Change change: The :py:class:`~tests.base.FakeChange` |
| 689 | instance which should cause the job to fail. This job |
| 690 | will also fail for changes depending on this change. |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 691 | |
| 692 | """ |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 693 | l = self.fail_tests.get(name, []) |
| 694 | l.append(change) |
| 695 | self.fail_tests[name] = l |
James E. Blair | f5dbd00 | 2015-12-23 15:26:17 -0800 | [diff] [blame] | 696 | |
James E. Blair | 962220f | 2016-08-03 11:22:38 -0700 | [diff] [blame] | 697 | def release(self, regex=None): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 698 | """Release a held build. |
| 699 | |
| 700 | :arg str regex: A regular expression which, if supplied, will |
| 701 | cause only builds with matching names to be released. If |
| 702 | not supplied, all builds will be released. |
| 703 | |
| 704 | """ |
James E. Blair | 962220f | 2016-08-03 11:22:38 -0700 | [diff] [blame] | 705 | builds = self.running_builds[:] |
| 706 | self.log.debug("Releasing build %s (%s)" % (regex, |
| 707 | len(self.running_builds))) |
| 708 | for build in builds: |
| 709 | if not regex or re.match(regex, build.name): |
| 710 | self.log.debug("Releasing build %s" % |
| 711 | (build.parameters['ZUUL_UUID'])) |
| 712 | build.release() |
| 713 | else: |
| 714 | self.log.debug("Not releasing build %s" % |
| 715 | (build.parameters['ZUUL_UUID'])) |
| 716 | self.log.debug("Done releasing builds %s (%s)" % |
| 717 | (regex, len(self.running_builds))) |
| 718 | |
James E. Blair | 1730297 | 2016-08-10 16:11:42 -0700 | [diff] [blame] | 719 | def launchJob(self, job): |
James E. Blair | 34776ee | 2016-08-25 13:53:54 -0700 | [diff] [blame] | 720 | build = FakeBuild(self, job) |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 721 | job.build = build |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 722 | self.running_builds.append(build) |
James E. Blair | ab7132b | 2016-08-05 12:36:22 -0700 | [diff] [blame] | 723 | self.job_builds[job.unique] = build |
James E. Blair | a92cbc8 | 2017-01-23 14:56:49 -0800 | [diff] [blame] | 724 | args = json.loads(job.arguments) |
| 725 | args['zuul']['_test'] = dict(test_root=self._test_root) |
| 726 | job.arguments = json.dumps(args) |
Joshua Hesketh | 50c2178 | 2016-10-13 21:34:14 +1100 | [diff] [blame] | 727 | self.job_workers[job.unique] = RecordingAnsibleJob(self, job) |
| 728 | self.job_workers[job.unique].run() |
James E. Blair | 1730297 | 2016-08-10 16:11:42 -0700 | [diff] [blame] | 729 | |
| 730 | def stopJob(self, job): |
| 731 | self.log.debug("handle stop") |
| 732 | parameters = json.loads(job.arguments) |
| 733 | uuid = parameters['uuid'] |
| 734 | for build in self.running_builds: |
| 735 | if build.unique == uuid: |
| 736 | build.aborted = True |
| 737 | build.release() |
| 738 | super(RecordingLaunchServer, self).stopJob(job) |
James E. Blair | ab7132b | 2016-08-05 12:36:22 -0700 | [diff] [blame] | 739 | |
Joshua Hesketh | 50c2178 | 2016-10-13 21:34:14 +1100 | [diff] [blame] | 740 | |
| 741 | class RecordingAnsibleJob(zuul.launcher.server.AnsibleJob): |
James E. Blair | 412fba8 | 2017-01-26 15:00:50 -0800 | [diff] [blame] | 742 | def runPlaybooks(self): |
Joshua Hesketh | 50c2178 | 2016-10-13 21:34:14 +1100 | [diff] [blame] | 743 | build = self.launcher_server.job_builds[self.job.unique] |
James E. Blair | 412fba8 | 2017-01-26 15:00:50 -0800 | [diff] [blame] | 744 | build.jobdir = self.jobdir |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 745 | |
James E. Blair | 412fba8 | 2017-01-26 15:00:50 -0800 | [diff] [blame] | 746 | result = super(RecordingAnsibleJob, self).runPlaybooks() |
| 747 | |
Joshua Hesketh | 50c2178 | 2016-10-13 21:34:14 +1100 | [diff] [blame] | 748 | self.launcher_server.lock.acquire() |
| 749 | self.launcher_server.build_history.append( |
James E. Blair | 1730297 | 2016-08-10 16:11:42 -0700 | [diff] [blame] | 750 | BuildHistory(name=build.name, result=result, changes=build.changes, |
| 751 | node=build.node, uuid=build.unique, |
| 752 | parameters=build.parameters, |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 753 | pipeline=build.parameters['ZUUL_PIPELINE']) |
| 754 | ) |
Joshua Hesketh | 50c2178 | 2016-10-13 21:34:14 +1100 | [diff] [blame] | 755 | self.launcher_server.running_builds.remove(build) |
| 756 | del self.launcher_server.job_builds[self.job.unique] |
| 757 | self.launcher_server.lock.release() |
James E. Blair | 412fba8 | 2017-01-26 15:00:50 -0800 | [diff] [blame] | 758 | return result |
| 759 | |
Monty Taylor | c231d93 | 2017-02-03 09:57:15 -0600 | [diff] [blame] | 760 | def runAnsible(self, cmd, timeout, secure=False): |
James E. Blair | 412fba8 | 2017-01-26 15:00:50 -0800 | [diff] [blame] | 761 | build = self.launcher_server.job_builds[self.job.unique] |
| 762 | |
| 763 | if self.launcher_server._run_ansible: |
Monty Taylor | c231d93 | 2017-02-03 09:57:15 -0600 | [diff] [blame] | 764 | result = super(RecordingAnsibleJob, self).runAnsible( |
| 765 | cmd, timeout, secure=secure) |
James E. Blair | 412fba8 | 2017-01-26 15:00:50 -0800 | [diff] [blame] | 766 | else: |
| 767 | result = build.run() |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 768 | return result |
James E. Blair | f5dbd00 | 2015-12-23 15:26:17 -0800 | [diff] [blame] | 769 | |
| 770 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 771 | class FakeGearmanServer(gear.Server): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 772 | """A Gearman server for use in tests. |
| 773 | |
| 774 | :ivar bool hold_jobs_in_queue: If true, submitted jobs will be |
| 775 | added to the queue but will not be distributed to workers |
| 776 | until released. This attribute may be changed at any time and |
| 777 | will take effect for subsequently enqueued jobs, but |
| 778 | previously held jobs will still need to be explicitly |
| 779 | released. |
| 780 | |
| 781 | """ |
| 782 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 783 | def __init__(self): |
| 784 | self.hold_jobs_in_queue = False |
| 785 | super(FakeGearmanServer, self).__init__(0) |
| 786 | |
| 787 | def getJobForConnection(self, connection, peek=False): |
| 788 | for queue in [self.high_queue, self.normal_queue, self.low_queue]: |
| 789 | for job in queue: |
| 790 | if not hasattr(job, 'waiting'): |
Paul Belanger | 6ab6af7 | 2016-11-06 11:32:59 -0500 | [diff] [blame] | 791 | if job.name.startswith('launcher:launch'): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 792 | job.waiting = self.hold_jobs_in_queue |
| 793 | else: |
| 794 | job.waiting = False |
| 795 | if job.waiting: |
| 796 | continue |
| 797 | if job.name in connection.functions: |
| 798 | if not peek: |
| 799 | queue.remove(job) |
| 800 | connection.related_jobs[job.handle] = job |
| 801 | job.worker_connection = connection |
| 802 | job.running = True |
| 803 | return job |
| 804 | return None |
| 805 | |
| 806 | def release(self, regex=None): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 807 | """Release a held job. |
| 808 | |
| 809 | :arg str regex: A regular expression which, if supplied, will |
| 810 | cause only jobs with matching names to be released. If |
| 811 | not supplied, all jobs will be released. |
| 812 | """ |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 813 | released = False |
| 814 | qlen = (len(self.high_queue) + len(self.normal_queue) + |
| 815 | len(self.low_queue)) |
| 816 | self.log.debug("releasing queued job %s (%s)" % (regex, qlen)) |
| 817 | for job in self.getQueue(): |
Paul Belanger | 6ab6af7 | 2016-11-06 11:32:59 -0500 | [diff] [blame] | 818 | if job.name != 'launcher:launch': |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 819 | continue |
Paul Belanger | 6ab6af7 | 2016-11-06 11:32:59 -0500 | [diff] [blame] | 820 | parameters = json.loads(job.arguments) |
| 821 | if not regex or re.match(regex, parameters.get('job')): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 822 | self.log.debug("releasing queued job %s" % |
| 823 | job.unique) |
| 824 | job.waiting = False |
| 825 | released = True |
| 826 | else: |
| 827 | self.log.debug("not releasing queued job %s" % |
| 828 | job.unique) |
| 829 | if released: |
| 830 | self.wakeConnections() |
| 831 | qlen = (len(self.high_queue) + len(self.normal_queue) + |
| 832 | len(self.low_queue)) |
| 833 | self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen)) |
| 834 | |
| 835 | |
| 836 | class FakeSMTP(object): |
| 837 | log = logging.getLogger('zuul.FakeSMTP') |
| 838 | |
| 839 | def __init__(self, messages, server, port): |
| 840 | self.server = server |
| 841 | self.port = port |
| 842 | self.messages = messages |
| 843 | |
| 844 | def sendmail(self, from_email, to_email, msg): |
| 845 | self.log.info("Sending email from %s, to %s, with msg %s" % ( |
| 846 | from_email, to_email, msg)) |
| 847 | |
| 848 | headers = msg.split('\n\n', 1)[0] |
| 849 | body = msg.split('\n\n', 1)[1] |
| 850 | |
| 851 | self.messages.append(dict( |
| 852 | from_email=from_email, |
| 853 | to_email=to_email, |
| 854 | msg=msg, |
| 855 | headers=headers, |
| 856 | body=body, |
| 857 | )) |
| 858 | |
| 859 | return True |
| 860 | |
| 861 | def quit(self): |
| 862 | return True |
| 863 | |
| 864 | |
| 865 | class FakeSwiftClientConnection(swiftclient.client.Connection): |
| 866 | def post_account(self, headers): |
| 867 | # Do nothing |
| 868 | pass |
| 869 | |
| 870 | def get_auth(self): |
| 871 | # Returns endpoint and (unused) auth token |
| 872 | endpoint = os.path.join('https://storage.example.org', 'V1', |
| 873 | 'AUTH_account') |
| 874 | return endpoint, '' |
| 875 | |
| 876 | |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 877 | class FakeNodepool(object): |
| 878 | REQUEST_ROOT = '/nodepool/requests' |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 879 | NODE_ROOT = '/nodepool/nodes' |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 880 | |
| 881 | log = logging.getLogger("zuul.test.FakeNodepool") |
| 882 | |
| 883 | def __init__(self, host, port, chroot): |
| 884 | self.client = kazoo.client.KazooClient( |
| 885 | hosts='%s:%s%s' % (host, port, chroot)) |
| 886 | self.client.start() |
| 887 | self._running = True |
James E. Blair | 15be0e1 | 2017-01-03 13:45:20 -0800 | [diff] [blame] | 888 | self.paused = False |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 889 | self.thread = threading.Thread(target=self.run) |
| 890 | self.thread.daemon = True |
| 891 | self.thread.start() |
James E. Blair | 6ab79e0 | 2017-01-06 10:10:17 -0800 | [diff] [blame] | 892 | self.fail_requests = set() |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 893 | |
| 894 | def stop(self): |
| 895 | self._running = False |
| 896 | self.thread.join() |
| 897 | self.client.stop() |
| 898 | self.client.close() |
| 899 | |
| 900 | def run(self): |
| 901 | while self._running: |
| 902 | self._run() |
| 903 | time.sleep(0.1) |
| 904 | |
| 905 | def _run(self): |
James E. Blair | 15be0e1 | 2017-01-03 13:45:20 -0800 | [diff] [blame] | 906 | if self.paused: |
| 907 | return |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 908 | for req in self.getNodeRequests(): |
| 909 | self.fulfillRequest(req) |
| 910 | |
| 911 | def getNodeRequests(self): |
| 912 | try: |
| 913 | reqids = self.client.get_children(self.REQUEST_ROOT) |
| 914 | except kazoo.exceptions.NoNodeError: |
| 915 | return [] |
| 916 | reqs = [] |
| 917 | for oid in sorted(reqids): |
| 918 | path = self.REQUEST_ROOT + '/' + oid |
James E. Blair | 0ef64f8 | 2017-02-02 11:25:16 -0800 | [diff] [blame] | 919 | try: |
| 920 | data, stat = self.client.get(path) |
| 921 | data = json.loads(data) |
| 922 | data['_oid'] = oid |
| 923 | reqs.append(data) |
| 924 | except kazoo.exceptions.NoNodeError: |
| 925 | pass |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 926 | return reqs |
| 927 | |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 928 | def getNodes(self): |
| 929 | try: |
| 930 | nodeids = self.client.get_children(self.NODE_ROOT) |
| 931 | except kazoo.exceptions.NoNodeError: |
| 932 | return [] |
| 933 | nodes = [] |
| 934 | for oid in sorted(nodeids): |
| 935 | path = self.NODE_ROOT + '/' + oid |
| 936 | data, stat = self.client.get(path) |
| 937 | data = json.loads(data) |
| 938 | data['_oid'] = oid |
| 939 | try: |
| 940 | lockfiles = self.client.get_children(path + '/lock') |
| 941 | except kazoo.exceptions.NoNodeError: |
| 942 | lockfiles = [] |
| 943 | if lockfiles: |
| 944 | data['_lock'] = True |
| 945 | else: |
| 946 | data['_lock'] = False |
| 947 | nodes.append(data) |
| 948 | return nodes |
| 949 | |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 950 | def makeNode(self, request_id, node_type): |
| 951 | now = time.time() |
| 952 | path = '/nodepool/nodes/' |
| 953 | data = dict(type=node_type, |
| 954 | provider='test-provider', |
| 955 | region='test-region', |
| 956 | az=None, |
| 957 | public_ipv4='127.0.0.1', |
| 958 | private_ipv4=None, |
| 959 | public_ipv6=None, |
| 960 | allocated_to=request_id, |
| 961 | state='ready', |
| 962 | state_time=now, |
| 963 | created_time=now, |
| 964 | updated_time=now, |
| 965 | image_id=None, |
| 966 | launcher='fake-nodepool') |
| 967 | data = json.dumps(data) |
| 968 | path = self.client.create(path, data, |
| 969 | makepath=True, |
| 970 | sequence=True) |
| 971 | nodeid = path.split("/")[-1] |
| 972 | return nodeid |
| 973 | |
James E. Blair | 6ab79e0 | 2017-01-06 10:10:17 -0800 | [diff] [blame] | 974 | def addFailRequest(self, request): |
| 975 | self.fail_requests.add(request['_oid']) |
| 976 | |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 977 | def fulfillRequest(self, request): |
James E. Blair | 6ab79e0 | 2017-01-06 10:10:17 -0800 | [diff] [blame] | 978 | if request['state'] != 'requested': |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 979 | return |
| 980 | request = request.copy() |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 981 | oid = request['_oid'] |
| 982 | del request['_oid'] |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 983 | |
James E. Blair | 6ab79e0 | 2017-01-06 10:10:17 -0800 | [diff] [blame] | 984 | if oid in self.fail_requests: |
| 985 | request['state'] = 'failed' |
| 986 | else: |
| 987 | request['state'] = 'fulfilled' |
| 988 | nodes = [] |
| 989 | for node in request['node_types']: |
| 990 | nodeid = self.makeNode(oid, node) |
| 991 | nodes.append(nodeid) |
| 992 | request['nodes'] = nodes |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 993 | |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 994 | request['state_time'] = time.time() |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 995 | path = self.REQUEST_ROOT + '/' + oid |
| 996 | data = json.dumps(request) |
| 997 | self.log.debug("Fulfilling node request: %s %s" % (oid, data)) |
| 998 | self.client.set(path, data) |
| 999 | |
| 1000 | |
James E. Blair | 498059b | 2016-12-20 13:50:13 -0800 | [diff] [blame] | 1001 | class ChrootedKazooFixture(fixtures.Fixture): |
| 1002 | def __init__(self): |
| 1003 | super(ChrootedKazooFixture, self).__init__() |
| 1004 | |
| 1005 | zk_host = os.environ.get('NODEPOOL_ZK_HOST', 'localhost') |
| 1006 | if ':' in zk_host: |
| 1007 | host, port = zk_host.split(':') |
| 1008 | else: |
| 1009 | host = zk_host |
| 1010 | port = None |
| 1011 | |
| 1012 | self.zookeeper_host = host |
| 1013 | |
| 1014 | if not port: |
| 1015 | self.zookeeper_port = 2181 |
| 1016 | else: |
| 1017 | self.zookeeper_port = int(port) |
| 1018 | |
| 1019 | def _setUp(self): |
| 1020 | # Make sure the test chroot paths do not conflict |
| 1021 | random_bits = ''.join(random.choice(string.ascii_lowercase + |
| 1022 | string.ascii_uppercase) |
| 1023 | for x in range(8)) |
| 1024 | |
| 1025 | rand_test_path = '%s_%s' % (random_bits, os.getpid()) |
| 1026 | self.zookeeper_chroot = "/nodepool_test/%s" % rand_test_path |
| 1027 | |
| 1028 | # Ensure the chroot path exists and clean up any pre-existing znodes. |
| 1029 | _tmp_client = kazoo.client.KazooClient( |
| 1030 | hosts='%s:%s' % (self.zookeeper_host, self.zookeeper_port)) |
| 1031 | _tmp_client.start() |
| 1032 | |
| 1033 | if _tmp_client.exists(self.zookeeper_chroot): |
| 1034 | _tmp_client.delete(self.zookeeper_chroot, recursive=True) |
| 1035 | |
| 1036 | _tmp_client.ensure_path(self.zookeeper_chroot) |
| 1037 | _tmp_client.stop() |
| 1038 | _tmp_client.close() |
| 1039 | |
| 1040 | self.addCleanup(self._cleanup) |
| 1041 | |
| 1042 | def _cleanup(self): |
| 1043 | '''Remove the chroot path.''' |
| 1044 | # Need a non-chroot'ed client to remove the chroot path |
| 1045 | _tmp_client = kazoo.client.KazooClient( |
| 1046 | hosts='%s:%s' % (self.zookeeper_host, self.zookeeper_port)) |
| 1047 | _tmp_client.start() |
| 1048 | _tmp_client.delete(self.zookeeper_chroot, recursive=True) |
| 1049 | _tmp_client.stop() |
| 1050 | |
| 1051 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 1052 | class BaseTestCase(testtools.TestCase): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1053 | log = logging.getLogger("zuul.test") |
| 1054 | |
James E. Blair | 1c236df | 2017-02-01 14:07:24 -0800 | [diff] [blame] | 1055 | def attachLogs(self, *args): |
| 1056 | def reader(): |
| 1057 | self._log_stream.seek(0) |
| 1058 | while True: |
| 1059 | x = self._log_stream.read(4096) |
| 1060 | if not x: |
| 1061 | break |
| 1062 | yield x.encode('utf8') |
| 1063 | content = testtools.content.content_from_reader( |
| 1064 | reader, |
| 1065 | testtools.content_type.UTF8_TEXT, |
| 1066 | False) |
| 1067 | self.addDetail('logging', content) |
| 1068 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1069 | def setUp(self): |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 1070 | super(BaseTestCase, self).setUp() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1071 | test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0) |
| 1072 | try: |
| 1073 | test_timeout = int(test_timeout) |
| 1074 | except ValueError: |
| 1075 | # If timeout value is invalid do not set a timeout. |
| 1076 | test_timeout = 0 |
| 1077 | if test_timeout > 0: |
| 1078 | self.useFixture(fixtures.Timeout(test_timeout, gentle=False)) |
| 1079 | |
| 1080 | if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or |
| 1081 | os.environ.get('OS_STDOUT_CAPTURE') == '1'): |
| 1082 | stdout = self.useFixture(fixtures.StringStream('stdout')).stream |
| 1083 | self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) |
| 1084 | if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or |
| 1085 | os.environ.get('OS_STDERR_CAPTURE') == '1'): |
| 1086 | stderr = self.useFixture(fixtures.StringStream('stderr')).stream |
| 1087 | self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) |
| 1088 | if (os.environ.get('OS_LOG_CAPTURE') == 'True' or |
| 1089 | os.environ.get('OS_LOG_CAPTURE') == '1'): |
James E. Blair | 1c236df | 2017-02-01 14:07:24 -0800 | [diff] [blame] | 1090 | self._log_stream = StringIO() |
| 1091 | self.addOnException(self.attachLogs) |
| 1092 | else: |
| 1093 | self._log_stream = sys.stdout |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 1094 | |
James E. Blair | 1c236df | 2017-02-01 14:07:24 -0800 | [diff] [blame] | 1095 | handler = logging.StreamHandler(self._log_stream) |
| 1096 | formatter = logging.Formatter('%(asctime)s %(name)-32s ' |
| 1097 | '%(levelname)-8s %(message)s') |
| 1098 | handler.setFormatter(formatter) |
| 1099 | |
| 1100 | logger = logging.getLogger() |
| 1101 | logger.setLevel(logging.DEBUG) |
| 1102 | logger.addHandler(handler) |
| 1103 | |
| 1104 | # NOTE(notmorgan): Extract logging overrides for specific |
| 1105 | # libraries from the OS_LOG_DEFAULTS env and create loggers |
| 1106 | # for each. This is used to limit the output during test runs |
| 1107 | # from libraries that zuul depends on such as gear. |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1108 | log_defaults_from_env = os.environ.get( |
| 1109 | 'OS_LOG_DEFAULTS', |
James E. Blair | 1c236df | 2017-02-01 14:07:24 -0800 | [diff] [blame] | 1110 | 'git.cmd=INFO,kazoo.client=WARNING,gear=INFO') |
Morgan Fainberg | d34e0b4 | 2016-06-09 19:10:38 -0700 | [diff] [blame] | 1111 | |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1112 | if log_defaults_from_env: |
| 1113 | for default in log_defaults_from_env.split(','): |
| 1114 | try: |
| 1115 | name, level_str = default.split('=', 1) |
| 1116 | level = getattr(logging, level_str, logging.DEBUG) |
James E. Blair | 1c236df | 2017-02-01 14:07:24 -0800 | [diff] [blame] | 1117 | logger = logging.getLogger(name) |
| 1118 | logger.setLevel(level) |
| 1119 | logger.addHandler(handler) |
| 1120 | logger.propagate = False |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1121 | except ValueError: |
| 1122 | # NOTE(notmorgan): Invalid format of the log default, |
| 1123 | # skip and don't try and apply a logger for the |
| 1124 | # specified module |
| 1125 | pass |
Morgan Fainberg | d34e0b4 | 2016-06-09 19:10:38 -0700 | [diff] [blame] | 1126 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 1127 | |
| 1128 | class ZuulTestCase(BaseTestCase): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 1129 | """A test case with a functioning Zuul. |
| 1130 | |
| 1131 | The following class variables are used during test setup and can |
| 1132 | be overidden by subclasses but are effectively read-only once a |
| 1133 | test method starts running: |
| 1134 | |
| 1135 | :cvar str config_file: This points to the main zuul config file |
| 1136 | within the fixtures directory. Subclasses may override this |
| 1137 | to obtain a different behavior. |
| 1138 | |
| 1139 | :cvar str tenant_config_file: This is the tenant config file |
| 1140 | (which specifies from what git repos the configuration should |
| 1141 | be loaded). It defaults to the value specified in |
| 1142 | `config_file` but can be overidden by subclasses to obtain a |
| 1143 | different tenant/project layout while using the standard main |
| 1144 | configuration. |
| 1145 | |
| 1146 | The following are instance variables that are useful within test |
| 1147 | methods: |
| 1148 | |
| 1149 | :ivar FakeGerritConnection fake_<connection>: |
| 1150 | A :py:class:`~tests.base.FakeGerritConnection` will be |
| 1151 | instantiated for each connection present in the config file |
| 1152 | and stored here. For instance, `fake_gerrit` will hold the |
| 1153 | FakeGerritConnection object for a connection named `gerrit`. |
| 1154 | |
| 1155 | :ivar FakeGearmanServer gearman_server: An instance of |
| 1156 | :py:class:`~tests.base.FakeGearmanServer` which is the Gearman |
| 1157 | server that all of the Zuul components in this test use to |
| 1158 | communicate with each other. |
| 1159 | |
| 1160 | :ivar RecordingLaunchServer launch_server: An instance of |
| 1161 | :py:class:`~tests.base.RecordingLaunchServer` which is the |
| 1162 | Ansible launch server used to run jobs for this test. |
| 1163 | |
| 1164 | :ivar list builds: A list of :py:class:`~tests.base.FakeBuild` objects |
| 1165 | representing currently running builds. They are appended to |
| 1166 | the list in the order they are launched, and removed from this |
| 1167 | list upon completion. |
| 1168 | |
| 1169 | :ivar list history: A list of :py:class:`~tests.base.BuildHistory` |
| 1170 | objects representing completed builds. They are appended to |
| 1171 | the list in the order they complete. |
| 1172 | |
| 1173 | """ |
| 1174 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 1175 | config_file = 'zuul.conf' |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1176 | run_ansible = False |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 1177 | |
| 1178 | def _startMerger(self): |
| 1179 | self.merge_server = zuul.merger.server.MergeServer(self.config, |
| 1180 | self.connections) |
| 1181 | self.merge_server.start() |
| 1182 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 1183 | def setUp(self): |
| 1184 | super(ZuulTestCase, self).setUp() |
James E. Blair | 498059b | 2016-12-20 13:50:13 -0800 | [diff] [blame] | 1185 | |
| 1186 | self.setupZK() |
| 1187 | |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 1188 | if USE_TEMPDIR: |
| 1189 | tmp_root = self.useFixture(fixtures.TempDir( |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 1190 | rootdir=os.environ.get("ZUUL_TEST_ROOT")) |
| 1191 | ).path |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 1192 | else: |
| 1193 | tmp_root = os.environ.get("ZUUL_TEST_ROOT") |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1194 | self.test_root = os.path.join(tmp_root, "zuul-test") |
| 1195 | self.upstream_root = os.path.join(self.test_root, "upstream") |
James E. Blair | 8c1be53 | 2017-02-07 14:04:12 -0800 | [diff] [blame] | 1196 | self.merger_git_root = os.path.join(self.test_root, "merger-git") |
| 1197 | self.launcher_git_root = os.path.join(self.test_root, "launcher-git") |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 1198 | self.state_root = os.path.join(self.test_root, "lib") |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1199 | |
| 1200 | if os.path.exists(self.test_root): |
| 1201 | shutil.rmtree(self.test_root) |
| 1202 | os.makedirs(self.test_root) |
| 1203 | os.makedirs(self.upstream_root) |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 1204 | os.makedirs(self.state_root) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1205 | |
| 1206 | # Make per test copy of Configuration. |
| 1207 | self.setup_config() |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 1208 | self.config.set('zuul', 'tenant_config', |
Joshua Hesketh | acccffc | 2015-03-31 23:38:17 +1100 | [diff] [blame] | 1209 | os.path.join(FIXTURE_DIR, |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 1210 | self.config.get('zuul', 'tenant_config'))) |
James E. Blair | 8c1be53 | 2017-02-07 14:04:12 -0800 | [diff] [blame] | 1211 | self.config.set('merger', 'git_dir', self.merger_git_root) |
| 1212 | self.config.set('launcher', 'git_dir', self.launcher_git_root) |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 1213 | self.config.set('zuul', 'state_dir', self.state_root) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1214 | |
| 1215 | # For each project in config: |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 1216 | # TODOv3(jeblair): remove these and replace with new git |
| 1217 | # filesystem fixtures |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1218 | self.init_repo("org/project3") |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 1219 | self.init_repo("org/project4") |
James E. Blair | bce35e1 | 2014-08-21 14:31:17 -0700 | [diff] [blame] | 1220 | self.init_repo("org/project5") |
| 1221 | self.init_repo("org/project6") |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1222 | self.init_repo("org/one-job-project") |
| 1223 | self.init_repo("org/nonvoting-project") |
| 1224 | self.init_repo("org/templated-project") |
| 1225 | self.init_repo("org/layered-project") |
| 1226 | self.init_repo("org/node-project") |
| 1227 | self.init_repo("org/conflict-project") |
| 1228 | self.init_repo("org/noop-project") |
| 1229 | self.init_repo("org/experimental-project") |
Evgeny Antyshev | d6e546c | 2015-06-11 15:13:57 +0000 | [diff] [blame] | 1230 | self.init_repo("org/no-jobs-project") |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1231 | |
| 1232 | self.statsd = FakeStatsd() |
Ian Wienand | ff977bf | 2015-09-30 15:38:47 +1000 | [diff] [blame] | 1233 | # note, use 127.0.0.1 rather than localhost to avoid getting ipv6 |
| 1234 | # see: https://github.com/jsocol/pystatsd/issues/61 |
| 1235 | os.environ['STATSD_HOST'] = '127.0.0.1' |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1236 | os.environ['STATSD_PORT'] = str(self.statsd.port) |
| 1237 | self.statsd.start() |
| 1238 | # the statsd client object is configured in the statsd module import |
Monty Taylor | 74fa386 | 2016-06-02 07:39:49 +0300 | [diff] [blame] | 1239 | reload_module(statsd) |
| 1240 | reload_module(zuul.scheduler) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1241 | |
| 1242 | self.gearman_server = FakeGearmanServer() |
| 1243 | |
| 1244 | self.config.set('gearman', 'port', str(self.gearman_server.port)) |
James E. Blair | e47eb77 | 2017-02-02 17:19:40 -0800 | [diff] [blame] | 1245 | self.log.info("Gearman server on port %s" % |
| 1246 | (self.gearman_server.port,)) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1247 | |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 1248 | gerritsource.GerritSource.replication_timeout = 1.5 |
| 1249 | gerritsource.GerritSource.replication_retry_interval = 0.5 |
| 1250 | gerritconnection.GerritEventConnector.delay = 0.0 |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1251 | |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 1252 | self.sched = zuul.scheduler.Scheduler(self.config) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1253 | |
| 1254 | self.useFixture(fixtures.MonkeyPatch('swiftclient.client.Connection', |
| 1255 | FakeSwiftClientConnection)) |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 1256 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1257 | self.swift = zuul.lib.swift.Swift(self.config) |
| 1258 | |
Jan Hruban | 6b71aff | 2015-10-22 16:58:08 +0200 | [diff] [blame] | 1259 | self.event_queues = [ |
| 1260 | self.sched.result_event_queue, |
| 1261 | self.sched.trigger_event_queue |
| 1262 | ] |
| 1263 | |
James E. Blair | fef7894 | 2016-03-11 16:28:56 -0800 | [diff] [blame] | 1264 | self.configure_connections() |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 1265 | self.sched.registerConnections(self.connections) |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 1266 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1267 | def URLOpenerFactory(*args, **kw): |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 1268 | if isinstance(args[0], urllib.request.Request): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1269 | return old_urlopen(*args, **kw) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1270 | return FakeURLOpener(self.upstream_root, *args, **kw) |
| 1271 | |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 1272 | old_urlopen = urllib.request.urlopen |
| 1273 | urllib.request.urlopen = URLOpenerFactory |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1274 | |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 1275 | self._startMerger() |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 1276 | |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1277 | self.launch_server = RecordingLaunchServer( |
James E. Blair | a92cbc8 | 2017-01-23 14:56:49 -0800 | [diff] [blame] | 1278 | self.config, self.connections, |
James E. Blair | 854f889 | 2017-02-02 11:25:39 -0800 | [diff] [blame] | 1279 | jobdir_root=self.test_root, |
James E. Blair | a92cbc8 | 2017-01-23 14:56:49 -0800 | [diff] [blame] | 1280 | _run_ansible=self.run_ansible, |
| 1281 | _test_root=self.test_root) |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1282 | self.launch_server.start() |
| 1283 | self.history = self.launch_server.build_history |
| 1284 | self.builds = self.launch_server.running_builds |
| 1285 | |
| 1286 | self.launch_client = zuul.launcher.client.LaunchClient( |
James E. Blair | 8293847 | 2016-01-11 14:38:13 -0800 | [diff] [blame] | 1287 | self.config, self.sched, self.swift) |
Joshua Hesketh | 850ccb6 | 2014-11-27 11:31:02 +1100 | [diff] [blame] | 1288 | self.merge_client = zuul.merger.client.MergeClient( |
| 1289 | self.config, self.sched) |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 1290 | self.nodepool = zuul.nodepool.Nodepool(self.sched) |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1291 | self.zk = zuul.zk.ZooKeeper() |
| 1292 | self.zk.connect([self.zk_config]) |
| 1293 | |
| 1294 | self.fake_nodepool = FakeNodepool(self.zk_config.host, |
| 1295 | self.zk_config.port, |
| 1296 | self.zk_config.chroot) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1297 | |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1298 | self.sched.setLauncher(self.launch_client) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1299 | self.sched.setMerger(self.merge_client) |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 1300 | self.sched.setNodepool(self.nodepool) |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1301 | self.sched.setZooKeeper(self.zk) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1302 | |
Paul Belanger | 88ef0ea | 2015-12-23 11:57:02 -0500 | [diff] [blame] | 1303 | self.webapp = zuul.webapp.WebApp( |
| 1304 | self.sched, port=0, listen_address='127.0.0.1') |
Joshua Hesketh | 850ccb6 | 2014-11-27 11:31:02 +1100 | [diff] [blame] | 1305 | self.rpc = zuul.rpclistener.RPCListener(self.config, self.sched) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1306 | |
| 1307 | self.sched.start() |
| 1308 | self.sched.reconfigure(self.config) |
| 1309 | self.sched.resume() |
| 1310 | self.webapp.start() |
| 1311 | self.rpc.start() |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1312 | self.launch_client.gearman.waitForServer() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1313 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1314 | self.addCleanup(self.shutdown) |
| 1315 | |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 1316 | def tearDown(self): |
| 1317 | super(ZuulTestCase, self).tearDown() |
| 1318 | self.assertFinalState() |
| 1319 | |
James E. Blair | fef7894 | 2016-03-11 16:28:56 -0800 | [diff] [blame] | 1320 | def configure_connections(self): |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 1321 | # Set up gerrit related fakes |
| 1322 | # Set a changes database so multiple FakeGerrit's can report back to |
| 1323 | # a virtual canonical database given by the configured hostname |
| 1324 | self.gerrit_changes_dbs = {} |
| 1325 | |
| 1326 | def getGerritConnection(driver, name, config): |
| 1327 | db = self.gerrit_changes_dbs.setdefault(config['server'], {}) |
| 1328 | con = FakeGerritConnection(driver, name, config, |
| 1329 | changes_db=db, |
| 1330 | upstream_root=self.upstream_root) |
| 1331 | self.event_queues.append(con.event_queue) |
| 1332 | setattr(self, 'fake_' + name, con) |
| 1333 | return con |
| 1334 | |
| 1335 | self.useFixture(fixtures.MonkeyPatch( |
| 1336 | 'zuul.driver.gerrit.GerritDriver.getConnection', |
| 1337 | getGerritConnection)) |
| 1338 | |
| 1339 | # Set up smtp related fakes |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 1340 | self.smtp_messages = [] |
Joshua Hesketh | 850ccb6 | 2014-11-27 11:31:02 +1100 | [diff] [blame] | 1341 | |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 1342 | def FakeSMTPFactory(*args, **kw): |
| 1343 | args = [self.smtp_messages] + list(args) |
| 1344 | return FakeSMTP(*args, **kw) |
Joshua Hesketh | 850ccb6 | 2014-11-27 11:31:02 +1100 | [diff] [blame] | 1345 | |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 1346 | self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTPFactory)) |
Joshua Hesketh | 850ccb6 | 2014-11-27 11:31:02 +1100 | [diff] [blame] | 1347 | |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 1348 | # Register connections from the config using fakes |
James E. Blair | fef7894 | 2016-03-11 16:28:56 -0800 | [diff] [blame] | 1349 | self.connections = zuul.lib.connections.ConnectionRegistry() |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 1350 | self.connections.configure(self.config) |
Joshua Hesketh | 850ccb6 | 2014-11-27 11:31:02 +1100 | [diff] [blame] | 1351 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 1352 | def setup_config(self): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 1353 | # This creates the per-test configuration object. It can be |
| 1354 | # overriden by subclasses, but should not need to be since it |
| 1355 | # obeys the config_file and tenant_config_file attributes. |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1356 | self.config = ConfigParser.ConfigParser() |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 1357 | self.config.read(os.path.join(FIXTURE_DIR, self.config_file)) |
James E. Blair | 2a629ec | 2015-12-22 15:32:02 -0800 | [diff] [blame] | 1358 | if hasattr(self, 'tenant_config_file'): |
| 1359 | self.config.set('zuul', 'tenant_config', self.tenant_config_file) |
James E. Blair | 96c6bf8 | 2016-01-15 16:20:40 -0800 | [diff] [blame] | 1360 | git_path = os.path.join( |
| 1361 | os.path.dirname( |
| 1362 | os.path.join(FIXTURE_DIR, self.tenant_config_file)), |
| 1363 | 'git') |
| 1364 | if os.path.exists(git_path): |
| 1365 | for reponame in os.listdir(git_path): |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 1366 | project = reponame.replace('_', '/') |
| 1367 | self.copyDirToRepo(project, |
James E. Blair | 96c6bf8 | 2016-01-15 16:20:40 -0800 | [diff] [blame] | 1368 | os.path.join(git_path, reponame)) |
| 1369 | |
James E. Blair | 498059b | 2016-12-20 13:50:13 -0800 | [diff] [blame] | 1370 | def setupZK(self): |
| 1371 | self.zk_chroot_fixture = self.useFixture(ChrootedKazooFixture()) |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1372 | self.zk_config = zuul.zk.ZooKeeperConnectionConfig( |
| 1373 | self.zk_chroot_fixture.zookeeper_host, |
| 1374 | self.zk_chroot_fixture.zookeeper_port, |
| 1375 | self.zk_chroot_fixture.zookeeper_chroot) |
James E. Blair | 498059b | 2016-12-20 13:50:13 -0800 | [diff] [blame] | 1376 | |
James E. Blair | 96c6bf8 | 2016-01-15 16:20:40 -0800 | [diff] [blame] | 1377 | def copyDirToRepo(self, project, source_path): |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 1378 | self.init_repo(project) |
James E. Blair | 96c6bf8 | 2016-01-15 16:20:40 -0800 | [diff] [blame] | 1379 | |
| 1380 | files = {} |
| 1381 | for (dirpath, dirnames, filenames) in os.walk(source_path): |
| 1382 | for filename in filenames: |
| 1383 | test_tree_filepath = os.path.join(dirpath, filename) |
| 1384 | common_path = os.path.commonprefix([test_tree_filepath, |
| 1385 | source_path]) |
| 1386 | relative_filepath = test_tree_filepath[len(common_path) + 1:] |
| 1387 | with open(test_tree_filepath, 'r') as f: |
| 1388 | content = f.read() |
| 1389 | files[relative_filepath] = content |
| 1390 | self.addCommitToRepo(project, 'add content from fixture', |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 1391 | files, branch='master', tag='init') |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 1392 | |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 1393 | def assertNodepoolState(self): |
| 1394 | # Make sure that there are no pending requests |
| 1395 | |
| 1396 | requests = self.fake_nodepool.getNodeRequests() |
| 1397 | self.assertEqual(len(requests), 0) |
| 1398 | |
| 1399 | nodes = self.fake_nodepool.getNodes() |
| 1400 | for node in nodes: |
| 1401 | self.assertFalse(node['_lock'], "Node %s is locked" % |
| 1402 | (node['_oid'],)) |
| 1403 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1404 | def assertFinalState(self): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1405 | # Make sure that git.Repo objects have been garbage collected. |
| 1406 | repos = [] |
| 1407 | gc.collect() |
| 1408 | for obj in gc.get_objects(): |
| 1409 | if isinstance(obj, git.Repo): |
| 1410 | repos.append(obj) |
| 1411 | self.assertEqual(len(repos), 0) |
| 1412 | self.assertEmptyQueues() |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 1413 | self.assertNodepoolState() |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 1414 | ipm = zuul.manager.independent.IndependentPipelineManager |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 1415 | for tenant in self.sched.abide.tenants.values(): |
| 1416 | for pipeline in tenant.layout.pipelines.values(): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 1417 | if isinstance(pipeline.manager, ipm): |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 1418 | self.assertEqual(len(pipeline.queues), 0) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1419 | |
| 1420 | def shutdown(self): |
| 1421 | self.log.debug("Shutting down after tests") |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1422 | self.launch_client.stop() |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 1423 | self.merge_server.stop() |
| 1424 | self.merge_server.join() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1425 | self.merge_client.stop() |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1426 | self.launch_server.stop() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1427 | self.sched.stop() |
| 1428 | self.sched.join() |
| 1429 | self.statsd.stop() |
| 1430 | self.statsd.join() |
| 1431 | self.webapp.stop() |
| 1432 | self.webapp.join() |
| 1433 | self.rpc.stop() |
| 1434 | self.rpc.join() |
| 1435 | self.gearman_server.shutdown() |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1436 | self.fake_nodepool.stop() |
| 1437 | self.zk.disconnect() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1438 | threads = threading.enumerate() |
| 1439 | if len(threads) > 1: |
| 1440 | self.log.error("More than one thread is running: %s" % threads) |
James E. Blair | 6ac368c | 2016-12-22 18:07:20 -0800 | [diff] [blame] | 1441 | self.printHistory() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1442 | |
| 1443 | def init_repo(self, project): |
| 1444 | parts = project.split('/') |
| 1445 | path = os.path.join(self.upstream_root, *parts[:-1]) |
| 1446 | if not os.path.exists(path): |
| 1447 | os.makedirs(path) |
| 1448 | path = os.path.join(self.upstream_root, project) |
| 1449 | repo = git.Repo.init(path) |
| 1450 | |
Morgan Fainberg | 78c301a | 2016-07-14 13:47:01 -0700 | [diff] [blame] | 1451 | with repo.config_writer() as config_writer: |
| 1452 | config_writer.set_value('user', 'email', 'user@example.com') |
| 1453 | config_writer.set_value('user', 'name', 'User Name') |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1454 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1455 | repo.index.commit('initial commit') |
| 1456 | master = repo.create_head('master') |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1457 | |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 1458 | repo.head.reference = master |
James E. Blair | 879dafb | 2015-07-17 14:04:49 -0700 | [diff] [blame] | 1459 | zuul.merger.merger.reset_repo_to_head(repo) |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 1460 | repo.git.clean('-x', '-f', '-d') |
| 1461 | |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 1462 | def create_branch(self, project, branch): |
| 1463 | path = os.path.join(self.upstream_root, project) |
| 1464 | repo = git.Repo.init(path) |
| 1465 | fn = os.path.join(path, 'README') |
| 1466 | |
| 1467 | branch_head = repo.create_head(branch) |
| 1468 | repo.head.reference = branch_head |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1469 | f = open(fn, 'a') |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 1470 | f.write("test %s\n" % branch) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1471 | f.close() |
| 1472 | repo.index.add([fn]) |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 1473 | repo.index.commit('%s commit' % branch) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1474 | |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 1475 | repo.head.reference = repo.heads['master'] |
James E. Blair | 879dafb | 2015-07-17 14:04:49 -0700 | [diff] [blame] | 1476 | zuul.merger.merger.reset_repo_to_head(repo) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1477 | repo.git.clean('-x', '-f', '-d') |
| 1478 | |
Sachi King | 9f16d52 | 2016-03-16 12:20:45 +1100 | [diff] [blame] | 1479 | def create_commit(self, project): |
| 1480 | path = os.path.join(self.upstream_root, project) |
| 1481 | repo = git.Repo(path) |
| 1482 | repo.head.reference = repo.heads['master'] |
| 1483 | file_name = os.path.join(path, 'README') |
| 1484 | with open(file_name, 'a') as f: |
| 1485 | f.write('creating fake commit\n') |
| 1486 | repo.index.add([file_name]) |
| 1487 | commit = repo.index.commit('Creating a fake commit') |
| 1488 | return commit.hexsha |
| 1489 | |
James E. Blair | b8c1647 | 2015-05-05 14:55:26 -0700 | [diff] [blame] | 1490 | def orderedRelease(self): |
| 1491 | # Run one build at a time to ensure non-race order: |
| 1492 | while len(self.builds): |
| 1493 | self.release(self.builds[0]) |
| 1494 | self.waitUntilSettled() |
| 1495 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1496 | def release(self, job): |
| 1497 | if isinstance(job, FakeBuild): |
| 1498 | job.release() |
| 1499 | else: |
| 1500 | job.waiting = False |
| 1501 | self.log.debug("Queued job %s released" % job.unique) |
| 1502 | self.gearman_server.wakeConnections() |
| 1503 | |
| 1504 | def getParameter(self, job, name): |
| 1505 | if isinstance(job, FakeBuild): |
| 1506 | return job.parameters[name] |
| 1507 | else: |
| 1508 | parameters = json.loads(job.arguments) |
| 1509 | return parameters[name] |
| 1510 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1511 | def haveAllBuildsReported(self): |
| 1512 | # See if Zuul is waiting on a meta job to complete |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1513 | if self.launch_client.meta_jobs: |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1514 | return False |
| 1515 | # Find out if every build that the worker has completed has been |
| 1516 | # reported back to Zuul. If it hasn't then that means a Gearman |
| 1517 | # event is still in transit and the system is not stable. |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 1518 | for build in self.history: |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1519 | zbuild = self.launch_client.builds.get(build.uuid) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1520 | if not zbuild: |
| 1521 | # It has already been reported |
| 1522 | continue |
| 1523 | # It hasn't been reported yet. |
| 1524 | return False |
| 1525 | # Make sure that none of the worker connections are in GRAB_WAIT |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1526 | for connection in self.launch_server.worker.active_connections: |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1527 | if connection.state == 'GRAB_WAIT': |
| 1528 | return False |
| 1529 | return True |
| 1530 | |
| 1531 | def areAllBuildsWaiting(self): |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1532 | builds = self.launch_client.builds.values() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1533 | for build in builds: |
| 1534 | client_job = None |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1535 | for conn in self.launch_client.gearman.active_connections: |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1536 | for j in conn.related_jobs.values(): |
| 1537 | if j.unique == build.uuid: |
| 1538 | client_job = j |
| 1539 | break |
| 1540 | if not client_job: |
| 1541 | self.log.debug("%s is not known to the gearman client" % |
| 1542 | build) |
James E. Blair | f15139b | 2015-04-02 16:37:15 -0700 | [diff] [blame] | 1543 | return False |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1544 | if not client_job.handle: |
| 1545 | self.log.debug("%s has no handle" % client_job) |
James E. Blair | f15139b | 2015-04-02 16:37:15 -0700 | [diff] [blame] | 1546 | return False |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1547 | server_job = self.gearman_server.jobs.get(client_job.handle) |
| 1548 | if not server_job: |
| 1549 | self.log.debug("%s is not known to the gearman server" % |
| 1550 | client_job) |
James E. Blair | f15139b | 2015-04-02 16:37:15 -0700 | [diff] [blame] | 1551 | return False |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1552 | if not hasattr(server_job, 'waiting'): |
| 1553 | self.log.debug("%s is being enqueued" % server_job) |
James E. Blair | f15139b | 2015-04-02 16:37:15 -0700 | [diff] [blame] | 1554 | return False |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1555 | if server_job.waiting: |
| 1556 | continue |
James E. Blair | 1730297 | 2016-08-10 16:11:42 -0700 | [diff] [blame] | 1557 | if build.url is None: |
James E. Blair | bbda470 | 2016-03-09 15:19:56 -0800 | [diff] [blame] | 1558 | self.log.debug("%s has not reported start" % build) |
| 1559 | return False |
James E. Blair | ab7132b | 2016-08-05 12:36:22 -0700 | [diff] [blame] | 1560 | worker_build = self.launch_server.job_builds.get(server_job.unique) |
James E. Blair | 962220f | 2016-08-03 11:22:38 -0700 | [diff] [blame] | 1561 | if worker_build: |
| 1562 | if worker_build.isWaiting(): |
| 1563 | continue |
| 1564 | else: |
| 1565 | self.log.debug("%s is running" % worker_build) |
| 1566 | return False |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1567 | else: |
James E. Blair | 962220f | 2016-08-03 11:22:38 -0700 | [diff] [blame] | 1568 | self.log.debug("%s is unassigned" % server_job) |
James E. Blair | f15139b | 2015-04-02 16:37:15 -0700 | [diff] [blame] | 1569 | return False |
| 1570 | return True |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1571 | |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1572 | def areAllNodeRequestsComplete(self): |
James E. Blair | 15be0e1 | 2017-01-03 13:45:20 -0800 | [diff] [blame] | 1573 | if self.fake_nodepool.paused: |
| 1574 | return True |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1575 | if self.sched.nodepool.requests: |
| 1576 | return False |
| 1577 | return True |
| 1578 | |
Jan Hruban | 6b71aff | 2015-10-22 16:58:08 +0200 | [diff] [blame] | 1579 | def eventQueuesEmpty(self): |
| 1580 | for queue in self.event_queues: |
| 1581 | yield queue.empty() |
| 1582 | |
| 1583 | def eventQueuesJoin(self): |
| 1584 | for queue in self.event_queues: |
| 1585 | queue.join() |
| 1586 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1587 | def waitUntilSettled(self): |
| 1588 | self.log.debug("Waiting until settled...") |
| 1589 | start = time.time() |
| 1590 | while True: |
James E. Blair | 7193248 | 2017-02-02 11:29:07 -0800 | [diff] [blame] | 1591 | if time.time() - start > 20: |
James E. Blair | 10fc1eb | 2016-12-21 16:16:25 -0800 | [diff] [blame] | 1592 | self.log.error("Timeout waiting for Zuul to settle") |
| 1593 | self.log.error("Queue status:") |
James E. Blair | 622c968 | 2016-06-09 08:14:53 -0700 | [diff] [blame] | 1594 | for queue in self.event_queues: |
James E. Blair | 10fc1eb | 2016-12-21 16:16:25 -0800 | [diff] [blame] | 1595 | self.log.error(" %s: %s" % (queue, queue.empty())) |
| 1596 | self.log.error("All builds waiting: %s" % |
James E. Blair | 622c968 | 2016-06-09 08:14:53 -0700 | [diff] [blame] | 1597 | (self.areAllBuildsWaiting(),)) |
James E. Blair | 10fc1eb | 2016-12-21 16:16:25 -0800 | [diff] [blame] | 1598 | self.log.error("All builds reported: %s" % |
James E. Blair | f3156c9 | 2016-08-10 15:32:19 -0700 | [diff] [blame] | 1599 | (self.haveAllBuildsReported(),)) |
James E. Blair | 10fc1eb | 2016-12-21 16:16:25 -0800 | [diff] [blame] | 1600 | self.log.error("All requests completed: %s" % |
| 1601 | (self.areAllNodeRequestsComplete(),)) |
| 1602 | self.log.error("Merge client jobs: %s" % |
| 1603 | (self.merge_client.jobs,)) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1604 | raise Exception("Timeout waiting for Zuul to settle") |
| 1605 | # Make sure no new events show up while we're checking |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 1606 | |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1607 | self.launch_server.lock.acquire() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1608 | # have all build states propogated to zuul? |
| 1609 | if self.haveAllBuildsReported(): |
| 1610 | # Join ensures that the queue is empty _and_ events have been |
| 1611 | # processed |
Jan Hruban | 6b71aff | 2015-10-22 16:58:08 +0200 | [diff] [blame] | 1612 | self.eventQueuesJoin() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1613 | self.sched.run_handler_lock.acquire() |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 1614 | if (not self.merge_client.jobs and |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1615 | self.haveAllBuildsReported() and |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1616 | self.areAllBuildsWaiting() and |
James E. Blair | 36c611a | 2017-02-06 15:59:43 -0800 | [diff] [blame] | 1617 | self.areAllNodeRequestsComplete() and |
| 1618 | all(self.eventQueuesEmpty())): |
| 1619 | # The queue empty check is placed at the end to |
| 1620 | # ensure that if a component adds an event between |
| 1621 | # when locked the run handler and checked that the |
| 1622 | # components were stable, we don't erroneously |
| 1623 | # report that we are settled. |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1624 | self.sched.run_handler_lock.release() |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1625 | self.launch_server.lock.release() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1626 | self.log.debug("...settled.") |
| 1627 | return |
| 1628 | self.sched.run_handler_lock.release() |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1629 | self.launch_server.lock.release() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1630 | self.sched.wake_event.wait(0.1) |
| 1631 | |
| 1632 | def countJobResults(self, jobs, result): |
| 1633 | jobs = filter(lambda x: x.result == result, jobs) |
| 1634 | return len(jobs) |
| 1635 | |
James E. Blair | 96c6bf8 | 2016-01-15 16:20:40 -0800 | [diff] [blame] | 1636 | def getJobFromHistory(self, name, project=None): |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 1637 | for job in self.history: |
| 1638 | if (job.name == name and |
| 1639 | (project is None or |
| 1640 | job.parameters['ZUUL_PROJECT'] == project)): |
| 1641 | return job |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1642 | raise Exception("Unable to find job %s in history" % name) |
| 1643 | |
| 1644 | def assertEmptyQueues(self): |
| 1645 | # Make sure there are no orphaned jobs |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 1646 | for tenant in self.sched.abide.tenants.values(): |
| 1647 | for pipeline in tenant.layout.pipelines.values(): |
| 1648 | for queue in pipeline.queues: |
| 1649 | if len(queue.queue) != 0: |
Joshua Hesketh | 0aa7e8b | 2016-07-14 00:12:25 +1000 | [diff] [blame] | 1650 | print('pipeline %s queue %s contents %s' % ( |
| 1651 | pipeline.name, queue.name, queue.queue)) |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 1652 | self.assertEqual(len(queue.queue), 0, |
| 1653 | "Pipelines queues should be empty") |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1654 | |
| 1655 | def assertReportedStat(self, key, value=None, kind=None): |
| 1656 | start = time.time() |
| 1657 | while time.time() < (start + 5): |
| 1658 | for stat in self.statsd.stats: |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1659 | k, v = stat.split(':') |
| 1660 | if key == k: |
| 1661 | if value is None and kind is None: |
| 1662 | return |
| 1663 | elif value: |
| 1664 | if value == v: |
| 1665 | return |
| 1666 | elif kind: |
| 1667 | if v.endswith('|' + kind): |
| 1668 | return |
| 1669 | time.sleep(0.1) |
| 1670 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1671 | raise Exception("Key %s not found in reported stats" % key) |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 1672 | |
James E. Blair | 2b2a8ab | 2016-08-11 14:39:11 -0700 | [diff] [blame] | 1673 | def assertBuilds(self, builds): |
| 1674 | """Assert that the running builds are as described. |
| 1675 | |
| 1676 | The list of running builds is examined and must match exactly |
| 1677 | the list of builds described by the input. |
| 1678 | |
| 1679 | :arg list builds: A list of dictionaries. Each item in the |
| 1680 | list must match the corresponding build in the build |
| 1681 | history, and each element of the dictionary must match the |
| 1682 | corresponding attribute of the build. |
| 1683 | |
| 1684 | """ |
James E. Blair | 3158e28 | 2016-08-19 09:34:11 -0700 | [diff] [blame] | 1685 | try: |
| 1686 | self.assertEqual(len(self.builds), len(builds)) |
| 1687 | for i, d in enumerate(builds): |
| 1688 | for k, v in d.items(): |
| 1689 | self.assertEqual( |
| 1690 | getattr(self.builds[i], k), v, |
| 1691 | "Element %i in builds does not match" % (i,)) |
| 1692 | except Exception: |
| 1693 | for build in self.builds: |
| 1694 | self.log.error("Running build: %s" % build) |
| 1695 | else: |
| 1696 | self.log.error("No running builds") |
| 1697 | raise |
James E. Blair | 2b2a8ab | 2016-08-11 14:39:11 -0700 | [diff] [blame] | 1698 | |
James E. Blair | b536ecc | 2016-08-31 10:11:42 -0700 | [diff] [blame] | 1699 | def assertHistory(self, history, ordered=True): |
James E. Blair | 2b2a8ab | 2016-08-11 14:39:11 -0700 | [diff] [blame] | 1700 | """Assert that the completed builds are as described. |
| 1701 | |
| 1702 | The list of completed builds is examined and must match |
| 1703 | exactly the list of builds described by the input. |
| 1704 | |
| 1705 | :arg list history: A list of dictionaries. Each item in the |
| 1706 | list must match the corresponding build in the build |
| 1707 | history, and each element of the dictionary must match the |
| 1708 | corresponding attribute of the build. |
| 1709 | |
James E. Blair | b536ecc | 2016-08-31 10:11:42 -0700 | [diff] [blame] | 1710 | :arg bool ordered: If true, the history must match the order |
| 1711 | supplied, if false, the builds are permitted to have |
| 1712 | arrived in any order. |
| 1713 | |
James E. Blair | 2b2a8ab | 2016-08-11 14:39:11 -0700 | [diff] [blame] | 1714 | """ |
James E. Blair | b536ecc | 2016-08-31 10:11:42 -0700 | [diff] [blame] | 1715 | def matches(history_item, item): |
| 1716 | for k, v in item.items(): |
| 1717 | if getattr(history_item, k) != v: |
| 1718 | return False |
| 1719 | return True |
James E. Blair | 3158e28 | 2016-08-19 09:34:11 -0700 | [diff] [blame] | 1720 | try: |
| 1721 | self.assertEqual(len(self.history), len(history)) |
James E. Blair | b536ecc | 2016-08-31 10:11:42 -0700 | [diff] [blame] | 1722 | if ordered: |
| 1723 | for i, d in enumerate(history): |
| 1724 | if not matches(self.history[i], d): |
| 1725 | raise Exception( |
| 1726 | "Element %i in history does not match" % (i,)) |
| 1727 | else: |
| 1728 | unseen = self.history[:] |
| 1729 | for i, d in enumerate(history): |
| 1730 | found = False |
| 1731 | for unseen_item in unseen: |
| 1732 | if matches(unseen_item, d): |
| 1733 | found = True |
| 1734 | unseen.remove(unseen_item) |
| 1735 | break |
| 1736 | if not found: |
| 1737 | raise Exception("No match found for element %i " |
| 1738 | "in history" % (i,)) |
| 1739 | if unseen: |
| 1740 | raise Exception("Unexpected items in history") |
James E. Blair | 3158e28 | 2016-08-19 09:34:11 -0700 | [diff] [blame] | 1741 | except Exception: |
| 1742 | for build in self.history: |
| 1743 | self.log.error("Completed build: %s" % build) |
| 1744 | else: |
| 1745 | self.log.error("No completed builds") |
| 1746 | raise |
James E. Blair | 2b2a8ab | 2016-08-11 14:39:11 -0700 | [diff] [blame] | 1747 | |
James E. Blair | 6ac368c | 2016-12-22 18:07:20 -0800 | [diff] [blame] | 1748 | def printHistory(self): |
| 1749 | """Log the build history. |
| 1750 | |
| 1751 | This can be useful during tests to summarize what jobs have |
| 1752 | completed. |
| 1753 | |
| 1754 | """ |
| 1755 | self.log.debug("Build history:") |
| 1756 | for build in self.history: |
| 1757 | self.log.debug(build) |
| 1758 | |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 1759 | def getPipeline(self, name): |
James E. Blair | f84026c | 2015-12-08 16:11:46 -0800 | [diff] [blame] | 1760 | return self.sched.abide.tenants.values()[0].layout.pipelines.get(name) |
| 1761 | |
| 1762 | def updateConfigLayout(self, path): |
| 1763 | root = os.path.join(self.test_root, "config") |
Clint Byrum | ead6c56 | 2017-02-01 16:34:04 -0800 | [diff] [blame] | 1764 | if not os.path.exists(root): |
| 1765 | os.makedirs(root) |
James E. Blair | f84026c | 2015-12-08 16:11:46 -0800 | [diff] [blame] | 1766 | f = tempfile.NamedTemporaryFile(dir=root, delete=False) |
| 1767 | f.write(""" |
Paul Belanger | 66e9596 | 2016-11-11 12:11:06 -0500 | [diff] [blame] | 1768 | - tenant: |
| 1769 | name: openstack |
| 1770 | source: |
| 1771 | gerrit: |
| 1772 | config-repos: |
| 1773 | - %s |
| 1774 | """ % path) |
James E. Blair | f84026c | 2015-12-08 16:11:46 -0800 | [diff] [blame] | 1775 | f.close() |
Paul Belanger | 66e9596 | 2016-11-11 12:11:06 -0500 | [diff] [blame] | 1776 | self.config.set('zuul', 'tenant_config', |
| 1777 | os.path.join(FIXTURE_DIR, f.name)) |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 1778 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 1779 | def addCommitToRepo(self, project, message, files, |
| 1780 | branch='master', tag=None): |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 1781 | path = os.path.join(self.upstream_root, project) |
| 1782 | repo = git.Repo(path) |
| 1783 | repo.head.reference = branch |
| 1784 | zuul.merger.merger.reset_repo_to_head(repo) |
| 1785 | for fn, content in files.items(): |
| 1786 | fn = os.path.join(path, fn) |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 1787 | try: |
| 1788 | os.makedirs(os.path.dirname(fn)) |
| 1789 | except OSError: |
| 1790 | pass |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 1791 | with open(fn, 'w') as f: |
| 1792 | f.write(content) |
| 1793 | repo.index.add([fn]) |
| 1794 | commit = repo.index.commit(message) |
| 1795 | repo.heads[branch].commit = commit |
| 1796 | repo.head.reference = branch |
| 1797 | repo.git.clean('-x', '-f', '-d') |
| 1798 | repo.heads[branch].checkout() |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 1799 | if tag: |
| 1800 | repo.create_tag(tag) |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 1801 | |
James E. Blair | 7fc8daa | 2016-08-08 15:37:15 -0700 | [diff] [blame] | 1802 | def addEvent(self, connection, event): |
| 1803 | """Inject a Fake (Gerrit) event. |
| 1804 | |
| 1805 | This method accepts a JSON-encoded event and simulates Zuul |
| 1806 | having received it from Gerrit. It could (and should) |
| 1807 | eventually apply to any connection type, but is currently only |
| 1808 | used with Gerrit connections. The name of the connection is |
| 1809 | used to look up the corresponding server, and the event is |
| 1810 | simulated as having been received by all Zuul connections |
| 1811 | attached to that server. So if two Gerrit connections in Zuul |
| 1812 | are connected to the same Gerrit server, and you invoke this |
| 1813 | method specifying the name of one of them, the event will be |
| 1814 | received by both. |
| 1815 | |
| 1816 | .. note:: |
| 1817 | |
| 1818 | "self.fake_gerrit.addEvent" calls should be migrated to |
| 1819 | this method. |
| 1820 | |
| 1821 | :arg str connection: The name of the connection corresponding |
| 1822 | to the gerrit server. |
| 1823 | :arg str event: The JSON-encoded event. |
| 1824 | |
| 1825 | """ |
| 1826 | specified_conn = self.connections.connections[connection] |
| 1827 | for conn in self.connections.connections.values(): |
| 1828 | if (isinstance(conn, specified_conn.__class__) and |
| 1829 | specified_conn.server == conn.server): |
| 1830 | conn.addEvent(event) |
| 1831 | |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 1832 | |
| 1833 | class AnsibleZuulTestCase(ZuulTestCase): |
| 1834 | """ZuulTestCase but with an actual ansible launcher running""" |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1835 | run_ansible = True |