cloudstack-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From seb...@apache.org
Subject [02/50] [abbrv] cloudstack-gcestack git commit: Finish networks tests
Date Tue, 05 May 2015 11:18:36 GMT
Finish networks tests


Project: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/commit/853231f3
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/tree/853231f3
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/diff/853231f3

Branch: refs/heads/master
Commit: 853231f38d9ac33101247bcba30192c65f69dbe0
Parents: c6f7565
Author: BroganD1993 <darrenbrogan@hotmail.com>
Authored: Sun Jun 8 12:47:08 2014 +0100
Committer: BroganD1993 <darrenbrogan@hotmail.com>
Committed: Sun Jun 8 12:47:08 2014 +0100

----------------------------------------------------------------------
 gstack/controllers/networks.py                  | 27 +++---
 tests/data/duplicate_create_security_group.json |  7 ++
 tests/networks_tests.py                         | 87 ++++++++++++++++++++
 3 files changed, 110 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/853231f3/gstack/controllers/networks.py
----------------------------------------------------------------------
diff --git a/gstack/controllers/networks.py b/gstack/controllers/networks.py
index 7fba7ab..03a1799 100644
--- a/gstack/controllers/networks.py
+++ b/gstack/controllers/networks.py
@@ -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',

http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/853231f3/tests/data/duplicate_create_security_group.json
----------------------------------------------------------------------
diff --git a/tests/data/duplicate_create_security_group.json b/tests/data/duplicate_create_security_group.json
new file mode 100644
index 0000000..3a038b8
--- /dev/null
+++ b/tests/data/duplicate_create_security_group.json
@@ -0,0 +1,7 @@
+{
+    "createsecuritygroupresponse": {
+        "errorcode": 431,
+        "uuidlist": [],
+        "errortext": "Unable to create security group, a group with name securitygroupname
already exisits."
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/853231f3/tests/networks_tests.py
----------------------------------------------------------------------
diff --git a/tests/networks_tests.py b/tests/networks_tests.py
index 2f2f533..edb8d33 100644
--- a/tests/networks_tests.py
+++ b/tests/networks_tests.py
@@ -2,6 +2,7 @@
 # encoding: utf-8
 
 import mock
+import json
 
 from gstack.helpers import read_file
 from . import GStackAppTestCase
@@ -45,3 +46,89 @@ class NetworksTestCase(GStackAppTestCase):
         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


Mime
View raw message