Add check-errors flag to Introspection

Failed introspection in OC returns 0, add check-errors
flag to handle if any errors occurred during the
introspection of nodes and get the correct exit status
code.

Change-Id: If30fdb9b8dbe29eb6803dafcde9380444e497e3d
Story: #2004376
Task: #27988
This commit is contained in:
Iury Gregory Melo Ferreira 2018-11-16 16:20:33 +01:00 committed by Iury Gregory Melo Ferreira
parent debd80fb3f
commit 129594ef55
4 changed files with 83 additions and 1 deletions

View File

@ -21,7 +21,7 @@ Start introspection on a node
::
$ openstack baremetal introspection start [--wait] NODE_ID [NODE_ID ...]
$ openstack baremetal introspection start [--wait] [--check-errors] NODE_ID [NODE_ID ...]
* ``NODE_ID`` - Ironic node UUID or name;
@ -36,6 +36,9 @@ Note that the CLI call accepts several UUID's and will stop on the first error.
With ``--wait`` flag it waits until introspection ends for all given nodes,
then displays the results as a table.
The ``--check-errors`` flag can only be used together with the ``--wait`` flag,
otherwise will throw an error.
Query introspection status
--------------------------

View File

@ -19,6 +19,8 @@ import json
import sys
from osc_lib.command import command
from osc_lib import exceptions
from osc_lib.i18n import _
from osc_lib import utils
import yaml
@ -71,9 +73,18 @@ class StartCommand(command.Lister):
action='store_true',
help='wait for introspection to finish; the result'
' will be displayed in the end')
parser.add_argument('--check-errors',
action='store_true',
help='check if errors occurred during the'
' introspection; if any error occurs only the'
' errors are displayed')
return parser
def take_action(self, parsed_args):
if parsed_args.check_errors and not parsed_args.wait:
raise exceptions.CommandError(
_("--check-errors can only be used with --wait"))
client = self.app.client_manager.baremetal_introspection
for uuid in parsed_args.node:
client.introspect(uuid)
@ -83,6 +94,14 @@ class StartCommand(command.Lister):
result = client.wait_for_finish(parsed_args.node)
result = [(uuid, s.get('error'))
for uuid, s in result.items()]
if parsed_args.check_errors:
uuids_errors = ", ".join("%s (%s)" % node_info
for node_info in result
if node_info[1] is not None)
if uuids_errors:
raise Exception(
_("Introspection failed for some nodes: %s")
% uuids_errors)
else:
result = []

View File

@ -15,6 +15,7 @@ import sys
import collections
import mock
from osc_lib import exceptions
from osc_lib.tests import utils
import six
import tempfile
@ -101,6 +102,56 @@ class TestIntrospect(BaseTest):
self.assertEqual([('uuid1', None), ('uuid2', 'boom'), ('uuid3', None)],
sorted(values))
def test_wait_with_check_errors_no_raise_exception(self):
nodes = ['uuid1', 'uuid2', 'uuid3']
arglist = ['--wait'] + ['--check-errors'] + nodes
verifylist = [('node', nodes), ('wait', True), ('check_errors', True)]
self.client.wait_for_finish.return_value = {
'uuid1': {'finished': True, 'error': None},
'uuid2': {'finished': True, 'error': None},
'uuid3': {'finished': True, 'error': None},
}
cmd = shell.StartCommand(self.app, None)
parsed_args = self.check_parser(cmd, arglist, verifylist)
_c, values = cmd.take_action(parsed_args)
calls = [mock.call(node) for node in nodes]
self.assertEqual(calls, self.client.introspect.call_args_list)
self.assertEqual([('uuid1', None), ('uuid2', None), ('uuid3', None)],
sorted(values))
def test_wait_with_check_errors(self):
nodes = ['uuid1', 'uuid2', 'uuid3']
arglist = ['--wait'] + ['--check-errors'] + nodes
verifylist = [('node', nodes), ('wait', True), ('check_errors', True)]
self.client.wait_for_finish.return_value = {
'uuid1': {'finished': True, 'error': None},
'uuid2': {'finished': True, 'error': 'boom'},
'uuid3': {'finished': True, 'error': None},
}
cmd = shell.StartCommand(self.app, None)
parsed_args = self.check_parser(cmd, arglist, verifylist)
msg = "Introspection failed for"
self.assertRaisesRegex(Exception, msg, cmd.take_action, parsed_args)
def test_check_errors_alone(self):
nodes = ['uuid1', 'uuid2', 'uuid3']
arglist = ['--check-errors'] + nodes
verifylist = [('node', nodes), ('check_errors', True)]
self.client.wait_for_finish.return_value = {
'uuid1': {'finished': True, 'error': None},
'uuid2': {'finished': True, 'error': 'boom'},
'uuid3': {'finished': True, 'error': None},
}
cmd = shell.StartCommand(self.app, None)
parsed_args = self.check_parser(cmd, arglist, verifylist)
msg = "--check-errors can only be used with --wait"
self.assertRaisesRegex(exceptions.CommandError, msg, cmd.take_action,
parsed_args)
def test_abort(self):
node = 'uuid1'
arglist = [node]

View File

@ -0,0 +1,9 @@
---
features:
- |
Adds ``--check-errors`` flag to verify if any error occurred when
waiting for the introspection to finish for the selected nodes.
If any error occurs no output is displayed and the exit status for the
command is different from 0 (success).
If ``--check-errors`` is used without the ``--wait`` a CommandError is
thrown.