From ad470c343e4cafd975c8c313d04e762d47c32309 Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Fri, 4 Dec 2015 14:03:15 +0100 Subject: [PATCH] Fail the introspection command if introspection has failed Currently it only prints an error message, but exists with success. This makes it impossible to use this command in any kind of automated scripts or test it in our gate. This patch makes it raise an exception. Change-Id: I150c87252a48a8062aa7ef04c7a52433dd5ee37d --- tripleoclient/exceptions.py | 4 ++++ .../tests/v1/baremetal/test_baremetal.py | 20 +++++++++++++++++++ tripleoclient/v1/baremetal.py | 9 +++++---- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/tripleoclient/exceptions.py b/tripleoclient/exceptions.py index f33d5baa2..353f0998c 100644 --- a/tripleoclient/exceptions.py +++ b/tripleoclient/exceptions.py @@ -48,3 +48,7 @@ class RootUserExecution(Exception): class InvalidConfiguration(ValueError): """Invalid parameters were specified for the deployment""" pass + + +class IntrospectionError(RuntimeError): + """Introspection failed""" diff --git a/tripleoclient/tests/v1/baremetal/test_baremetal.py b/tripleoclient/tests/v1/baremetal/test_baremetal.py index 422a248b9..69bbd4ee9 100644 --- a/tripleoclient/tests/v1/baremetal/test_baremetal.py +++ b/tripleoclient/tests/v1/baremetal/test_baremetal.py @@ -458,6 +458,7 @@ class TestStartBaremetalIntrospectionBulk(fakes.TestBaremetal): client.node.list.return_value = [ mock.Mock(uuid="ABCDEFGH", provision_state="manageable") ] + get_status_mock.return_value = {'finished': True, 'error': None} parsed_args = self.check_parser(self.cmd, [], []) self.cmd.take_action(parsed_args) @@ -465,6 +466,25 @@ class TestStartBaremetalIntrospectionBulk(fakes.TestBaremetal): inspection_mock.assert_called_once_with( 'ABCDEFGH', base_url=None, auth_token='TOKEN') + @mock.patch.object(baremetal.inspector_client, 'get_status', autospec=True) + @mock.patch.object(baremetal.inspector_client, 'introspect', autospec=True) + def test_introspect_bulk_failed(self, inspection_mock, get_status_mock): + + client = self.app.client_manager.tripleoclient.baremetal + client.node.list.return_value = [ + mock.Mock(uuid="ABCDEFGH", provision_state="manageable") + ] + get_status_mock.return_value = {'finished': True, + 'error': 'fake error'} + + parsed_args = self.check_parser(self.cmd, [], []) + self.assertRaisesRegexp(exceptions.IntrospectionError, + 'ABCDEFGH: fake error', + self.cmd.take_action, parsed_args) + + inspection_mock.assert_called_once_with( + 'ABCDEFGH', base_url=None, auth_token='TOKEN') + @mock.patch('tripleoclient.utils.wait_for_node_introspection', autospec=True) @mock.patch('tripleoclient.utils.wait_for_provision_state', diff --git a/tripleoclient/v1/baremetal.py b/tripleoclient/v1/baremetal.py index 13e2d7c09..39aac5df9 100644 --- a/tripleoclient/v1/baremetal.py +++ b/tripleoclient/v1/baremetal.py @@ -238,7 +238,7 @@ class StartBaremetalIntrospectionBulk(IntrospectionParser, command.Command): time.sleep(5) print("Waiting for introspection to finish...") - has_errors = False + errors = [] for uuid, status in utils.wait_for_node_introspection( inspector_client, auth_token, parsed_args.inspector_url, node_uuids): @@ -248,7 +248,7 @@ class StartBaremetalIntrospectionBulk(IntrospectionParser, command.Command): else: print("Introspection for UUID {0} finished with error: {1}" .format(uuid, status['error'])) - has_errors = True + errors.append("%s: %s" % (uuid, status['error'])) print("Setting manageable nodes to available...") @@ -261,8 +261,9 @@ class StartBaremetalIntrospectionBulk(IntrospectionParser, command.Command): 'available', skipped_states=("available", "active")): print("Node {0} has been set to available.".format(uuid)) - if has_errors: - print("Introspection completed with errors.") + if errors: + raise exceptions.IntrospectionError( + "Introspection completed with errors:\n%s" % '\n'.join(errors)) else: print("Introspection completed.")