<s> import mock <EOL> from keystoneclient import exceptions <EOL> from keystoneclient . v2_0 import client as keystone_client <EOL> from oslotest import mockpatch <EOL> from cloudferry import cfglib <EOL> from cloudferry . lib . os . identity import keystone <EOL> from cloudferry . lib . utils import utils <EOL> from tests import test <EOL> FAKE_CONFIG = utils . ext_dict ( <EOL> cloud = utils . ext_dict ( { '<STR_LIT:user>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False } ) , <EOL> migrate = utils . ext_dict ( { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:5> , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False } ) , <EOL> mail = utils . ext_dict ( { '<STR_LIT>' : '<STR_LIT:->' } ) ) <EOL> @ mock . patch ( "<STR_LIT>" , mock . MagicMock ( ) ) <EOL> class KeystoneIdentityTestCase ( test . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( KeystoneIdentityTestCase , self ) . setUp ( ) <EOL> self . mock_client = mock . MagicMock ( ) <EOL> self . kc_patch = mockpatch . PatchObject ( keystone_client , '<STR_LIT>' , <EOL> new = self . mock_client ) <EOL> self . useFixture ( self . kc_patch ) <EOL> self . fake_cloud = mock . Mock ( ) <EOL> self . fake_cloud . mysql_connector = mock . Mock ( ) <EOL> self . keystone_client = keystone . KeystoneIdentity ( FAKE_CONFIG , <EOL> self . fake_cloud ) <EOL> self . fake_tenant_0 = mock . Mock ( spec = keystone_client . tenants . Tenant ) <EOL> self . fake_tenant_0 . name = '<STR_LIT>' <EOL> self . fake_tenant_0 . description = '<STR_LIT>' <EOL> self . fake_tenant_0 . id = '<STR_LIT>' <EOL> self . fake_tenant_1 = mock . Mock ( spec = keystone_client . tenants . Tenant ) <EOL> self . fake_tenant_1 . name = '<STR_LIT>' <EOL> self . fake_tenant_1 . description = '<STR_LIT>' <EOL> self . fake_tenant_1 . id = '<STR_LIT>' <EOL> self . fake_user_0 = mock . Mock ( spec = keystone_client . users . User ) <EOL> self . fake_user_0 . name = '<STR_LIT>' <EOL> self . fake_user_0 . id = '<STR_LIT>' <EOL> self . fake_user_0 . tenantId = self . fake_tenant_0 . id <EOL> self . fake_user_0 . email = '<STR_LIT>' <EOL> self . fake_user_1 = mock . Mock ( spec = keystone_client . users . User ) <EOL> self . fake_user_1 . name = '<STR_LIT>' <EOL> self . fake_user_1 . id = '<STR_LIT>' <EOL> self . fake_user_1 . tenantId = self . fake_tenant_1 . id <EOL> self . fake_user_1 . email = '<STR_LIT>' <EOL> self . fake_role_0 = mock . Mock ( spec = keystone_client . roles . Role ) <EOL> self . fake_role_0 . name = '<STR_LIT>' <EOL> self . fake_role_0 . id = '<STR_LIT>' <EOL> self . fake_role_1 = mock . Mock ( spec = keystone_client . roles . Role ) <EOL> self . fake_role_1 . name = '<STR_LIT>' <EOL> self . fake_role_1 . id = '<STR_LIT>' <EOL> cfglib . init_config ( ) <EOL> cfglib . CONF . src . user = '<STR_LIT>' <EOL> cfglib . CONF . dst . user = '<STR_LIT>' <EOL> self . fake_src_admin_user = mock . Mock ( ) <EOL> self . fake_src_admin_user . name = '<STR_LIT>' <EOL> self . fake_dst_admin_user = mock . Mock ( ) <EOL> self . fake_dst_admin_user . name = '<STR_LIT>' <EOL> self . fake_same_user = mock . Mock ( ) <EOL> self . fake_same_user . id = '<STR_LIT>' <EOL> self . fake_same_user . name = '<STR_LIT>' <EOL> src_keystone = mock . Mock ( ) <EOL> dst_keystone = mock . Mock ( ) <EOL> self . fake_src_keystone = src_keystone <EOL> self . fake_dst_keystone = dst_keystone <EOL> src_keystone . keystone_client . users . list . return_value = [ <EOL> self . fake_user_0 , <EOL> self . fake_same_user , <EOL> self . fake_src_admin_user ] <EOL> dst_keystone . keystone_client . users . list . return_value = [ <EOL> self . fake_user_1 , <EOL> self . fake_same_user , <EOL> self . fake_dst_admin_user ] <EOL> src_keystone . keystone_client . users . get . side_effect = self . mock_user_get <EOL> dst_keystone . keystone_client . users . get . side_effect = self . mock_user_get <EOL> src_keystone . try_get_user_by_name . side_effect = self . mock_user_get <EOL> dst_keystone . try_get_user_by_name . side_effect = self . mock_user_get <EOL> def test_get_client_generates_new_token ( self ) : <EOL> client1 = self . keystone_client . keystone_client <EOL> client2 = self . keystone_client . keystone_client <EOL> self . assertFalse ( client1 == client2 ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> @ mock . patch . object ( keystone_client , "<STR_LIT>" ) <EOL> def test_retry_when_get_client_with_exception ( self , mock_call , <EOL> sleep_mock ) : <EOL> """<STR_LIT>""" <EOL> mock_call . side_effect = exceptions . Unauthorized <EOL> self . assertRaises ( exceptions . Unauthorized , <EOL> self . keystone_client . get_client ) <EOL> self . assertFalse ( sleep_mock . called ) <EOL> mock_call . reset_mock ( ) <EOL> mock_call . side_effect = exceptions . AuthorizationFailure <EOL> self . assertRaises ( exceptions . AuthorizationFailure , <EOL> self . keystone_client . get_client ) <EOL> self . assertTrue ( sleep_mock . called ) <EOL> def test_get_tenants_list ( self ) : <EOL> fake_tenants_list = [ self . fake_tenant_0 , self . fake_tenant_1 ] <EOL> self . mock_client ( ) . tenants . list . return_value = fake_tenants_list <EOL> tenant_list = self . keystone_client . get_tenants_list ( ) <EOL> self . assertEqual ( fake_tenants_list , tenant_list ) <EOL> def test_get_tenant_by_name ( self ) : <EOL> fake_tenants_list = [ self . fake_tenant_0 , self . fake_tenant_1 ] <EOL> self . mock_client ( ) . tenants . list . return_value = fake_tenants_list <EOL> tenant = self . keystone_client . get_tenant_by_name ( '<STR_LIT>' ) <EOL> self . assertEqual ( self . fake_tenant_0 , tenant ) <EOL> def test_get_tenant_by_name_default ( self ) : <EOL> self . mock_client ( ) . tenants . list . return_value = [ ] <EOL> self . assertRaises ( <EOL> exceptions . NotFound , <EOL> self . keystone_client . get_tenant_by_name , '<STR_LIT>' ) <EOL> def test_get_users_list ( self ) : <EOL> fake_users_list = [ self . fake_user_0 , self . fake_user_1 ] <EOL> self . mock_client ( ) . users . list . return_value = fake_users_list <EOL> users_list = self . keystone_client . get_users_list ( ) <EOL> self . assertEqual ( fake_users_list , users_list ) <EOL> def test_get_roles_list ( self ) : <EOL> fake_roles_list = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . mock_client ( ) . roles . list . return_value = fake_roles_list <EOL> roles_list = self . keystone_client . get_roles_list ( ) <EOL> self . assertEqual ( fake_roles_list , roles_list ) <EOL> def test_create_role ( self ) : <EOL> self . keystone_client . create_role ( '<STR_LIT>' ) <EOL> self . mock_client ( ) . roles . create . assert_called_once_with ( '<STR_LIT>' ) <EOL> def test_create_tenant ( self ) : <EOL> self . keystone_client . create_tenant ( '<STR_LIT>' ) <EOL> test_args = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:description>' : None } <EOL> self . mock_client ( ) . tenants . create . assert_called_once_with ( ** test_args ) <EOL> def test_create_user ( self ) : <EOL> self . keystone_client . create_user ( '<STR_LIT>' ) <EOL> test_args = { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : None , <EOL> '<STR_LIT:email>' : None , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : None } <EOL> self . mock_client ( ) . users . create . assert_called_once_with ( ** test_args ) <EOL> def test_update_tenant ( self ) : <EOL> self . keystone_client . update_tenant ( tenant_id = '<STR_LIT>' , <EOL> tenant_name = '<STR_LIT>' ) <EOL> test_args = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT:description>' : None } <EOL> self . mock_client ( ) . tenants . update . assert_called_once_with ( '<STR_LIT>' , <EOL> ** test_args ) <EOL> def test_update_user ( self ) : <EOL> self . keystone_client . update_user ( self . fake_user_0 , <EOL> name = '<STR_LIT>' , <EOL> email = '<STR_LIT>' , <EOL> enabled = False ) <EOL> test_args = { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False } <EOL> self . mock_client ( ) . users . update . assert_called_once_with ( <EOL> self . fake_user_0 , <EOL> ** test_args ) <EOL> def test_auth_token_from_user ( self ) : <EOL> fake_auth_token = '<STR_LIT>' <EOL> self . mock_client ( ) . auth_token = fake_auth_token <EOL> self . assertEquals ( fake_auth_token , <EOL> self . keystone_client . get_auth_token_from_user ( ) ) <EOL> def test_read_info ( self ) : <EOL> fake_tenants_list = [ self . fake_tenant_0 , self . fake_tenant_1 ] <EOL> fake_users_list = [ self . fake_user_0 , self . fake_user_1 ] <EOL> fake_roles_list = [ self . fake_role_0 , self . fake_role_1 ] <EOL> fake_info = self . _get_fake_info ( fake_tenants_list , fake_users_list , <EOL> fake_roles_list ) <EOL> self . mock_client ( ) . tenants . list . return_value = fake_tenants_list <EOL> self . mock_client ( ) . users . list . return_value = fake_users_list <EOL> self . mock_client ( ) . roles . list . return_value = fake_roles_list <EOL> self . keystone_client . _get_user_roles_cached = mock . MagicMock ( ) <EOL> self . keystone_client . _get_user_roles_cached . return_value = mock . MagicMock ( ) . return_value = [ self . fake_role_0 ] <EOL> self . mock_client ( ) . roles . roles_for_user . return_value = [ <EOL> self . fake_role_0 ] <EOL> info = self . keystone_client . read_info ( ) <EOL> self . assertEquals ( fake_info , info ) <EOL> def test_deploy ( self ) : <EOL> fake_tenants_list = [ self . fake_tenant_0 , self . fake_tenant_1 ] <EOL> fake_users_list = [ self . fake_user_0 , self . fake_user_1 ] <EOL> fake_roles_list = [ self . fake_role_0 , self . fake_role_1 ] <EOL> fake_info = self . _get_fake_info ( fake_tenants_list , fake_users_list , <EOL> fake_roles_list ) <EOL> self . mock_client ( ) . tenants . list . return_value = [ fake_tenants_list [ <NUM_LIT:0> ] ] <EOL> self . mock_client ( ) . users . list . return_value = [ fake_users_list [ <NUM_LIT:0> ] ] <EOL> self . mock_client ( ) . roles . list . return_value = [ fake_roles_list [ <NUM_LIT:0> ] ] <EOL> self . mock_client ( ) . roles . roles_for_user . return_value = [ <EOL> self . fake_role_1 ] <EOL> def tenant_create ( ** kwargs ) : <EOL> self . mock_client ( ) . tenants . list . return_value . append ( <EOL> fake_tenants_list [ <NUM_LIT:1> ] ) <EOL> return fake_tenants_list [ <NUM_LIT:1> ] <EOL> def user_create ( ** kwars ) : <EOL> self . mock_client ( ) . users . list . return_value . append ( <EOL> fake_users_list [ <NUM_LIT:1> ] ) <EOL> return fake_users_list [ <NUM_LIT:1> ] <EOL> def roles_create ( role_name ) : <EOL> self . mock_client ( ) . roles . list . return_value . append ( <EOL> fake_roles_list [ <NUM_LIT:1> ] ) <EOL> return fake_roles_list [ <NUM_LIT:1> ] <EOL> self . mock_client ( ) . tenants . create = tenant_create <EOL> self . mock_client ( ) . users . create = user_create <EOL> self . mock_client ( ) . roles . create = roles_create <EOL> self . keystone_client . deploy ( fake_info ) <EOL> mock_calls = [ ] <EOL> for user in fake_users_list : <EOL> for tenant in fake_tenants_list : <EOL> mock_calls . append ( <EOL> mock . call ( user . id , fake_roles_list [ <NUM_LIT:0> ] . id , tenant . id ) ) <EOL> self . assertEquals ( mock_calls , <EOL> self . mock_client ( ) . roles . add_user_role . mock_calls ) <EOL> @ staticmethod <EOL> def _get_fake_info ( fake_tenants_list , fake_users_list , fake_roles_list ) : <EOL> fake_user_tenants_roles = { } <EOL> for user in fake_users_list : <EOL> fake_user_tenants_roles [ user . name ] = { } <EOL> for tenant in fake_tenants_list : <EOL> fake_user_tenants_roles [ user . name ] [ <EOL> tenant . name ] = [ { '<STR_LIT>' : { '<STR_LIT:name>' : fake_roles_list [ <NUM_LIT:0> ] . name , <EOL> '<STR_LIT:id>' : fake_roles_list [ <NUM_LIT:0> ] . id } } ] <EOL> fake_info = { '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : fake_user_tenants_roles } <EOL> for tenant in fake_tenants_list : <EOL> fake_info [ '<STR_LIT>' ] . append ( <EOL> { '<STR_LIT>' : { '<STR_LIT:name>' : tenant . name , <EOL> '<STR_LIT:id>' : tenant . id , <EOL> '<STR_LIT:description>' : tenant . description } , <EOL> '<STR_LIT>' : { } } ) <EOL> for user in fake_users_list : <EOL> fake_info [ '<STR_LIT>' ] . append ( <EOL> { '<STR_LIT:user>' : { '<STR_LIT:name>' : user . name , <EOL> '<STR_LIT:id>' : user . id , <EOL> '<STR_LIT:email>' : user . email , <EOL> '<STR_LIT>' : user . tenantId } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : False } } ) <EOL> for role in fake_roles_list : <EOL> fake_info [ '<STR_LIT>' ] . append ( <EOL> { '<STR_LIT>' : { '<STR_LIT:name>' : role . name , <EOL> '<STR_LIT:id>' : role } , <EOL> '<STR_LIT>' : { } } ) <EOL> return fake_info <EOL> def mock_user_get ( self , id , default = None ) : <EOL> map_dict = { '<STR_LIT>' : self . fake_user_0 , <EOL> '<STR_LIT>' : self . fake_user_1 , <EOL> '<STR_LIT>' : self . fake_same_user , <EOL> '<STR_LIT>' : self . fake_same_user , <EOL> '<STR_LIT>' : self . fake_src_admin_user , <EOL> '<STR_LIT>' : self . fake_dst_admin_user } <EOL> try : <EOL> return map_dict [ id ] <EOL> except KeyError : <EOL> if default is not None : <EOL> return map_dict [ default ] <EOL> raise exceptions . NotFound <EOL> def test_get_dst_user_from_src_user_id_0 ( self ) : <EOL> """<STR_LIT>""" <EOL> user = keystone . get_dst_user_from_src_user_id ( self . fake_src_keystone , <EOL> self . fake_dst_keystone , <EOL> '<STR_LIT>' , <EOL> fallback_to_admin = False ) <EOL> self . assertIsNone ( user ) <EOL> def test_get_dst_user_from_src_user_id_1 ( self ) : <EOL> """<STR_LIT>""" <EOL> user = keystone . get_dst_user_from_src_user_id ( self . fake_src_keystone , <EOL> self . fake_dst_keystone , <EOL> '<STR_LIT>' , <EOL> fallback_to_admin = False ) <EOL> self . assertIsNone ( user ) <EOL> def test_get_dst_user_from_src_user_id_2 ( self ) : <EOL> """<STR_LIT>""" <EOL> user = keystone . get_dst_user_from_src_user_id ( self . fake_src_keystone , <EOL> self . fake_dst_keystone , <EOL> '<STR_LIT>' , <EOL> fallback_to_admin = False ) <EOL> self . assertIsNone ( user ) <EOL> def test_get_dst_user_from_src_user_id_3 ( self ) : <EOL> """<STR_LIT>""" <EOL> user = keystone . get_dst_user_from_src_user_id ( self . fake_src_keystone , <EOL> self . fake_dst_keystone , <EOL> '<STR_LIT>' , <EOL> fallback_to_admin = False ) <EOL> self . assertEquals ( self . fake_same_user , user ) <EOL> def test_get_dst_user_from_src_user_id_4 ( self ) : <EOL> """<STR_LIT>""" <EOL> user = keystone . get_dst_user_from_src_user_id ( self . fake_src_keystone , <EOL> self . fake_dst_keystone , <EOL> '<STR_LIT>' , <EOL> fallback_to_admin = True ) <EOL> self . assertEquals ( self . fake_dst_admin_user , user ) <EOL> def test_get_dst_user_from_src_user_id_5 ( self ) : <EOL> """<STR_LIT>""" <EOL> user = keystone . get_dst_user_from_src_user_id ( self . fake_src_keystone , <EOL> self . fake_dst_keystone , <EOL> '<STR_LIT>' , <EOL> fallback_to_admin = True ) <EOL> self . assertEquals ( self . fake_dst_admin_user , user ) <EOL> def test_get_dst_user_from_src_user_id_6 ( self ) : <EOL> """<STR_LIT>""" <EOL> user = keystone . get_dst_user_from_src_user_id ( self . fake_src_keystone , <EOL> self . fake_dst_keystone , <EOL> '<STR_LIT>' , <EOL> fallback_to_admin = True ) <EOL> self . assertEquals ( self . fake_dst_admin_user , user ) <EOL> def test_get_dst_user_from_src_user_id_7 ( self ) : <EOL> """<STR_LIT>""" <EOL> user = keystone . get_dst_user_from_src_user_id ( self . fake_src_keystone , <EOL> self . fake_dst_keystone , <EOL> '<STR_LIT>' , <EOL> fallback_to_admin = True ) <EOL> self . assertEquals ( self . fake_same_user , user ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> class KeystoneClientTestCase ( test . TestCase ) : <EOL> def test_adds_region_if_set_in_config ( self , ks_client ) : <EOL> cloud = mock . MagicMock ( ) <EOL> config = mock . MagicMock ( ) <EOL> tenant = '<STR_LIT>' <EOL> region = '<STR_LIT>' <EOL> user = '<STR_LIT:user>' <EOL> auth_url = '<STR_LIT>' <EOL> password = '<STR_LIT:password>' <EOL> insecure = False <EOL> cacert = '<STR_LIT>' <EOL> config . cloud . user = user <EOL> config . cloud . tenant = tenant <EOL> config . cloud . region = region <EOL> config . cloud . auth_url = auth_url <EOL> config . cloud . password = password <EOL> config . cloud . insecure = insecure <EOL> config . cloud . cacert = cacert <EOL> ks = keystone . KeystoneIdentity ( config , cloud ) <EOL> ks . _get_client_by_creds ( ) <EOL> ks_client . assert_called_with ( <EOL> region_name = region , <EOL> tenant_name = tenant , <EOL> password = password , <EOL> auth_url = auth_url , <EOL> username = user , <EOL> cacert = cacert , <EOL> insecure = insecure <EOL> ) <EOL> def test_does_not_add_region_if_not_set_in_config ( self , ks_client ) : <EOL> cloud = mock . MagicMock ( ) <EOL> config = mock . MagicMock ( ) <EOL> tenant = '<STR_LIT>' <EOL> user = '<STR_LIT:user>' <EOL> auth_url = '<STR_LIT>' <EOL> password = '<STR_LIT:password>' <EOL> insecure = False <EOL> cacert = '<STR_LIT>' <EOL> config . cloud . region = None <EOL> config . cloud . user = user <EOL> config . cloud . tenant = tenant <EOL> config . cloud . auth_url = auth_url <EOL> config . cloud . password = password <EOL> config . cloud . insecure = insecure <EOL> config . cloud . cacert = cacert <EOL> ks = keystone . KeystoneIdentity ( config , cloud ) <EOL> ks . _get_client_by_creds ( ) <EOL> ks_client . assert_called_with ( <EOL> tenant_name = tenant , <EOL> password = password , <EOL> auth_url = auth_url , <EOL> username = user , <EOL> cacert = cacert , <EOL> insecure = insecure , <EOL> region_name = None <EOL> ) <EOL> class AddAdminToNonAdminTenantTestCase ( test . TestCase ) : <EOL> def test_user_role_is_removed_on_scope_exit ( self ) : <EOL> ksclient = mock . MagicMock ( ) <EOL> member_role = mock . Mock ( ) <EOL> member_role . name = '<STR_LIT>' <EOL> user = mock . MagicMock ( ) <EOL> user . name = '<STR_LIT>' <EOL> tenant = mock . MagicMock ( ) <EOL> tenant . name = '<STR_LIT>' <EOL> ksclient . roles . list . return_value = [ member_role ] <EOL> ksclient . users . list . return_value = [ user ] <EOL> ksclient . tenants . list . return_value = [ tenant ] <EOL> ksclient . roles . roles_for_user . return_value = [ ] <EOL> with keystone . AddAdminUserToNonAdminTenant ( ksclient , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> pass <EOL> assert ksclient . roles . add_user_role . called <EOL> assert ksclient . roles . remove_user_role . called <EOL> def test_nothing_happens_if_admin_is_already_member_of_a_tenant ( self ) : <EOL> ksclient = mock . MagicMock ( ) <EOL> role_name = '<STR_LIT>' <EOL> member_role = mock . Mock ( ) <EOL> member_role . name = role_name <EOL> user = mock . MagicMock ( ) <EOL> user . name = '<STR_LIT>' <EOL> tenant = mock . MagicMock ( ) <EOL> tenant . name = '<STR_LIT>' <EOL> ksclient . roles . list . return_value = [ member_role ] <EOL> ksclient . users . list . return_value = [ user ] <EOL> ksclient . tenants . list . return_value = [ tenant ] <EOL> ksclient . roles . roles_for_user . return_value = [ member_role ] <EOL> with keystone . AddAdminUserToNonAdminTenant ( ksclient , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> member_role = role_name ) : <EOL> pass <EOL> assert not ksclient . roles . add_user_role . called <EOL> assert not ksclient . roles . remove_user_role . called </s>
