<s> import functools <EOL> import posixpath <EOL> import unittest <EOL> try : <EOL> import unittest . mock as mock <EOL> except ImportError : <EOL> import mock <EOL> from oslo_config import cfg <EOL> from cloudbaseinit . metadata . services import base <EOL> from cloudbaseinit . metadata . services import baseopenstackservice <EOL> from cloudbaseinit . tests . metadata import fake_json_response <EOL> from cloudbaseinit . utils import x509constants <EOL> CONF = cfg . CONF <EOL> MODPATH = "<STR_LIT>" <EOL> class FinalBaseOpenStackService ( baseopenstackservice . BaseOpenStackService ) : <EOL> def _get_data ( self ) : <EOL> pass <EOL> class TestBaseOpenStackService ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> CONF . set_override ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> self . _service = FinalBaseOpenStackService ( ) <EOL> date = "<STR_LIT>" <EOL> fake_metadata = fake_json_response . get_fake_metadata_json ( date ) <EOL> self . _fake_network_config = fake_metadata [ "<STR_LIT>" ] <EOL> self . _fake_content = self . _fake_network_config [ "<STR_LIT>" ] <EOL> self . _fake_public_keys = fake_metadata [ "<STR_LIT>" ] <EOL> self . _fake_keys = fake_metadata [ "<STR_LIT>" ] <EOL> self . _partial_test_get_network_details = functools . partial ( <EOL> self . _test_get_network_details , <EOL> network_config = self . _fake_network_config , <EOL> content = self . _fake_content <EOL> ) <EOL> @ mock . patch ( MODPATH + <EOL> "<STR_LIT>" ) <EOL> def test_get_content ( self , mock_get_cache_data ) : <EOL> response = self . _service . get_content ( '<STR_LIT>' ) <EOL> path = posixpath . join ( '<STR_LIT>' , '<STR_LIT:content>' , '<STR_LIT>' ) <EOL> mock_get_cache_data . assert_called_once_with ( path ) <EOL> self . assertEqual ( mock_get_cache_data . return_value , response ) <EOL> @ mock . patch ( MODPATH + <EOL> "<STR_LIT>" ) <EOL> def test_get_user_data ( self , mock_get_cache_data ) : <EOL> response = self . _service . get_user_data ( ) <EOL> path = posixpath . join ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> mock_get_cache_data . assert_called_once_with ( path ) <EOL> self . assertEqual ( mock_get_cache_data . return_value , response ) <EOL> @ mock . patch ( MODPATH + <EOL> "<STR_LIT>" ) <EOL> def test_get_meta_data ( self , mock_get_cache_data ) : <EOL> mock_get_cache_data . return_value = '<STR_LIT>' <EOL> response = self . _service . _get_meta_data ( <EOL> version = '<STR_LIT>' ) <EOL> path = posixpath . join ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> mock_get_cache_data . assert_called_with ( path , decode = True ) <EOL> self . assertEqual ( { "<STR_LIT>" : "<STR_LIT:data>" } , response ) <EOL> @ mock . patch ( MODPATH + <EOL> "<STR_LIT>" ) <EOL> def test_get_instance_id ( self , mock_get_meta_data ) : <EOL> response = self . _service . get_instance_id ( ) <EOL> mock_get_meta_data . assert_called_once_with ( ) <EOL> mock_get_meta_data ( ) . get . assert_called_once_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( mock_get_meta_data . return_value . get . return_value , <EOL> response ) <EOL> @ mock . patch ( MODPATH + <EOL> "<STR_LIT>" ) <EOL> def test_get_host_name ( self , mock_get_meta_data ) : <EOL> response = self . _service . get_host_name ( ) <EOL> mock_get_meta_data . assert_called_once_with ( ) <EOL> mock_get_meta_data ( ) . get . assert_called_once_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( mock_get_meta_data . return_value . get . return_value , <EOL> response ) <EOL> @ mock . patch ( MODPATH + <EOL> "<STR_LIT>" ) <EOL> def test_get_public_keys ( self , mock_get_meta_data ) : <EOL> mock_get_meta_data . return_value . get . side_effect = [ self . _fake_public_keys , self . _fake_keys ] <EOL> response = self . _service . get_public_keys ( ) <EOL> mock_get_meta_data . assert_called_once_with ( ) <EOL> mock_get_meta_data . return_value . get . assert_any_call ( "<STR_LIT>" ) <EOL> mock_get_meta_data . return_value . get . assert_any_call ( "<STR_LIT>" ) <EOL> public_keys = ( list ( self . _fake_public_keys . values ( ) ) + <EOL> [ key [ "<STR_LIT:data>" ] for key in self . _fake_keys <EOL> if key [ "<STR_LIT:type>" ] == "<STR_LIT>" ] ) <EOL> public_keys = [ key . strip ( ) for key in public_keys ] <EOL> self . assertEqual ( sorted ( list ( set ( public_keys ) ) ) , <EOL> sorted ( response ) ) <EOL> @ mock . patch ( MODPATH + <EOL> "<STR_LIT>" ) <EOL> def _test_get_admin_password ( self , mock_get_meta_data , meta_data ) : <EOL> mock_get_meta_data . return_value = meta_data <EOL> response = self . _service . get_admin_password ( ) <EOL> mock_get_meta_data . assert_called_once_with ( ) <EOL> if meta_data and '<STR_LIT>' in meta_data : <EOL> self . assertEqual ( meta_data [ '<STR_LIT>' ] , response ) <EOL> elif meta_data and '<STR_LIT>' in meta_data . get ( '<STR_LIT>' ) : <EOL> self . assertEqual ( meta_data . get ( '<STR_LIT>' ) [ '<STR_LIT>' ] , response ) <EOL> else : <EOL> self . assertIsNone ( response ) <EOL> def test_get_admin_pass ( self ) : <EOL> self . _test_get_admin_password ( meta_data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def test_get_admin_pass_in_meta ( self ) : <EOL> self . _test_get_admin_password ( <EOL> meta_data = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } ) <EOL> def test_get_admin_pass_no_pass ( self ) : <EOL> self . _test_get_admin_password ( meta_data = { } ) <EOL> @ mock . patch ( MODPATH + <EOL> "<STR_LIT>" ) <EOL> @ mock . patch ( MODPATH + <EOL> "<STR_LIT>" ) <EOL> def _test_get_client_auth_certs ( self , mock_get_user_data , <EOL> mock_get_meta_data , meta_data , <EOL> ret_value = None ) : <EOL> mock_get_meta_data . return_value = meta_data <EOL> mock_get_user_data . side_effect = [ ret_value ] <EOL> response = self . _service . get_client_auth_certs ( ) <EOL> mock_get_meta_data . assert_called_once_with ( ) <EOL> if isinstance ( ret_value , bytes ) and ret_value . startswith ( <EOL> x509constants . PEM_HEADER . encode ( ) ) : <EOL> mock_get_user_data . assert_called_once_with ( ) <EOL> self . assertEqual ( [ ret_value . decode ( ) ] , response ) <EOL> elif ret_value is base . NotExistingMetadataException : <EOL> self . assertFalse ( response ) <EOL> else : <EOL> expected = [ ] <EOL> expectation = { <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : [ key [ "<STR_LIT:data>" ] . strip ( ) for key in self . _fake_keys <EOL> if key [ "<STR_LIT:type>" ] == "<STR_LIT>" ] <EOL> } <EOL> for field , value in expectation . items ( ) : <EOL> if field in meta_data : <EOL> expected . extend ( value if isinstance ( value , list ) <EOL> else [ value ] ) <EOL> self . assertEqual ( sorted ( list ( set ( expected ) ) ) , sorted ( response ) ) <EOL> def test_get_client_auth_certs ( self ) : <EOL> self . _test_get_client_auth_certs ( <EOL> meta_data = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } , <EOL> "<STR_LIT>" : self . _fake_keys } ) <EOL> def test_get_client_auth_certs_no_cert_data ( self ) : <EOL> self . _test_get_client_auth_certs ( <EOL> meta_data = { } , ret_value = x509constants . PEM_HEADER . encode ( ) ) <EOL> def test_get_client_auth_certs_no_cert_data_exception ( self ) : <EOL> self . _test_get_client_auth_certs ( <EOL> meta_data = { } , ret_value = base . NotExistingMetadataException ) <EOL> @ mock . patch ( MODPATH + <EOL> "<STR_LIT>" ) <EOL> @ mock . patch ( MODPATH + <EOL> "<STR_LIT>" ) <EOL> def _test_get_network_details ( self , <EOL> mock_get_meta_data , <EOL> mock_get_content , <EOL> network_config = None , <EOL> content = None , <EOL> search_fail = False , <EOL> no_path = False ) : <EOL> mock_get_meta_data ( ) . get . return_value = network_config <EOL> mock_get_content . return_value = content <EOL> if search_fail : <EOL> ret = self . _service . get_network_details ( ) <EOL> self . assertFalse ( ret ) <EOL> return <EOL> ret = self . _service . get_network_details ( ) <EOL> mock_get_meta_data ( ) . get . assert_called_once_with ( "<STR_LIT>" ) <EOL> if network_config and not no_path : <EOL> mock_get_content . assert_called_once_with ( "<STR_LIT>" ) <EOL> if not network_config : <EOL> self . assertIsNone ( ret ) <EOL> return <EOL> if no_path : <EOL> self . assertIsNone ( ret ) <EOL> return <EOL> nic0 = base . NetworkDetails ( <EOL> fake_json_response . NAME0 , <EOL> fake_json_response . MAC0 . upper ( ) , <EOL> fake_json_response . ADDRESS0 , <EOL> fake_json_response . ADDRESS60 , <EOL> fake_json_response . NETMASK0 , <EOL> fake_json_response . NETMASK60 , <EOL> fake_json_response . BROADCAST0 , <EOL> fake_json_response . GATEWAY0 , <EOL> fake_json_response . GATEWAY60 , <EOL> fake_json_response . DNSNS0 . split ( ) <EOL> ) <EOL> nic1 = base . NetworkDetails ( <EOL> fake_json_response . NAME1 , <EOL> None , <EOL> fake_json_response . ADDRESS1 , <EOL> fake_json_response . ADDRESS61 , <EOL> fake_json_response . NETMASK1 , <EOL> fake_json_response . NETMASK61 , <EOL> fake_json_response . BROADCAST1 , <EOL> fake_json_response . GATEWAY1 , <EOL> fake_json_response . GATEWAY61 , <EOL> None <EOL> ) <EOL> self . assertEqual ( [ nic0 , nic1 ] , ret ) <EOL> def test_get_network_details_no_config ( self ) : <EOL> self . _partial_test_get_network_details ( <EOL> network_config = None <EOL> ) <EOL> def test_get_network_details_no_path ( self ) : <EOL> self . _fake_network_config . pop ( "<STR_LIT>" , None ) <EOL> self . _partial_test_get_network_details ( <EOL> network_config = self . _fake_network_config , <EOL> no_path = True <EOL> ) <EOL> def test_get_network_details_search_fail ( self ) : <EOL> self . _fake_content = "<STR_LIT>" <EOL> self . _partial_test_get_network_details ( <EOL> content = self . _fake_content , <EOL> search_fail = True <EOL> ) <EOL> def test_get_network_details ( self ) : <EOL> self . _partial_test_get_network_details ( ) </s>
