2016-06-22 15:50:22 +01:00
|
|
|
# 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.
|
|
|
|
|
2014-02-12 11:55:14 +01:00
|
|
|
import os
|
2016-03-22 19:30:20 +00:00
|
|
|
import sys
|
|
|
|
|
2020-06-08 22:35:37 +02:00
|
|
|
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'
|
|
|
|
|
2016-03-22 19:30:20 +00:00
|
|
|
# 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:
|
2015-09-14 14:56:38 +01:00
|
|
|
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',
|
2014-07-03 10:57:07 +01:00
|
|
|
# charmhelpers.contrib.network.ip
|
2017-04-26 13:42:21 -07:00
|
|
|
'get_relation_ip',
|
2013-10-17 14:48:08 -07:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class TestClusterHooks(CharmTestCase):
|
2014-03-31 12:11:01 +01:00
|
|
|
|
2013-10-17 14:48:08 -07:00
|
|
|
def setUp(self):
|
|
|
|
super(TestClusterHooks, self).setUp(hooks, TO_PATCH)
|
2014-10-01 21:42:34 +01:00
|
|
|
self.config.side_effect = self.test_config.get
|
2013-10-17 14:48:08 -07:00
|
|
|
|
2017-08-23 13:47:14 +01: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'),
|
2016-04-12 14:12:38 +00:00
|
|
|
call('stop', 'cinder-volume'),
|
2014-02-17 13:06:39 +01:00
|
|
|
call('start', 'cinder-volume'),
|
2016-04-12 14:12:38 +00:00
|
|
|
call('stop', 'cinder-scheduler'),
|
2014-02-17 13:06:39 +01:00
|
|
|
call('start', 'cinder-scheduler'),
|
2016-04-12 14:12:38 +00:00
|
|
|
call('stop', 'haproxy'),
|
2014-02-17 13:06:39 +01:00
|
|
|
call('start', 'haproxy'),
|
2016-04-12 14:12:38 +00:00
|
|
|
call('stop', 'apache2'),
|
|
|
|
call('start', 'apache2'),
|
|
|
|
]
|
2017-09-13 16:24:18 +08:00
|
|
|
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')
|
2014-09-26 11:01:31 +01:00
|
|
|
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)
|