Completed
Push — master ( 9caa82...ed1c1b )
by W
07:04
created

integration.mistral.WiringTest.test_basic_rerun()   B

Complexity

Conditions 1

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 32
rs 8.8571
cc 1
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 st2client import models
20
21
from integration.mistral import base
22
23
24
class WiringTest(base.TestWorkflowExecution):
25
26
    def test_basic_workflow(self):
27
        execution = self._execute_workflow('examples.mistral-basic', {'cmd': 'date'})
28
        execution = self._wait_for_completion(execution)
29
        self._assert_success(execution, num_tasks=1)
30
        self.assertIn('stdout', execution.result)
31
32
    def test_basic_workbook(self):
33
        execution = self._execute_workflow('examples.mistral-workbook-basic', {'cmd': 'date'})
34
        execution = self._wait_for_completion(execution)
35
        self._assert_success(execution, num_tasks=1)
36
        self.assertIn('stdout', execution.result)
37
38
    def test_complex_workbook(self):
39
        execution = self._execute_workflow(
40
            'examples.mistral-workbook-complex', {'vm_name': 'demo1'})
41
        execution = self._wait_for_completion(execution)
42
        self._assert_success(execution, num_tasks=8)
43
        self.assertIn('vm_id', execution.result)
44
45
    def test_complex_workbook_subflow_actions(self):
46
        execution = self._execute_workflow(
47
            'examples.mistral-workbook-subflows', {'subject': 'st2', 'adjective': 'cool'})
48
        execution = self._wait_for_completion(execution)
49
        self._assert_success(execution, num_tasks=2)
50
        self.assertIn('tagline', execution.result)
51
        self.assertEqual(execution.result['tagline'], 'st2 is cool!')
52
53
    def test_with_items(self):
54
        params = {'cmd': 'date', 'count': 8}
55
        execution = self._execute_workflow('examples.mistral-repeat', params)
56
        execution = self._wait_for_completion(execution)
57
        self._assert_success(execution, num_tasks=1)
58
        self.assertEqual(len(execution.result['result']), params['count'])
59
60
    def test_concurrent_load(self):
61
        wf_name = 'examples.mistral-workbook-complex'
62
        wf_params = {'vm_name': 'demo1'}
63
        executions = [self._execute_workflow(wf_name, wf_params) for i in range(3)]
64
65
        eventlet.sleep(30)
66
67
        for execution in executions:
68
            e = self._wait_for_completion(execution)
69
            self._assert_success(e, num_tasks=8)
70
            self.assertIn('vm_id', e.result)
71
72
    def test_execution_failure(self):
73
        execution = self._execute_workflow('examples.mistral-basic', {'cmd': 'foo'})
74
        execution = self._wait_for_completion(execution)
75
        self._assert_failure(execution)
76
77
    def test_cancellation(self):
78
        execution = self._execute_workflow('examples.mistral-test-cancel', {'sleep': 10})
79
        execution = self._wait_for_state(execution, ['running'])
80
        self.st2client.liveactions.delete(execution)
81
        execution = self._wait_for_completion(execution, expect_tasks_completed=False)
82
        self._assert_canceled(execution, are_tasks_completed=False)
83
84
    def test_task_cancellation(self):
85
        execution = self._execute_workflow('examples.mistral-test-cancel', {'sleep': 30})
86
        execution = self._wait_for_state(execution, ['running'])
87
88
        task_executions = [e for e in self.st2client.liveactions.get_all()
89
                           if e.context.get('parent', {}).get('execution_id') == execution.id]
90
91
        self.assertGreater(len(task_executions), 0)
92
93
        self.st2client.liveactions.delete(task_executions[0])
94
        execution = self._wait_for_completion(execution, expect_tasks_completed=True)
95
        self._assert_failure(execution)
96
97
        task_results = execution.result.get('tasks', [])
98
        self.assertGreater(len(task_results), 0)
99
        expected_state_info = {'error': 'Execution canceled by user.'}
100
        self.assertDictEqual(ast.literal_eval(task_results[0]['state_info']), expected_state_info)
101
102
    def test_basic_rerun(self):
103
        switch = 'mistral-test-rerun-switch'
104
105
        # Rerun the workflow from the beginning.
106
        self.st2client.keys.update(models.KeyValuePair(name=switch, value='1'))
107
        execution = self._execute_workflow('examples.mistral-test-rerun')
108
        execution = self._wait_for_completion(execution)
109
        self._assert_failure(execution)
110
        orig_st2_ex_id = execution.id
111
        orig_wf_ex_id = execution.context['mistral']['execution_id']
112
113
        self.st2client.keys.update(models.KeyValuePair(name=switch, value='0'))
114
        execution = self.st2client.liveactions.re_run(orig_st2_ex_id)
115
        self.assertNotEqual(execution.id, orig_st2_ex_id)
116
        execution = self._wait_for_completion(execution)
117
        self._assert_success(execution, num_tasks=1)
118
        self.assertNotEqual(execution.context['mistral']['execution_id'], orig_wf_ex_id)
119
120
        # Rerun the workflow from the failed task.
121
        self.st2client.keys.update(models.KeyValuePair(name=switch, value='1'))
122
        execution = self._execute_workflow('examples.mistral-test-rerun')
123
        execution = self._wait_for_completion(execution)
124
        self._assert_failure(execution)
125
        orig_st2_ex_id = execution.id
126
        orig_wf_ex_id = execution.context['mistral']['execution_id']
127
128
        self.st2client.keys.update(models.KeyValuePair(name=switch, value='0'))
129
        execution = self.st2client.liveactions.re_run(orig_st2_ex_id, tasks=['task1'])
130
        self.assertNotEqual(execution.id, orig_st2_ex_id)
131
        execution = self._wait_for_completion(execution)
132
        self._assert_success(execution, num_tasks=1)
133
        self.assertEqual(execution.context['mistral']['execution_id'], orig_wf_ex_id)
134