2013-07-19 12:52:45 -07:00
|
|
|
from mock import patch, MagicMock
|
|
|
|
|
2013-09-27 17:33:06 +01:00
|
|
|
from test_utils import CharmTestCase
|
2013-07-19 12:52:45 -07:00
|
|
|
|
2013-09-27 17:33:06 +01:00
|
|
|
import swift_storage_utils as utils
|
2013-07-19 12:52:45 -07:00
|
|
|
|
|
|
|
_reg = utils.register_configs
|
|
|
|
utils.register_configs = MagicMock()
|
|
|
|
|
2013-09-27 17:33:06 +01:00
|
|
|
import swift_storage_hooks as hooks
|
2013-07-19 12:52:45 -07:00
|
|
|
|
|
|
|
utils.register_configs = _reg
|
|
|
|
|
2013-09-27 17:33:06 +01:00
|
|
|
from 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',
|
|
|
|
# charmhelpers.core.host
|
|
|
|
'apt_update',
|
|
|
|
'apt_install',
|
|
|
|
# charmehelpers.contrib.openstack.utils
|
|
|
|
'configure_installation_source',
|
|
|
|
'openstack_upgrade_available',
|
|
|
|
# swift_storage_utils
|
|
|
|
'determine_block_devices',
|
|
|
|
'do_openstack_upgrade',
|
|
|
|
'ensure_swift_directories',
|
|
|
|
'fetch_swift_rings',
|
|
|
|
'save_script_rc',
|
|
|
|
'setup_storage',
|
|
|
|
'register_configs',
|
2013-09-27 17:33:06 +01:00
|
|
|
'execd_preinstall'
|
2013-07-19 12:52:45 -07:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class SwiftStorageRelationsTests(CharmTestCase):
|
|
|
|
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',
|
|
|
|
)
|
|
|
|
self.apt_update.assert_called()
|
|
|
|
self.apt_install.assert_called_with(PACKAGES, fatal=True)
|
|
|
|
|
|
|
|
self.setup_storage.assert_called()
|
2013-09-27 17:33:06 +01:00
|
|
|
self.execd_preinstall.assert_called()
|
2013-07-19 12:52:45 -07:00
|
|
|
|
|
|
|
def test_config_changed_no_upgrade_available(self):
|
|
|
|
self.openstack_upgrade_available.return_value = False
|
2013-07-19 14:21:28 -07:00
|
|
|
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)
|
|
|
|
|
|
|
|
def test_config_changed_upgrade_available(self):
|
|
|
|
self.openstack_upgrade_available.return_value = True
|
2013-07-19 14:21:28 -07:00
|
|
|
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)
|
|
|
|
|
|
|
|
def test_storage_joined_single_device(self):
|
|
|
|
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(
|
|
|
|
device='vdb', object_port=6000, account_port=6002,
|
|
|
|
zone=1, container_port=6001
|
|
|
|
)
|
|
|
|
|
|
|
|
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(
|
|
|
|
device='vdb:vdc:vdd', object_port=6000, account_port=6002,
|
|
|
|
zone=1, container_port=6001
|
|
|
|
)
|
|
|
|
|
|
|
|
@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')
|
2013-07-19 14:21:28 -07:00
|
|
|
@patch.object(hooks, 'install')
|
2013-07-19 13:44:37 -07:00
|
|
|
def test_main_hook_exists(self, _install, _argv):
|
2013-07-19 14:21:28 -07:00
|
|
|
hooks.main()
|
2013-07-19 13:44:37 -07:00
|
|
|
_install.assert_called()
|
|
|
|
|
|
|
|
@patch('sys.argv')
|
|
|
|
def test_main_hook_missing(self, _argv):
|
2013-07-19 14:21:28 -07:00
|
|
|
hooks.main()
|
2013-07-19 13:44:37 -07:00
|
|
|
self.log.assert_called()
|