1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 from aria.orchestrator.decorators import operation, workflow
19 from aria.orchestrator.workflows.core import engine, graph_compiler
20 from aria.orchestrator.workflows.executor.thread import ThreadExecutor
21 from aria.orchestrator.workflows import api
22 from aria.modeling.service_instance import NodeBase
24 from tests import mock, storage
26 global_test_dict = {} # used to capture transitional node state changes
31 context = mock.context.simple(str(tmpdir))
33 storage.release_sqlite_storage(context.model)
35 # TODO another possible approach of writing these tests:
36 # Don't create a ctx for every test.
37 # Problem is, that if for every test we create a workflow that contains just one standard
38 # lifecycle operation, then by the time we try to run the second test, the workflow failes since
39 # the execution tries to go from 'terminated' to 'pending'.
40 # And if we write a workflow that contains all the lifecycle operations, then first we need to
41 # change the api of `mock.models.create_interface`, which a lot of other tests use, and second how
42 # do we check all the state transition during the workflow execution in a convenient way.
44 TYPE_URI_NAME = 'tosca.interfaces.node.lifecycle.Standard'
45 SHORTHAND_NAME = 'Standard'
48 def test_node_state_changes_as_a_result_of_standard_lifecycle_create(ctx, executor):
49 node = run_operation_on_node(
50 ctx, interface_name=TYPE_URI_NAME, op_name='create', executor=executor)
51 _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'create')
54 def test_node_state_changes_as_a_result_of_standard_lifecycle_configure(ctx, executor):
55 node = run_operation_on_node(
56 ctx, interface_name=TYPE_URI_NAME, op_name='configure', executor=executor)
57 _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'configure')
60 def test_node_state_changes_as_a_result_of_standard_lifecycle_start(ctx, executor):
61 node = run_operation_on_node(
62 ctx, interface_name=TYPE_URI_NAME, op_name='start', executor=executor)
63 _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'start')
66 def test_node_state_changes_as_a_result_of_standard_lifecycle_stop(ctx, executor):
67 node = run_operation_on_node(
68 ctx, interface_name=TYPE_URI_NAME, op_name='stop', executor=executor)
69 _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'stop')
72 def test_node_state_changes_as_a_result_of_standard_lifecycle_delete(ctx, executor):
73 node = run_operation_on_node(
74 ctx, interface_name=TYPE_URI_NAME, op_name='delete', executor=executor)
75 _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'delete')
78 def test_node_state_changes_as_a_result_of_standard_lifecycle_create_shorthand_name(ctx, executor):
79 node = run_operation_on_node(
80 ctx, interface_name=SHORTHAND_NAME, op_name='create', executor=executor)
81 _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'create')
84 def test_node_state_changes_as_a_result_of_standard_lifecycle_configure_shorthand_name(
86 node = run_operation_on_node(
87 ctx, interface_name=SHORTHAND_NAME, op_name='configure', executor=executor)
88 _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'configure')
91 def test_node_state_changes_as_a_result_of_standard_lifecycle_start_shorthand_name(ctx, executor):
92 node = run_operation_on_node(
93 ctx, interface_name=SHORTHAND_NAME, op_name='start', executor=executor)
94 _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'start')
97 def test_node_state_changes_as_a_result_of_standard_lifecycle_stop_shorthand_name(ctx, executor):
98 node = run_operation_on_node(
99 ctx, interface_name=SHORTHAND_NAME, op_name='stop', executor=executor)
100 _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'stop')
103 def test_node_state_changes_as_a_result_of_standard_lifecycle_delete_shorthand_name(ctx, executor):
104 node = run_operation_on_node(
105 ctx, interface_name=SHORTHAND_NAME, op_name='delete', executor=executor)
106 _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, 'delete')
109 def test_node_state_doesnt_change_as_a_result_of_an_operation_that_is_not_standard_lifecycle1(
111 node = run_operation_on_node(
112 ctx, interface_name='interface_name', op_name='op_name', executor=executor)
113 assert node.state == node.INITIAL
116 def test_node_state_doesnt_change_as_a_result_of_an_operation_that_is_not_standard_lifecycle2(
118 node = run_operation_on_node(
119 ctx, interface_name='interface_name', op_name='create', executor=executor)
120 assert node.state == node.INITIAL
123 def run_operation_on_node(ctx, op_name, interface_name, executor):
124 node = ctx.model.node.get_by_name(mock.models.DEPENDENCY_NODE_NAME)
125 interface = mock.models.create_interface(
126 service=node.service,
127 interface_name=interface_name,
128 operation_name=op_name,
129 operation_kwargs=dict(function='{name}.{func.__name__}'.format(name=__name__, func=func)))
130 node.interfaces[interface.name] = interface
131 graph_compiler.GraphCompiler(ctx, ThreadExecutor).compile(
132 single_operation_workflow(ctx, node=node, interface_name=interface_name, op_name=op_name)
135 eng = engine.Engine(executors={executor.__class__: executor})
140 def run_standard_lifecycle_operation_on_node(ctx, op_name, executor):
141 return run_operation_on_node(ctx,
142 interface_name='aria.interfaces.lifecycle.Standard',
147 def _assert_node_state_changed_as_a_result_of_standard_lifecycle_operation(node, op_name):
148 assert global_test_dict['transitional_state'] == NodeBase._OP_TO_STATE[op_name]['transitional']
149 assert node.state == NodeBase._OP_TO_STATE[op_name]['finished']
153 def single_operation_workflow(graph, node, interface_name, op_name, **_):
154 graph.add_tasks(api.task.OperationTask(
156 interface_name=interface_name,
157 operation_name=op_name))
162 global_test_dict['transitional_state'] = ctx.node.state
167 result = ThreadExecutor()