charm-cinder/unit_tests/test_cluster_hooks.py

116 lines
3.8 KiB
Python
Raw Normal View History

# Copyright 2016 Canonical Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
from unittest.mock import patch, call, MagicMock
2013-10-17 14:48:08 -07:00
from test_utils import (
CharmTestCase,
RESTART_MAP,
)
2015-09-14 14:39:00 +01:00
os.environ['JUJU_UNIT_NAME'] = 'cinder'
# python-apt is not installed as part of test-requirements but is imported by
# some charmhelpers modules so create a fake import.
mock_apt = MagicMock()
sys.modules['apt'] = mock_apt
mock_apt.apt_pkg = MagicMock()
2015-09-14 14:39:00 +01:00
with patch('cinder_utils.register_configs') as register_configs:
with patch('cinder_utils.restart_map') as restart_map:
restart_map.return_value = RESTART_MAP
2015-09-14 14:39:00 +01:00
import cinder_hooks as hooks
hooks.hooks._config_save = False
2013-10-17 14:48:08 -07:00
TO_PATCH = [
# cinder_utils
'determine_packages',
'juju_log',
'lsb_release',
'migrate_database',
2014-04-07 14:09:04 +01:00
'configure_lvm_storage',
2013-10-17 14:48:08 -07:00
'register_configs',
'service_enabled',
'CONFIGS',
'CLUSTER_RES',
# charmhelpers.core.hookenv
'config',
'relation_set',
2013-10-19 17:58:37 +01:00
'relation_get',
'relation_ids',
2013-10-17 14:48:08 -07:00
'service_name',
# charmhelpers.core.host
'apt_install',
'apt_update',
# charmhelpers.contrib.openstack.openstack_utils
'configure_installation_source',
# charmhelpers.contrib.hahelpers.cluster_utils
2015-03-10 09:06:37 +00:00
'is_elected_leader',
# charmhelpers.contrib.network.ip
'get_relation_ip',
2013-10-17 14:48:08 -07:00
]
class TestClusterHooks(CharmTestCase):
2013-10-17 14:48:08 -07:00
def setUp(self):
super(TestClusterHooks, self).setUp(hooks, TO_PATCH)
self.config.side_effect = self.test_config.get
2013-10-17 14:48:08 -07:00
@patch.object(hooks, 'check_local_db_actions_complete',
lambda *args, **kwargs: None)
2013-10-17 14:48:08 -07:00
@patch('charmhelpers.core.host.service')
2015-06-19 16:29:55 +00:00
@patch('charmhelpers.core.host.path_hash')
def test_cluster_hook(self, path_hash, service):
2014-01-15 16:30:21 +01:00
'Ensure API restart before haproxy on cluster changed'
2013-10-17 14:48:08 -07:00
# set first hash lookup on all files
side_effects = []
# set first hash lookup on all configs in restart_on_change
[side_effects.append('foo') for f in RESTART_MAP.keys()]
# set second hash lookup on all configs in restart_on_change
[side_effects.append('bar') for f in RESTART_MAP.keys()]
2015-06-19 16:29:55 +00:00
path_hash.side_effect = side_effects
2013-10-17 14:48:08 -07:00
hooks.hooks.execute(['hooks/cluster-relation-changed'])
ex = [
2014-02-17 13:06:39 +01:00
call('stop', 'cinder-api'),
call('start', 'cinder-api'),
call('stop', 'cinder-volume'),
2014-02-17 13:06:39 +01:00
call('start', 'cinder-volume'),
call('stop', 'cinder-scheduler'),
2014-02-17 13:06:39 +01:00
call('start', 'cinder-scheduler'),
call('stop', 'haproxy'),
2014-02-17 13:06:39 +01:00
call('start', 'haproxy'),
call('stop', 'apache2'),
call('start', 'apache2'),
]
self.assertEqual(ex, service.call_args_list)
2013-10-17 14:48:08 -07:00
2013-10-19 17:58:37 +01:00
@patch.object(hooks, 'identity_joined')
def test_ha_changed_clustered(self, joined):
2013-10-19 17:58:37 +01:00
self.relation_get.return_value = True
self.relation_ids.return_value = ['identity:0']
hooks.hooks.execute(['hooks/ha-relation-changed'])
joined.assert_called_with(rid='identity:0')
def test_ha_changed_not_clustered(self):
2014-01-15 16:30:21 +01:00
'Ensure ha_changed exits early if not yet clustered'
2013-10-19 17:58:37 +01:00
self.relation_get.return_value = None
hooks.hooks.execute(['hooks/ha-relation-changed'])
2013-10-20 11:31:12 -07:00
self.assertTrue(self.juju_log.called)