vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / tests / orchestrator / workflows / core / test_events.py
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
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15
16 import pytest
17
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
23
24 from tests import mock, storage
25
26 global_test_dict = {}  # used to capture transitional node state changes
27
28
29 @pytest.fixture
30 def ctx(tmpdir):
31     context = mock.context.simple(str(tmpdir))
32     yield context
33     storage.release_sqlite_storage(context.model)
34
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.
43
44 TYPE_URI_NAME = 'tosca.interfaces.node.lifecycle.Standard'
45 SHORTHAND_NAME = 'Standard'
46
47
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')
52
53
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')
58
59
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')
64
65
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')
70
71
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')
76
77
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')
82
83
84 def test_node_state_changes_as_a_result_of_standard_lifecycle_configure_shorthand_name(
85         ctx, executor):
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')
89
90
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')
95
96
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')
101
102
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')
107
108
109 def test_node_state_doesnt_change_as_a_result_of_an_operation_that_is_not_standard_lifecycle1(
110         ctx, executor):
111     node = run_operation_on_node(
112         ctx, interface_name='interface_name', op_name='op_name', executor=executor)
113     assert node.state == node.INITIAL
114
115
116 def test_node_state_doesnt_change_as_a_result_of_an_operation_that_is_not_standard_lifecycle2(
117         ctx, executor):
118     node = run_operation_on_node(
119         ctx, interface_name='interface_name', op_name='create', executor=executor)
120     assert node.state == node.INITIAL
121
122
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)
133     )
134
135     eng = engine.Engine(executors={executor.__class__: executor})
136     eng.execute(ctx)
137     return node
138
139
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',
143                                  op_name=op_name,
144                                  executor=executor)
145
146
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']
150
151
152 @workflow
153 def single_operation_workflow(graph, node, interface_name, op_name, **_):
154     graph.add_tasks(api.task.OperationTask(
155         node,
156         interface_name=interface_name,
157         operation_name=op_name))
158
159
160 @operation
161 def func(ctx):
162     global_test_dict['transitional_state'] = ctx.node.state
163
164
165 @pytest.fixture
166 def executor():
167     result = ThreadExecutor()
168     try:
169         yield result
170     finally:
171         result.close()