<s> import copy <EOL> from django . core . urlresolvers import reverse <EOL> from django import http <EOL> from mox import IgnoreArg <EOL> from mox import IsA <EOL> from openstack_dashboard import api <EOL> from openstack_dashboard . dashboards . project . routers . extensions . routerrules import rulemanager <EOL> from openstack_dashboard . dashboards . project . routers import tables <EOL> from openstack_dashboard . test import helpers as test <EOL> from openstack_dashboard . usage import quotas <EOL> class RouterMixin : <EOL> @ test . create_stubs ( { <EOL> api . neutron : ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> } ) <EOL> def _get_detail ( self , router ) : <EOL> api . neutron . router_get ( IsA ( http . HttpRequest ) , router . id ) . AndReturn ( router ) <EOL> api . neutron . port_list ( IsA ( http . HttpRequest ) , <EOL> device_id = router . id ) . AndReturn ( [ self . ports . first ( ) ] ) <EOL> self . _mock_external_network_get ( router ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . get ( reverse ( '<STR_LIT>' <EOL> '<STR_LIT>' % self . DASHBOARD , <EOL> args = [ router . id ] ) ) <EOL> return res <EOL> def _mock_external_network_list ( self , alter_ids = False ) : <EOL> search_opts = { '<STR_LIT>' : True } <EOL> ext_nets = [ n for n in self . networks . list ( ) if n [ '<STR_LIT>' ] ] <EOL> if alter_ids : <EOL> for ext_net in ext_nets : <EOL> ext_net . id += '<STR_LIT>' <EOL> api . neutron . network_list ( <EOL> IsA ( http . HttpRequest ) , <EOL> ** search_opts ) . AndReturn ( ext_nets ) <EOL> def _mock_external_network_get ( self , router ) : <EOL> ext_net_id = router . external_gateway_info [ '<STR_LIT>' ] <EOL> ext_net = self . networks . list ( ) [ <NUM_LIT:2> ] <EOL> api . neutron . network_get ( IsA ( http . HttpRequest ) , ext_net_id , <EOL> expand_subnet = False ) . AndReturn ( ext_net ) <EOL> def _mock_network_list ( self , tenant_id ) : <EOL> api . neutron . network_list ( <EOL> IsA ( http . HttpRequest ) , <EOL> shared = False , <EOL> tenant_id = tenant_id ) . AndReturn ( self . networks . list ( ) ) <EOL> api . neutron . network_list ( <EOL> IsA ( http . HttpRequest ) , <EOL> shared = True ) . AndReturn ( [ ] ) <EOL> class RouterTests ( RouterMixin , test . TestCase ) : <EOL> DASHBOARD = '<STR_LIT>' <EOL> INDEX_URL = reverse ( '<STR_LIT>' % DASHBOARD ) <EOL> DETAIL_PATH = '<STR_LIT>' % DASHBOARD <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> quotas : ( '<STR_LIT>' , ) } ) <EOL> def test_index ( self ) : <EOL> quota_data = self . quota_usages . first ( ) <EOL> quota_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] = <NUM_LIT:5> <EOL> api . neutron . router_list ( <EOL> IsA ( http . HttpRequest ) , <EOL> tenant_id = self . tenant . id , <EOL> search_opts = None ) . AndReturn ( self . routers . list ( ) ) <EOL> quotas . tenant_quota_usages ( <EOL> IsA ( http . HttpRequest ) ) . MultipleTimes ( ) . AndReturn ( quota_data ) <EOL> self . _mock_external_network_list ( ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . get ( self . INDEX_URL ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' % self . DASHBOARD ) <EOL> routers = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( routers , self . routers . list ( ) ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> quotas : ( '<STR_LIT>' , ) } ) <EOL> def test_index_router_list_exception ( self ) : <EOL> quota_data = self . quota_usages . first ( ) <EOL> quota_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] = <NUM_LIT:5> <EOL> api . neutron . router_list ( <EOL> IsA ( http . HttpRequest ) , <EOL> tenant_id = self . tenant . id , <EOL> search_opts = None ) . MultipleTimes ( ) . AndRaise ( self . exceptions . neutron ) <EOL> quotas . tenant_quota_usages ( <EOL> IsA ( http . HttpRequest ) ) . MultipleTimes ( ) . AndReturn ( quota_data ) <EOL> self . _mock_external_network_list ( ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . get ( self . INDEX_URL ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' % self . DASHBOARD ) <EOL> self . assertEqual ( len ( res . context [ '<STR_LIT>' ] . data ) , <NUM_LIT:0> ) <EOL> self . assertMessageCount ( res , error = <NUM_LIT:1> ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> quotas : ( '<STR_LIT>' , ) } ) <EOL> def test_set_external_network_empty ( self ) : <EOL> router = self . routers . first ( ) <EOL> quota_data = self . quota_usages . first ( ) <EOL> quota_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] = <NUM_LIT:5> <EOL> api . neutron . router_list ( <EOL> IsA ( http . HttpRequest ) , <EOL> tenant_id = self . tenant . id , <EOL> search_opts = None ) . MultipleTimes ( ) . AndReturn ( [ router ] ) <EOL> quotas . tenant_quota_usages ( <EOL> IsA ( http . HttpRequest ) ) . MultipleTimes ( ) . AndReturn ( quota_data ) <EOL> self . _mock_external_network_list ( alter_ids = True ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . get ( self . INDEX_URL ) <EOL> table_data = res . context [ '<STR_LIT>' ] . data <EOL> self . assertEqual ( len ( table_data ) , <NUM_LIT:1> ) <EOL> self . assertIn ( '<STR_LIT>' , <EOL> table_data [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' % self . DASHBOARD ) <EOL> self . assertMessageCount ( res , error = <NUM_LIT:1> ) <EOL> def test_router_detail ( self ) : <EOL> router = self . routers . first ( ) <EOL> res = self . _get_detail ( router ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' % self . DASHBOARD ) <EOL> ports = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( ports , [ self . ports . first ( ) ] ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , ) } ) <EOL> def test_router_detail_exception ( self ) : <EOL> router = self . routers . first ( ) <EOL> api . neutron . router_get ( IsA ( http . HttpRequest ) , router . id ) . AndRaise ( self . exceptions . neutron ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . get ( reverse ( '<STR_LIT>' <EOL> '<STR_LIT>' % self . DASHBOARD , <EOL> args = [ router . id ] ) ) <EOL> self . assertRedirectsNoFollow ( res , self . INDEX_URL ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , ) , <EOL> quotas : ( '<STR_LIT>' , ) } ) <EOL> def test_router_delete ( self ) : <EOL> router = self . routers . first ( ) <EOL> quota_data = self . quota_usages . first ( ) <EOL> quota_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] = <NUM_LIT:5> <EOL> api . neutron . router_list ( <EOL> IsA ( http . HttpRequest ) , <EOL> tenant_id = self . tenant . id , <EOL> search_opts = None ) . AndReturn ( self . routers . list ( ) ) <EOL> quotas . tenant_quota_usages ( <EOL> IsA ( http . HttpRequest ) ) . MultipleTimes ( ) . AndReturn ( quota_data ) <EOL> self . _mock_external_network_list ( ) <EOL> api . neutron . router_list ( <EOL> IsA ( http . HttpRequest ) , <EOL> tenant_id = self . tenant . id , <EOL> search_opts = None ) . AndReturn ( self . routers . list ( ) ) <EOL> self . _mock_external_network_list ( ) <EOL> api . neutron . router_list ( <EOL> IsA ( http . HttpRequest ) , <EOL> tenant_id = self . tenant . id , <EOL> search_opts = None ) . AndReturn ( self . routers . list ( ) ) <EOL> self . _mock_external_network_list ( ) <EOL> api . neutron . port_list ( IsA ( http . HttpRequest ) , <EOL> device_id = router . id , device_owner = IgnoreArg ( ) ) . AndReturn ( [ ] ) <EOL> api . neutron . router_delete ( IsA ( http . HttpRequest ) , router . id ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . get ( self . INDEX_URL ) <EOL> formData = { '<STR_LIT:action>' : '<STR_LIT>' + router . id } <EOL> res = self . client . post ( self . INDEX_URL , formData , follow = True ) <EOL> self . assertNoFormErrors ( res ) <EOL> self . assertMessageCount ( response = res , success = <NUM_LIT:1> ) <EOL> self . assertIn ( '<STR_LIT>' + router . name , res . content ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , ) , <EOL> quotas : ( '<STR_LIT>' , ) } ) <EOL> def test_router_with_interface_delete ( self ) : <EOL> router = self . routers . first ( ) <EOL> ports = self . ports . list ( ) <EOL> quota_data = self . quota_usages . first ( ) <EOL> quota_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] = <NUM_LIT:5> <EOL> api . neutron . router_list ( <EOL> IsA ( http . HttpRequest ) , <EOL> tenant_id = self . tenant . id , <EOL> search_opts = None ) . AndReturn ( self . routers . list ( ) ) <EOL> quotas . tenant_quota_usages ( <EOL> IsA ( http . HttpRequest ) ) . MultipleTimes ( ) . AndReturn ( quota_data ) <EOL> self . _mock_external_network_list ( ) <EOL> api . neutron . router_list ( <EOL> IsA ( http . HttpRequest ) , <EOL> tenant_id = self . tenant . id , <EOL> search_opts = None ) . AndReturn ( self . routers . list ( ) ) <EOL> self . _mock_external_network_list ( ) <EOL> api . neutron . router_list ( <EOL> IsA ( http . HttpRequest ) , <EOL> tenant_id = self . tenant . id , <EOL> search_opts = None ) . AndReturn ( self . routers . list ( ) ) <EOL> self . _mock_external_network_list ( ) <EOL> api . neutron . port_list ( IsA ( http . HttpRequest ) , <EOL> device_id = router . id , device_owner = IgnoreArg ( ) ) . AndReturn ( ports ) <EOL> for port in ports : <EOL> api . neutron . router_remove_interface ( IsA ( http . HttpRequest ) , <EOL> router . id , port_id = port . id ) <EOL> api . neutron . router_delete ( IsA ( http . HttpRequest ) , router . id ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . get ( self . INDEX_URL ) <EOL> formData = { '<STR_LIT:action>' : '<STR_LIT>' + router . id } <EOL> res = self . client . post ( self . INDEX_URL , formData , follow = True ) <EOL> self . assertNoFormErrors ( res ) <EOL> self . assertMessageCount ( response = res , success = <NUM_LIT:1> ) <EOL> self . assertIn ( '<STR_LIT>' + router . name , res . content ) <EOL> class RouterActionTests ( RouterMixin , test . TestCase ) : <EOL> DASHBOARD = '<STR_LIT>' <EOL> INDEX_URL = reverse ( '<STR_LIT>' % DASHBOARD ) <EOL> DETAIL_PATH = '<STR_LIT>' % DASHBOARD <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , ) } ) <EOL> def test_router_create_post ( self ) : <EOL> router = self . routers . first ( ) <EOL> api . neutron . get_feature_permission ( IsA ( http . HttpRequest ) , <EOL> "<STR_LIT>" , "<STR_LIT>" ) . AndReturn ( False ) <EOL> api . neutron . get_feature_permission ( IsA ( http . HttpRequest ) , <EOL> "<STR_LIT>" , "<STR_LIT>" ) . AndReturn ( False ) <EOL> api . neutron . router_create ( IsA ( http . HttpRequest ) , name = router . name ) . AndReturn ( router ) <EOL> self . mox . ReplayAll ( ) <EOL> form_data = { '<STR_LIT:name>' : router . name } <EOL> url = reverse ( '<STR_LIT>' % self . DASHBOARD ) <EOL> res = self . client . post ( url , form_data ) <EOL> self . assertNoFormErrors ( res ) <EOL> self . assertRedirectsNoFollow ( res , self . INDEX_URL ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , ) } ) <EOL> def test_router_create_post_mode_server_default ( self ) : <EOL> router = self . routers . first ( ) <EOL> api . neutron . get_feature_permission ( IsA ( http . HttpRequest ) , <EOL> "<STR_LIT>" , "<STR_LIT>" ) . AndReturn ( True ) <EOL> api . neutron . get_feature_permission ( IsA ( http . HttpRequest ) , <EOL> "<STR_LIT>" , "<STR_LIT>" ) . AndReturn ( True ) <EOL> api . neutron . router_create ( IsA ( http . HttpRequest ) , name = router . name ) . AndReturn ( router ) <EOL> self . mox . ReplayAll ( ) <EOL> form_data = { '<STR_LIT:name>' : router . name , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> url = reverse ( '<STR_LIT>' % self . DASHBOARD ) <EOL> res = self . client . post ( url , form_data ) <EOL> self . assertNoFormErrors ( res ) <EOL> self . assertRedirectsNoFollow ( res , self . INDEX_URL ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , ) } ) <EOL> def test_dvr_ha_router_create_post ( self ) : <EOL> router = self . routers . first ( ) <EOL> api . neutron . get_feature_permission ( IsA ( http . HttpRequest ) , <EOL> "<STR_LIT>" , "<STR_LIT>" ) . MultipleTimes ( ) . AndReturn ( True ) <EOL> api . neutron . get_feature_permission ( IsA ( http . HttpRequest ) , <EOL> "<STR_LIT>" , "<STR_LIT>" ) . MultipleTimes ( ) . AndReturn ( True ) <EOL> param = { '<STR_LIT:name>' : router . name , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True } <EOL> api . neutron . router_create ( IsA ( http . HttpRequest ) , ** param ) . AndReturn ( router ) <EOL> self . mox . ReplayAll ( ) <EOL> form_data = { '<STR_LIT:name>' : router . name , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> url = reverse ( '<STR_LIT>' % self . DASHBOARD ) <EOL> res = self . client . post ( url , form_data ) <EOL> self . assertNoFormErrors ( res ) <EOL> self . assertRedirectsNoFollow ( res , self . INDEX_URL ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , ) } ) <EOL> def test_router_create_post_exception_error_case_409 ( self ) : <EOL> router = self . routers . first ( ) <EOL> api . neutron . get_feature_permission ( IsA ( http . HttpRequest ) , <EOL> "<STR_LIT>" , "<STR_LIT>" ) . MultipleTimes ( ) . AndReturn ( False ) <EOL> api . neutron . get_feature_permission ( IsA ( http . HttpRequest ) , <EOL> "<STR_LIT>" , "<STR_LIT>" ) . AndReturn ( False ) <EOL> self . exceptions . neutron . status_code = <NUM_LIT> <EOL> api . neutron . router_create ( IsA ( http . HttpRequest ) , name = router . name ) . AndRaise ( self . exceptions . neutron ) <EOL> self . mox . ReplayAll ( ) <EOL> form_data = { '<STR_LIT:name>' : router . name } <EOL> url = reverse ( '<STR_LIT>' % self . DASHBOARD ) <EOL> res = self . client . post ( url , form_data ) <EOL> self . assertNoFormErrors ( res ) <EOL> self . assertRedirectsNoFollow ( res , self . INDEX_URL ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , ) } ) <EOL> def test_router_create_post_exception_error_case_non_409 ( self ) : <EOL> router = self . routers . first ( ) <EOL> api . neutron . get_feature_permission ( IsA ( http . HttpRequest ) , <EOL> "<STR_LIT>" , "<STR_LIT>" ) . MultipleTimes ( ) . AndReturn ( False ) <EOL> api . neutron . get_feature_permission ( IsA ( http . HttpRequest ) , <EOL> "<STR_LIT>" , "<STR_LIT>" ) . MultipleTimes ( ) . AndReturn ( False ) <EOL> self . exceptions . neutron . status_code = <NUM_LIT> <EOL> api . neutron . router_create ( IsA ( http . HttpRequest ) , name = router . name ) . AndRaise ( self . exceptions . neutron ) <EOL> self . mox . ReplayAll ( ) <EOL> form_data = { '<STR_LIT:name>' : router . name } <EOL> url = reverse ( '<STR_LIT>' % self . DASHBOARD ) <EOL> res = self . client . post ( url , form_data ) <EOL> self . assertNoFormErrors ( res ) <EOL> self . assertRedirectsNoFollow ( res , self . INDEX_URL ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) } ) <EOL> def _test_router_update_get ( self , dvr_enabled = False , <EOL> current_dvr = False , <EOL> ha_enabled = False ) : <EOL> router = [ r for r in self . routers . list ( ) <EOL> if r . distributed == current_dvr ] [ <NUM_LIT:0> ] <EOL> api . neutron . router_get ( IsA ( http . HttpRequest ) , router . id ) . AndReturn ( router ) <EOL> api . neutron . get_feature_permission ( IsA ( http . HttpRequest ) , <EOL> "<STR_LIT>" , "<STR_LIT>" ) . AndReturn ( dvr_enabled ) <EOL> self . mox . ReplayAll ( ) <EOL> url = reverse ( '<STR_LIT>' % self . DASHBOARD , <EOL> args = [ router . id ] ) <EOL> return self . client . get ( url ) <EOL> def test_router_update_get_dvr_disabled ( self ) : <EOL> res = self . _test_router_update_get ( dvr_enabled = False ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' ) <EOL> self . assertNotContains ( res , '<STR_LIT>' ) <EOL> self . assertNotContains ( res , '<STR_LIT>' ) <EOL> def test_router_update_get_dvr_enabled_mode_centralized ( self ) : <EOL> res = self . _test_router_update_get ( dvr_enabled = True , current_dvr = False ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' ) <EOL> self . assertContains ( res , '<STR_LIT>' ) <EOL> self . assertContains ( <EOL> res , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> html = True ) <EOL> self . assertContains ( <EOL> res , <EOL> '<STR_LIT>' , <EOL> html = True ) <EOL> def test_router_update_get_dvr_enabled_mode_distributed ( self ) : <EOL> res = self . _test_router_update_get ( dvr_enabled = True , current_dvr = True ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' ) <EOL> self . assertContains ( res , '<STR_LIT>' ) <EOL> self . assertContains ( <EOL> res , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> html = True ) <EOL> self . assertNotContains ( res , '<STR_LIT>' ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) } ) <EOL> def test_router_update_post_dvr_ha_disabled ( self ) : <EOL> router = self . routers . first ( ) <EOL> api . neutron . get_feature_permission ( IsA ( http . HttpRequest ) , <EOL> "<STR_LIT>" , "<STR_LIT>" ) . AndReturn ( False ) <EOL> api . neutron . router_update ( IsA ( http . HttpRequest ) , router . id , <EOL> name = router . name , <EOL> admin_state_up = router . admin_state_up ) . AndReturn ( router ) <EOL> api . neutron . router_get ( IsA ( http . HttpRequest ) , router . id ) . AndReturn ( router ) <EOL> self . mox . ReplayAll ( ) <EOL> form_data = { '<STR_LIT>' : router . id , <EOL> '<STR_LIT:name>' : router . name , <EOL> '<STR_LIT>' : router . admin_state_up } <EOL> url = reverse ( '<STR_LIT>' % self . DASHBOARD , <EOL> args = [ router . id ] ) <EOL> res = self . client . post ( url , form_data ) <EOL> self . assertRedirectsNoFollow ( res , self . INDEX_URL ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) } ) <EOL> def test_router_update_post_dvr_ha_enabled ( self ) : <EOL> router = self . routers . first ( ) <EOL> api . neutron . get_feature_permission ( IsA ( http . HttpRequest ) , <EOL> "<STR_LIT>" , "<STR_LIT>" ) . AndReturn ( True ) <EOL> api . neutron . router_update ( IsA ( http . HttpRequest ) , router . id , <EOL> name = router . name , <EOL> admin_state_up = router . admin_state_up , <EOL> distributed = True ) . AndReturn ( router ) <EOL> api . neutron . router_get ( IsA ( http . HttpRequest ) , router . id ) . AndReturn ( router ) <EOL> self . mox . ReplayAll ( ) <EOL> form_data = { '<STR_LIT>' : router . id , <EOL> '<STR_LIT:name>' : router . name , <EOL> '<STR_LIT>' : router . admin_state_up , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True } <EOL> url = reverse ( '<STR_LIT>' % self . DASHBOARD , <EOL> args = [ router . id ] ) <EOL> res = self . client . post ( url , form_data ) <EOL> self . assertRedirectsNoFollow ( res , self . INDEX_URL ) <EOL> def _test_router_addinterface ( self , raise_error = False ) : <EOL> router = self . routers . first ( ) <EOL> subnet = self . subnets . first ( ) <EOL> port = self . ports . first ( ) <EOL> add_interface = api . neutron . router_add_interface ( <EOL> IsA ( http . HttpRequest ) , router . id , subnet_id = subnet . id ) <EOL> if raise_error : <EOL> add_interface . AndRaise ( self . exceptions . neutron ) <EOL> else : <EOL> add_interface . AndReturn ( { '<STR_LIT>' : subnet . id , <EOL> '<STR_LIT>' : port . id } ) <EOL> api . neutron . port_get ( IsA ( http . HttpRequest ) , port . id ) . AndReturn ( port ) <EOL> self . _check_router_addinterface ( router , subnet ) <EOL> def _check_router_addinterface ( self , router , subnet , ip_address = '<STR_LIT>' ) : <EOL> api . neutron . router_get ( IsA ( http . HttpRequest ) , router . id ) . AndReturn ( router ) <EOL> self . _mock_network_list ( router [ '<STR_LIT>' ] ) <EOL> self . mox . ReplayAll ( ) <EOL> form_data = { '<STR_LIT>' : router . id , <EOL> '<STR_LIT>' : router . name , <EOL> '<STR_LIT>' : subnet . id , <EOL> '<STR_LIT>' : ip_address } <EOL> url = reverse ( '<STR_LIT>' % self . DASHBOARD , <EOL> args = [ router . id ] ) <EOL> res = self . client . post ( url , form_data ) <EOL> self . assertNoFormErrors ( res ) <EOL> detail_url = reverse ( self . DETAIL_PATH , args = [ router . id ] ) <EOL> self . assertRedirectsNoFollow ( res , detail_url ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) } ) <EOL> def test_router_addinterface ( self ) : <EOL> self . _test_router_addinterface ( ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) } ) <EOL> def test_router_addinterface_exception ( self ) : <EOL> self . _test_router_addinterface ( raise_error = True ) <EOL> def _test_router_addinterface_ip_addr ( self , errors = [ ] ) : <EOL> router = self . routers . first ( ) <EOL> subnet = self . subnets . first ( ) <EOL> port = self . ports . first ( ) <EOL> ip_addr = port [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> self . _setup_mock_addinterface_ip_addr ( router , subnet , port , <EOL> ip_addr , errors ) <EOL> self . _check_router_addinterface ( router , subnet , ip_addr ) <EOL> def _setup_mock_addinterface_ip_addr ( self , router , subnet , port , <EOL> ip_addr , errors = [ ] ) : <EOL> subnet_get = api . neutron . subnet_get ( IsA ( http . HttpRequest ) , subnet . id ) <EOL> if '<STR_LIT>' in errors : <EOL> subnet_get . AndRaise ( self . exceptions . neutron ) <EOL> return <EOL> subnet_get . AndReturn ( subnet ) <EOL> params = { '<STR_LIT>' : subnet . network_id , <EOL> '<STR_LIT>' : [ { '<STR_LIT>' : subnet . id , <EOL> '<STR_LIT>' : ip_addr } ] } <EOL> port_create = api . neutron . port_create ( IsA ( http . HttpRequest ) , ** params ) <EOL> if '<STR_LIT>' in errors : <EOL> port_create . AndRaise ( self . exceptions . neutron ) <EOL> return <EOL> port_create . AndReturn ( port ) <EOL> add_inf = api . neutron . router_add_interface ( <EOL> IsA ( http . HttpRequest ) , router . id , port_id = port . id ) <EOL> if '<STR_LIT>' not in errors : <EOL> return <EOL> add_inf . AndRaise ( self . exceptions . neutron ) <EOL> port_delete = api . neutron . port_delete ( IsA ( http . HttpRequest ) , port . id ) <EOL> if '<STR_LIT>' in errors : <EOL> port_delete . AndRaise ( self . exceptions . neutron ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) } ) <EOL> def test_router_addinterface_ip_addr ( self ) : <EOL> self . _test_router_addinterface_ip_addr ( ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) } ) <EOL> def test_router_addinterface_ip_addr_exception_subnet_get ( self ) : <EOL> self . _test_router_addinterface_ip_addr ( errors = [ '<STR_LIT>' ] ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) } ) <EOL> def test_router_addinterface_ip_addr_exception_port_create ( self ) : <EOL> self . _test_router_addinterface_ip_addr ( errors = [ '<STR_LIT>' ] ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) } ) <EOL> def test_router_addinterface_ip_addr_exception_add_interface ( self ) : <EOL> self . _test_router_addinterface_ip_addr ( errors = [ '<STR_LIT>' ] ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) } ) <EOL> def test_router_addinterface_ip_addr_exception_port_delete ( self ) : <EOL> self . _test_router_addinterface_ip_addr ( errors = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) } ) <EOL> def test_router_add_gateway ( self ) : <EOL> router = self . routers . first ( ) <EOL> network = self . networks . first ( ) <EOL> api . neutron . router_add_gateway ( <EOL> IsA ( http . HttpRequest ) , <EOL> router . id , <EOL> network . id ) . AndReturn ( None ) <EOL> api . neutron . router_get ( <EOL> IsA ( http . HttpRequest ) , router . id ) . AndReturn ( router ) <EOL> search_opts = { '<STR_LIT>' : True } <EOL> api . neutron . network_list ( <EOL> IsA ( http . HttpRequest ) , ** search_opts ) . AndReturn ( [ network ] ) <EOL> self . mox . ReplayAll ( ) <EOL> form_data = { '<STR_LIT>' : router . id , <EOL> '<STR_LIT>' : router . name , <EOL> '<STR_LIT>' : network . id } <EOL> url = reverse ( '<STR_LIT>' % self . DASHBOARD , <EOL> args = [ router . id ] ) <EOL> res = self . client . post ( url , form_data ) <EOL> self . assertNoFormErrors ( res ) <EOL> detail_url = self . INDEX_URL <EOL> self . assertRedirectsNoFollow ( res , detail_url ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) } ) <EOL> def test_router_add_gateway_exception ( self ) : <EOL> router = self . routers . first ( ) <EOL> network = self . networks . first ( ) <EOL> api . neutron . router_add_gateway ( <EOL> IsA ( http . HttpRequest ) , <EOL> router . id , <EOL> network . id ) . AndRaise ( self . exceptions . neutron ) <EOL> api . neutron . router_get ( <EOL> IsA ( http . HttpRequest ) , router . id ) . AndReturn ( router ) <EOL> search_opts = { '<STR_LIT>' : True } <EOL> api . neutron . network_list ( <EOL> IsA ( http . HttpRequest ) , ** search_opts ) . AndReturn ( [ network ] ) <EOL> self . mox . ReplayAll ( ) <EOL> form_data = { '<STR_LIT>' : router . id , <EOL> '<STR_LIT>' : router . name , <EOL> '<STR_LIT>' : network . id } <EOL> url = reverse ( '<STR_LIT>' % self . DASHBOARD , <EOL> args = [ router . id ] ) <EOL> res = self . client . post ( url , form_data ) <EOL> self . assertNoFormErrors ( res ) <EOL> detail_url = self . INDEX_URL <EOL> self . assertRedirectsNoFollow ( res , detail_url ) <EOL> class RouterRuleTests ( RouterMixin , test . TestCase ) : <EOL> DASHBOARD = '<STR_LIT>' <EOL> INDEX_URL = reverse ( '<STR_LIT>' % DASHBOARD ) <EOL> DETAIL_PATH = '<STR_LIT>' % DASHBOARD <EOL> def test_extension_hides_without_rules ( self ) : <EOL> router = self . routers . first ( ) <EOL> res = self . _get_detail ( router ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' % self . DASHBOARD ) <EOL> self . assertTemplateNotUsed ( <EOL> res , <EOL> '<STR_LIT>' % self . DASHBOARD ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , ) } ) <EOL> def test_routerrule_detail ( self ) : <EOL> router = self . routers_with_rules . first ( ) <EOL> if self . DASHBOARD == '<STR_LIT>' : <EOL> api . neutron . network_list ( <EOL> IsA ( http . HttpRequest ) , <EOL> shared = False , <EOL> tenant_id = router [ '<STR_LIT>' ] ) . AndReturn ( self . networks . list ( ) ) <EOL> api . neutron . network_list ( <EOL> IsA ( http . HttpRequest ) , <EOL> shared = True ) . AndReturn ( [ ] ) <EOL> res = self . _get_detail ( router ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' % self . DASHBOARD ) <EOL> if self . DASHBOARD == '<STR_LIT>' : <EOL> self . assertTemplateUsed ( <EOL> res , <EOL> '<STR_LIT>' % self . DASHBOARD ) <EOL> rules = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( rules , router [ '<STR_LIT>' ] ) <EOL> def _test_router_addrouterrule ( self , raise_error = False ) : <EOL> pre_router = self . routers_with_rules . first ( ) <EOL> post_router = copy . deepcopy ( pre_router ) <EOL> rule = { '<STR_LIT:source>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:id>' : <NUM_LIT> , <EOL> '<STR_LIT:action>' : '<STR_LIT>' , '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] } <EOL> post_router [ '<STR_LIT>' ] . insert ( <NUM_LIT:0> , rule ) <EOL> api . neutron . router_get ( IsA ( http . HttpRequest ) , <EOL> pre_router . id ) . AndReturn ( pre_router ) <EOL> params = { } <EOL> params [ '<STR_LIT>' ] = rulemanager . format_for_api ( <EOL> post_router [ '<STR_LIT>' ] ) <EOL> router_update = api . neutron . router_update ( IsA ( http . HttpRequest ) , <EOL> pre_router . id , ** params ) <EOL> if raise_error : <EOL> router_update . AndRaise ( self . exceptions . neutron ) <EOL> else : <EOL> router_update . AndReturn ( { '<STR_LIT>' : post_router } ) <EOL> self . mox . ReplayAll ( ) <EOL> form_data = { '<STR_LIT>' : pre_router . id , <EOL> '<STR_LIT:source>' : rule [ '<STR_LIT:source>' ] , <EOL> '<STR_LIT>' : rule [ '<STR_LIT>' ] , <EOL> '<STR_LIT:action>' : rule [ '<STR_LIT:action>' ] , <EOL> '<STR_LIT>' : '<STR_LIT:U+002C>' . join ( rule [ '<STR_LIT>' ] ) } <EOL> url = reverse ( '<STR_LIT>' % self . DASHBOARD , <EOL> args = [ pre_router . id ] ) <EOL> res = self . client . post ( url , form_data ) <EOL> self . assertNoFormErrors ( res ) <EOL> detail_url = reverse ( self . DETAIL_PATH , args = [ pre_router . id ] ) <EOL> self . assertRedirectsNoFollow ( res , detail_url ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) } ) <EOL> def test_router_addrouterrule ( self ) : <EOL> self . _test_router_addrouterrule ( ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) } ) <EOL> def test_router_addrouterrule_exception ( self ) : <EOL> self . _test_router_addrouterrule ( raise_error = True ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) } ) <EOL> def test_router_removerouterrule ( self ) : <EOL> pre_router = self . routers_with_rules . first ( ) <EOL> post_router = copy . deepcopy ( pre_router ) <EOL> rule = post_router [ '<STR_LIT>' ] . pop ( ) <EOL> api . neutron . router_get ( IsA ( http . HttpRequest ) , <EOL> pre_router . id ) . AndReturn ( pre_router ) <EOL> params = { } <EOL> params [ '<STR_LIT>' ] = rulemanager . format_for_api ( <EOL> post_router [ '<STR_LIT>' ] ) <EOL> api . neutron . router_get ( IsA ( http . HttpRequest ) , <EOL> pre_router . id ) . AndReturn ( pre_router ) <EOL> router_update = api . neutron . router_update ( IsA ( http . HttpRequest ) , <EOL> pre_router . id , ** params ) <EOL> router_update . AndReturn ( { '<STR_LIT>' : post_router } ) <EOL> api . neutron . router_get ( IsA ( http . HttpRequest ) , <EOL> pre_router . id ) . AndReturn ( pre_router ) <EOL> api . neutron . port_list ( IsA ( http . HttpRequest ) , <EOL> device_id = pre_router . id ) . AndReturn ( [ self . ports . first ( ) ] ) <EOL> self . _mock_external_network_get ( pre_router ) <EOL> self . mox . ReplayAll ( ) <EOL> form_rule_id = rule [ '<STR_LIT:source>' ] + rule [ '<STR_LIT>' ] <EOL> form_data = { '<STR_LIT>' : pre_router . id , <EOL> '<STR_LIT:action>' : '<STR_LIT>' % form_rule_id } <EOL> url = reverse ( self . DETAIL_PATH , args = [ pre_router . id ] ) <EOL> res = self . client . post ( url , form_data ) <EOL> self . assertNoFormErrors ( res ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) } ) <EOL> def test_router_resetrouterrules ( self ) : <EOL> pre_router = self . routers_with_rules . first ( ) <EOL> post_router = copy . deepcopy ( pre_router ) <EOL> default_rules = [ { '<STR_LIT:source>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:action>' : '<STR_LIT>' , '<STR_LIT>' : [ ] , '<STR_LIT:id>' : '<STR_LIT:2>' } ] <EOL> del post_router [ '<STR_LIT>' ] [ : ] <EOL> post_router [ '<STR_LIT>' ] . extend ( default_rules ) <EOL> api . neutron . router_get ( IsA ( http . HttpRequest ) , <EOL> pre_router . id ) . AndReturn ( post_router ) <EOL> params = { } <EOL> params [ '<STR_LIT>' ] = rulemanager . format_for_api ( <EOL> post_router [ '<STR_LIT>' ] ) <EOL> router_update = api . neutron . router_update ( IsA ( http . HttpRequest ) , <EOL> pre_router . id , ** params ) <EOL> router_update . AndReturn ( { '<STR_LIT>' : post_router } ) <EOL> api . neutron . router_get ( IsA ( http . HttpRequest ) , <EOL> pre_router . id ) . AndReturn ( post_router ) <EOL> api . neutron . port_list ( IsA ( http . HttpRequest ) , <EOL> device_id = pre_router . id ) . AndReturn ( [ self . ports . first ( ) ] ) <EOL> self . _mock_external_network_get ( pre_router ) <EOL> self . _mock_network_list ( pre_router [ '<STR_LIT>' ] ) <EOL> api . neutron . router_get ( IsA ( http . HttpRequest ) , <EOL> pre_router . id ) . AndReturn ( post_router ) <EOL> self . mox . ReplayAll ( ) <EOL> form_data = { '<STR_LIT>' : pre_router . id , <EOL> '<STR_LIT:action>' : '<STR_LIT>' } <EOL> url = reverse ( self . DETAIL_PATH , args = [ pre_router . id ] ) <EOL> res = self . client . post ( url , form_data ) <EOL> self . assertNoFormErrors ( res ) <EOL> class RouterViewTests ( RouterMixin , test . TestCase ) : <EOL> DASHBOARD = '<STR_LIT>' <EOL> INDEX_URL = reverse ( '<STR_LIT>' % DASHBOARD ) <EOL> @ test . create_stubs ( { api . neutron : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> quotas : ( '<STR_LIT>' , ) } ) <EOL> def test_create_button_disabled_when_quota_exceeded ( self ) : <EOL> quota_data = self . quota_usages . first ( ) <EOL> quota_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> api . neutron . router_list ( <EOL> IsA ( http . HttpRequest ) , <EOL> tenant_id = self . tenant . id , <EOL> search_opts = None ) . AndReturn ( self . routers . list ( ) ) <EOL> quotas . tenant_quota_usages ( <EOL> IsA ( http . HttpRequest ) ) . MultipleTimes ( ) . AndReturn ( quota_data ) <EOL> self . _mock_external_network_list ( ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . get ( self . INDEX_URL ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' ) <EOL> routers = res . context [ '<STR_LIT>' ] . data <EOL> self . assertItemsEqual ( routers , self . routers . list ( ) ) <EOL> create_link = tables . CreateRouter ( ) <EOL> url = create_link . get_link_url ( ) <EOL> classes = ( list ( create_link . get_default_classes ( ) ) <EOL> + list ( create_link . classes ) ) <EOL> link_name = "<STR_LIT>" % ( unicode ( create_link . verbose_name ) , <EOL> "<STR_LIT>" ) <EOL> expected_string = "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" % ( url , link_name , "<STR_LIT:U+0020>" . join ( classes ) , link_name ) <EOL> self . assertContains ( res , expected_string , html = True , <EOL> msg_prefix = "<STR_LIT>" ) </s>
