Joshua Hesketh | 66f9f60 | 2013-08-14 11:28:10 +1000 | [diff] [blame] | 1 | # Copyright 2013 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. |
Joshua Hesketh | ef72b8a | 2013-08-12 11:17:14 +1000 | [diff] [blame] | 14 | |
Joshua Hesketh | 66f9f60 | 2013-08-14 11:28:10 +1000 | [diff] [blame] | 15 | import os |
Joshua Hesketh | 0d9a8c0 | 2013-08-14 12:38:14 +1000 | [diff] [blame] | 16 | import time |
Joshua Hesketh | ef72b8a | 2013-08-12 11:17:14 +1000 | [diff] [blame] | 17 | |
Joshua Hesketh | e185911 | 2014-04-05 17:00:04 +1100 | [diff] [blame] | 18 | import base |
Joshua Hesketh | 96adb28 | 2014-03-25 16:26:45 +1100 | [diff] [blame] | 19 | import fakes |
| 20 | |
Joshua Hesketh | ef72b8a | 2013-08-12 11:17:14 +1000 | [diff] [blame] | 21 | |
Joshua Hesketh | e185911 | 2014-04-05 17:00:04 +1100 | [diff] [blame] | 22 | class TestWorkerServer(base.TestWithGearman): |
Joshua Hesketh | 62721fb | 2014-12-02 13:22:10 +1100 | [diff] [blame] | 23 | def test_jobs_load_from_legacy_plugins(self): |
| 24 | "Test the configured plugins are loaded from legacy config.yaml layout" |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 25 | |
Joshua Hesketh | 96adb28 | 2014-03-25 16:26:45 +1100 | [diff] [blame] | 26 | self.start_server() |
| 27 | |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 28 | self.assertFalse(self.worker_server.stopped()) |
Joshua Hesketh | 62721fb | 2014-12-02 13:22:10 +1100 | [diff] [blame] | 29 | self.assertEqual(3, len(self.worker_server.jobs)) |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 30 | |
Joshua Hesketh | 62721fb | 2014-12-02 13:22:10 +1100 | [diff] [blame] | 31 | expected_jobs = { |
| 32 | 'build:real-db-upgrade_nova_mysql_devstack_131007': { |
| 33 | "name": "build:real-db-upgrade_nova_mysql_devstack_131007", |
| 34 | "plugin": "real_db_upgrade", |
| 35 | "runner_module_name": "turbo_hipster.task_plugins." |
| 36 | "real_db_upgrade.task", |
| 37 | "plugin_config": { |
| 38 | "name": "real_db_upgrade", |
| 39 | "datasets_dir": "/var/lib/turbo-hipster/" |
| 40 | "datasets_devstack_131007", |
| 41 | "function": "build:real-db-upgrade_nova_mysql_devstack_" |
| 42 | "131007" |
| 43 | }, |
| 44 | }, |
| 45 | 'build:real-db-upgrade_nova_mysql_user_001': { |
| 46 | "name": "build:real-db-upgrade_nova_mysql_user_001", |
| 47 | "plugin": "real_db_upgrade", |
| 48 | "runner_module_name": "turbo_hipster.task_plugins." |
| 49 | "real_db_upgrade.task", |
| 50 | "plugin_config": { |
| 51 | "name": "real_db_upgrade", |
| 52 | "datasets_dir": "/var/lib/turbo-hipster/datasets_user_001", |
| 53 | "function": "build:real-db-upgrade_nova_mysql_user_001" |
| 54 | }, |
| 55 | }, |
| 56 | 'build:do_something_shelly': { |
| 57 | "name": "build:do_something_shelly", |
| 58 | "plugin": "shell_script", |
| 59 | "runner_module_name": "turbo_hipster.task_plugins." |
| 60 | "shell_script.task", |
| 61 | "job_config": { |
| 62 | "name": "build:do_something_shelly", |
| 63 | "shell_script": "ls -lah && echo", |
| 64 | }, |
| 65 | }, |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 66 | } |
| 67 | |
Joshua Hesketh | 62721fb | 2014-12-02 13:22:10 +1100 | [diff] [blame] | 68 | for job_name, job in self.worker_server.jobs.items(): |
| 69 | self.assertEqual(expected_jobs[job_name]['name'], |
| 70 | job['name']) |
| 71 | self.assertEqual(expected_jobs[job_name]['plugin'], |
| 72 | job['plugin']) |
| 73 | if 'plugin_config' in job: |
| 74 | self.assertEqual(expected_jobs[job_name]['plugin_config'], |
| 75 | job['plugin_config']) |
| 76 | if 'job_config' in job: |
| 77 | self.assertEqual(expected_jobs[job_name]['job_config'], |
| 78 | job['job_config']) |
| 79 | self.assertEqual( |
| 80 | expected_jobs[job_name]['runner_module_name'], |
| 81 | job['runner'].__module__ |
| 82 | ) |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 83 | |
Joshua Hesketh | 62721fb | 2014-12-02 13:22:10 +1100 | [diff] [blame] | 84 | def test_job_configuration(self): |
| 85 | "Test config.yaml job layout" |
| 86 | self._load_config_fixture('config.yaml') |
| 87 | self.start_server() |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 88 | |
Joshua Hesketh | 62721fb | 2014-12-02 13:22:10 +1100 | [diff] [blame] | 89 | self.assertFalse(self.worker_server.stopped()) |
| 90 | self.assertEqual(3, len(self.worker_server.jobs)) |
| 91 | |
| 92 | expected_jobs = { |
| 93 | 'build:real-db-upgrade_nova_mysql': { |
| 94 | "name": "build:real-db-upgrade_nova_mysql", |
| 95 | "plugin": "real_db_upgrade", |
| 96 | "runner_module_name": "turbo_hipster.task_plugins." |
| 97 | "real_db_upgrade.task", |
| 98 | "job_config": { |
| 99 | "name": "build:real-db-upgrade_nova_mysql", |
| 100 | "plugin": "real_db_upgrade", |
| 101 | "datasets_dir": "/home/josh/var/lib/turbo-hipster/datasets" |
| 102 | }, |
| 103 | }, |
| 104 | 'build:real-db-upgrade_nova_mysql_user_001': { |
| 105 | "name": "build:real-db-upgrade_nova_mysql_user_001", |
| 106 | "plugin": "real_db_upgrade", |
| 107 | "runner_module_name": "turbo_hipster.task_plugins." |
| 108 | "real_db_upgrade.task", |
| 109 | "plugin_config": { |
| 110 | "name": "real_db_upgrade", |
| 111 | "datasets_dir": "/var/lib/turbo-hipster/datasets_user_001", |
| 112 | "function": "build:real-db-upgrade_nova_mysql_user_001", |
| 113 | }, |
| 114 | }, |
| 115 | 'build:some_shell_job': { |
| 116 | "name": "build:some_shell_job", |
| 117 | "plugin": "shell_script", |
| 118 | "runner_module_name": "turbo_hipster.task_plugins." |
| 119 | "shell_script.task", |
| 120 | "job_config": { |
| 121 | "name": "build:some_shell_job", |
| 122 | "shell_script": "/dev/null", |
| 123 | }, |
| 124 | }, |
| 125 | } |
| 126 | |
| 127 | for job_name, job in self.worker_server.jobs.items(): |
| 128 | self.assertEqual(expected_jobs[job_name]['name'], |
| 129 | job['name']) |
| 130 | self.assertEqual(expected_jobs[job_name]['plugin'], |
| 131 | job['plugin']) |
| 132 | if 'plugin_config' in job: |
| 133 | self.assertEqual(expected_jobs[job_name]['plugin_config'], |
| 134 | job['plugin_config']) |
| 135 | if 'job_config' in job: |
| 136 | self.assertEqual(expected_jobs[job_name]['job_config'], |
| 137 | job['job_config']) |
| 138 | self.assertEqual( |
| 139 | expected_jobs[job_name]['runner_module_name'], |
| 140 | job['runner'].__module__ |
| 141 | ) |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 142 | |
| 143 | def test_zuul_client_started(self): |
| 144 | "Test the zuul client has been started" |
Joshua Hesketh | 96adb28 | 2014-03-25 16:26:45 +1100 | [diff] [blame] | 145 | self.start_server() |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 146 | self.assertFalse(self.worker_server.zuul_client.stopped()) |
| 147 | |
| 148 | def test_zuul_manager_started(self): |
| 149 | "Test the zuul manager has been started" |
Joshua Hesketh | 96adb28 | 2014-03-25 16:26:45 +1100 | [diff] [blame] | 150 | self.start_server() |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 151 | self.assertFalse(self.worker_server.zuul_manager.stopped()) |
| 152 | |
| 153 | |
Joshua Hesketh | e185911 | 2014-04-05 17:00:04 +1100 | [diff] [blame] | 154 | class TestZuulClient(base.TestWithGearman): |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 155 | def test_setup_gearman_worker(self): |
| 156 | "Make sure the client is registered as a worker with gearman" |
| 157 | pass |
| 158 | |
| 159 | def test_registered_functions(self): |
| 160 | "Test the correct functions are registered with gearman" |
Joshua Hesketh | 96adb28 | 2014-03-25 16:26:45 +1100 | [diff] [blame] | 161 | |
| 162 | self.start_server() |
| 163 | |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 164 | # The client should have all of the functions defined in the config |
| 165 | # registered with gearman |
| 166 | |
| 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 |
Joshua Hesketh | 0d9a8c0 | 2013-08-14 12:38:14 +1000 | [diff] [blame] | 176 | break |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 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") |
| 182 | |
Joshua Hesketh | ab964fb | 2014-05-02 10:05:10 +1000 | [diff] [blame] | 183 | self.assertIn('build:real-db-upgrade_nova_mysql_devstack_131007', |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 184 | self.gearman_server.functions) |
Joshua Hesketh | ab964fb | 2014-05-02 10:05:10 +1000 | [diff] [blame] | 185 | self.assertIn('build:real-db-upgrade_nova_mysql_user_001', |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 186 | self.gearman_server.functions) |
| 187 | self.assertIn('build:do_something_shelly', |
| 188 | self.gearman_server.functions) |
| 189 | |
| 190 | def test_waiting_for_job(self): |
| 191 | "Make sure the client waits for jobs as expected" |
| 192 | pass |
| 193 | |
| 194 | def test_stop(self): |
| 195 | "Test sending a stop signal to the client exists correctly" |
| 196 | pass |
| 197 | |
Joshua Hesketh | 96adb28 | 2014-03-25 16:26:45 +1100 | [diff] [blame] | 198 | def test_job_can_shutdown_th(self): |
| 199 | self._load_config_fixture('shutdown-config.yaml') |
| 200 | self.start_server() |
| 201 | zuul = fakes.FakeZuul(self.config['zuul_server']['gearman_host'], |
| 202 | self.config['zuul_server']['gearman_port']) |
| 203 | |
| 204 | # First check we can run a job that /doesn't/ shut down turbo-hipster |
| 205 | data_req = zuul.make_zuul_data() |
| 206 | zuul.submit_job('build:demo_job_clean', data_req) |
| 207 | zuul.wait_for_completion() |
| 208 | self.assertTrue(zuul.job.complete) |
| 209 | self.assertFalse(self.worker_server.stopped()) |
| 210 | |
| 211 | # Now run a job that leaves the environment dirty and /should/ shut |
| 212 | # down turbo-hipster |
| 213 | zuul.job = None |
| 214 | zuul.submit_job('build:demo_job_dirty', data_req) |
| 215 | zuul.wait_for_completion() |
| 216 | self.assertTrue(zuul.job.complete) |
| 217 | # Give the server a second to shutdown |
| 218 | time.sleep(1) |
| 219 | self.assertTrue(self.worker_server.stopped()) |
| 220 | |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 221 | |
Joshua Hesketh | e185911 | 2014-04-05 17:00:04 +1100 | [diff] [blame] | 222 | class TestZuulManager(base.TestWithGearman): |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 223 | def test_registered_functions(self): |
| 224 | "Test the correct functions are registered with gearman" |
Joshua Hesketh | 96adb28 | 2014-03-25 16:26:45 +1100 | [diff] [blame] | 225 | |
| 226 | self.start_server() |
| 227 | |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 228 | # We need to wait for all the functions to register with the server.. |
| 229 | # We'll give it up to 10seconds to do so |
| 230 | t0 = time.time() |
| 231 | failed = True |
| 232 | while time.time() - t0 < 10: |
| 233 | # There should be 4 functions. 1 for each plugin + 1 for the |
| 234 | # manager |
| 235 | if len(self.gearman_server.functions) == 4: |
| 236 | failed = False |
| 237 | break |
| 238 | time.sleep(0.01) |
| 239 | if failed: |
| 240 | self.log.debug(self.gearman_server.functions) |
| 241 | self.fail("The correct number of functions haven't registered with" |
| 242 | " gearman") |
Joshua Hesketh | 0d9a8c0 | 2013-08-14 12:38:14 +1000 | [diff] [blame] | 243 | |
| 244 | hostname = os.uname()[1] |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 245 | self.assertIn('stop:turbo-hipster-manager-%s' % hostname, |
| 246 | self.gearman_server.functions) |