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 | 66f9f60 | 2013-08-14 11:28:10 +1000 | [diff] [blame] | 18 | import json |
| 19 | import os |
| 20 | import testtools |
Joshua Hesketh | 0d9a8c0 | 2013-08-14 12:38:14 +1000 | [diff] [blame] | 21 | import time |
Joshua Hesketh | b9a7f74 | 2013-08-14 12:55:14 +1000 | [diff] [blame] | 22 | from fakes import FakeGearmanManager, FakeGearmanServer,\ |
| 23 | FakeRealDbUpgradeRunner |
Joshua Hesketh | ef72b8a | 2013-08-12 11:17:14 +1000 | [diff] [blame] | 24 | |
Joshua Hesketh | 66f9f60 | 2013-08-14 11:28:10 +1000 | [diff] [blame] | 25 | CONFIG_DIR = os.path.join(os.path.dirname(__file__), 'etc') |
| 26 | with open(os.path.join(CONFIG_DIR, 'config.json'), 'r') as config_stream: |
| 27 | CONFIG = json.load(config_stream) |
Joshua Hesketh | ef72b8a | 2013-08-12 11:17:14 +1000 | [diff] [blame] | 28 | |
Joshua Hesketh | ef72b8a | 2013-08-12 11:17:14 +1000 | [diff] [blame] | 29 | |
Joshua Hesketh | 66f9f60 | 2013-08-14 11:28:10 +1000 | [diff] [blame] | 30 | class TestGearmanManager(testtools.TestCase): |
| 31 | def setUp(self): |
| 32 | super(TestGearmanManager, self).setUp() |
| 33 | self.config = CONFIG |
Joshua Hesketh | 66f9f60 | 2013-08-14 11:28:10 +1000 | [diff] [blame] | 34 | self.gearman_server = FakeGearmanServer( |
| 35 | self.config['zuul_server']['gearman_port']) |
Joshua Hesketh | 0d9a8c0 | 2013-08-14 12:38:14 +1000 | [diff] [blame] | 36 | self.config['zuul_server']['gearman_port'] = self.gearman_server.port |
| 37 | |
Joshua Hesketh | 123625c | 2013-09-17 13:34:52 +1000 | [diff] [blame] | 38 | self.task = FakeRealDbUpgradeRunner(self.config, |
Joshua Hesketh | 527966b | 2013-11-19 12:01:06 +1100 | [diff] [blame^] | 39 | self.config['plugins'][0], |
| 40 | 'test-worker-1', self) |
Joshua Hesketh | 0d9a8c0 | 2013-08-14 12:38:14 +1000 | [diff] [blame] | 41 | self.tasks = dict(FakeRealDbUpgradeRunner_worker=self.task) |
Joshua Hesketh | 66f9f60 | 2013-08-14 11:28:10 +1000 | [diff] [blame] | 42 | |
| 43 | self.gearman_manager = FakeGearmanManager(self.config, |
| 44 | self.tasks, |
| 45 | self) |
| 46 | |
| 47 | def test_manager_function_registered(self): |
| 48 | """ Check the manager is set up correctly and registered with the |
| 49 | gearman server with an appropriate function """ |
Joshua Hesketh | 0d9a8c0 | 2013-08-14 12:38:14 +1000 | [diff] [blame] | 50 | |
| 51 | # Give the gearman server up to 5 seconds to register the function |
| 52 | for x in range(500): |
| 53 | time.sleep(0.01) |
| 54 | if len(self.gearman_server.functions) > 0: |
| 55 | break |
| 56 | |
| 57 | hostname = os.uname()[1] |
| 58 | function_name = 'stop:turbo-hipster-manager-%s' % hostname |
| 59 | |
| 60 | self.assertIn(function_name, self.gearman_server.functions) |
| 61 | |
| 62 | def test_task_registered_with_manager(self): |
| 63 | """ Check the FakeRealDbUpgradeRunner_worker task is registered """ |
| 64 | self.assertIn('FakeRealDbUpgradeRunner_worker', |
| 65 | self.gearman_manager.tasks.keys()) |
| 66 | |
| 67 | def test_stop_task(self): |
| 68 | """ Check that the manager successfully stops a task when requested |
| 69 | """ |
Joshua Hesketh | 66f9f60 | 2013-08-14 11:28:10 +1000 | [diff] [blame] | 70 | pass |