<s> from django . test . utils import override_settings <EOL> from openstack_dashboard import api <EOL> from openstack_dashboard import policy <EOL> from openstack_dashboard . test import helpers as test <EOL> class NeutronApiTests ( test . APITestCase ) : <EOL> def test_network_list ( self ) : <EOL> networks = { '<STR_LIT>' : self . api_networks . list ( ) } <EOL> subnets = { '<STR_LIT>' : self . api_subnets . list ( ) } <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . list_networks ( ) . AndReturn ( networks ) <EOL> neutronclient . list_subnets ( ) . AndReturn ( subnets ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . neutron . network_list ( self . request ) <EOL> for n in ret_val : <EOL> self . assertIsInstance ( n , api . neutron . Network ) <EOL> def test_network_get ( self ) : <EOL> network = { '<STR_LIT>' : self . api_networks . first ( ) } <EOL> subnet = { '<STR_LIT>' : self . api_subnets . first ( ) } <EOL> network_id = self . api_networks . first ( ) [ '<STR_LIT:id>' ] <EOL> subnet_id = self . api_networks . first ( ) [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . show_network ( network_id ) . AndReturn ( network ) <EOL> neutronclient . show_subnet ( subnet_id ) . AndReturn ( subnet ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . neutron . network_get ( self . request , network_id ) <EOL> self . assertIsInstance ( ret_val , api . neutron . Network ) <EOL> def test_network_create ( self ) : <EOL> network = { '<STR_LIT>' : self . api_networks . first ( ) } <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> form_data = { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . request . user . project_id } } <EOL> neutronclient . create_network ( body = form_data ) . AndReturn ( network ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . neutron . network_create ( self . request , name = '<STR_LIT>' ) <EOL> self . assertIsInstance ( ret_val , api . neutron . Network ) <EOL> def test_network_update ( self ) : <EOL> network = { '<STR_LIT>' : self . api_networks . first ( ) } <EOL> network_id = self . api_networks . first ( ) [ '<STR_LIT:id>' ] <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> form_data = { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' } } <EOL> neutronclient . update_network ( network_id , body = form_data ) . AndReturn ( network ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . neutron . network_update ( self . request , network_id , <EOL> name = '<STR_LIT>' ) <EOL> self . assertIsInstance ( ret_val , api . neutron . Network ) <EOL> def test_network_delete ( self ) : <EOL> network_id = self . api_networks . first ( ) [ '<STR_LIT:id>' ] <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . delete_network ( network_id ) <EOL> self . mox . ReplayAll ( ) <EOL> api . neutron . network_delete ( self . request , network_id ) <EOL> def test_subnet_list ( self ) : <EOL> subnets = { '<STR_LIT>' : self . api_subnets . list ( ) } <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . list_subnets ( ) . AndReturn ( subnets ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . neutron . subnet_list ( self . request ) <EOL> for n in ret_val : <EOL> self . assertIsInstance ( n , api . neutron . Subnet ) <EOL> def test_subnet_get ( self ) : <EOL> subnet = { '<STR_LIT>' : self . api_subnets . first ( ) } <EOL> subnet_id = self . api_subnets . first ( ) [ '<STR_LIT:id>' ] <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . show_subnet ( subnet_id ) . AndReturn ( subnet ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . neutron . subnet_get ( self . request , subnet_id ) <EOL> self . assertIsInstance ( ret_val , api . neutron . Subnet ) <EOL> def test_subnet_create ( self ) : <EOL> subnet_data = self . api_subnets . first ( ) <EOL> params = { '<STR_LIT>' : subnet_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : subnet_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT:name>' : subnet_data [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' : subnet_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : subnet_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : subnet_data [ '<STR_LIT>' ] } <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . create_subnet ( body = { '<STR_LIT>' : params } ) . AndReturn ( { '<STR_LIT>' : subnet_data } ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . neutron . subnet_create ( self . request , ** params ) <EOL> self . assertIsInstance ( ret_val , api . neutron . Subnet ) <EOL> def test_subnet_update ( self ) : <EOL> subnet_data = self . api_subnets . first ( ) <EOL> subnet_id = subnet_data [ '<STR_LIT:id>' ] <EOL> params = { '<STR_LIT:name>' : subnet_data [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' : subnet_data [ '<STR_LIT>' ] } <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . update_subnet ( subnet_id , body = { '<STR_LIT>' : params } ) . AndReturn ( { '<STR_LIT>' : subnet_data } ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . neutron . subnet_update ( self . request , subnet_id , ** params ) <EOL> self . assertIsInstance ( ret_val , api . neutron . Subnet ) <EOL> def test_subnet_delete ( self ) : <EOL> subnet_id = self . api_subnets . first ( ) [ '<STR_LIT:id>' ] <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . delete_subnet ( subnet_id ) <EOL> self . mox . ReplayAll ( ) <EOL> api . neutron . subnet_delete ( self . request , subnet_id ) <EOL> def test_port_list ( self ) : <EOL> ports = { '<STR_LIT>' : self . api_ports . list ( ) } <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . list_ports ( ) . AndReturn ( ports ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . neutron . port_list ( self . request ) <EOL> for p in ret_val : <EOL> self . assertIsInstance ( p , api . neutron . Port ) <EOL> def test_port_get ( self ) : <EOL> port = { '<STR_LIT:port>' : self . api_ports . first ( ) } <EOL> port_id = self . api_ports . first ( ) [ '<STR_LIT:id>' ] <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . show_port ( port_id ) . AndReturn ( port ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . neutron . port_get ( self . request , port_id ) <EOL> self . assertIsInstance ( ret_val , api . neutron . Port ) <EOL> def test_port_create ( self ) : <EOL> port_data = self . api_ports . first ( ) <EOL> params = { '<STR_LIT>' : port_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : port_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT:name>' : port_data [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' : port_data [ '<STR_LIT>' ] } <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . create_port ( body = { '<STR_LIT:port>' : params } ) . AndReturn ( { '<STR_LIT:port>' : port_data } ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . neutron . port_create ( self . request , ** params ) <EOL> self . assertIsInstance ( ret_val , api . neutron . Port ) <EOL> self . assertEqual ( api . neutron . Port ( port_data ) . id , ret_val . id ) <EOL> def test_port_update ( self ) : <EOL> port_data = self . api_ports . first ( ) <EOL> port_id = port_data [ '<STR_LIT:id>' ] <EOL> params = { '<STR_LIT:name>' : port_data [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' : port_data [ '<STR_LIT>' ] } <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . update_port ( port_id , body = { '<STR_LIT:port>' : params } ) . AndReturn ( { '<STR_LIT:port>' : port_data } ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . neutron . port_update ( self . request , port_id , ** params ) <EOL> self . assertIsInstance ( ret_val , api . neutron . Port ) <EOL> self . assertEqual ( api . neutron . Port ( port_data ) . id , ret_val . id ) <EOL> def test_port_delete ( self ) : <EOL> port_id = self . api_ports . first ( ) [ '<STR_LIT:id>' ] <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . delete_port ( port_id ) <EOL> self . mox . ReplayAll ( ) <EOL> api . neutron . port_delete ( self . request , port_id ) <EOL> def test_router_list ( self ) : <EOL> routers = { '<STR_LIT>' : self . api_routers . list ( ) } <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . list_routers ( ) . AndReturn ( routers ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . neutron . router_list ( self . request ) <EOL> for n in ret_val : <EOL> self . assertIsInstance ( n , api . neutron . Router ) <EOL> def test_router_get ( self ) : <EOL> router = { '<STR_LIT>' : self . api_routers . first ( ) } <EOL> router_id = self . api_routers . first ( ) [ '<STR_LIT:id>' ] <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . show_router ( router_id ) . AndReturn ( router ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . neutron . router_get ( self . request , router_id ) <EOL> self . assertIsInstance ( ret_val , api . neutron . Router ) <EOL> def test_router_create ( self ) : <EOL> router = { '<STR_LIT>' : self . api_routers . first ( ) } <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> form_data = { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . request . user . project_id } } <EOL> neutronclient . create_router ( body = form_data ) . AndReturn ( router ) <EOL> self . mox . ReplayAll ( ) <EOL> ret_val = api . neutron . router_create ( self . request , name = '<STR_LIT>' ) <EOL> self . assertIsInstance ( ret_val , api . neutron . Router ) <EOL> def test_router_delete ( self ) : <EOL> router_id = self . api_routers . first ( ) [ '<STR_LIT:id>' ] <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . delete_router ( router_id ) <EOL> self . mox . ReplayAll ( ) <EOL> api . neutron . router_delete ( self . request , router_id ) <EOL> def test_router_add_interface ( self ) : <EOL> subnet_id = self . api_subnets . first ( ) [ '<STR_LIT:id>' ] <EOL> router_id = self . api_routers . first ( ) [ '<STR_LIT:id>' ] <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> form_data = { '<STR_LIT>' : subnet_id } <EOL> neutronclient . add_interface_router ( <EOL> router_id , form_data ) . AndReturn ( None ) <EOL> self . mox . ReplayAll ( ) <EOL> api . neutron . router_add_interface ( <EOL> self . request , router_id , subnet_id = subnet_id ) <EOL> def test_router_remove_interface ( self ) : <EOL> router_id = self . api_routers . first ( ) [ '<STR_LIT:id>' ] <EOL> fake_port = self . api_ports . first ( ) [ '<STR_LIT:id>' ] <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . remove_interface_router ( <EOL> router_id , { '<STR_LIT>' : fake_port } ) <EOL> self . mox . ReplayAll ( ) <EOL> api . neutron . router_remove_interface ( <EOL> self . request , router_id , port_id = fake_port ) <EOL> def test_is_extension_supported ( self ) : <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . list_extensions ( ) . MultipleTimes ( ) . AndReturn ( { '<STR_LIT>' : self . api_extensions . list ( ) } ) <EOL> self . mox . ReplayAll ( ) <EOL> self . assertTrue ( <EOL> api . neutron . is_extension_supported ( self . request , '<STR_LIT>' ) ) <EOL> self . assertFalse ( <EOL> api . neutron . is_extension_supported ( self . request , '<STR_LIT>' ) ) <EOL> @ override_settings ( OPENSTACK_NEUTRON_NETWORK = { '<STR_LIT>' : <EOL> True } , <EOL> POLICY_CHECK_FUNCTION = None ) <EOL> def _test_get_dvr_permission_dvr_supported ( self , dvr_enabled ) : <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> extensions = self . api_extensions . list ( ) <EOL> if not dvr_enabled : <EOL> extensions = [ ext for ext in extensions if ext [ '<STR_LIT>' ] != '<STR_LIT>' ] <EOL> neutronclient . list_extensions ( ) . AndReturn ( { '<STR_LIT>' : extensions } ) <EOL> self . mox . ReplayAll ( ) <EOL> self . assertEqual ( dvr_enabled , <EOL> api . neutron . get_feature_permission ( self . request , <EOL> '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_get_dvr_permission_dvr_supported ( self ) : <EOL> self . _test_get_dvr_permission_dvr_supported ( dvr_enabled = True ) <EOL> def test_get_dvr_permission_dvr_not_supported ( self ) : <EOL> self . _test_get_dvr_permission_dvr_supported ( dvr_enabled = False ) <EOL> @ override_settings ( OPENSTACK_NEUTRON_NETWORK = { '<STR_LIT>' : <EOL> True } , <EOL> POLICY_CHECK_FUNCTION = policy . check ) <EOL> def _test_get_dvr_permission_with_policy_check ( self , policy_check_allowed , <EOL> operation ) : <EOL> self . mox . StubOutWithMock ( policy , '<STR_LIT>' ) <EOL> if operation == "<STR_LIT>" : <EOL> role = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> elif operation == "<STR_LIT>" : <EOL> role = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> policy . check ( role , self . request ) . AndReturn ( policy_check_allowed ) <EOL> if policy_check_allowed : <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> neutronclient . list_extensions ( ) . AndReturn ( { '<STR_LIT>' : self . api_extensions . list ( ) } ) <EOL> self . mox . ReplayAll ( ) <EOL> self . assertEqual ( policy_check_allowed , <EOL> api . neutron . get_feature_permission ( self . request , <EOL> '<STR_LIT>' , operation ) ) <EOL> def test_get_dvr_permission_with_policy_check_allowed ( self ) : <EOL> self . _test_get_dvr_permission_with_policy_check ( True , "<STR_LIT>" ) <EOL> def test_get_dvr_permission_with_policy_check_disallowed ( self ) : <EOL> self . _test_get_dvr_permission_with_policy_check ( False , "<STR_LIT>" ) <EOL> def test_get_dvr_permission_create_with_policy_check_allowed ( self ) : <EOL> self . _test_get_dvr_permission_with_policy_check ( True , "<STR_LIT>" ) <EOL> def test_get_dvr_permission_create_with_policy_check_disallowed ( self ) : <EOL> self . _test_get_dvr_permission_with_policy_check ( False , "<STR_LIT>" ) <EOL> @ override_settings ( OPENSTACK_NEUTRON_NETWORK = { '<STR_LIT>' : <EOL> False } ) <EOL> def test_get_dvr_permission_dvr_disabled_by_config ( self ) : <EOL> self . assertFalse ( api . neutron . get_feature_permission ( self . request , <EOL> '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> @ override_settings ( OPENSTACK_NEUTRON_NETWORK = { '<STR_LIT>' : <EOL> True } , <EOL> POLICY_CHECK_FUNCTION = policy . check ) <EOL> def test_get_dvr_permission_dvr_unsupported_operation ( self ) : <EOL> self . assertRaises ( ValueError , <EOL> api . neutron . get_feature_permission , <EOL> self . request , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ override_settings ( OPENSTACK_NEUTRON_NETWORK = { } ) <EOL> def test_get_dvr_permission_dvr_default_config ( self ) : <EOL> self . assertFalse ( api . neutron . get_feature_permission ( self . request , <EOL> '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> @ override_settings ( OPENSTACK_NEUTRON_NETWORK = { } ) <EOL> def test_get_dvr_permission_router_ha_default_config ( self ) : <EOL> self . assertFalse ( api . neutron . get_feature_permission ( self . request , <EOL> '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> @ override_settings ( OPENSTACK_NEUTRON_NETWORK = { '<STR_LIT>' : True } , <EOL> POLICY_CHECK_FUNCTION = policy . check ) <EOL> def _test_get_router_ha_permission_with_policy_check ( self , ha_enabled ) : <EOL> self . mox . StubOutWithMock ( policy , '<STR_LIT>' ) <EOL> role = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> policy . check ( role , self . request ) . AndReturn ( True ) <EOL> neutronclient = self . stub_neutronclient ( ) <EOL> if ha_enabled : <EOL> extensions = self . api_extensions . list ( ) <EOL> else : <EOL> extensions = { } <EOL> neutronclient . list_extensions ( ) . AndReturn ( { '<STR_LIT>' : extensions } ) <EOL> self . mox . ReplayAll ( ) <EOL> self . assertEqual ( ha_enabled , <EOL> api . neutron . get_feature_permission ( self . request , <EOL> '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_get_router_ha_permission_with_l3_ha_extension ( self ) : <EOL> self . _test_get_router_ha_permission_with_policy_check ( True ) <EOL> def test_get_router_ha_permission_without_l3_ha_extension ( self ) : <EOL> self . _test_get_router_ha_permission_with_policy_check ( False ) </s>
