Completed
Pull Request — master (#2383)
by W
06:07
created

test_task_cancellation()   A

Complexity

Conditions 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 17
rs 9.4285
1
# Licensed to the StackStorm, Inc ('StackStorm') 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 ast
17
import eventlet
18
19
from integration.mistral import base
20
21
22
class WiringTest(base.TestWorkflowExecution):
23
24
    def test_basic_workflow(self):
25
        execution = self._execute_workflow('examples.mistral-basic', {'cmd': 'date'})
26
        execution = self._wait_for_completion(execution)
27
        self._assert_success(execution, num_tasks=1)
28
        self.assertIn('stdout', execution.result)
29
30
    def test_basic_workbook(self):
31
        execution = self._execute_workflow('examples.mistral-workbook-basic', {'cmd': 'date'})
32
        execution = self._wait_for_completion(execution)
33
        self._assert_success(execution, num_tasks=1)
34
        self.assertIn('stdout', execution.result)
35
36
    def test_complex_workbook(self):
37
        execution = self._execute_workflow(
38
            'examples.mistral-workbook-complex', {'vm_name': 'demo1'})
39
        execution = self._wait_for_completion(execution)
40
        self._assert_success(execution, num_tasks=8)
41
        self.assertIn('vm_id', execution.result)
42
43
    def test_complex_workbook_subflow_actions(self):
44
        execution = self._execute_workflow(
45
            'examples.mistral-workbook-subflows', {'subject': 'st2', 'adjective': 'cool'})
46
        execution = self._wait_for_completion(execution)
47
        self._assert_success(execution, num_tasks=2)
48
        self.assertIn('tagline', execution.result)
49
        self.assertEqual(execution.result['tagline'], 'st2 is cool!')
50
51
    def test_with_items(self):
52
        params = {'cmd': 'date', 'count': 8}
53
        execution = self._execute_workflow('examples.mistral-repeat', params)
54
        execution = self._wait_for_completion(execution)
55
        self._assert_success(execution, num_tasks=1)
56
        self.assertEqual(len(execution.result['result']), params['count'])
57
58
    def test_concurrent_load(self):
59
        wf_name = 'examples.mistral-workbook-complex'
60
        wf_params = {'vm_name': 'demo1'}
61
        executions = [self._execute_workflow(wf_name, wf_params) for i in range(3)]
62
63
        eventlet.sleep(30)
64
65
        for execution in executions:
66
            e = self._wait_for_completion(execution)
67
            self._assert_success(e, num_tasks=8)
68
            self.assertIn('vm_id', e.result)
69
70
    def test_execution_failure(self):
71
        execution = self._execute_workflow('examples.mistral-basic', {'cmd': 'foo'})
72
        execution = self._wait_for_completion(execution)
73
        self._assert_failure(execution)
74
75
    def test_cancellation(self):
76
        execution = self._execute_workflow('examples.mistral-test-cancel', {'sleep': 10})
77
        execution = self._wait_for_state(execution, ['running'])
78
        self.st2client.liveactions.delete(execution)
79
        execution = self._wait_for_completion(execution, expect_tasks_completed=False)
80
        self._assert_canceled(execution, are_tasks_completed=False)
81
82
    def test_task_cancellation(self):
83
        execution = self._execute_workflow('examples.mistral-test-cancel', {'sleep': 30})
84
        execution = self._wait_for_state(execution, ['running'])
85
86
        task_executions = [e for e in self.st2client.liveactions.get_all()
87
                           if e.context.get('parent', {}).get('execution_id') == execution.id]
88
89
        self.assertGreater(len(task_executions), 0)
90
91
        self.st2client.liveactions.delete(task_executions[0])
92
        execution = self._wait_for_completion(execution, expect_tasks_completed=True)
93
        self._assert_failure(execution)
94
95
        task_results = execution.result.get('tasks', [])
96
        self.assertGreater(len(task_results), 0)
97
        expected_state_info = {'error': 'Execution canceled by user.'}
98
        self.assertDictEqual(ast.literal_eval(task_results[0]['state_info']), expected_state_info)
99