<s> import datetime <EOL> import iso8601 <EOL> import mock <EOL> import netaddr <EOL> from oslo_utils import timeutils <EOL> from oslo_versionedobjects import base as ovo_base <EOL> from nova import exception <EOL> from nova . objects import fixed_ip <EOL> from nova . tests . unit import fake_instance <EOL> from nova . tests . unit . objects import test_network <EOL> from nova . tests . unit . objects import test_objects <EOL> from nova . tests import uuidsentinel as uuids <EOL> from nova import utils <EOL> fake_fixed_ip = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT:id>' : <NUM_LIT> , <EOL> '<STR_LIT:address>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT:host>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : [ ] , <EOL> } <EOL> class _TestFixedIPObject ( object ) : <EOL> def _compare ( self , obj , db_obj ) : <EOL> for field in obj . fields : <EOL> if field in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> continue <EOL> if field in fixed_ip . FIXED_IP_OPTIONAL_ATTRS : <EOL> if obj . obj_attr_is_set ( field ) and db_obj [ field ] is not None : <EOL> obj_val = obj [ field ] . uuid <EOL> db_val = db_obj [ field ] [ '<STR_LIT>' ] <EOL> else : <EOL> continue <EOL> else : <EOL> obj_val = obj [ field ] <EOL> db_val = db_obj [ field ] <EOL> if isinstance ( obj_val , netaddr . IPAddress ) : <EOL> obj_val = str ( obj_val ) <EOL> self . assertEqual ( db_val , obj_val ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_by_id ( self , get ) : <EOL> get . return_value = fake_fixed_ip <EOL> fixedip = fixed_ip . FixedIP . get_by_id ( self . context , <NUM_LIT> ) <EOL> get . assert_called_once_with ( self . context , <NUM_LIT> , get_network = False ) <EOL> self . _compare ( fixedip , fake_fixed_ip ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_by_id_with_extras ( self , network_get , fixed_get ) : <EOL> db_fixed = dict ( fake_fixed_ip , <EOL> network = test_network . fake_network ) <EOL> fixed_get . return_value = db_fixed <EOL> fixedip = fixed_ip . FixedIP . get_by_id ( self . context , <NUM_LIT> , <EOL> expected_attrs = [ '<STR_LIT>' ] ) <EOL> fixed_get . assert_called_once_with ( self . context , <NUM_LIT> , get_network = True ) <EOL> self . _compare ( fixedip , db_fixed ) <EOL> self . assertEqual ( db_fixed [ '<STR_LIT>' ] [ '<STR_LIT>' ] , fixedip . network . uuid ) <EOL> self . assertFalse ( network_get . called ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_by_address ( self , get ) : <EOL> get . return_value = fake_fixed_ip <EOL> fixedip = fixed_ip . FixedIP . get_by_address ( self . context , '<STR_LIT>' ) <EOL> get . assert_called_once_with ( self . context , '<STR_LIT>' , <EOL> columns_to_join = [ ] ) <EOL> self . _compare ( fixedip , fake_fixed_ip ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_by_address_with_extras ( self , instance_get , network_get , <EOL> fixed_get ) : <EOL> db_fixed = dict ( fake_fixed_ip , network = test_network . fake_network , <EOL> instance = fake_instance . fake_db_instance ( ) ) <EOL> fixed_get . return_value = db_fixed <EOL> fixedip = fixed_ip . FixedIP . get_by_address ( self . context , '<STR_LIT>' , <EOL> expected_attrs = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> fixed_get . assert_called_once_with ( self . context , '<STR_LIT>' , <EOL> columns_to_join = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> self . _compare ( fixedip , db_fixed ) <EOL> self . assertEqual ( db_fixed [ '<STR_LIT>' ] [ '<STR_LIT>' ] , fixedip . network . uuid ) <EOL> self . assertEqual ( db_fixed [ '<STR_LIT>' ] [ '<STR_LIT>' ] , fixedip . instance . uuid ) <EOL> self . assertFalse ( network_get . called ) <EOL> self . assertFalse ( instance_get . called ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_by_address_with_extras_deleted_instance ( self , instance_get , <EOL> network_get , <EOL> fixed_get ) : <EOL> db_fixed = dict ( fake_fixed_ip , network = test_network . fake_network , <EOL> instance = None ) <EOL> fixed_get . return_value = db_fixed <EOL> fixedip = fixed_ip . FixedIP . get_by_address ( self . context , '<STR_LIT>' , <EOL> expected_attrs = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> fixed_get . assert_called_once_with ( self . context , '<STR_LIT>' , <EOL> columns_to_join = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> self . _compare ( fixedip , db_fixed ) <EOL> self . assertEqual ( db_fixed [ '<STR_LIT>' ] [ '<STR_LIT>' ] , fixedip . network . uuid ) <EOL> self . assertIsNone ( fixedip . instance ) <EOL> self . assertFalse ( network_get . called ) <EOL> self . assertFalse ( instance_get . called ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_by_floating_address ( self , get ) : <EOL> get . return_value = fake_fixed_ip <EOL> fixedip = fixed_ip . FixedIP . get_by_floating_address ( self . context , <EOL> '<STR_LIT>' ) <EOL> get . assert_called_once_with ( self . context , '<STR_LIT>' ) <EOL> self . _compare ( fixedip , fake_fixed_ip ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_by_floating_address_none ( self , get ) : <EOL> get . return_value = None <EOL> fixedip = fixed_ip . FixedIP . get_by_floating_address ( self . context , <EOL> '<STR_LIT>' ) <EOL> get . assert_called_once_with ( self . context , '<STR_LIT>' ) <EOL> self . assertIsNone ( fixedip ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_by_network_and_host ( self , get ) : <EOL> get . return_value = fake_fixed_ip <EOL> fixedip = fixed_ip . FixedIP . get_by_network_and_host ( self . context , <EOL> <NUM_LIT> , '<STR_LIT:host>' ) <EOL> get . assert_called_once_with ( self . context , <NUM_LIT> , '<STR_LIT:host>' ) <EOL> self . _compare ( fixedip , fake_fixed_ip ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_associate ( self , associate ) : <EOL> associate . return_value = fake_fixed_ip <EOL> fixedip = fixed_ip . FixedIP . associate ( self . context , '<STR_LIT>' , <EOL> uuids . instance ) <EOL> associate . assert_called_with ( self . context , '<STR_LIT>' , uuids . instance , <EOL> network_id = None , reserved = False , <EOL> virtual_interface_id = None ) <EOL> self . _compare ( fixedip , fake_fixed_ip ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_associate_with_vif ( self , associate ) : <EOL> associate . return_value = fake_fixed_ip <EOL> fixedip = fixed_ip . FixedIP . associate ( self . context , '<STR_LIT>' , <EOL> uuids . instance , <EOL> vif_id = <NUM_LIT:0> ) <EOL> associate . assert_called_with ( self . context , '<STR_LIT>' , <EOL> uuids . instance , <EOL> network_id = None , reserved = False , <EOL> virtual_interface_id = <NUM_LIT:0> ) <EOL> self . _compare ( fixedip , fake_fixed_ip ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_associate_pool ( self , associate ) : <EOL> associate . return_value = fake_fixed_ip <EOL> fixedip = fixed_ip . FixedIP . associate_pool ( self . context , <NUM_LIT> , <EOL> uuids . instance , '<STR_LIT:host>' ) <EOL> associate . assert_called_with ( self . context , <NUM_LIT> , <EOL> instance_uuid = uuids . instance , <EOL> host = '<STR_LIT:host>' , virtual_interface_id = None ) <EOL> self . _compare ( fixedip , fake_fixed_ip ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_associate_pool_with_vif ( self , associate ) : <EOL> associate . return_value = fake_fixed_ip <EOL> fixedip = fixed_ip . FixedIP . associate_pool ( self . context , <NUM_LIT> , <EOL> uuids . instance , '<STR_LIT:host>' , <EOL> vif_id = <NUM_LIT:0> ) <EOL> associate . assert_called_with ( self . context , <NUM_LIT> , <EOL> instance_uuid = uuids . instance , <EOL> host = '<STR_LIT:host>' , virtual_interface_id = <NUM_LIT:0> ) <EOL> self . _compare ( fixedip , fake_fixed_ip ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_disassociate_by_address ( self , disassociate ) : <EOL> fixed_ip . FixedIP . disassociate_by_address ( self . context , '<STR_LIT>' ) <EOL> disassociate . assert_called_with ( self . context , '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_disassociate_all_by_timeout ( self , disassociate ) : <EOL> now = timeutils . utcnow ( ) <EOL> now_tz = timeutils . parse_isotime ( <EOL> utils . isotime ( now ) ) . replace ( <EOL> tzinfo = iso8601 . iso8601 . Utc ( ) ) <EOL> disassociate . return_value = <NUM_LIT> <EOL> result = fixed_ip . FixedIP . disassociate_all_by_timeout ( self . context , <EOL> '<STR_LIT:host>' , now ) <EOL> self . assertEqual ( <NUM_LIT> , result ) <EOL> args , kwargs = disassociate . call_args_list [ <NUM_LIT:0> ] <EOL> self . assertEqual ( now_tz , args [ <NUM_LIT:2> ] ) <EOL> self . assertEqual ( ( self . context , '<STR_LIT:host>' ) , args [ : <NUM_LIT:2> ] ) <EOL> self . assertEqual ( { } , kwargs ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_create ( self , create ) : <EOL> create . return_value = fake_fixed_ip <EOL> fixedip = fixed_ip . FixedIP ( context = self . context , address = '<STR_LIT>' ) <EOL> fixedip . create ( ) <EOL> create . assert_called_once_with ( <EOL> self . context , { '<STR_LIT:address>' : '<STR_LIT>' } ) <EOL> self . _compare ( fixedip , fake_fixed_ip ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_save ( self , update ) : <EOL> update . return_value = fake_fixed_ip <EOL> fixedip = fixed_ip . FixedIP ( context = self . context , address = '<STR_LIT>' , <EOL> instance_uuid = uuids . instance ) <EOL> self . assertRaises ( exception . ObjectActionError , fixedip . save ) <EOL> fixedip . obj_reset_changes ( [ '<STR_LIT:address>' ] ) <EOL> fixedip . save ( ) <EOL> update . assert_called_once_with ( self . context , '<STR_LIT>' , <EOL> { '<STR_LIT>' : uuids . instance } ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_disassociate ( self , disassociate ) : <EOL> fixedip = fixed_ip . FixedIP ( context = self . context , address = '<STR_LIT>' , <EOL> instance_uuid = uuids . instance ) <EOL> fixedip . obj_reset_changes ( ) <EOL> fixedip . disassociate ( ) <EOL> disassociate . assert_called_once_with ( self . context , '<STR_LIT>' ) <EOL> self . assertIsNone ( fixedip . instance_uuid ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_all ( self , get_all ) : <EOL> get_all . return_value = [ fake_fixed_ip ] <EOL> fixedips = fixed_ip . FixedIPList . get_all ( self . context ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( fixedips ) ) <EOL> get_all . assert_called_once_with ( self . context ) <EOL> self . _compare ( fixedips [ <NUM_LIT:0> ] , fake_fixed_ip ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_by_instance ( self , get ) : <EOL> get . return_value = [ fake_fixed_ip ] <EOL> fixedips = fixed_ip . FixedIPList . get_by_instance_uuid ( self . context , <EOL> uuids . instance ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( fixedips ) ) <EOL> get . assert_called_once_with ( self . context , uuids . instance ) <EOL> self . _compare ( fixedips [ <NUM_LIT:0> ] , fake_fixed_ip ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_by_host ( self , get ) : <EOL> get . return_value = [ fake_fixed_ip ] <EOL> fixedips = fixed_ip . FixedIPList . get_by_host ( self . context , '<STR_LIT:host>' ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( fixedips ) ) <EOL> get . assert_called_once_with ( self . context , '<STR_LIT:host>' ) <EOL> self . _compare ( fixedips [ <NUM_LIT:0> ] , fake_fixed_ip ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_by_virtual_interface_id ( self , get ) : <EOL> get . return_value = [ fake_fixed_ip ] <EOL> fixedips = fixed_ip . FixedIPList . get_by_virtual_interface_id ( <EOL> self . context , <NUM_LIT> ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( fixedips ) ) <EOL> get . assert_called_once_with ( self . context , <NUM_LIT> ) <EOL> self . _compare ( fixedips [ <NUM_LIT:0> ] , fake_fixed_ip ) <EOL> def test_floating_ips_do_not_lazy_load ( self ) : <EOL> fixedip = fixed_ip . FixedIP ( ) <EOL> self . assertRaises ( NotImplementedError , lambda : fixedip . floating_ips ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_bulk_create ( self , bulk ) : <EOL> fixed_ips = [ fixed_ip . FixedIP ( address = '<STR_LIT>' ) , <EOL> fixed_ip . FixedIP ( address = '<STR_LIT>' ) ] <EOL> fixed_ip . FixedIPList . bulk_create ( self . context , fixed_ips ) <EOL> bulk . assert_called_once_with ( self . context , <EOL> [ { '<STR_LIT:address>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:address>' : '<STR_LIT>' } ] ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_by_network ( self , get ) : <EOL> info = { '<STR_LIT:address>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : uuids . instance , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT:5> ) , <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT:5> ) , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> get . return_value = [ info ] <EOL> fixed_ips = fixed_ip . FixedIPList . get_by_network ( <EOL> self . context , { '<STR_LIT:id>' : <NUM_LIT:0> } , host = '<STR_LIT>' ) <EOL> get . assert_called_once_with ( self . context , <NUM_LIT:0> , host = '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( fixed_ips ) ) <EOL> fip = fixed_ips [ <NUM_LIT:0> ] <EOL> self . assertEqual ( '<STR_LIT>' , str ( fip . address ) ) <EOL> self . assertEqual ( uuids . instance , fip . instance_uuid ) <EOL> self . assertEqual ( <NUM_LIT:0> , fip . network_id ) <EOL> self . assertEqual ( <NUM_LIT:1> , fip . virtual_interface_id ) <EOL> self . assertTrue ( fip . allocated ) <EOL> self . assertTrue ( fip . leased ) <EOL> self . assertEqual ( uuids . instance , fip . instance . uuid ) <EOL> self . assertEqual ( '<STR_LIT>' , fip . instance . hostname ) <EOL> self . assertIsInstance ( fip . instance . created_at , datetime . datetime ) <EOL> self . assertIsInstance ( fip . instance . updated_at , datetime . datetime ) <EOL> self . assertEqual ( <NUM_LIT:1> , fip . virtual_interface . id ) <EOL> self . assertEqual ( info [ '<STR_LIT>' ] , fip . virtual_interface . address ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_backport_default_route ( self , mock_get ) : <EOL> info = { '<STR_LIT:address>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : uuids . instance , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT:5> ) , <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT:5> ) , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> mock_get . return_value = [ info ] <EOL> fixed_ips = fixed_ip . FixedIPList . get_by_network ( <EOL> self . context , { '<STR_LIT:id>' : <NUM_LIT:0> } , host = '<STR_LIT>' ) <EOL> primitive = fixed_ips [ <NUM_LIT:0> ] . obj_to_primitive ( ) <EOL> self . assertIn ( '<STR_LIT>' , primitive [ '<STR_LIT>' ] ) <EOL> versions = ovo_base . obj_tree_get_versions ( '<STR_LIT>' ) <EOL> fixed_ips [ <NUM_LIT:0> ] . obj_make_compatible_from_manifest ( <EOL> primitive [ '<STR_LIT>' ] , <EOL> target_version = '<STR_LIT>' , <EOL> version_manifest = versions ) <EOL> self . assertNotIn ( '<STR_LIT>' , primitive [ '<STR_LIT>' ] ) <EOL> class TestFixedIPObject ( test_objects . _LocalTest , <EOL> _TestFixedIPObject ) : <EOL> pass <EOL> class TestRemoteFixedIPObject ( test_objects . _RemoteTest , <EOL> _TestFixedIPObject ) : <EOL> pass </s>
