From f52588c0409132435489226e8682e9b848394457 Mon Sep 17 00:00:00 2001 From: Salvatore Orlando Date: Fri, 12 Aug 2011 01:02:12 +0100 Subject: [PATCH] Adding more templates More tests Small change in client to return error details in exception upon API failure --- quantum/cli.py | 56 ++++++++++++++++++++++--------------- quantum/cli_output.template | 4 +++ quantum/client.py | 4 ++- tests/unit/test_cli.py | 46 +++++++++++++++++++++++++++--- 4 files changed, 83 insertions(+), 27 deletions(-) diff --git a/quantum/cli.py b/quantum/cli.py index 091daf2ca..df4b71cd2 100644 --- a/quantum/cli.py +++ b/quantum/cli.py @@ -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 '' + print "Error message:%s" % message or '' 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) diff --git a/quantum/cli_output.template b/quantum/cli_output.template index 02faf06af..2bbeb8721 100644 --- a/quantum/cli_output.template +++ b/quantum/cli_output.template @@ -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 diff --git a/quantum/client.py b/quantum/client.py index 3bc04ec45..d993d68e6 100644 --- a/quantum/client.py +++ b/quantum/client.py @@ -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 " diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index 838bdcb10..5b26debe5 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -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") -- 2.45.2