<s> import os <EOL> import tempfile <EOL> import mock <EOL> import os_client_config <EOL> from openstack import connection <EOL> from openstack import profile <EOL> from openstack . tests . unit import base <EOL> CONFIG_AUTH_URL = "<STR_LIT>" <EOL> CONFIG_USERNAME = "<STR_LIT>" <EOL> CONFIG_PASSWORD = "<STR_LIT>" <EOL> CONFIG_PROJECT = "<STR_LIT>" <EOL> CONFIG_CACERT = "<STR_LIT>" <EOL> CLOUD_CONFIG = """<STR_LIT>""" . format ( auth_url = CONFIG_AUTH_URL , username = CONFIG_USERNAME , <EOL> password = CONFIG_PASSWORD , project = CONFIG_PROJECT , <EOL> cacert = CONFIG_CACERT ) <EOL> class TestConnection ( base . TestCase ) : <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test_other_parameters ( self , mock_session_init ) : <EOL> mock_session_init . return_value = mock_session_init <EOL> mock_profile = mock . Mock ( ) <EOL> mock_profile . get_services = mock . Mock ( return_value = [ ] ) <EOL> conn = connection . Connection ( profile = mock_profile , authenticator = '<STR_LIT:2>' , <EOL> verify = True , cert = '<STR_LIT>' , user_agent = '<STR_LIT:1>' ) <EOL> args = { '<STR_LIT>' : '<STR_LIT:2>' , '<STR_LIT>' : '<STR_LIT:1>' , '<STR_LIT>' : True , '<STR_LIT>' : '<STR_LIT>' } <EOL> mock_session_init . assert_called_with ( mock_profile , ** args ) <EOL> self . assertEqual ( mock_session_init , conn . session ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test_create_authenticator ( self , mock_get_plugin ) : <EOL> mock_plugin = mock . Mock ( ) <EOL> mock_loader = mock . Mock ( ) <EOL> mock_options = [ <EOL> mock . Mock ( dest = "<STR_LIT>" ) , <EOL> mock . Mock ( dest = "<STR_LIT:password>" ) , <EOL> mock . Mock ( dest = "<STR_LIT:username>" ) , <EOL> ] <EOL> mock_loader . get_options = mock . Mock ( return_value = mock_options ) <EOL> mock_loader . load_from_options = mock . Mock ( return_value = mock_plugin ) <EOL> mock_get_plugin . return_value = mock_loader <EOL> auth_args = { <EOL> '<STR_LIT>' : '<STR_LIT:0>' , <EOL> '<STR_LIT:username>' : '<STR_LIT:1>' , <EOL> '<STR_LIT:password>' : '<STR_LIT:2>' , <EOL> } <EOL> conn = connection . Connection ( auth_plugin = '<STR_LIT>' , ** auth_args ) <EOL> mock_get_plugin . assert_called_with ( '<STR_LIT>' ) <EOL> mock_loader . load_from_options . assert_called_with ( ** auth_args ) <EOL> self . assertEqual ( mock_plugin , conn . authenticator ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test_default_plugin ( self , mock_get_plugin ) : <EOL> connection . Connection ( ) <EOL> self . assertTrue ( mock_get_plugin . called ) <EOL> self . assertEqual ( mock_get_plugin . call_args , mock . call ( "<STR_LIT:password>" ) ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test_pass_authenticator ( self , mock_get_plugin ) : <EOL> mock_plugin = mock . Mock ( ) <EOL> mock_get_plugin . return_value = None <EOL> conn = connection . Connection ( authenticator = mock_plugin ) <EOL> self . assertFalse ( mock_get_plugin . called ) <EOL> self . assertEqual ( mock_plugin , conn . authenticator ) <EOL> def test_create_session ( self ) : <EOL> auth = mock . Mock ( ) <EOL> prof = profile . Profile ( ) <EOL> conn = connection . Connection ( authenticator = auth , profile = prof ) <EOL> self . assertEqual ( auth , conn . authenticator ) <EOL> self . assertEqual ( prof , conn . profile ) <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> conn . cluster . __class__ . __module__ ) <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> conn . compute . __class__ . __module__ ) <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> conn . database . __class__ . __module__ ) <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> conn . identity . __class__ . __module__ ) <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> conn . image . __class__ . __module__ ) <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> conn . network . __class__ . __module__ ) <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> conn . object_store . __class__ . __module__ ) <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> conn . orchestration . __class__ . __module__ ) <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> conn . telemetry . __class__ . __module__ ) <EOL> def _prepare_test_config ( self ) : <EOL> config_dir = tempfile . mkdtemp ( ) <EOL> config_path = os . path . join ( config_dir , "<STR_LIT>" ) <EOL> with open ( config_path , "<STR_LIT:w>" ) as conf : <EOL> conf . write ( CLOUD_CONFIG ) <EOL> os . environ [ "<STR_LIT>" ] = config_path <EOL> def test_from_config_given_data ( self ) : <EOL> self . _prepare_test_config ( ) <EOL> data = os_client_config . OpenStackConfig ( ) . get_one_cloud ( "<STR_LIT>" ) <EOL> sot = connection . from_config ( cloud_config = data ) <EOL> self . assertEqual ( CONFIG_USERNAME , <EOL> sot . authenticator . _username ) <EOL> self . assertEqual ( CONFIG_PASSWORD , <EOL> sot . authenticator . _password ) <EOL> self . assertEqual ( CONFIG_AUTH_URL , <EOL> sot . authenticator . auth_url ) <EOL> self . assertEqual ( CONFIG_PROJECT , <EOL> sot . authenticator . _project_name ) <EOL> def test_from_config_given_name ( self ) : <EOL> self . _prepare_test_config ( ) <EOL> sot = connection . from_config ( cloud_name = "<STR_LIT>" ) <EOL> self . assertEqual ( CONFIG_USERNAME , <EOL> sot . authenticator . _username ) <EOL> self . assertEqual ( CONFIG_PASSWORD , <EOL> sot . authenticator . _password ) <EOL> self . assertEqual ( CONFIG_AUTH_URL , <EOL> sot . authenticator . auth_url ) <EOL> self . assertEqual ( CONFIG_PROJECT , <EOL> sot . authenticator . _project_name ) <EOL> def test_from_config_given_options ( self ) : <EOL> self . _prepare_test_config ( ) <EOL> version = "<STR_LIT:100>" <EOL> class Opts ( object ) : <EOL> compute_api_version = version <EOL> sot = connection . from_config ( cloud_name = "<STR_LIT>" , options = Opts ) <EOL> pref = sot . session . profile . get_filter ( "<STR_LIT>" ) <EOL> self . assertEqual ( "<STR_LIT:v>" + version , pref . version ) <EOL> def test_from_config_verify ( self ) : <EOL> self . _prepare_test_config ( ) <EOL> sot = connection . from_config ( cloud_name = "<STR_LIT>" ) <EOL> self . assertFalse ( sot . session . verify ) <EOL> sot = connection . from_config ( cloud_name = "<STR_LIT>" ) <EOL> self . assertEqual ( CONFIG_CACERT , sot . session . verify ) <EOL> def test_authorize_works ( self ) : <EOL> fake_session = mock . Mock ( ) <EOL> fake_headers = { '<STR_LIT>' : '<STR_LIT>' } <EOL> fake_session . get_auth_headers . return_value = fake_headers <EOL> sot = connection . Connection ( session = fake_session , <EOL> authenticator = mock . Mock ( ) ) <EOL> res = sot . authorize ( ) <EOL> self . assertEqual ( '<STR_LIT>' , res ) <EOL> def test_authorize_silent_failure ( self ) : <EOL> fake_session = mock . Mock ( ) <EOL> fake_session . get_auth_headers . return_value = None <EOL> sot = connection . Connection ( session = fake_session , <EOL> authenticator = mock . Mock ( ) ) <EOL> res = sot . authorize ( ) <EOL> self . assertIsNone ( res ) </s>
