From aa9b34d0a238d770644586e7f0a2f673b51f1365 Mon Sep 17 00:00:00 2001 From: Andrey Tykhonov Date: Mon, 19 Oct 2015 14:07:09 +0300 Subject: [PATCH] Replace exit_with_error with exceptions. Change-Id: I2c182eb821565a1d2a8d4bf720e05c2e58841652 Closes-bug: #1506542 --- fuelclient/cli/actions/health.py | 6 +++--- fuelclient/cli/error.py | 4 ++++ fuelclient/cli/formatting.py | 5 +++-- fuelclient/objects/node.py | 4 ++-- fuelclient/tests/unit/v1/test_performance.py | 4 +--- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/fuelclient/cli/actions/health.py b/fuelclient/cli/actions/health.py index a3b10fd..5afb5ec 100644 --- a/fuelclient/cli/actions/health.py +++ b/fuelclient/cli/actions/health.py @@ -15,7 +15,7 @@ from fuelclient.cli.actions.base import Action import fuelclient.cli.arguments as Args -from fuelclient.cli.error import exit_with_error +from fuelclient.cli.error import EnvironmentException from fuelclient.cli.formatting import format_table from fuelclient.cli.formatting import print_health_check from fuelclient.objects.environment import Environment @@ -53,7 +53,7 @@ class HealthCheckAction(Action): env = Environment(params.env) if env.status not in self._allowed_statuses and not params.force: - exit_with_error( + raise EnvironmentException( "Environment is not ready to run health check " "because it is in {0} state. " "Health check is likely to fail because of " @@ -61,7 +61,7 @@ class HealthCheckAction(Action): ) if env.is_customized and not params.force: - exit_with_error( + raise EnvironmentException( "Environment deployment facts were updated. " "Health check is likely to fail because of " "that. Use --force flag to proceed anyway." diff --git a/fuelclient/cli/error.py b/fuelclient/cli/error.py index e7e24b8..debbe65 100644 --- a/fuelclient/cli/error.py +++ b/fuelclient/cli/error.py @@ -100,6 +100,10 @@ class InvalidFileException(FuelClientException): pass +class EnvironmentException(Exception): + pass + + def exceptions_decorator(func): """Handles HTTP errors and expected exceptions that may occur in methods of APIClient class diff --git a/fuelclient/cli/formatting.py b/fuelclient/cli/formatting.py index ee9053f..48793e2 100644 --- a/fuelclient/cli/formatting.py +++ b/fuelclient/cli/formatting.py @@ -24,7 +24,7 @@ from time import sleep import six from fuelclient.cli.error import DeployProgressError -from fuelclient.cli.error import exit_with_error +from fuelclient.cli.error import InvalidDirectoryException from six.moves import urllib @@ -108,7 +108,8 @@ def download_snapshot_with_progress_bar( to some 'directory'. """ if not os.path.exists(directory): - exit_with_error("Folder {0} doesn't exist.".format(directory)) + raise InvalidDirectoryException( + "Folder {0} doesn't exist.".format(directory)) file_name = os.path.join( os.path.abspath(directory), url.split('/')[-1] diff --git a/fuelclient/objects/node.py b/fuelclient/objects/node.py index c80b225..b778411 100644 --- a/fuelclient/objects/node.py +++ b/fuelclient/objects/node.py @@ -15,7 +15,7 @@ from operator import attrgetter import os -from fuelclient.cli.error import exit_with_error +from fuelclient.cli.error import InvalidDirectoryException from fuelclient.objects.base import BaseObject from fuelclient.objects.environment import Environment @@ -105,7 +105,7 @@ class Node(BaseObject): def read_attribute(self, attributes_type, directory, serializer=None): attributes_directory = self.get_attributes_path(directory) if not os.path.exists(attributes_directory): - exit_with_error( + raise InvalidDirectoryException( "Folder {0} doesn't contain node folder '{1}'" .format(directory, "node_{0}".format(self.id)) ) diff --git a/fuelclient/tests/unit/v1/test_performance.py b/fuelclient/tests/unit/v1/test_performance.py index 54cc321..cd9a136 100644 --- a/fuelclient/tests/unit/v1/test_performance.py +++ b/fuelclient/tests/unit/v1/test_performance.py @@ -27,7 +27,6 @@ import testtools from fuelclient import client from fuelclient import fuelclient_settings -from fuelclient.objects import node from fuelclient import profiler from fuelclient.tests.unit.v1 import base from fuelclient.tests import utils @@ -137,9 +136,8 @@ class ClientPerfTest(base.UnitTestCase): self._invoke_client('env', '--list') - @mock.patch.object(node, 'exit_with_error', new_callable=mock.MagicMock) @mock.patch('__builtin__.open', create=True) - def test_upload_node_settings(self, m_open, m_exit): + def test_upload_node_settings(self, m_open): node_configs = [json.dumps(utils.get_fake_network_config(3)) for i in six_moves.range(self.NUMBER_OF_NODES)]