]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Adding more templates
authorSalvatore Orlando <salvatore.orlando@eu.citrix.com>
Fri, 12 Aug 2011 00:02:12 +0000 (01:02 +0100)
committerSalvatore Orlando <salvatore.orlando@eu.citrix.com>
Fri, 12 Aug 2011 00:02:12 +0000 (01:02 +0100)
More tests

Small change in client to return error details in exception upon API failure

quantum/cli.py
quantum/cli_output.template
quantum/client.py
tests/unit/test_cli.py

index 091daf2cac654d9eb798af1d26c9fa7a5fd01853..df4b71cd29a9d104eb5349e0b27a3a8134aa6619 100644 (file)
@@ -33,11 +33,10 @@ from quantum.api.views.ports import ViewBuilder as PortBuilder
 
 FORMAT = "json"
 CLI_TEMPLATE = "../quantum/cli_output.template"
-
-### -- Core CLI functions
-
+#TODO(salvatore-orlando): do proper logging!
 
 def prepare_output(cmd, tenant_id, response):
+    """ Fills a cheetah template with the response """
     #add command and tenant to response for output generation
     response['cmd'] = cmd
     response['tenant_id'] = tenant_id
@@ -65,37 +64,50 @@ def api_list_nets(client, *args):
 
 
 def create_net(manager, *args):
-    tid, name = args
-    new_net_id = manager.create_network(tid, name)
-    print "Created a new Virtual Network with ID:%s\n" % new_net_id
+    tenant_id, name = args
+    new_net_id = manager.create_network(tenant_id, name)['net-id']
+    output = prepare_output("create_net", tenant_id,
+                            dict(network_id=new_net_id))
+    print output
 
 
 def api_create_net(client, *args):
-    tid, name = args
-    data = {'network': {'net-name': '%s' % name}}
-    res = client.create_network(data)
-    LOG.debug(res)
-    nid = None
+    tenant_id, name = args
+    data = {'network': {'net-name': name}}
+    new_net_id = None
     try:
-        nid = res["networks"]["network"]["id"]
-    except Exception, e:
-        print "Failed to create network"
-        # TODO(bgh): grab error details from ws request result
+        res = client.create_network(data)
+        new_net_id = res["networks"]["network"]["id"]
+    except Exception as ex:
+        status_code = None
+        message = None
+        #Retrieve dict at 1st element of tuple at last argument
+        if ex.args and isinstance(ex.args[-1][0],dict):
+            status_code = ex.args[-1][0].get('status_code', None)
+            message = ex.args[-1][0].get('message', None)
+        print "Failed to create network: %s" % status_code or '<missing>'
+        print "Error message:%s" % message or '<missing>'
         return
-    print "Created a new Virtual Network with ID:%s\n" % nid
+    output = prepare_output("create_net", tenant_id,
+                            dict(network_id=new_net_id))
+    print output
 
 
 def delete_net(manager, *args):
-    tid, nid = args
-    manager.delete_network(tid, nid)
-    print "Deleted Virtual Network with ID:%s" % nid
+    tenant_id, network_id = args
+    manager.delete_network(tenant_id, network_id)
+    output = prepare_output("delete_net", tenant_id,
+                            dict(network_id=network_id))
+    print output
 
 
 def api_delete_net(client, *args):
-    tid, nid = args
+    tenant_id, network_id = args
     try:
-        res = client.delete_network(nid)
-        print "Deleted Virtual Network with ID:%s" % nid
+        client.delete_network(network_id)
+        output = prepare_output("delete_net", tenant_id,
+                            dict(network_id=network_id))
+        print output
     except Exception, e:
         print "Failed to delete network"
         LOG.error("Failed to delete network: %s" % e)
index 02faf06afd76cc012e326a0cd31e08144751472e..2bbeb8721ff58c5f955244ad863ec3904002d86d 100644 (file)
@@ -4,5 +4,9 @@ Virtual Networks on Tenant $tenant_id
 #for $network in $networks
 \tNetwork ID: $network.id
 #end for 
+#elif $cmd == 'create_net'
+Created a new Virtual Network with ID: $network_id for Tenant $tenant_id
+#elif $cmd == 'delete_net'
+Deleted Virtual Network with ID: $network_id for Tenant $tenant_id
 #end if
 
index 3bc04ec452b5980e594d8d5e32c3dfc47e2ccf00..d993d68e665391db614f75f341131ebdcb9b2b86 100644 (file)
@@ -148,7 +148,9 @@ class Client(object):
                                httplib.NO_CONTENT):
                 return self.deserialize(res)
             else:
-                raise Exception("Server returned error: %s" % res.read())
+                ex = Exception("Server returned error: %s" % status_code)
+                ex.args = ([dict(status_code=status_code, message=res.read())],)
+                raise ex
 
         except (socket.error, IOError), e:
             raise Exception("Unable to connect to "
index 838bdcb10be03b98f4ae76a73510a00322898d0f..5b26debe59d3c1548bcbc82692a0baf68889a618 100644 (file)
@@ -54,9 +54,6 @@ class CLITest(unittest.TestCase):
         # Redirect stdout
         self.fake_stdout = client_stubs.FakeStdout()
         sys.stdout = self.fake_stdout
-        # Pre-populate data for testing using db api
-        db.network_create(self.tenant_id, self.network_name_1)
-        db.network_create(self.tenant_id, self.network_name_2)
     
     def tearDown(self):
         """Clear the test environment"""
@@ -74,20 +71,41 @@ class CLITest(unittest.TestCase):
             # Verify!
             # Must add newline at the end to match effect of print call
             self.assertEquals(self.fake_stdout.make_string(), output + '\n')
+
+    def _verify_create_network(self):
+            # Verification - get raw result from db
+            nw_list = db.network_list(self.tenant_id)
+            if len(nw_list) != 1:
+                self.fail("No network created")
+            network_id = nw_list[0].uuid
+            # Fill CLI template
+            output = cli.prepare_output('create_net', self.tenant_id,
+                                        dict(network_id=network_id))
+            # Verify!
+            # Must add newline at the end to match effect of print call
+            self.assertEquals(self.fake_stdout.make_string(), output + '\n')
         
     def test_list_networks(self):
         try: 
+            # Pre-populate data for testing using db api
+            db.network_create(self.tenant_id, self.network_name_1)
+            db.network_create(self.tenant_id, self.network_name_2)
+            
             cli.list_nets(self.manager, self.tenant_id)
             LOG.debug("Operation completed. Verifying result")
             LOG.debug(self.fake_stdout.content)
             self._verify_list_networks()
         except: 
             LOG.exception("Exception caught: %s", sys.exc_info())
-            self.fail("test_list_network_api failed due to an exception")
+            self.fail("test_list_network failed due to an exception")
 
     
     def test_list_networks_api(self):
         try: 
+            # Pre-populate data for testing using db api
+            db.network_create(self.tenant_id, self.network_name_1)
+            db.network_create(self.tenant_id, self.network_name_2)
+            
             cli.api_list_nets(self.client, self.tenant_id)
             LOG.debug("Operation completed. Verifying result")
             LOG.debug(self.fake_stdout.content)
@@ -96,3 +114,23 @@ class CLITest(unittest.TestCase):
             LOG.exception("Exception caught: %s", sys.exc_info())
             self.fail("test_list_network_api failed due to an exception")
 
+    def test_create_network(self):
+        try: 
+            cli.create_net(self.manager, self.tenant_id, "test")
+            LOG.debug("Operation completed. Verifying result")
+            LOG.debug(self.fake_stdout.content)
+            self._verify_create_network()
+        except: 
+            LOG.exception("Exception caught: %s", sys.exc_info())
+            self.fail("test_create_network failed due to an exception")
+
+    
+    def test_create_network_api(self):
+        try: 
+            cli.api_create_net(self.client, self.tenant_id, "test")
+            LOG.debug("Operation completed. Verifying result")
+            LOG.debug(self.fake_stdout.content)
+            self._verify_create_network()  
+        except: 
+            LOG.exception("Exception caught: %s", sys.exc_info())
+            self.fail("test_create_network_api failed due to an exception")