Add tests
Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/c2481088 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/c2481088 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/c2481088 Branch: refs/heads/ARIA-126-update-node-statuses Commit: c248108870614f374410a959ac727646089e25b3 Parents: 2fa8466 Author: Avia Efrat <[email protected]> Authored: Fri Mar 24 16:44:00 2017 +0300 Committer: Avia Efrat <[email protected]> Committed: Sun Mar 26 11:31:09 2017 +0300 ---------------------------------------------------------------------- .../orchestrator/workflows/core/test_events.py | 130 +++++++++++++++++++ 1 file changed, 130 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c2481088/tests/orchestrator/workflows/core/test_events.py ---------------------------------------------------------------------- diff --git a/tests/orchestrator/workflows/core/test_events.py b/tests/orchestrator/workflows/core/test_events.py new file mode 100644 index 0000000..5e717e7 --- /dev/null +++ b/tests/orchestrator/workflows/core/test_events.py @@ -0,0 +1,130 @@ +import pytest +from tests import mock, storage +from aria.modeling.service_instance import NodeBase +from aria.orchestrator.decorators import operation, workflow +from aria.orchestrator.workflows.core import engine +from aria.orchestrator.workflows.executor.thread import ThreadExecutor +from aria.orchestrator.workflows import api + +global_test_dict = {} # used the capture transitional node state changes + + [email protected] +def ctx(tmpdir): + context = mock.context.simple(str(tmpdir)) + yield context + storage.release_sqlite_storage(context.model) + +# TODO another possible approach of writing these tests: +# Don't create a ctx for every test. +# Problem is, that if for every test we create a workflow that contains just one standard +# lifecycle operation, then by the time we try to run the second test, the workflow failes since +# the execution tries to go from 'terminated' to 'pending'. +# And if we write a workflow that contains all the lifecycle operations, then first we need to +# change the api of `mock.models.create_interface`, which a lot of other tests use, and second how +# do we check all the state transition during the workflow execution in a convenient way. + +TYPE_URI_NAME = 'tosca.interfaces.node.lifecycle.Standard' +SHORTHAND_NAME = 'Standard' + +def test_node_state_changes_as_a_result_of_standard_lifecycle_create(ctx): + node = run_operation_on_node(ctx, interface_name=TYPE_URI_NAME, op_name='create') + _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'create') + + +def test_node_state_changes_as_a_result_of_standard_lifecycle_configure(ctx): + node = run_operation_on_node(ctx, interface_name=TYPE_URI_NAME, op_name='configure') + _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'configure') + + +def test_node_state_changes_as_a_result_of_standard_lifecycle_start(ctx): + node = run_operation_on_node(ctx, interface_name=TYPE_URI_NAME, op_name='start') + _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'start') + + +def test_node_state_changes_as_a_result_of_standard_lifecycle_stop(ctx): + node = run_operation_on_node(ctx, interface_name=TYPE_URI_NAME, op_name='stop') + _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'stop') + + +def test_node_state_changes_as_a_result_of_standard_lifecycle_delete(ctx): + node = run_operation_on_node(ctx, interface_name=TYPE_URI_NAME, op_name='delete') + _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'delete') + + +def test_node_state_changes_as_a_result_of_standard_lifecycle_create_shorthand_name(ctx): + node = run_operation_on_node(ctx, interface_name=SHORTHAND_NAME, op_name='create') + _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'create') + + +def test_node_state_changes_as_a_result_of_standard_lifecycle_configure_shorthand_name(ctx): + node = run_operation_on_node(ctx, interface_name=SHORTHAND_NAME, op_name='configure') + _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'configure') + + +def test_node_state_changes_as_a_result_of_standard_lifecycle_start_shorthand_name(ctx): + node = run_operation_on_node(ctx, interface_name=SHORTHAND_NAME, op_name='start') + _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'start') + + +def test_node_state_changes_as_a_result_of_standard_lifecycle_stop_shorthand_name(ctx): + node = run_operation_on_node(ctx, interface_name=SHORTHAND_NAME, op_name='stop') + _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'stop') + + +def test_node_state_changes_as_a_result_of_standard_lifecycle_delete_shorthand_name(ctx): + node = run_operation_on_node(ctx, interface_name=SHORTHAND_NAME, op_name='delete') + _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'delete') + + +def test_node_state_doesnt_change_as_a_result_of_an_operation_that_is_not_standard_lifecycle1(ctx): + node = run_operation_on_node(ctx, interface_name='interface_name', op_name='op_name') + assert node.state == node.INITIAL + + +def test_node_state_doesnt_change_as_a_result_of_an_operation_that_is_not_standard_lifecycle2(ctx): + node = run_operation_on_node(ctx, interface_name='interface_name', op_name='create') + assert node.state == node.INITIAL + + +def run_operation_on_node(ctx, op_name, interface_name): + node = ctx.model.node.get_by_name(mock.models.DEPENDENCY_NODE_NAME) + interface = mock.models.create_interface( + service=node.service, + interface_name=interface_name, + operation_name=op_name, + operation_kwargs=dict(implementation='{name}.{func.__name__}'.format(name=__name__, + func=func))) + node.interfaces[interface.name] = interface + + eng = engine.Engine(executor=ThreadExecutor(), + workflow_context=ctx, + tasks_graph=single_operation_workflow(ctx=ctx, + node=node, + interface_name=interface_name, + op_name=op_name)) + eng.execute() + return node + + +def run_standard_lifecycle_operation_on_node(ctx, op_name): + return run_operation_on_node(ctx, interface_name='aria.interfaces.lifecycle.Standard', + op_name=op_name) + + +def _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, op_name): + assert global_test_dict['transitional_state'] == NodeBase._op_to_state[op_name]['transitional'] + assert node.state == NodeBase._op_to_state[op_name]['finished'] + + +@workflow +def single_operation_workflow(ctx, graph, node, interface_name, op_name): + graph.add_tasks(api.task.OperationTask.for_node( + node=node, + interface_name=interface_name, + operation_name=op_name)) + + +@operation +def func(ctx): + global_test_dict['transitional_state'] = ctx.node.state
