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
This commit is contained in:
Reedip
2016-09-24 11:29:38 +05:30
parent 291b31e0e5
commit 9ecea3bbf1
3 changed files with 34 additions and 16 deletions

View File

@@ -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:

View File

@@ -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)

View File

@@ -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]}