<s> from webob import exc <EOL> from nova . api . openstack . compute import pci <EOL> from nova . api . openstack import wsgi <EOL> from nova import context <EOL> from nova import exception <EOL> from nova import objects <EOL> from nova . objects import fields <EOL> from nova . objects import pci_device_pool <EOL> from nova import test <EOL> from nova . tests . unit . api . openstack import fakes <EOL> from nova . tests . unit . objects import test_pci_device <EOL> from nova . tests import uuidsentinel as uuids <EOL> pci_stats = [ { "<STR_LIT:count>" : <NUM_LIT:3> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:1> } ] <EOL> fake_compute_node = objects . ComputeNode ( <EOL> pci_device_pools = pci_device_pool . from_pci_stats ( pci_stats ) ) <EOL> class FakeResponse ( wsgi . ResponseObject ) : <EOL> pass <EOL> class PciServerControllerTestV21 ( test . NoDBTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( PciServerControllerTestV21 , self ) . setUp ( ) <EOL> self . controller = pci . PciServerController ( ) <EOL> self . fake_obj = { '<STR_LIT>' : { '<STR_LIT>' : { } , <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } } <EOL> self . fake_list = { '<STR_LIT>' : [ { '<STR_LIT>' : { } , <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ] } <EOL> self . _create_fake_instance ( ) <EOL> self . _create_fake_pci_device ( ) <EOL> self . pci_device . claim ( self . inst ) <EOL> self . pci_device . allocate ( self . inst ) <EOL> def _create_fake_instance ( self ) : <EOL> self . inst = objects . Instance ( ) <EOL> self . inst . uuid = uuids . instance <EOL> self . inst . pci_devices = objects . PciDeviceList ( ) <EOL> def _create_fake_pci_device ( self ) : <EOL> def fake_pci_device_get_by_addr ( ctxt , id , addr ) : <EOL> return test_pci_device . fake_db_dev <EOL> ctxt = context . get_admin_context ( ) <EOL> self . stub_out ( '<STR_LIT>' , <EOL> fake_pci_device_get_by_addr ) <EOL> self . pci_device = objects . PciDevice . get_by_dev_addr ( ctxt , <NUM_LIT:1> , '<STR_LIT:a>' ) <EOL> def test_show ( self ) : <EOL> def fake_get_db_instance ( id ) : <EOL> return self . inst <EOL> resp = FakeResponse ( self . fake_obj , '<STR_LIT>' ) <EOL> req = fakes . HTTPRequest . blank ( '<STR_LIT>' , use_admin_context = True ) <EOL> self . stubs . Set ( req , '<STR_LIT>' , fake_get_db_instance ) <EOL> self . controller . show ( req , resp , '<STR_LIT:1>' ) <EOL> self . assertEqual ( [ { '<STR_LIT:id>' : <NUM_LIT:1> } ] , <EOL> resp . obj [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> def test_detail ( self ) : <EOL> def fake_get_db_instance ( id ) : <EOL> return self . inst <EOL> resp = FakeResponse ( self . fake_list , '<STR_LIT>' ) <EOL> req = fakes . HTTPRequest . blank ( '<STR_LIT>' , <EOL> use_admin_context = True ) <EOL> self . stubs . Set ( req , '<STR_LIT>' , fake_get_db_instance ) <EOL> self . controller . detail ( req , resp ) <EOL> self . assertEqual ( [ { '<STR_LIT:id>' : <NUM_LIT:1> } ] , <EOL> resp . obj [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> class PciHypervisorControllerTestV21 ( test . NoDBTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( PciHypervisorControllerTestV21 , self ) . setUp ( ) <EOL> self . controller = pci . PciHypervisorController ( ) <EOL> self . fake_objs = dict ( hypervisors = [ <EOL> dict ( id = <NUM_LIT:1> , <EOL> service = dict ( id = <NUM_LIT:1> , host = "<STR_LIT>" ) , <EOL> hypervisor_type = "<STR_LIT>" , <EOL> hypervisor_version = <NUM_LIT:3> , <EOL> hypervisor_hostname = "<STR_LIT>" ) ] ) <EOL> self . fake_obj = dict ( hypervisor = dict ( <EOL> id = <NUM_LIT:1> , <EOL> service = dict ( id = <NUM_LIT:1> , host = "<STR_LIT>" ) , <EOL> hypervisor_type = "<STR_LIT>" , <EOL> hypervisor_version = <NUM_LIT:3> , <EOL> hypervisor_hostname = "<STR_LIT>" ) ) <EOL> def test_show ( self ) : <EOL> def fake_get_db_compute_node ( id ) : <EOL> return fake_compute_node <EOL> req = fakes . HTTPRequest . blank ( '<STR_LIT>' , <EOL> use_admin_context = True ) <EOL> resp = FakeResponse ( self . fake_obj , '<STR_LIT>' ) <EOL> self . stubs . Set ( req , '<STR_LIT>' , fake_get_db_compute_node ) <EOL> self . controller . show ( req , resp , '<STR_LIT:1>' ) <EOL> self . assertIn ( '<STR_LIT>' , resp . obj [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( pci_stats [ <NUM_LIT:0> ] , <EOL> resp . obj [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> def test_detail ( self ) : <EOL> def fake_get_db_compute_node ( id ) : <EOL> return fake_compute_node <EOL> req = fakes . HTTPRequest . blank ( '<STR_LIT>' , <EOL> use_admin_context = True ) <EOL> resp = FakeResponse ( self . fake_objs , '<STR_LIT>' ) <EOL> self . stubs . Set ( req , '<STR_LIT>' , fake_get_db_compute_node ) <EOL> self . controller . detail ( req , resp ) <EOL> self . assertIn ( '<STR_LIT>' , resp . obj [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( pci_stats [ <NUM_LIT:0> ] , <EOL> resp . obj [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> class PciControlletestV21 ( test . NoDBTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( PciControlletestV21 , self ) . setUp ( ) <EOL> self . controller = pci . PciController ( ) <EOL> def test_show ( self ) : <EOL> def fake_pci_device_get_by_id ( context , id ) : <EOL> return test_pci_device . fake_db_dev <EOL> self . stub_out ( '<STR_LIT>' , <EOL> fake_pci_device_get_by_id ) <EOL> req = fakes . HTTPRequest . blank ( '<STR_LIT>' , use_admin_context = True ) <EOL> result = self . controller . show ( req , '<STR_LIT:1>' ) <EOL> dist = { '<STR_LIT>' : { '<STR_LIT:address>' : '<STR_LIT:a>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT:i>' , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : fields . PciDeviceType . STANDARD , <EOL> '<STR_LIT:id>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT:label>' : '<STR_LIT:l>' , <EOL> '<STR_LIT>' : '<STR_LIT:p>' , <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:v>' } } <EOL> self . assertEqual ( dist , result ) <EOL> def test_show_error_id ( self ) : <EOL> def fake_pci_device_get_by_id ( context , id ) : <EOL> raise exception . PciDeviceNotFoundById ( id = id ) <EOL> self . stub_out ( '<STR_LIT>' , <EOL> fake_pci_device_get_by_id ) <EOL> req = fakes . HTTPRequest . blank ( '<STR_LIT>' , use_admin_context = True ) <EOL> self . assertRaises ( exc . HTTPNotFound , self . controller . show , req , '<STR_LIT:0>' ) <EOL> def _fake_compute_node_get_all ( self , context ) : <EOL> return [ objects . ComputeNode ( id = <NUM_LIT:1> , <EOL> service_id = <NUM_LIT:1> , <EOL> host = '<STR_LIT>' , <EOL> cpu_info = '<STR_LIT>' , <EOL> disk_available_least = <NUM_LIT:100> ) ] <EOL> def _fake_pci_device_get_all_by_node ( self , context , node ) : <EOL> return [ test_pci_device . fake_db_dev , test_pci_device . fake_db_dev_1 ] <EOL> def test_index ( self ) : <EOL> self . stubs . Set ( self . controller . host_api , '<STR_LIT>' , <EOL> self . _fake_compute_node_get_all ) <EOL> self . stub_out ( '<STR_LIT>' , <EOL> self . _fake_pci_device_get_all_by_node ) <EOL> req = fakes . HTTPRequest . blank ( '<STR_LIT>' , use_admin_context = True ) <EOL> result = self . controller . index ( req ) <EOL> dist = { '<STR_LIT>' : [ test_pci_device . fake_db_dev , <EOL> test_pci_device . fake_db_dev_1 ] } <EOL> for i in range ( len ( result [ '<STR_LIT>' ] ) ) : <EOL> self . assertEqual ( dist [ '<STR_LIT>' ] [ i ] [ '<STR_LIT>' ] , <EOL> result [ '<STR_LIT>' ] [ i ] [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( dist [ '<STR_LIT>' ] [ i ] [ '<STR_LIT:id>' ] , <EOL> result [ '<STR_LIT>' ] [ i ] [ '<STR_LIT:id>' ] ) <EOL> self . assertEqual ( dist [ '<STR_LIT>' ] [ i ] [ '<STR_LIT:status>' ] , <EOL> result [ '<STR_LIT>' ] [ i ] [ '<STR_LIT:status>' ] ) <EOL> self . assertEqual ( dist [ '<STR_LIT>' ] [ i ] [ '<STR_LIT:address>' ] , <EOL> result [ '<STR_LIT>' ] [ i ] [ '<STR_LIT:address>' ] ) <EOL> def test_detail ( self ) : <EOL> self . stubs . Set ( self . controller . host_api , '<STR_LIT>' , <EOL> self . _fake_compute_node_get_all ) <EOL> self . stub_out ( '<STR_LIT>' , <EOL> self . _fake_pci_device_get_all_by_node ) <EOL> req = fakes . HTTPRequest . blank ( '<STR_LIT>' , <EOL> use_admin_context = True ) <EOL> result = self . controller . detail ( req ) <EOL> dist = { '<STR_LIT>' : [ test_pci_device . fake_db_dev , <EOL> test_pci_device . fake_db_dev_1 ] } <EOL> for i in range ( len ( result [ '<STR_LIT>' ] ) ) : <EOL> self . assertEqual ( dist [ '<STR_LIT>' ] [ i ] [ '<STR_LIT>' ] , <EOL> result [ '<STR_LIT>' ] [ i ] [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( dist [ '<STR_LIT>' ] [ i ] [ '<STR_LIT:id>' ] , <EOL> result [ '<STR_LIT>' ] [ i ] [ '<STR_LIT:id>' ] ) <EOL> self . assertEqual ( dist [ '<STR_LIT>' ] [ i ] [ '<STR_LIT:label>' ] , <EOL> result [ '<STR_LIT>' ] [ i ] [ '<STR_LIT:label>' ] ) <EOL> self . assertEqual ( dist [ '<STR_LIT>' ] [ i ] [ '<STR_LIT>' ] , <EOL> result [ '<STR_LIT>' ] [ i ] [ '<STR_LIT>' ] ) <EOL> class PciControllerPolicyEnforcementV21 ( test . NoDBTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( PciControllerPolicyEnforcementV21 , self ) . setUp ( ) <EOL> self . controller = pci . PciController ( ) <EOL> self . req = fakes . HTTPRequest . blank ( '<STR_LIT>' ) <EOL> def _test_policy_failed ( self , action , * args ) : <EOL> rule_name = "<STR_LIT>" % action <EOL> rule = { rule_name : "<STR_LIT>" } <EOL> self . policy . set_rules ( rule ) <EOL> exc = self . assertRaises ( <EOL> exception . PolicyNotAuthorized , getattr ( self . controller , action ) , <EOL> self . req , * args ) <EOL> self . assertEqual ( <EOL> "<STR_LIT>" % rule_name , <EOL> exc . format_message ( ) ) <EOL> def test_index_policy_failed ( self ) : <EOL> self . _test_policy_failed ( '<STR_LIT:index>' ) <EOL> def test_detail_policy_failed ( self ) : <EOL> self . _test_policy_failed ( '<STR_LIT>' ) <EOL> def test_show_policy_failed ( self ) : <EOL> self . _test_policy_failed ( '<STR_LIT>' , <NUM_LIT:1> ) </s>
