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. |
Joshua Hesketh | ef72b8a | 2013-08-12 11:17:14 +1000 | [diff] [blame] | 16 | |
Joshua Hesketh | ef72b8a | 2013-08-12 11:17:14 +1000 | [diff] [blame] | 17 | |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame^] | 18 | import gear |
| 19 | import logging |
Joshua Hesketh | 66f9f60 | 2013-08-14 11:28:10 +1000 | [diff] [blame] | 20 | import os |
| 21 | import testtools |
Joshua Hesketh | 0d9a8c0 | 2013-08-14 12:38:14 +1000 | [diff] [blame] | 22 | import time |
Matthew Oliver | 49c9063 | 2014-02-14 16:37:08 +1100 | [diff] [blame] | 23 | import yaml |
Joshua Hesketh | ef72b8a | 2013-08-12 11:17:14 +1000 | [diff] [blame] | 24 | |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame^] | 25 | import turbo_hipster.task_plugins.gate_real_db_upgrade.task |
| 26 | import turbo_hipster.worker_server |
| 27 | |
| 28 | logging.basicConfig(level=logging.DEBUG, |
| 29 | format='%(asctime)s %(name)-32s ' |
| 30 | '%(levelname)-8s %(message)s') |
Joshua Hesketh | ef72b8a | 2013-08-12 11:17:14 +1000 | [diff] [blame] | 31 | |
Joshua Hesketh | ef72b8a | 2013-08-12 11:17:14 +1000 | [diff] [blame] | 32 | |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame^] | 33 | class TestWithGearman(testtools.TestCase): |
| 34 | |
| 35 | log = logging.getLogger("TestWithGearman") |
| 36 | |
Joshua Hesketh | 66f9f60 | 2013-08-14 11:28:10 +1000 | [diff] [blame] | 37 | def setUp(self): |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame^] | 38 | super(TestWithGearman, self).setUp() |
| 39 | |
| 40 | self.config = [] |
| 41 | self._load_config_fixture() |
| 42 | |
| 43 | self.gearman_server = gear.Server(0) |
| 44 | |
| 45 | # Grab the port so the clients can connect to it |
Joshua Hesketh | 0d9a8c0 | 2013-08-14 12:38:14 +1000 | [diff] [blame] | 46 | self.config['zuul_server']['gearman_port'] = self.gearman_server.port |
| 47 | |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame^] | 48 | self.worker_server = turbo_hipster.worker_server.Server(self.config) |
| 49 | self.worker_server.setup_logging() |
| 50 | self.worker_server.start() |
| 51 | t0 = time.time() |
| 52 | while time.time() - t0 < 10: |
| 53 | if self.worker_server.services_started: |
| 54 | break |
| 55 | time.sleep(0.01) |
| 56 | if not self.worker_server.services_started: |
| 57 | self.fail("Failed to start worker_service services") |
Joshua Hesketh | 66f9f60 | 2013-08-14 11:28:10 +1000 | [diff] [blame] | 58 | |
Joshua Hesketh | 3fc1264 | 2014-01-29 16:32:02 +1100 | [diff] [blame] | 59 | def tearDown(self): |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame^] | 60 | self.worker_server.stop() |
Joshua Hesketh | 3fc1264 | 2014-01-29 16:32:02 +1100 | [diff] [blame] | 61 | self.gearman_server.shutdown() |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame^] | 62 | super(TestWithGearman, self).tearDown() |
Joshua Hesketh | 3fc1264 | 2014-01-29 16:32:02 +1100 | [diff] [blame] | 63 | |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame^] | 64 | def _load_config_fixture(self, config_name='default-config.json'): |
| 65 | config_dir = os.path.join(os.path.dirname(__file__), 'fixtures') |
| 66 | with open(os.path.join(config_dir, config_name), 'r') as config_stream: |
| 67 | self.config = yaml.safe_load(config_stream) |
Joshua Hesketh | 0d9a8c0 | 2013-08-14 12:38:14 +1000 | [diff] [blame] | 68 | |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame^] | 69 | |
| 70 | class TestWorkerServer(TestWithGearman): |
| 71 | def test_plugins_load(self): |
| 72 | "Test the configured plugins are loaded" |
| 73 | |
| 74 | self.assertFalse(self.worker_server.stopped()) |
| 75 | self.assertEqual(3, len(self.worker_server.plugins)) |
| 76 | |
| 77 | plugin0_config = { |
| 78 | "name": "gate_real_db_upgrade", |
| 79 | "datasets_dir": "/var/lib/turbo-hipster/datasets_devstack_131007", |
| 80 | "function": "build:gate-real-db-upgrade_nova_mysql_devstack_131007" |
| 81 | } |
| 82 | plugin1_config = { |
| 83 | "name": "gate_real_db_upgrade", |
| 84 | "datasets_dir": "/var/lib/turbo-hipster/datasets_user_001", |
| 85 | "function": "build:gate-real-db-upgrade_nova_mysql_user_001" |
| 86 | } |
| 87 | plugin2_config = { |
| 88 | "name": "shell_script", |
| 89 | "function": "build:do_something_shelly" |
| 90 | } |
| 91 | |
| 92 | self.assertEqual(plugin0_config, |
| 93 | self.worker_server.plugins[0]['plugin_config']) |
| 94 | self.assertEqual( |
| 95 | 'turbo_hipster.task_plugins.gate_real_db_upgrade.task', |
| 96 | self.worker_server.plugins[0]['module'].__name__ |
| 97 | ) |
| 98 | |
| 99 | self.assertEqual(plugin1_config, |
| 100 | self.worker_server.plugins[1]['plugin_config']) |
| 101 | self.assertEqual( |
| 102 | 'turbo_hipster.task_plugins.gate_real_db_upgrade.task', |
| 103 | self.worker_server.plugins[1]['module'].__name__ |
| 104 | ) |
| 105 | |
| 106 | self.assertEqual(plugin2_config, |
| 107 | self.worker_server.plugins[2]['plugin_config']) |
| 108 | self.assertEqual( |
| 109 | 'turbo_hipster.task_plugins.shell_script.task', |
| 110 | self.worker_server.plugins[2]['module'].__name__ |
| 111 | ) |
| 112 | |
| 113 | def test_zuul_client_started(self): |
| 114 | "Test the zuul client has been started" |
| 115 | self.assertFalse(self.worker_server.zuul_client.stopped()) |
| 116 | |
| 117 | def test_zuul_manager_started(self): |
| 118 | "Test the zuul manager has been started" |
| 119 | self.assertFalse(self.worker_server.zuul_manager.stopped()) |
| 120 | |
| 121 | |
| 122 | class TestZuulClient(TestWithGearman): |
| 123 | def test_setup_gearman_worker(self): |
| 124 | "Make sure the client is registered as a worker with gearman" |
| 125 | pass |
| 126 | |
| 127 | def test_registered_functions(self): |
| 128 | "Test the correct functions are registered with gearman" |
| 129 | # The client should have all of the functions defined in the config |
| 130 | # registered with gearman |
| 131 | |
| 132 | # We need to wait for all the functions to register with the server.. |
| 133 | # We'll give it up to 10seconds to do so |
| 134 | t0 = time.time() |
| 135 | failed = True |
| 136 | while time.time() - t0 < 10: |
| 137 | # There should be 4 functions. 1 for each plugin + 1 for the |
| 138 | # manager |
| 139 | if len(self.gearman_server.functions) == 4: |
| 140 | failed = False |
Joshua Hesketh | 0d9a8c0 | 2013-08-14 12:38:14 +1000 | [diff] [blame] | 141 | break |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame^] | 142 | time.sleep(0.01) |
| 143 | if failed: |
| 144 | self.log.debug(self.gearman_server.functions) |
| 145 | self.fail("The correct number of functions haven't registered with" |
| 146 | " gearman") |
| 147 | |
| 148 | self.assertIn('build:gate-real-db-upgrade_nova_mysql_devstack_131007', |
| 149 | self.gearman_server.functions) |
| 150 | self.assertIn('build:gate-real-db-upgrade_nova_mysql_user_001', |
| 151 | self.gearman_server.functions) |
| 152 | self.assertIn('build:do_something_shelly', |
| 153 | self.gearman_server.functions) |
| 154 | |
| 155 | def test_waiting_for_job(self): |
| 156 | "Make sure the client waits for jobs as expected" |
| 157 | pass |
| 158 | |
| 159 | def test_stop(self): |
| 160 | "Test sending a stop signal to the client exists correctly" |
| 161 | pass |
| 162 | |
| 163 | |
| 164 | class TestZuulManager(TestWithGearman): |
| 165 | def test_registered_functions(self): |
| 166 | "Test the correct functions are registered with gearman" |
| 167 | # We need to wait for all the functions to register with the server.. |
| 168 | # We'll give it up to 10seconds to do so |
| 169 | t0 = time.time() |
| 170 | failed = True |
| 171 | while time.time() - t0 < 10: |
| 172 | # There should be 4 functions. 1 for each plugin + 1 for the |
| 173 | # manager |
| 174 | if len(self.gearman_server.functions) == 4: |
| 175 | failed = False |
| 176 | break |
| 177 | time.sleep(0.01) |
| 178 | if failed: |
| 179 | self.log.debug(self.gearman_server.functions) |
| 180 | self.fail("The correct number of functions haven't registered with" |
| 181 | " gearman") |
Joshua Hesketh | 0d9a8c0 | 2013-08-14 12:38:14 +1000 | [diff] [blame] | 182 | |
| 183 | hostname = os.uname()[1] |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame^] | 184 | self.assertIn('stop:turbo-hipster-manager-%s' % hostname, |
| 185 | self.gearman_server.functions) |