-
-
Notifications
You must be signed in to change notification settings - Fork 740
/
base.py
165 lines (132 loc) · 5.48 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
import os
import retrying
import shutil
import six
import tempfile
import unittest2
from st2client import client as st2
from st2client import models
from st2common.constants import action as action_constants
LIVEACTION_LAUNCHED_STATUSES = [
action_constants.LIVEACTION_STATUS_REQUESTED,
action_constants.LIVEACTION_STATUS_SCHEDULED,
action_constants.LIVEACTION_STATUS_RUNNING,
]
def retry_on_exceptions(exc):
return isinstance(exc, AssertionError)
class WorkflowControlTestCaseMixin(object):
def _create_temp_file(self):
_, temp_file_path = tempfile.mkstemp()
os.chmod(temp_file_path, 0o755) # nosec
return temp_file_path
def _delete_temp_file(self, temp_file_path):
if temp_file_path and os.path.exists(temp_file_path):
if os.path.isdir(temp_file_path):
shutil.rmtree(temp_file_path)
else:
os.remove(temp_file_path)
class TestWorkflowExecution(unittest2.TestCase):
@classmethod
def setUpClass(cls):
cls.st2client = st2.Client(base_url='http://127.0.0.1')
def _execute_workflow(
self,
action,
parameters=None,
execute_async=True,
expected_status=None,
expected_result=None,
):
ex = models.LiveAction(action=action, parameters=(parameters or {}))
ex = self.st2client.executions.create(ex)
self.assertIsNotNone(ex.id)
self.assertEqual(ex.action['ref'], action)
self.assertIn(ex.status, LIVEACTION_LAUNCHED_STATUSES)
if execute_async:
return ex
if expected_status is None:
expected_status = action_constants.LIVEACTION_STATUS_SUCCEEDED
self.assertIn(expected_status, action_constants.LIVEACTION_STATUSES)
ex = self._wait_for_completion(ex)
self.assertEqual(ex.status, expected_status)
self.assertDictEqual(ex.result, expected_result)
return ex
@retrying.retry(retry_on_exception=retry_on_exceptions, wait_fixed=3000, stop_max_delay=900000)
def _wait_for_state(self, ex, states):
if isinstance(states, six.string_types):
states = [states]
for state in states:
if state not in action_constants.LIVEACTION_STATUSES:
raise ValueError('Status %s is not valid.' % state)
try:
ex = self.st2client.executions.get_by_id(ex.id)
self.assertIn(ex.status, states)
except:
if ex.status in action_constants.LIVEACTION_COMPLETED_STATES:
raise Exception(
'Execution is in completed state "%s" and '
'does not match expected state(s). %s' % (ex.status, ex.result)
)
else:
raise
return ex
def _get_children(self, ex):
return self.st2client.executions.query(parent=ex.id)
@retrying.retry(retry_on_exception=retry_on_exceptions, wait_fixed=3000, stop_max_delay=900000)
def _wait_for_task(self, ex, task, status=None, num_task_exs=1):
ex = self.st2client.executions.get_by_id(ex.id)
task_exs = [
task_ex
for task_ex in self._get_children(ex)
if task_ex.context.get('orquesta', {}).get('task_name', '') == task
]
try:
self.assertEqual(len(task_exs), num_task_exs)
except:
if ex.status in action_constants.LIVEACTION_COMPLETED_STATES:
raise Exception(
'Execution is in completed state and does not '
'match expected number of tasks.'
)
else:
raise
if status is not None:
try:
self.assertTrue(all([task_ex.status == status for task_ex in task_exs]))
except:
if ex.status in action_constants.LIVEACTION_COMPLETED_STATES:
raise Exception(
'Execution is in completed state and not all tasks '
'match expected status "%s".' % status
)
else:
raise
return task_exs
@retrying.retry(retry_on_exception=retry_on_exceptions, wait_fixed=3000, stop_max_delay=900000)
def _wait_for_completion(self, ex):
ex = self._wait_for_state(ex, action_constants.LIVEACTION_COMPLETED_STATES)
try:
self.assertTrue(hasattr(ex, 'result'))
except:
if ex.status in action_constants.LIVEACTION_COMPLETED_STATES:
raise Exception(
'Execution is in completed state and does not ' 'contain expected result.'
)
else:
raise
return ex