Add instance tests, fix bug encountered in instances.py
diff --git a/gstack/controllers/instances.py b/gstack/controllers/instances.py
index f2bee6d..e13ec07 100755
--- a/gstack/controllers/instances.py
+++ b/gstack/controllers/instances.py
@@ -118,17 +118,17 @@
response['disks'] = []
networking = {}
- if cloudstack_response['securitygroup']:
+ accessconfig = {}
+ if 'securitygroup' in cloudstack_response:
networking['network'] = cloudstack_response['securitygroup'][0]['name']
networking['networkIP'] = cloudstack_response['nic'][0]['ipaddress']
networking['name'] = cloudstack_response['nic'][0]['id']
+ accessconfig['natIP'] = cloudstack_response['nic'][0]['ipaddress']
networking['accessConfigs'] = []
- accessconfig = {}
accessconfig['kind'] = 'compute#accessConfig'
accessconfig['type'] = 'ONE_TO_ONE_NAT'
accessconfig['name'] = 'External NAT'
- accessconfig['natIP'] = cloudstack_response['nic'][0]['ipaddress']
networking['accessConfigs'] = accessconfig
@@ -282,6 +282,7 @@
function_route = url_for(
'getinstance',
projectid=projectid,
+ zone=zone,
instance=instance)
return errors.resource_not_found(function_route)
diff --git a/tests/data/valid_describe_instance.json b/tests/data/valid_describe_instance.json
index 5dfbea7..8415635 100644
--- a/tests/data/valid_describe_instance.json
+++ b/tests/data/valid_describe_instance.json
@@ -28,7 +28,7 @@
"affinitygroup": [],
"account": "admin",
"hostid": "e880bd0f-1597-4cec-b108-c61dd02d78fd",
- "name": "43791f77-26f8-48ca-b557-3a9392f735ae",
+ "name": "instancename",
"networkkbsread": 42369024,
"created": "2014-02-21T17:28:40+0000",
"hypervisor": "Simulator",
diff --git a/tests/images_tests.py b/tests/images_tests.py
index 1370c5d..8a9f2c0 100644
--- a/tests/images_tests.py
+++ b/tests/images_tests.py
@@ -6,7 +6,7 @@
from gstack.helpers import read_file
from . import GStackAppTestCase
-class ZonesTestCase(GStackAppTestCase):
+class ImagesTestCase(GStackAppTestCase):
def test_list_images(self):
@@ -32,7 +32,7 @@
self.assert_ok(response)
- def test_get_zone_not_found(self):
+ def test_get_image_image_not_found(self):
get = mock.Mock()
get.return_value.text = read_file('tests/data/empty_describe_images.json')
diff --git a/tests/instances_tests.py b/tests/instances_tests.py
new file mode 100644
index 0000000..13cbadc
--- /dev/null
+++ b/tests/instances_tests.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python
+# encoding: utf-8
+
+import mock
+
+from gstack.helpers import read_file
+from . import GStackAppTestCase
+
+class InstancesTestCase(GStackAppTestCase):
+
+ def test_list_instances(self):
+
+ get = mock.Mock()
+ get.return_value.text = read_file('tests/data/valid_describe_instances.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/instances', headers=headers)
+
+ self.assert_ok(response)
+
+ def test_get_instance(self):
+
+ get = mock.Mock()
+ get.return_value.text = read_file('tests/data/valid_describe_instance.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/instances/instancename', headers=headers)
+
+ self.assert_ok(response)
+
+ def test_get_instance_instance_not_found(self):
+
+ get = mock.Mock()
+ get.return_value.text = read_file('tests/data/empty_describe_instances.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/instances/instancename', headers=headers)
+
+ self.assert_not_found(response)
+ assert 'The resource \'/compute/v1/projects/exampleproject/zones/examplezone/instances/instancename\' was not found' \
+ in response.data
diff --git a/tests/zones_tests.py b/tests/zones_tests.py
index 1232342..fa89ad8 100644
--- a/tests/zones_tests.py
+++ b/tests/zones_tests.py
@@ -32,7 +32,7 @@
self.assert_ok(response)
- def test_get_zone_not_found(self):
+ def test_get_zone_zone_not_found(self):
get = mock.Mock()
get.return_value.text = read_file('tests/data/empty_describe_zone.json')