blob: 1f3de5ee0fbed515166f99a6c1830f742fa2a81d [file] [log] [blame]
Joshua Hesketh66f9f602013-08-14 11:28:10 +10001#!/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 Heskethef72b8a2013-08-12 11:17:14 +100016
Joshua Heskethef72b8a2013-08-12 11:17:14 +100017
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +110018import gear
19import logging
Joshua Hesketh66f9f602013-08-14 11:28:10 +100020import os
21import testtools
Joshua Hesketh0d9a8c02013-08-14 12:38:14 +100022import time
Matthew Oliver49c90632014-02-14 16:37:08 +110023import yaml
Joshua Heskethef72b8a2013-08-12 11:17:14 +100024
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +110025import turbo_hipster.task_plugins.gate_real_db_upgrade.task
26import turbo_hipster.worker_server
27
28logging.basicConfig(level=logging.DEBUG,
29 format='%(asctime)s %(name)-32s '
30 '%(levelname)-8s %(message)s')
Joshua Heskethef72b8a2013-08-12 11:17:14 +100031
Joshua Heskethef72b8a2013-08-12 11:17:14 +100032
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +110033class TestWithGearman(testtools.TestCase):
34
35 log = logging.getLogger("TestWithGearman")
36
Joshua Hesketh66f9f602013-08-14 11:28:10 +100037 def setUp(self):
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +110038 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 Hesketh0d9a8c02013-08-14 12:38:14 +100046 self.config['zuul_server']['gearman_port'] = self.gearman_server.port
47
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +110048 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 Hesketh66f9f602013-08-14 11:28:10 +100058
Joshua Hesketh3fc12642014-01-29 16:32:02 +110059 def tearDown(self):
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +110060 self.worker_server.stop()
Joshua Hesketh3fc12642014-01-29 16:32:02 +110061 self.gearman_server.shutdown()
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +110062 super(TestWithGearman, self).tearDown()
Joshua Hesketh3fc12642014-01-29 16:32:02 +110063
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +110064 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 Hesketh0d9a8c02013-08-14 12:38:14 +100068
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +110069
70class 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
122class 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 Hesketh0d9a8c02013-08-14 12:38:14 +1000141 break
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +1100142 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
164class 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 Hesketh0d9a8c02013-08-14 12:38:14 +1000182
183 hostname = os.uname()[1]
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +1100184 self.assertIn('stop:turbo-hipster-manager-%s' % hostname,
185 self.gearman_server.functions)