blob: ae2d5b54976d06087c50854cc81f7ed0a7a7179f [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 Hesketh66f9f602013-08-14 11:28:10 +100018import os
19import testtools
Joshua Hesketh0d9a8c02013-08-14 12:38:14 +100020import time
Matthew Oliver49c90632014-02-14 16:37:08 +110021import yaml
Joshua Hesketh4343b952013-11-20 12:11:55 +110022from fakes import FakeZuulManager, FakeGearmanServer,\
Joshua Heskethb9a7f742013-08-14 12:55:14 +100023 FakeRealDbUpgradeRunner
Joshua Heskethef72b8a2013-08-12 11:17:14 +100024
Joshua Hesketh66f9f602013-08-14 11:28:10 +100025CONFIG_DIR = os.path.join(os.path.dirname(__file__), 'etc')
Matthew Oliver49c90632014-02-14 16:37:08 +110026with open(os.path.join(CONFIG_DIR, 'config.yaml'), 'r') as config_stream:
27 CONFIG = yaml.safe_load(config_stream)
Joshua Heskethef72b8a2013-08-12 11:17:14 +100028
Joshua Heskethef72b8a2013-08-12 11:17:14 +100029
Joshua Hesketh3fc12642014-01-29 16:32:02 +110030class TestZuulManager(testtools.TestCase):
Joshua Hesketh66f9f602013-08-14 11:28:10 +100031 def setUp(self):
Joshua Hesketh3fc12642014-01-29 16:32:02 +110032 super(TestZuulManager, self).setUp()
Joshua Hesketh66f9f602013-08-14 11:28:10 +100033 self.config = CONFIG
Joshua Hesketh66f9f602013-08-14 11:28:10 +100034 self.gearman_server = FakeGearmanServer(
35 self.config['zuul_server']['gearman_port'])
Joshua Hesketh0d9a8c02013-08-14 12:38:14 +100036 self.config['zuul_server']['gearman_port'] = self.gearman_server.port
37
Joshua Hesketh123625c2013-09-17 13:34:52 +100038 self.task = FakeRealDbUpgradeRunner(self.config,
Joshua Hesketh527966b2013-11-19 12:01:06 +110039 self.config['plugins'][0],
40 'test-worker-1', self)
Joshua Hesketh0d9a8c02013-08-14 12:38:14 +100041 self.tasks = dict(FakeRealDbUpgradeRunner_worker=self.task)
Joshua Hesketh66f9f602013-08-14 11:28:10 +100042
Joshua Hesketh4343b952013-11-20 12:11:55 +110043 self.gearman_manager = FakeZuulManager(self.config, self.tasks, self)
Joshua Hesketh66f9f602013-08-14 11:28:10 +100044
Joshua Hesketh3fc12642014-01-29 16:32:02 +110045 def tearDown(self):
46 super(TestZuulManager, self).tearDown()
47 self.gearman_server.shutdown()
48
49 def test_manager_function_registered(self):
Joshua Hesketh66f9f602013-08-14 11:28:10 +100050 """ Check the manager is set up correctly and registered with the
51 gearman server with an appropriate function """
Joshua Hesketh0d9a8c02013-08-14 12:38:14 +100052
53 # Give the gearman server up to 5 seconds to register the function
54 for x in range(500):
55 time.sleep(0.01)
56 if len(self.gearman_server.functions) > 0:
57 break
58
59 hostname = os.uname()[1]
60 function_name = 'stop:turbo-hipster-manager-%s' % hostname
61
62 self.assertIn(function_name, self.gearman_server.functions)
63
Joshua Hesketh3fc12642014-01-29 16:32:02 +110064 def test_task_registered_with_manager(self):
Joshua Hesketh0d9a8c02013-08-14 12:38:14 +100065 """ Check the FakeRealDbUpgradeRunner_worker task is registered """
66 self.assertIn('FakeRealDbUpgradeRunner_worker',
67 self.gearman_manager.tasks.keys())
68
Joshua Hesketh3fc12642014-01-29 16:32:02 +110069 def test_stop_task(self):
Joshua Hesketh0d9a8c02013-08-14 12:38:14 +100070 """ Check that the manager successfully stops a task when requested
71 """
Joshua Hesketh66f9f602013-08-14 11:28:10 +100072 pass