Passed
Pull Request — master (#4000)
by W
05:52
created

TestWorkflowExecution._wait_for_state()   B

Complexity

Conditions 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
c 1
b 0
f 0
dl 0
loc 24
rs 7.6129
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
from __future__ import absolute_import
17
import retrying
18
import six
19
import unittest2
20
21
from st2client import client as st2
22
from st2client import models
23
from st2common.constants import action as action_constants
24
25
26
LIVEACTION_LAUNCHED_STATUSES = [
27
    action_constants.LIVEACTION_STATUS_REQUESTED,
28
    action_constants.LIVEACTION_STATUS_SCHEDULED,
29
    action_constants.LIVEACTION_STATUS_RUNNING
30
]
31
32
33
def retry_on_exceptions(exc):
34
    return isinstance(exc, AssertionError)
35
36
37
class TestWorkflowExecution(unittest2.TestCase):
38
39
    @classmethod
40
    def setUpClass(cls):
41
        cls.st2client = st2.Client(base_url='http://127.0.0.1')
42
43
    def _execute_workflow(self, action, parameters=None):
44
        ex = models.LiveAction(action=action, parameters=(parameters or {}))
45
        ex = self.st2client.liveactions.create(ex)
46
        self.assertIsNotNone(ex.id)
47
        self.assertEqual(ex.action['ref'], action)
48
        self.assertIn(ex.status, LIVEACTION_LAUNCHED_STATUSES)
49
50
        return ex
51
52
    @retrying.retry(
53
        retry_on_exception=retry_on_exceptions,
54
        wait_fixed=3000, stop_max_delay=900000)
55
    def _wait_for_state(self, ex, states):
56
        if isinstance(states, six.string_types):
57
            states = [states]
58
59
        for state in states:
60
            if state not in action_constants.LIVEACTION_STATUSES:
61
                raise ValueError('Status %s is not valid.' % state)
62
63
        try:
64
            ex = self.st2client.liveactions.get_by_id(ex.id)
65
            self.assertIn(ex.status, states)
66
        except:
67
            if ex.status in action_constants.LIVEACTION_COMPLETED_STATES:
68
                raise Exception(
69
                    'Execution is in completed state and does not '
70
                    'match expected state(s).'
71
                )
72
            else:
73
                raise
74
75
        return ex
76
77
    def _get_children(self, ex):
78
        return self.st2client.liveactions.query(parent=ex.id)
79
80
    @retrying.retry(
81
        retry_on_exception=retry_on_exceptions,
82
        wait_fixed=3000, stop_max_delay=900000)
83
    def _wait_for_task(self, ex, task, status, num_task_exs=1):
84
        ex = self.st2client.liveactions.get_by_id(ex.id)
85
86
        task_exs = [
87
            task_ex for task_ex in self._get_children(ex)
88
            if (task_ex.context.get('mistral', {}).get('task_name', '') == task and
89
                task_ex.status == status)
90
        ]
91
92
        try:
93
            self.assertEqual(len(task_exs), num_task_exs)
94
            self.assertTrue(all([task_ex.status == status for task_ex in task_exs]))
95
        except:
96
            if ex.status in action_constants.LIVEACTION_COMPLETED_STATES:
97
                raise Exception(
98
                    'Execution is in completed state and does not '
99
                    'match expected task.'
100
                )
101
            else:
102
                raise
103
104
        return task_exs
105
106
    @retrying.retry(
107
        retry_on_exception=retry_on_exceptions,
108
        wait_fixed=3000, stop_max_delay=900000)
109
    def _wait_for_completion(self, ex):
110
        ex = self._wait_for_state(ex, action_constants.LIVEACTION_COMPLETED_STATES)
111
112
        try:
113
            self.assertTrue(hasattr(ex, 'result'))
114
        except:
115
            if ex.status in action_constants.LIVEACTION_COMPLETED_STATES:
116
                raise Exception(
117
                    'Execution is in completed state and does not '
118
                    'contain expected result.'
119
                )
120
            else:
121
                raise
122
123
        return ex
124