Cancel Action Plan

This patch adds feature to cancel action plan in watcher.
A General flow from watcher-api to watcher-applier is implemented.

action plan cancel can cancel any [ongoing, pending, recommended]
action plan, it will update the action states also to "cancelled".
For ongoing actions in action plan, actions needs to be aborted.
Seperate patches will be added to support abort operation
in each action.

Notification part is addressed by a seperate blueprint.
https://blueprints.launchpad.net/watcher/+spec/notifications-actionplan-cancel

Change-Id: I895a5eaca5239d5657702c8d1875b9ece21682dc
Partially-Implements: blueprint cancel-action-plan
This commit is contained in:
aditi
2017-05-26 11:40:36 +00:00
parent 58d86de064
commit d7a44739a6
16 changed files with 362 additions and 28 deletions

View File

@@ -456,7 +456,7 @@ ALLOWED_TRANSITIONS = [
{"original_state": objects.action_plan.State.RECOMMENDED,
"new_state": objects.action_plan.State.CANCELLED},
{"original_state": objects.action_plan.State.ONGOING,
"new_state": objects.action_plan.State.CANCELLED},
"new_state": objects.action_plan.State.CANCELLING},
{"original_state": objects.action_plan.State.PENDING,
"new_state": objects.action_plan.State.CANCELLED},
]

View File

@@ -19,6 +19,7 @@ import mock
from watcher.applier.action_plan import default
from watcher.applier import default as ap_applier
from watcher.common import exception
from watcher import notifications
from watcher import objects
from watcher.objects import action_plan as ap_objects
@@ -99,3 +100,27 @@ class TestDefaultActionPlanHandler(base.DbTestCase):
self.m_action_plan_notifications
.send_action_notification
.call_args_list)
@mock.patch.object(objects.ActionPlan, "get_by_uuid")
def test_cancel_action_plan(self, m_get_action_plan):
m_get_action_plan.return_value = self.action_plan
self.action_plan.state = ap_objects.State.CANCELLED
self.action_plan.save()
command = default.DefaultActionPlanHandler(
self.context, mock.MagicMock(), self.action_plan.uuid)
command.execute()
action = self.action.get_by_uuid(self.context, self.action.uuid)
self.assertEqual(ap_objects.State.CANCELLED, self.action_plan.state)
self.assertEqual(objects.action.State.CANCELLED, action.state)
@mock.patch.object(ap_applier.DefaultApplier, "execute")
@mock.patch.object(objects.ActionPlan, "get_by_uuid")
def test_cancel_action_plan_with_exception(self, m_get_action_plan,
m_execute):
m_get_action_plan.return_value = self.action_plan
m_execute.side_effect = exception.ActionPlanCancelled(
self.action_plan.uuid)
command = default.DefaultActionPlanHandler(
self.context, mock.MagicMock(), self.action_plan.uuid)
command.execute()
self.assertEqual(ap_objects.State.CANCELLED, self.action_plan.state)

View File

@@ -29,6 +29,7 @@ from watcher.common import utils
from watcher import notifications
from watcher import objects
from watcher.tests.db import base
from watcher.tests.objects import utils as obj_utils
class ExpectedException(Exception):
@@ -75,7 +76,8 @@ class TestDefaultWorkFlowEngine(base.DbTestCase):
except Exception as exc:
self.fail(exc)
def create_action(self, action_type, parameters, parents=None, uuid=None):
def create_action(self, action_type, parameters, parents=None, uuid=None,
state=None):
action = {
'uuid': uuid or utils.generate_uuid(),
'action_plan_id': 0,
@@ -88,7 +90,6 @@ class TestDefaultWorkFlowEngine(base.DbTestCase):
new_action = objects.Action(self.context, **action)
with mock.patch.object(notifications.action, 'send_create'):
new_action.create()
return new_action
def check_action_state(self, action, expected_state):
@@ -110,10 +111,14 @@ class TestDefaultWorkFlowEngine(base.DbTestCase):
except Exception as exc:
self.fail(exc)
@mock.patch.object(objects.ActionPlan, "get_by_id")
@mock.patch.object(notifications.action, 'send_execution_notification')
@mock.patch.object(notifications.action, 'send_update')
def test_execute_with_one_action(self, mock_send_update,
mock_execution_notification):
mock_execution_notification,
m_get_actionplan):
m_get_actionplan.return_value = obj_utils.get_test_action_plan(
self.context, id=0)
actions = [self.create_action("nop", {'message': 'test'})]
try:
self.engine.execute(actions)
@@ -122,10 +127,14 @@ class TestDefaultWorkFlowEngine(base.DbTestCase):
except Exception as exc:
self.fail(exc)
@mock.patch.object(objects.ActionPlan, "get_by_id")
@mock.patch.object(notifications.action, 'send_execution_notification')
@mock.patch.object(notifications.action, 'send_update')
def test_execute_nop_sleep(self, mock_send_update,
mock_execution_notification):
mock_execution_notification,
m_get_actionplan):
m_get_actionplan.return_value = obj_utils.get_test_action_plan(
self.context, id=0)
actions = []
first_nop = self.create_action("nop", {'message': 'test'})
second_nop = self.create_action("nop", {'message': 'second test'})
@@ -140,10 +149,14 @@ class TestDefaultWorkFlowEngine(base.DbTestCase):
except Exception as exc:
self.fail(exc)
@mock.patch.object(objects.ActionPlan, "get_by_id")
@mock.patch.object(notifications.action, 'send_execution_notification')
@mock.patch.object(notifications.action, 'send_update')
def test_execute_with_parents(self, mock_send_update,
mock_execution_notification):
mock_execution_notification,
m_get_actionplan):
m_get_actionplan.return_value = obj_utils.get_test_action_plan(
self.context, id=0)
actions = []
first_nop = self.create_action(
"nop", {'message': 'test'},
@@ -208,9 +221,13 @@ class TestDefaultWorkFlowEngine(base.DbTestCase):
except Exception as exc:
self.fail(exc)
@mock.patch.object(objects.ActionPlan, "get_by_id")
@mock.patch.object(notifications.action, 'send_execution_notification')
@mock.patch.object(notifications.action, 'send_update')
def test_execute_with_two_actions(self, m_send_update, m_execution):
def test_execute_with_two_actions(self, m_send_update, m_execution,
m_get_actionplan):
m_get_actionplan.return_value = obj_utils.get_test_action_plan(
self.context, id=0)
actions = []
second = self.create_action("sleep", {'duration': 0.0})
first = self.create_action("nop", {'message': 'test'})
@@ -225,11 +242,14 @@ class TestDefaultWorkFlowEngine(base.DbTestCase):
except Exception as exc:
self.fail(exc)
@mock.patch.object(objects.ActionPlan, "get_by_id")
@mock.patch.object(notifications.action, 'send_execution_notification')
@mock.patch.object(notifications.action, 'send_update')
def test_execute_with_three_actions(self, m_send_update, m_execution):
def test_execute_with_three_actions(self, m_send_update, m_execution,
m_get_actionplan):
m_get_actionplan.return_value = obj_utils.get_test_action_plan(
self.context, id=0)
actions = []
third = self.create_action("nop", {'message': 'next'})
second = self.create_action("sleep", {'duration': 0.0})
first = self.create_action("nop", {'message': 'hello'})
@@ -249,9 +269,13 @@ class TestDefaultWorkFlowEngine(base.DbTestCase):
except Exception as exc:
self.fail(exc)
@mock.patch.object(objects.ActionPlan, "get_by_id")
@mock.patch.object(notifications.action, 'send_execution_notification')
@mock.patch.object(notifications.action, 'send_update')
def test_execute_with_exception(self, m_send_update, m_execution):
def test_execute_with_exception(self, m_send_update, m_execution,
m_get_actionplan):
m_get_actionplan.return_value = obj_utils.get_test_action_plan(
self.context, id=0)
actions = []
third = self.create_action("no_exist", {'message': 'next'})
@@ -273,11 +297,14 @@ class TestDefaultWorkFlowEngine(base.DbTestCase):
self.check_action_state(second, objects.action.State.SUCCEEDED)
self.check_action_state(third, objects.action.State.FAILED)
@mock.patch.object(objects.ActionPlan, "get_by_id")
@mock.patch.object(notifications.action, 'send_execution_notification')
@mock.patch.object(notifications.action, 'send_update')
@mock.patch.object(factory.ActionFactory, "make_action")
def test_execute_with_action_exception(self, m_make_action, m_send_update,
m_send_execution):
m_send_execution, m_get_actionplan):
m_get_actionplan.return_value = obj_utils.get_test_action_plan(
self.context, id=0)
actions = [self.create_action("fake_action", {})]
m_make_action.return_value = FakeAction(mock.Mock())
@@ -286,3 +313,43 @@ class TestDefaultWorkFlowEngine(base.DbTestCase):
self.assertIsInstance(exc.kwargs['error'], ExpectedException)
self.check_action_state(actions[0], objects.action.State.FAILED)
@mock.patch.object(objects.ActionPlan, "get_by_uuid")
def test_execute_with_action_plan_cancel(self, m_get_actionplan):
obj_utils.create_test_goal(self.context)
strategy = obj_utils.create_test_strategy(self.context)
audit = obj_utils.create_test_audit(
self.context, strategy_id=strategy.id)
action_plan = obj_utils.create_test_action_plan(
self.context, audit_id=audit.id,
strategy_id=strategy.id,
state=objects.action_plan.State.CANCELLING)
action1 = obj_utils.create_test_action(
self.context, action_plan_id=action_plan.id,
action_type='nop', state=objects.action.State.SUCCEEDED,
input_parameters={'message': 'hello World'})
action2 = obj_utils.create_test_action(
self.context, action_plan_id=action_plan.id,
action_type='nop', state=objects.action.State.ONGOING,
uuid='9eb51e14-936d-4d12-a500-6ba0f5e0bb1c',
input_parameters={'message': 'hello World'})
action3 = obj_utils.create_test_action(
self.context, action_plan_id=action_plan.id,
action_type='nop', state=objects.action.State.PENDING,
uuid='bc7eee5c-4fbe-4def-9744-b539be55aa19',
input_parameters={'message': 'hello World'})
m_get_actionplan.return_value = action_plan
actions = []
actions.append(action1)
actions.append(action2)
actions.append(action3)
self.assertRaises(exception.ActionPlanCancelled,
self.engine.execute, actions)
try:
self.check_action_state(action1, objects.action.State.SUCCEEDED)
self.check_action_state(action2, objects.action.State.CANCELLED)
self.check_action_state(action3, objects.action.State.CANCELLED)
except Exception as exc:
self.fail(exc)

View File

@@ -0,0 +1,79 @@
# -*- encoding: utf-8 -*-
# Copyright (c) 2015 b<>com
#
# Authors: Jean-Emile DARTOIS <jean-emile.dartois@b-com.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import eventlet
import mock
from watcher.applier.workflow_engine import default as tflow
from watcher import objects
from watcher.tests.db import base
from watcher.tests.objects import utils as obj_utils
class TestTaskFlowActionContainer(base.DbTestCase):
def setUp(self):
super(TestTaskFlowActionContainer, self).setUp()
self.engine = tflow.DefaultWorkFlowEngine(
config=mock.Mock(),
context=self.context,
applier_manager=mock.MagicMock())
obj_utils.create_test_goal(self.context)
self.strategy = obj_utils.create_test_strategy(self.context)
self.audit = obj_utils.create_test_audit(
self.context, strategy_id=self.strategy.id)
def test_execute(self):
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='nop',
input_parameters={'message': 'hello World'})
action_container = tflow.TaskFlowActionContainer(
db_action=action,
engine=self.engine)
action_container.execute()
self.assertTrue(action.state, objects.action.State.SUCCEEDED)
@mock.patch('eventlet.spawn')
def test_execute_with_cancel_action_plan(self, mock_eventlet_spawn):
action_plan = obj_utils.create_test_action_plan(
self.context, audit_id=self.audit.id,
strategy_id=self.strategy.id,
state=objects.action_plan.State.CANCELLING)
action = obj_utils.create_test_action(
self.context, action_plan_id=action_plan.id,
state=objects.action.State.ONGOING,
action_type='nop',
input_parameters={'message': 'hello World'})
action_container = tflow.TaskFlowActionContainer(
db_action=action,
engine=self.engine)
def empty_test():
pass
et = eventlet.spawn(empty_test)
mock_eventlet_spawn.return_value = et
action_container.execute()
et.kill.assert_called_with()