Skip to content
Permalink
Browse files
Add disks/firewalls tests, fix bugs encountered
  • Loading branch information
brogand93 committed Jun 2, 2014
1 parent a383e91 commit 4652b4c8991d2b6e9dc7c120368dd7102bb3970e
Showing 9 changed files with 266 additions and 33 deletions.
@@ -204,6 +204,7 @@ def getdisk(projectid, authorization, zone, disk):
func_route = url_for(
'getdisk',
projectid=projectid,
zone=zone,
disk=disk
)
return errors.resource_not_found(func_route)
@@ -26,38 +26,39 @@


def _cloudstack_securitygroup_to_gce(response_item):
rules = response_item['ingressrule']
allowed = []
sourceranges = []
for rule in rules:
ports = []
for i in range(rule['startport'], rule['endport'] + 1):
ports.append(str(i))
allowed.append({
"IPProtocol": rule['protocol'],
"ports": ports
if 'ingressrule' in response_item:
rules = response_item['ingressrule']
allowed = []
sourceranges = []
for rule in rules:
ports = []
if 'startport' in rule:
for i in range(rule['startport'], rule['endport'] + 1):
ports.append(str(i))
allowed.append({
"IPProtocol": rule['protocol'],
"ports": ports
})
if 'cidr' in rule.keys():
sourceranges.append(rule['cidr'])
return ({
"kind": "compute#firewall",
"selfLink": '',
"id": response_item['id'],
"creationTimestamp": '',
"name": response_item['name'],
"description": response_item['description'],
"network": '',
"sourceRanges": sourceranges,
"sourceTags": [
''
],
"targetTags": response_item['tags'],
"allowed": allowed
})
if 'cidr' in rule.keys():
sourceranges.append(rule['cidr'])
return ({
"kind": "compute#firewall",
"selfLink": '',
"id": response_item['id'],
"creationTimestamp": '',
"name": response_item['name'],
"description": response_item['description'],
"network": '',
"sourceRanges": sourceranges,
"sourceTags": [
''
],
"targetTags": response_item['tags'],
"allowed": allowed
})


@app.route('/compute/v1/projects/<projectid>/global/firewalls',
methods=['GET'])


@app.route('/compute/v1/projects/<projectid>/global/firewalls', methods=['GET'])
@authentication.required
def listsecuritygroups(projectid, authorization):
command = 'listSecurityGroups'
@@ -121,7 +122,7 @@ def getsecuritygroup(projectid, authorization, firewall):
json.dumps(cloudstack_response, indent=4, separators=(',', ': '))
)

if cloudstack_response['listsecuritygroupsresponse']:
if cloudstack_response['listsecuritygroupsresponse']['securitygroup']:
response_item = cloudstack_response[
'listsecuritygroupsresponse']['securitygroup'][0]
firewall = _cloudstack_securitygroup_to_gce(response_item)
@@ -152,6 +152,7 @@ def _get_virtual_machine_by_name(authorization, instance):
'keyword': instance
}
)
print virtual_machine_list

if virtual_machine_list['listvirtualmachinesresponse']:
response = helper.filter_by_name(
@@ -0,0 +1,5 @@
{
"listvolumesresponse": {
"volume": {}
}
}
@@ -0,0 +1,28 @@
{
"listvolumesresponse": {
"count": 1,
"volume": [
{
"diskofferingname": "Custom",
"account": "admin",
"domainid": "2edae3e4-95e4-11e3-b2e4-d19c9d3e5e1d",
"name": "volumename",
"created": "2014-02-22T13:25:12+0000",
"storagetype": "shared",
"type": "DATADISK",
"tags": [],
"domain": "ROOT",
"isextractable": true,
"zoneid": "1e47a2fc-61c7-401c-b90e-416b472ada64",
"destroyed": false,
"state": "Allocated",
"diskofferingdisplaytext": "Custom Disk",
"displayvolume": true,
"size": 10737418240,
"diskofferingid": "46f0a62e-ba3c-4a49-b619-877a8b2a07ed",
"id": "aa903a80-efa9-4fac-b424-544cbda9fce9",
"zonename": "Sandbox-simulator"
}
]
}
}
@@ -0,0 +1,55 @@
{
"listvolumesresponse": {
"count": 21,
"volume": [
{
"diskofferingname": "Custom",
"account": "admin",
"domainid": "2edae3e4-95e4-11e3-b2e4-d19c9d3e5e1d",
"name": "6f78a7d4-8863-40da-96f8-0e10ad4ea94e",
"created": "2014-02-22T13:25:12+0000",
"storagetype": "shared",
"type": "DATADISK",
"tags": [],
"domain": "ROOT",
"isextractable": true,
"zoneid": "1e47a2fc-61c7-401c-b90e-416b472ada64",
"destroyed": false,
"state": "Allocated",
"diskofferingdisplaytext": "Custom Disk",
"displayvolume": true,
"size": 10737418240,
"diskofferingid": "46f0a62e-ba3c-4a49-b619-877a8b2a07ed",
"id": "aa903a80-efa9-4fac-b424-544cbda9fce9",
"zonename": "Sandbox-simulator"
},
{
"domain": "ROOT",
"domainid": "2edae3e4-95e4-11e3-b2e4-d19c9d3e5e1d",
"zoneid": "1e47a2fc-61c7-401c-b90e-416b472ada64",
"vmname": "43791f77-26f8-48ca-b557-3a9392f735ae",
"id": "de2d8297-eaaf-4e81-8ffe-97f37ddbbde5",
"size": 5368709120,
"diskofferingname": "Small",
"diskofferingdisplaytext": "Small Disk, 5 GB",
"storage": "PS0",
"displayvolume": false,
"destroyed": false,
"state": "Ready",
"type": "DATADISK",
"zonename": "Sandbox-simulator",
"tags": [],
"isextractable": true,
"account": "admin",
"name": "DATA-5",
"virtualmachineid": "43791f77-26f8-48ca-b557-3a9392f735ae",
"storagetype": "shared",
"hypervisor": "Simulator",
"created": "2014-02-21T17:28:41+0000",
"deviceid": 1,
"diskofferingid": "8fade6f4-13ea-490f-aff2-65c9b37b7651",
"vmstate": "Running"
}
]
}
}
@@ -0,0 +1,91 @@
#!/usr/bin/env python
# encoding: utf-8

import mock
import json

from gstack.helpers import read_file
from . import GStackAppTestCase

class DisksTestCase(GStackAppTestCase):

def test_list_disks(self):

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

with mock.patch('requests.get', get):
headers = {'authorization': 'Bearer ' + str(GStackAppTestCase.access_token)}
response = self.get('/compute/v1/projects/exampleproject/zones/examplezone/disks', headers=headers)

self.assert_ok(response)

def test_aggregated_list_disks(self):

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

get_zones = mock.Mock()
get_zones.return_value = json.loads(read_file('tests/data/valid_describe_zone.json'))

with mock.patch('requests.get', get):
with mock.patch(
'gstack.controllers.zones._get_zones',
get_zones
):
headers = {'authorization': 'Bearer ' + str(GStackAppTestCase.access_token)}
response = self.get('/compute/v1/projects/projectid/aggregated/disks', headers=headers)

self.assert_ok(response)

def test_aggregated_list_disks_with_name_filter(self):

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

get_zones = mock.Mock()
get_zones.return_value = json.loads(read_file('tests/data/valid_describe_zone.json'))

with mock.patch('requests.get', get):
with mock.patch(
'gstack.controllers.zones._get_zones',
get_zones
):
headers = {'authorization': 'Bearer ' + str(GStackAppTestCase.access_token)}
response = self.get(
'/compute/v1/projects/projectid/aggregated/disks?filter=name+eq+volumename',
headers=headers)

self.assert_ok(response)

def test_get_disk(self):

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

with mock.patch('requests.get', get):
headers = {'authorization': 'Bearer ' + str(GStackAppTestCase.access_token)}
response = self.get('/compute/v1/projects/exampleproject/zones/examplezone/disks/volumename', headers=headers)

self.assert_ok(response)

def test_get_disk_disk_not_found(self):

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

with mock.patch('requests.get', get):
headers = {'authorization': 'Bearer ' + str(GStackAppTestCase.access_token)}
response = self.get('/compute/v1/projects/exampleproject/zones/examplezone/disks/volumename', headers=headers)

self.assert_not_found(response)

assert 'The resource \'/compute/v1/projects/exampleproject/zones/examplezone/disks/volumename\' was not found' \
in response.data


@@ -0,0 +1,49 @@
#!/usr/bin/env python
# encoding: utf-8

import mock
import json

from gstack.helpers import read_file
from . import GStackAppTestCase

class FirewallsTestCase(GStackAppTestCase):

def test_list_firewalls(self):

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

with mock.patch('requests.get', get):
headers = {'authorization': 'Bearer ' + str(GStackAppTestCase.access_token)}
response = self.get('/compute/v1/projects/exampleproject/global/firewalls', headers=headers)

self.assert_ok(response)


def test_get_firewall(self):

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

with mock.patch('requests.get', get):
headers = {'authorization': 'Bearer ' + str(GStackAppTestCase.access_token)}
response = self.get('/compute/v1/projects/exampleproject/global/firewalls/securitygroupname', headers=headers)

self.assert_ok(response)

def test_get_firewall_firewall_not_found(self):

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

with mock.patch('requests.get', get):
headers = {'authorization': 'Bearer ' + str(GStackAppTestCase.access_token)}
response = self.get('/compute/v1/projects/exampleproject/global/firewalls/securitygroupname', headers=headers)

self.assert_not_found(response)
assert 'The resource \'/compute/v1/projects/exampleproject/global/firewalls/securitygroupname\' was not found' \
in response.data
@@ -43,7 +43,7 @@ def test_aggregated_list_instances(self):
def test_aggregated_list_instances_with_name_filter(self):

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

get_zones = mock.Mock()
@@ -86,3 +86,5 @@ def test_get_instance_instance_not_found(self):
self.assert_not_found(response)
assert 'The resource \'/compute/v1/projects/exampleproject/zones/examplezone/instances/instancename\' was not found' \
in response.data


0 comments on commit 4652b4c

Please sign in to comment.