<s> import mock <EOL> from oslo_serialization import jsonutils <EOL> from nova import objects <EOL> from nova . tests . unit . objects import test_objects <EOL> FAKE_UUID = '<STR_LIT>' <EOL> FAKE_REQUEST_UUID = '<STR_LIT>' <EOL> fake_pci_requests = [ <EOL> { '<STR_LIT:count>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : [ { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : FAKE_REQUEST_UUID } , <EOL> { '<STR_LIT:count>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : [ { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : FAKE_REQUEST_UUID } , <EOL> ] <EOL> fake_legacy_pci_requests = [ <EOL> { '<STR_LIT:count>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : [ { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ] , <EOL> '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:count>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : [ { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ] , <EOL> '<STR_LIT>' : '<STR_LIT>' } , <EOL> ] <EOL> class _TestInstancePCIRequests ( object ) : <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_by_instance_uuid ( self , mock_get ) : <EOL> mock_get . return_value = { <EOL> '<STR_LIT>' : FAKE_UUID , <EOL> '<STR_LIT>' : jsonutils . dumps ( fake_pci_requests ) , <EOL> } <EOL> requests = objects . InstancePCIRequests . get_by_instance_uuid ( <EOL> self . context , FAKE_UUID ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( requests . requests ) ) <EOL> for index , request in enumerate ( requests . requests ) : <EOL> self . assertEqual ( fake_pci_requests [ index ] [ '<STR_LIT>' ] , <EOL> request . alias_name ) <EOL> self . assertEqual ( fake_pci_requests [ index ] [ '<STR_LIT:count>' ] , <EOL> request . count ) <EOL> self . assertEqual ( fake_pci_requests [ index ] [ '<STR_LIT>' ] , <EOL> [ dict ( x . items ( ) ) for x in request . spec ] ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_by_instance_uuid_and_newness ( self , mock_get ) : <EOL> pcir = objects . InstancePCIRequests <EOL> mock_get . return_value = objects . InstancePCIRequests ( <EOL> instance_uuid = '<STR_LIT>' , <EOL> requests = [ objects . InstancePCIRequest ( count = <NUM_LIT:1> , is_new = False ) , <EOL> objects . InstancePCIRequest ( count = <NUM_LIT:2> , is_new = True ) ] ) <EOL> old_req = pcir . get_by_instance_uuid_and_newness ( self . context , <EOL> '<STR_LIT>' , <EOL> False ) <EOL> mock_get . return_value = objects . InstancePCIRequests ( <EOL> instance_uuid = '<STR_LIT>' , <EOL> requests = [ objects . InstancePCIRequest ( count = <NUM_LIT:1> , is_new = False ) , <EOL> objects . InstancePCIRequest ( count = <NUM_LIT:2> , is_new = True ) ] ) <EOL> new_req = pcir . get_by_instance_uuid_and_newness ( self . context , <EOL> '<STR_LIT>' , <EOL> True ) <EOL> self . assertEqual ( <NUM_LIT:1> , old_req . requests [ <NUM_LIT:0> ] . count ) <EOL> self . assertEqual ( <NUM_LIT:2> , new_req . requests [ <NUM_LIT:0> ] . count ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_by_instance_current ( self , mock_get ) : <EOL> instance = objects . Instance ( uuid = '<STR_LIT>' , <EOL> system_metadata = { } ) <EOL> objects . InstancePCIRequests . get_by_instance ( self . context , <EOL> instance ) <EOL> mock_get . assert_called_once_with ( self . context , '<STR_LIT>' ) <EOL> def test_get_by_instance_legacy ( self ) : <EOL> fakesysmeta = { <EOL> '<STR_LIT>' : jsonutils . dumps ( [ fake_legacy_pci_requests [ <NUM_LIT:0> ] ] ) , <EOL> '<STR_LIT>' : jsonutils . dumps ( [ fake_legacy_pci_requests [ <NUM_LIT:1> ] ] ) , <EOL> } <EOL> instance = objects . Instance ( uuid = '<STR_LIT>' , <EOL> system_metadata = fakesysmeta ) <EOL> requests = objects . InstancePCIRequests . get_by_instance ( self . context , <EOL> instance ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( requests . requests ) ) <EOL> self . assertEqual ( '<STR_LIT>' , requests . requests [ <NUM_LIT:0> ] . alias_name ) <EOL> self . assertFalse ( requests . requests [ <NUM_LIT:0> ] . is_new ) <EOL> self . assertEqual ( '<STR_LIT>' , requests . requests [ <NUM_LIT:1> ] . alias_name ) <EOL> self . assertTrue ( requests . requests [ <NUM_LIT:1> ] . is_new ) <EOL> def test_new_compatibility ( self ) : <EOL> request = objects . InstancePCIRequest ( is_new = False ) <EOL> self . assertFalse ( request . new ) <EOL> def test_backport_1_0 ( self ) : <EOL> requests = objects . InstancePCIRequests ( <EOL> requests = [ objects . InstancePCIRequest ( count = <NUM_LIT:1> , <EOL> request_id = FAKE_UUID ) , <EOL> objects . InstancePCIRequest ( count = <NUM_LIT:2> , <EOL> request_id = FAKE_UUID ) ] ) <EOL> primitive = requests . obj_to_primitive ( target_version = '<STR_LIT:1.0>' ) <EOL> backported = objects . InstancePCIRequests . obj_from_primitive ( <EOL> primitive ) <EOL> self . assertEqual ( '<STR_LIT:1.0>' , backported . VERSION ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( backported . requests ) ) <EOL> self . assertFalse ( backported . requests [ <NUM_LIT:0> ] . obj_attr_is_set ( '<STR_LIT>' ) ) <EOL> self . assertFalse ( backported . requests [ <NUM_LIT:1> ] . obj_attr_is_set ( '<STR_LIT>' ) ) <EOL> def test_obj_from_db ( self ) : <EOL> req = objects . InstancePCIRequests . obj_from_db ( None , FAKE_UUID , None ) <EOL> self . assertEqual ( FAKE_UUID , req . instance_uuid ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( req . requests ) ) <EOL> db_req = jsonutils . dumps ( fake_pci_requests ) <EOL> req = objects . InstancePCIRequests . obj_from_db ( None , FAKE_UUID , db_req ) <EOL> self . assertEqual ( FAKE_UUID , req . instance_uuid ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( req . requests ) ) <EOL> self . assertEqual ( '<STR_LIT>' , req . requests [ <NUM_LIT:0> ] . alias_name ) <EOL> def test_from_request_spec_instance_props ( self ) : <EOL> requests = objects . InstancePCIRequests ( <EOL> requests = [ objects . InstancePCIRequest ( count = <NUM_LIT:1> , <EOL> request_id = FAKE_UUID , <EOL> spec = [ { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ] ) <EOL> ] , <EOL> instance_uuid = FAKE_UUID ) <EOL> result = jsonutils . to_primitive ( requests ) <EOL> result = objects . InstancePCIRequests . from_request_spec_instance_props ( <EOL> result ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( result . requests ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , result . requests [ <NUM_LIT:0> ] . count ) <EOL> self . assertEqual ( FAKE_UUID , result . requests [ <NUM_LIT:0> ] . request_id ) <EOL> self . assertEqual ( [ { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ] , <EOL> result . requests [ <NUM_LIT:0> ] . spec ) <EOL> class TestInstancePCIRequests ( test_objects . _LocalTest , <EOL> _TestInstancePCIRequests ) : <EOL> pass <EOL> class TestRemoteInstancePCIRequests ( test_objects . _RemoteTest , <EOL> _TestInstancePCIRequests ) : <EOL> pass </s>
