Skip to content
Permalink
Browse files
Finish networks tests
  • Loading branch information
brogand93 committed Jun 8, 2014
1 parent c6f7565 commit 853231f38d9ac33101247bcba30192c65f69dbe0
Showing 3 changed files with 110 additions and 11 deletions.
@@ -57,7 +57,7 @@ def get_network_by_name(authorization, securitygroup):
return None


def _get_network(authorization, args=None):
def _add_network(authorization, args=None):
command = 'createSecurityGroup'
if not args:
args = {}
@@ -73,16 +73,12 @@ def _get_network(authorization, args=None):


def _delete_network(authorization, projectid, network):
securitygroup_id = get_network_by_name(authorization, network)['id']
network_response = get_network_by_name(authorization, network)

if securitygroup_id is None:
func_route = url_for(
'getnetwork',
projectid=projectid,
network=network
)
if not network_response:
return None

return errors.resource_not_found(func_route)
securitygroup_id = network_response['id']

args = {
'id': securitygroup_id
@@ -114,6 +110,7 @@ def _cloudstack_network_to_gce(cloudstack_response, selfLink=None):
def _create_populated_network_response(projectid, networks=None):
if not networks:
networks = []

populated_response = {
'kind': 'compute#networkList',
'selfLink': request.base_url,
@@ -172,7 +169,7 @@ def addnetwork(authorization, projectid):
args['name'] = data['name']
args['description'] = data['description']

network_result = _get_network(authorization, args)
network_result = _add_network(authorization, args)

if 'errortext' in network_result['createsecuritygroupresponse']:
populated_response = {
@@ -209,7 +206,15 @@ def addnetwork(authorization, projectid):
@app.route('/compute/v1/projects/<projectid>/global/networks/<network>', methods=['DELETE'])
@authentication.required
def deletenetwork(projectid, authorization, network):
_delete_network(authorization, projectid, network)
response = _delete_network(authorization, projectid, network)

if not response:
func_route = url_for(
'getnetwork',
projectid=projectid,
network=network
)
return errors.resource_not_found(func_route)

populated_response = {
'kind': 'compute#operation',
@@ -0,0 +1,7 @@
{
"createsecuritygroupresponse": {
"errorcode": 431,
"uuidlist": [],
"errortext": "Unable to create security group, a group with name securitygroupname already exisits."
}
}
@@ -2,6 +2,7 @@
# encoding: utf-8

import mock
import json

from gstack.helpers import read_file
from . import GStackAppTestCase
@@ -45,3 +46,89 @@ def test_get_network_network_not_found(self):
self.assert_not_found(response)
assert 'The resource \'/compute/v1/projects/exampleproject/global/networks/networkname\'' \
in response.data

def test_add_network(self):
data = {
'IPv4Range': '10.0.0.0/8',
'kind': 'compute#network',
'name': 'networkname',
'description': ''
}

data = json.dumps(data)

get = mock.Mock()
get.return_value.text = read_file('tests/data/valid_create_security_group.json')
get.return_value.status_code = 200

with mock.patch('requests.get', get):
headers = {
'authorization': 'Bearer ' + str(GStackAppTestCase.access_token),
}

response = self.post_json('/compute/v1/projects/admin/global/networks', data=data, headers=headers)

self.assert_ok(response)

def test_add_network_network_duplicate(self):
data = {
'IPv4Range': '10.0.0.0/8',
'kind': 'compute#network',
'name': 'networkname',
'description': ''
}

data = json.dumps(data)

get = mock.Mock()
get.return_value.text = read_file('tests/data/duplicate_create_security_group.json')
get.return_value.status_code = 200

with mock.patch('requests.get', get):
headers = {
'authorization': 'Bearer ' + str(GStackAppTestCase.access_token),
}

response = self.post_json('/compute/v1/projects/admin/global/networks', data=data, headers=headers)

assert 'RESOURCE_ALREADY_EXISTS' in response.data

def test_delete_network(self):

get = mock.Mock()
get.return_value.text = read_file('tests/data/valid_delete_security_group.json')
get.return_value.status_code = 200

get_networks = mock.Mock()
get_networks.return_value = json.loads(read_file('tests/data/valid_describe_security_group.json'))

with mock.patch('requests.get', get):
with mock.patch('gstack.controllers.networks._get_networks', get_networks):
headers = {
'authorization': 'Bearer ' + str(GStackAppTestCase.access_token),
}

response = self.delete('/compute/v1/projects/exampleproject/global/networks/networkname', headers=headers)

self.assert_ok(response)

def test_delete_network_network_not_found(self):

get = mock.Mock()
get.return_value.text = read_file('tests/data/valid_delete_security_group.json')
get.return_value.status_code = 200

get_networks = mock.Mock()
get_networks.return_value = json.loads(read_file('tests/data/valid_describe_security_group.json'))

with mock.patch('requests.get', get):
with mock.patch('gstack.controllers.networks._get_networks', get_networks):
headers = {
'authorization': 'Bearer ' + str(GStackAppTestCase.access_token),
}

response = self.delete('/compute/v1/projects/exampleproject/global/networks/invalidnetworkname', headers=headers)

self.assert_not_found(response)
assert 'The resource \'/compute/v1/projects/exampleproject/global/networks/invalidnetworkname\'' \
in response.data

0 comments on commit 853231f

Please sign in to comment.