<s> from oslo_serialization import jsonutils <EOL> import six <EOL> import webob <EOL> from nova import compute <EOL> from nova . compute import vm_states <EOL> from nova import exception <EOL> from nova import objects <EOL> from nova . objects import instance as instance_obj <EOL> from nova import test <EOL> from nova . tests . unit . api . openstack import fakes <EOL> from nova . tests . unit import fake_instance <EOL> SENTINEL = object ( ) <EOL> def fake_compute_get ( * args , ** kwargs ) : <EOL> def _return_server ( * _args , ** _kwargs ) : <EOL> inst = fakes . stub_instance ( * args , ** kwargs ) <EOL> return fake_instance . fake_instance_obj ( _args [ <NUM_LIT:1> ] , ** inst ) <EOL> return _return_server <EOL> class HideServerAddressesTestV21 ( test . TestCase ) : <EOL> content_type = '<STR_LIT:application/json>' <EOL> base_url = '<STR_LIT>' <EOL> def _setup_wsgi ( self ) : <EOL> self . wsgi_app = fakes . wsgi_app_v21 ( <EOL> init_only = ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def setUp ( self ) : <EOL> super ( HideServerAddressesTestV21 , self ) . setUp ( ) <EOL> fakes . stub_out_nw_api ( self ) <EOL> return_server = fakes . fake_instance_get ( ) <EOL> self . stub_out ( '<STR_LIT>' , return_server ) <EOL> self . _setup_wsgi ( ) <EOL> def _make_request ( self , url ) : <EOL> req = webob . Request . blank ( url ) <EOL> req . headers [ '<STR_LIT>' ] = self . content_type <EOL> res = req . get_response ( self . wsgi_app ) <EOL> return res <EOL> @ staticmethod <EOL> def _get_server ( body ) : <EOL> return jsonutils . loads ( body ) . get ( '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def _get_servers ( body ) : <EOL> return jsonutils . loads ( body ) . get ( '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def _get_addresses ( server ) : <EOL> return server . get ( '<STR_LIT>' , SENTINEL ) <EOL> def _check_addresses ( self , addresses , exists ) : <EOL> self . assertTrue ( addresses is not SENTINEL ) <EOL> if exists : <EOL> self . assertTrue ( addresses ) <EOL> else : <EOL> self . assertFalse ( addresses ) <EOL> def test_show_hides_in_building ( self ) : <EOL> instance_id = <NUM_LIT:1> <EOL> uuid = fakes . get_fake_uuid ( instance_id ) <EOL> self . stubs . Set ( compute . api . API , '<STR_LIT>' , <EOL> fake_compute_get ( instance_id , uuid = uuid , <EOL> vm_state = vm_states . BUILDING ) ) <EOL> res = self . _make_request ( self . base_url + '<STR_LIT>' % uuid ) <EOL> self . assertEqual ( res . status_int , <NUM_LIT:200> ) <EOL> server = self . _get_server ( res . body ) <EOL> addresses = self . _get_addresses ( server ) <EOL> self . _check_addresses ( addresses , exists = False ) <EOL> def test_show ( self ) : <EOL> instance_id = <NUM_LIT:1> <EOL> uuid = fakes . get_fake_uuid ( instance_id ) <EOL> self . stubs . Set ( compute . api . API , '<STR_LIT>' , <EOL> fake_compute_get ( instance_id , uuid = uuid , <EOL> vm_state = vm_states . ACTIVE ) ) <EOL> res = self . _make_request ( self . base_url + '<STR_LIT>' % uuid ) <EOL> self . assertEqual ( res . status_int , <NUM_LIT:200> ) <EOL> server = self . _get_server ( res . body ) <EOL> addresses = self . _get_addresses ( server ) <EOL> self . _check_addresses ( addresses , exists = True ) <EOL> def test_detail_hides_building_server_addresses ( self ) : <EOL> instance_0 = fakes . stub_instance ( <NUM_LIT:0> , uuid = fakes . get_fake_uuid ( <NUM_LIT:0> ) , <EOL> vm_state = vm_states . ACTIVE ) <EOL> instance_1 = fakes . stub_instance ( <NUM_LIT:1> , uuid = fakes . get_fake_uuid ( <NUM_LIT:1> ) , <EOL> vm_state = vm_states . BUILDING ) <EOL> instances = [ instance_0 , instance_1 ] <EOL> def get_all ( * args , ** kwargs ) : <EOL> fields = instance_obj . INSTANCE_DEFAULT_FIELDS <EOL> return instance_obj . _make_instance_list ( <EOL> args [ <NUM_LIT:1> ] , objects . InstanceList ( ) , instances , fields ) <EOL> self . stubs . Set ( compute . api . API , '<STR_LIT>' , get_all ) <EOL> res = self . _make_request ( self . base_url + '<STR_LIT>' ) <EOL> self . assertEqual ( res . status_int , <NUM_LIT:200> ) <EOL> servers = self . _get_servers ( res . body ) <EOL> self . assertEqual ( len ( servers ) , len ( instances ) ) <EOL> for instance , server in six . moves . zip ( instances , servers ) : <EOL> addresses = self . _get_addresses ( server ) <EOL> exists = ( instance [ '<STR_LIT>' ] == vm_states . ACTIVE ) <EOL> self . _check_addresses ( addresses , exists = exists ) <EOL> def test_no_instance_passthrough_404 ( self ) : <EOL> def fake_compute_get ( * args , ** kwargs ) : <EOL> raise exception . InstanceNotFound ( instance_id = '<STR_LIT>' ) <EOL> self . stubs . Set ( compute . api . API , '<STR_LIT>' , fake_compute_get ) <EOL> res = self . _make_request ( self . base_url + '<STR_LIT:/>' + fakes . get_fake_uuid ( ) ) <EOL> self . assertEqual ( res . status_int , <NUM_LIT> ) <EOL> class HideServerAddressesTestV2 ( HideServerAddressesTestV21 ) : <EOL> def _setup_wsgi ( self ) : <EOL> self . flags ( <EOL> osapi_compute_extension = [ <EOL> '<STR_LIT>' ] , <EOL> osapi_compute_ext_list = [ '<STR_LIT>' ] ) <EOL> self . wsgi_app = fakes . wsgi_app ( init_only = ( '<STR_LIT>' , ) ) </s>
