From 51c9db293692a503822299b33e09ebd1d37808a7 Mon Sep 17 00:00:00 2001 From: licanwei Date: Mon, 7 Aug 2017 00:50:03 -0700 Subject: [PATCH] Adjust the action state judgment logic Only when True is returned, the action state is set to SUCCEEDED some actions(such as migrate) will return None if exception raised Change-Id: I52e7a1ffb68f54594f2b00d9843e8e0a4c985667 (cherry picked from commit 965af1b6fd4c52627bfb1d772543cb2410b57537) --- watcher/applier/workflow_engine/default.py | 11 ++++---- .../test_taskflow_action_container.py | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/watcher/applier/workflow_engine/default.py b/watcher/applier/workflow_engine/default.py index 22630f607..8f2fbc74e 100644 --- a/watcher/applier/workflow_engine/default.py +++ b/watcher/applier/workflow_engine/default.py @@ -120,14 +120,15 @@ class TaskFlowActionContainer(base.BaseTaskFlowActionContainer): def do_execute(self, *args, **kwargs): LOG.debug("Running action: %s", self.name) - # NOTE: For result is False, set action state fail + # NOTE:Some actions(such as migrate) will return None when exception + # Only when True is returned, the action state is set to SUCCEEDED result = self.action.execute() - if result is False: - return self.engine.notify(self._db_action, - objects.action.State.FAILED) - else: + if result is True: return self.engine.notify(self._db_action, objects.action.State.SUCCEEDED) + else: + return self.engine.notify(self._db_action, + objects.action.State.FAILED) def do_post_execute(self): LOG.debug("Post-condition action: %s", self.name) diff --git a/watcher/tests/applier/workflow_engine/test_taskflow_action_container.py b/watcher/tests/applier/workflow_engine/test_taskflow_action_container.py index c05d47130..4139abbd7 100644 --- a/watcher/tests/applier/workflow_engine/test_taskflow_action_container.py +++ b/watcher/tests/applier/workflow_engine/test_taskflow_action_container.py @@ -20,6 +20,8 @@ import eventlet import mock from watcher.applier.workflow_engine import default as tflow +from watcher.common import clients +from watcher.common import nova_helper from watcher import objects from watcher.tests.db import base from watcher.tests.objects import utils as obj_utils @@ -55,6 +57,32 @@ class TestTaskFlowActionContainer(base.DbTestCase): self.assertTrue(action.state, objects.action.State.SUCCEEDED) + @mock.patch.object(clients.OpenStackClients, 'nova', mock.Mock()) + def test_execute_with_failed(self): + nova_util = nova_helper.NovaHelper() + instance = "31b9dd5c-b1fd-4f61-9b68-a47096326dac" + nova_util.nova.servers.get.return_value = instance + action_plan = obj_utils.create_test_action_plan( + self.context, audit_id=self.audit.id, + strategy_id=self.strategy.id, + state=objects.action.State.ONGOING) + + action = obj_utils.create_test_action( + self.context, action_plan_id=action_plan.id, + state=objects.action.State.ONGOING, + action_type='migrate', + input_parameters={"resource_id": + instance, + "migration_type": "live", + "destination_node": "host2", + "source_node": "host1"}) + action_container = tflow.TaskFlowActionContainer( + db_action=action, + engine=self.engine) + action_container.execute() + + self.assertTrue(action.state, objects.action.State.FAILED) + @mock.patch('eventlet.spawn') def test_execute_with_cancel_action_plan(self, mock_eventlet_spawn): action_plan = obj_utils.create_test_action_plan(