summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorReedip <reedip.banerjee@nectechnologies.in>2016-09-24 11:29:38 +0530
committerReedip <reedip.banerjee@nectechnologies.in>2016-10-03 10:32:39 +0000
commit9ecea3bbf1d4664f2a70f9a6caac88120d7c119b (patch)
treedf72771341e2553d52f09ff9bf0d51d8ac64ed3e
parent291b31e0e5eed01cbbe917a644e38c2cedafae3e (diff)
downloadpython-neutronclient-9ecea3bbf1d4664f2a70f9a6caac88120d7c119b.tar.gz
Return proper error code for CLI failure
Due to [1] , the return code which was expected from NeutronClient changed if a particular CLI failed to find an object belonging to the resource to be deleted. The following patch fixes it. [1]: https://review.openstack.org/#/c/263609/ Change-Id: I19b4328361157fbca3e557e02797ed0c895e924b Closes-Bug:#1623169
-rw-r--r--neutronclient/neutron/v2_0/__init__.py26
-rw-r--r--neutronclient/tests/unit/test_cli20.py12
-rw-r--r--neutronclient/tests/unit/test_cli20_network.py12
3 files changed, 34 insertions, 16 deletions
diff --git a/neutronclient/neutron/v2_0/__init__.py b/neutronclient/neutron/v2_0/__init__.py
index 4468bfb..f169ad5 100644
--- a/neutronclient/neutron/v2_0/__init__.py
+++ b/neutronclient/neutron/v2_0/__init__.py
@@ -528,18 +528,20 @@ class DeleteCommand(NeutronCommand):
% {'id': ", ".join(successful_delete),
'resource': self.cmd_resource},
file=self.app.stdout)
- if non_existent:
- print((_("Unable to find %(resource)s(s) with id(s) "
- "'%(id)s'") %
- {'resource': self.cmd_resource,
- 'id': ", ".join(non_existent)}),
- file=self.app.stdout)
- if multiple_ids:
- print((_("Multiple %(resource)s(s) matches found for name(s)"
- " '%(id)s'. Please use an ID to be more specific.")) %
- {'resource': self.cmd_resource,
- 'id': ", ".join(multiple_ids)},
- file=self.app.stdout)
+ if non_existent or multiple_ids:
+ err_msgs = []
+ if non_existent:
+ err_msgs.append((_("Unable to find %(resource)s(s) with id(s) "
+ "'%(id)s'.") %
+ {'resource': self.cmd_resource,
+ 'id': ", ".join(non_existent)}))
+ if multiple_ids:
+ err_msgs.append((_("Multiple %(resource)s(s) matches found "
+ "for name(s) '%(id)s'. Please use an ID "
+ "to be more specific.") %
+ {'resource': self.cmd_resource,
+ 'id': ", ".join(multiple_ids)}))
+ raise exceptions.NeutronCLIError(message='\n'.join(err_msgs))
def delete_item(self, obj_deleter, neutron_client, item_id):
if self.allow_names:
diff --git a/neutronclient/tests/unit/test_cli20.py b/neutronclient/tests/unit/test_cli20.py
index d071d85..be643e7 100644
--- a/neutronclient/tests/unit/test_cli20.py
+++ b/neutronclient/tests/unit/test_cli20.py
@@ -512,7 +512,9 @@ class CLITestV20Base(base.BaseTestCase):
self.assertIn(myid, _str)
self.assertIn('myname', _str)
- def _test_set_path_and_delete(self, path, parent_id, myid):
+ def _test_set_path_and_delete(self, path, parent_id, myid,
+ delete_fail=False):
+ return_val = 404 if delete_fail else 204
if parent_id:
path = path % (parent_id, myid)
else:
@@ -521,11 +523,12 @@ class CLITestV20Base(base.BaseTestCase):
end_url(path, format=self.format), 'DELETE',
body=None,
headers=mox.ContainsKeyValue(
- 'X-Auth-Token', TOKEN)).AndReturn((MyResp(204), None))
+ 'X-Auth-Token', TOKEN)).AndReturn((MyResp(
+ return_val), None))
def _test_delete_resource(self, resource, cmd, myid, args,
cmd_resource=None, parent_id=None,
- extra_id=None):
+ extra_id=None, delete_fail=False):
self.mox.StubOutWithMock(cmd, "get_client")
self.mox.StubOutWithMock(self.client.httpclient, "request")
cmd.get_client().MultipleTimes().AndReturn(self.client)
@@ -535,7 +538,8 @@ class CLITestV20Base(base.BaseTestCase):
self._test_set_path_and_delete(path, parent_id, myid)
# extra_id is used to test for bulk_delete
if extra_id:
- self._test_set_path_and_delete(path, parent_id, extra_id)
+ self._test_set_path_and_delete(path, parent_id, extra_id,
+ delete_fail)
self.mox.ReplayAll()
cmd_parser = cmd.get_parser("delete_" + cmd_resource)
shell.run_command(cmd, cmd_parser, args)
diff --git a/neutronclient/tests/unit/test_cli20_network.py b/neutronclient/tests/unit/test_cli20_network.py
index 5c728b0..cbd41bd 100644
--- a/neutronclient/tests/unit/test_cli20_network.py
+++ b/neutronclient/tests/unit/test_cli20_network.py
@@ -597,6 +597,18 @@ class CLITestV20NetworkJSON(test_cli20.CLITestV20Base):
args = [myid1, myid2]
self._test_delete_resource(resource, cmd, myid1, args, extra_id=myid2)
+ def test_bulk_delete_network_fail(self):
+ # Delete net: myid1 myid2.
+ resource = 'network'
+ cmd = network.DeleteNetwork(test_cli20.MyApp(sys.stdout), None)
+ myid1 = 'myid1'
+ myid2 = 'myid2'
+ args = [myid1, myid2]
+ self.assertRaises(exceptions.NeutronCLIError,
+ self._test_delete_resource,
+ resource, cmd, myid1, args, extra_id=myid2,
+ delete_fail=True)
+
def _test_extend_list(self, mox_calls):
data = [{'id': 'netid%d' % i, 'name': 'net%d' % i,
'subnets': ['mysubid%d' % i]}