blob: cebf8dc2a0600a78c75b9db65fbc434b79ce65ed [file] [log] [blame]
Joshua Hesketh66f9f602013-08-14 11:28:10 +10001# 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 Heskethef72b8a2013-08-12 11:17:14 +100014
Joshua Hesketh66f9f602013-08-14 11:28:10 +100015import os
Joshua Hesketh0d9a8c02013-08-14 12:38:14 +100016import time
Joshua Heskethef72b8a2013-08-12 11:17:14 +100017
Joshua Heskethe1859112014-04-05 17:00:04 +110018import base
Joshua Hesketh96adb282014-03-25 16:26:45 +110019import fakes
20
Joshua Heskethef72b8a2013-08-12 11:17:14 +100021
Joshua Heskethe1859112014-04-05 17:00:04 +110022class TestWorkerServer(base.TestWithGearman):
Joshua Hesketh62721fb2014-12-02 13:22:10 +110023 def test_jobs_load_from_legacy_plugins(self):
24 "Test the configured plugins are loaded from legacy config.yaml layout"
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +110025
Joshua Hesketh96adb282014-03-25 16:26:45 +110026 self.start_server()
27
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +110028 self.assertFalse(self.worker_server.stopped())
Joshua Hesketh62721fb2014-12-02 13:22:10 +110029 self.assertEqual(3, len(self.worker_server.jobs))
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +110030
Joshua Hesketh62721fb2014-12-02 13:22:10 +110031 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 Hesketh5cd8ea32014-03-27 15:28:00 +110066 }
67
Joshua Hesketh62721fb2014-12-02 13:22:10 +110068 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 Hesketh5cd8ea32014-03-27 15:28:00 +110083
Joshua Hesketh62721fb2014-12-02 13:22:10 +110084 def test_job_configuration(self):
85 "Test config.yaml job layout"
86 self._load_config_fixture('config.yaml')
87 self.start_server()
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +110088
Joshua Hesketh62721fb2014-12-02 13:22:10 +110089 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 Hesketh5cd8ea32014-03-27 15:28:00 +1100142
143 def test_zuul_client_started(self):
144 "Test the zuul client has been started"
Joshua Hesketh96adb282014-03-25 16:26:45 +1100145 self.start_server()
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +1100146 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 Hesketh96adb282014-03-25 16:26:45 +1100150 self.start_server()
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +1100151 self.assertFalse(self.worker_server.zuul_manager.stopped())
152
153
Joshua Heskethe1859112014-04-05 17:00:04 +1100154class TestZuulClient(base.TestWithGearman):
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +1100155 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 Hesketh96adb282014-03-25 16:26:45 +1100161
162 self.start_server()
163
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +1100164 # 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 Hesketh0d9a8c02013-08-14 12:38:14 +1000176 break
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +1100177 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 Heskethab964fb2014-05-02 10:05:10 +1000183 self.assertIn('build:real-db-upgrade_nova_mysql_devstack_131007',
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +1100184 self.gearman_server.functions)
Joshua Heskethab964fb2014-05-02 10:05:10 +1000185 self.assertIn('build:real-db-upgrade_nova_mysql_user_001',
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +1100186 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 Hesketh96adb282014-03-25 16:26:45 +1100198 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 Hesketh5cd8ea32014-03-27 15:28:00 +1100221
Joshua Heskethe1859112014-04-05 17:00:04 +1100222class TestZuulManager(base.TestWithGearman):
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +1100223 def test_registered_functions(self):
224 "Test the correct functions are registered with gearman"
Joshua Hesketh96adb282014-03-25 16:26:45 +1100225
226 self.start_server()
227
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +1100228 # 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 Hesketh0d9a8c02013-08-14 12:38:14 +1000243
244 hostname = os.uname()[1]
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +1100245 self.assertIn('stop:turbo-hipster-manager-%s' % hostname,
246 self.gearman_server.functions)