Joshua Hesketh | e185911 | 2014-04-05 17:00:04 +1100 | [diff] [blame] | 1 | # Copyright 2014 Rackspace Australia |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | import fixtures |
| 16 | import gear |
| 17 | import logging |
| 18 | import os |
| 19 | import testtools |
| 20 | import time |
| 21 | import yaml |
| 22 | |
| 23 | import turbo_hipster.worker_server |
| 24 | |
| 25 | logging.basicConfig(level=logging.DEBUG, |
| 26 | format='%(asctime)s %(name)-32s ' |
| 27 | '%(levelname)-8s %(message)s') |
| 28 | |
| 29 | |
| 30 | class TestWithGearman(testtools.TestCase): |
| 31 | |
| 32 | log = logging.getLogger("TestWithGearman") |
| 33 | |
| 34 | def setUp(self): |
| 35 | super(TestWithGearman, self).setUp() |
| 36 | self.config = None |
| 37 | self.worker_server = None |
| 38 | self.gearman_server = gear.Server(0) |
| 39 | |
| 40 | def start_server(self): |
| 41 | if not self.config: |
| 42 | self._load_config_fixture() |
| 43 | # Grab the port so the clients can connect to it |
| 44 | self.config['zuul_server']['gearman_port'] = self.gearman_server.port |
| 45 | |
| 46 | self.worker_server = turbo_hipster.worker_server.Server(self.config) |
| 47 | self.worker_server.setup_logging() |
| 48 | self.worker_server.start() |
| 49 | t0 = time.time() |
| 50 | while time.time() - t0 < 10: |
| 51 | if self.worker_server.services_started: |
| 52 | break |
| 53 | time.sleep(0.01) |
| 54 | if not self.worker_server.services_started: |
| 55 | self.fail("Failed to start worker_service services") |
| 56 | |
| 57 | def tearDown(self): |
| 58 | if self.worker_server and not self.worker_server.stopped(): |
| 59 | self.worker_server.shutdown() |
| 60 | self.gearman_server.shutdown() |
| 61 | super(TestWithGearman, self).tearDown() |
| 62 | |
| 63 | def _load_config_fixture(self, config_name='default-config.yaml'): |
| 64 | config_dir = os.path.join(os.path.dirname(__file__), 'etc') |
| 65 | with open(os.path.join(config_dir, config_name), 'r') as config_stream: |
| 66 | self.config = yaml.safe_load(config_stream) |
| 67 | |
| 68 | # Set all of the working dirs etc to a writeable temp dir |
Joshua Hesketh | d5d7a21 | 2014-10-29 17:42:59 +1100 | [diff] [blame] | 69 | self.temp_path = self.useFixture(fixtures.TempDir()).path |
Joshua Hesketh | e185911 | 2014-04-05 17:00:04 +1100 | [diff] [blame] | 70 | for config_dir in ['debug_log', 'jobs_working_dir', 'git_working_dir', |
| 71 | 'pip_download_cache']: |
| 72 | if config_dir in self.config: |
| 73 | if self.config[config_dir][0] == '/': |
| 74 | self.config[config_dir] = self.config[config_dir][1:] |
Joshua Hesketh | d5d7a21 | 2014-10-29 17:42:59 +1100 | [diff] [blame] | 75 | self.config[config_dir] = os.path.join(self.temp_path, |
Joshua Hesketh | e185911 | 2014-04-05 17:00:04 +1100 | [diff] [blame] | 76 | self.config[config_dir]) |
| 77 | if self.config['publish_logs']['type'] == 'local': |
| 78 | if self.config['publish_logs']['path'][0] == '/': |
| 79 | self.config['publish_logs']['path'] = \ |
| 80 | self.config['publish_logs']['path'][1:] |
| 81 | self.config['publish_logs']['path'] = os.path.join( |
Joshua Hesketh | d5d7a21 | 2014-10-29 17:42:59 +1100 | [diff] [blame] | 82 | self.temp_path, self.config['publish_logs']['path']) |
| 83 | |
| 84 | if not os.path.isdir( |
| 85 | os.path.dirname(self.config['publish_logs']['path'])): |
| 86 | os.makedirs(os.path.dirname(self.config['publish_logs']['path'])) |