<s> from exceptions import Exception <EOL> from mox import IsA <EOL> from rack import context <EOL> from rack import db <EOL> from rack import exception <EOL> from rack . openstack . common import jsonutils <EOL> from rack . resourceoperator import manager <EOL> from rack import test <EOL> from rack . tests . api import fakes <EOL> import uuid <EOL> import webob <EOL> GID = unicode ( uuid . uuid4 ( ) ) <EOL> NETWORK_ID1 = unicode ( uuid . uuid4 ( ) ) <EOL> NETWORK_ID2 = unicode ( uuid . uuid4 ( ) ) <EOL> RO_HOST_NAME = "<STR_LIT>" <EOL> NEUTRON_NW_ID = "<STR_LIT>" <EOL> def fake_create_db ( context , values ) : <EOL> values [ "<STR_LIT>" ] = NETWORK_ID1 <EOL> return values <EOL> def fake_group_get_by_gid ( context , gid ) : <EOL> return { "<STR_LIT>" : gid , <EOL> "<STR_LIT:status>" : "<STR_LIT>" <EOL> } <EOL> def fake_select_destinations ( context , request_spec , filter_properties ) : <EOL> return { "<STR_LIT:host>" : RO_HOST_NAME } <EOL> def fake_network_get_all ( context , gid ) : <EOL> return _return_base_network_list ( context , gid ) <EOL> def fake_network_get_all_empty_list ( contextm , gid ) : <EOL> return [ ] <EOL> def fake_raise_exception ( ) : <EOL> raise Exception ( ) <EOL> def _return_base_network_list ( context , gid ) : <EOL> return [ <EOL> { "<STR_LIT>" : NETWORK_ID1 , <EOL> "<STR_LIT>" : "<STR_LIT>" + NETWORK_ID1 , <EOL> "<STR_LIT>" : gid , <EOL> "<STR_LIT>" : context . user_id , <EOL> "<STR_LIT>" : context . project_id , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:True>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:status>" : "<STR_LIT>" } , <EOL> { "<STR_LIT>" : NETWORK_ID2 , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : gid , <EOL> "<STR_LIT>" : context . user_id , <EOL> "<STR_LIT>" : context . project_id , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:True>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:status>" : "<STR_LIT>" } <EOL> ] <EOL> def fake_network_get_by_network_id ( context , gid , network_id ) : <EOL> network_dict = _return_base_network_list ( context , gid ) [ <NUM_LIT:0> ] <EOL> network_dict [ "<STR_LIT>" ] = [ ] <EOL> return network_dict <EOL> def fake_network_delete ( context , gid , network_id ) : <EOL> return { <EOL> "<STR_LIT>" : NEUTRON_NW_ID , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> def get_request ( url , method , body = None ) : <EOL> req = webob . Request . blank ( url ) <EOL> req . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> req . method = method <EOL> if body is not None : <EOL> req . body = jsonutils . dumps ( body ) <EOL> return req <EOL> class FakeContext ( context . RequestContext ) : <EOL> def elevated ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _elevated = super ( FakeContext , self ) . elevated ( ) <EOL> return self . _elevated <EOL> class NetworksTest ( test . NoDBTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( NetworksTest , self ) . setUp ( ) <EOL> self . stubs . Set ( db , "<STR_LIT>" , fake_group_get_by_gid ) <EOL> self . user_id = '<STR_LIT>' <EOL> self . project_id = '<STR_LIT>' <EOL> self . context = FakeContext ( self . user_id , self . project_id ) <EOL> self . app = fakes . wsgi_app ( ) <EOL> def test_index ( self ) : <EOL> self . stubs . Set ( db , "<STR_LIT>" , fake_network_get_all ) <EOL> self . mox . StubOutWithMock ( manager . ResourceOperator , "<STR_LIT>" ) <EOL> manager . ResourceOperator . network_list ( <EOL> IsA ( context . RequestContext ) , <EOL> IsA ( list ) ) . AndReturn ( _return_base_network_list ( self . context , GID ) ) <EOL> self . mox . ReplayAll ( ) <EOL> expect = _return_base_network_list ( self . context , GID ) <EOL> expect [ <NUM_LIT:0> ] . update ( ext_router_id = "<STR_LIT>" ) <EOL> expect [ <NUM_LIT:1> ] . update ( ext_router_id = "<STR_LIT>" ) <EOL> expect [ <NUM_LIT:0> ] . update ( name = "<STR_LIT>" ) <EOL> expect [ <NUM_LIT:1> ] . update ( name = "<STR_LIT>" ) <EOL> expect [ <NUM_LIT:0> ] . update ( cidr = "<STR_LIT>" ) <EOL> expect [ <NUM_LIT:1> ] . update ( cidr = "<STR_LIT>" ) <EOL> url = "<STR_LIT>" + GID + "<STR_LIT>" <EOL> req = get_request ( url , '<STR_LIT:GET>' ) <EOL> res = req . get_response ( self . app ) <EOL> body = jsonutils . loads ( res . body ) <EOL> self . assertEqual ( <NUM_LIT:200> , res . status_code ) <EOL> for i in range ( len ( body [ "<STR_LIT>" ] ) ) : <EOL> for key in body [ "<STR_LIT>" ] [ i ] : <EOL> self . assertEqual ( expect [ i ] [ key ] , body [ "<STR_LIT>" ] [ i ] [ key ] ) <EOL> def test_index_return_empty_list ( self ) : <EOL> self . stubs . Set ( db , "<STR_LIT>" , fake_network_get_all_empty_list ) <EOL> self . mox . StubOutWithMock ( manager . ResourceOperator , "<STR_LIT>" ) <EOL> manager . ResourceOperator . network_list ( <EOL> IsA ( context . RequestContext ) , [ ] ) . AndReturn ( [ ] ) <EOL> self . mox . ReplayAll ( ) <EOL> url = '<STR_LIT>' + GID + '<STR_LIT>' <EOL> req = get_request ( url , '<STR_LIT:GET>' ) <EOL> res = req . get_response ( self . app ) <EOL> body = jsonutils . loads ( res . body ) <EOL> expected_body = { "<STR_LIT>" : [ ] } <EOL> self . assertEqual ( expected_body , body ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT:200> ) <EOL> def test_index_validate_exception_by_gid_format ( self ) : <EOL> not_uuid_gid = "<STR_LIT>" <EOL> url = '<STR_LIT>' + not_uuid_gid + '<STR_LIT>' <EOL> req = get_request ( url , '<STR_LIT:GET>' ) <EOL> res = req . get_response ( self . app ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) <EOL> def test_show ( self ) : <EOL> def _mock_data ( ) : <EOL> return { <EOL> "<STR_LIT>" : NETWORK_ID1 , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : GID , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:True>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:status>" : "<STR_LIT>" } <EOL> mock_data = _mock_data ( ) <EOL> self . mox . StubOutWithMock ( db , "<STR_LIT>" ) <EOL> self . mox . StubOutWithMock ( manager . ResourceOperator , "<STR_LIT>" ) <EOL> db . network_get_by_network_id ( IsA ( context . RequestContext ) , <EOL> GID , NETWORK_ID1 ) . AndReturn ( _mock_data ( ) ) <EOL> manager . ResourceOperator . network_show ( IsA ( context . RequestContext ) , <EOL> IsA ( dict ) ) <EOL> self . mox . ReplayAll ( ) <EOL> url = '<STR_LIT>' + GID + '<STR_LIT>' <EOL> req = get_request ( url + "<STR_LIT:/>" + NETWORK_ID1 , '<STR_LIT:GET>' ) <EOL> res = req . get_response ( self . app ) <EOL> body = jsonutils . loads ( res . body ) <EOL> expect = mock_data <EOL> expect . update ( ext_router_id = "<STR_LIT>" ) <EOL> expect . update ( name = "<STR_LIT>" ) <EOL> expect . update ( cidr = "<STR_LIT>" ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT:200> ) <EOL> for key in body [ "<STR_LIT>" ] : <EOL> self . assertEqual ( expect [ key ] , body [ "<STR_LIT>" ] [ key ] ) <EOL> def test_show_validate_exception_by_gid_format ( self ) : <EOL> not_uuid_gid = "<STR_LIT>" <EOL> url = '<STR_LIT>' + not_uuid_gid + '<STR_LIT>' + NETWORK_ID1 <EOL> req = get_request ( url , '<STR_LIT:GET>' ) <EOL> res = req . get_response ( self . app ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) <EOL> def test_show_validate_exception_by_network_id_format ( self ) : <EOL> url = '<STR_LIT>' + GID + '<STR_LIT>' + NETWORK_ID1 + "<STR_LIT>" <EOL> req = get_request ( url , '<STR_LIT:GET>' ) <EOL> res = req . get_response ( self . app ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) <EOL> def test_show_exception_networknotfound ( self ) : <EOL> self . mox . StubOutWithMock ( db , "<STR_LIT>" ) <EOL> db . network_get_by_network_id ( IsA ( context . RequestContext ) , GID , <EOL> NETWORK_ID1 ) . AndRaise ( exception . NetworkNotFound ( network_id = NETWORK_ID1 ) ) <EOL> self . mox . ReplayAll ( ) <EOL> url = '<STR_LIT>' + GID + '<STR_LIT>' <EOL> req = get_request ( url + "<STR_LIT:/>" + NETWORK_ID1 , '<STR_LIT:GET>' ) <EOL> res = req . get_response ( self . app ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) <EOL> def test_create ( self ) : <EOL> self . stubs . Set ( db , "<STR_LIT>" , fake_group_get_by_gid ) <EOL> self . stubs . Set ( db , "<STR_LIT>" , fake_create_db ) <EOL> self . mox . StubOutWithMock ( manager . ResourceOperator , "<STR_LIT>" ) <EOL> manager . ResourceOperator . network_create ( <EOL> IsA ( context . RequestContext ) , <EOL> name = IsA ( unicode ) , <EOL> cidr = IsA ( unicode ) , <EOL> gateway = IsA ( unicode ) , <EOL> dns_nameservers = IsA ( list ) , <EOL> ext_router = IsA ( unicode ) ) . AndReturn ( <EOL> { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . mox . ReplayAll ( ) <EOL> request_body = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT:True>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> expect = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : NETWORK_ID1 , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : GID , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> url = '<STR_LIT>' + GID + '<STR_LIT>' <EOL> req = get_request ( url , '<STR_LIT:POST>' , request_body ) <EOL> res = req . get_response ( self . app ) <EOL> body = jsonutils . loads ( res . body ) <EOL> for key in body [ "<STR_LIT>" ] : <EOL> self . assertEqual ( <EOL> expect [ "<STR_LIT>" ] [ key ] , body [ "<STR_LIT>" ] [ key ] ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) <EOL> def test_create_validate_exception_by_gid_notfound_format ( self ) : <EOL> request_body = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:True>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> url = '<STR_LIT>' + GID + "<STR_LIT:a>" + '<STR_LIT>' <EOL> req = get_request ( url , '<STR_LIT:POST>' , request_body ) <EOL> res = req . get_response ( self . app ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) <EOL> def test_create_validate_exception_by_gid_notfound ( self ) : <EOL> self . mox . StubOutWithMock ( db , "<STR_LIT>" ) <EOL> db . group_get_by_gid ( IsA ( context . RequestContext ) , <EOL> GID ) . AndRaise ( exception . GroupNotFound ( gid = GID ) ) <EOL> self . mox . ReplayAll ( ) <EOL> request_body = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:True>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> url = '<STR_LIT>' + GID + '<STR_LIT>' <EOL> req = get_request ( url , '<STR_LIT:POST>' , request_body ) <EOL> res = req . get_response ( self . app ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) <EOL> def test_create_validate_exception_no_body ( self ) : <EOL> self . stubs . Set ( db , "<STR_LIT>" , fake_group_get_by_gid ) <EOL> url = '<STR_LIT>' + GID + '<STR_LIT>' <EOL> req = get_request ( url , '<STR_LIT:POST>' ) <EOL> res = req . get_response ( self . app ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) <EOL> def test_create_validate_exception_body_format ( self ) : <EOL> self . stubs . Set ( db , "<STR_LIT>" , fake_group_get_by_gid ) <EOL> request_body = { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:True>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> url = '<STR_LIT>' + GID + '<STR_LIT>' <EOL> req = get_request ( url , '<STR_LIT:POST>' , request_body ) <EOL> res = req . get_response ( self . app ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) <EOL> def test_create_by_name_blank ( self ) : <EOL> self . stubs . Set ( db , "<STR_LIT>" , fake_create_db ) <EOL> self . stubs . Set ( db , "<STR_LIT>" , fake_group_get_by_gid ) <EOL> self . mox . StubOutWithMock ( uuid , "<STR_LIT>" ) <EOL> self . mox . StubOutWithMock ( manager . ResourceOperator , "<STR_LIT>" ) <EOL> mock_uuid = NETWORK_ID1 <EOL> uuid . uuid4 ( ) . AndReturn ( mock_uuid ) <EOL> uuid . uuid4 ( ) . AndReturn ( mock_uuid ) <EOL> network_value = { <EOL> "<STR_LIT:name>" : "<STR_LIT>" + mock_uuid , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> manager . ResourceOperator . network_create ( <EOL> IsA ( context . RequestContext ) , ** network_value ) . AndReturn ( <EOL> { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . mox . ReplayAll ( ) <EOL> request_body = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : None , <EOL> "<STR_LIT>" : "<STR_LIT:True>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> expect = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : mock_uuid , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : GID , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" + mock_uuid , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> url = '<STR_LIT>' + GID + '<STR_LIT>' <EOL> req = get_request ( url , '<STR_LIT:POST>' , request_body ) <EOL> res = req . get_response ( self . app ) <EOL> body = jsonutils . loads ( res . body ) <EOL> for key in body [ "<STR_LIT>" ] : <EOL> self . assertEqual ( <EOL> expect [ "<STR_LIT>" ] [ key ] , body [ "<STR_LIT>" ] [ key ] ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) <EOL> def test_create_validate_exception_by_is_admin_not_boolean ( self ) : <EOL> self . stubs . Set ( db , "<STR_LIT>" , fake_group_get_by_gid ) <EOL> request_body = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> url = '<STR_LIT>' + GID + '<STR_LIT>' <EOL> req = get_request ( url , '<STR_LIT:POST>' , request_body ) <EOL> res = req . get_response ( self . app ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) <EOL> def test_create_validate_exception_by_dns_nameservers_is_not_list ( self ) : <EOL> self . stubs . Set ( db , "<STR_LIT>" , fake_group_get_by_gid ) <EOL> request_body = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> url = '<STR_LIT>' + GID + '<STR_LIT>' <EOL> req = get_request ( url , '<STR_LIT:POST>' , request_body ) <EOL> res = req . get_response ( self . app ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) <EOL> def test_delete ( self ) : <EOL> self . stubs . Set ( db , <EOL> "<STR_LIT>" , <EOL> fake_network_get_by_network_id ) <EOL> self . stubs . Set ( db , <EOL> "<STR_LIT>" , fake_network_delete ) <EOL> self . mox . StubOutWithMock ( manager . ResourceOperator , "<STR_LIT>" ) <EOL> manager . ResourceOperator . network_delete ( <EOL> IsA ( context . RequestContext ) , IsA ( unicode ) , IsA ( str ) ) <EOL> self . mox . ReplayAll ( ) <EOL> url = "<STR_LIT>" + GID + "<STR_LIT>" + NETWORK_ID1 <EOL> req = get_request ( url , "<STR_LIT>" ) <EOL> res = req . get_response ( self . app ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) <EOL> def test_delete_validate_exception_by_gid_format ( self ) : <EOL> not_uuid_gid = "<STR_LIT>" <EOL> url = '<STR_LIT>' + not_uuid_gid + '<STR_LIT>' + NETWORK_ID1 <EOL> req = get_request ( url , '<STR_LIT>' ) <EOL> res = req . get_response ( self . app ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) <EOL> def test_delete_exception_not_found ( self ) : <EOL> self . mox . StubOutWithMock ( db , "<STR_LIT>" ) <EOL> db . network_get_by_network_id ( <EOL> IsA ( context . RequestContext ) , GID , NETWORK_ID1 ) . AndRaise ( exception . NetworkNotFound ( network_id = NETWORK_ID1 ) ) <EOL> self . mox . ReplayAll ( ) <EOL> url = '<STR_LIT>' + GID + '<STR_LIT>' + NETWORK_ID1 <EOL> req = get_request ( url , '<STR_LIT>' ) <EOL> res = req . get_response ( self . app ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) <EOL> def test_delete_exception_inuse ( self ) : <EOL> self . mox . StubOutWithMock ( db , "<STR_LIT>" ) <EOL> network_process_inuse = { "<STR_LIT>" : [ { "<STR_LIT>" : "<STR_LIT>" } ] } <EOL> db . network_get_by_network_id ( IsA ( context . RequestContext ) , <EOL> GID , <EOL> NETWORK_ID1 ) . AndReturn ( network_process_inuse ) <EOL> self . mox . ReplayAll ( ) <EOL> url = '<STR_LIT>' + GID + '<STR_LIT>' + NETWORK_ID1 <EOL> req = get_request ( url , '<STR_LIT>' ) <EOL> res = req . get_response ( self . app ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) </s>
