<s> import mock <EOL> import webob <EOL> from nova . api . openstack . compute . legacy_v2 . contrib import multinic as multinic_v2 <EOL> from nova . api . openstack . compute import multinic as multinic_v21 <EOL> from nova import compute <EOL> from nova import exception <EOL> from nova import objects <EOL> from nova import test <EOL> from nova . tests . unit . api . openstack import fakes <EOL> UUID = '<STR_LIT>' <EOL> last_add_fixed_ip = ( None , None ) <EOL> last_remove_fixed_ip = ( None , None ) <EOL> def compute_api_add_fixed_ip ( self , context , instance , network_id ) : <EOL> global last_add_fixed_ip <EOL> last_add_fixed_ip = ( instance [ '<STR_LIT>' ] , network_id ) <EOL> def compute_api_remove_fixed_ip ( self , context , instance , address ) : <EOL> global last_remove_fixed_ip <EOL> last_remove_fixed_ip = ( instance [ '<STR_LIT>' ] , address ) <EOL> def compute_api_get ( self , context , instance_id , want_objects = False , <EOL> expected_attrs = None ) : <EOL> instance = objects . Instance ( ) <EOL> instance . uuid = instance_id <EOL> instance . id = <NUM_LIT:1> <EOL> instance . vm_state = '<STR_LIT>' <EOL> instance . task_state = '<STR_LIT>' <EOL> instance . obj_reset_changes ( ) <EOL> return instance <EOL> class FixedIpTestV21 ( test . NoDBTestCase ) : <EOL> controller_class = multinic_v21 <EOL> validation_error = exception . ValidationError <EOL> def setUp ( self ) : <EOL> super ( FixedIpTestV21 , self ) . setUp ( ) <EOL> fakes . stub_out_networking ( self ) <EOL> fakes . stub_out_rate_limiting ( self . stubs ) <EOL> self . stub_out ( '<STR_LIT>' , <EOL> compute_api_add_fixed_ip ) <EOL> self . stub_out ( '<STR_LIT>' , <EOL> compute_api_remove_fixed_ip ) <EOL> self . stub_out ( '<STR_LIT>' , compute_api_get ) <EOL> self . controller = self . controller_class . MultinicController ( ) <EOL> self . fake_req = fakes . HTTPRequest . blank ( '<STR_LIT>' ) <EOL> def test_add_fixed_ip ( self ) : <EOL> global last_add_fixed_ip <EOL> last_add_fixed_ip = ( None , None ) <EOL> body = dict ( addFixedIp = dict ( networkId = '<STR_LIT>' ) ) <EOL> resp = self . controller . _add_fixed_ip ( self . fake_req , UUID , body = body ) <EOL> if isinstance ( self . controller , <EOL> multinic_v21 . MultinicController ) : <EOL> status_int = self . controller . _add_fixed_ip . wsgi_code <EOL> else : <EOL> status_int = resp . status_int <EOL> self . assertEqual ( status_int , <NUM_LIT> ) <EOL> self . assertEqual ( last_add_fixed_ip , ( UUID , '<STR_LIT>' ) ) <EOL> def _test_add_fixed_ip_bad_request ( self , body ) : <EOL> self . assertRaises ( self . validation_error , <EOL> self . controller . _add_fixed_ip , <EOL> self . fake_req , <EOL> UUID , body = body ) <EOL> def test_add_fixed_ip_empty_network_id ( self ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } <EOL> self . _test_add_fixed_ip_bad_request ( body ) <EOL> def test_add_fixed_ip_network_id_bigger_than_36 ( self ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT:a>' * <NUM_LIT> } } <EOL> self . _test_add_fixed_ip_bad_request ( body ) <EOL> def test_add_fixed_ip_no_network ( self ) : <EOL> global last_add_fixed_ip <EOL> last_add_fixed_ip = ( None , None ) <EOL> body = dict ( addFixedIp = dict ( ) ) <EOL> self . _test_add_fixed_ip_bad_request ( body ) <EOL> self . assertEqual ( last_add_fixed_ip , ( None , None ) ) <EOL> @ mock . patch . object ( compute . api . API , '<STR_LIT>' ) <EOL> def test_add_fixed_ip_no_more_ips_available ( self , mock_add_fixed_ip ) : <EOL> mock_add_fixed_ip . side_effect = exception . NoMoreFixedIps ( net = '<STR_LIT>' ) <EOL> body = dict ( addFixedIp = dict ( networkId = '<STR_LIT>' ) ) <EOL> self . assertRaises ( webob . exc . HTTPBadRequest , <EOL> self . controller . _add_fixed_ip , <EOL> self . fake_req , <EOL> UUID , body = body ) <EOL> def test_remove_fixed_ip ( self ) : <EOL> global last_remove_fixed_ip <EOL> last_remove_fixed_ip = ( None , None ) <EOL> body = dict ( removeFixedIp = dict ( address = '<STR_LIT>' ) ) <EOL> resp = self . controller . _remove_fixed_ip ( self . fake_req , UUID , body = body ) <EOL> if isinstance ( self . controller , <EOL> multinic_v21 . MultinicController ) : <EOL> status_int = self . controller . _remove_fixed_ip . wsgi_code <EOL> else : <EOL> status_int = resp . status_int <EOL> self . assertEqual ( status_int , <NUM_LIT> ) <EOL> self . assertEqual ( last_remove_fixed_ip , ( UUID , '<STR_LIT>' ) ) <EOL> def test_remove_fixed_ip_no_address ( self ) : <EOL> global last_remove_fixed_ip <EOL> last_remove_fixed_ip = ( None , None ) <EOL> body = dict ( removeFixedIp = dict ( ) ) <EOL> self . assertRaises ( self . validation_error , <EOL> self . controller . _remove_fixed_ip , <EOL> self . fake_req , <EOL> UUID , body = body ) <EOL> self . assertEqual ( last_remove_fixed_ip , ( None , None ) ) <EOL> def test_remove_fixed_ip_invalid_address ( self ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT:address>' : '<STR_LIT>' } } <EOL> self . assertRaises ( self . validation_error , <EOL> self . controller . _remove_fixed_ip , <EOL> self . fake_req , <EOL> UUID , body = body ) <EOL> @ mock . patch . object ( compute . api . API , '<STR_LIT>' , <EOL> side_effect = exception . FixedIpNotFoundForSpecificInstance ( <EOL> instance_uuid = UUID , ip = '<STR_LIT>' ) ) <EOL> def test_remove_fixed_ip_not_found ( self , _remove_fixed_ip ) : <EOL> body = { '<STR_LIT>' : { '<STR_LIT:address>' : '<STR_LIT>' } } <EOL> self . assertRaises ( webob . exc . HTTPBadRequest , <EOL> self . controller . _remove_fixed_ip , <EOL> self . fake_req , <EOL> UUID , body = body ) <EOL> class FixedIpTestV2 ( FixedIpTestV21 ) : <EOL> controller_class = multinic_v2 <EOL> validation_error = webob . exc . HTTPBadRequest <EOL> def test_remove_fixed_ip_invalid_address ( self ) : <EOL> pass <EOL> class MultinicPolicyEnforcementV21 ( test . NoDBTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( MultinicPolicyEnforcementV21 , self ) . setUp ( ) <EOL> self . controller = multinic_v21 . MultinicController ( ) <EOL> self . req = fakes . HTTPRequest . blank ( '<STR_LIT>' ) <EOL> def test_add_fixed_ip_policy_failed ( self ) : <EOL> rule_name = "<STR_LIT>" <EOL> self . policy . set_rules ( { rule_name : "<STR_LIT>" } ) <EOL> exc = self . assertRaises ( <EOL> exception . PolicyNotAuthorized , <EOL> self . controller . _add_fixed_ip , self . req , fakes . FAKE_UUID , <EOL> body = { '<STR_LIT>' : { '<STR_LIT>' : fakes . FAKE_UUID } } ) <EOL> self . assertEqual ( <EOL> "<STR_LIT>" % rule_name , <EOL> exc . format_message ( ) ) <EOL> def test_remove_fixed_ip_policy_failed ( self ) : <EOL> rule_name = "<STR_LIT>" <EOL> self . policy . set_rules ( { rule_name : "<STR_LIT>" } ) <EOL> exc = self . assertRaises ( <EOL> exception . PolicyNotAuthorized , <EOL> self . controller . _remove_fixed_ip , self . req , fakes . FAKE_UUID , <EOL> body = { '<STR_LIT>' : { '<STR_LIT:address>' : "<STR_LIT>" } } ) <EOL> self . assertEqual ( <EOL> "<STR_LIT>" % rule_name , <EOL> exc . format_message ( ) ) </s>
