@ -15,151 +15,93 @@
from __future__ import absolute_import
from __future__ import print_function
import unittest
import mock
import charms_openstack . test_utils as test_utils
import charm . openstack . barbican as barbican
class Helper ( unittest . TestCase ) :
class Helper ( test_utils . PatchHelper ) :
def setUp ( self ) :
self . _patches = { }
self . _patches_start = { }
# patch out the select_release to always return 'mitaka'
self . patch ( barbican . unitdata , ' kv ' )
_getter = mock . MagicMock ( )
_getter . get . return_value = barbican . BarbicanCharm . release
self . kv . return_value = _getter
def tearDown ( self ) :
for k , v in self . _patches . items ( ) :
v . stop ( )
setattr ( self , k , None )
self . _patches = None
self . _patches_start = None
def patch ( self , obj , attr , return_value = None , * * kwargs ) :
mocked = mock . patch . object ( obj , attr , * * kwargs )
self . _patches [ attr ] = mocked
started = mocked . start ( )
started . return_value = return_value
self . _patches_start [ attr ] = started
setattr ( self , attr , started )
class TestOpenStackBarbican ( Helper ) :
def test_install ( self ) :
self . patch ( barbican . BarbicanCharm , ' set_config_defined_certs_and_keys ' )
self . patch ( barbican . BarbicanCharm . singleton , ' install ' )
barbican . install ( )
self . install . assert_called_once_with ( )
super ( ) . setUp ( )
self . patch_release ( barbican . BarbicanCharm . release )
def test_setup_endpoint ( self ) :
self . patch ( barbican . BarbicanCharm , ' set_config_defined_certs_and_keys ' )
self . patch ( barbican . BarbicanCharm , ' service_type ' ,
new_callable = mock . PropertyMock )
self . patch ( barbican . BarbicanCharm , ' region ' ,
new_callable = mock . PropertyMock )
self . patch ( barbican . BarbicanCharm , ' public_url ' ,
new_callable = mock . PropertyMock )
self . patch ( barbican . BarbicanCharm , ' internal_url ' ,
new_callable = mock . PropertyMock )
self . patch ( barbican . BarbicanCharm , ' admin_url ' ,
new_callable = mock . PropertyMock )
self . service_type . return_value = ' type1 '
self . region . return_value = ' region1 '
self . public_url . return_value = ' public_url '
self . internal_url . return_value = ' internal_url '
self . admin_url . return_value = ' admin_url '
keystone = mock . MagicMock ( )
barbican . setup_endpoint ( keystone )
keystone . register_endpoints . assert_called_once_with (
' type1 ' , ' region1 ' , ' public_url ' , ' internal_url ' , ' admin_url ' )
def test_render_configs ( self ) :
self . patch ( barbican . BarbicanCharm , ' set_config_defined_certs_and_keys ' )
self . patch ( barbican . BarbicanCharm . singleton , ' render_with_interfaces ' )
barbican . render_configs ( ' interfaces-list ' )
self . render_with_interfaces . assert_called_once_with (
' interfaces-list ' )
class TestCustomProperties ( Helper ) :
def test_validate_keystone_api_version ( self ) :
config = mock . MagicMock ( )
for v in [ ' 2 ' , ' 3 ' , ' none ' ] :
config . keystone_api_version = v
barbican . validate_keystone_api_version ( config )
# ensure that it fails
with self . assertRaises ( ValueError ) :
config . keystone_api_version = ' fail-me '
barbican . validate_keystone_api_version ( config )
class TestBarbicanConfigurationAdapter ( Helper ) :
@mock . patch ( ' charmhelpers.core.hookenv.config ' )
def test_barbican_configuration_adapter ( self , config ) :
self . patch (
barbican . charms_openstack . adapters . APIConfigurationAdapter ,
' get_network_addresses ' )
reply = {
' keystone-api-version ' : ' 2 ' ,
}
config . side_effect = lambda : reply
# Make one with no errors, api version 2
a = barbican . BarbicanConfigurationAdapter ( )
self . assertEqual ( a . barbican_api_keystone_pipeline ,
def test_barbican_api_keystone_pipeline ( self ) :
config = mock . MagicMock ( )
config . keystone_api_version = ' 2 '
self . assertEqual ( barbican . barbican_api_keystone_pipeline ( config ) ,
' cors keystone_authtoken context apiapp ' )
self . assertEqual ( a . barbican_api_pipeline ,
' cors keystone_authtoken context apiapp ' )
# Now test it with api version 3
reply [ ' keystone-api-version ' ] = ' 3 '
a = barbican . BarbicanConfigurationAdapter ( )
self . assertEqual ( a . barbican_api_keystone_pipeline ,
' cors keystone_v3_authtoken context apiapp ' )
self . assertEqual ( a . barbican_api_pipeline ,
config . keystone_api_version = ' '
self . assertEqual ( barbican . barbican_api_keystone_pipeline ( config ) ,
' cors keystone_v3_authtoken context apiapp ' )
# and a 'none' version
reply [ ' keystone-api-version ' ] = ' none '
a = barbican . BarbicanConfigurationAdapter ( )
self . assertEqual ( a . barbican_api_keystone_pipeline ,
def test_barbican_api_pipeline ( self ) :
config = mock . MagicMock ( )
config . keystone_api_version = ' 2 '
self . assertEqual ( barbican . barbican_api_pipeline ( config ) ,
' cors keystone_authtoken context apiapp ' )
config . keystone_api_version = ' 3 '
self . assertEqual ( barbican . barbican_api_pipeline ( config ) ,
' cors keystone_v3_authtoken context apiapp ' )
self . assertEqual ( a . barbican_api_pipeline ,
config . keystone_api_version = ' none '
self . assertEqual ( barbican . barbican_api_pipeline ( config ) ,
' cors unauthenticated-context apiapp ' )
# finally, try to create an invalid one.
reply [ ' keystone-api-version ' ] = None
with self . assertRaises ( ValueError ) :
a = barbican . BarbicanConfigurationAdapter ( )
def test_barbican_api_keystone_audit_pipeline ( self ) :
config = mock . MagicMock ( )
config . keystone_api_version = ' 2 '
self . assertEqual ( barbican . barbican_api_keystone_audit_pipeline ( config ) ,
' keystone_authtoken context audit apiapp ' )
config . keystone_api_version = ' '
self . assertEqual ( barbican . barbican_api_keystone_audit_pipeline ( config ) ,
' keystone_v3_authtoken context audit apiapp ' )
class TestBarbicanAdapters ( Helper ) :
@mock . patch ( ' charmhelpers.core.hookenv.config ' )
def test_barbican_adapters ( self , config ) :
reply = {
' keystone-api-version ' : ' 2 ' ,
# for the charms.openstack code, which breaks if we don't have:
' os-public-hostname ' : ' host ' ,
' os-internal-hostname ' : ' internal ' ,
' os-admin-hostname ' : ' admin ' ,
class TestHSMProperties ( Helper ) :
def setUp ( self ) :
super ( ) . setUp ( )
self . data_none = { }
self . data_set = {
' library_path ' : ' a-path ' ,
' login ' : ' a-login ' ,
' slot_id ' : ' a-slot_id ' ,
}
def cf ( key = None ) :
if key is not None :
return reply [ key ]
return reply
def test_library_path ( self ) :
hsm = mock . MagicMock ( )
hsm . relation . plugin_data = self . data_none
self . assertEqual ( barbican . library_path ( hsm ) , ' ' )
hsm . relation . plugin_data = self . data_set
self . assertEqual ( barbican . library_path ( hsm ) , ' a-path ' )
config . side_effect = cf
amqp_relation = mock . MagicMock ( )
amqp_relation . relation_name = ' amqp '
shared_db_relation = mock . MagicMock ( )
shared_db_relation . relation_name = ' shared_db '
other_relation = mock . MagicMock ( )
other_relation . relation_name = ' other '
other_relation . thingy = ' help '
# verify that the class is created with a BarbicanConfigurationAdapter
b = barbican . BarbicanAdapters ( [ amqp_relation ,
shared_db_relation ,
other_relation ] )
# ensure that the relevant things got put on.
self . assertTrue (
isinstance (
b . other ,
barbican . charms_openstack . adapters . OpenStackRelationAdapter ) )
self . assertTrue ( isinstance ( b . options ,
barbican . BarbicanConfigurationAdapter ) )
def test_login ( self ) :
hsm = mock . MagicMock ( )
hsm . relation . plugin_data = self . data_none
self . assertEqual ( barbican . login ( hsm ) , ' ' )
hsm . relation . plugin_data = self . data_set
self . assertEqual ( barbican . login ( hsm ) , ' a-login ' )
def test_slot_id ( self ) :
hsm = mock . MagicMock ( )
hsm . relation . plugin_data = self . data_none
self . assertEqual ( barbican . slot_id ( hsm ) , ' ' )
hsm . relation . plugin_data = self . data_set
self . assertEqual ( barbican . slot_id ( hsm ) , ' a-slot_id ' )
class TestBarbicanCharm ( Helper ) :
@ -171,7 +113,7 @@ class TestBarbicanCharm(Helper):
' login ' : ' 1234 ' ,
' slot_id ' : ' slot1 '
}
self . patch ( barbican . hookenv , ' config ' )
self . patch_object ( barbican . hookenv , ' config ' )
config = {
' mkek-key-length ' : 5 ,
' label-mkek ' : ' the-label '
@ -183,8 +125,8 @@ class TestBarbicanCharm(Helper):
return config
self . config . side_effect = cf
self . patch ( barbican . subprocess , ' check_call ' )
self . patch ( barbican . hookenv , ' log ' )
self . patch_object ( barbican . subprocess , ' check_call ' )
self . patch_object ( barbican . hookenv , ' log ' )
# try generating a an mkek with no failure
c = barbican . BarbicanCharm ( )
c . action_generate_mkek ( hsm )
@ -218,7 +160,7 @@ class TestBarbicanCharm(Helper):
' login ' : ' 1234 ' ,
' slot_id ' : ' slot1 '
}
self . patch ( barbican . hookenv , ' config ' )
self . patch_object ( barbican . hookenv , ' config ' )
config = {
' hmac-key-length ' : 5 ,
' label-hmac ' : ' the-label '
@ -230,8 +172,8 @@ class TestBarbicanCharm(Helper):
return config
self . config . side_effect = cf
self . patch ( barbican . subprocess , ' check_call ' )
self . patch ( barbican . hookenv , ' log ' )
self . patch_object ( barbican . subprocess , ' check_call ' )
self . patch_object ( barbican . hookenv , ' log ' )
# try generating a an hmac with no failure
c = barbican . BarbicanCharm ( )
c . action_generate_hmac ( hsm )