blob: cbb06b775e1a904e25f184d0efe74921b759b7fa [file] [log] [blame]
Clark Boylanb640e052014-04-03 16:41:46 -07001#!/usr/bin/env python
2
3# Copyright 2012 Hewlett-Packard Development Company, L.P.
James E. Blair498059b2016-12-20 13:50:13 -08004# Copyright 2016 Red Hat, Inc.
Clark Boylanb640e052014-04-03 16:41:46 -07005#
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 Berendtffba5df2014-06-07 21:30:22 +020018from six.moves import configparser as ConfigParser
Clark Boylanb640e052014-04-03 16:41:46 -070019import gc
20import hashlib
21import json
22import logging
23import os
24import pprint
Christian Berendt12d4d722014-06-07 21:03:45 +020025from six.moves import queue as Queue
Morgan Fainberg293f7f82016-05-30 14:01:22 -070026from six.moves import urllib
Clark Boylanb640e052014-04-03 16:41:46 -070027import random
28import re
29import select
30import shutil
Monty Taylor74fa3862016-06-02 07:39:49 +030031from six.moves import reload_module
Clark Boylanb640e052014-04-03 16:41:46 -070032import socket
33import string
34import subprocess
35import swiftclient
James E. Blairf84026c2015-12-08 16:11:46 -080036import tempfile
Clark Boylanb640e052014-04-03 16:41:46 -070037import threading
38import time
Clark Boylanb640e052014-04-03 16:41:46 -070039
40import git
41import gear
42import fixtures
James E. Blair498059b2016-12-20 13:50:13 -080043import kazoo.client
James E. Blairdce6cea2016-12-20 16:45:32 -080044import kazoo.exceptions
Clark Boylanb640e052014-04-03 16:41:46 -070045import statsd
46import testtools
Clint Byrum3343e3e2016-11-15 16:05:03 -080047from git.exc import NoSuchPathError
Clark Boylanb640e052014-04-03 16:41:46 -070048
Joshua Hesketh352264b2015-08-11 23:42:08 +100049import zuul.connection.gerrit
50import zuul.connection.smtp
Clark Boylanb640e052014-04-03 16:41:46 -070051import zuul.scheduler
52import zuul.webapp
53import zuul.rpclistener
Joshua Hesketh0c54b2a2016-04-11 21:23:33 +100054import zuul.launcher.server
55import zuul.launcher.client
Clark Boylanb640e052014-04-03 16:41:46 -070056import zuul.lib.swift
James E. Blair83005782015-12-11 14:46:03 -080057import zuul.lib.connections
Clark Boylanb640e052014-04-03 16:41:46 -070058import zuul.merger.client
James E. Blair879dafb2015-07-17 14:04:49 -070059import zuul.merger.merger
60import zuul.merger.server
James E. Blair8d692392016-04-08 17:47:58 -070061import zuul.nodepool
Clark Boylanb640e052014-04-03 16:41:46 -070062import zuul.reporter.gerrit
63import zuul.reporter.smtp
Joshua Hesketh850ccb62014-11-27 11:31:02 +110064import zuul.source.gerrit
Clark Boylanb640e052014-04-03 16:41:46 -070065import zuul.trigger.gerrit
66import zuul.trigger.timer
James E. Blairc494d542014-08-06 09:23:52 -070067import zuul.trigger.zuultrigger
James E. Blairdce6cea2016-12-20 16:45:32 -080068import zuul.zk
Clark Boylanb640e052014-04-03 16:41:46 -070069
70FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
71 'fixtures')
James E. Blair97d902e2014-08-21 13:25:56 -070072USE_TEMPDIR = True
Clark Boylanb640e052014-04-03 16:41:46 -070073
74logging.basicConfig(level=logging.DEBUG,
75 format='%(asctime)s %(name)-32s '
76 '%(levelname)-8s %(message)s')
77
78
79def repack_repo(path):
80 cmd = ['git', '--git-dir=%s/.git' % path, 'repack', '-afd']
81 output = subprocess.Popen(cmd, close_fds=True,
82 stdout=subprocess.PIPE,
83 stderr=subprocess.PIPE)
84 out = output.communicate()
85 if output.returncode:
86 raise Exception("git repack returned %d" % output.returncode)
87 return out
88
89
90def random_sha1():
91 return hashlib.sha1(str(random.random())).hexdigest()
92
93
James E. Blaira190f3b2015-01-05 14:56:54 -080094def iterate_timeout(max_seconds, purpose):
95 start = time.time()
96 count = 0
97 while (time.time() < start + max_seconds):
98 count += 1
99 yield count
100 time.sleep(0)
101 raise Exception("Timeout waiting for %s" % purpose)
102
103
Clark Boylanb640e052014-04-03 16:41:46 -0700104class ChangeReference(git.Reference):
105 _common_path_default = "refs/changes"
106 _points_to_commits_only = True
107
108
109class FakeChange(object):
James E. Blair8b5408c2016-08-08 15:37:46 -0700110 categories = {'approved': ('Approved', -1, 1),
111 'code-review': ('Code-Review', -2, 2),
112 'verified': ('Verified', -2, 2)}
Clark Boylanb640e052014-04-03 16:41:46 -0700113
114 def __init__(self, gerrit, number, project, branch, subject,
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700115 status='NEW', upstream_root=None, files={}):
Clark Boylanb640e052014-04-03 16:41:46 -0700116 self.gerrit = gerrit
117 self.reported = 0
118 self.queried = 0
119 self.patchsets = []
120 self.number = number
121 self.project = project
122 self.branch = branch
123 self.subject = subject
124 self.latest_patchset = 0
125 self.depends_on_change = None
126 self.needed_by_changes = []
127 self.fail_merge = False
128 self.messages = []
129 self.data = {
130 'branch': branch,
131 'comments': [],
132 'commitMessage': subject,
133 'createdOn': time.time(),
134 'id': 'I' + random_sha1(),
135 'lastUpdated': time.time(),
136 'number': str(number),
137 'open': status == 'NEW',
138 'owner': {'email': 'user@example.com',
139 'name': 'User Name',
140 'username': 'username'},
141 'patchSets': self.patchsets,
142 'project': project,
143 'status': status,
144 'subject': subject,
145 'submitRecords': [],
146 'url': 'https://hostname/%s' % number}
147
148 self.upstream_root = upstream_root
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700149 self.addPatchset(files=files)
Clark Boylanb640e052014-04-03 16:41:46 -0700150 self.data['submitRecords'] = self.getSubmitRecords()
151 self.open = status == 'NEW'
152
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700153 def addFakeChangeToRepo(self, msg, files, large):
Clark Boylanb640e052014-04-03 16:41:46 -0700154 path = os.path.join(self.upstream_root, self.project)
155 repo = git.Repo(path)
156 ref = ChangeReference.create(repo, '1/%s/%s' % (self.number,
157 self.latest_patchset),
158 'refs/tags/init')
159 repo.head.reference = ref
James E. Blair879dafb2015-07-17 14:04:49 -0700160 zuul.merger.merger.reset_repo_to_head(repo)
Clark Boylanb640e052014-04-03 16:41:46 -0700161 repo.git.clean('-x', '-f', '-d')
162
163 path = os.path.join(self.upstream_root, self.project)
164 if not large:
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700165 for fn, content in files.items():
166 fn = os.path.join(path, fn)
167 with open(fn, 'w') as f:
168 f.write(content)
169 repo.index.add([fn])
Clark Boylanb640e052014-04-03 16:41:46 -0700170 else:
171 for fni in range(100):
172 fn = os.path.join(path, str(fni))
173 f = open(fn, 'w')
174 for ci in range(4096):
175 f.write(random.choice(string.printable))
176 f.close()
177 repo.index.add([fn])
178
179 r = repo.index.commit(msg)
180 repo.head.reference = 'master'
James E. Blair879dafb2015-07-17 14:04:49 -0700181 zuul.merger.merger.reset_repo_to_head(repo)
Clark Boylanb640e052014-04-03 16:41:46 -0700182 repo.git.clean('-x', '-f', '-d')
183 repo.heads['master'].checkout()
184 return r
185
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700186 def addPatchset(self, files=None, large=False):
Clark Boylanb640e052014-04-03 16:41:46 -0700187 self.latest_patchset += 1
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700188 if not files:
James E. Blair97d902e2014-08-21 13:25:56 -0700189 fn = '%s-%s' % (self.branch.replace('/', '_'), self.number)
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700190 data = ("test %s %s %s\n" %
191 (self.branch, self.number, self.latest_patchset))
192 files = {fn: data}
Clark Boylanb640e052014-04-03 16:41:46 -0700193 msg = self.subject + '-' + str(self.latest_patchset)
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700194 c = self.addFakeChangeToRepo(msg, files, large)
Clark Boylanb640e052014-04-03 16:41:46 -0700195 ps_files = [{'file': '/COMMIT_MSG',
196 'type': 'ADDED'},
197 {'file': 'README',
198 'type': 'MODIFIED'}]
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700199 for f in files.keys():
Clark Boylanb640e052014-04-03 16:41:46 -0700200 ps_files.append({'file': f, 'type': 'ADDED'})
201 d = {'approvals': [],
202 'createdOn': time.time(),
203 'files': ps_files,
204 'number': str(self.latest_patchset),
205 'ref': 'refs/changes/1/%s/%s' % (self.number,
206 self.latest_patchset),
207 'revision': c.hexsha,
208 'uploader': {'email': 'user@example.com',
209 'name': 'User name',
210 'username': 'user'}}
211 self.data['currentPatchSet'] = d
212 self.patchsets.append(d)
213 self.data['submitRecords'] = self.getSubmitRecords()
214
215 def getPatchsetCreatedEvent(self, patchset):
216 event = {"type": "patchset-created",
217 "change": {"project": self.project,
218 "branch": self.branch,
219 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
220 "number": str(self.number),
221 "subject": self.subject,
222 "owner": {"name": "User Name"},
223 "url": "https://hostname/3"},
224 "patchSet": self.patchsets[patchset - 1],
225 "uploader": {"name": "User Name"}}
226 return event
227
228 def getChangeRestoredEvent(self):
229 event = {"type": "change-restored",
230 "change": {"project": self.project,
231 "branch": self.branch,
232 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
233 "number": str(self.number),
234 "subject": self.subject,
235 "owner": {"name": "User Name"},
236 "url": "https://hostname/3"},
237 "restorer": {"name": "User Name"},
Antoine Mussobd86a312014-01-08 14:51:33 +0100238 "patchSet": self.patchsets[-1],
239 "reason": ""}
240 return event
241
242 def getChangeAbandonedEvent(self):
243 event = {"type": "change-abandoned",
244 "change": {"project": self.project,
245 "branch": self.branch,
246 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
247 "number": str(self.number),
248 "subject": self.subject,
249 "owner": {"name": "User Name"},
250 "url": "https://hostname/3"},
251 "abandoner": {"name": "User Name"},
252 "patchSet": self.patchsets[-1],
Clark Boylanb640e052014-04-03 16:41:46 -0700253 "reason": ""}
254 return event
255
256 def getChangeCommentEvent(self, patchset):
257 event = {"type": "comment-added",
258 "change": {"project": self.project,
259 "branch": self.branch,
260 "id": "I5459869c07352a31bfb1e7a8cac379cabfcb25af",
261 "number": str(self.number),
262 "subject": self.subject,
263 "owner": {"name": "User Name"},
264 "url": "https://hostname/3"},
265 "patchSet": self.patchsets[patchset - 1],
266 "author": {"name": "User Name"},
James E. Blair8b5408c2016-08-08 15:37:46 -0700267 "approvals": [{"type": "code-review",
Clark Boylanb640e052014-04-03 16:41:46 -0700268 "description": "Code-Review",
269 "value": "0"}],
270 "comment": "This is a comment"}
271 return event
272
Joshua Hesketh642824b2014-07-01 17:54:59 +1000273 def addApproval(self, category, value, username='reviewer_john',
274 granted_on=None, message=''):
Clark Boylanb640e052014-04-03 16:41:46 -0700275 if not granted_on:
276 granted_on = time.time()
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000277 approval = {
278 'description': self.categories[category][0],
279 'type': category,
280 'value': str(value),
281 'by': {
282 'username': username,
283 'email': username + '@example.com',
284 },
285 'grantedOn': int(granted_on)
286 }
Clark Boylanb640e052014-04-03 16:41:46 -0700287 for i, x in enumerate(self.patchsets[-1]['approvals'][:]):
288 if x['by']['username'] == username and x['type'] == category:
289 del self.patchsets[-1]['approvals'][i]
290 self.patchsets[-1]['approvals'].append(approval)
291 event = {'approvals': [approval],
Joshua Hesketh642824b2014-07-01 17:54:59 +1000292 'author': {'email': 'author@example.com',
293 'name': 'Patchset Author',
294 'username': 'author_phil'},
Clark Boylanb640e052014-04-03 16:41:46 -0700295 'change': {'branch': self.branch,
296 'id': 'Iaa69c46accf97d0598111724a38250ae76a22c87',
297 'number': str(self.number),
Joshua Hesketh642824b2014-07-01 17:54:59 +1000298 'owner': {'email': 'owner@example.com',
299 'name': 'Change Owner',
300 'username': 'owner_jane'},
Clark Boylanb640e052014-04-03 16:41:46 -0700301 'project': self.project,
302 'subject': self.subject,
303 'topic': 'master',
304 'url': 'https://hostname/459'},
Joshua Hesketh642824b2014-07-01 17:54:59 +1000305 'comment': message,
Clark Boylanb640e052014-04-03 16:41:46 -0700306 'patchSet': self.patchsets[-1],
307 'type': 'comment-added'}
308 self.data['submitRecords'] = self.getSubmitRecords()
309 return json.loads(json.dumps(event))
310
311 def getSubmitRecords(self):
312 status = {}
313 for cat in self.categories.keys():
314 status[cat] = 0
315
316 for a in self.patchsets[-1]['approvals']:
317 cur = status[a['type']]
318 cat_min, cat_max = self.categories[a['type']][1:]
319 new = int(a['value'])
320 if new == cat_min:
321 cur = new
322 elif abs(new) > abs(cur):
323 cur = new
324 status[a['type']] = cur
325
326 labels = []
327 ok = True
328 for typ, cat in self.categories.items():
329 cur = status[typ]
330 cat_min, cat_max = cat[1:]
331 if cur == cat_min:
332 value = 'REJECT'
333 ok = False
334 elif cur == cat_max:
335 value = 'OK'
336 else:
337 value = 'NEED'
338 ok = False
339 labels.append({'label': cat[0], 'status': value})
340 if ok:
341 return [{'status': 'OK'}]
342 return [{'status': 'NOT_READY',
343 'labels': labels}]
344
345 def setDependsOn(self, other, patchset):
346 self.depends_on_change = other
347 d = {'id': other.data['id'],
348 'number': other.data['number'],
349 'ref': other.patchsets[patchset - 1]['ref']
350 }
351 self.data['dependsOn'] = [d]
352
353 other.needed_by_changes.append(self)
354 needed = other.data.get('neededBy', [])
355 d = {'id': self.data['id'],
356 'number': self.data['number'],
357 'ref': self.patchsets[patchset - 1]['ref'],
358 'revision': self.patchsets[patchset - 1]['revision']
359 }
360 needed.append(d)
361 other.data['neededBy'] = needed
362
363 def query(self):
364 self.queried += 1
365 d = self.data.get('dependsOn')
366 if d:
367 d = d[0]
368 if (self.depends_on_change.patchsets[-1]['ref'] == d['ref']):
369 d['isCurrentPatchSet'] = True
370 else:
371 d['isCurrentPatchSet'] = False
372 return json.loads(json.dumps(self.data))
373
374 def setMerged(self):
375 if (self.depends_on_change and
Joshua Hesketh29d99b72014-08-19 16:27:42 +1000376 self.depends_on_change.data['status'] != 'MERGED'):
Clark Boylanb640e052014-04-03 16:41:46 -0700377 return
378 if self.fail_merge:
379 return
380 self.data['status'] = 'MERGED'
381 self.open = False
382
383 path = os.path.join(self.upstream_root, self.project)
384 repo = git.Repo(path)
385 repo.heads[self.branch].commit = \
386 repo.commit(self.patchsets[-1]['revision'])
387
388 def setReported(self):
389 self.reported += 1
390
391
Joshua Hesketh352264b2015-08-11 23:42:08 +1000392class FakeGerritConnection(zuul.connection.gerrit.GerritConnection):
James E. Blaire7b99a02016-08-05 14:27:34 -0700393 """A Fake Gerrit connection for use in tests.
394
395 This subclasses
396 :py:class:`~zuul.connection.gerrit.GerritConnection` to add the
397 ability for tests to add changes to the fake Gerrit it represents.
398 """
399
Joshua Hesketh352264b2015-08-11 23:42:08 +1000400 log = logging.getLogger("zuul.test.FakeGerritConnection")
James E. Blair96698e22015-04-02 07:48:21 -0700401
Joshua Hesketh352264b2015-08-11 23:42:08 +1000402 def __init__(self, connection_name, connection_config,
James E. Blair7fc8daa2016-08-08 15:37:15 -0700403 changes_db=None, upstream_root=None):
Joshua Hesketh352264b2015-08-11 23:42:08 +1000404 super(FakeGerritConnection, self).__init__(connection_name,
405 connection_config)
406
James E. Blair7fc8daa2016-08-08 15:37:15 -0700407 self.event_queue = Queue.Queue()
Clark Boylanb640e052014-04-03 16:41:46 -0700408 self.fixture_dir = os.path.join(FIXTURE_DIR, 'gerrit')
409 self.change_number = 0
Joshua Hesketh352264b2015-08-11 23:42:08 +1000410 self.changes = changes_db
James E. Blairf8ff9932014-08-15 15:24:24 -0700411 self.queries = []
Jan Hruban6b71aff2015-10-22 16:58:08 +0200412 self.upstream_root = upstream_root
Clark Boylanb640e052014-04-03 16:41:46 -0700413
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700414 def addFakeChange(self, project, branch, subject, status='NEW',
415 files=None):
James E. Blaire7b99a02016-08-05 14:27:34 -0700416 """Add a change to the fake Gerrit."""
Clark Boylanb640e052014-04-03 16:41:46 -0700417 self.change_number += 1
418 c = FakeChange(self, self.change_number, project, branch, subject,
419 upstream_root=self.upstream_root,
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700420 status=status, files=files)
Clark Boylanb640e052014-04-03 16:41:46 -0700421 self.changes[self.change_number] = c
422 return c
423
Clark Boylanb640e052014-04-03 16:41:46 -0700424 def review(self, project, changeid, message, action):
425 number, ps = changeid.split(',')
426 change = self.changes[int(number)]
Joshua Hesketh642824b2014-07-01 17:54:59 +1000427
428 # Add the approval back onto the change (ie simulate what gerrit would
429 # do).
430 # Usually when zuul leaves a review it'll create a feedback loop where
431 # zuul's review enters another gerrit event (which is then picked up by
432 # zuul). However, we can't mimic this behaviour (by adding this
433 # approval event into the queue) as it stops jobs from checking what
434 # happens before this event is triggered. If a job needs to see what
435 # happens they can add their own verified event into the queue.
436 # Nevertheless, we can update change with the new review in gerrit.
437
James E. Blair8b5408c2016-08-08 15:37:46 -0700438 for cat in action.keys():
439 if cat != 'submit':
Joshua Hesketh352264b2015-08-11 23:42:08 +1000440 change.addApproval(cat, action[cat], username=self.user)
Joshua Hesketh642824b2014-07-01 17:54:59 +1000441
James E. Blair8b5408c2016-08-08 15:37:46 -0700442 # TODOv3(jeblair): can this be removed?
Joshua Hesketh642824b2014-07-01 17:54:59 +1000443 if 'label' in action:
444 parts = action['label'].split('=')
Joshua Hesketh352264b2015-08-11 23:42:08 +1000445 change.addApproval(parts[0], parts[2], username=self.user)
Joshua Hesketh642824b2014-07-01 17:54:59 +1000446
Clark Boylanb640e052014-04-03 16:41:46 -0700447 change.messages.append(message)
Joshua Hesketh642824b2014-07-01 17:54:59 +1000448
Clark Boylanb640e052014-04-03 16:41:46 -0700449 if 'submit' in action:
450 change.setMerged()
451 if message:
452 change.setReported()
453
454 def query(self, number):
455 change = self.changes.get(int(number))
456 if change:
457 return change.query()
458 return {}
459
James E. Blairc494d542014-08-06 09:23:52 -0700460 def simpleQuery(self, query):
James E. Blair96698e22015-04-02 07:48:21 -0700461 self.log.debug("simpleQuery: %s" % query)
James E. Blairf8ff9932014-08-15 15:24:24 -0700462 self.queries.append(query)
James E. Blair5ee24252014-12-30 10:12:29 -0800463 if query.startswith('change:'):
464 # Query a specific changeid
465 changeid = query[len('change:'):]
466 l = [change.query() for change in self.changes.values()
467 if change.data['id'] == changeid]
James E. Blair96698e22015-04-02 07:48:21 -0700468 elif query.startswith('message:'):
469 # Query the content of a commit message
470 msg = query[len('message:'):].strip()
471 l = [change.query() for change in self.changes.values()
472 if msg in change.data['commitMessage']]
James E. Blair5ee24252014-12-30 10:12:29 -0800473 else:
474 # Query all open changes
475 l = [change.query() for change in self.changes.values()]
James E. Blairf8ff9932014-08-15 15:24:24 -0700476 return l
James E. Blairc494d542014-08-06 09:23:52 -0700477
Joshua Hesketh352264b2015-08-11 23:42:08 +1000478 def _start_watcher_thread(self, *args, **kw):
Clark Boylanb640e052014-04-03 16:41:46 -0700479 pass
480
Joshua Hesketh352264b2015-08-11 23:42:08 +1000481 def getGitUrl(self, project):
482 return os.path.join(self.upstream_root, project.name)
483
Adam Gandelmanc5e4f1d2016-11-29 14:27:17 -0800484 def _getGitwebUrl(self, project, sha=None):
485 return self.getGitwebUrl(project, sha)
486
Clark Boylanb640e052014-04-03 16:41:46 -0700487
488class BuildHistory(object):
489 def __init__(self, **kw):
490 self.__dict__.update(kw)
491
492 def __repr__(self):
James E. Blair17302972016-08-10 16:11:42 -0700493 return ("<Completed build, result: %s name: %s uuid: %s changes: %s>" %
494 (self.result, self.name, self.uuid, self.changes))
Clark Boylanb640e052014-04-03 16:41:46 -0700495
496
497class FakeURLOpener(object):
Jan Hruban6b71aff2015-10-22 16:58:08 +0200498 def __init__(self, upstream_root, url):
Clark Boylanb640e052014-04-03 16:41:46 -0700499 self.upstream_root = upstream_root
Clark Boylanb640e052014-04-03 16:41:46 -0700500 self.url = url
501
502 def read(self):
Morgan Fainberg293f7f82016-05-30 14:01:22 -0700503 res = urllib.parse.urlparse(self.url)
Clark Boylanb640e052014-04-03 16:41:46 -0700504 path = res.path
505 project = '/'.join(path.split('/')[2:-2])
506 ret = '001e# service=git-upload-pack\n'
507 ret += ('000000a31270149696713ba7e06f1beb760f20d359c4abed HEAD\x00'
508 'multi_ack thin-pack side-band side-band-64k ofs-delta '
509 'shallow no-progress include-tag multi_ack_detailed no-done\n')
510 path = os.path.join(self.upstream_root, project)
511 repo = git.Repo(path)
512 for ref in repo.refs:
513 r = ref.object.hexsha + ' ' + ref.path + '\n'
514 ret += '%04x%s' % (len(r) + 4, r)
515 ret += '0000'
516 return ret
517
518
Clark Boylanb640e052014-04-03 16:41:46 -0700519class FakeStatsd(threading.Thread):
520 def __init__(self):
521 threading.Thread.__init__(self)
522 self.daemon = True
523 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
524 self.sock.bind(('', 0))
525 self.port = self.sock.getsockname()[1]
526 self.wake_read, self.wake_write = os.pipe()
527 self.stats = []
528
529 def run(self):
530 while True:
531 poll = select.poll()
532 poll.register(self.sock, select.POLLIN)
533 poll.register(self.wake_read, select.POLLIN)
534 ret = poll.poll()
535 for (fd, event) in ret:
536 if fd == self.sock.fileno():
537 data = self.sock.recvfrom(1024)
538 if not data:
539 return
540 self.stats.append(data[0])
541 if fd == self.wake_read:
542 return
543
544 def stop(self):
545 os.write(self.wake_write, '1\n')
546
547
James E. Blaire1767bc2016-08-02 10:00:27 -0700548class FakeBuild(object):
Clark Boylanb640e052014-04-03 16:41:46 -0700549 log = logging.getLogger("zuul.test")
550
James E. Blair34776ee2016-08-25 13:53:54 -0700551 def __init__(self, launch_server, job):
Clark Boylanb640e052014-04-03 16:41:46 -0700552 self.daemon = True
James E. Blaire1767bc2016-08-02 10:00:27 -0700553 self.launch_server = launch_server
Clark Boylanb640e052014-04-03 16:41:46 -0700554 self.job = job
James E. Blairab7132b2016-08-05 12:36:22 -0700555 self.jobdir = None
James E. Blair17302972016-08-10 16:11:42 -0700556 self.uuid = job.unique
Clark Boylanb640e052014-04-03 16:41:46 -0700557 self.parameters = json.loads(job.arguments)
James E. Blair34776ee2016-08-25 13:53:54 -0700558 # TODOv3(jeblair): self.node is really "the image of the node
559 # assigned". We should rename it (self.node_image?) if we
560 # keep using it like this, or we may end up exposing more of
561 # the complexity around multi-node jobs here
562 # (self.nodes[0].image?)
563 self.node = None
564 if len(self.parameters.get('nodes')) == 1:
565 self.node = self.parameters['nodes'][0]['image']
Clark Boylanb640e052014-04-03 16:41:46 -0700566 self.unique = self.parameters['ZUUL_UUID']
James E. Blair3f876d52016-07-22 13:07:14 -0700567 self.name = self.parameters['job']
Clark Boylanb640e052014-04-03 16:41:46 -0700568 self.wait_condition = threading.Condition()
569 self.waiting = False
570 self.aborted = False
Paul Belanger71d98172016-11-08 10:56:31 -0500571 self.requeue = False
Clark Boylanb640e052014-04-03 16:41:46 -0700572 self.created = time.time()
Clark Boylanb640e052014-04-03 16:41:46 -0700573 self.run_error = False
James E. Blaire1767bc2016-08-02 10:00:27 -0700574 self.changes = None
575 if 'ZUUL_CHANGE_IDS' in self.parameters:
576 self.changes = self.parameters['ZUUL_CHANGE_IDS']
Clark Boylanb640e052014-04-03 16:41:46 -0700577
James E. Blair3158e282016-08-19 09:34:11 -0700578 def __repr__(self):
579 waiting = ''
580 if self.waiting:
581 waiting = ' [waiting]'
582 return '<FakeBuild %s %s%s>' % (self.name, self.changes, waiting)
583
Clark Boylanb640e052014-04-03 16:41:46 -0700584 def release(self):
James E. Blaire7b99a02016-08-05 14:27:34 -0700585 """Release this build."""
Clark Boylanb640e052014-04-03 16:41:46 -0700586 self.wait_condition.acquire()
587 self.wait_condition.notify()
588 self.waiting = False
589 self.log.debug("Build %s released" % self.unique)
590 self.wait_condition.release()
591
592 def isWaiting(self):
James E. Blaire7b99a02016-08-05 14:27:34 -0700593 """Return whether this build is being held.
594
595 :returns: Whether the build is being held.
596 :rtype: bool
597 """
598
Clark Boylanb640e052014-04-03 16:41:46 -0700599 self.wait_condition.acquire()
600 if self.waiting:
601 ret = True
602 else:
603 ret = False
604 self.wait_condition.release()
605 return ret
606
607 def _wait(self):
608 self.wait_condition.acquire()
609 self.waiting = True
610 self.log.debug("Build %s waiting" % self.unique)
611 self.wait_condition.wait()
612 self.wait_condition.release()
613
614 def run(self):
Clark Boylanb640e052014-04-03 16:41:46 -0700615 self.log.debug('Running build %s' % self.unique)
616
James E. Blaire1767bc2016-08-02 10:00:27 -0700617 if self.launch_server.hold_jobs_in_build:
Clark Boylanb640e052014-04-03 16:41:46 -0700618 self.log.debug('Holding build %s' % self.unique)
619 self._wait()
620 self.log.debug("Build %s continuing" % self.unique)
621
Clark Boylanb640e052014-04-03 16:41:46 -0700622 result = 'SUCCESS'
James E. Blaira5dba232016-08-08 15:53:24 -0700623 if (('ZUUL_REF' in self.parameters) and self.shouldFail()):
Clark Boylanb640e052014-04-03 16:41:46 -0700624 result = 'FAILURE'
625 if self.aborted:
626 result = 'ABORTED'
Paul Belanger71d98172016-11-08 10:56:31 -0500627 if self.requeue:
628 result = None
Clark Boylanb640e052014-04-03 16:41:46 -0700629
630 if self.run_error:
Clark Boylanb640e052014-04-03 16:41:46 -0700631 result = 'RUN_ERROR'
Clark Boylanb640e052014-04-03 16:41:46 -0700632
James E. Blaire1767bc2016-08-02 10:00:27 -0700633 return result
Clark Boylanb640e052014-04-03 16:41:46 -0700634
James E. Blaira5dba232016-08-08 15:53:24 -0700635 def shouldFail(self):
636 changes = self.launch_server.fail_tests.get(self.name, [])
637 for change in changes:
638 if self.hasChanges(change):
639 return True
640 return False
641
James E. Blaire7b99a02016-08-05 14:27:34 -0700642 def hasChanges(self, *changes):
643 """Return whether this build has certain changes in its git repos.
644
645 :arg FakeChange changes: One or more changes (varargs) that
646 are expected to be present (in order) in the git repository of
647 the active project.
648
649 :returns: Whether the build has the indicated changes.
650 :rtype: bool
651
652 """
Clint Byrum3343e3e2016-11-15 16:05:03 -0800653 for change in changes:
654 path = os.path.join(self.jobdir.git_root, change.project)
655 try:
656 repo = git.Repo(path)
657 except NoSuchPathError as e:
658 self.log.debug('%s' % e)
659 return False
660 ref = self.parameters['ZUUL_REF']
661 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
662 commit_message = '%s-1' % change.subject
663 self.log.debug("Checking if build %s has changes; commit_message "
664 "%s; repo_messages %s" % (self, commit_message,
665 repo_messages))
666 if commit_message not in repo_messages:
James E. Blair962220f2016-08-03 11:22:38 -0700667 self.log.debug(" messages do not match")
668 return False
669 self.log.debug(" OK")
670 return True
671
Clark Boylanb640e052014-04-03 16:41:46 -0700672
Joshua Hesketh0c54b2a2016-04-11 21:23:33 +1000673class RecordingLaunchServer(zuul.launcher.server.LaunchServer):
James E. Blaire7b99a02016-08-05 14:27:34 -0700674 """An Ansible launcher to be used in tests.
675
676 :ivar bool hold_jobs_in_build: If true, when jobs are launched
677 they will report that they have started but then pause until
678 released before reporting completion. This attribute may be
679 changed at any time and will take effect for subsequently
680 launched builds, but previously held builds will still need to
681 be explicitly released.
682
683 """
James E. Blairf5dbd002015-12-23 15:26:17 -0800684 def __init__(self, *args, **kw):
James E. Blaire1767bc2016-08-02 10:00:27 -0700685 self._run_ansible = kw.pop('_run_ansible', False)
James E. Blairf5dbd002015-12-23 15:26:17 -0800686 super(RecordingLaunchServer, self).__init__(*args, **kw)
James E. Blaire1767bc2016-08-02 10:00:27 -0700687 self.hold_jobs_in_build = False
688 self.lock = threading.Lock()
689 self.running_builds = []
James E. Blair3f876d52016-07-22 13:07:14 -0700690 self.build_history = []
James E. Blaire1767bc2016-08-02 10:00:27 -0700691 self.fail_tests = {}
James E. Blairab7132b2016-08-05 12:36:22 -0700692 self.job_builds = {}
James E. Blairf5dbd002015-12-23 15:26:17 -0800693
James E. Blaira5dba232016-08-08 15:53:24 -0700694 def failJob(self, name, change):
James E. Blaire7b99a02016-08-05 14:27:34 -0700695 """Instruct the launcher to report matching builds as failures.
696
697 :arg str name: The name of the job to fail.
James E. Blaira5dba232016-08-08 15:53:24 -0700698 :arg Change change: The :py:class:`~tests.base.FakeChange`
699 instance which should cause the job to fail. This job
700 will also fail for changes depending on this change.
James E. Blaire7b99a02016-08-05 14:27:34 -0700701
702 """
James E. Blaire1767bc2016-08-02 10:00:27 -0700703 l = self.fail_tests.get(name, [])
704 l.append(change)
705 self.fail_tests[name] = l
James E. Blairf5dbd002015-12-23 15:26:17 -0800706
James E. Blair962220f2016-08-03 11:22:38 -0700707 def release(self, regex=None):
James E. Blaire7b99a02016-08-05 14:27:34 -0700708 """Release a held build.
709
710 :arg str regex: A regular expression which, if supplied, will
711 cause only builds with matching names to be released. If
712 not supplied, all builds will be released.
713
714 """
James E. Blair962220f2016-08-03 11:22:38 -0700715 builds = self.running_builds[:]
716 self.log.debug("Releasing build %s (%s)" % (regex,
717 len(self.running_builds)))
718 for build in builds:
719 if not regex or re.match(regex, build.name):
720 self.log.debug("Releasing build %s" %
721 (build.parameters['ZUUL_UUID']))
722 build.release()
723 else:
724 self.log.debug("Not releasing build %s" %
725 (build.parameters['ZUUL_UUID']))
726 self.log.debug("Done releasing builds %s (%s)" %
727 (regex, len(self.running_builds)))
728
James E. Blair17302972016-08-10 16:11:42 -0700729 def launchJob(self, job):
James E. Blair34776ee2016-08-25 13:53:54 -0700730 build = FakeBuild(self, job)
James E. Blaire1767bc2016-08-02 10:00:27 -0700731 job.build = build
James E. Blaire1767bc2016-08-02 10:00:27 -0700732 self.running_builds.append(build)
James E. Blairab7132b2016-08-05 12:36:22 -0700733 self.job_builds[job.unique] = build
James E. Blair17302972016-08-10 16:11:42 -0700734 super(RecordingLaunchServer, self).launchJob(job)
735
736 def stopJob(self, job):
737 self.log.debug("handle stop")
738 parameters = json.loads(job.arguments)
739 uuid = parameters['uuid']
740 for build in self.running_builds:
741 if build.unique == uuid:
742 build.aborted = True
743 build.release()
744 super(RecordingLaunchServer, self).stopJob(job)
James E. Blairab7132b2016-08-05 12:36:22 -0700745
746 def runAnsible(self, jobdir, job):
747 build = self.job_builds[job.unique]
748 build.jobdir = jobdir
James E. Blaire1767bc2016-08-02 10:00:27 -0700749
750 if self._run_ansible:
751 result = super(RecordingLaunchServer, self).runAnsible(jobdir, job)
752 else:
753 result = build.run()
754
755 self.lock.acquire()
756 self.build_history.append(
James E. Blair17302972016-08-10 16:11:42 -0700757 BuildHistory(name=build.name, result=result, changes=build.changes,
758 node=build.node, uuid=build.unique,
759 parameters=build.parameters,
James E. Blaire1767bc2016-08-02 10:00:27 -0700760 pipeline=build.parameters['ZUUL_PIPELINE'])
761 )
James E. Blairab7132b2016-08-05 12:36:22 -0700762 self.running_builds.remove(build)
763 del self.job_builds[job.unique]
James E. Blaire1767bc2016-08-02 10:00:27 -0700764 self.lock.release()
Clint Byrum69e47122016-12-02 16:40:35 -0800765 if build.run_error:
766 result = None
James E. Blaire1767bc2016-08-02 10:00:27 -0700767 return result
James E. Blairf5dbd002015-12-23 15:26:17 -0800768
769
Clark Boylanb640e052014-04-03 16:41:46 -0700770class FakeGearmanServer(gear.Server):
James E. Blaire7b99a02016-08-05 14:27:34 -0700771 """A Gearman server for use in tests.
772
773 :ivar bool hold_jobs_in_queue: If true, submitted jobs will be
774 added to the queue but will not be distributed to workers
775 until released. This attribute may be changed at any time and
776 will take effect for subsequently enqueued jobs, but
777 previously held jobs will still need to be explicitly
778 released.
779
780 """
781
Clark Boylanb640e052014-04-03 16:41:46 -0700782 def __init__(self):
783 self.hold_jobs_in_queue = False
784 super(FakeGearmanServer, self).__init__(0)
785
786 def getJobForConnection(self, connection, peek=False):
787 for queue in [self.high_queue, self.normal_queue, self.low_queue]:
788 for job in queue:
789 if not hasattr(job, 'waiting'):
Paul Belanger6ab6af72016-11-06 11:32:59 -0500790 if job.name.startswith('launcher:launch'):
Clark Boylanb640e052014-04-03 16:41:46 -0700791 job.waiting = self.hold_jobs_in_queue
792 else:
793 job.waiting = False
794 if job.waiting:
795 continue
796 if job.name in connection.functions:
797 if not peek:
798 queue.remove(job)
799 connection.related_jobs[job.handle] = job
800 job.worker_connection = connection
801 job.running = True
802 return job
803 return None
804
805 def release(self, regex=None):
James E. Blaire7b99a02016-08-05 14:27:34 -0700806 """Release a held job.
807
808 :arg str regex: A regular expression which, if supplied, will
809 cause only jobs with matching names to be released. If
810 not supplied, all jobs will be released.
811 """
Clark Boylanb640e052014-04-03 16:41:46 -0700812 released = False
813 qlen = (len(self.high_queue) + len(self.normal_queue) +
814 len(self.low_queue))
815 self.log.debug("releasing queued job %s (%s)" % (regex, qlen))
816 for job in self.getQueue():
Paul Belanger6ab6af72016-11-06 11:32:59 -0500817 if job.name != 'launcher:launch':
Clark Boylanb640e052014-04-03 16:41:46 -0700818 continue
Paul Belanger6ab6af72016-11-06 11:32:59 -0500819 parameters = json.loads(job.arguments)
820 if not regex or re.match(regex, parameters.get('job')):
Clark Boylanb640e052014-04-03 16:41:46 -0700821 self.log.debug("releasing queued job %s" %
822 job.unique)
823 job.waiting = False
824 released = True
825 else:
826 self.log.debug("not releasing queued job %s" %
827 job.unique)
828 if released:
829 self.wakeConnections()
830 qlen = (len(self.high_queue) + len(self.normal_queue) +
831 len(self.low_queue))
832 self.log.debug("done releasing queued jobs %s (%s)" % (regex, qlen))
833
834
835class FakeSMTP(object):
836 log = logging.getLogger('zuul.FakeSMTP')
837
838 def __init__(self, messages, server, port):
839 self.server = server
840 self.port = port
841 self.messages = messages
842
843 def sendmail(self, from_email, to_email, msg):
844 self.log.info("Sending email from %s, to %s, with msg %s" % (
845 from_email, to_email, msg))
846
847 headers = msg.split('\n\n', 1)[0]
848 body = msg.split('\n\n', 1)[1]
849
850 self.messages.append(dict(
851 from_email=from_email,
852 to_email=to_email,
853 msg=msg,
854 headers=headers,
855 body=body,
856 ))
857
858 return True
859
860 def quit(self):
861 return True
862
863
864class FakeSwiftClientConnection(swiftclient.client.Connection):
865 def post_account(self, headers):
866 # Do nothing
867 pass
868
869 def get_auth(self):
870 # Returns endpoint and (unused) auth token
871 endpoint = os.path.join('https://storage.example.org', 'V1',
872 'AUTH_account')
873 return endpoint, ''
874
875
James E. Blairdce6cea2016-12-20 16:45:32 -0800876class FakeNodepool(object):
877 REQUEST_ROOT = '/nodepool/requests'
878
879 log = logging.getLogger("zuul.test.FakeNodepool")
880
881 def __init__(self, host, port, chroot):
882 self.client = kazoo.client.KazooClient(
883 hosts='%s:%s%s' % (host, port, chroot))
884 self.client.start()
885 self._running = True
886 self.thread = threading.Thread(target=self.run)
887 self.thread.daemon = True
888 self.thread.start()
889
890 def stop(self):
891 self._running = False
892 self.thread.join()
893 self.client.stop()
894 self.client.close()
895
896 def run(self):
897 while self._running:
898 self._run()
899 time.sleep(0.1)
900
901 def _run(self):
902 for req in self.getNodeRequests():
903 self.fulfillRequest(req)
904
905 def getNodeRequests(self):
906 try:
907 reqids = self.client.get_children(self.REQUEST_ROOT)
908 except kazoo.exceptions.NoNodeError:
909 return []
910 reqs = []
911 for oid in sorted(reqids):
912 path = self.REQUEST_ROOT + '/' + oid
913 data, stat = self.client.get(path)
914 data = json.loads(data)
915 data['_oid'] = oid
916 reqs.append(data)
917 return reqs
918
919 def fulfillRequest(self, request):
920 if request['state'] == 'fulfilled':
921 return
922 request = request.copy()
923 request['state'] = 'fulfilled'
924 request['state_time'] = time.time()
925 oid = request['_oid']
926 del request['_oid']
927 path = self.REQUEST_ROOT + '/' + oid
928 data = json.dumps(request)
929 self.log.debug("Fulfilling node request: %s %s" % (oid, data))
930 self.client.set(path, data)
931
932
James E. Blair498059b2016-12-20 13:50:13 -0800933class ChrootedKazooFixture(fixtures.Fixture):
934 def __init__(self):
935 super(ChrootedKazooFixture, self).__init__()
936
937 zk_host = os.environ.get('NODEPOOL_ZK_HOST', 'localhost')
938 if ':' in zk_host:
939 host, port = zk_host.split(':')
940 else:
941 host = zk_host
942 port = None
943
944 self.zookeeper_host = host
945
946 if not port:
947 self.zookeeper_port = 2181
948 else:
949 self.zookeeper_port = int(port)
950
951 def _setUp(self):
952 # Make sure the test chroot paths do not conflict
953 random_bits = ''.join(random.choice(string.ascii_lowercase +
954 string.ascii_uppercase)
955 for x in range(8))
956
957 rand_test_path = '%s_%s' % (random_bits, os.getpid())
958 self.zookeeper_chroot = "/nodepool_test/%s" % rand_test_path
959
960 # Ensure the chroot path exists and clean up any pre-existing znodes.
961 _tmp_client = kazoo.client.KazooClient(
962 hosts='%s:%s' % (self.zookeeper_host, self.zookeeper_port))
963 _tmp_client.start()
964
965 if _tmp_client.exists(self.zookeeper_chroot):
966 _tmp_client.delete(self.zookeeper_chroot, recursive=True)
967
968 _tmp_client.ensure_path(self.zookeeper_chroot)
969 _tmp_client.stop()
970 _tmp_client.close()
971
972 self.addCleanup(self._cleanup)
973
974 def _cleanup(self):
975 '''Remove the chroot path.'''
976 # Need a non-chroot'ed client to remove the chroot path
977 _tmp_client = kazoo.client.KazooClient(
978 hosts='%s:%s' % (self.zookeeper_host, self.zookeeper_port))
979 _tmp_client.start()
980 _tmp_client.delete(self.zookeeper_chroot, recursive=True)
981 _tmp_client.stop()
982
983
Maru Newby3fe5f852015-01-13 04:22:14 +0000984class BaseTestCase(testtools.TestCase):
Clark Boylanb640e052014-04-03 16:41:46 -0700985 log = logging.getLogger("zuul.test")
986
987 def setUp(self):
Maru Newby3fe5f852015-01-13 04:22:14 +0000988 super(BaseTestCase, self).setUp()
Clark Boylanb640e052014-04-03 16:41:46 -0700989 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
990 try:
991 test_timeout = int(test_timeout)
992 except ValueError:
993 # If timeout value is invalid do not set a timeout.
994 test_timeout = 0
995 if test_timeout > 0:
996 self.useFixture(fixtures.Timeout(test_timeout, gentle=False))
997
998 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
999 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
1000 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
1001 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
1002 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
1003 os.environ.get('OS_STDERR_CAPTURE') == '1'):
1004 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
1005 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
1006 if (os.environ.get('OS_LOG_CAPTURE') == 'True' or
1007 os.environ.get('OS_LOG_CAPTURE') == '1'):
James E. Blair79e94b62016-10-18 08:20:22 -07001008 log_level = logging.DEBUG
Jan Hrubanb4f9c612016-01-04 18:41:04 +01001009 if os.environ.get('OS_LOG_LEVEL') == 'DEBUG':
1010 log_level = logging.DEBUG
James E. Blair79e94b62016-10-18 08:20:22 -07001011 elif os.environ.get('OS_LOG_LEVEL') == 'INFO':
1012 log_level = logging.INFO
Jan Hrubanb4f9c612016-01-04 18:41:04 +01001013 elif os.environ.get('OS_LOG_LEVEL') == 'WARNING':
1014 log_level = logging.WARNING
1015 elif os.environ.get('OS_LOG_LEVEL') == 'ERROR':
1016 log_level = logging.ERROR
1017 elif os.environ.get('OS_LOG_LEVEL') == 'CRITICAL':
1018 log_level = logging.CRITICAL
Clark Boylanb640e052014-04-03 16:41:46 -07001019 self.useFixture(fixtures.FakeLogger(
Jan Hrubanb4f9c612016-01-04 18:41:04 +01001020 level=log_level,
Clark Boylanb640e052014-04-03 16:41:46 -07001021 format='%(asctime)s %(name)-32s '
1022 '%(levelname)-8s %(message)s'))
Maru Newby3fe5f852015-01-13 04:22:14 +00001023
James E. Blairdce6cea2016-12-20 16:45:32 -08001024 # NOTE(notmorgan): Extract logging overrides for specific libraries
1025 # from the OS_LOG_DEFAULTS env and create FakeLogger fixtures for
1026 # each. This is used to limit the output during test runs from
1027 # libraries that zuul depends on such as gear.
1028 log_defaults_from_env = os.environ.get(
1029 'OS_LOG_DEFAULTS',
1030 'git.cmd=INFO,kazoo.client=INFO')
Morgan Fainbergd34e0b42016-06-09 19:10:38 -07001031
James E. Blairdce6cea2016-12-20 16:45:32 -08001032 if log_defaults_from_env:
1033 for default in log_defaults_from_env.split(','):
1034 try:
1035 name, level_str = default.split('=', 1)
1036 level = getattr(logging, level_str, logging.DEBUG)
1037 self.useFixture(fixtures.FakeLogger(
1038 name=name,
1039 level=level,
1040 format='%(asctime)s %(name)-32s '
1041 '%(levelname)-8s %(message)s'))
1042 except ValueError:
1043 # NOTE(notmorgan): Invalid format of the log default,
1044 # skip and don't try and apply a logger for the
1045 # specified module
1046 pass
Morgan Fainbergd34e0b42016-06-09 19:10:38 -07001047
Maru Newby3fe5f852015-01-13 04:22:14 +00001048
1049class ZuulTestCase(BaseTestCase):
James E. Blaire7b99a02016-08-05 14:27:34 -07001050 """A test case with a functioning Zuul.
1051
1052 The following class variables are used during test setup and can
1053 be overidden by subclasses but are effectively read-only once a
1054 test method starts running:
1055
1056 :cvar str config_file: This points to the main zuul config file
1057 within the fixtures directory. Subclasses may override this
1058 to obtain a different behavior.
1059
1060 :cvar str tenant_config_file: This is the tenant config file
1061 (which specifies from what git repos the configuration should
1062 be loaded). It defaults to the value specified in
1063 `config_file` but can be overidden by subclasses to obtain a
1064 different tenant/project layout while using the standard main
1065 configuration.
1066
1067 The following are instance variables that are useful within test
1068 methods:
1069
1070 :ivar FakeGerritConnection fake_<connection>:
1071 A :py:class:`~tests.base.FakeGerritConnection` will be
1072 instantiated for each connection present in the config file
1073 and stored here. For instance, `fake_gerrit` will hold the
1074 FakeGerritConnection object for a connection named `gerrit`.
1075
1076 :ivar FakeGearmanServer gearman_server: An instance of
1077 :py:class:`~tests.base.FakeGearmanServer` which is the Gearman
1078 server that all of the Zuul components in this test use to
1079 communicate with each other.
1080
1081 :ivar RecordingLaunchServer launch_server: An instance of
1082 :py:class:`~tests.base.RecordingLaunchServer` which is the
1083 Ansible launch server used to run jobs for this test.
1084
1085 :ivar list builds: A list of :py:class:`~tests.base.FakeBuild` objects
1086 representing currently running builds. They are appended to
1087 the list in the order they are launched, and removed from this
1088 list upon completion.
1089
1090 :ivar list history: A list of :py:class:`~tests.base.BuildHistory`
1091 objects representing completed builds. They are appended to
1092 the list in the order they complete.
1093
1094 """
1095
James E. Blair83005782015-12-11 14:46:03 -08001096 config_file = 'zuul.conf'
James E. Blaire1767bc2016-08-02 10:00:27 -07001097 run_ansible = False
James E. Blair3f876d52016-07-22 13:07:14 -07001098
1099 def _startMerger(self):
1100 self.merge_server = zuul.merger.server.MergeServer(self.config,
1101 self.connections)
1102 self.merge_server.start()
1103
Maru Newby3fe5f852015-01-13 04:22:14 +00001104 def setUp(self):
1105 super(ZuulTestCase, self).setUp()
James E. Blair498059b2016-12-20 13:50:13 -08001106
1107 self.setupZK()
1108
James E. Blair97d902e2014-08-21 13:25:56 -07001109 if USE_TEMPDIR:
1110 tmp_root = self.useFixture(fixtures.TempDir(
Joshua Hesketh29d99b72014-08-19 16:27:42 +10001111 rootdir=os.environ.get("ZUUL_TEST_ROOT"))
1112 ).path
James E. Blair97d902e2014-08-21 13:25:56 -07001113 else:
1114 tmp_root = os.environ.get("ZUUL_TEST_ROOT")
Clark Boylanb640e052014-04-03 16:41:46 -07001115 self.test_root = os.path.join(tmp_root, "zuul-test")
1116 self.upstream_root = os.path.join(self.test_root, "upstream")
1117 self.git_root = os.path.join(self.test_root, "git")
James E. Blairce8a2132016-05-19 15:21:52 -07001118 self.state_root = os.path.join(self.test_root, "lib")
Clark Boylanb640e052014-04-03 16:41:46 -07001119
1120 if os.path.exists(self.test_root):
1121 shutil.rmtree(self.test_root)
1122 os.makedirs(self.test_root)
1123 os.makedirs(self.upstream_root)
James E. Blairce8a2132016-05-19 15:21:52 -07001124 os.makedirs(self.state_root)
Clark Boylanb640e052014-04-03 16:41:46 -07001125
1126 # Make per test copy of Configuration.
1127 self.setup_config()
James E. Blair59fdbac2015-12-07 17:08:06 -08001128 self.config.set('zuul', 'tenant_config',
Joshua Heskethacccffc2015-03-31 23:38:17 +11001129 os.path.join(FIXTURE_DIR,
James E. Blair59fdbac2015-12-07 17:08:06 -08001130 self.config.get('zuul', 'tenant_config')))
Clark Boylanb640e052014-04-03 16:41:46 -07001131 self.config.set('merger', 'git_dir', self.git_root)
James E. Blairce8a2132016-05-19 15:21:52 -07001132 self.config.set('zuul', 'state_dir', self.state_root)
Clark Boylanb640e052014-04-03 16:41:46 -07001133
1134 # For each project in config:
James E. Blair8b1dc3f2016-07-05 16:49:00 -07001135 # TODOv3(jeblair): remove these and replace with new git
1136 # filesystem fixtures
Clark Boylanb640e052014-04-03 16:41:46 -07001137 self.init_repo("org/project3")
James E. Blair97d902e2014-08-21 13:25:56 -07001138 self.init_repo("org/project4")
James E. Blairbce35e12014-08-21 14:31:17 -07001139 self.init_repo("org/project5")
1140 self.init_repo("org/project6")
Clark Boylanb640e052014-04-03 16:41:46 -07001141 self.init_repo("org/one-job-project")
1142 self.init_repo("org/nonvoting-project")
1143 self.init_repo("org/templated-project")
1144 self.init_repo("org/layered-project")
1145 self.init_repo("org/node-project")
1146 self.init_repo("org/conflict-project")
1147 self.init_repo("org/noop-project")
1148 self.init_repo("org/experimental-project")
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001149 self.init_repo("org/no-jobs-project")
Clark Boylanb640e052014-04-03 16:41:46 -07001150
1151 self.statsd = FakeStatsd()
Ian Wienandff977bf2015-09-30 15:38:47 +10001152 # note, use 127.0.0.1 rather than localhost to avoid getting ipv6
1153 # see: https://github.com/jsocol/pystatsd/issues/61
1154 os.environ['STATSD_HOST'] = '127.0.0.1'
Clark Boylanb640e052014-04-03 16:41:46 -07001155 os.environ['STATSD_PORT'] = str(self.statsd.port)
1156 self.statsd.start()
1157 # the statsd client object is configured in the statsd module import
Monty Taylor74fa3862016-06-02 07:39:49 +03001158 reload_module(statsd)
1159 reload_module(zuul.scheduler)
Clark Boylanb640e052014-04-03 16:41:46 -07001160
1161 self.gearman_server = FakeGearmanServer()
1162
1163 self.config.set('gearman', 'port', str(self.gearman_server.port))
1164
Joshua Hesketh352264b2015-08-11 23:42:08 +10001165 zuul.source.gerrit.GerritSource.replication_timeout = 1.5
1166 zuul.source.gerrit.GerritSource.replication_retry_interval = 0.5
1167 zuul.connection.gerrit.GerritEventConnector.delay = 0.0
Clark Boylanb640e052014-04-03 16:41:46 -07001168
Joshua Hesketh352264b2015-08-11 23:42:08 +10001169 self.sched = zuul.scheduler.Scheduler(self.config)
Clark Boylanb640e052014-04-03 16:41:46 -07001170
1171 self.useFixture(fixtures.MonkeyPatch('swiftclient.client.Connection',
1172 FakeSwiftClientConnection))
1173 self.swift = zuul.lib.swift.Swift(self.config)
1174
Jan Hruban6b71aff2015-10-22 16:58:08 +02001175 self.event_queues = [
1176 self.sched.result_event_queue,
1177 self.sched.trigger_event_queue
1178 ]
1179
James E. Blairfef78942016-03-11 16:28:56 -08001180 self.configure_connections()
Joshua Hesketh352264b2015-08-11 23:42:08 +10001181 self.sched.registerConnections(self.connections)
Joshua Hesketh352264b2015-08-11 23:42:08 +10001182
Clark Boylanb640e052014-04-03 16:41:46 -07001183 def URLOpenerFactory(*args, **kw):
Morgan Fainberg293f7f82016-05-30 14:01:22 -07001184 if isinstance(args[0], urllib.request.Request):
Clark Boylanb640e052014-04-03 16:41:46 -07001185 return old_urlopen(*args, **kw)
Clark Boylanb640e052014-04-03 16:41:46 -07001186 return FakeURLOpener(self.upstream_root, *args, **kw)
1187
Morgan Fainberg293f7f82016-05-30 14:01:22 -07001188 old_urlopen = urllib.request.urlopen
1189 urllib.request.urlopen = URLOpenerFactory
Clark Boylanb640e052014-04-03 16:41:46 -07001190
James E. Blair3f876d52016-07-22 13:07:14 -07001191 self._startMerger()
James E. Blair3f876d52016-07-22 13:07:14 -07001192
James E. Blaire1767bc2016-08-02 10:00:27 -07001193 self.launch_server = RecordingLaunchServer(
1194 self.config, self.connections, _run_ansible=self.run_ansible)
1195 self.launch_server.start()
1196 self.history = self.launch_server.build_history
1197 self.builds = self.launch_server.running_builds
1198
1199 self.launch_client = zuul.launcher.client.LaunchClient(
James E. Blair82938472016-01-11 14:38:13 -08001200 self.config, self.sched, self.swift)
Joshua Hesketh850ccb62014-11-27 11:31:02 +11001201 self.merge_client = zuul.merger.client.MergeClient(
1202 self.config, self.sched)
James E. Blair8d692392016-04-08 17:47:58 -07001203 self.nodepool = zuul.nodepool.Nodepool(self.sched)
James E. Blairdce6cea2016-12-20 16:45:32 -08001204 self.zk = zuul.zk.ZooKeeper()
1205 self.zk.connect([self.zk_config])
1206
1207 self.fake_nodepool = FakeNodepool(self.zk_config.host,
1208 self.zk_config.port,
1209 self.zk_config.chroot)
Clark Boylanb640e052014-04-03 16:41:46 -07001210
James E. Blaire1767bc2016-08-02 10:00:27 -07001211 self.sched.setLauncher(self.launch_client)
Clark Boylanb640e052014-04-03 16:41:46 -07001212 self.sched.setMerger(self.merge_client)
James E. Blair8d692392016-04-08 17:47:58 -07001213 self.sched.setNodepool(self.nodepool)
James E. Blairdce6cea2016-12-20 16:45:32 -08001214 self.sched.setZooKeeper(self.zk)
Clark Boylanb640e052014-04-03 16:41:46 -07001215
Paul Belanger88ef0ea2015-12-23 11:57:02 -05001216 self.webapp = zuul.webapp.WebApp(
1217 self.sched, port=0, listen_address='127.0.0.1')
Joshua Hesketh850ccb62014-11-27 11:31:02 +11001218 self.rpc = zuul.rpclistener.RPCListener(self.config, self.sched)
Clark Boylanb640e052014-04-03 16:41:46 -07001219
1220 self.sched.start()
1221 self.sched.reconfigure(self.config)
1222 self.sched.resume()
1223 self.webapp.start()
1224 self.rpc.start()
James E. Blaire1767bc2016-08-02 10:00:27 -07001225 self.launch_client.gearman.waitForServer()
Clark Boylanb640e052014-04-03 16:41:46 -07001226
1227 self.addCleanup(self.assertFinalState)
1228 self.addCleanup(self.shutdown)
1229
James E. Blairfef78942016-03-11 16:28:56 -08001230 def configure_connections(self):
Joshua Hesketh352264b2015-08-11 23:42:08 +10001231 # Register connections from the config
1232 self.smtp_messages = []
Joshua Hesketh850ccb62014-11-27 11:31:02 +11001233
Joshua Hesketh352264b2015-08-11 23:42:08 +10001234 def FakeSMTPFactory(*args, **kw):
1235 args = [self.smtp_messages] + list(args)
1236 return FakeSMTP(*args, **kw)
Joshua Hesketh850ccb62014-11-27 11:31:02 +11001237
Joshua Hesketh352264b2015-08-11 23:42:08 +10001238 self.useFixture(fixtures.MonkeyPatch('smtplib.SMTP', FakeSMTPFactory))
Joshua Hesketh850ccb62014-11-27 11:31:02 +11001239
Joshua Hesketh352264b2015-08-11 23:42:08 +10001240 # Set a changes database so multiple FakeGerrit's can report back to
1241 # a virtual canonical database given by the configured hostname
1242 self.gerrit_changes_dbs = {}
James E. Blairfef78942016-03-11 16:28:56 -08001243 self.connections = zuul.lib.connections.ConnectionRegistry()
Joshua Hesketh850ccb62014-11-27 11:31:02 +11001244
Joshua Hesketh352264b2015-08-11 23:42:08 +10001245 for section_name in self.config.sections():
1246 con_match = re.match(r'^connection ([\'\"]?)(.*)(\1)$',
1247 section_name, re.I)
1248 if not con_match:
1249 continue
1250 con_name = con_match.group(2)
1251 con_config = dict(self.config.items(section_name))
1252
1253 if 'driver' not in con_config:
1254 raise Exception("No driver specified for connection %s."
1255 % con_name)
1256
1257 con_driver = con_config['driver']
1258
1259 # TODO(jhesketh): load the required class automatically
1260 if con_driver == 'gerrit':
Joshua Heskethacccffc2015-03-31 23:38:17 +11001261 if con_config['server'] not in self.gerrit_changes_dbs.keys():
1262 self.gerrit_changes_dbs[con_config['server']] = {}
James E. Blair83005782015-12-11 14:46:03 -08001263 self.connections.connections[con_name] = FakeGerritConnection(
Joshua Hesketh352264b2015-08-11 23:42:08 +10001264 con_name, con_config,
Joshua Heskethacccffc2015-03-31 23:38:17 +11001265 changes_db=self.gerrit_changes_dbs[con_config['server']],
Jan Hruban6b71aff2015-10-22 16:58:08 +02001266 upstream_root=self.upstream_root
Joshua Hesketh352264b2015-08-11 23:42:08 +10001267 )
James E. Blair7fc8daa2016-08-08 15:37:15 -07001268 self.event_queues.append(
1269 self.connections.connections[con_name].event_queue)
James E. Blair83005782015-12-11 14:46:03 -08001270 setattr(self, 'fake_' + con_name,
1271 self.connections.connections[con_name])
Joshua Hesketh352264b2015-08-11 23:42:08 +10001272 elif con_driver == 'smtp':
James E. Blair83005782015-12-11 14:46:03 -08001273 self.connections.connections[con_name] = \
Joshua Hesketh352264b2015-08-11 23:42:08 +10001274 zuul.connection.smtp.SMTPConnection(con_name, con_config)
1275 else:
1276 raise Exception("Unknown driver, %s, for connection %s"
1277 % (con_config['driver'], con_name))
1278
1279 # If the [gerrit] or [smtp] sections still exist, load them in as a
1280 # connection named 'gerrit' or 'smtp' respectfully
1281
1282 if 'gerrit' in self.config.sections():
1283 self.gerrit_changes_dbs['gerrit'] = {}
James E. Blair7fc8daa2016-08-08 15:37:15 -07001284 self.event_queues.append(
1285 self.connections.connections[con_name].event_queue)
James E. Blair83005782015-12-11 14:46:03 -08001286 self.connections.connections['gerrit'] = FakeGerritConnection(
Joshua Hesketh352264b2015-08-11 23:42:08 +10001287 '_legacy_gerrit', dict(self.config.items('gerrit')),
James E. Blair7fc8daa2016-08-08 15:37:15 -07001288 changes_db=self.gerrit_changes_dbs['gerrit'])
Joshua Hesketh352264b2015-08-11 23:42:08 +10001289
1290 if 'smtp' in self.config.sections():
James E. Blair83005782015-12-11 14:46:03 -08001291 self.connections.connections['smtp'] = \
Joshua Hesketh352264b2015-08-11 23:42:08 +10001292 zuul.connection.smtp.SMTPConnection(
1293 '_legacy_smtp', dict(self.config.items('smtp')))
Joshua Hesketh850ccb62014-11-27 11:31:02 +11001294
James E. Blair83005782015-12-11 14:46:03 -08001295 def setup_config(self):
James E. Blaire7b99a02016-08-05 14:27:34 -07001296 # This creates the per-test configuration object. It can be
1297 # overriden by subclasses, but should not need to be since it
1298 # obeys the config_file and tenant_config_file attributes.
Clark Boylanb640e052014-04-03 16:41:46 -07001299 self.config = ConfigParser.ConfigParser()
James E. Blair83005782015-12-11 14:46:03 -08001300 self.config.read(os.path.join(FIXTURE_DIR, self.config_file))
James E. Blair2a629ec2015-12-22 15:32:02 -08001301 if hasattr(self, 'tenant_config_file'):
1302 self.config.set('zuul', 'tenant_config', self.tenant_config_file)
James E. Blair96c6bf82016-01-15 16:20:40 -08001303 git_path = os.path.join(
1304 os.path.dirname(
1305 os.path.join(FIXTURE_DIR, self.tenant_config_file)),
1306 'git')
1307 if os.path.exists(git_path):
1308 for reponame in os.listdir(git_path):
James E. Blair8b1dc3f2016-07-05 16:49:00 -07001309 project = reponame.replace('_', '/')
1310 self.copyDirToRepo(project,
James E. Blair96c6bf82016-01-15 16:20:40 -08001311 os.path.join(git_path, reponame))
1312
James E. Blair498059b2016-12-20 13:50:13 -08001313 def setupZK(self):
1314 self.zk_chroot_fixture = self.useFixture(ChrootedKazooFixture())
James E. Blairdce6cea2016-12-20 16:45:32 -08001315 self.zk_config = zuul.zk.ZooKeeperConnectionConfig(
1316 self.zk_chroot_fixture.zookeeper_host,
1317 self.zk_chroot_fixture.zookeeper_port,
1318 self.zk_chroot_fixture.zookeeper_chroot)
James E. Blair498059b2016-12-20 13:50:13 -08001319
James E. Blair96c6bf82016-01-15 16:20:40 -08001320 def copyDirToRepo(self, project, source_path):
James E. Blair8b1dc3f2016-07-05 16:49:00 -07001321 self.init_repo(project)
James E. Blair96c6bf82016-01-15 16:20:40 -08001322
1323 files = {}
1324 for (dirpath, dirnames, filenames) in os.walk(source_path):
1325 for filename in filenames:
1326 test_tree_filepath = os.path.join(dirpath, filename)
1327 common_path = os.path.commonprefix([test_tree_filepath,
1328 source_path])
1329 relative_filepath = test_tree_filepath[len(common_path) + 1:]
1330 with open(test_tree_filepath, 'r') as f:
1331 content = f.read()
1332 files[relative_filepath] = content
1333 self.addCommitToRepo(project, 'add content from fixture',
James E. Blair8b1dc3f2016-07-05 16:49:00 -07001334 files, branch='master', tag='init')
James E. Blair83005782015-12-11 14:46:03 -08001335
Clark Boylanb640e052014-04-03 16:41:46 -07001336 def assertFinalState(self):
Clark Boylanb640e052014-04-03 16:41:46 -07001337 # Make sure that git.Repo objects have been garbage collected.
1338 repos = []
1339 gc.collect()
1340 for obj in gc.get_objects():
1341 if isinstance(obj, git.Repo):
1342 repos.append(obj)
1343 self.assertEqual(len(repos), 0)
1344 self.assertEmptyQueues()
James E. Blair83005782015-12-11 14:46:03 -08001345 ipm = zuul.manager.independent.IndependentPipelineManager
James E. Blair59fdbac2015-12-07 17:08:06 -08001346 for tenant in self.sched.abide.tenants.values():
1347 for pipeline in tenant.layout.pipelines.values():
James E. Blair83005782015-12-11 14:46:03 -08001348 if isinstance(pipeline.manager, ipm):
James E. Blair59fdbac2015-12-07 17:08:06 -08001349 self.assertEqual(len(pipeline.queues), 0)
Clark Boylanb640e052014-04-03 16:41:46 -07001350
1351 def shutdown(self):
1352 self.log.debug("Shutting down after tests")
James E. Blaire1767bc2016-08-02 10:00:27 -07001353 self.launch_client.stop()
James E. Blair3f876d52016-07-22 13:07:14 -07001354 self.merge_server.stop()
1355 self.merge_server.join()
Clark Boylanb640e052014-04-03 16:41:46 -07001356 self.merge_client.stop()
James E. Blaire1767bc2016-08-02 10:00:27 -07001357 self.launch_server.stop()
Clark Boylanb640e052014-04-03 16:41:46 -07001358 self.sched.stop()
1359 self.sched.join()
1360 self.statsd.stop()
1361 self.statsd.join()
1362 self.webapp.stop()
1363 self.webapp.join()
1364 self.rpc.stop()
1365 self.rpc.join()
1366 self.gearman_server.shutdown()
James E. Blairdce6cea2016-12-20 16:45:32 -08001367 self.fake_nodepool.stop()
1368 self.zk.disconnect()
Clark Boylanb640e052014-04-03 16:41:46 -07001369 threads = threading.enumerate()
1370 if len(threads) > 1:
1371 self.log.error("More than one thread is running: %s" % threads)
James E. Blair6ac368c2016-12-22 18:07:20 -08001372 self.printHistory()
Clark Boylanb640e052014-04-03 16:41:46 -07001373
1374 def init_repo(self, project):
1375 parts = project.split('/')
1376 path = os.path.join(self.upstream_root, *parts[:-1])
1377 if not os.path.exists(path):
1378 os.makedirs(path)
1379 path = os.path.join(self.upstream_root, project)
1380 repo = git.Repo.init(path)
1381
Morgan Fainberg78c301a2016-07-14 13:47:01 -07001382 with repo.config_writer() as config_writer:
1383 config_writer.set_value('user', 'email', 'user@example.com')
1384 config_writer.set_value('user', 'name', 'User Name')
Clark Boylanb640e052014-04-03 16:41:46 -07001385
Clark Boylanb640e052014-04-03 16:41:46 -07001386 repo.index.commit('initial commit')
1387 master = repo.create_head('master')
Clark Boylanb640e052014-04-03 16:41:46 -07001388
James E. Blair97d902e2014-08-21 13:25:56 -07001389 repo.head.reference = master
James E. Blair879dafb2015-07-17 14:04:49 -07001390 zuul.merger.merger.reset_repo_to_head(repo)
James E. Blair97d902e2014-08-21 13:25:56 -07001391 repo.git.clean('-x', '-f', '-d')
1392
James E. Blair97d902e2014-08-21 13:25:56 -07001393 def create_branch(self, project, branch):
1394 path = os.path.join(self.upstream_root, project)
1395 repo = git.Repo.init(path)
1396 fn = os.path.join(path, 'README')
1397
1398 branch_head = repo.create_head(branch)
1399 repo.head.reference = branch_head
Clark Boylanb640e052014-04-03 16:41:46 -07001400 f = open(fn, 'a')
James E. Blair97d902e2014-08-21 13:25:56 -07001401 f.write("test %s\n" % branch)
Clark Boylanb640e052014-04-03 16:41:46 -07001402 f.close()
1403 repo.index.add([fn])
James E. Blair97d902e2014-08-21 13:25:56 -07001404 repo.index.commit('%s commit' % branch)
Clark Boylanb640e052014-04-03 16:41:46 -07001405
James E. Blair97d902e2014-08-21 13:25:56 -07001406 repo.head.reference = repo.heads['master']
James E. Blair879dafb2015-07-17 14:04:49 -07001407 zuul.merger.merger.reset_repo_to_head(repo)
Clark Boylanb640e052014-04-03 16:41:46 -07001408 repo.git.clean('-x', '-f', '-d')
1409
Sachi King9f16d522016-03-16 12:20:45 +11001410 def create_commit(self, project):
1411 path = os.path.join(self.upstream_root, project)
1412 repo = git.Repo(path)
1413 repo.head.reference = repo.heads['master']
1414 file_name = os.path.join(path, 'README')
1415 with open(file_name, 'a') as f:
1416 f.write('creating fake commit\n')
1417 repo.index.add([file_name])
1418 commit = repo.index.commit('Creating a fake commit')
1419 return commit.hexsha
1420
James E. Blairb8c16472015-05-05 14:55:26 -07001421 def orderedRelease(self):
1422 # Run one build at a time to ensure non-race order:
1423 while len(self.builds):
1424 self.release(self.builds[0])
1425 self.waitUntilSettled()
1426
Clark Boylanb640e052014-04-03 16:41:46 -07001427 def release(self, job):
1428 if isinstance(job, FakeBuild):
1429 job.release()
1430 else:
1431 job.waiting = False
1432 self.log.debug("Queued job %s released" % job.unique)
1433 self.gearman_server.wakeConnections()
1434
1435 def getParameter(self, job, name):
1436 if isinstance(job, FakeBuild):
1437 return job.parameters[name]
1438 else:
1439 parameters = json.loads(job.arguments)
1440 return parameters[name]
1441
Clark Boylanb640e052014-04-03 16:41:46 -07001442 def haveAllBuildsReported(self):
1443 # See if Zuul is waiting on a meta job to complete
James E. Blaire1767bc2016-08-02 10:00:27 -07001444 if self.launch_client.meta_jobs:
Clark Boylanb640e052014-04-03 16:41:46 -07001445 return False
1446 # Find out if every build that the worker has completed has been
1447 # reported back to Zuul. If it hasn't then that means a Gearman
1448 # event is still in transit and the system is not stable.
James E. Blair3f876d52016-07-22 13:07:14 -07001449 for build in self.history:
James E. Blaire1767bc2016-08-02 10:00:27 -07001450 zbuild = self.launch_client.builds.get(build.uuid)
Clark Boylanb640e052014-04-03 16:41:46 -07001451 if not zbuild:
1452 # It has already been reported
1453 continue
1454 # It hasn't been reported yet.
1455 return False
1456 # Make sure that none of the worker connections are in GRAB_WAIT
James E. Blaire1767bc2016-08-02 10:00:27 -07001457 for connection in self.launch_server.worker.active_connections:
Clark Boylanb640e052014-04-03 16:41:46 -07001458 if connection.state == 'GRAB_WAIT':
1459 return False
1460 return True
1461
1462 def areAllBuildsWaiting(self):
James E. Blaire1767bc2016-08-02 10:00:27 -07001463 builds = self.launch_client.builds.values()
Clark Boylanb640e052014-04-03 16:41:46 -07001464 for build in builds:
1465 client_job = None
James E. Blaire1767bc2016-08-02 10:00:27 -07001466 for conn in self.launch_client.gearman.active_connections:
Clark Boylanb640e052014-04-03 16:41:46 -07001467 for j in conn.related_jobs.values():
1468 if j.unique == build.uuid:
1469 client_job = j
1470 break
1471 if not client_job:
1472 self.log.debug("%s is not known to the gearman client" %
1473 build)
James E. Blairf15139b2015-04-02 16:37:15 -07001474 return False
Clark Boylanb640e052014-04-03 16:41:46 -07001475 if not client_job.handle:
1476 self.log.debug("%s has no handle" % client_job)
James E. Blairf15139b2015-04-02 16:37:15 -07001477 return False
Clark Boylanb640e052014-04-03 16:41:46 -07001478 server_job = self.gearman_server.jobs.get(client_job.handle)
1479 if not server_job:
1480 self.log.debug("%s is not known to the gearman server" %
1481 client_job)
James E. Blairf15139b2015-04-02 16:37:15 -07001482 return False
Clark Boylanb640e052014-04-03 16:41:46 -07001483 if not hasattr(server_job, 'waiting'):
1484 self.log.debug("%s is being enqueued" % server_job)
James E. Blairf15139b2015-04-02 16:37:15 -07001485 return False
Clark Boylanb640e052014-04-03 16:41:46 -07001486 if server_job.waiting:
1487 continue
James E. Blair17302972016-08-10 16:11:42 -07001488 if build.url is None:
James E. Blairbbda4702016-03-09 15:19:56 -08001489 self.log.debug("%s has not reported start" % build)
1490 return False
James E. Blairab7132b2016-08-05 12:36:22 -07001491 worker_build = self.launch_server.job_builds.get(server_job.unique)
James E. Blair962220f2016-08-03 11:22:38 -07001492 if worker_build:
1493 if worker_build.isWaiting():
1494 continue
1495 else:
1496 self.log.debug("%s is running" % worker_build)
1497 return False
Clark Boylanb640e052014-04-03 16:41:46 -07001498 else:
James E. Blair962220f2016-08-03 11:22:38 -07001499 self.log.debug("%s is unassigned" % server_job)
James E. Blairf15139b2015-04-02 16:37:15 -07001500 return False
1501 return True
Clark Boylanb640e052014-04-03 16:41:46 -07001502
James E. Blairdce6cea2016-12-20 16:45:32 -08001503 def areAllNodeRequestsComplete(self):
1504 if self.sched.nodepool.requests:
1505 return False
1506 return True
1507
Jan Hruban6b71aff2015-10-22 16:58:08 +02001508 def eventQueuesEmpty(self):
1509 for queue in self.event_queues:
1510 yield queue.empty()
1511
1512 def eventQueuesJoin(self):
1513 for queue in self.event_queues:
1514 queue.join()
1515
Clark Boylanb640e052014-04-03 16:41:46 -07001516 def waitUntilSettled(self):
1517 self.log.debug("Waiting until settled...")
1518 start = time.time()
1519 while True:
1520 if time.time() - start > 10:
James E. Blair10fc1eb2016-12-21 16:16:25 -08001521 self.log.error("Timeout waiting for Zuul to settle")
1522 self.log.error("Queue status:")
James E. Blair622c9682016-06-09 08:14:53 -07001523 for queue in self.event_queues:
James E. Blair10fc1eb2016-12-21 16:16:25 -08001524 self.log.error(" %s: %s" % (queue, queue.empty()))
1525 self.log.error("All builds waiting: %s" %
James E. Blair622c9682016-06-09 08:14:53 -07001526 (self.areAllBuildsWaiting(),))
James E. Blair10fc1eb2016-12-21 16:16:25 -08001527 self.log.error("All builds reported: %s" %
James E. Blairf3156c92016-08-10 15:32:19 -07001528 (self.haveAllBuildsReported(),))
James E. Blair10fc1eb2016-12-21 16:16:25 -08001529 self.log.error("All requests completed: %s" %
1530 (self.areAllNodeRequestsComplete(),))
1531 self.log.error("Merge client jobs: %s" %
1532 (self.merge_client.jobs,))
Clark Boylanb640e052014-04-03 16:41:46 -07001533 raise Exception("Timeout waiting for Zuul to settle")
1534 # Make sure no new events show up while we're checking
James E. Blair3f876d52016-07-22 13:07:14 -07001535
James E. Blaire1767bc2016-08-02 10:00:27 -07001536 self.launch_server.lock.acquire()
Clark Boylanb640e052014-04-03 16:41:46 -07001537 # have all build states propogated to zuul?
1538 if self.haveAllBuildsReported():
1539 # Join ensures that the queue is empty _and_ events have been
1540 # processed
Jan Hruban6b71aff2015-10-22 16:58:08 +02001541 self.eventQueuesJoin()
Clark Boylanb640e052014-04-03 16:41:46 -07001542 self.sched.run_handler_lock.acquire()
James E. Blair14abdf42015-12-09 16:11:53 -08001543 if (not self.merge_client.jobs and
Jan Hruban6b71aff2015-10-22 16:58:08 +02001544 all(self.eventQueuesEmpty()) and
Clark Boylanb640e052014-04-03 16:41:46 -07001545 self.haveAllBuildsReported() and
James E. Blairdce6cea2016-12-20 16:45:32 -08001546 self.areAllBuildsWaiting() and
1547 self.areAllNodeRequestsComplete()):
Clark Boylanb640e052014-04-03 16:41:46 -07001548 self.sched.run_handler_lock.release()
James E. Blaire1767bc2016-08-02 10:00:27 -07001549 self.launch_server.lock.release()
Clark Boylanb640e052014-04-03 16:41:46 -07001550 self.log.debug("...settled.")
1551 return
1552 self.sched.run_handler_lock.release()
James E. Blaire1767bc2016-08-02 10:00:27 -07001553 self.launch_server.lock.release()
Clark Boylanb640e052014-04-03 16:41:46 -07001554 self.sched.wake_event.wait(0.1)
1555
1556 def countJobResults(self, jobs, result):
1557 jobs = filter(lambda x: x.result == result, jobs)
1558 return len(jobs)
1559
James E. Blair96c6bf82016-01-15 16:20:40 -08001560 def getJobFromHistory(self, name, project=None):
James E. Blair3f876d52016-07-22 13:07:14 -07001561 for job in self.history:
1562 if (job.name == name and
1563 (project is None or
1564 job.parameters['ZUUL_PROJECT'] == project)):
1565 return job
Clark Boylanb640e052014-04-03 16:41:46 -07001566 raise Exception("Unable to find job %s in history" % name)
1567
1568 def assertEmptyQueues(self):
1569 # Make sure there are no orphaned jobs
James E. Blair59fdbac2015-12-07 17:08:06 -08001570 for tenant in self.sched.abide.tenants.values():
1571 for pipeline in tenant.layout.pipelines.values():
1572 for queue in pipeline.queues:
1573 if len(queue.queue) != 0:
Joshua Hesketh0aa7e8b2016-07-14 00:12:25 +10001574 print('pipeline %s queue %s contents %s' % (
1575 pipeline.name, queue.name, queue.queue))
James E. Blair59fdbac2015-12-07 17:08:06 -08001576 self.assertEqual(len(queue.queue), 0,
1577 "Pipelines queues should be empty")
Clark Boylanb640e052014-04-03 16:41:46 -07001578
1579 def assertReportedStat(self, key, value=None, kind=None):
1580 start = time.time()
1581 while time.time() < (start + 5):
1582 for stat in self.statsd.stats:
1583 pprint.pprint(self.statsd.stats)
1584 k, v = stat.split(':')
1585 if key == k:
1586 if value is None and kind is None:
1587 return
1588 elif value:
1589 if value == v:
1590 return
1591 elif kind:
1592 if v.endswith('|' + kind):
1593 return
1594 time.sleep(0.1)
1595
1596 pprint.pprint(self.statsd.stats)
1597 raise Exception("Key %s not found in reported stats" % key)
James E. Blair59fdbac2015-12-07 17:08:06 -08001598
James E. Blair2b2a8ab2016-08-11 14:39:11 -07001599 def assertBuilds(self, builds):
1600 """Assert that the running builds are as described.
1601
1602 The list of running builds is examined and must match exactly
1603 the list of builds described by the input.
1604
1605 :arg list builds: A list of dictionaries. Each item in the
1606 list must match the corresponding build in the build
1607 history, and each element of the dictionary must match the
1608 corresponding attribute of the build.
1609
1610 """
James E. Blair3158e282016-08-19 09:34:11 -07001611 try:
1612 self.assertEqual(len(self.builds), len(builds))
1613 for i, d in enumerate(builds):
1614 for k, v in d.items():
1615 self.assertEqual(
1616 getattr(self.builds[i], k), v,
1617 "Element %i in builds does not match" % (i,))
1618 except Exception:
1619 for build in self.builds:
1620 self.log.error("Running build: %s" % build)
1621 else:
1622 self.log.error("No running builds")
1623 raise
James E. Blair2b2a8ab2016-08-11 14:39:11 -07001624
James E. Blairb536ecc2016-08-31 10:11:42 -07001625 def assertHistory(self, history, ordered=True):
James E. Blair2b2a8ab2016-08-11 14:39:11 -07001626 """Assert that the completed builds are as described.
1627
1628 The list of completed builds is examined and must match
1629 exactly the list of builds described by the input.
1630
1631 :arg list history: A list of dictionaries. Each item in the
1632 list must match the corresponding build in the build
1633 history, and each element of the dictionary must match the
1634 corresponding attribute of the build.
1635
James E. Blairb536ecc2016-08-31 10:11:42 -07001636 :arg bool ordered: If true, the history must match the order
1637 supplied, if false, the builds are permitted to have
1638 arrived in any order.
1639
James E. Blair2b2a8ab2016-08-11 14:39:11 -07001640 """
James E. Blairb536ecc2016-08-31 10:11:42 -07001641 def matches(history_item, item):
1642 for k, v in item.items():
1643 if getattr(history_item, k) != v:
1644 return False
1645 return True
James E. Blair3158e282016-08-19 09:34:11 -07001646 try:
1647 self.assertEqual(len(self.history), len(history))
James E. Blairb536ecc2016-08-31 10:11:42 -07001648 if ordered:
1649 for i, d in enumerate(history):
1650 if not matches(self.history[i], d):
1651 raise Exception(
1652 "Element %i in history does not match" % (i,))
1653 else:
1654 unseen = self.history[:]
1655 for i, d in enumerate(history):
1656 found = False
1657 for unseen_item in unseen:
1658 if matches(unseen_item, d):
1659 found = True
1660 unseen.remove(unseen_item)
1661 break
1662 if not found:
1663 raise Exception("No match found for element %i "
1664 "in history" % (i,))
1665 if unseen:
1666 raise Exception("Unexpected items in history")
James E. Blair3158e282016-08-19 09:34:11 -07001667 except Exception:
1668 for build in self.history:
1669 self.log.error("Completed build: %s" % build)
1670 else:
1671 self.log.error("No completed builds")
1672 raise
James E. Blair2b2a8ab2016-08-11 14:39:11 -07001673
James E. Blair6ac368c2016-12-22 18:07:20 -08001674 def printHistory(self):
1675 """Log the build history.
1676
1677 This can be useful during tests to summarize what jobs have
1678 completed.
1679
1680 """
1681 self.log.debug("Build history:")
1682 for build in self.history:
1683 self.log.debug(build)
1684
James E. Blair59fdbac2015-12-07 17:08:06 -08001685 def getPipeline(self, name):
James E. Blairf84026c2015-12-08 16:11:46 -08001686 return self.sched.abide.tenants.values()[0].layout.pipelines.get(name)
1687
1688 def updateConfigLayout(self, path):
1689 root = os.path.join(self.test_root, "config")
1690 os.makedirs(root)
1691 f = tempfile.NamedTemporaryFile(dir=root, delete=False)
1692 f.write("""
Paul Belanger66e95962016-11-11 12:11:06 -05001693- tenant:
1694 name: openstack
1695 source:
1696 gerrit:
1697 config-repos:
1698 - %s
1699 """ % path)
James E. Blairf84026c2015-12-08 16:11:46 -08001700 f.close()
Paul Belanger66e95962016-11-11 12:11:06 -05001701 self.config.set('zuul', 'tenant_config',
1702 os.path.join(FIXTURE_DIR, f.name))
James E. Blair14abdf42015-12-09 16:11:53 -08001703
James E. Blair8b1dc3f2016-07-05 16:49:00 -07001704 def addCommitToRepo(self, project, message, files,
1705 branch='master', tag=None):
James E. Blair14abdf42015-12-09 16:11:53 -08001706 path = os.path.join(self.upstream_root, project)
1707 repo = git.Repo(path)
1708 repo.head.reference = branch
1709 zuul.merger.merger.reset_repo_to_head(repo)
1710 for fn, content in files.items():
1711 fn = os.path.join(path, fn)
1712 with open(fn, 'w') as f:
1713 f.write(content)
1714 repo.index.add([fn])
1715 commit = repo.index.commit(message)
1716 repo.heads[branch].commit = commit
1717 repo.head.reference = branch
1718 repo.git.clean('-x', '-f', '-d')
1719 repo.heads[branch].checkout()
James E. Blair8b1dc3f2016-07-05 16:49:00 -07001720 if tag:
1721 repo.create_tag(tag)
James E. Blair3f876d52016-07-22 13:07:14 -07001722
James E. Blair7fc8daa2016-08-08 15:37:15 -07001723 def addEvent(self, connection, event):
1724 """Inject a Fake (Gerrit) event.
1725
1726 This method accepts a JSON-encoded event and simulates Zuul
1727 having received it from Gerrit. It could (and should)
1728 eventually apply to any connection type, but is currently only
1729 used with Gerrit connections. The name of the connection is
1730 used to look up the corresponding server, and the event is
1731 simulated as having been received by all Zuul connections
1732 attached to that server. So if two Gerrit connections in Zuul
1733 are connected to the same Gerrit server, and you invoke this
1734 method specifying the name of one of them, the event will be
1735 received by both.
1736
1737 .. note::
1738
1739 "self.fake_gerrit.addEvent" calls should be migrated to
1740 this method.
1741
1742 :arg str connection: The name of the connection corresponding
1743 to the gerrit server.
1744 :arg str event: The JSON-encoded event.
1745
1746 """
1747 specified_conn = self.connections.connections[connection]
1748 for conn in self.connections.connections.values():
1749 if (isinstance(conn, specified_conn.__class__) and
1750 specified_conn.server == conn.server):
1751 conn.addEvent(event)
1752
James E. Blair3f876d52016-07-22 13:07:14 -07001753
1754class AnsibleZuulTestCase(ZuulTestCase):
1755 """ZuulTestCase but with an actual ansible launcher running"""
James E. Blaire1767bc2016-08-02 10:00:27 -07001756 run_ansible = True