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