2015-11-03 14:03:25 +00:00
|
|
|
from mock import patch
|
2013-07-19 12:52:45 -07:00
|
|
|
|
2014-10-30 00:52:15 -05:00
|
|
|
from test_utils import CharmTestCase, patch_open
|
2013-07-19 12:52:45 -07:00
|
|
|
|
2015-10-13 10:35:23 +00:00
|
|
|
with patch('hooks.lib.misc_utils.is_paused') as is_paused:
|
2015-11-03 14:03:25 +00:00
|
|
|
with patch('hooks.lib.swift_storage_utils.register_configs') as _:
|
|
|
|
import hooks.swift_storage_hooks as hooks
|
2013-07-19 12:52:45 -07:00
|
|
|
|
2015-07-17 16:52:38 +01:00
|
|
|
from lib.swift_storage_utils import PACKAGES
|
2013-07-19 12:52:45 -07:00
|
|
|
|
|
|
|
TO_PATCH = [
|
|
|
|
'CONFIGS',
|
|
|
|
# charmhelpers.core.hookenv
|
|
|
|
'Hooks',
|
|
|
|
'config',
|
|
|
|
'log',
|
|
|
|
'relation_set',
|
|
|
|
'relation_get',
|
2014-10-30 00:52:15 -05:00
|
|
|
'relations_of_type',
|
2013-07-19 12:52:45 -07:00
|
|
|
# charmhelpers.core.host
|
|
|
|
'apt_update',
|
|
|
|
'apt_install',
|
2014-10-07 09:37:20 +01:00
|
|
|
'filter_installed_packages',
|
2013-07-19 12:52:45 -07:00
|
|
|
# charmehelpers.contrib.openstack.utils
|
|
|
|
'configure_installation_source',
|
|
|
|
'openstack_upgrade_available',
|
|
|
|
# swift_storage_utils
|
|
|
|
'determine_block_devices',
|
|
|
|
'do_openstack_upgrade',
|
|
|
|
'ensure_swift_directories',
|
2015-01-09 10:21:49 +00:00
|
|
|
'execd_preinstall',
|
2013-07-19 12:52:45 -07:00
|
|
|
'fetch_swift_rings',
|
|
|
|
'save_script_rc',
|
2015-01-09 10:21:49 +00:00
|
|
|
'setup_rsync',
|
2013-07-19 12:52:45 -07:00
|
|
|
'setup_storage',
|
|
|
|
'register_configs',
|
2015-01-27 18:39:28 -08:00
|
|
|
'update_nrpe_config',
|
|
|
|
'get_ipv6_addr',
|
2015-10-08 12:17:44 +00:00
|
|
|
'status_set',
|
2015-10-09 03:58:08 +00:00
|
|
|
'set_os_workload_status',
|
2013-07-19 12:52:45 -07:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class SwiftStorageRelationsTests(CharmTestCase):
|
2014-06-19 09:40:58 +01:00
|
|
|
|
2013-07-19 12:52:45 -07:00
|
|
|
def setUp(self):
|
2013-07-19 14:21:28 -07:00
|
|
|
super(SwiftStorageRelationsTests, self).setUp(hooks,
|
2013-07-19 12:52:45 -07:00
|
|
|
TO_PATCH)
|
|
|
|
self.config.side_effect = self.test_config.get
|
|
|
|
self.relation_get.side_effect = self.test_relation.get
|
|
|
|
|
|
|
|
def test_install_hook(self):
|
|
|
|
self.test_config.set('openstack-origin', 'cloud:precise-havana')
|
2013-07-19 14:21:28 -07:00
|
|
|
hooks.install()
|
2013-07-19 12:52:45 -07:00
|
|
|
self.configure_installation_source.assert_called_with(
|
|
|
|
'cloud:precise-havana',
|
|
|
|
)
|
2015-10-10 15:14:11 -07:00
|
|
|
self.assertTrue(self.apt_update.called)
|
2013-07-19 12:52:45 -07:00
|
|
|
self.apt_install.assert_called_with(PACKAGES, fatal=True)
|
|
|
|
|
2015-10-10 15:14:11 -07:00
|
|
|
self.assertTrue(self.setup_storage.called)
|
|
|
|
self.assertTrue(self.execd_preinstall.called)
|
2013-07-19 12:52:45 -07:00
|
|
|
|
2015-10-15 15:05:42 -04:00
|
|
|
def test_config_changed_no_upgrade_available(self):
|
2013-07-19 12:52:45 -07:00
|
|
|
self.openstack_upgrade_available.return_value = False
|
2014-10-30 00:52:15 -05:00
|
|
|
self.relations_of_type.return_value = False
|
|
|
|
with patch_open() as (_open, _file):
|
|
|
|
_file.read.return_value = "foo"
|
|
|
|
hooks.config_changed()
|
2013-07-19 12:52:45 -07:00
|
|
|
self.assertFalse(self.do_openstack_upgrade.called)
|
|
|
|
self.assertTrue(self.CONFIGS.write_all.called)
|
2015-01-09 10:21:49 +00:00
|
|
|
self.assertTrue(self.setup_rsync.called)
|
2013-07-19 12:52:45 -07:00
|
|
|
|
2015-10-15 15:05:42 -04:00
|
|
|
def test_config_changed_upgrade_available(self):
|
2013-07-19 12:52:45 -07:00
|
|
|
self.openstack_upgrade_available.return_value = True
|
2014-10-30 00:52:15 -05:00
|
|
|
self.relations_of_type.return_value = False
|
|
|
|
with patch_open() as (_open, _file):
|
|
|
|
_file.read.return_value = "foo"
|
|
|
|
hooks.config_changed()
|
2013-07-19 12:52:45 -07:00
|
|
|
self.assertTrue(self.do_openstack_upgrade.called)
|
|
|
|
self.assertTrue(self.CONFIGS.write_all.called)
|
|
|
|
|
2015-10-15 15:05:42 -04:00
|
|
|
def test_config_changed_with_openstack_upgrade_action(self):
|
2015-09-23 10:12:53 -04:00
|
|
|
self.openstack_upgrade_available.return_value = True
|
|
|
|
self.test_config.set('action-managed-upgrade', True)
|
|
|
|
|
|
|
|
with patch_open() as (_open, _file):
|
|
|
|
_file.read.return_value = "foo"
|
|
|
|
hooks.config_changed()
|
|
|
|
|
|
|
|
self.assertFalse(self.do_openstack_upgrade.called)
|
|
|
|
|
2015-10-15 15:05:42 -04:00
|
|
|
def test_config_changed_nrpe_master(self):
|
2015-01-27 18:39:28 -08:00
|
|
|
self.openstack_upgrade_available.return_value = False
|
2015-02-03 04:01:59 -08:00
|
|
|
self.relations_of_type.return_value = True
|
|
|
|
with patch_open() as (_open, _file):
|
|
|
|
_file.read.return_value = "foo"
|
|
|
|
hooks.config_changed()
|
2015-01-27 18:39:28 -08:00
|
|
|
self.assertTrue(self.CONFIGS.write_all.called)
|
2015-02-03 04:01:59 -08:00
|
|
|
self.assertTrue(self.setup_rsync.called)
|
2015-01-27 18:39:28 -08:00
|
|
|
self.assertTrue(self.update_nrpe_config.called)
|
|
|
|
|
2015-02-25 09:41:50 +00:00
|
|
|
@patch.object(hooks, 'assert_charm_supports_ipv6')
|
2015-10-15 15:05:42 -04:00
|
|
|
def test_config_changed_ipv6(self, mock_assert_charm_supports_ipv6):
|
2015-01-27 18:39:28 -08:00
|
|
|
self.test_config.set('prefer-ipv6', True)
|
|
|
|
self.openstack_upgrade_available.return_value = False
|
|
|
|
self.relations_of_type.return_value = False
|
|
|
|
with patch_open() as (_open, _file):
|
|
|
|
_file.read.return_value = "foo"
|
2015-02-25 09:41:50 +00:00
|
|
|
hooks.config_changed()
|
|
|
|
self.assertTrue(self.CONFIGS.write_all.called)
|
|
|
|
self.assertTrue(self.setup_rsync.called)
|
2015-01-27 18:39:28 -08:00
|
|
|
|
2014-10-07 09:37:20 +01:00
|
|
|
def test_upgrade_charm(self):
|
2015-02-03 04:01:59 -08:00
|
|
|
self.filter_installed_packages.return_value = [
|
|
|
|
'python-psutil']
|
2014-10-07 09:37:20 +01:00
|
|
|
hooks.upgrade_charm()
|
2015-02-03 04:01:59 -08:00
|
|
|
self.apt_install.assert_called_with([
|
|
|
|
'python-psutil'], fatal=True)
|
2015-01-09 10:21:49 +00:00
|
|
|
self.assertTrue(self.update_nrpe_config.called)
|
2014-10-07 09:37:20 +01:00
|
|
|
|
2013-07-19 12:52:45 -07:00
|
|
|
def test_storage_joined_single_device(self):
|
2015-02-03 04:01:59 -08:00
|
|
|
self.determine_block_devices.return_value = [
|
|
|
|
'/dev/vdb']
|
2013-07-19 14:21:28 -07:00
|
|
|
hooks.swift_storage_relation_joined()
|
2013-07-19 12:52:45 -07:00
|
|
|
self.relation_set.assert_called_with(
|
2015-10-15 15:05:42 -04:00
|
|
|
device='vdb', object_port=6000, account_port=6002,
|
|
|
|
zone=1, container_port=6001
|
2013-07-19 12:52:45 -07:00
|
|
|
)
|
|
|
|
|
2015-02-03 04:01:59 -08:00
|
|
|
def test_storage_joined_ipv6(self):
|
2015-01-27 18:39:28 -08:00
|
|
|
self.determine_block_devices.return_value = ['/dev/vdb']
|
|
|
|
self.test_config.set('prefer-ipv6', True)
|
2015-03-05 10:24:01 +08:00
|
|
|
self.get_ipv6_addr.return_value = ['2001:db8:1::1']
|
2015-02-03 04:01:59 -08:00
|
|
|
hooks.swift_storage_relation_joined()
|
2015-01-28 17:37:50 -08:00
|
|
|
args = {
|
2015-02-03 04:01:59 -08:00
|
|
|
'device': 'vdb', 'object_port': 6000,
|
|
|
|
'account_port': 6002, 'zone': 1, 'container_port': 6001,
|
2015-03-05 10:24:01 +08:00
|
|
|
'private-address': '2001:db8:1::1',
|
2015-01-28 17:37:50 -08:00
|
|
|
}
|
2015-10-15 15:05:42 -04:00
|
|
|
self.relation_set.assert_called_with(**args)
|
2015-01-27 18:39:28 -08:00
|
|
|
|
2013-07-19 12:52:45 -07:00
|
|
|
def test_storage_joined_multi_device(self):
|
|
|
|
self.determine_block_devices.return_value = ['/dev/vdb', '/dev/vdc',
|
|
|
|
'/dev/vdd']
|
2013-07-19 14:21:28 -07:00
|
|
|
hooks.swift_storage_relation_joined()
|
2013-07-19 12:52:45 -07:00
|
|
|
self.relation_set.assert_called_with(
|
2015-10-15 15:05:42 -04:00
|
|
|
device='vdb:vdc:vdd', object_port=6000, account_port=6002,
|
|
|
|
zone=1, container_port=6001
|
2013-07-19 12:52:45 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
@patch('sys.exit')
|
|
|
|
def test_storage_changed_missing_relation_data(self, exit):
|
2013-07-19 14:21:28 -07:00
|
|
|
hooks.swift_storage_relation_changed()
|
2013-07-19 12:52:45 -07:00
|
|
|
exit.assert_called_with(0)
|
|
|
|
|
|
|
|
def test_storage_changed_with_relation_data(self):
|
|
|
|
self.test_relation.set({
|
|
|
|
'swift_hash': 'foo_hash',
|
|
|
|
'rings_url': 'http://swift-proxy.com/rings/',
|
|
|
|
})
|
2013-07-19 14:21:28 -07:00
|
|
|
hooks.swift_storage_relation_changed()
|
2013-07-19 12:52:45 -07:00
|
|
|
self.CONFIGS.write.assert_called_with('/etc/swift/swift.conf')
|
|
|
|
self.fetch_swift_rings.assert_called_with(
|
|
|
|
'http://swift-proxy.com/rings/'
|
|
|
|
)
|
2013-07-19 13:44:37 -07:00
|
|
|
|
|
|
|
@patch('sys.argv')
|
|
|
|
def test_main_hook_missing(self, _argv):
|
2013-07-19 14:21:28 -07:00
|
|
|
hooks.main()
|
2015-10-10 15:14:11 -07:00
|
|
|
self.assertTrue(self.log.called)
|