diff --git a/tests/base.py b/tests/base.py new file mode 100644 index 0000000..a79b535 --- /dev/null +++ b/tests/base.py @@ -0,0 +1,82 @@ +# Copyright 2014 Rackspace Australia +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import fixtures +import gear +import logging +import os +import testtools +import time +import yaml + +import turbo_hipster.worker_server + +logging.basicConfig(level=logging.DEBUG, + format='%(asctime)s %(name)-32s ' + '%(levelname)-8s %(message)s') + + +class TestWithGearman(testtools.TestCase): + + log = logging.getLogger("TestWithGearman") + + def setUp(self): + super(TestWithGearman, self).setUp() + self.config = None + self.worker_server = None + self.gearman_server = gear.Server(0) + + def start_server(self): + if not self.config: + self._load_config_fixture() + # Grab the port so the clients can connect to it + self.config['zuul_server']['gearman_port'] = self.gearman_server.port + + self.worker_server = turbo_hipster.worker_server.Server(self.config) + self.worker_server.setup_logging() + self.worker_server.start() + t0 = time.time() + while time.time() - t0 < 10: + if self.worker_server.services_started: + break + time.sleep(0.01) + if not self.worker_server.services_started: + self.fail("Failed to start worker_service services") + + def tearDown(self): + if self.worker_server and not self.worker_server.stopped(): + self.worker_server.shutdown() + self.gearman_server.shutdown() + super(TestWithGearman, self).tearDown() + + def _load_config_fixture(self, config_name='default-config.yaml'): + config_dir = os.path.join(os.path.dirname(__file__), 'etc') + with open(os.path.join(config_dir, config_name), 'r') as config_stream: + self.config = yaml.safe_load(config_stream) + + # Set all of the working dirs etc to a writeable temp dir + temp_path = self.useFixture(fixtures.TempDir()).path + for config_dir in ['debug_log', 'jobs_working_dir', 'git_working_dir', + 'pip_download_cache']: + if config_dir in self.config: + if self.config[config_dir][0] == '/': + self.config[config_dir] = self.config[config_dir][1:] + self.config[config_dir] = os.path.join(temp_path, + self.config[config_dir]) + if self.config['publish_logs']['type'] == 'local': + if self.config['publish_logs']['path'][0] == '/': + self.config['publish_logs']['path'] = \ + self.config['publish_logs']['path'][1:] + self.config['publish_logs']['path'] = os.path.join( + temp_path, self.config[config_dir]) diff --git a/tests/task_plugins/__init__.py b/tests/task_plugins/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/task_plugins/gate_real_db_upgrade/__init__.py b/tests/task_plugins/gate_real_db_upgrade/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/task_plugins/gate_real_db_upgrade/test_handle_results.py b/tests/test_real_db_upgrade.py similarity index 98% rename from tests/task_plugins/gate_real_db_upgrade/test_handle_results.py rename to tests/test_real_db_upgrade.py index b05d306..0a88156 100644 --- a/tests/task_plugins/gate_real_db_upgrade/test_handle_results.py +++ b/tests/test_real_db_upgrade.py @@ -18,7 +18,7 @@ import testtools from turbo_hipster.task_plugins.gate_real_db_upgrade import handle_results -TESTS_DIR = os.path.join(os.path.dirname(__file__), '../..') +TESTS_DIR = os.path.join(os.path.dirname(__file__)) class TestHandleResults(testtools.TestCase): diff --git a/tests/test_worker_manager.py b/tests/test_worker_manager.py index ff0733f..741f223 100644 --- a/tests/test_worker_manager.py +++ b/tests/test_worker_manager.py @@ -1,5 +1,3 @@ -#!/usr/bin/python2 -# # Copyright 2013 Rackspace Australia # # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -14,81 +12,14 @@ # License for the specific language governing permissions and limitations # under the License. - -import fixtures -import gear -import logging import os -import testtools import time -import yaml - -import turbo_hipster.task_plugins.gate_real_db_upgrade.task -import turbo_hipster.worker_server +import base import fakes -logging.basicConfig(level=logging.DEBUG, - format='%(asctime)s %(name)-32s ' - '%(levelname)-8s %(message)s') - -class TestWithGearman(testtools.TestCase): - - log = logging.getLogger("TestWithGearman") - - def setUp(self): - super(TestWithGearman, self).setUp() - self.config = None - self.worker_server = None - self.gearman_server = gear.Server(0) - - def start_server(self): - if not self.config: - self._load_config_fixture() - # Grab the port so the clients can connect to it - self.config['zuul_server']['gearman_port'] = self.gearman_server.port - - self.worker_server = turbo_hipster.worker_server.Server(self.config) - self.worker_server.setup_logging() - self.worker_server.start() - t0 = time.time() - while time.time() - t0 < 10: - if self.worker_server.services_started: - break - time.sleep(0.01) - if not self.worker_server.services_started: - self.fail("Failed to start worker_service services") - - def tearDown(self): - if self.worker_server and not self.worker_server.stopped(): - self.worker_server.shutdown() - self.gearman_server.shutdown() - super(TestWithGearman, self).tearDown() - - def _load_config_fixture(self, config_name='default-config.yaml'): - config_dir = os.path.join(os.path.dirname(__file__), 'etc') - with open(os.path.join(config_dir, config_name), 'r') as config_stream: - self.config = yaml.safe_load(config_stream) - - # Set all of the working dirs etc to a writeable temp dir - temp_path = self.useFixture(fixtures.TempDir()).path - for config_dir in ['debug_log', 'jobs_working_dir', 'git_working_dir', - 'pip_download_cache']: - if config_dir in self.config: - if self.config[config_dir][0] == '/': - self.config[config_dir] = self.config[config_dir][1:] - self.config[config_dir] = os.path.join(temp_path, - self.config[config_dir]) - if self.config['publish_logs']['type'] == 'local': - if self.config['publish_logs']['path'][0] == '/': - self.config['publish_logs']['path'] = \ - self.config['publish_logs']['path'][1:] - self.config['publish_logs']['path'] = os.path.join( - temp_path, self.config[config_dir]) - - -class TestWorkerServer(TestWithGearman): +class TestWorkerServer(base.TestWithGearman): def test_plugins_load(self): "Test the configured plugins are loaded" @@ -144,7 +75,7 @@ class TestWorkerServer(TestWithGearman): self.assertFalse(self.worker_server.zuul_manager.stopped()) -class TestZuulClient(TestWithGearman): +class TestZuulClient(base.TestWithGearman): def test_setup_gearman_worker(self): "Make sure the client is registered as a worker with gearman" pass @@ -212,7 +143,7 @@ class TestZuulClient(TestWithGearman): self.assertTrue(self.worker_server.stopped()) -class TestZuulManager(TestWithGearman): +class TestZuulManager(base.TestWithGearman): def test_registered_functions(self): "Test the correct functions are registered with gearman"