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