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 | |
Monty Taylor | b934c1a | 2017-06-16 19:31:47 -0500 | [diff] [blame] | 18 | import configparser |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 19 | from contextlib import contextmanager |
Adam Gandelman | d81dd76 | 2017-02-09 15:15:49 -0800 | [diff] [blame] | 20 | import datetime |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 21 | import gc |
| 22 | import hashlib |
Monty Taylor | b934c1a | 2017-06-16 19:31:47 -0500 | [diff] [blame] | 23 | from io import StringIO |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 24 | import json |
| 25 | import logging |
| 26 | import os |
Monty Taylor | b934c1a | 2017-06-16 19:31:47 -0500 | [diff] [blame] | 27 | import queue |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 28 | import random |
| 29 | import re |
| 30 | import select |
| 31 | import shutil |
| 32 | import socket |
| 33 | import string |
| 34 | import subprocess |
James E. Blair | 1c236df | 2017-02-01 14:07:24 -0800 | [diff] [blame] | 35 | import sys |
James E. Blair | f84026c | 2015-12-08 16:11:46 -0800 | [diff] [blame] | 36 | import tempfile |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 37 | import threading |
Clark Boylan | 8208c19 | 2017-04-24 18:08:08 -0700 | [diff] [blame] | 38 | import traceback |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 39 | import time |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 40 | import uuid |
Monty Taylor | b934c1a | 2017-06-16 19:31:47 -0500 | [diff] [blame] | 41 | import urllib |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 42 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 43 | |
| 44 | import git |
| 45 | import gear |
| 46 | import fixtures |
James E. Blair | 498059b | 2016-12-20 13:50:13 -0800 | [diff] [blame] | 47 | import kazoo.client |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 48 | import kazoo.exceptions |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 49 | import pymysql |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 50 | import testtools |
James E. Blair | 1c236df | 2017-02-01 14:07:24 -0800 | [diff] [blame] | 51 | import testtools.content |
| 52 | import testtools.content_type |
Clint Byrum | 3343e3e | 2016-11-15 16:05:03 -0800 | [diff] [blame] | 53 | from git.exc import NoSuchPathError |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 54 | import yaml |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 55 | |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 56 | import zuul.driver.gerrit.gerritsource as gerritsource |
| 57 | import zuul.driver.gerrit.gerritconnection as gerritconnection |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 58 | import zuul.driver.github.githubconnection as githubconnection |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 59 | import zuul.scheduler |
| 60 | import zuul.webapp |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 61 | import zuul.executor.server |
| 62 | import zuul.executor.client |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 63 | import zuul.lib.connections |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 64 | import zuul.merger.client |
James E. Blair | 879dafb | 2015-07-17 14:04:49 -0700 | [diff] [blame] | 65 | import zuul.merger.merger |
| 66 | import zuul.merger.server |
Tobias Henkel | d91b4d7 | 2017-05-23 15:43:40 +0200 | [diff] [blame] | 67 | import zuul.model |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 68 | import zuul.nodepool |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 69 | import zuul.zk |
James E. Blair | b09a0c5 | 2017-10-04 07:35:14 -0700 | [diff] [blame] | 70 | import zuul.configloader |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 71 | from zuul.exceptions import MergeFailure |
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(): |
Clint Byrum | c0923d5 | 2017-05-10 15:47:41 -0400 | [diff] [blame] | 91 | return hashlib.sha1(str(random.random()).encode('ascii')).hexdigest() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 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 | |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 129 | class GerritChangeReference(git.Reference): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 130 | _common_path_default = "refs/changes" |
| 131 | _points_to_commits_only = True |
| 132 | |
| 133 | |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 134 | class FakeGerritChange(object): |
Tobias Henkel | ea98a19 | 2017-05-29 21:15:17 +0200 | [diff] [blame] | 135 | categories = {'Approved': ('Approved', -1, 1), |
| 136 | 'Code-Review': ('Code-Review', -2, 2), |
| 137 | 'Verified': ('Verified', -2, 2)} |
| 138 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 139 | def __init__(self, gerrit, number, project, branch, subject, |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 140 | status='NEW', upstream_root=None, files={}, |
| 141 | parent=None): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 142 | self.gerrit = gerrit |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 143 | self.source = gerrit |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 144 | self.reported = 0 |
| 145 | self.queried = 0 |
| 146 | self.patchsets = [] |
| 147 | self.number = number |
| 148 | self.project = project |
| 149 | self.branch = branch |
| 150 | self.subject = subject |
| 151 | self.latest_patchset = 0 |
| 152 | self.depends_on_change = None |
| 153 | self.needed_by_changes = [] |
| 154 | self.fail_merge = False |
| 155 | self.messages = [] |
| 156 | self.data = { |
| 157 | 'branch': branch, |
| 158 | 'comments': [], |
| 159 | 'commitMessage': subject, |
| 160 | 'createdOn': time.time(), |
| 161 | 'id': 'I' + random_sha1(), |
| 162 | 'lastUpdated': time.time(), |
| 163 | 'number': str(number), |
| 164 | 'open': status == 'NEW', |
| 165 | 'owner': {'email': 'user@example.com', |
| 166 | 'name': 'User Name', |
| 167 | 'username': 'username'}, |
| 168 | 'patchSets': self.patchsets, |
| 169 | 'project': project, |
| 170 | 'status': status, |
| 171 | 'subject': subject, |
| 172 | 'submitRecords': [], |
| 173 | 'url': 'https://hostname/%s' % number} |
| 174 | |
| 175 | self.upstream_root = upstream_root |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 176 | self.addPatchset(files=files, parent=parent) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 177 | self.data['submitRecords'] = self.getSubmitRecords() |
| 178 | self.open = status == 'NEW' |
| 179 | |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 180 | def addFakeChangeToRepo(self, msg, files, large, parent): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 181 | path = os.path.join(self.upstream_root, self.project) |
| 182 | repo = git.Repo(path) |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 183 | if parent is None: |
| 184 | parent = 'refs/tags/init' |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 185 | ref = GerritChangeReference.create( |
| 186 | repo, '1/%s/%s' % (self.number, self.latest_patchset), |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 187 | parent) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 188 | repo.head.reference = ref |
James E. Blair | 879dafb | 2015-07-17 14:04:49 -0700 | [diff] [blame] | 189 | zuul.merger.merger.reset_repo_to_head(repo) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 190 | repo.git.clean('-x', '-f', '-d') |
| 191 | |
| 192 | path = os.path.join(self.upstream_root, self.project) |
| 193 | if not large: |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 194 | for fn, content in files.items(): |
| 195 | fn = os.path.join(path, fn) |
James E. Blair | 332636e | 2017-09-05 10:14:35 -0700 | [diff] [blame] | 196 | if content is None: |
| 197 | os.unlink(fn) |
| 198 | repo.index.remove([fn]) |
| 199 | else: |
| 200 | d = os.path.dirname(fn) |
| 201 | if not os.path.exists(d): |
| 202 | os.makedirs(d) |
| 203 | with open(fn, 'w') as f: |
| 204 | f.write(content) |
| 205 | repo.index.add([fn]) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 206 | else: |
| 207 | for fni in range(100): |
| 208 | fn = os.path.join(path, str(fni)) |
| 209 | f = open(fn, 'w') |
| 210 | for ci in range(4096): |
| 211 | f.write(random.choice(string.printable)) |
| 212 | f.close() |
| 213 | repo.index.add([fn]) |
| 214 | |
| 215 | r = repo.index.commit(msg) |
| 216 | repo.head.reference = 'master' |
James E. Blair | 879dafb | 2015-07-17 14:04:49 -0700 | [diff] [blame] | 217 | zuul.merger.merger.reset_repo_to_head(repo) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 218 | repo.git.clean('-x', '-f', '-d') |
| 219 | repo.heads['master'].checkout() |
| 220 | return r |
| 221 | |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 222 | def addPatchset(self, files=None, large=False, parent=None): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 223 | self.latest_patchset += 1 |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 224 | if not files: |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 225 | fn = '%s-%s' % (self.branch.replace('/', '_'), self.number) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 226 | data = ("test %s %s %s\n" % |
| 227 | (self.branch, self.number, self.latest_patchset)) |
| 228 | files = {fn: data} |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 229 | msg = self.subject + '-' + str(self.latest_patchset) |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 230 | c = self.addFakeChangeToRepo(msg, files, large, parent) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 231 | ps_files = [{'file': '/COMMIT_MSG', |
| 232 | 'type': 'ADDED'}, |
| 233 | {'file': 'README', |
| 234 | 'type': 'MODIFIED'}] |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 235 | for f in files.keys(): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 236 | ps_files.append({'file': f, 'type': 'ADDED'}) |
| 237 | d = {'approvals': [], |
| 238 | 'createdOn': time.time(), |
| 239 | 'files': ps_files, |
| 240 | 'number': str(self.latest_patchset), |
| 241 | 'ref': 'refs/changes/1/%s/%s' % (self.number, |
| 242 | self.latest_patchset), |
| 243 | 'revision': c.hexsha, |
| 244 | 'uploader': {'email': 'user@example.com', |
| 245 | 'name': 'User name', |
| 246 | 'username': 'user'}} |
| 247 | self.data['currentPatchSet'] = d |
| 248 | self.patchsets.append(d) |
| 249 | self.data['submitRecords'] = self.getSubmitRecords() |
| 250 | |
| 251 | def getPatchsetCreatedEvent(self, patchset): |
| 252 | event = {"type": "patchset-created", |
| 253 | "change": {"project": self.project, |
| 254 | "branch": self.branch, |
| 255 | "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af", |
| 256 | "number": str(self.number), |
| 257 | "subject": self.subject, |
| 258 | "owner": {"name": "User Name"}, |
| 259 | "url": "https://hostname/3"}, |
| 260 | "patchSet": self.patchsets[patchset - 1], |
| 261 | "uploader": {"name": "User Name"}} |
| 262 | return event |
| 263 | |
| 264 | def getChangeRestoredEvent(self): |
| 265 | event = {"type": "change-restored", |
| 266 | "change": {"project": self.project, |
| 267 | "branch": self.branch, |
| 268 | "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af", |
| 269 | "number": str(self.number), |
| 270 | "subject": self.subject, |
| 271 | "owner": {"name": "User Name"}, |
| 272 | "url": "https://hostname/3"}, |
| 273 | "restorer": {"name": "User Name"}, |
Antoine Musso | bd86a31 | 2014-01-08 14:51:33 +0100 | [diff] [blame] | 274 | "patchSet": self.patchsets[-1], |
| 275 | "reason": ""} |
| 276 | return event |
| 277 | |
| 278 | def getChangeAbandonedEvent(self): |
| 279 | event = {"type": "change-abandoned", |
| 280 | "change": {"project": self.project, |
| 281 | "branch": self.branch, |
| 282 | "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af", |
| 283 | "number": str(self.number), |
| 284 | "subject": self.subject, |
| 285 | "owner": {"name": "User Name"}, |
| 286 | "url": "https://hostname/3"}, |
| 287 | "abandoner": {"name": "User Name"}, |
| 288 | "patchSet": self.patchsets[-1], |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 289 | "reason": ""} |
| 290 | return event |
| 291 | |
| 292 | def getChangeCommentEvent(self, patchset): |
| 293 | event = {"type": "comment-added", |
| 294 | "change": {"project": self.project, |
| 295 | "branch": self.branch, |
| 296 | "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af", |
| 297 | "number": str(self.number), |
| 298 | "subject": self.subject, |
| 299 | "owner": {"name": "User Name"}, |
| 300 | "url": "https://hostname/3"}, |
| 301 | "patchSet": self.patchsets[patchset - 1], |
| 302 | "author": {"name": "User Name"}, |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 303 | "approvals": [{"type": "Code-Review", |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 304 | "description": "Code-Review", |
| 305 | "value": "0"}], |
| 306 | "comment": "This is a comment"} |
| 307 | return event |
| 308 | |
James E. Blair | c2a5ed7 | 2017-02-20 14:12:01 -0500 | [diff] [blame] | 309 | def getChangeMergedEvent(self): |
| 310 | event = {"submitter": {"name": "Jenkins", |
| 311 | "username": "jenkins"}, |
| 312 | "newRev": "29ed3b5f8f750a225c5be70235230e3a6ccb04d9", |
| 313 | "patchSet": self.patchsets[-1], |
| 314 | "change": self.data, |
| 315 | "type": "change-merged", |
| 316 | "eventCreatedOn": 1487613810} |
| 317 | return event |
| 318 | |
James E. Blair | 8cce42e | 2016-10-18 08:18:36 -0700 | [diff] [blame] | 319 | def getRefUpdatedEvent(self): |
| 320 | path = os.path.join(self.upstream_root, self.project) |
| 321 | repo = git.Repo(path) |
| 322 | oldrev = repo.heads[self.branch].commit.hexsha |
| 323 | |
| 324 | event = { |
| 325 | "type": "ref-updated", |
| 326 | "submitter": { |
| 327 | "name": "User Name", |
| 328 | }, |
| 329 | "refUpdate": { |
| 330 | "oldRev": oldrev, |
| 331 | "newRev": self.patchsets[-1]['revision'], |
| 332 | "refName": self.branch, |
| 333 | "project": self.project, |
| 334 | } |
| 335 | } |
| 336 | return event |
| 337 | |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 338 | def addApproval(self, category, value, username='reviewer_john', |
| 339 | granted_on=None, message=''): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 340 | if not granted_on: |
| 341 | granted_on = time.time() |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 342 | approval = { |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 343 | 'description': self.categories[category][0], |
| 344 | 'type': category, |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 345 | 'value': str(value), |
| 346 | 'by': { |
| 347 | 'username': username, |
| 348 | 'email': username + '@example.com', |
| 349 | }, |
| 350 | 'grantedOn': int(granted_on) |
| 351 | } |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 352 | for i, x in enumerate(self.patchsets[-1]['approvals'][:]): |
Tobias Henkel | bf24fd1 | 2017-07-27 06:13:07 +0200 | [diff] [blame] | 353 | if x['by']['username'] == username and x['type'] == category: |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 354 | del self.patchsets[-1]['approvals'][i] |
| 355 | self.patchsets[-1]['approvals'].append(approval) |
| 356 | event = {'approvals': [approval], |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 357 | 'author': {'email': 'author@example.com', |
| 358 | 'name': 'Patchset Author', |
| 359 | 'username': 'author_phil'}, |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 360 | 'change': {'branch': self.branch, |
| 361 | 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87', |
| 362 | 'number': str(self.number), |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 363 | 'owner': {'email': 'owner@example.com', |
| 364 | 'name': 'Change Owner', |
| 365 | 'username': 'owner_jane'}, |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 366 | 'project': self.project, |
| 367 | 'subject': self.subject, |
| 368 | 'topic': 'master', |
| 369 | 'url': 'https://hostname/459'}, |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 370 | 'comment': message, |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 371 | 'patchSet': self.patchsets[-1], |
| 372 | 'type': 'comment-added'} |
| 373 | self.data['submitRecords'] = self.getSubmitRecords() |
| 374 | return json.loads(json.dumps(event)) |
| 375 | |
| 376 | def getSubmitRecords(self): |
| 377 | status = {} |
| 378 | for cat in self.categories.keys(): |
| 379 | status[cat] = 0 |
| 380 | |
| 381 | for a in self.patchsets[-1]['approvals']: |
| 382 | cur = status[a['type']] |
| 383 | cat_min, cat_max = self.categories[a['type']][1:] |
| 384 | new = int(a['value']) |
| 385 | if new == cat_min: |
| 386 | cur = new |
| 387 | elif abs(new) > abs(cur): |
| 388 | cur = new |
| 389 | status[a['type']] = cur |
| 390 | |
| 391 | labels = [] |
| 392 | ok = True |
| 393 | for typ, cat in self.categories.items(): |
| 394 | cur = status[typ] |
| 395 | cat_min, cat_max = cat[1:] |
| 396 | if cur == cat_min: |
| 397 | value = 'REJECT' |
| 398 | ok = False |
| 399 | elif cur == cat_max: |
| 400 | value = 'OK' |
| 401 | else: |
| 402 | value = 'NEED' |
| 403 | ok = False |
| 404 | labels.append({'label': cat[0], 'status': value}) |
| 405 | if ok: |
| 406 | return [{'status': 'OK'}] |
| 407 | return [{'status': 'NOT_READY', |
| 408 | 'labels': labels}] |
| 409 | |
| 410 | def setDependsOn(self, other, patchset): |
| 411 | self.depends_on_change = other |
| 412 | d = {'id': other.data['id'], |
| 413 | 'number': other.data['number'], |
| 414 | 'ref': other.patchsets[patchset - 1]['ref'] |
| 415 | } |
| 416 | self.data['dependsOn'] = [d] |
| 417 | |
| 418 | other.needed_by_changes.append(self) |
| 419 | needed = other.data.get('neededBy', []) |
| 420 | d = {'id': self.data['id'], |
| 421 | 'number': self.data['number'], |
James E. Blair | db93b30 | 2017-07-19 15:33:11 -0700 | [diff] [blame] | 422 | 'ref': self.patchsets[-1]['ref'], |
| 423 | 'revision': self.patchsets[-1]['revision'] |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 424 | } |
| 425 | needed.append(d) |
| 426 | other.data['neededBy'] = needed |
| 427 | |
| 428 | def query(self): |
| 429 | self.queried += 1 |
| 430 | d = self.data.get('dependsOn') |
| 431 | if d: |
| 432 | d = d[0] |
| 433 | if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']): |
| 434 | d['isCurrentPatchSet'] = True |
| 435 | else: |
| 436 | d['isCurrentPatchSet'] = False |
| 437 | return json.loads(json.dumps(self.data)) |
| 438 | |
| 439 | def setMerged(self): |
| 440 | if (self.depends_on_change and |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 441 | self.depends_on_change.data['status'] != 'MERGED'): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 442 | return |
| 443 | if self.fail_merge: |
| 444 | return |
| 445 | self.data['status'] = 'MERGED' |
| 446 | self.open = False |
| 447 | |
| 448 | path = os.path.join(self.upstream_root, self.project) |
| 449 | repo = git.Repo(path) |
| 450 | repo.heads[self.branch].commit = \ |
| 451 | repo.commit(self.patchsets[-1]['revision']) |
| 452 | |
| 453 | def setReported(self): |
| 454 | self.reported += 1 |
| 455 | |
| 456 | |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 457 | class FakeGerritConnection(gerritconnection.GerritConnection): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 458 | """A Fake Gerrit connection for use in tests. |
| 459 | |
| 460 | This subclasses |
| 461 | :py:class:`~zuul.connection.gerrit.GerritConnection` to add the |
| 462 | ability for tests to add changes to the fake Gerrit it represents. |
| 463 | """ |
| 464 | |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 465 | log = logging.getLogger("zuul.test.FakeGerritConnection") |
James E. Blair | 96698e2 | 2015-04-02 07:48:21 -0700 | [diff] [blame] | 466 | |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 467 | def __init__(self, driver, connection_name, connection_config, |
James E. Blair | 7fc8daa | 2016-08-08 15:37:15 -0700 | [diff] [blame] | 468 | changes_db=None, upstream_root=None): |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 469 | super(FakeGerritConnection, self).__init__(driver, connection_name, |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 470 | connection_config) |
| 471 | |
Monty Taylor | b934c1a | 2017-06-16 19:31:47 -0500 | [diff] [blame] | 472 | self.event_queue = queue.Queue() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 473 | self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit') |
| 474 | self.change_number = 0 |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 475 | self.changes = changes_db |
James E. Blair | f8ff993 | 2014-08-15 15:24:24 -0700 | [diff] [blame] | 476 | self.queries = [] |
Jan Hruban | 6b71aff | 2015-10-22 16:58:08 +0200 | [diff] [blame] | 477 | self.upstream_root = upstream_root |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 478 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 479 | def addFakeChange(self, project, branch, subject, status='NEW', |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 480 | files=None, parent=None): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 481 | """Add a change to the fake Gerrit.""" |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 482 | self.change_number += 1 |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 483 | c = FakeGerritChange(self, self.change_number, project, branch, |
| 484 | subject, upstream_root=self.upstream_root, |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 485 | status=status, files=files, parent=parent) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 486 | self.changes[self.change_number] = c |
| 487 | return c |
| 488 | |
James E. Blair | 72facdc | 2017-08-17 10:29:12 -0700 | [diff] [blame] | 489 | def getFakeBranchCreatedEvent(self, project, branch): |
| 490 | path = os.path.join(self.upstream_root, project) |
| 491 | repo = git.Repo(path) |
| 492 | oldrev = 40 * '0' |
| 493 | |
| 494 | event = { |
| 495 | "type": "ref-updated", |
| 496 | "submitter": { |
| 497 | "name": "User Name", |
| 498 | }, |
| 499 | "refUpdate": { |
| 500 | "oldRev": oldrev, |
| 501 | "newRev": repo.heads[branch].commit.hexsha, |
| 502 | "refName": branch, |
| 503 | "project": project, |
| 504 | } |
| 505 | } |
| 506 | return event |
| 507 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 508 | def review(self, project, changeid, message, action): |
| 509 | number, ps = changeid.split(',') |
| 510 | change = self.changes[int(number)] |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 511 | |
| 512 | # Add the approval back onto the change (ie simulate what gerrit would |
| 513 | # do). |
| 514 | # Usually when zuul leaves a review it'll create a feedback loop where |
| 515 | # zuul's review enters another gerrit event (which is then picked up by |
| 516 | # zuul). However, we can't mimic this behaviour (by adding this |
| 517 | # approval event into the queue) as it stops jobs from checking what |
| 518 | # happens before this event is triggered. If a job needs to see what |
| 519 | # happens they can add their own verified event into the queue. |
| 520 | # Nevertheless, we can update change with the new review in gerrit. |
| 521 | |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 522 | for cat in action.keys(): |
| 523 | if cat != 'submit': |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 524 | change.addApproval(cat, action[cat], username=self.user) |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 525 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 526 | change.messages.append(message) |
Joshua Hesketh | 642824b | 2014-07-01 17:54:59 +1000 | [diff] [blame] | 527 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 528 | if 'submit' in action: |
| 529 | change.setMerged() |
| 530 | if message: |
| 531 | change.setReported() |
| 532 | |
| 533 | def query(self, number): |
| 534 | change = self.changes.get(int(number)) |
| 535 | if change: |
| 536 | return change.query() |
| 537 | return {} |
| 538 | |
James E. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 539 | def simpleQuery(self, query): |
James E. Blair | 96698e2 | 2015-04-02 07:48:21 -0700 | [diff] [blame] | 540 | self.log.debug("simpleQuery: %s" % query) |
James E. Blair | f8ff993 | 2014-08-15 15:24:24 -0700 | [diff] [blame] | 541 | self.queries.append(query) |
James E. Blair | 5ee2425 | 2014-12-30 10:12:29 -0800 | [diff] [blame] | 542 | if query.startswith('change:'): |
| 543 | # Query a specific changeid |
| 544 | changeid = query[len('change:'):] |
| 545 | l = [change.query() for change in self.changes.values() |
| 546 | if change.data['id'] == changeid] |
James E. Blair | 96698e2 | 2015-04-02 07:48:21 -0700 | [diff] [blame] | 547 | elif query.startswith('message:'): |
| 548 | # Query the content of a commit message |
| 549 | msg = query[len('message:'):].strip() |
| 550 | l = [change.query() for change in self.changes.values() |
| 551 | if msg in change.data['commitMessage']] |
James E. Blair | 5ee2425 | 2014-12-30 10:12:29 -0800 | [diff] [blame] | 552 | else: |
| 553 | # Query all open changes |
| 554 | l = [change.query() for change in self.changes.values()] |
James E. Blair | f8ff993 | 2014-08-15 15:24:24 -0700 | [diff] [blame] | 555 | return l |
James E. Blair | c494d54 | 2014-08-06 09:23:52 -0700 | [diff] [blame] | 556 | |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 557 | def _start_watcher_thread(self, *args, **kw): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 558 | pass |
| 559 | |
Tobias Henkel | d91b4d7 | 2017-05-23 15:43:40 +0200 | [diff] [blame] | 560 | def _uploadPack(self, project): |
| 561 | ret = ('00a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00' |
| 562 | 'multi_ack thin-pack side-band side-band-64k ofs-delta ' |
| 563 | 'shallow no-progress include-tag multi_ack_detailed no-done\n') |
| 564 | path = os.path.join(self.upstream_root, project.name) |
| 565 | repo = git.Repo(path) |
| 566 | for ref in repo.refs: |
| 567 | r = ref.object.hexsha + ' ' + ref.path + '\n' |
| 568 | ret += '%04x%s' % (len(r) + 4, r) |
| 569 | ret += '0000' |
| 570 | return ret |
| 571 | |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 572 | def getGitUrl(self, project): |
| 573 | return os.path.join(self.upstream_root, project.name) |
| 574 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 575 | |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 576 | class GithubChangeReference(git.Reference): |
| 577 | _common_path_default = "refs/pull" |
| 578 | _points_to_commits_only = True |
| 579 | |
| 580 | |
Tobias Henkel | 64e37a0 | 2017-08-02 10:13:30 +0200 | [diff] [blame] | 581 | class FakeGithub(object): |
| 582 | |
| 583 | class FakeUser(object): |
| 584 | def __init__(self, login): |
| 585 | self.login = login |
| 586 | self.name = "Github User" |
| 587 | self.email = "github.user@example.com" |
| 588 | |
Tobias Henkel | 90b32ea | 2017-08-02 16:22:08 +0200 | [diff] [blame] | 589 | class FakeBranch(object): |
| 590 | def __init__(self, branch='master'): |
| 591 | self.name = branch |
| 592 | |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 593 | class FakeStatus(object): |
| 594 | def __init__(self, state, url, description, context, user): |
| 595 | self._state = state |
| 596 | self._url = url |
| 597 | self._description = description |
| 598 | self._context = context |
| 599 | self._user = user |
| 600 | |
| 601 | def as_dict(self): |
| 602 | return { |
| 603 | 'state': self._state, |
| 604 | 'url': self._url, |
| 605 | 'description': self._description, |
| 606 | 'context': self._context, |
| 607 | 'creator': { |
| 608 | 'login': self._user |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | class FakeCommit(object): |
| 613 | def __init__(self): |
| 614 | self._statuses = [] |
| 615 | |
| 616 | def set_status(self, state, url, description, context, user): |
| 617 | status = FakeGithub.FakeStatus( |
| 618 | state, url, description, context, user) |
| 619 | # always insert a status to the front of the list, to represent |
| 620 | # the last status provided for a commit. |
| 621 | self._statuses.insert(0, status) |
| 622 | |
| 623 | def statuses(self): |
| 624 | return self._statuses |
| 625 | |
Tobias Henkel | 90b32ea | 2017-08-02 16:22:08 +0200 | [diff] [blame] | 626 | class FakeRepository(object): |
| 627 | def __init__(self): |
| 628 | self._branches = [FakeGithub.FakeBranch()] |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 629 | self._commits = {} |
Tobias Henkel | 90b32ea | 2017-08-02 16:22:08 +0200 | [diff] [blame] | 630 | |
Tobias Henkel | eca4620 | 2017-08-02 20:27:10 +0200 | [diff] [blame] | 631 | def branches(self, protected=False): |
| 632 | if protected: |
| 633 | # simulate there is no protected branch |
| 634 | return [] |
Tobias Henkel | 90b32ea | 2017-08-02 16:22:08 +0200 | [diff] [blame] | 635 | return self._branches |
| 636 | |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 637 | def create_status(self, sha, state, url, description, context, |
| 638 | user='zuul'): |
| 639 | # Since we're bypassing github API, which would require a user, we |
| 640 | # default the user as 'zuul' here. |
| 641 | commit = self._commits.get(sha, None) |
| 642 | if commit is None: |
| 643 | commit = FakeGithub.FakeCommit() |
| 644 | self._commits[sha] = commit |
| 645 | commit.set_status(state, url, description, context, user) |
| 646 | |
| 647 | def commit(self, sha): |
| 648 | commit = self._commits.get(sha, None) |
| 649 | if commit is None: |
| 650 | commit = FakeGithub.FakeCommit() |
| 651 | self._commits[sha] = commit |
| 652 | return commit |
| 653 | |
| 654 | def __init__(self): |
| 655 | self._repos = {} |
| 656 | |
Tobias Henkel | 64e37a0 | 2017-08-02 10:13:30 +0200 | [diff] [blame] | 657 | def user(self, login): |
| 658 | return self.FakeUser(login) |
| 659 | |
Tobias Henkel | 90b32ea | 2017-08-02 16:22:08 +0200 | [diff] [blame] | 660 | def repository(self, owner, proj): |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 661 | return self._repos.get((owner, proj), None) |
| 662 | |
| 663 | def repo_from_project(self, project): |
| 664 | # This is a convenience method for the tests. |
| 665 | owner, proj = project.split('/') |
| 666 | return self.repository(owner, proj) |
| 667 | |
| 668 | def addProject(self, project): |
| 669 | owner, proj = project.name.split('/') |
| 670 | self._repos[(owner, proj)] = self.FakeRepository() |
Tobias Henkel | 90b32ea | 2017-08-02 16:22:08 +0200 | [diff] [blame] | 671 | |
Tobias Henkel | 64e37a0 | 2017-08-02 10:13:30 +0200 | [diff] [blame] | 672 | |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 673 | class FakeGithubPullRequest(object): |
| 674 | |
| 675 | def __init__(self, github, number, project, branch, |
Jesse Keating | ae4cd27 | 2017-01-30 17:10:44 -0800 | [diff] [blame] | 676 | subject, upstream_root, files=[], number_of_commits=1, |
Jesse Keating | 152a402 | 2017-07-07 08:39:52 -0700 | [diff] [blame] | 677 | writers=[], body=None): |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 678 | """Creates a new PR with several commits. |
| 679 | Sends an event about opened PR.""" |
| 680 | self.github = github |
| 681 | self.source = github |
| 682 | self.number = number |
| 683 | self.project = project |
| 684 | self.branch = branch |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 685 | self.subject = subject |
Jesse Keating | a41566f | 2017-06-14 18:17:51 -0700 | [diff] [blame] | 686 | self.body = body |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 687 | self.number_of_commits = 0 |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 688 | self.upstream_root = upstream_root |
Jan Hruban | 570d01c | 2016-03-10 21:51:32 +0100 | [diff] [blame] | 689 | self.files = [] |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 690 | self.comments = [] |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 691 | self.labels = [] |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 692 | self.statuses = {} |
Jesse Keating | ae4cd27 | 2017-01-30 17:10:44 -0800 | [diff] [blame] | 693 | self.reviews = [] |
| 694 | self.writers = [] |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 695 | self.updated_at = None |
| 696 | self.head_sha = None |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 697 | self.is_merged = False |
Jan Hruban | 3b41592 | 2016-02-03 13:10:22 +0100 | [diff] [blame] | 698 | self.merge_message = None |
Jesse Keating | 4a27f13 | 2017-05-25 16:44:01 -0700 | [diff] [blame] | 699 | self.state = 'open' |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 700 | self._createPRRef() |
Jan Hruban | 570d01c | 2016-03-10 21:51:32 +0100 | [diff] [blame] | 701 | self._addCommitToRepo(files=files) |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 702 | self._updateTimeStamp() |
| 703 | |
Jan Hruban | 570d01c | 2016-03-10 21:51:32 +0100 | [diff] [blame] | 704 | def addCommit(self, files=[]): |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 705 | """Adds a commit on top of the actual PR head.""" |
Jan Hruban | 570d01c | 2016-03-10 21:51:32 +0100 | [diff] [blame] | 706 | self._addCommitToRepo(files=files) |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 707 | self._updateTimeStamp() |
| 708 | |
Jan Hruban | 570d01c | 2016-03-10 21:51:32 +0100 | [diff] [blame] | 709 | def forcePush(self, files=[]): |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 710 | """Clears actual commits and add a commit on top of the base.""" |
Jan Hruban | 570d01c | 2016-03-10 21:51:32 +0100 | [diff] [blame] | 711 | self._addCommitToRepo(files=files, reset=True) |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 712 | self._updateTimeStamp() |
| 713 | |
| 714 | def getPullRequestOpenedEvent(self): |
| 715 | return self._getPullRequestEvent('opened') |
| 716 | |
| 717 | def getPullRequestSynchronizeEvent(self): |
| 718 | return self._getPullRequestEvent('synchronize') |
| 719 | |
| 720 | def getPullRequestReopenedEvent(self): |
| 721 | return self._getPullRequestEvent('reopened') |
| 722 | |
| 723 | def getPullRequestClosedEvent(self): |
| 724 | return self._getPullRequestEvent('closed') |
| 725 | |
Jesse Keating | a41566f | 2017-06-14 18:17:51 -0700 | [diff] [blame] | 726 | def getPullRequestEditedEvent(self): |
| 727 | return self._getPullRequestEvent('edited') |
| 728 | |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 729 | def addComment(self, message): |
| 730 | self.comments.append(message) |
| 731 | self._updateTimeStamp() |
| 732 | |
Jan Hruban | c7ab160 | 2015-10-14 15:29:33 +0200 | [diff] [blame] | 733 | def getCommentAddedEvent(self, text): |
| 734 | name = 'issue_comment' |
| 735 | data = { |
| 736 | 'action': 'created', |
| 737 | 'issue': { |
| 738 | 'number': self.number |
| 739 | }, |
| 740 | 'comment': { |
| 741 | 'body': text |
| 742 | }, |
| 743 | 'repository': { |
| 744 | 'full_name': self.project |
Jan Hruban | 3b41592 | 2016-02-03 13:10:22 +0100 | [diff] [blame] | 745 | }, |
| 746 | 'sender': { |
| 747 | 'login': 'ghuser' |
Jan Hruban | c7ab160 | 2015-10-14 15:29:33 +0200 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | return (name, data) |
| 751 | |
Jesse Keating | 5c05a9f | 2017-01-12 14:44:58 -0800 | [diff] [blame] | 752 | def getReviewAddedEvent(self, review): |
| 753 | name = 'pull_request_review' |
| 754 | data = { |
| 755 | 'action': 'submitted', |
| 756 | 'pull_request': { |
| 757 | 'number': self.number, |
| 758 | 'title': self.subject, |
| 759 | 'updated_at': self.updated_at, |
| 760 | 'base': { |
| 761 | 'ref': self.branch, |
| 762 | 'repo': { |
| 763 | 'full_name': self.project |
| 764 | } |
| 765 | }, |
| 766 | 'head': { |
| 767 | 'sha': self.head_sha |
| 768 | } |
| 769 | }, |
| 770 | 'review': { |
| 771 | 'state': review |
| 772 | }, |
| 773 | 'repository': { |
| 774 | 'full_name': self.project |
| 775 | }, |
| 776 | 'sender': { |
| 777 | 'login': 'ghuser' |
| 778 | } |
| 779 | } |
| 780 | return (name, data) |
| 781 | |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 782 | def addLabel(self, name): |
| 783 | if name not in self.labels: |
| 784 | self.labels.append(name) |
| 785 | self._updateTimeStamp() |
| 786 | return self._getLabelEvent(name) |
| 787 | |
| 788 | def removeLabel(self, name): |
| 789 | if name in self.labels: |
| 790 | self.labels.remove(name) |
| 791 | self._updateTimeStamp() |
| 792 | return self._getUnlabelEvent(name) |
| 793 | |
| 794 | def _getLabelEvent(self, label): |
| 795 | name = 'pull_request' |
| 796 | data = { |
| 797 | 'action': 'labeled', |
| 798 | 'pull_request': { |
| 799 | 'number': self.number, |
| 800 | 'updated_at': self.updated_at, |
| 801 | 'base': { |
| 802 | 'ref': self.branch, |
| 803 | 'repo': { |
| 804 | 'full_name': self.project |
| 805 | } |
| 806 | }, |
| 807 | 'head': { |
| 808 | 'sha': self.head_sha |
| 809 | } |
| 810 | }, |
| 811 | 'label': { |
| 812 | 'name': label |
Jan Hruban | 3b41592 | 2016-02-03 13:10:22 +0100 | [diff] [blame] | 813 | }, |
| 814 | 'sender': { |
| 815 | 'login': 'ghuser' |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | return (name, data) |
| 819 | |
| 820 | def _getUnlabelEvent(self, label): |
| 821 | name = 'pull_request' |
| 822 | data = { |
| 823 | 'action': 'unlabeled', |
| 824 | 'pull_request': { |
| 825 | 'number': self.number, |
Jan Hruban | 3b41592 | 2016-02-03 13:10:22 +0100 | [diff] [blame] | 826 | 'title': self.subject, |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 827 | 'updated_at': self.updated_at, |
| 828 | 'base': { |
| 829 | 'ref': self.branch, |
| 830 | 'repo': { |
| 831 | 'full_name': self.project |
| 832 | } |
| 833 | }, |
| 834 | 'head': { |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 835 | 'sha': self.head_sha, |
| 836 | 'repo': { |
| 837 | 'full_name': self.project |
| 838 | } |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 839 | } |
| 840 | }, |
| 841 | 'label': { |
| 842 | 'name': label |
Jan Hruban | 3b41592 | 2016-02-03 13:10:22 +0100 | [diff] [blame] | 843 | }, |
| 844 | 'sender': { |
| 845 | 'login': 'ghuser' |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | return (name, data) |
| 849 | |
Jesse Keating | a41566f | 2017-06-14 18:17:51 -0700 | [diff] [blame] | 850 | def editBody(self, body): |
| 851 | self.body = body |
| 852 | self._updateTimeStamp() |
| 853 | |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 854 | def _getRepo(self): |
| 855 | repo_path = os.path.join(self.upstream_root, self.project) |
| 856 | return git.Repo(repo_path) |
| 857 | |
| 858 | def _createPRRef(self): |
| 859 | repo = self._getRepo() |
| 860 | GithubChangeReference.create( |
| 861 | repo, self._getPRReference(), 'refs/tags/init') |
| 862 | |
Jan Hruban | 570d01c | 2016-03-10 21:51:32 +0100 | [diff] [blame] | 863 | def _addCommitToRepo(self, files=[], reset=False): |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 864 | repo = self._getRepo() |
| 865 | ref = repo.references[self._getPRReference()] |
| 866 | if reset: |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 867 | self.number_of_commits = 0 |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 868 | ref.set_object('refs/tags/init') |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 869 | self.number_of_commits += 1 |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 870 | repo.head.reference = ref |
| 871 | zuul.merger.merger.reset_repo_to_head(repo) |
| 872 | repo.git.clean('-x', '-f', '-d') |
| 873 | |
Jan Hruban | 570d01c | 2016-03-10 21:51:32 +0100 | [diff] [blame] | 874 | if files: |
| 875 | fn = files[0] |
| 876 | self.files = files |
| 877 | else: |
| 878 | fn = '%s-%s' % (self.branch.replace('/', '_'), self.number) |
| 879 | self.files = [fn] |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 880 | msg = self.subject + '-' + str(self.number_of_commits) |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 881 | fn = os.path.join(repo.working_dir, fn) |
| 882 | f = open(fn, 'w') |
| 883 | with open(fn, 'w') as f: |
| 884 | f.write("test %s %s\n" % |
| 885 | (self.branch, self.number)) |
| 886 | repo.index.add([fn]) |
| 887 | |
| 888 | self.head_sha = repo.index.commit(msg).hexsha |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 889 | # Create an empty set of statuses for the given sha, |
| 890 | # each sha on a PR may have a status set on it |
| 891 | self.statuses[self.head_sha] = [] |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 892 | repo.head.reference = 'master' |
| 893 | zuul.merger.merger.reset_repo_to_head(repo) |
| 894 | repo.git.clean('-x', '-f', '-d') |
| 895 | repo.heads['master'].checkout() |
| 896 | |
| 897 | def _updateTimeStamp(self): |
| 898 | self.updated_at = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime()) |
| 899 | |
| 900 | def getPRHeadSha(self): |
| 901 | repo = self._getRepo() |
| 902 | return repo.references[self._getPRReference()].commit.hexsha |
| 903 | |
Jesse Keating | ae4cd27 | 2017-01-30 17:10:44 -0800 | [diff] [blame] | 904 | def addReview(self, user, state, granted_on=None): |
Adam Gandelman | d81dd76 | 2017-02-09 15:15:49 -0800 | [diff] [blame] | 905 | gh_time_format = '%Y-%m-%dT%H:%M:%SZ' |
| 906 | # convert the timestamp to a str format that would be returned |
| 907 | # from github as 'submitted_at' in the API response |
Jesse Keating | ae4cd27 | 2017-01-30 17:10:44 -0800 | [diff] [blame] | 908 | |
Adam Gandelman | d81dd76 | 2017-02-09 15:15:49 -0800 | [diff] [blame] | 909 | if granted_on: |
| 910 | granted_on = datetime.datetime.utcfromtimestamp(granted_on) |
| 911 | submitted_at = time.strftime( |
| 912 | gh_time_format, granted_on.timetuple()) |
| 913 | else: |
| 914 | # github timestamps only down to the second, so we need to make |
| 915 | # sure reviews that tests add appear to be added over a period of |
| 916 | # time in the past and not all at once. |
| 917 | if not self.reviews: |
| 918 | # the first review happens 10 mins ago |
| 919 | offset = 600 |
| 920 | else: |
| 921 | # subsequent reviews happen 1 minute closer to now |
| 922 | offset = 600 - (len(self.reviews) * 60) |
| 923 | |
| 924 | granted_on = datetime.datetime.utcfromtimestamp( |
| 925 | time.time() - offset) |
| 926 | submitted_at = time.strftime( |
| 927 | gh_time_format, granted_on.timetuple()) |
| 928 | |
Jesse Keating | ae4cd27 | 2017-01-30 17:10:44 -0800 | [diff] [blame] | 929 | self.reviews.append({ |
| 930 | 'state': state, |
| 931 | 'user': { |
| 932 | 'login': user, |
| 933 | 'email': user + "@derp.com", |
| 934 | }, |
Adam Gandelman | d81dd76 | 2017-02-09 15:15:49 -0800 | [diff] [blame] | 935 | 'submitted_at': submitted_at, |
Jesse Keating | ae4cd27 | 2017-01-30 17:10:44 -0800 | [diff] [blame] | 936 | }) |
| 937 | |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 938 | def _getPRReference(self): |
| 939 | return '%s/head' % self.number |
| 940 | |
| 941 | def _getPullRequestEvent(self, action): |
| 942 | name = 'pull_request' |
| 943 | data = { |
| 944 | 'action': action, |
| 945 | 'number': self.number, |
| 946 | 'pull_request': { |
| 947 | 'number': self.number, |
Jan Hruban | 3b41592 | 2016-02-03 13:10:22 +0100 | [diff] [blame] | 948 | 'title': self.subject, |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 949 | 'updated_at': self.updated_at, |
| 950 | 'base': { |
| 951 | 'ref': self.branch, |
| 952 | 'repo': { |
| 953 | 'full_name': self.project |
| 954 | } |
| 955 | }, |
| 956 | 'head': { |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 957 | 'sha': self.head_sha, |
| 958 | 'repo': { |
| 959 | 'full_name': self.project |
| 960 | } |
Jesse Keating | a41566f | 2017-06-14 18:17:51 -0700 | [diff] [blame] | 961 | }, |
| 962 | 'body': self.body |
Jan Hruban | 3b41592 | 2016-02-03 13:10:22 +0100 | [diff] [blame] | 963 | }, |
| 964 | 'sender': { |
| 965 | 'login': 'ghuser' |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | return (name, data) |
| 969 | |
Adam Gandelman | 8c6eeb5 | 2017-01-23 16:31:06 -0800 | [diff] [blame] | 970 | def getCommitStatusEvent(self, context, state='success', user='zuul'): |
| 971 | name = 'status' |
| 972 | data = { |
| 973 | 'state': state, |
| 974 | 'sha': self.head_sha, |
Jesse Keating | 9021a01 | 2017-08-29 14:45:27 -0700 | [diff] [blame] | 975 | 'name': self.project, |
Adam Gandelman | 8c6eeb5 | 2017-01-23 16:31:06 -0800 | [diff] [blame] | 976 | 'description': 'Test results for %s: %s' % (self.head_sha, state), |
| 977 | 'target_url': 'http://zuul/%s' % self.head_sha, |
| 978 | 'branches': [], |
| 979 | 'context': context, |
| 980 | 'sender': { |
| 981 | 'login': user |
| 982 | } |
| 983 | } |
| 984 | return (name, data) |
| 985 | |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 986 | def setMerged(self, commit_message): |
| 987 | self.is_merged = True |
| 988 | self.merge_message = commit_message |
| 989 | |
| 990 | repo = self._getRepo() |
| 991 | repo.heads[self.branch].commit = repo.commit(self.head_sha) |
| 992 | |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 993 | |
| 994 | class FakeGithubConnection(githubconnection.GithubConnection): |
| 995 | log = logging.getLogger("zuul.test.FakeGithubConnection") |
| 996 | |
| 997 | def __init__(self, driver, connection_name, connection_config, |
| 998 | upstream_root=None): |
| 999 | super(FakeGithubConnection, self).__init__(driver, connection_name, |
| 1000 | connection_config) |
| 1001 | self.connection_name = connection_name |
| 1002 | self.pr_number = 0 |
| 1003 | self.pull_requests = [] |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 1004 | self.statuses = {} |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 1005 | self.upstream_root = upstream_root |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 1006 | self.merge_failure = False |
| 1007 | self.merge_not_allowed_count = 0 |
Jesse Keating | 08dab8f | 2017-06-21 12:59:23 +0100 | [diff] [blame] | 1008 | self.reports = [] |
Tobias Henkel | 64e37a0 | 2017-08-02 10:13:30 +0200 | [diff] [blame] | 1009 | self.github_client = FakeGithub() |
| 1010 | |
| 1011 | def getGithubClient(self, |
| 1012 | project=None, |
Jesse Keating | 97b4248 | 2017-09-12 16:13:13 -0600 | [diff] [blame] | 1013 | user_id=None): |
Tobias Henkel | 64e37a0 | 2017-08-02 10:13:30 +0200 | [diff] [blame] | 1014 | return self.github_client |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 1015 | |
Jesse Keating | a41566f | 2017-06-14 18:17:51 -0700 | [diff] [blame] | 1016 | def openFakePullRequest(self, project, branch, subject, files=[], |
Jesse Keating | 152a402 | 2017-07-07 08:39:52 -0700 | [diff] [blame] | 1017 | body=None): |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 1018 | self.pr_number += 1 |
| 1019 | pull_request = FakeGithubPullRequest( |
Jan Hruban | 570d01c | 2016-03-10 21:51:32 +0100 | [diff] [blame] | 1020 | self, self.pr_number, project, branch, subject, self.upstream_root, |
Jesse Keating | a41566f | 2017-06-14 18:17:51 -0700 | [diff] [blame] | 1021 | files=files, body=body) |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 1022 | self.pull_requests.append(pull_request) |
| 1023 | return pull_request |
| 1024 | |
Jesse Keating | 71a47ff | 2017-06-06 11:36:43 -0700 | [diff] [blame] | 1025 | def getPushEvent(self, project, ref, old_rev=None, new_rev=None, |
| 1026 | added_files=[], removed_files=[], modified_files=[]): |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 1027 | if not old_rev: |
James E. Blair | b8203e4 | 2017-08-02 17:00:14 -0700 | [diff] [blame] | 1028 | old_rev = '0' * 40 |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 1029 | if not new_rev: |
| 1030 | new_rev = random_sha1() |
| 1031 | name = 'push' |
| 1032 | data = { |
| 1033 | 'ref': ref, |
| 1034 | 'before': old_rev, |
| 1035 | 'after': new_rev, |
| 1036 | 'repository': { |
| 1037 | 'full_name': project |
Jesse Keating | 71a47ff | 2017-06-06 11:36:43 -0700 | [diff] [blame] | 1038 | }, |
| 1039 | 'commits': [ |
| 1040 | { |
| 1041 | 'added': added_files, |
| 1042 | 'removed': removed_files, |
| 1043 | 'modified': modified_files |
| 1044 | } |
| 1045 | ] |
Wayne | 1a78c61 | 2015-06-11 17:14:13 -0700 | [diff] [blame] | 1046 | } |
| 1047 | return (name, data) |
| 1048 | |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 1049 | def emitEvent(self, event): |
| 1050 | """Emulates sending the GitHub webhook event to the connection.""" |
| 1051 | port = self.webapp.server.socket.getsockname()[1] |
| 1052 | name, data = event |
Clint Byrum | 607d10e | 2017-05-18 12:05:13 -0700 | [diff] [blame] | 1053 | payload = json.dumps(data).encode('utf8') |
Clint Byrum | cf1b742 | 2017-07-27 17:12:00 -0700 | [diff] [blame] | 1054 | secret = self.connection_config['webhook_token'] |
| 1055 | signature = githubconnection._sign_request(payload, secret) |
| 1056 | headers = {'X-Github-Event': name, 'X-Hub-Signature': signature} |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 1057 | req = urllib.request.Request( |
| 1058 | 'http://localhost:%s/connection/%s/payload' |
| 1059 | % (port, self.connection_name), |
| 1060 | data=payload, headers=headers) |
Tristan Cacqueray | 2bafb1f | 2017-06-12 07:10:26 +0000 | [diff] [blame] | 1061 | return urllib.request.urlopen(req) |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 1062 | |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 1063 | def addProject(self, project): |
| 1064 | # use the original method here and additionally register it in the |
| 1065 | # fake github |
| 1066 | super(FakeGithubConnection, self).addProject(project) |
| 1067 | self.getGithubClient(project).addProject(project) |
| 1068 | |
Jan Hruban | c7ab160 | 2015-10-14 15:29:33 +0200 | [diff] [blame] | 1069 | def getPull(self, project, number): |
| 1070 | pr = self.pull_requests[number - 1] |
| 1071 | data = { |
| 1072 | 'number': number, |
Jan Hruban | 3b41592 | 2016-02-03 13:10:22 +0100 | [diff] [blame] | 1073 | 'title': pr.subject, |
Jan Hruban | c7ab160 | 2015-10-14 15:29:33 +0200 | [diff] [blame] | 1074 | 'updated_at': pr.updated_at, |
| 1075 | 'base': { |
| 1076 | 'repo': { |
| 1077 | 'full_name': pr.project |
| 1078 | }, |
| 1079 | 'ref': pr.branch, |
| 1080 | }, |
Jan Hruban | 37615e5 | 2015-11-19 14:30:49 +0100 | [diff] [blame] | 1081 | 'mergeable': True, |
Jesse Keating | 4a27f13 | 2017-05-25 16:44:01 -0700 | [diff] [blame] | 1082 | 'state': pr.state, |
Jan Hruban | c7ab160 | 2015-10-14 15:29:33 +0200 | [diff] [blame] | 1083 | 'head': { |
Jesse Keating | d96e588 | 2017-01-19 13:55:50 -0800 | [diff] [blame] | 1084 | 'sha': pr.head_sha, |
| 1085 | 'repo': { |
| 1086 | 'full_name': pr.project |
| 1087 | } |
Jesse Keating | 61040e7 | 2017-06-08 15:08:27 -0700 | [diff] [blame] | 1088 | }, |
Jesse Keating | 19dfb49 | 2017-06-13 12:32:33 -0700 | [diff] [blame] | 1089 | 'files': pr.files, |
Jesse Keating | a41566f | 2017-06-14 18:17:51 -0700 | [diff] [blame] | 1090 | 'labels': pr.labels, |
| 1091 | 'merged': pr.is_merged, |
| 1092 | 'body': pr.body |
Jan Hruban | c7ab160 | 2015-10-14 15:29:33 +0200 | [diff] [blame] | 1093 | } |
| 1094 | return data |
| 1095 | |
Jesse Keating | 9021a01 | 2017-08-29 14:45:27 -0700 | [diff] [blame] | 1096 | def getPullBySha(self, sha, project): |
| 1097 | prs = list(set([p for p in self.pull_requests if |
| 1098 | sha == p.head_sha and project == p.project])) |
Adam Gandelman | 8c6eeb5 | 2017-01-23 16:31:06 -0800 | [diff] [blame] | 1099 | if len(prs) > 1: |
| 1100 | raise Exception('Multiple pulls found with head sha: %s' % sha) |
| 1101 | pr = prs[0] |
| 1102 | return self.getPull(pr.project, pr.number) |
| 1103 | |
Jesse Keating | ae4cd27 | 2017-01-30 17:10:44 -0800 | [diff] [blame] | 1104 | def _getPullReviews(self, owner, project, number): |
| 1105 | pr = self.pull_requests[number - 1] |
| 1106 | return pr.reviews |
| 1107 | |
Jesse Keating | ae4cd27 | 2017-01-30 17:10:44 -0800 | [diff] [blame] | 1108 | def getRepoPermission(self, project, login): |
| 1109 | owner, proj = project.split('/') |
| 1110 | for pr in self.pull_requests: |
| 1111 | pr_owner, pr_project = pr.project.split('/') |
| 1112 | if (pr_owner == owner and proj == pr_project): |
| 1113 | if login in pr.writers: |
| 1114 | return 'write' |
| 1115 | else: |
| 1116 | return 'read' |
| 1117 | |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 1118 | def getGitUrl(self, project): |
| 1119 | return os.path.join(self.upstream_root, str(project)) |
| 1120 | |
Jan Hruban | 6d53c5e | 2015-10-24 03:03:34 +0200 | [diff] [blame] | 1121 | def real_getGitUrl(self, project): |
| 1122 | return super(FakeGithubConnection, self).getGitUrl(project) |
| 1123 | |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 1124 | def commentPull(self, project, pr_number, message): |
Jesse Keating | 08dab8f | 2017-06-21 12:59:23 +0100 | [diff] [blame] | 1125 | # record that this got reported |
| 1126 | self.reports.append((project, pr_number, 'comment')) |
Wayne | 40f4004 | 2015-06-12 16:56:30 -0700 | [diff] [blame] | 1127 | pull_request = self.pull_requests[pr_number - 1] |
| 1128 | pull_request.addComment(message) |
| 1129 | |
Jan Hruban | 3b41592 | 2016-02-03 13:10:22 +0100 | [diff] [blame] | 1130 | def mergePull(self, project, pr_number, commit_message='', sha=None): |
Jesse Keating | 08dab8f | 2017-06-21 12:59:23 +0100 | [diff] [blame] | 1131 | # record that this got reported |
| 1132 | self.reports.append((project, pr_number, 'merge')) |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 1133 | pull_request = self.pull_requests[pr_number - 1] |
| 1134 | if self.merge_failure: |
| 1135 | raise Exception('Pull request was not merged') |
| 1136 | if self.merge_not_allowed_count > 0: |
| 1137 | self.merge_not_allowed_count -= 1 |
| 1138 | raise MergeFailure('Merge was not successful due to mergeability' |
| 1139 | ' conflict') |
James E. Blair | 289f593 | 2017-07-27 15:02:29 -0700 | [diff] [blame] | 1140 | pull_request.setMerged(commit_message) |
Jan Hruban | 49bff07 | 2015-11-03 11:45:46 +0100 | [diff] [blame] | 1141 | |
Jesse Keating | 1f7ebe9 | 2017-06-12 17:21:00 -0700 | [diff] [blame] | 1142 | def setCommitStatus(self, project, sha, state, url='', description='', |
| 1143 | context='default', user='zuul'): |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 1144 | # record that this got reported and call original method |
Jesse Keating | 08dab8f | 2017-06-21 12:59:23 +0100 | [diff] [blame] | 1145 | self.reports.append((project, sha, 'status', (user, context, state))) |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 1146 | super(FakeGithubConnection, self).setCommitStatus( |
| 1147 | project, sha, state, |
| 1148 | url=url, description=description, context=context) |
Jan Hruban | e252a73 | 2017-01-03 15:03:09 +0100 | [diff] [blame] | 1149 | |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 1150 | def labelPull(self, project, pr_number, label): |
Jesse Keating | 08dab8f | 2017-06-21 12:59:23 +0100 | [diff] [blame] | 1151 | # record that this got reported |
| 1152 | self.reports.append((project, pr_number, 'label', label)) |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 1153 | pull_request = self.pull_requests[pr_number - 1] |
| 1154 | pull_request.addLabel(label) |
| 1155 | |
| 1156 | def unlabelPull(self, project, pr_number, label): |
Jesse Keating | 08dab8f | 2017-06-21 12:59:23 +0100 | [diff] [blame] | 1157 | # record that this got reported |
| 1158 | self.reports.append((project, pr_number, 'unlabel', label)) |
Jan Hruban | 16ad31f | 2015-11-07 14:39:07 +0100 | [diff] [blame] | 1159 | pull_request = self.pull_requests[pr_number - 1] |
| 1160 | pull_request.removeLabel(label) |
| 1161 | |
Jesse Keating | a41566f | 2017-06-14 18:17:51 -0700 | [diff] [blame] | 1162 | def _getNeededByFromPR(self, change): |
| 1163 | prs = [] |
| 1164 | pattern = re.compile(r"Depends-On.*https://%s/%s/pull/%s" % |
James E. Blair | 5f11ff3 | 2017-06-23 21:46:45 +0100 | [diff] [blame] | 1165 | (self.server, change.project.name, |
Jesse Keating | a41566f | 2017-06-14 18:17:51 -0700 | [diff] [blame] | 1166 | change.number)) |
| 1167 | for pr in self.pull_requests: |
Jesse Keating | 152a402 | 2017-07-07 08:39:52 -0700 | [diff] [blame] | 1168 | if not pr.body: |
| 1169 | body = '' |
| 1170 | else: |
| 1171 | body = pr.body |
| 1172 | if pattern.search(body): |
Jesse Keating | a41566f | 2017-06-14 18:17:51 -0700 | [diff] [blame] | 1173 | # Get our version of a pull so that it's a dict |
| 1174 | pull = self.getPull(pr.project, pr.number) |
| 1175 | prs.append(pull) |
| 1176 | |
| 1177 | return prs |
| 1178 | |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 1179 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1180 | class BuildHistory(object): |
| 1181 | def __init__(self, **kw): |
| 1182 | self.__dict__.update(kw) |
| 1183 | |
| 1184 | def __repr__(self): |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 1185 | return ("<Completed build, result: %s name: %s uuid: %s " |
| 1186 | "changes: %s ref: %s>" % |
| 1187 | (self.result, self.name, self.uuid, |
| 1188 | self.changes, self.ref)) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1189 | |
| 1190 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1191 | class FakeStatsd(threading.Thread): |
| 1192 | def __init__(self): |
| 1193 | threading.Thread.__init__(self) |
| 1194 | self.daemon = True |
Monty Taylor | 211883d | 2017-09-06 08:40:47 -0500 | [diff] [blame] | 1195 | self.sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1196 | self.sock.bind(('', 0)) |
| 1197 | self.port = self.sock.getsockname()[1] |
| 1198 | self.wake_read, self.wake_write = os.pipe() |
| 1199 | self.stats = [] |
| 1200 | |
| 1201 | def run(self): |
| 1202 | while True: |
| 1203 | poll = select.poll() |
| 1204 | poll.register(self.sock, select.POLLIN) |
| 1205 | poll.register(self.wake_read, select.POLLIN) |
| 1206 | ret = poll.poll() |
| 1207 | for (fd, event) in ret: |
| 1208 | if fd == self.sock.fileno(): |
| 1209 | data = self.sock.recvfrom(1024) |
| 1210 | if not data: |
| 1211 | return |
| 1212 | self.stats.append(data[0]) |
| 1213 | if fd == self.wake_read: |
| 1214 | return |
| 1215 | |
| 1216 | def stop(self): |
Clint Byrum | f322fe2 | 2017-05-10 20:53:12 -0700 | [diff] [blame] | 1217 | os.write(self.wake_write, b'1\n') |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1218 | |
| 1219 | |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1220 | class FakeBuild(object): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1221 | log = logging.getLogger("zuul.test") |
| 1222 | |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1223 | def __init__(self, executor_server, job): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1224 | self.daemon = True |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1225 | self.executor_server = executor_server |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1226 | self.job = job |
James E. Blair | ab7132b | 2016-08-05 12:36:22 -0700 | [diff] [blame] | 1227 | self.jobdir = None |
James E. Blair | 1730297 | 2016-08-10 16:11:42 -0700 | [diff] [blame] | 1228 | self.uuid = job.unique |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1229 | self.parameters = json.loads(job.arguments) |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 1230 | # TODOv3(jeblair): self.node is really "the label of the node |
| 1231 | # assigned". We should rename it (self.node_label?) if we |
James E. Blair | 34776ee | 2016-08-25 13:53:54 -0700 | [diff] [blame] | 1232 | # keep using it like this, or we may end up exposing more of |
| 1233 | # the complexity around multi-node jobs here |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 1234 | # (self.nodes[0].label?) |
James E. Blair | 34776ee | 2016-08-25 13:53:54 -0700 | [diff] [blame] | 1235 | self.node = None |
| 1236 | if len(self.parameters.get('nodes')) == 1: |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 1237 | self.node = self.parameters['nodes'][0]['label'] |
James E. Blair | 74f101b | 2017-07-21 15:32:01 -0700 | [diff] [blame] | 1238 | self.unique = self.parameters['zuul']['build'] |
James E. Blair | e675d68 | 2017-07-21 15:29:35 -0700 | [diff] [blame] | 1239 | self.pipeline = self.parameters['zuul']['pipeline'] |
James E. Blair | e536609 | 2017-07-21 15:30:39 -0700 | [diff] [blame] | 1240 | self.project = self.parameters['zuul']['project']['name'] |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 1241 | self.name = self.parameters['job'] |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1242 | self.wait_condition = threading.Condition() |
| 1243 | self.waiting = False |
| 1244 | self.aborted = False |
Paul Belanger | 71d9817 | 2016-11-08 10:56:31 -0500 | [diff] [blame] | 1245 | self.requeue = False |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1246 | self.created = time.time() |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1247 | self.changes = None |
James E. Blair | 6193a1f | 2017-07-21 15:13:15 -0700 | [diff] [blame] | 1248 | items = self.parameters['zuul']['items'] |
| 1249 | self.changes = ' '.join(['%s,%s' % (x['change'], x['patchset']) |
| 1250 | for x in items if 'change' in x]) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1251 | |
James E. Blair | 3158e28 | 2016-08-19 09:34:11 -0700 | [diff] [blame] | 1252 | def __repr__(self): |
| 1253 | waiting = '' |
| 1254 | if self.waiting: |
| 1255 | waiting = ' [waiting]' |
Jamie Lennox | d2e3733 | 2016-12-05 15:26:19 +1100 | [diff] [blame] | 1256 | return '<FakeBuild %s:%s %s%s>' % (self.pipeline, self.name, |
| 1257 | self.changes, waiting) |
James E. Blair | 3158e28 | 2016-08-19 09:34:11 -0700 | [diff] [blame] | 1258 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1259 | def release(self): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 1260 | """Release this build.""" |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1261 | self.wait_condition.acquire() |
| 1262 | self.wait_condition.notify() |
| 1263 | self.waiting = False |
| 1264 | self.log.debug("Build %s released" % self.unique) |
| 1265 | self.wait_condition.release() |
| 1266 | |
| 1267 | def isWaiting(self): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 1268 | """Return whether this build is being held. |
| 1269 | |
| 1270 | :returns: Whether the build is being held. |
| 1271 | :rtype: bool |
| 1272 | """ |
| 1273 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1274 | self.wait_condition.acquire() |
| 1275 | if self.waiting: |
| 1276 | ret = True |
| 1277 | else: |
| 1278 | ret = False |
| 1279 | self.wait_condition.release() |
| 1280 | return ret |
| 1281 | |
| 1282 | def _wait(self): |
| 1283 | self.wait_condition.acquire() |
| 1284 | self.waiting = True |
| 1285 | self.log.debug("Build %s waiting" % self.unique) |
| 1286 | self.wait_condition.wait() |
| 1287 | self.wait_condition.release() |
| 1288 | |
| 1289 | def run(self): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1290 | self.log.debug('Running build %s' % self.unique) |
| 1291 | |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1292 | if self.executor_server.hold_jobs_in_build: |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1293 | self.log.debug('Holding build %s' % self.unique) |
| 1294 | self._wait() |
| 1295 | self.log.debug("Build %s continuing" % self.unique) |
| 1296 | |
James E. Blair | 412fba8 | 2017-01-26 15:00:50 -0800 | [diff] [blame] | 1297 | result = (RecordingAnsibleJob.RESULT_NORMAL, 0) # Success |
James E. Blair | 247cab7 | 2017-07-20 16:52:36 -0700 | [diff] [blame] | 1298 | if self.shouldFail(): |
James E. Blair | 412fba8 | 2017-01-26 15:00:50 -0800 | [diff] [blame] | 1299 | result = (RecordingAnsibleJob.RESULT_NORMAL, 1) # Failure |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1300 | if self.aborted: |
James E. Blair | 412fba8 | 2017-01-26 15:00:50 -0800 | [diff] [blame] | 1301 | result = (RecordingAnsibleJob.RESULT_ABORTED, None) |
Paul Belanger | 71d9817 | 2016-11-08 10:56:31 -0500 | [diff] [blame] | 1302 | if self.requeue: |
James E. Blair | 412fba8 | 2017-01-26 15:00:50 -0800 | [diff] [blame] | 1303 | result = (RecordingAnsibleJob.RESULT_UNREACHABLE, None) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1304 | |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1305 | return result |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1306 | |
James E. Blair | a5dba23 | 2016-08-08 15:53:24 -0700 | [diff] [blame] | 1307 | def shouldFail(self): |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1308 | changes = self.executor_server.fail_tests.get(self.name, []) |
James E. Blair | a5dba23 | 2016-08-08 15:53:24 -0700 | [diff] [blame] | 1309 | for change in changes: |
| 1310 | if self.hasChanges(change): |
| 1311 | return True |
| 1312 | return False |
| 1313 | |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 1314 | def hasChanges(self, *changes): |
| 1315 | """Return whether this build has certain changes in its git repos. |
| 1316 | |
| 1317 | :arg FakeChange changes: One or more changes (varargs) that |
Clark Boylan | 500992b | 2017-04-03 14:28:24 -0700 | [diff] [blame] | 1318 | are expected to be present (in order) in the git repository of |
| 1319 | the active project. |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 1320 | |
| 1321 | :returns: Whether the build has the indicated changes. |
| 1322 | :rtype: bool |
| 1323 | |
| 1324 | """ |
Clint Byrum | 3343e3e | 2016-11-15 16:05:03 -0800 | [diff] [blame] | 1325 | for change in changes: |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 1326 | hostname = change.source.canonical_hostname |
James E. Blair | 2a53567 | 2017-04-27 12:03:15 -0700 | [diff] [blame] | 1327 | path = os.path.join(self.jobdir.src_root, hostname, change.project) |
Clint Byrum | 3343e3e | 2016-11-15 16:05:03 -0800 | [diff] [blame] | 1328 | try: |
| 1329 | repo = git.Repo(path) |
| 1330 | except NoSuchPathError as e: |
| 1331 | self.log.debug('%s' % e) |
| 1332 | return False |
James E. Blair | 247cab7 | 2017-07-20 16:52:36 -0700 | [diff] [blame] | 1333 | repo_messages = [c.message.strip() for c in repo.iter_commits()] |
Clint Byrum | 3343e3e | 2016-11-15 16:05:03 -0800 | [diff] [blame] | 1334 | commit_message = '%s-1' % change.subject |
| 1335 | self.log.debug("Checking if build %s has changes; commit_message " |
| 1336 | "%s; repo_messages %s" % (self, commit_message, |
| 1337 | repo_messages)) |
| 1338 | if commit_message not in repo_messages: |
James E. Blair | 962220f | 2016-08-03 11:22:38 -0700 | [diff] [blame] | 1339 | self.log.debug(" messages do not match") |
| 1340 | return False |
| 1341 | self.log.debug(" OK") |
| 1342 | return True |
| 1343 | |
James E. Blair | d8af542 | 2017-05-24 13:59:40 -0700 | [diff] [blame] | 1344 | def getWorkspaceRepos(self, projects): |
| 1345 | """Return workspace git repo objects for the listed projects |
| 1346 | |
| 1347 | :arg list projects: A list of strings, each the canonical name |
| 1348 | of a project. |
| 1349 | |
| 1350 | :returns: A dictionary of {name: repo} for every listed |
| 1351 | project. |
| 1352 | :rtype: dict |
| 1353 | |
| 1354 | """ |
| 1355 | |
| 1356 | repos = {} |
| 1357 | for project in projects: |
| 1358 | path = os.path.join(self.jobdir.src_root, project) |
| 1359 | repo = git.Repo(path) |
| 1360 | repos[project] = repo |
| 1361 | return repos |
| 1362 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1363 | |
James E. Blair | 107bb25 | 2017-10-13 15:53:16 -0700 | [diff] [blame] | 1364 | class RecordingAnsibleJob(zuul.executor.server.AnsibleJob): |
| 1365 | def doMergeChanges(self, merger, items, repo_state): |
| 1366 | # Get a merger in order to update the repos involved in this job. |
| 1367 | commit = super(RecordingAnsibleJob, self).doMergeChanges( |
| 1368 | merger, items, repo_state) |
| 1369 | if not commit: # merge conflict |
| 1370 | self.recordResult('MERGER_FAILURE') |
| 1371 | return commit |
| 1372 | |
| 1373 | def recordResult(self, result): |
| 1374 | build = self.executor_server.job_builds[self.job.unique] |
| 1375 | self.executor_server.lock.acquire() |
| 1376 | self.executor_server.build_history.append( |
| 1377 | BuildHistory(name=build.name, result=result, changes=build.changes, |
| 1378 | node=build.node, uuid=build.unique, |
| 1379 | ref=build.parameters['zuul']['ref'], |
| 1380 | parameters=build.parameters, jobdir=build.jobdir, |
| 1381 | pipeline=build.parameters['zuul']['pipeline']) |
| 1382 | ) |
| 1383 | self.executor_server.running_builds.remove(build) |
| 1384 | del self.executor_server.job_builds[self.job.unique] |
| 1385 | self.executor_server.lock.release() |
| 1386 | |
| 1387 | def runPlaybooks(self, args): |
| 1388 | build = self.executor_server.job_builds[self.job.unique] |
| 1389 | build.jobdir = self.jobdir |
| 1390 | |
| 1391 | result = super(RecordingAnsibleJob, self).runPlaybooks(args) |
| 1392 | self.recordResult(result) |
| 1393 | return result |
| 1394 | |
James E. Blair | a86aaf1 | 2017-10-15 20:59:50 -0700 | [diff] [blame] | 1395 | def runAnsible(self, cmd, timeout, playbook, wrapped=True): |
James E. Blair | 107bb25 | 2017-10-13 15:53:16 -0700 | [diff] [blame] | 1396 | build = self.executor_server.job_builds[self.job.unique] |
| 1397 | |
| 1398 | if self.executor_server._run_ansible: |
| 1399 | result = super(RecordingAnsibleJob, self).runAnsible( |
James E. Blair | a86aaf1 | 2017-10-15 20:59:50 -0700 | [diff] [blame] | 1400 | cmd, timeout, playbook, wrapped) |
James E. Blair | 107bb25 | 2017-10-13 15:53:16 -0700 | [diff] [blame] | 1401 | else: |
| 1402 | if playbook.path: |
| 1403 | result = build.run() |
| 1404 | else: |
| 1405 | result = (self.RESULT_NORMAL, 0) |
| 1406 | return result |
| 1407 | |
| 1408 | def getHostList(self, args): |
| 1409 | self.log.debug("hostlist") |
| 1410 | hosts = super(RecordingAnsibleJob, self).getHostList(args) |
| 1411 | for host in hosts: |
| 1412 | host['host_vars']['ansible_connection'] = 'local' |
| 1413 | |
| 1414 | hosts.append(dict( |
| 1415 | name='localhost', |
| 1416 | host_vars=dict(ansible_connection='local'), |
| 1417 | host_keys=[])) |
| 1418 | return hosts |
| 1419 | |
| 1420 | |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1421 | class RecordingExecutorServer(zuul.executor.server.ExecutorServer): |
| 1422 | """An Ansible executor to be used in tests. |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 1423 | |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1424 | :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] | 1425 | they will report that they have started but then pause until |
| 1426 | released before reporting completion. This attribute may be |
| 1427 | changed at any time and will take effect for subsequently |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1428 | executed builds, but previously held builds will still need to |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 1429 | be explicitly released. |
| 1430 | |
| 1431 | """ |
James E. Blair | faf8198 | 2017-10-10 15:42:26 -0700 | [diff] [blame] | 1432 | |
| 1433 | _job_class = RecordingAnsibleJob |
| 1434 | |
James E. Blair | f5dbd00 | 2015-12-23 15:26:17 -0800 | [diff] [blame] | 1435 | def __init__(self, *args, **kw): |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1436 | self._run_ansible = kw.pop('_run_ansible', False) |
James E. Blair | a92cbc8 | 2017-01-23 14:56:49 -0800 | [diff] [blame] | 1437 | self._test_root = kw.pop('_test_root', False) |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1438 | super(RecordingExecutorServer, self).__init__(*args, **kw) |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1439 | self.hold_jobs_in_build = False |
| 1440 | self.lock = threading.Lock() |
| 1441 | self.running_builds = [] |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 1442 | self.build_history = [] |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1443 | self.fail_tests = {} |
James E. Blair | ab7132b | 2016-08-05 12:36:22 -0700 | [diff] [blame] | 1444 | self.job_builds = {} |
James E. Blair | f5dbd00 | 2015-12-23 15:26:17 -0800 | [diff] [blame] | 1445 | |
James E. Blair | a5dba23 | 2016-08-08 15:53:24 -0700 | [diff] [blame] | 1446 | def failJob(self, name, change): |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1447 | """Instruct the executor to report matching builds as failures. |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 1448 | |
| 1449 | :arg str name: The name of the job to fail. |
James E. Blair | a5dba23 | 2016-08-08 15:53:24 -0700 | [diff] [blame] | 1450 | :arg Change change: The :py:class:`~tests.base.FakeChange` |
| 1451 | instance which should cause the job to fail. This job |
| 1452 | will also fail for changes depending on this change. |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 1453 | |
| 1454 | """ |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1455 | l = self.fail_tests.get(name, []) |
| 1456 | l.append(change) |
| 1457 | self.fail_tests[name] = l |
James E. Blair | f5dbd00 | 2015-12-23 15:26:17 -0800 | [diff] [blame] | 1458 | |
James E. Blair | 962220f | 2016-08-03 11:22:38 -0700 | [diff] [blame] | 1459 | def release(self, regex=None): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 1460 | """Release a held build. |
| 1461 | |
| 1462 | :arg str regex: A regular expression which, if supplied, will |
| 1463 | cause only builds with matching names to be released. If |
| 1464 | not supplied, all builds will be released. |
| 1465 | |
| 1466 | """ |
James E. Blair | 962220f | 2016-08-03 11:22:38 -0700 | [diff] [blame] | 1467 | builds = self.running_builds[:] |
| 1468 | self.log.debug("Releasing build %s (%s)" % (regex, |
| 1469 | len(self.running_builds))) |
| 1470 | for build in builds: |
| 1471 | if not regex or re.match(regex, build.name): |
| 1472 | self.log.debug("Releasing build %s" % |
James E. Blair | 74f101b | 2017-07-21 15:32:01 -0700 | [diff] [blame] | 1473 | (build.parameters['zuul']['build'])) |
James E. Blair | 962220f | 2016-08-03 11:22:38 -0700 | [diff] [blame] | 1474 | build.release() |
| 1475 | else: |
| 1476 | self.log.debug("Not releasing build %s" % |
James E. Blair | 74f101b | 2017-07-21 15:32:01 -0700 | [diff] [blame] | 1477 | (build.parameters['zuul']['build'])) |
James E. Blair | 962220f | 2016-08-03 11:22:38 -0700 | [diff] [blame] | 1478 | self.log.debug("Done releasing builds %s (%s)" % |
| 1479 | (regex, len(self.running_builds))) |
| 1480 | |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1481 | def executeJob(self, job): |
James E. Blair | 34776ee | 2016-08-25 13:53:54 -0700 | [diff] [blame] | 1482 | build = FakeBuild(self, job) |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1483 | job.build = build |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1484 | self.running_builds.append(build) |
James E. Blair | ab7132b | 2016-08-05 12:36:22 -0700 | [diff] [blame] | 1485 | self.job_builds[job.unique] = build |
James E. Blair | a92cbc8 | 2017-01-23 14:56:49 -0800 | [diff] [blame] | 1486 | args = json.loads(job.arguments) |
Monty Taylor | d13bc36 | 2017-06-30 13:11:37 -0500 | [diff] [blame] | 1487 | args['zuul']['_test'] = dict(test_root=self._test_root) |
James E. Blair | a92cbc8 | 2017-01-23 14:56:49 -0800 | [diff] [blame] | 1488 | job.arguments = json.dumps(args) |
James E. Blair | faf8198 | 2017-10-10 15:42:26 -0700 | [diff] [blame] | 1489 | super(RecordingExecutorServer, self).executeJob(job) |
James E. Blair | 1730297 | 2016-08-10 16:11:42 -0700 | [diff] [blame] | 1490 | |
| 1491 | def stopJob(self, job): |
| 1492 | self.log.debug("handle stop") |
| 1493 | parameters = json.loads(job.arguments) |
| 1494 | uuid = parameters['uuid'] |
| 1495 | for build in self.running_builds: |
| 1496 | if build.unique == uuid: |
| 1497 | build.aborted = True |
| 1498 | build.release() |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1499 | super(RecordingExecutorServer, self).stopJob(job) |
James E. Blair | ab7132b | 2016-08-05 12:36:22 -0700 | [diff] [blame] | 1500 | |
James E. Blair | a002b03 | 2017-04-18 10:35:48 -0700 | [diff] [blame] | 1501 | def stop(self): |
| 1502 | for build in self.running_builds: |
| 1503 | build.release() |
| 1504 | super(RecordingExecutorServer, self).stop() |
| 1505 | |
Joshua Hesketh | 50c2178 | 2016-10-13 21:34:14 +1100 | [diff] [blame] | 1506 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1507 | class FakeGearmanServer(gear.Server): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 1508 | """A Gearman server for use in tests. |
| 1509 | |
| 1510 | :ivar bool hold_jobs_in_queue: If true, submitted jobs will be |
| 1511 | added to the queue but will not be distributed to workers |
| 1512 | until released. This attribute may be changed at any time and |
| 1513 | will take effect for subsequently enqueued jobs, but |
| 1514 | previously held jobs will still need to be explicitly |
| 1515 | released. |
| 1516 | |
| 1517 | """ |
| 1518 | |
Paul Belanger | 0a21f0a | 2017-06-13 13:14:42 -0400 | [diff] [blame] | 1519 | def __init__(self, use_ssl=False): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1520 | self.hold_jobs_in_queue = False |
James E. Blair | a615c36 | 2017-10-02 17:34:42 -0700 | [diff] [blame] | 1521 | self.hold_merge_jobs_in_queue = False |
Paul Belanger | 0a21f0a | 2017-06-13 13:14:42 -0400 | [diff] [blame] | 1522 | if use_ssl: |
| 1523 | ssl_ca = os.path.join(FIXTURE_DIR, 'gearman/root-ca.pem') |
| 1524 | ssl_cert = os.path.join(FIXTURE_DIR, 'gearman/server.pem') |
| 1525 | ssl_key = os.path.join(FIXTURE_DIR, 'gearman/server.key') |
| 1526 | else: |
| 1527 | ssl_ca = None |
| 1528 | ssl_cert = None |
| 1529 | ssl_key = None |
| 1530 | |
| 1531 | super(FakeGearmanServer, self).__init__(0, ssl_key=ssl_key, |
| 1532 | ssl_cert=ssl_cert, |
| 1533 | ssl_ca=ssl_ca) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1534 | |
| 1535 | def getJobForConnection(self, connection, peek=False): |
Monty Taylor | b934c1a | 2017-06-16 19:31:47 -0500 | [diff] [blame] | 1536 | for job_queue in [self.high_queue, self.normal_queue, self.low_queue]: |
| 1537 | for job in job_queue: |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1538 | if not hasattr(job, 'waiting'): |
Clint Byrum | f322fe2 | 2017-05-10 20:53:12 -0700 | [diff] [blame] | 1539 | if job.name.startswith(b'executor:execute'): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1540 | job.waiting = self.hold_jobs_in_queue |
James E. Blair | a615c36 | 2017-10-02 17:34:42 -0700 | [diff] [blame] | 1541 | elif job.name.startswith(b'merger:'): |
| 1542 | job.waiting = self.hold_merge_jobs_in_queue |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1543 | else: |
| 1544 | job.waiting = False |
| 1545 | if job.waiting: |
| 1546 | continue |
| 1547 | if job.name in connection.functions: |
| 1548 | if not peek: |
Monty Taylor | b934c1a | 2017-06-16 19:31:47 -0500 | [diff] [blame] | 1549 | job_queue.remove(job) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1550 | connection.related_jobs[job.handle] = job |
| 1551 | job.worker_connection = connection |
| 1552 | job.running = True |
| 1553 | return job |
| 1554 | return None |
| 1555 | |
| 1556 | def release(self, regex=None): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 1557 | """Release a held job. |
| 1558 | |
| 1559 | :arg str regex: A regular expression which, if supplied, will |
| 1560 | cause only jobs with matching names to be released. If |
| 1561 | not supplied, all jobs will be released. |
| 1562 | """ |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1563 | released = False |
| 1564 | qlen = (len(self.high_queue) + len(self.normal_queue) + |
| 1565 | len(self.low_queue)) |
| 1566 | self.log.debug("releasing queued job %s (%s)" % (regex, qlen)) |
| 1567 | for job in self.getQueue(): |
James E. Blair | a615c36 | 2017-10-02 17:34:42 -0700 | [diff] [blame] | 1568 | match = False |
| 1569 | if job.name == b'executor:execute': |
| 1570 | parameters = json.loads(job.arguments.decode('utf8')) |
| 1571 | if not regex or re.match(regex, parameters.get('job')): |
| 1572 | match = True |
James E. Blair | 29c7700 | 2017-10-05 14:56:35 -0700 | [diff] [blame] | 1573 | if job.name.startswith(b'merger:'): |
James E. Blair | a615c36 | 2017-10-02 17:34:42 -0700 | [diff] [blame] | 1574 | if not regex: |
| 1575 | match = True |
| 1576 | if match: |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1577 | self.log.debug("releasing queued job %s" % |
| 1578 | job.unique) |
| 1579 | job.waiting = False |
| 1580 | released = True |
| 1581 | else: |
| 1582 | self.log.debug("not releasing queued job %s" % |
| 1583 | job.unique) |
| 1584 | if released: |
| 1585 | self.wakeConnections() |
| 1586 | qlen = (len(self.high_queue) + len(self.normal_queue) + |
| 1587 | len(self.low_queue)) |
| 1588 | self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen)) |
| 1589 | |
| 1590 | |
| 1591 | class FakeSMTP(object): |
| 1592 | log = logging.getLogger('zuul.FakeSMTP') |
| 1593 | |
| 1594 | def __init__(self, messages, server, port): |
| 1595 | self.server = server |
| 1596 | self.port = port |
| 1597 | self.messages = messages |
| 1598 | |
| 1599 | def sendmail(self, from_email, to_email, msg): |
| 1600 | self.log.info("Sending email from %s, to %s, with msg %s" % ( |
| 1601 | from_email, to_email, msg)) |
| 1602 | |
| 1603 | headers = msg.split('\n\n', 1)[0] |
| 1604 | body = msg.split('\n\n', 1)[1] |
| 1605 | |
| 1606 | self.messages.append(dict( |
| 1607 | from_email=from_email, |
| 1608 | to_email=to_email, |
| 1609 | msg=msg, |
| 1610 | headers=headers, |
| 1611 | body=body, |
| 1612 | )) |
| 1613 | |
| 1614 | return True |
| 1615 | |
| 1616 | def quit(self): |
| 1617 | return True |
| 1618 | |
| 1619 | |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1620 | class FakeNodepool(object): |
| 1621 | REQUEST_ROOT = '/nodepool/requests' |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 1622 | NODE_ROOT = '/nodepool/nodes' |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1623 | |
| 1624 | log = logging.getLogger("zuul.test.FakeNodepool") |
| 1625 | |
| 1626 | def __init__(self, host, port, chroot): |
| 1627 | self.client = kazoo.client.KazooClient( |
| 1628 | hosts='%s:%s%s' % (host, port, chroot)) |
| 1629 | self.client.start() |
| 1630 | self._running = True |
James E. Blair | 15be0e1 | 2017-01-03 13:45:20 -0800 | [diff] [blame] | 1631 | self.paused = False |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1632 | self.thread = threading.Thread(target=self.run) |
| 1633 | self.thread.daemon = True |
| 1634 | self.thread.start() |
James E. Blair | 6ab79e0 | 2017-01-06 10:10:17 -0800 | [diff] [blame] | 1635 | self.fail_requests = set() |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1636 | |
| 1637 | def stop(self): |
| 1638 | self._running = False |
| 1639 | self.thread.join() |
| 1640 | self.client.stop() |
| 1641 | self.client.close() |
| 1642 | |
| 1643 | def run(self): |
| 1644 | while self._running: |
James E. Blair | cbbce0d | 2017-05-19 07:28:29 -0700 | [diff] [blame] | 1645 | try: |
| 1646 | self._run() |
| 1647 | except Exception: |
| 1648 | self.log.exception("Error in fake nodepool:") |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1649 | time.sleep(0.1) |
| 1650 | |
| 1651 | def _run(self): |
James E. Blair | 15be0e1 | 2017-01-03 13:45:20 -0800 | [diff] [blame] | 1652 | if self.paused: |
| 1653 | return |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1654 | for req in self.getNodeRequests(): |
| 1655 | self.fulfillRequest(req) |
| 1656 | |
| 1657 | def getNodeRequests(self): |
| 1658 | try: |
| 1659 | reqids = self.client.get_children(self.REQUEST_ROOT) |
| 1660 | except kazoo.exceptions.NoNodeError: |
| 1661 | return [] |
| 1662 | reqs = [] |
| 1663 | for oid in sorted(reqids): |
| 1664 | path = self.REQUEST_ROOT + '/' + oid |
James E. Blair | 0ef64f8 | 2017-02-02 11:25:16 -0800 | [diff] [blame] | 1665 | try: |
| 1666 | data, stat = self.client.get(path) |
Clint Byrum | f322fe2 | 2017-05-10 20:53:12 -0700 | [diff] [blame] | 1667 | data = json.loads(data.decode('utf8')) |
James E. Blair | 0ef64f8 | 2017-02-02 11:25:16 -0800 | [diff] [blame] | 1668 | data['_oid'] = oid |
| 1669 | reqs.append(data) |
| 1670 | except kazoo.exceptions.NoNodeError: |
| 1671 | pass |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1672 | return reqs |
| 1673 | |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 1674 | def getNodes(self): |
| 1675 | try: |
| 1676 | nodeids = self.client.get_children(self.NODE_ROOT) |
| 1677 | except kazoo.exceptions.NoNodeError: |
| 1678 | return [] |
| 1679 | nodes = [] |
| 1680 | for oid in sorted(nodeids): |
| 1681 | path = self.NODE_ROOT + '/' + oid |
| 1682 | data, stat = self.client.get(path) |
Clint Byrum | f322fe2 | 2017-05-10 20:53:12 -0700 | [diff] [blame] | 1683 | data = json.loads(data.decode('utf8')) |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 1684 | data['_oid'] = oid |
| 1685 | try: |
| 1686 | lockfiles = self.client.get_children(path + '/lock') |
| 1687 | except kazoo.exceptions.NoNodeError: |
| 1688 | lockfiles = [] |
| 1689 | if lockfiles: |
| 1690 | data['_lock'] = True |
| 1691 | else: |
| 1692 | data['_lock'] = False |
| 1693 | nodes.append(data) |
| 1694 | return nodes |
| 1695 | |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 1696 | def makeNode(self, request_id, node_type): |
| 1697 | now = time.time() |
| 1698 | path = '/nodepool/nodes/' |
| 1699 | data = dict(type=node_type, |
Paul Belanger | d28c755 | 2017-08-11 13:10:38 -0400 | [diff] [blame] | 1700 | cloud='test-cloud', |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 1701 | provider='test-provider', |
| 1702 | region='test-region', |
Paul Belanger | 30ba93a | 2017-03-16 16:28:10 -0400 | [diff] [blame] | 1703 | az='test-az', |
Monty Taylor | 56f6133 | 2017-04-11 05:38:12 -0500 | [diff] [blame] | 1704 | interface_ip='127.0.0.1', |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 1705 | public_ipv4='127.0.0.1', |
| 1706 | private_ipv4=None, |
| 1707 | public_ipv6=None, |
| 1708 | allocated_to=request_id, |
| 1709 | state='ready', |
| 1710 | state_time=now, |
| 1711 | created_time=now, |
| 1712 | updated_time=now, |
| 1713 | image_id=None, |
Paul Belanger | c5bf375 | 2017-03-16 19:38:43 -0400 | [diff] [blame] | 1714 | host_keys=["fake-key1", "fake-key2"], |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1715 | executor='fake-nodepool') |
Clint Byrum | f322fe2 | 2017-05-10 20:53:12 -0700 | [diff] [blame] | 1716 | data = json.dumps(data).encode('utf8') |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 1717 | path = self.client.create(path, data, |
| 1718 | makepath=True, |
| 1719 | sequence=True) |
| 1720 | nodeid = path.split("/")[-1] |
| 1721 | return nodeid |
| 1722 | |
James E. Blair | 6ab79e0 | 2017-01-06 10:10:17 -0800 | [diff] [blame] | 1723 | def addFailRequest(self, request): |
| 1724 | self.fail_requests.add(request['_oid']) |
| 1725 | |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1726 | def fulfillRequest(self, request): |
James E. Blair | 6ab79e0 | 2017-01-06 10:10:17 -0800 | [diff] [blame] | 1727 | if request['state'] != 'requested': |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1728 | return |
| 1729 | request = request.copy() |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1730 | oid = request['_oid'] |
| 1731 | del request['_oid'] |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 1732 | |
James E. Blair | 6ab79e0 | 2017-01-06 10:10:17 -0800 | [diff] [blame] | 1733 | if oid in self.fail_requests: |
| 1734 | request['state'] = 'failed' |
| 1735 | else: |
| 1736 | request['state'] = 'fulfilled' |
| 1737 | nodes = [] |
| 1738 | for node in request['node_types']: |
| 1739 | nodeid = self.makeNode(oid, node) |
| 1740 | nodes.append(nodeid) |
| 1741 | request['nodes'] = nodes |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 1742 | |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 1743 | request['state_time'] = time.time() |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1744 | path = self.REQUEST_ROOT + '/' + oid |
Clint Byrum | f322fe2 | 2017-05-10 20:53:12 -0700 | [diff] [blame] | 1745 | data = json.dumps(request).encode('utf8') |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1746 | self.log.debug("Fulfilling node request: %s %s" % (oid, data)) |
James E. Blair | cbbce0d | 2017-05-19 07:28:29 -0700 | [diff] [blame] | 1747 | try: |
| 1748 | self.client.set(path, data) |
| 1749 | except kazoo.exceptions.NoNodeError: |
| 1750 | self.log.debug("Node request %s %s disappeared" % (oid, data)) |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1751 | |
| 1752 | |
James E. Blair | 498059b | 2016-12-20 13:50:13 -0800 | [diff] [blame] | 1753 | class ChrootedKazooFixture(fixtures.Fixture): |
Clark Boylan | 621ec9a | 2017-04-07 17:41:33 -0700 | [diff] [blame] | 1754 | def __init__(self, test_id): |
James E. Blair | 498059b | 2016-12-20 13:50:13 -0800 | [diff] [blame] | 1755 | super(ChrootedKazooFixture, self).__init__() |
| 1756 | |
| 1757 | zk_host = os.environ.get('NODEPOOL_ZK_HOST', 'localhost') |
| 1758 | if ':' in zk_host: |
| 1759 | host, port = zk_host.split(':') |
| 1760 | else: |
| 1761 | host = zk_host |
| 1762 | port = None |
| 1763 | |
| 1764 | self.zookeeper_host = host |
| 1765 | |
| 1766 | if not port: |
| 1767 | self.zookeeper_port = 2181 |
| 1768 | else: |
| 1769 | self.zookeeper_port = int(port) |
| 1770 | |
Clark Boylan | 621ec9a | 2017-04-07 17:41:33 -0700 | [diff] [blame] | 1771 | self.test_id = test_id |
| 1772 | |
James E. Blair | 498059b | 2016-12-20 13:50:13 -0800 | [diff] [blame] | 1773 | def _setUp(self): |
| 1774 | # Make sure the test chroot paths do not conflict |
| 1775 | random_bits = ''.join(random.choice(string.ascii_lowercase + |
| 1776 | string.ascii_uppercase) |
| 1777 | for x in range(8)) |
| 1778 | |
Clark Boylan | 621ec9a | 2017-04-07 17:41:33 -0700 | [diff] [blame] | 1779 | 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] | 1780 | self.zookeeper_chroot = "/nodepool_test/%s" % rand_test_path |
| 1781 | |
Clark Boylan | 7f5f1ec | 2017-04-07 17:39:56 -0700 | [diff] [blame] | 1782 | self.addCleanup(self._cleanup) |
| 1783 | |
James E. Blair | 498059b | 2016-12-20 13:50:13 -0800 | [diff] [blame] | 1784 | # Ensure the chroot path exists and clean up any pre-existing znodes. |
| 1785 | _tmp_client = kazoo.client.KazooClient( |
| 1786 | hosts='%s:%s' % (self.zookeeper_host, self.zookeeper_port)) |
| 1787 | _tmp_client.start() |
| 1788 | |
| 1789 | if _tmp_client.exists(self.zookeeper_chroot): |
| 1790 | _tmp_client.delete(self.zookeeper_chroot, recursive=True) |
| 1791 | |
| 1792 | _tmp_client.ensure_path(self.zookeeper_chroot) |
| 1793 | _tmp_client.stop() |
| 1794 | _tmp_client.close() |
| 1795 | |
James E. Blair | 498059b | 2016-12-20 13:50:13 -0800 | [diff] [blame] | 1796 | def _cleanup(self): |
| 1797 | '''Remove the chroot path.''' |
| 1798 | # Need a non-chroot'ed client to remove the chroot path |
| 1799 | _tmp_client = kazoo.client.KazooClient( |
| 1800 | hosts='%s:%s' % (self.zookeeper_host, self.zookeeper_port)) |
| 1801 | _tmp_client.start() |
| 1802 | _tmp_client.delete(self.zookeeper_chroot, recursive=True) |
| 1803 | _tmp_client.stop() |
Clark Boylan | 7f5f1ec | 2017-04-07 17:39:56 -0700 | [diff] [blame] | 1804 | _tmp_client.close() |
James E. Blair | 498059b | 2016-12-20 13:50:13 -0800 | [diff] [blame] | 1805 | |
| 1806 | |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 1807 | class MySQLSchemaFixture(fixtures.Fixture): |
| 1808 | def setUp(self): |
| 1809 | super(MySQLSchemaFixture, self).setUp() |
| 1810 | |
| 1811 | random_bits = ''.join(random.choice(string.ascii_lowercase + |
| 1812 | string.ascii_uppercase) |
| 1813 | for x in range(8)) |
| 1814 | self.name = '%s_%s' % (random_bits, os.getpid()) |
| 1815 | self.passwd = uuid.uuid4().hex |
| 1816 | db = pymysql.connect(host="localhost", |
| 1817 | user="openstack_citest", |
| 1818 | passwd="openstack_citest", |
| 1819 | db="openstack_citest") |
| 1820 | cur = db.cursor() |
| 1821 | cur.execute("create database %s" % self.name) |
| 1822 | cur.execute( |
| 1823 | "grant all on %s.* to '%s'@'localhost' identified by '%s'" % |
| 1824 | (self.name, self.name, self.passwd)) |
| 1825 | cur.execute("flush privileges") |
| 1826 | |
| 1827 | self.dburi = 'mysql+pymysql://%s:%s@localhost/%s' % (self.name, |
| 1828 | self.passwd, |
| 1829 | self.name) |
| 1830 | self.addDetail('dburi', testtools.content.text_content(self.dburi)) |
| 1831 | self.addCleanup(self.cleanup) |
| 1832 | |
| 1833 | def cleanup(self): |
| 1834 | db = pymysql.connect(host="localhost", |
| 1835 | user="openstack_citest", |
| 1836 | passwd="openstack_citest", |
| 1837 | db="openstack_citest") |
| 1838 | cur = db.cursor() |
| 1839 | cur.execute("drop database %s" % self.name) |
| 1840 | cur.execute("drop user '%s'@'localhost'" % self.name) |
| 1841 | cur.execute("flush privileges") |
| 1842 | |
| 1843 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 1844 | class BaseTestCase(testtools.TestCase): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1845 | log = logging.getLogger("zuul.test") |
James E. Blair | 267e516 | 2017-04-07 10:08:20 -0700 | [diff] [blame] | 1846 | wait_timeout = 30 |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1847 | |
James E. Blair | 1c236df | 2017-02-01 14:07:24 -0800 | [diff] [blame] | 1848 | def attachLogs(self, *args): |
| 1849 | def reader(): |
| 1850 | self._log_stream.seek(0) |
| 1851 | while True: |
| 1852 | x = self._log_stream.read(4096) |
| 1853 | if not x: |
| 1854 | break |
| 1855 | yield x.encode('utf8') |
| 1856 | content = testtools.content.content_from_reader( |
| 1857 | reader, |
| 1858 | testtools.content_type.UTF8_TEXT, |
| 1859 | False) |
| 1860 | self.addDetail('logging', content) |
| 1861 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1862 | def setUp(self): |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 1863 | super(BaseTestCase, self).setUp() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 1864 | test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0) |
| 1865 | try: |
| 1866 | test_timeout = int(test_timeout) |
| 1867 | except ValueError: |
| 1868 | # If timeout value is invalid do not set a timeout. |
| 1869 | test_timeout = 0 |
| 1870 | if test_timeout > 0: |
| 1871 | self.useFixture(fixtures.Timeout(test_timeout, gentle=False)) |
| 1872 | |
| 1873 | if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or |
| 1874 | os.environ.get('OS_STDOUT_CAPTURE') == '1'): |
| 1875 | stdout = self.useFixture(fixtures.StringStream('stdout')).stream |
| 1876 | self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) |
| 1877 | if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or |
| 1878 | os.environ.get('OS_STDERR_CAPTURE') == '1'): |
| 1879 | stderr = self.useFixture(fixtures.StringStream('stderr')).stream |
| 1880 | self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) |
| 1881 | if (os.environ.get('OS_LOG_CAPTURE') == 'True' or |
| 1882 | os.environ.get('OS_LOG_CAPTURE') == '1'): |
James E. Blair | 1c236df | 2017-02-01 14:07:24 -0800 | [diff] [blame] | 1883 | self._log_stream = StringIO() |
| 1884 | self.addOnException(self.attachLogs) |
| 1885 | else: |
| 1886 | self._log_stream = sys.stdout |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 1887 | |
James E. Blair | 73b4177 | 2017-05-22 13:22:55 -0700 | [diff] [blame] | 1888 | # NOTE(jeblair): this is temporary extra debugging to try to |
| 1889 | # track down a possible leak. |
| 1890 | orig_git_repo_init = git.Repo.__init__ |
| 1891 | |
| 1892 | def git_repo_init(myself, *args, **kw): |
| 1893 | orig_git_repo_init(myself, *args, **kw) |
| 1894 | self.log.debug("Created git repo 0x%x %s" % |
| 1895 | (id(myself), repr(myself))) |
| 1896 | |
| 1897 | self.useFixture(fixtures.MonkeyPatch('git.Repo.__init__', |
| 1898 | git_repo_init)) |
| 1899 | |
James E. Blair | 1c236df | 2017-02-01 14:07:24 -0800 | [diff] [blame] | 1900 | handler = logging.StreamHandler(self._log_stream) |
| 1901 | formatter = logging.Formatter('%(asctime)s %(name)-32s ' |
| 1902 | '%(levelname)-8s %(message)s') |
| 1903 | handler.setFormatter(formatter) |
| 1904 | |
| 1905 | logger = logging.getLogger() |
| 1906 | logger.setLevel(logging.DEBUG) |
| 1907 | logger.addHandler(handler) |
| 1908 | |
Clark Boylan | 3410d53 | 2017-04-25 12:35:29 -0700 | [diff] [blame] | 1909 | # Make sure we don't carry old handlers around in process state |
| 1910 | # which slows down test runs |
| 1911 | self.addCleanup(logger.removeHandler, handler) |
| 1912 | self.addCleanup(handler.close) |
| 1913 | self.addCleanup(handler.flush) |
| 1914 | |
James E. Blair | 1c236df | 2017-02-01 14:07:24 -0800 | [diff] [blame] | 1915 | # NOTE(notmorgan): Extract logging overrides for specific |
| 1916 | # libraries from the OS_LOG_DEFAULTS env and create loggers |
| 1917 | # for each. This is used to limit the output during test runs |
| 1918 | # from libraries that zuul depends on such as gear. |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1919 | log_defaults_from_env = os.environ.get( |
| 1920 | 'OS_LOG_DEFAULTS', |
Monty Taylor | 73db649 | 2017-05-18 17:31:17 -0500 | [diff] [blame] | 1921 | 'git.cmd=INFO,kazoo.client=WARNING,gear=INFO,paste=INFO') |
Morgan Fainberg | d34e0b4 | 2016-06-09 19:10:38 -0700 | [diff] [blame] | 1922 | |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1923 | if log_defaults_from_env: |
| 1924 | for default in log_defaults_from_env.split(','): |
| 1925 | try: |
| 1926 | name, level_str = default.split('=', 1) |
| 1927 | level = getattr(logging, level_str, logging.DEBUG) |
James E. Blair | 1c236df | 2017-02-01 14:07:24 -0800 | [diff] [blame] | 1928 | logger = logging.getLogger(name) |
| 1929 | logger.setLevel(level) |
| 1930 | logger.addHandler(handler) |
| 1931 | logger.propagate = False |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 1932 | except ValueError: |
| 1933 | # NOTE(notmorgan): Invalid format of the log default, |
| 1934 | # skip and don't try and apply a logger for the |
| 1935 | # specified module |
| 1936 | pass |
Morgan Fainberg | d34e0b4 | 2016-06-09 19:10:38 -0700 | [diff] [blame] | 1937 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 1938 | |
| 1939 | class ZuulTestCase(BaseTestCase): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 1940 | """A test case with a functioning Zuul. |
| 1941 | |
| 1942 | The following class variables are used during test setup and can |
| 1943 | be overidden by subclasses but are effectively read-only once a |
| 1944 | test method starts running: |
| 1945 | |
| 1946 | :cvar str config_file: This points to the main zuul config file |
| 1947 | within the fixtures directory. Subclasses may override this |
| 1948 | to obtain a different behavior. |
| 1949 | |
| 1950 | :cvar str tenant_config_file: This is the tenant config file |
| 1951 | (which specifies from what git repos the configuration should |
| 1952 | be loaded). It defaults to the value specified in |
| 1953 | `config_file` but can be overidden by subclasses to obtain a |
| 1954 | different tenant/project layout while using the standard main |
James E. Blair | 06cc392 | 2017-04-19 10:08:10 -0700 | [diff] [blame] | 1955 | configuration. See also the :py:func:`simple_layout` |
| 1956 | decorator. |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 1957 | |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 1958 | :cvar bool create_project_keys: Indicates whether Zuul should |
| 1959 | auto-generate keys for each project, or whether the test |
| 1960 | infrastructure should insert dummy keys to save time during |
| 1961 | startup. Defaults to False. |
| 1962 | |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 1963 | The following are instance variables that are useful within test |
| 1964 | methods: |
| 1965 | |
| 1966 | :ivar FakeGerritConnection fake_<connection>: |
| 1967 | A :py:class:`~tests.base.FakeGerritConnection` will be |
| 1968 | instantiated for each connection present in the config file |
| 1969 | and stored here. For instance, `fake_gerrit` will hold the |
| 1970 | FakeGerritConnection object for a connection named `gerrit`. |
| 1971 | |
| 1972 | :ivar FakeGearmanServer gearman_server: An instance of |
| 1973 | :py:class:`~tests.base.FakeGearmanServer` which is the Gearman |
| 1974 | server that all of the Zuul components in this test use to |
| 1975 | communicate with each other. |
| 1976 | |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1977 | :ivar RecordingExecutorServer executor_server: An instance of |
| 1978 | :py:class:`~tests.base.RecordingExecutorServer` which is the |
| 1979 | Ansible execute server used to run jobs for this test. |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 1980 | |
| 1981 | :ivar list builds: A list of :py:class:`~tests.base.FakeBuild` objects |
| 1982 | representing currently running builds. They are appended to |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1983 | 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] | 1984 | list upon completion. |
| 1985 | |
| 1986 | :ivar list history: A list of :py:class:`~tests.base.BuildHistory` |
| 1987 | objects representing completed builds. They are appended to |
| 1988 | the list in the order they complete. |
| 1989 | |
| 1990 | """ |
| 1991 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 1992 | config_file = 'zuul.conf' |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 1993 | run_ansible = False |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 1994 | create_project_keys = False |
Paul Belanger | 0a21f0a | 2017-06-13 13:14:42 -0400 | [diff] [blame] | 1995 | use_ssl = False |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 1996 | |
| 1997 | def _startMerger(self): |
| 1998 | self.merge_server = zuul.merger.server.MergeServer(self.config, |
| 1999 | self.connections) |
| 2000 | self.merge_server.start() |
| 2001 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 2002 | def setUp(self): |
| 2003 | super(ZuulTestCase, self).setUp() |
James E. Blair | 498059b | 2016-12-20 13:50:13 -0800 | [diff] [blame] | 2004 | |
| 2005 | self.setupZK() |
| 2006 | |
K Jonathan Harker | cc3a6f0 | 2017-02-22 19:08:06 -0800 | [diff] [blame] | 2007 | if not KEEP_TEMPDIRS: |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 2008 | tmp_root = self.useFixture(fixtures.TempDir( |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 2009 | rootdir=os.environ.get("ZUUL_TEST_ROOT")) |
| 2010 | ).path |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 2011 | else: |
K Jonathan Harker | cc3a6f0 | 2017-02-22 19:08:06 -0800 | [diff] [blame] | 2012 | tmp_root = tempfile.mkdtemp( |
| 2013 | dir=os.environ.get("ZUUL_TEST_ROOT", None)) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2014 | self.test_root = os.path.join(tmp_root, "zuul-test") |
| 2015 | self.upstream_root = os.path.join(self.test_root, "upstream") |
Monty Taylor | d642d85 | 2017-02-23 14:05:42 -0500 | [diff] [blame] | 2016 | self.merger_src_root = os.path.join(self.test_root, "merger-git") |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2017 | 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] | 2018 | self.state_root = os.path.join(self.test_root, "lib") |
James E. Blair | 01d733e | 2017-06-23 20:47:51 +0100 | [diff] [blame] | 2019 | self.merger_state_root = os.path.join(self.test_root, "merger-lib") |
| 2020 | self.executor_state_root = os.path.join(self.test_root, "executor-lib") |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2021 | |
| 2022 | if os.path.exists(self.test_root): |
| 2023 | shutil.rmtree(self.test_root) |
| 2024 | os.makedirs(self.test_root) |
| 2025 | os.makedirs(self.upstream_root) |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 2026 | os.makedirs(self.state_root) |
James E. Blair | 01d733e | 2017-06-23 20:47:51 +0100 | [diff] [blame] | 2027 | os.makedirs(self.merger_state_root) |
| 2028 | os.makedirs(self.executor_state_root) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2029 | |
| 2030 | # Make per test copy of Configuration. |
| 2031 | self.setup_config() |
Clint Byrum | 50c69d8 | 2017-05-04 11:55:20 -0700 | [diff] [blame] | 2032 | self.private_key_file = os.path.join(self.test_root, 'test_id_rsa') |
| 2033 | if not os.path.exists(self.private_key_file): |
| 2034 | src_private_key_file = os.path.join(FIXTURE_DIR, 'test_id_rsa') |
| 2035 | shutil.copy(src_private_key_file, self.private_key_file) |
| 2036 | shutil.copy('{}.pub'.format(src_private_key_file), |
| 2037 | '{}.pub'.format(self.private_key_file)) |
| 2038 | os.chmod(self.private_key_file, 0o0600) |
James E. Blair | 3984036 | 2017-06-23 20:34:02 +0100 | [diff] [blame] | 2039 | self.config.set('scheduler', 'tenant_config', |
| 2040 | os.path.join( |
| 2041 | FIXTURE_DIR, |
| 2042 | self.config.get('scheduler', 'tenant_config'))) |
James E. Blair | d1de946 | 2017-06-23 20:53:09 +0100 | [diff] [blame] | 2043 | self.config.set('scheduler', 'state_dir', self.state_root) |
Monty Taylor | d642d85 | 2017-02-23 14:05:42 -0500 | [diff] [blame] | 2044 | self.config.set('merger', 'git_dir', self.merger_src_root) |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2045 | self.config.set('executor', 'git_dir', self.executor_src_root) |
Clint Byrum | 50c69d8 | 2017-05-04 11:55:20 -0700 | [diff] [blame] | 2046 | self.config.set('executor', 'private_key_file', self.private_key_file) |
James E. Blair | 01d733e | 2017-06-23 20:47:51 +0100 | [diff] [blame] | 2047 | self.config.set('executor', 'state_dir', self.executor_state_root) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2048 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2049 | self.statsd = FakeStatsd() |
James E. Blair | ded241e | 2017-10-10 13:22:40 -0700 | [diff] [blame] | 2050 | if self.config.has_section('statsd'): |
| 2051 | self.config.set('statsd', 'port', str(self.statsd.port)) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2052 | self.statsd.start() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2053 | |
Paul Belanger | 0a21f0a | 2017-06-13 13:14:42 -0400 | [diff] [blame] | 2054 | self.gearman_server = FakeGearmanServer(self.use_ssl) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2055 | |
| 2056 | self.config.set('gearman', 'port', str(self.gearman_server.port)) |
James E. Blair | e47eb77 | 2017-02-02 17:19:40 -0800 | [diff] [blame] | 2057 | self.log.info("Gearman server on port %s" % |
| 2058 | (self.gearman_server.port,)) |
Paul Belanger | 0a21f0a | 2017-06-13 13:14:42 -0400 | [diff] [blame] | 2059 | if self.use_ssl: |
| 2060 | self.log.info('SSL enabled for gearman') |
| 2061 | self.config.set( |
| 2062 | 'gearman', 'ssl_ca', |
| 2063 | os.path.join(FIXTURE_DIR, 'gearman/root-ca.pem')) |
| 2064 | self.config.set( |
| 2065 | 'gearman', 'ssl_cert', |
| 2066 | os.path.join(FIXTURE_DIR, 'gearman/client.pem')) |
| 2067 | self.config.set( |
| 2068 | 'gearman', 'ssl_key', |
| 2069 | os.path.join(FIXTURE_DIR, 'gearman/client.key')) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2070 | |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 2071 | gerritsource.GerritSource.replication_timeout = 1.5 |
| 2072 | gerritsource.GerritSource.replication_retry_interval = 0.5 |
| 2073 | gerritconnection.GerritEventConnector.delay = 0.0 |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2074 | |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 2075 | self.sched = zuul.scheduler.Scheduler(self.config) |
James E. Blair | bdd50e6 | 2017-10-21 08:18:55 -0700 | [diff] [blame] | 2076 | self.sched._stats_interval = 1 |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2077 | |
Jan Hruban | 7083edd | 2015-08-21 14:00:54 +0200 | [diff] [blame] | 2078 | self.webapp = zuul.webapp.WebApp( |
| 2079 | self.sched, port=0, listen_address='127.0.0.1') |
| 2080 | |
Jan Hruban | 6b71aff | 2015-10-22 16:58:08 +0200 | [diff] [blame] | 2081 | self.event_queues = [ |
| 2082 | self.sched.result_event_queue, |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 2083 | self.sched.trigger_event_queue, |
| 2084 | self.sched.management_event_queue |
Jan Hruban | 6b71aff | 2015-10-22 16:58:08 +0200 | [diff] [blame] | 2085 | ] |
| 2086 | |
James E. Blair | fef7894 | 2016-03-11 16:28:56 -0800 | [diff] [blame] | 2087 | self.configure_connections() |
Jan Hruban | 7083edd | 2015-08-21 14:00:54 +0200 | [diff] [blame] | 2088 | self.sched.registerConnections(self.connections, self.webapp) |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 2089 | |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2090 | self.executor_server = RecordingExecutorServer( |
James E. Blair | a92cbc8 | 2017-01-23 14:56:49 -0800 | [diff] [blame] | 2091 | self.config, self.connections, |
James E. Blair | 854f889 | 2017-02-02 11:25:39 -0800 | [diff] [blame] | 2092 | jobdir_root=self.test_root, |
James E. Blair | a92cbc8 | 2017-01-23 14:56:49 -0800 | [diff] [blame] | 2093 | _run_ansible=self.run_ansible, |
K Jonathan Harker | cc3a6f0 | 2017-02-22 19:08:06 -0800 | [diff] [blame] | 2094 | _test_root=self.test_root, |
| 2095 | keep_jobdir=KEEP_TEMPDIRS) |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2096 | self.executor_server.start() |
| 2097 | self.history = self.executor_server.build_history |
| 2098 | self.builds = self.executor_server.running_builds |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 2099 | |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2100 | self.executor_client = zuul.executor.client.ExecutorClient( |
James E. Blair | 92e953a | 2017-03-07 13:08:47 -0800 | [diff] [blame] | 2101 | self.config, self.sched) |
Joshua Hesketh | 850ccb6 | 2014-11-27 11:31:02 +1100 | [diff] [blame] | 2102 | self.merge_client = zuul.merger.client.MergeClient( |
| 2103 | self.config, self.sched) |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 2104 | self.nodepool = zuul.nodepool.Nodepool(self.sched) |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 2105 | self.zk = zuul.zk.ZooKeeper() |
James E. Blair | 0d5a36e | 2017-02-21 10:53:44 -0500 | [diff] [blame] | 2106 | self.zk.connect(self.zk_config) |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 2107 | |
James E. Blair | 0d5a36e | 2017-02-21 10:53:44 -0500 | [diff] [blame] | 2108 | self.fake_nodepool = FakeNodepool( |
| 2109 | self.zk_chroot_fixture.zookeeper_host, |
| 2110 | self.zk_chroot_fixture.zookeeper_port, |
| 2111 | self.zk_chroot_fixture.zookeeper_chroot) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2112 | |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2113 | self.sched.setExecutor(self.executor_client) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2114 | self.sched.setMerger(self.merge_client) |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 2115 | self.sched.setNodepool(self.nodepool) |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 2116 | self.sched.setZooKeeper(self.zk) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2117 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2118 | self.sched.start() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2119 | self.webapp.start() |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2120 | self.executor_client.gearman.waitForServer() |
James E. Blair | a002b03 | 2017-04-18 10:35:48 -0700 | [diff] [blame] | 2121 | # Cleanups are run in reverse order |
| 2122 | self.addCleanup(self.assertCleanShutdown) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2123 | self.addCleanup(self.shutdown) |
James E. Blair | a002b03 | 2017-04-18 10:35:48 -0700 | [diff] [blame] | 2124 | self.addCleanup(self.assertFinalState) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2125 | |
James E. Blair | b9c0d77 | 2017-03-03 14:34:49 -0800 | [diff] [blame] | 2126 | self.sched.reconfigure(self.config) |
| 2127 | self.sched.resume() |
| 2128 | |
Tobias Henkel | 7df274b | 2017-05-26 17:41:11 +0200 | [diff] [blame] | 2129 | def configure_connections(self, source_only=False): |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 2130 | # Set up gerrit related fakes |
| 2131 | # Set a changes database so multiple FakeGerrit's can report back to |
| 2132 | # a virtual canonical database given by the configured hostname |
| 2133 | self.gerrit_changes_dbs = {} |
| 2134 | |
| 2135 | def getGerritConnection(driver, name, config): |
| 2136 | db = self.gerrit_changes_dbs.setdefault(config['server'], {}) |
| 2137 | con = FakeGerritConnection(driver, name, config, |
| 2138 | changes_db=db, |
| 2139 | upstream_root=self.upstream_root) |
| 2140 | self.event_queues.append(con.event_queue) |
| 2141 | setattr(self, 'fake_' + name, con) |
| 2142 | return con |
| 2143 | |
| 2144 | self.useFixture(fixtures.MonkeyPatch( |
| 2145 | 'zuul.driver.gerrit.GerritDriver.getConnection', |
| 2146 | getGerritConnection)) |
| 2147 | |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 2148 | def getGithubConnection(driver, name, config): |
| 2149 | con = FakeGithubConnection(driver, name, config, |
| 2150 | upstream_root=self.upstream_root) |
Jesse Keating | 64d2901 | 2017-09-06 12:27:49 -0700 | [diff] [blame] | 2151 | self.event_queues.append(con.event_queue) |
Gregory Haynes | 4fc1254 | 2015-04-22 20:38:06 -0700 | [diff] [blame] | 2152 | setattr(self, 'fake_' + name, con) |
| 2153 | return con |
| 2154 | |
| 2155 | self.useFixture(fixtures.MonkeyPatch( |
| 2156 | 'zuul.driver.github.GithubDriver.getConnection', |
| 2157 | getGithubConnection)) |
| 2158 | |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 2159 | # Set up smtp related fakes |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 2160 | # TODO(jhesketh): This should come from lib.connections for better |
| 2161 | # coverage |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 2162 | # Register connections from the config |
| 2163 | self.smtp_messages = [] |
Joshua Hesketh | 850ccb6 | 2014-11-27 11:31:02 +1100 | [diff] [blame] | 2164 | |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 2165 | def FakeSMTPFactory(*args, **kw): |
| 2166 | args = [self.smtp_messages] + list(args) |
| 2167 | return FakeSMTP(*args, **kw) |
Joshua Hesketh | 850ccb6 | 2014-11-27 11:31:02 +1100 | [diff] [blame] | 2168 | |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 2169 | self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTPFactory)) |
Joshua Hesketh | 850ccb6 | 2014-11-27 11:31:02 +1100 | [diff] [blame] | 2170 | |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 2171 | # Register connections from the config using fakes |
James E. Blair | fef7894 | 2016-03-11 16:28:56 -0800 | [diff] [blame] | 2172 | self.connections = zuul.lib.connections.ConnectionRegistry() |
Tobias Henkel | 7df274b | 2017-05-26 17:41:11 +0200 | [diff] [blame] | 2173 | self.connections.configure(self.config, source_only=source_only) |
Joshua Hesketh | 850ccb6 | 2014-11-27 11:31:02 +1100 | [diff] [blame] | 2174 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 2175 | def setup_config(self): |
James E. Blair | e7b99a0 | 2016-08-05 14:27:34 -0700 | [diff] [blame] | 2176 | # This creates the per-test configuration object. It can be |
| 2177 | # overriden by subclasses, but should not need to be since it |
| 2178 | # obeys the config_file and tenant_config_file attributes. |
Monty Taylor | b934c1a | 2017-06-16 19:31:47 -0500 | [diff] [blame] | 2179 | self.config = configparser.ConfigParser() |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 2180 | self.config.read(os.path.join(FIXTURE_DIR, self.config_file)) |
James E. Blair | 06cc392 | 2017-04-19 10:08:10 -0700 | [diff] [blame] | 2181 | |
James E. Blair | 3984036 | 2017-06-23 20:34:02 +0100 | [diff] [blame] | 2182 | sections = ['zuul', 'scheduler', 'executor', 'merger'] |
| 2183 | for section in sections: |
| 2184 | if not self.config.has_section(section): |
| 2185 | self.config.add_section(section) |
| 2186 | |
James E. Blair | 06cc392 | 2017-04-19 10:08:10 -0700 | [diff] [blame] | 2187 | if not self.setupSimpleLayout(): |
| 2188 | if hasattr(self, 'tenant_config_file'): |
James E. Blair | 3984036 | 2017-06-23 20:34:02 +0100 | [diff] [blame] | 2189 | self.config.set('scheduler', 'tenant_config', |
James E. Blair | 06cc392 | 2017-04-19 10:08:10 -0700 | [diff] [blame] | 2190 | self.tenant_config_file) |
| 2191 | git_path = os.path.join( |
| 2192 | os.path.dirname( |
| 2193 | os.path.join(FIXTURE_DIR, self.tenant_config_file)), |
| 2194 | 'git') |
| 2195 | if os.path.exists(git_path): |
| 2196 | for reponame in os.listdir(git_path): |
| 2197 | project = reponame.replace('_', '/') |
| 2198 | self.copyDirToRepo(project, |
| 2199 | os.path.join(git_path, reponame)) |
Tristan Cacqueray | 44aef15 | 2017-06-15 06:00:12 +0000 | [diff] [blame] | 2200 | # Make test_root persist after ansible run for .flag test |
Monty Taylor | 01380dd | 2017-07-28 16:01:20 -0500 | [diff] [blame] | 2201 | self.config.set('executor', 'trusted_rw_paths', self.test_root) |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 2202 | self.setupAllProjectKeys() |
| 2203 | |
James E. Blair | 06cc392 | 2017-04-19 10:08:10 -0700 | [diff] [blame] | 2204 | def setupSimpleLayout(self): |
| 2205 | # If the test method has been decorated with a simple_layout, |
| 2206 | # use that instead of the class tenant_config_file. Set up a |
| 2207 | # single config-project with the specified layout, and |
| 2208 | # initialize repos for all of the 'project' entries which |
| 2209 | # appear in the layout. |
| 2210 | test_name = self.id().split('.')[-1] |
| 2211 | test = getattr(self, test_name) |
| 2212 | if hasattr(test, '__simple_layout__'): |
Jesse Keating | 436a545 | 2017-04-20 11:48:41 -0700 | [diff] [blame] | 2213 | path, driver = getattr(test, '__simple_layout__') |
James E. Blair | 06cc392 | 2017-04-19 10:08:10 -0700 | [diff] [blame] | 2214 | else: |
| 2215 | return False |
| 2216 | |
James E. Blair | b70e55a | 2017-04-19 12:57:02 -0700 | [diff] [blame] | 2217 | files = {} |
James E. Blair | 06cc392 | 2017-04-19 10:08:10 -0700 | [diff] [blame] | 2218 | path = os.path.join(FIXTURE_DIR, path) |
| 2219 | with open(path) as f: |
James E. Blair | b70e55a | 2017-04-19 12:57:02 -0700 | [diff] [blame] | 2220 | data = f.read() |
| 2221 | layout = yaml.safe_load(data) |
| 2222 | files['zuul.yaml'] = data |
James E. Blair | 06cc392 | 2017-04-19 10:08:10 -0700 | [diff] [blame] | 2223 | untrusted_projects = [] |
| 2224 | for item in layout: |
| 2225 | if 'project' in item: |
| 2226 | name = item['project']['name'] |
| 2227 | untrusted_projects.append(name) |
| 2228 | self.init_repo(name) |
| 2229 | self.addCommitToRepo(name, 'initial commit', |
| 2230 | files={'README': ''}, |
| 2231 | branch='master', tag='init') |
James E. Blair | b70e55a | 2017-04-19 12:57:02 -0700 | [diff] [blame] | 2232 | if 'job' in item: |
James E. Blair | b09a0c5 | 2017-10-04 07:35:14 -0700 | [diff] [blame] | 2233 | if 'run' in item['job']: |
| 2234 | files['%s.yaml' % item['job']['run']] = '' |
| 2235 | for fn in zuul.configloader.as_list( |
| 2236 | item['job'].get('pre-run', [])): |
| 2237 | files['%s.yaml' % fn] = '' |
| 2238 | for fn in zuul.configloader.as_list( |
| 2239 | item['job'].get('post-run', [])): |
| 2240 | files['%s.yaml' % fn] = '' |
James E. Blair | 06cc392 | 2017-04-19 10:08:10 -0700 | [diff] [blame] | 2241 | |
| 2242 | root = os.path.join(self.test_root, "config") |
| 2243 | if not os.path.exists(root): |
| 2244 | os.makedirs(root) |
| 2245 | f = tempfile.NamedTemporaryFile(dir=root, delete=False) |
| 2246 | config = [{'tenant': |
| 2247 | {'name': 'tenant-one', |
Jesse Keating | 436a545 | 2017-04-20 11:48:41 -0700 | [diff] [blame] | 2248 | 'source': {driver: |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 2249 | {'config-projects': ['org/common-config'], |
James E. Blair | 06cc392 | 2017-04-19 10:08:10 -0700 | [diff] [blame] | 2250 | 'untrusted-projects': untrusted_projects}}}}] |
Clint Byrum | d52b7d7 | 2017-05-10 19:40:35 -0700 | [diff] [blame] | 2251 | f.write(yaml.dump(config).encode('utf8')) |
James E. Blair | 06cc392 | 2017-04-19 10:08:10 -0700 | [diff] [blame] | 2252 | f.close() |
James E. Blair | 3984036 | 2017-06-23 20:34:02 +0100 | [diff] [blame] | 2253 | self.config.set('scheduler', 'tenant_config', |
James E. Blair | 06cc392 | 2017-04-19 10:08:10 -0700 | [diff] [blame] | 2254 | os.path.join(FIXTURE_DIR, f.name)) |
| 2255 | |
Tobias Henkel | 3c17d5f | 2017-08-03 11:46:54 +0200 | [diff] [blame] | 2256 | self.init_repo('org/common-config') |
| 2257 | self.addCommitToRepo('org/common-config', 'add content from fixture', |
James E. Blair | 06cc392 | 2017-04-19 10:08:10 -0700 | [diff] [blame] | 2258 | files, branch='master', tag='init') |
| 2259 | |
| 2260 | return True |
| 2261 | |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 2262 | def setupAllProjectKeys(self): |
| 2263 | if self.create_project_keys: |
| 2264 | return |
| 2265 | |
James E. Blair | 3984036 | 2017-06-23 20:34:02 +0100 | [diff] [blame] | 2266 | path = self.config.get('scheduler', 'tenant_config') |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 2267 | with open(os.path.join(FIXTURE_DIR, path)) as f: |
| 2268 | tenant_config = yaml.safe_load(f.read()) |
| 2269 | for tenant in tenant_config: |
| 2270 | sources = tenant['tenant']['source'] |
| 2271 | for source, conf in sources.items(): |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 2272 | for project in conf.get('config-projects', []): |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 2273 | self.setupProjectKeys(source, project) |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 2274 | for project in conf.get('untrusted-projects', []): |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 2275 | self.setupProjectKeys(source, project) |
| 2276 | |
| 2277 | def setupProjectKeys(self, source, project): |
| 2278 | # Make sure we set up an RSA key for the project so that we |
| 2279 | # don't spend time generating one: |
| 2280 | |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 2281 | if isinstance(project, dict): |
| 2282 | project = list(project.keys())[0] |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 2283 | key_root = os.path.join(self.state_root, 'keys') |
| 2284 | if not os.path.isdir(key_root): |
| 2285 | os.mkdir(key_root, 0o700) |
| 2286 | private_key_file = os.path.join(key_root, source, project + '.pem') |
| 2287 | private_key_dir = os.path.dirname(private_key_file) |
| 2288 | self.log.debug("Installing test keys for project %s at %s" % ( |
| 2289 | project, private_key_file)) |
| 2290 | if not os.path.isdir(private_key_dir): |
| 2291 | os.makedirs(private_key_dir) |
| 2292 | with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i: |
| 2293 | with open(private_key_file, 'w') as o: |
| 2294 | o.write(i.read()) |
James E. Blair | 96c6bf8 | 2016-01-15 16:20:40 -0800 | [diff] [blame] | 2295 | |
James E. Blair | 498059b | 2016-12-20 13:50:13 -0800 | [diff] [blame] | 2296 | def setupZK(self): |
Clark Boylan | 621ec9a | 2017-04-07 17:41:33 -0700 | [diff] [blame] | 2297 | self.zk_chroot_fixture = self.useFixture( |
| 2298 | ChrootedKazooFixture(self.id())) |
James E. Blair | 0d5a36e | 2017-02-21 10:53:44 -0500 | [diff] [blame] | 2299 | self.zk_config = '%s:%s%s' % ( |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 2300 | self.zk_chroot_fixture.zookeeper_host, |
| 2301 | self.zk_chroot_fixture.zookeeper_port, |
| 2302 | self.zk_chroot_fixture.zookeeper_chroot) |
James E. Blair | 498059b | 2016-12-20 13:50:13 -0800 | [diff] [blame] | 2303 | |
James E. Blair | 96c6bf8 | 2016-01-15 16:20:40 -0800 | [diff] [blame] | 2304 | def copyDirToRepo(self, project, source_path): |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 2305 | self.init_repo(project) |
James E. Blair | 96c6bf8 | 2016-01-15 16:20:40 -0800 | [diff] [blame] | 2306 | |
| 2307 | files = {} |
| 2308 | for (dirpath, dirnames, filenames) in os.walk(source_path): |
| 2309 | for filename in filenames: |
| 2310 | test_tree_filepath = os.path.join(dirpath, filename) |
| 2311 | common_path = os.path.commonprefix([test_tree_filepath, |
| 2312 | source_path]) |
| 2313 | relative_filepath = test_tree_filepath[len(common_path) + 1:] |
| 2314 | with open(test_tree_filepath, 'r') as f: |
| 2315 | content = f.read() |
| 2316 | files[relative_filepath] = content |
| 2317 | self.addCommitToRepo(project, 'add content from fixture', |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 2318 | files, branch='master', tag='init') |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 2319 | |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 2320 | def assertNodepoolState(self): |
| 2321 | # Make sure that there are no pending requests |
| 2322 | |
| 2323 | requests = self.fake_nodepool.getNodeRequests() |
| 2324 | self.assertEqual(len(requests), 0) |
| 2325 | |
| 2326 | nodes = self.fake_nodepool.getNodes() |
| 2327 | for node in nodes: |
| 2328 | self.assertFalse(node['_lock'], "Node %s is locked" % |
| 2329 | (node['_oid'],)) |
| 2330 | |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 2331 | def assertNoGeneratedKeys(self): |
| 2332 | # Make sure that Zuul did not generate any project keys |
| 2333 | # (unless it was supposed to). |
| 2334 | |
| 2335 | if self.create_project_keys: |
| 2336 | return |
| 2337 | |
| 2338 | with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i: |
| 2339 | test_key = i.read() |
| 2340 | |
| 2341 | key_root = os.path.join(self.state_root, 'keys') |
| 2342 | for root, dirname, files in os.walk(key_root): |
| 2343 | for fn in files: |
| 2344 | with open(os.path.join(root, fn)) as f: |
| 2345 | self.assertEqual(test_key, f.read()) |
| 2346 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2347 | def assertFinalState(self): |
James E. Blair | a002b03 | 2017-04-18 10:35:48 -0700 | [diff] [blame] | 2348 | self.log.debug("Assert final state") |
| 2349 | # Make sure no jobs are running |
| 2350 | self.assertEqual({}, self.executor_server.job_workers) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2351 | # Make sure that git.Repo objects have been garbage collected. |
James E. Blair | 73b4177 | 2017-05-22 13:22:55 -0700 | [diff] [blame] | 2352 | gc.disable() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2353 | gc.collect() |
| 2354 | for obj in gc.get_objects(): |
| 2355 | if isinstance(obj, git.Repo): |
James E. Blair | 73b4177 | 2017-05-22 13:22:55 -0700 | [diff] [blame] | 2356 | self.log.debug("Leaked git repo object: 0x%x %s" % |
| 2357 | (id(obj), repr(obj))) |
James E. Blair | 73b4177 | 2017-05-22 13:22:55 -0700 | [diff] [blame] | 2358 | gc.enable() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2359 | self.assertEmptyQueues() |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 2360 | self.assertNodepoolState() |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 2361 | self.assertNoGeneratedKeys() |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 2362 | ipm = zuul.manager.independent.IndependentPipelineManager |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 2363 | for tenant in self.sched.abide.tenants.values(): |
| 2364 | for pipeline in tenant.layout.pipelines.values(): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 2365 | if isinstance(pipeline.manager, ipm): |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 2366 | self.assertEqual(len(pipeline.queues), 0) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2367 | |
| 2368 | def shutdown(self): |
| 2369 | self.log.debug("Shutting down after tests") |
James E. Blair | 5426b11 | 2017-05-26 14:19:54 -0700 | [diff] [blame] | 2370 | self.executor_server.hold_jobs_in_build = False |
| 2371 | self.executor_server.release() |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2372 | self.executor_client.stop() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2373 | self.merge_client.stop() |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2374 | self.executor_server.stop() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2375 | self.sched.stop() |
| 2376 | self.sched.join() |
| 2377 | self.statsd.stop() |
| 2378 | self.statsd.join() |
| 2379 | self.webapp.stop() |
| 2380 | self.webapp.join() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2381 | self.gearman_server.shutdown() |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 2382 | self.fake_nodepool.stop() |
| 2383 | self.zk.disconnect() |
Clark Boylan | 8208c19 | 2017-04-24 18:08:08 -0700 | [diff] [blame] | 2384 | self.printHistory() |
Tobias Henkel | 9b546cd | 2017-05-16 09:48:03 +0200 | [diff] [blame] | 2385 | # We whitelist watchdog threads as they have relatively long delays |
Clark Boylan | f18e3b8 | 2017-04-24 17:34:13 -0700 | [diff] [blame] | 2386 | # before noticing they should exit, but they should exit on their own. |
Tobias Henkel | 9b546cd | 2017-05-16 09:48:03 +0200 | [diff] [blame] | 2387 | # Further the pydevd threads also need to be whitelisted so debugging |
| 2388 | # e.g. in PyCharm is possible without breaking shutdown. |
James E. Blair | 7a04df2 | 2017-10-17 08:44:52 -0700 | [diff] [blame] | 2389 | whitelist = ['watchdog', |
Tobias Henkel | 9b546cd | 2017-05-16 09:48:03 +0200 | [diff] [blame] | 2390 | 'pydevd.CommandThread', |
| 2391 | 'pydevd.Reader', |
| 2392 | 'pydevd.Writer', |
| 2393 | ] |
Clark Boylan | f18e3b8 | 2017-04-24 17:34:13 -0700 | [diff] [blame] | 2394 | threads = [t for t in threading.enumerate() |
Tobias Henkel | 9b546cd | 2017-05-16 09:48:03 +0200 | [diff] [blame] | 2395 | if t.name not in whitelist] |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2396 | if len(threads) > 1: |
Clark Boylan | 8208c19 | 2017-04-24 18:08:08 -0700 | [diff] [blame] | 2397 | log_str = "" |
| 2398 | for thread_id, stack_frame in sys._current_frames().items(): |
| 2399 | log_str += "Thread: %s\n" % thread_id |
| 2400 | log_str += "".join(traceback.format_stack(stack_frame)) |
| 2401 | self.log.debug(log_str) |
| 2402 | raise Exception("More than one thread is running: %s" % threads) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2403 | |
James E. Blair | a002b03 | 2017-04-18 10:35:48 -0700 | [diff] [blame] | 2404 | def assertCleanShutdown(self): |
| 2405 | pass |
| 2406 | |
James E. Blair | c4ba97a | 2017-04-19 16:26:24 -0700 | [diff] [blame] | 2407 | def init_repo(self, project, tag=None): |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2408 | parts = project.split('/') |
| 2409 | path = os.path.join(self.upstream_root, *parts[:-1]) |
| 2410 | if not os.path.exists(path): |
| 2411 | os.makedirs(path) |
| 2412 | path = os.path.join(self.upstream_root, project) |
| 2413 | repo = git.Repo.init(path) |
| 2414 | |
Morgan Fainberg | 78c301a | 2016-07-14 13:47:01 -0700 | [diff] [blame] | 2415 | with repo.config_writer() as config_writer: |
| 2416 | config_writer.set_value('user', 'email', 'user@example.com') |
| 2417 | config_writer.set_value('user', 'name', 'User Name') |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2418 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2419 | repo.index.commit('initial commit') |
| 2420 | master = repo.create_head('master') |
James E. Blair | c4ba97a | 2017-04-19 16:26:24 -0700 | [diff] [blame] | 2421 | if tag: |
| 2422 | repo.create_tag(tag) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2423 | |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 2424 | repo.head.reference = master |
James E. Blair | 879dafb | 2015-07-17 14:04:49 -0700 | [diff] [blame] | 2425 | zuul.merger.merger.reset_repo_to_head(repo) |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 2426 | repo.git.clean('-x', '-f', '-d') |
| 2427 | |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 2428 | def create_branch(self, project, branch): |
| 2429 | path = os.path.join(self.upstream_root, project) |
James E. Blair | b815c71 | 2017-09-22 10:10:19 -0700 | [diff] [blame] | 2430 | repo = git.Repo(path) |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 2431 | fn = os.path.join(path, 'README') |
| 2432 | |
| 2433 | branch_head = repo.create_head(branch) |
| 2434 | repo.head.reference = branch_head |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2435 | f = open(fn, 'a') |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 2436 | f.write("test %s\n" % branch) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2437 | f.close() |
| 2438 | repo.index.add([fn]) |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 2439 | repo.index.commit('%s commit' % branch) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2440 | |
James E. Blair | 97d902e | 2014-08-21 13:25:56 -0700 | [diff] [blame] | 2441 | repo.head.reference = repo.heads['master'] |
James E. Blair | 879dafb | 2015-07-17 14:04:49 -0700 | [diff] [blame] | 2442 | zuul.merger.merger.reset_repo_to_head(repo) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2443 | repo.git.clean('-x', '-f', '-d') |
| 2444 | |
Sachi King | 9f16d52 | 2016-03-16 12:20:45 +1100 | [diff] [blame] | 2445 | def create_commit(self, project): |
| 2446 | path = os.path.join(self.upstream_root, project) |
| 2447 | repo = git.Repo(path) |
| 2448 | repo.head.reference = repo.heads['master'] |
| 2449 | file_name = os.path.join(path, 'README') |
| 2450 | with open(file_name, 'a') as f: |
| 2451 | f.write('creating fake commit\n') |
| 2452 | repo.index.add([file_name]) |
| 2453 | commit = repo.index.commit('Creating a fake commit') |
| 2454 | return commit.hexsha |
| 2455 | |
James E. Blair | f4a5f02 | 2017-04-18 14:01:10 -0700 | [diff] [blame] | 2456 | def orderedRelease(self, count=None): |
James E. Blair | b8c1647 | 2015-05-05 14:55:26 -0700 | [diff] [blame] | 2457 | # Run one build at a time to ensure non-race order: |
James E. Blair | f4a5f02 | 2017-04-18 14:01:10 -0700 | [diff] [blame] | 2458 | i = 0 |
James E. Blair | b8c1647 | 2015-05-05 14:55:26 -0700 | [diff] [blame] | 2459 | while len(self.builds): |
| 2460 | self.release(self.builds[0]) |
| 2461 | self.waitUntilSettled() |
James E. Blair | f4a5f02 | 2017-04-18 14:01:10 -0700 | [diff] [blame] | 2462 | i += 1 |
| 2463 | if count is not None and i >= count: |
| 2464 | break |
James E. Blair | b8c1647 | 2015-05-05 14:55:26 -0700 | [diff] [blame] | 2465 | |
James E. Blair | df25ddc | 2017-07-08 07:57:09 -0700 | [diff] [blame] | 2466 | def getSortedBuilds(self): |
| 2467 | "Return the list of currently running builds sorted by name" |
| 2468 | |
| 2469 | return sorted(self.builds, key=lambda x: x.name) |
| 2470 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2471 | def release(self, job): |
| 2472 | if isinstance(job, FakeBuild): |
| 2473 | job.release() |
| 2474 | else: |
| 2475 | job.waiting = False |
| 2476 | self.log.debug("Queued job %s released" % job.unique) |
| 2477 | self.gearman_server.wakeConnections() |
| 2478 | |
| 2479 | def getParameter(self, job, name): |
| 2480 | if isinstance(job, FakeBuild): |
| 2481 | return job.parameters[name] |
| 2482 | else: |
| 2483 | parameters = json.loads(job.arguments) |
| 2484 | return parameters[name] |
| 2485 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2486 | def haveAllBuildsReported(self): |
| 2487 | # See if Zuul is waiting on a meta job to complete |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2488 | if self.executor_client.meta_jobs: |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2489 | return False |
| 2490 | # Find out if every build that the worker has completed has been |
| 2491 | # reported back to Zuul. If it hasn't then that means a Gearman |
| 2492 | # event is still in transit and the system is not stable. |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 2493 | for build in self.history: |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2494 | zbuild = self.executor_client.builds.get(build.uuid) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2495 | if not zbuild: |
| 2496 | # It has already been reported |
| 2497 | continue |
| 2498 | # It hasn't been reported yet. |
| 2499 | return False |
| 2500 | # Make sure that none of the worker connections are in GRAB_WAIT |
James E. Blair | 24c0703 | 2017-06-02 15:26:35 -0700 | [diff] [blame] | 2501 | worker = self.executor_server.executor_worker |
| 2502 | for connection in worker.active_connections: |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2503 | if connection.state == 'GRAB_WAIT': |
| 2504 | return False |
| 2505 | return True |
| 2506 | |
| 2507 | def areAllBuildsWaiting(self): |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2508 | builds = self.executor_client.builds.values() |
James E. Blair | a002b03 | 2017-04-18 10:35:48 -0700 | [diff] [blame] | 2509 | seen_builds = set() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2510 | for build in builds: |
James E. Blair | a002b03 | 2017-04-18 10:35:48 -0700 | [diff] [blame] | 2511 | seen_builds.add(build.uuid) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2512 | client_job = None |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2513 | for conn in self.executor_client.gearman.active_connections: |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2514 | for j in conn.related_jobs.values(): |
| 2515 | if j.unique == build.uuid: |
| 2516 | client_job = j |
| 2517 | break |
| 2518 | if not client_job: |
| 2519 | self.log.debug("%s is not known to the gearman client" % |
| 2520 | build) |
James E. Blair | f15139b | 2015-04-02 16:37:15 -0700 | [diff] [blame] | 2521 | return False |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2522 | if not client_job.handle: |
| 2523 | self.log.debug("%s has no handle" % client_job) |
James E. Blair | f15139b | 2015-04-02 16:37:15 -0700 | [diff] [blame] | 2524 | return False |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2525 | server_job = self.gearman_server.jobs.get(client_job.handle) |
| 2526 | if not server_job: |
| 2527 | self.log.debug("%s is not known to the gearman server" % |
| 2528 | client_job) |
James E. Blair | f15139b | 2015-04-02 16:37:15 -0700 | [diff] [blame] | 2529 | return False |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2530 | if not hasattr(server_job, 'waiting'): |
| 2531 | self.log.debug("%s is being enqueued" % server_job) |
James E. Blair | f15139b | 2015-04-02 16:37:15 -0700 | [diff] [blame] | 2532 | return False |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2533 | if server_job.waiting: |
| 2534 | continue |
James E. Blair | 1730297 | 2016-08-10 16:11:42 -0700 | [diff] [blame] | 2535 | if build.url is None: |
James E. Blair | bbda470 | 2016-03-09 15:19:56 -0800 | [diff] [blame] | 2536 | self.log.debug("%s has not reported start" % build) |
| 2537 | return False |
Clint Byrum | f322fe2 | 2017-05-10 20:53:12 -0700 | [diff] [blame] | 2538 | # using internal ServerJob which offers no Text interface |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2539 | worker_build = self.executor_server.job_builds.get( |
Clint Byrum | f322fe2 | 2017-05-10 20:53:12 -0700 | [diff] [blame] | 2540 | server_job.unique.decode('utf8')) |
James E. Blair | 962220f | 2016-08-03 11:22:38 -0700 | [diff] [blame] | 2541 | if worker_build: |
| 2542 | if worker_build.isWaiting(): |
| 2543 | continue |
| 2544 | else: |
| 2545 | self.log.debug("%s is running" % worker_build) |
| 2546 | return False |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2547 | else: |
James E. Blair | 962220f | 2016-08-03 11:22:38 -0700 | [diff] [blame] | 2548 | self.log.debug("%s is unassigned" % server_job) |
James E. Blair | f15139b | 2015-04-02 16:37:15 -0700 | [diff] [blame] | 2549 | return False |
James E. Blair | a002b03 | 2017-04-18 10:35:48 -0700 | [diff] [blame] | 2550 | for (build_uuid, job_worker) in \ |
| 2551 | self.executor_server.job_workers.items(): |
| 2552 | if build_uuid not in seen_builds: |
| 2553 | self.log.debug("%s is not finalized" % build_uuid) |
| 2554 | return False |
James E. Blair | f15139b | 2015-04-02 16:37:15 -0700 | [diff] [blame] | 2555 | return True |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2556 | |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 2557 | def areAllNodeRequestsComplete(self): |
James E. Blair | 15be0e1 | 2017-01-03 13:45:20 -0800 | [diff] [blame] | 2558 | if self.fake_nodepool.paused: |
| 2559 | return True |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 2560 | if self.sched.nodepool.requests: |
| 2561 | return False |
| 2562 | return True |
| 2563 | |
James E. Blair | a615c36 | 2017-10-02 17:34:42 -0700 | [diff] [blame] | 2564 | def areAllMergeJobsWaiting(self): |
| 2565 | for client_job in list(self.merge_client.jobs): |
| 2566 | if not client_job.handle: |
| 2567 | self.log.debug("%s has no handle" % client_job) |
| 2568 | return False |
| 2569 | server_job = self.gearman_server.jobs.get(client_job.handle) |
| 2570 | if not server_job: |
| 2571 | self.log.debug("%s is not known to the gearman server" % |
| 2572 | client_job) |
| 2573 | return False |
| 2574 | if not hasattr(server_job, 'waiting'): |
| 2575 | self.log.debug("%s is being enqueued" % server_job) |
| 2576 | return False |
| 2577 | if server_job.waiting: |
| 2578 | self.log.debug("%s is waiting" % server_job) |
| 2579 | continue |
| 2580 | self.log.debug("%s is not waiting" % server_job) |
| 2581 | return False |
| 2582 | return True |
| 2583 | |
Jan Hruban | 6b71aff | 2015-10-22 16:58:08 +0200 | [diff] [blame] | 2584 | def eventQueuesEmpty(self): |
Monty Taylor | b934c1a | 2017-06-16 19:31:47 -0500 | [diff] [blame] | 2585 | for event_queue in self.event_queues: |
| 2586 | yield event_queue.empty() |
Jan Hruban | 6b71aff | 2015-10-22 16:58:08 +0200 | [diff] [blame] | 2587 | |
| 2588 | def eventQueuesJoin(self): |
Monty Taylor | b934c1a | 2017-06-16 19:31:47 -0500 | [diff] [blame] | 2589 | for event_queue in self.event_queues: |
| 2590 | event_queue.join() |
Jan Hruban | 6b71aff | 2015-10-22 16:58:08 +0200 | [diff] [blame] | 2591 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2592 | def waitUntilSettled(self): |
| 2593 | self.log.debug("Waiting until settled...") |
| 2594 | start = time.time() |
| 2595 | while True: |
Clint Byrum | a962657 | 2017-02-22 14:04:00 -0500 | [diff] [blame] | 2596 | if time.time() - start > self.wait_timeout: |
James E. Blair | 10fc1eb | 2016-12-21 16:16:25 -0800 | [diff] [blame] | 2597 | self.log.error("Timeout waiting for Zuul to settle") |
| 2598 | self.log.error("Queue status:") |
Monty Taylor | b934c1a | 2017-06-16 19:31:47 -0500 | [diff] [blame] | 2599 | for event_queue in self.event_queues: |
| 2600 | self.log.error(" %s: %s" % |
| 2601 | (event_queue, event_queue.empty())) |
James E. Blair | 10fc1eb | 2016-12-21 16:16:25 -0800 | [diff] [blame] | 2602 | self.log.error("All builds waiting: %s" % |
James E. Blair | 622c968 | 2016-06-09 08:14:53 -0700 | [diff] [blame] | 2603 | (self.areAllBuildsWaiting(),)) |
James E. Blair | 10fc1eb | 2016-12-21 16:16:25 -0800 | [diff] [blame] | 2604 | self.log.error("All builds reported: %s" % |
James E. Blair | f3156c9 | 2016-08-10 15:32:19 -0700 | [diff] [blame] | 2605 | (self.haveAllBuildsReported(),)) |
James E. Blair | 10fc1eb | 2016-12-21 16:16:25 -0800 | [diff] [blame] | 2606 | self.log.error("All requests completed: %s" % |
| 2607 | (self.areAllNodeRequestsComplete(),)) |
| 2608 | self.log.error("Merge client jobs: %s" % |
| 2609 | (self.merge_client.jobs,)) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2610 | raise Exception("Timeout waiting for Zuul to settle") |
| 2611 | # Make sure no new events show up while we're checking |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 2612 | |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2613 | self.executor_server.lock.acquire() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2614 | # have all build states propogated to zuul? |
| 2615 | if self.haveAllBuildsReported(): |
| 2616 | # Join ensures that the queue is empty _and_ events have been |
| 2617 | # processed |
Jan Hruban | 6b71aff | 2015-10-22 16:58:08 +0200 | [diff] [blame] | 2618 | self.eventQueuesJoin() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2619 | self.sched.run_handler_lock.acquire() |
James E. Blair | a615c36 | 2017-10-02 17:34:42 -0700 | [diff] [blame] | 2620 | if (self.areAllMergeJobsWaiting() and |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2621 | self.haveAllBuildsReported() and |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 2622 | self.areAllBuildsWaiting() and |
James E. Blair | 36c611a | 2017-02-06 15:59:43 -0800 | [diff] [blame] | 2623 | self.areAllNodeRequestsComplete() and |
| 2624 | all(self.eventQueuesEmpty())): |
| 2625 | # The queue empty check is placed at the end to |
| 2626 | # ensure that if a component adds an event between |
| 2627 | # when locked the run handler and checked that the |
| 2628 | # components were stable, we don't erroneously |
| 2629 | # report that we are settled. |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2630 | self.sched.run_handler_lock.release() |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2631 | self.executor_server.lock.release() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2632 | self.log.debug("...settled.") |
| 2633 | return |
| 2634 | self.sched.run_handler_lock.release() |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2635 | self.executor_server.lock.release() |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2636 | self.sched.wake_event.wait(0.1) |
| 2637 | |
| 2638 | def countJobResults(self, jobs, result): |
| 2639 | jobs = filter(lambda x: x.result == result, jobs) |
Clint Byrum | 1d0c7d1 | 2017-05-10 19:40:53 -0700 | [diff] [blame] | 2640 | return len(list(jobs)) |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2641 | |
Monty Taylor | 0d92612 | 2017-05-24 08:07:56 -0500 | [diff] [blame] | 2642 | def getBuildByName(self, name): |
| 2643 | for build in self.builds: |
| 2644 | if build.name == name: |
| 2645 | return build |
| 2646 | raise Exception("Unable to find build %s" % name) |
| 2647 | |
David Shrewsbury | f6dc176 | 2017-10-02 13:34:37 -0400 | [diff] [blame] | 2648 | def assertJobNotInHistory(self, name, project=None): |
| 2649 | for job in self.history: |
| 2650 | if (project is None or |
| 2651 | job.parameters['zuul']['project']['name'] == project): |
| 2652 | self.assertNotEqual(job.name, name, |
| 2653 | 'Job %s found in history' % name) |
| 2654 | |
James E. Blair | 96c6bf8 | 2016-01-15 16:20:40 -0800 | [diff] [blame] | 2655 | def getJobFromHistory(self, name, project=None): |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 2656 | for job in self.history: |
| 2657 | if (job.name == name and |
| 2658 | (project is None or |
James E. Blair | e536609 | 2017-07-21 15:30:39 -0700 | [diff] [blame] | 2659 | job.parameters['zuul']['project']['name'] == project)): |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 2660 | return job |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2661 | raise Exception("Unable to find job %s in history" % name) |
| 2662 | |
| 2663 | def assertEmptyQueues(self): |
| 2664 | # Make sure there are no orphaned jobs |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 2665 | for tenant in self.sched.abide.tenants.values(): |
| 2666 | for pipeline in tenant.layout.pipelines.values(): |
Monty Taylor | b934c1a | 2017-06-16 19:31:47 -0500 | [diff] [blame] | 2667 | for pipeline_queue in pipeline.queues: |
| 2668 | if len(pipeline_queue.queue) != 0: |
Joshua Hesketh | 0aa7e8b | 2016-07-14 00:12:25 +1000 | [diff] [blame] | 2669 | print('pipeline %s queue %s contents %s' % ( |
Monty Taylor | b934c1a | 2017-06-16 19:31:47 -0500 | [diff] [blame] | 2670 | pipeline.name, pipeline_queue.name, |
| 2671 | pipeline_queue.queue)) |
| 2672 | self.assertEqual(len(pipeline_queue.queue), 0, |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 2673 | "Pipelines queues should be empty") |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2674 | |
| 2675 | def assertReportedStat(self, key, value=None, kind=None): |
| 2676 | start = time.time() |
| 2677 | while time.time() < (start + 5): |
| 2678 | for stat in self.statsd.stats: |
Clint Byrum | f322fe2 | 2017-05-10 20:53:12 -0700 | [diff] [blame] | 2679 | k, v = stat.decode('utf-8').split(':') |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2680 | if key == k: |
| 2681 | if value is None and kind is None: |
| 2682 | return |
| 2683 | elif value: |
| 2684 | if value == v: |
| 2685 | return |
| 2686 | elif kind: |
| 2687 | if v.endswith('|' + kind): |
| 2688 | return |
| 2689 | time.sleep(0.1) |
| 2690 | |
Clark Boylan | b640e05 | 2014-04-03 16:41:46 -0700 | [diff] [blame] | 2691 | raise Exception("Key %s not found in reported stats" % key) |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 2692 | |
James E. Blair | 2b2a8ab | 2016-08-11 14:39:11 -0700 | [diff] [blame] | 2693 | def assertBuilds(self, builds): |
| 2694 | """Assert that the running builds are as described. |
| 2695 | |
| 2696 | The list of running builds is examined and must match exactly |
| 2697 | the list of builds described by the input. |
| 2698 | |
| 2699 | :arg list builds: A list of dictionaries. Each item in the |
| 2700 | list must match the corresponding build in the build |
| 2701 | history, and each element of the dictionary must match the |
| 2702 | corresponding attribute of the build. |
| 2703 | |
| 2704 | """ |
James E. Blair | 3158e28 | 2016-08-19 09:34:11 -0700 | [diff] [blame] | 2705 | try: |
| 2706 | self.assertEqual(len(self.builds), len(builds)) |
| 2707 | for i, d in enumerate(builds): |
| 2708 | for k, v in d.items(): |
| 2709 | self.assertEqual( |
| 2710 | getattr(self.builds[i], k), v, |
| 2711 | "Element %i in builds does not match" % (i,)) |
| 2712 | except Exception: |
| 2713 | for build in self.builds: |
| 2714 | self.log.error("Running build: %s" % build) |
| 2715 | else: |
| 2716 | self.log.error("No running builds") |
| 2717 | raise |
James E. Blair | 2b2a8ab | 2016-08-11 14:39:11 -0700 | [diff] [blame] | 2718 | |
James E. Blair | b536ecc | 2016-08-31 10:11:42 -0700 | [diff] [blame] | 2719 | def assertHistory(self, history, ordered=True): |
James E. Blair | 2b2a8ab | 2016-08-11 14:39:11 -0700 | [diff] [blame] | 2720 | """Assert that the completed builds are as described. |
| 2721 | |
| 2722 | The list of completed builds is examined and must match |
| 2723 | exactly the list of builds described by the input. |
| 2724 | |
| 2725 | :arg list history: A list of dictionaries. Each item in the |
| 2726 | list must match the corresponding build in the build |
| 2727 | history, and each element of the dictionary must match the |
| 2728 | corresponding attribute of the build. |
| 2729 | |
James E. Blair | b536ecc | 2016-08-31 10:11:42 -0700 | [diff] [blame] | 2730 | :arg bool ordered: If true, the history must match the order |
| 2731 | supplied, if false, the builds are permitted to have |
| 2732 | arrived in any order. |
| 2733 | |
James E. Blair | 2b2a8ab | 2016-08-11 14:39:11 -0700 | [diff] [blame] | 2734 | """ |
James E. Blair | b536ecc | 2016-08-31 10:11:42 -0700 | [diff] [blame] | 2735 | def matches(history_item, item): |
| 2736 | for k, v in item.items(): |
| 2737 | if getattr(history_item, k) != v: |
| 2738 | return False |
| 2739 | return True |
James E. Blair | 3158e28 | 2016-08-19 09:34:11 -0700 | [diff] [blame] | 2740 | try: |
| 2741 | self.assertEqual(len(self.history), len(history)) |
James E. Blair | b536ecc | 2016-08-31 10:11:42 -0700 | [diff] [blame] | 2742 | if ordered: |
| 2743 | for i, d in enumerate(history): |
| 2744 | if not matches(self.history[i], d): |
| 2745 | raise Exception( |
K Jonathan Harker | cc3a6f0 | 2017-02-22 19:08:06 -0800 | [diff] [blame] | 2746 | "Element %i in history does not match %s" % |
| 2747 | (i, self.history[i])) |
James E. Blair | b536ecc | 2016-08-31 10:11:42 -0700 | [diff] [blame] | 2748 | else: |
| 2749 | unseen = self.history[:] |
| 2750 | for i, d in enumerate(history): |
| 2751 | found = False |
| 2752 | for unseen_item in unseen: |
| 2753 | if matches(unseen_item, d): |
| 2754 | found = True |
| 2755 | unseen.remove(unseen_item) |
| 2756 | break |
| 2757 | if not found: |
| 2758 | raise Exception("No match found for element %i " |
| 2759 | "in history" % (i,)) |
| 2760 | if unseen: |
| 2761 | raise Exception("Unexpected items in history") |
James E. Blair | 3158e28 | 2016-08-19 09:34:11 -0700 | [diff] [blame] | 2762 | except Exception: |
| 2763 | for build in self.history: |
| 2764 | self.log.error("Completed build: %s" % build) |
| 2765 | else: |
| 2766 | self.log.error("No completed builds") |
| 2767 | raise |
James E. Blair | 2b2a8ab | 2016-08-11 14:39:11 -0700 | [diff] [blame] | 2768 | |
James E. Blair | 6ac368c | 2016-12-22 18:07:20 -0800 | [diff] [blame] | 2769 | def printHistory(self): |
| 2770 | """Log the build history. |
| 2771 | |
| 2772 | This can be useful during tests to summarize what jobs have |
| 2773 | completed. |
| 2774 | |
| 2775 | """ |
| 2776 | self.log.debug("Build history:") |
| 2777 | for build in self.history: |
| 2778 | self.log.debug(build) |
| 2779 | |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 2780 | def getPipeline(self, name): |
James E. Blair | f84026c | 2015-12-08 16:11:46 -0800 | [diff] [blame] | 2781 | return self.sched.abide.tenants.values()[0].layout.pipelines.get(name) |
| 2782 | |
James E. Blair | 9ea7007 | 2017-04-19 16:05:30 -0700 | [diff] [blame] | 2783 | def updateConfigLayout(self, path): |
James E. Blair | f84026c | 2015-12-08 16:11:46 -0800 | [diff] [blame] | 2784 | root = os.path.join(self.test_root, "config") |
Clint Byrum | ead6c56 | 2017-02-01 16:34:04 -0800 | [diff] [blame] | 2785 | if not os.path.exists(root): |
| 2786 | os.makedirs(root) |
James E. Blair | f84026c | 2015-12-08 16:11:46 -0800 | [diff] [blame] | 2787 | f = tempfile.NamedTemporaryFile(dir=root, delete=False) |
| 2788 | f.write(""" |
Paul Belanger | 66e9596 | 2016-11-11 12:11:06 -0500 | [diff] [blame] | 2789 | - tenant: |
| 2790 | name: openstack |
| 2791 | source: |
| 2792 | gerrit: |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 2793 | config-projects: |
Paul Belanger | 66e9596 | 2016-11-11 12:11:06 -0500 | [diff] [blame] | 2794 | - %s |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 2795 | untrusted-projects: |
James E. Blair | 0ffa010 | 2017-03-30 13:11:33 -0700 | [diff] [blame] | 2796 | - org/project |
| 2797 | - org/project1 |
James E. Blair | 7cb8454 | 2017-04-19 13:35:05 -0700 | [diff] [blame] | 2798 | - org/project2\n""" % path) |
James E. Blair | f84026c | 2015-12-08 16:11:46 -0800 | [diff] [blame] | 2799 | f.close() |
James E. Blair | 3984036 | 2017-06-23 20:34:02 +0100 | [diff] [blame] | 2800 | self.config.set('scheduler', 'tenant_config', |
Paul Belanger | 66e9596 | 2016-11-11 12:11:06 -0500 | [diff] [blame] | 2801 | os.path.join(FIXTURE_DIR, f.name)) |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 2802 | self.setupAllProjectKeys() |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 2803 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 2804 | def addCommitToRepo(self, project, message, files, |
| 2805 | branch='master', tag=None): |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 2806 | path = os.path.join(self.upstream_root, project) |
| 2807 | repo = git.Repo(path) |
| 2808 | repo.head.reference = branch |
| 2809 | zuul.merger.merger.reset_repo_to_head(repo) |
| 2810 | for fn, content in files.items(): |
| 2811 | fn = os.path.join(path, fn) |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 2812 | try: |
| 2813 | os.makedirs(os.path.dirname(fn)) |
| 2814 | except OSError: |
| 2815 | pass |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 2816 | with open(fn, 'w') as f: |
| 2817 | f.write(content) |
| 2818 | repo.index.add([fn]) |
| 2819 | commit = repo.index.commit(message) |
Clint Byrum | 58264dc | 2017-02-07 21:21:22 -0800 | [diff] [blame] | 2820 | before = repo.heads[branch].commit |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 2821 | repo.heads[branch].commit = commit |
| 2822 | repo.head.reference = branch |
| 2823 | repo.git.clean('-x', '-f', '-d') |
| 2824 | repo.heads[branch].checkout() |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 2825 | if tag: |
| 2826 | repo.create_tag(tag) |
Clint Byrum | 58264dc | 2017-02-07 21:21:22 -0800 | [diff] [blame] | 2827 | return before |
| 2828 | |
James E. Blair | 9ea0d0b | 2017-04-20 09:27:15 -0700 | [diff] [blame] | 2829 | def commitConfigUpdate(self, project_name, source_name): |
| 2830 | """Commit an update to zuul.yaml |
| 2831 | |
| 2832 | This overwrites the zuul.yaml in the specificed project with |
| 2833 | the contents specified. |
| 2834 | |
| 2835 | :arg str project_name: The name of the project containing |
| 2836 | zuul.yaml (e.g., common-config) |
| 2837 | |
| 2838 | :arg str source_name: The path to the file (underneath the |
| 2839 | test fixture directory) whose contents should be used to |
| 2840 | replace zuul.yaml. |
| 2841 | """ |
| 2842 | |
| 2843 | source_path = os.path.join(FIXTURE_DIR, source_name) |
James E. Blair | dfdfcfc | 2017-04-20 10:19:20 -0700 | [diff] [blame] | 2844 | files = {} |
| 2845 | with open(source_path, 'r') as f: |
| 2846 | data = f.read() |
| 2847 | layout = yaml.safe_load(data) |
| 2848 | files['zuul.yaml'] = data |
| 2849 | for item in layout: |
| 2850 | if 'job' in item: |
| 2851 | jobname = item['job']['name'] |
| 2852 | files['playbooks/%s.yaml' % jobname] = '' |
James E. Blair | 9ea0d0b | 2017-04-20 09:27:15 -0700 | [diff] [blame] | 2853 | before = self.addCommitToRepo( |
| 2854 | project_name, 'Pulling content from %s' % source_name, |
James E. Blair | dfdfcfc | 2017-04-20 10:19:20 -0700 | [diff] [blame] | 2855 | files) |
James E. Blair | 9ea0d0b | 2017-04-20 09:27:15 -0700 | [diff] [blame] | 2856 | return before |
| 2857 | |
Clint Byrum | 627ba36 | 2017-08-14 13:20:40 -0700 | [diff] [blame] | 2858 | def newTenantConfig(self, source_name): |
| 2859 | """ Use this to update the tenant config file in tests |
| 2860 | |
| 2861 | This will update self.tenant_config_file to point to a temporary file |
| 2862 | for the duration of this particular test. The content of that file will |
| 2863 | be taken from FIXTURE_DIR/source_name |
| 2864 | |
| 2865 | After the test the original value of self.tenant_config_file will be |
| 2866 | restored. |
| 2867 | |
| 2868 | :arg str source_name: The path of the file under |
| 2869 | FIXTURE_DIR that will be used to populate the new tenant |
| 2870 | config file. |
| 2871 | """ |
| 2872 | source_path = os.path.join(FIXTURE_DIR, source_name) |
| 2873 | orig_tenant_config_file = self.tenant_config_file |
| 2874 | with tempfile.NamedTemporaryFile( |
| 2875 | delete=False, mode='wb') as new_tenant_config: |
| 2876 | self.tenant_config_file = new_tenant_config.name |
| 2877 | with open(source_path, mode='rb') as source_tenant_config: |
| 2878 | new_tenant_config.write(source_tenant_config.read()) |
| 2879 | self.config['scheduler']['tenant_config'] = self.tenant_config_file |
| 2880 | self.setupAllProjectKeys() |
| 2881 | self.log.debug( |
| 2882 | 'tenant_config_file = {}'.format(self.tenant_config_file)) |
| 2883 | |
| 2884 | def _restoreTenantConfig(): |
| 2885 | self.log.debug( |
| 2886 | 'restoring tenant_config_file = {}'.format( |
| 2887 | orig_tenant_config_file)) |
| 2888 | os.unlink(self.tenant_config_file) |
| 2889 | self.tenant_config_file = orig_tenant_config_file |
| 2890 | self.config['scheduler']['tenant_config'] = orig_tenant_config_file |
| 2891 | self.addCleanup(_restoreTenantConfig) |
| 2892 | |
James E. Blair | 7fc8daa | 2016-08-08 15:37:15 -0700 | [diff] [blame] | 2893 | def addEvent(self, connection, event): |
James E. Blair | 9ea0d0b | 2017-04-20 09:27:15 -0700 | [diff] [blame] | 2894 | |
James E. Blair | 7fc8daa | 2016-08-08 15:37:15 -0700 | [diff] [blame] | 2895 | """Inject a Fake (Gerrit) event. |
| 2896 | |
| 2897 | This method accepts a JSON-encoded event and simulates Zuul |
| 2898 | having received it from Gerrit. It could (and should) |
| 2899 | eventually apply to any connection type, but is currently only |
| 2900 | used with Gerrit connections. The name of the connection is |
| 2901 | used to look up the corresponding server, and the event is |
| 2902 | simulated as having been received by all Zuul connections |
| 2903 | attached to that server. So if two Gerrit connections in Zuul |
| 2904 | are connected to the same Gerrit server, and you invoke this |
| 2905 | method specifying the name of one of them, the event will be |
| 2906 | received by both. |
| 2907 | |
| 2908 | .. note:: |
| 2909 | |
| 2910 | "self.fake_gerrit.addEvent" calls should be migrated to |
| 2911 | this method. |
| 2912 | |
| 2913 | :arg str connection: The name of the connection corresponding |
Clark Boylan | 500992b | 2017-04-03 14:28:24 -0700 | [diff] [blame] | 2914 | to the gerrit server. |
James E. Blair | 7fc8daa | 2016-08-08 15:37:15 -0700 | [diff] [blame] | 2915 | :arg str event: The JSON-encoded event. |
| 2916 | |
| 2917 | """ |
| 2918 | specified_conn = self.connections.connections[connection] |
| 2919 | for conn in self.connections.connections.values(): |
| 2920 | if (isinstance(conn, specified_conn.__class__) and |
| 2921 | specified_conn.server == conn.server): |
| 2922 | conn.addEvent(event) |
| 2923 | |
James E. Blair | d8af542 | 2017-05-24 13:59:40 -0700 | [diff] [blame] | 2924 | def getUpstreamRepos(self, projects): |
| 2925 | """Return upstream git repo objects for the listed projects |
| 2926 | |
| 2927 | :arg list projects: A list of strings, each the canonical name |
| 2928 | of a project. |
| 2929 | |
| 2930 | :returns: A dictionary of {name: repo} for every listed |
| 2931 | project. |
| 2932 | :rtype: dict |
| 2933 | |
| 2934 | """ |
| 2935 | |
| 2936 | repos = {} |
| 2937 | for project in projects: |
| 2938 | # FIXME(jeblair): the upstream root does not yet have a |
| 2939 | # hostname component; that needs to be added, and this |
| 2940 | # line removed: |
| 2941 | tmp_project_name = '/'.join(project.split('/')[1:]) |
| 2942 | path = os.path.join(self.upstream_root, tmp_project_name) |
| 2943 | repo = git.Repo(path) |
| 2944 | repos[project] = repo |
| 2945 | return repos |
| 2946 | |
James E. Blair | 3f876d5 | 2016-07-22 13:07:14 -0700 | [diff] [blame] | 2947 | |
| 2948 | class AnsibleZuulTestCase(ZuulTestCase): |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 2949 | """ZuulTestCase but with an actual ansible executor running""" |
James E. Blair | e1767bc | 2016-08-02 10:00:27 -0700 | [diff] [blame] | 2950 | run_ansible = True |
Joshua Hesketh | 25695cb | 2017-03-06 12:50:04 +1100 | [diff] [blame] | 2951 | |
Jamie Lennox | 7655b55 | 2017-03-17 12:33:38 +1100 | [diff] [blame] | 2952 | @contextmanager |
| 2953 | def jobLog(self, build): |
| 2954 | """Print job logs on assertion errors |
| 2955 | |
| 2956 | This method is a context manager which, if it encounters an |
| 2957 | ecxeption, adds the build log to the debug output. |
| 2958 | |
| 2959 | :arg Build build: The build that's being asserted. |
| 2960 | """ |
| 2961 | try: |
| 2962 | yield |
| 2963 | except Exception: |
| 2964 | path = os.path.join(self.test_root, build.uuid, |
| 2965 | 'work', 'logs', 'job-output.txt') |
| 2966 | with open(path) as f: |
| 2967 | self.log.debug(f.read()) |
| 2968 | raise |
| 2969 | |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 2970 | |
Paul Belanger | 0a21f0a | 2017-06-13 13:14:42 -0400 | [diff] [blame] | 2971 | class SSLZuulTestCase(ZuulTestCase): |
Paul Belanger | d3232f5 | 2017-06-14 13:54:31 -0400 | [diff] [blame] | 2972 | """ZuulTestCase but using SSL when possible""" |
Paul Belanger | 0a21f0a | 2017-06-13 13:14:42 -0400 | [diff] [blame] | 2973 | use_ssl = True |
| 2974 | |
| 2975 | |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 2976 | class ZuulDBTestCase(ZuulTestCase): |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 2977 | def setup_config(self): |
| 2978 | super(ZuulDBTestCase, self).setup_config() |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 2979 | for section_name in self.config.sections(): |
| 2980 | con_match = re.match(r'^connection ([\'\"]?)(.*)(\1)$', |
| 2981 | section_name, re.I) |
| 2982 | if not con_match: |
| 2983 | continue |
| 2984 | |
| 2985 | if self.config.get(section_name, 'driver') == 'sql': |
| 2986 | f = MySQLSchemaFixture() |
| 2987 | self.useFixture(f) |
| 2988 | if (self.config.get(section_name, 'dburi') == |
| 2989 | '$MYSQL_FIXTURE_DBURI$'): |
| 2990 | self.config.set(section_name, 'dburi', f.dburi) |