Joshua Hesketh | 66f9f60 | 2013-08-14 11:28:10 +1000 | [diff] [blame] | 1 | #!/usr/bin/python2 |
| 2 | # |
| 3 | # Copyright 2013 Rackspace Australia |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
Joshua Hesketh | 96adb28 | 2014-03-25 16:26:45 +1100 | [diff] [blame] | 17 | import gear |
| 18 | import json |
| 19 | import time |
| 20 | import uuid |
| 21 | |
Joshua Hesketh | e095c8c | 2014-01-16 19:19:34 +1100 | [diff] [blame] | 22 | |
| 23 | class FakeJob(object): |
| 24 | def __init__(self): |
| 25 | pass |
| 26 | |
| 27 | def sendWorkStatus(self, *args, **kwargs): |
| 28 | pass |
Joshua Hesketh | 96adb28 | 2014-03-25 16:26:45 +1100 | [diff] [blame] | 29 | |
| 30 | |
| 31 | class FakeZuul(object): |
| 32 | """A fake zuul/gearman client to request work from gearman and check |
| 33 | results""" |
| 34 | def __init__(self, server, port): |
| 35 | self.gearman = gear.Client('FakeZuul') |
| 36 | self.gearman.addServer(server, port) |
| 37 | self.gearman.waitForServer() |
| 38 | self.job = None |
| 39 | |
| 40 | def make_zuul_data(self, data={}): |
Joshua Hesketh | 96052bf | 2014-04-05 19:48:06 +1100 | [diff] [blame] | 41 | job_uuid = str(uuid.uuid1()) |
Joshua Hesketh | 96adb28 | 2014-03-25 16:26:45 +1100 | [diff] [blame] | 42 | defaults = { |
Joshua Hesketh | 96052bf | 2014-04-05 19:48:06 +1100 | [diff] [blame] | 43 | 'ZUUL_UUID': job_uuid, |
Joshua Hesketh | 96adb28 | 2014-03-25 16:26:45 +1100 | [diff] [blame] | 44 | 'ZUUL_REF': 'a', |
| 45 | 'ZUUL_COMMIT': 'a', |
| 46 | 'ZUUL_PROJECT': 'a', |
| 47 | 'ZUUL_PIPELINE': 'a', |
| 48 | 'ZUUL_URL': 'http://localhost', |
| 49 | 'BASE_LOG_PATH': '56/123456/8', |
Joshua Hesketh | 96052bf | 2014-04-05 19:48:06 +1100 | [diff] [blame] | 50 | 'LOG_PATH': '56/123456/8/check/job_name/%s' % job_uuid |
Joshua Hesketh | 96adb28 | 2014-03-25 16:26:45 +1100 | [diff] [blame] | 51 | } |
| 52 | defaults.update(data) |
| 53 | return defaults |
| 54 | |
| 55 | def submit_job(self, name, data): |
| 56 | if not self.job: |
| 57 | self.job = gear.Job(name, |
| 58 | json.dumps(data), |
| 59 | unique=str(time.time())) |
| 60 | self.gearman.submitJob(self.job) |
| 61 | else: |
| 62 | raise Exception('A job already exists in self.job') |
| 63 | |
| 64 | return self.job |
| 65 | |
| 66 | def wait_for_completion(self): |
| 67 | if self.job: |
| 68 | while not self.job.complete: |
| 69 | time.sleep(0.1) |