Debian Codegraph @

mistralclient/tests/functional/cli/v2/base_v2.py

raw symbols SCIP
1 # Copyright (c) 2014 Mirantis, Inc.
2 # Copyright 2020 Nokia Software.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # 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, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import os
17 import time
18
19 from tempest.lib import exceptions
20
21 from mistralclient.tests.functional.cli import base
22
23
24 # NOTE(amorin) this is the hardcoded mistral URL in devstack
25 # Since we moved to uwsgi, this is behind apache ProxyPass /workflow
26 MISTRAL_URL = "http://localhost/workflow/v2"
27
28
29 class MistralClientTestBase(base.MistralCLIAuth, base.MistralCLIAltAuth):
30
31 _mistral_url = MISTRAL_URL
32
33 @classmethod
34 def setUpClass(cls):
35 super(MistralClientTestBase, cls).setUpClass()
36
37 cls.wb_def = os.path.relpath(
38 'functionaltests/resources/v2/wb_v2.yaml', os.getcwd()
39 )
40
41 cls.wb_with_tags_def = os.path.relpath(
42 'functionaltests/resources/v2/wb_with_tags_v2.yaml', os.getcwd()
43 )
44
45 cls.wf_def = os.path.relpath(
46 'functionaltests/resources/v2/wf_v2.yaml', os.getcwd()
47 )
48
49 cls.wf_single_def = os.path.relpath(
50 'functionaltests/resources/v2/wf_single_v2.yaml', os.getcwd()
51 )
52
53 cls.wf_with_delay_def = os.path.relpath(
54 'functionaltests/resources/v2/wf_delay_v2.yaml', os.getcwd()
55 )
56
57 cls.wf_wrapping_wf = os.path.relpath(
58 'functionaltests/resources/v2/wf_wrapping_wf_v2.yaml', os.getcwd()
59 )
60 cls.top_level_wf = os.path.relpath(
61 'functionaltests/resources/v2/for_namespaces/top_level_wf.yaml',
62 os.getcwd()
63 )
64 cls.middle_wf = os.path.relpath(
65 'functionaltests/resources/v2/for_namespaces/middle_wf.yaml',
66 os.getcwd()
67 )
68 cls.lowest_level_wf = os.path.relpath(
69 'functionaltests/resources/v2/for_namespaces/lowest_level_wf.yaml',
70 os.getcwd()
71 )
72 cls.async_wf_def = os.path.relpath(
73 'functionaltests/resources/v2/async.yaml',
74 os.getcwd()
75 )
76
77 cls.act_def = os.path.relpath(
78 'functionaltests/resources/v2/action_v2.yaml', os.getcwd()
79 )
80
81 cls.act_tag_def = os.path.relpath(
82 'functionaltests/resources/v2/action_v2_tags.yaml', os.getcwd()
83 )
84
85 def setUp(self):
86 super(MistralClientTestBase, self).setUp()
87
88 def assertPartIn(self, needle, haystack, message=''):
89 self.assertTrue(any(needle in s for s in haystack), message)
90
91 def get_field_value(self, obj, field):
92 return [
93 o['Value'] for o in obj
94 if o['Field'] == "{0}".format(field)
95 ][0]
96
97 def get_item_info(self, get_from, get_by, value):
98 return [i for i in get_from if i[get_by] == value][0]
99
100 def mistral_admin(self, cmd, params=""):
101 self.clients = self._get_admin_clients()
102
103 return self.parser.listing(
104 self.mistral('{0}'.format(cmd), params='{0}'.format(params))
105 )
106
107 def mistral_alt_user(self, cmd, params=""):
108 self.clients = self._get_alt_clients()
109
110 return self.parser.listing(
111 self.mistral_alt('{0}'.format(cmd), params='{0}'.format(params))
112 )
113
114 def mistral_cli(self, admin, cmd, params=''):
115 if admin:
116 return self.mistral_admin(cmd, params)
117 else:
118 return self.mistral_alt_user(cmd, params)
119
120 def workbook_create(self, wb_def, namespace='', admin=True,
121 scope='private'):
122
123 params = '{0}'.format(wb_def)
124
125 namespace_params = ''
126
127 if namespace:
128 namespace_params += ' --namespace {}'.format(namespace)
129
130 if scope == 'public':
131 params += ' --public'
132
133 params += namespace_params
134
135 wb = self.mistral_cli(
136 admin,
137 'workbook-create',
138 params=params
139 )
140
141 wb_name = self.get_field_value(wb, "Name")
142
143 wb_delete_params = wb_name + namespace_params
144
145 self.addCleanup(
146 self.mistral_cli,
147 admin,
148 'workbook-delete',
149 params=wb_delete_params
150 )
151
152 self.addCleanup(
153 self.mistral_cli,
154 admin,
155 'workflow-delete',
156 params='wb.wf1'
157 )
158
159 return wb
160
161 def workflow_create(self, wf_def, namespace='', admin=True,
162 scope='private'):
163 params = '{0}'.format(wf_def)
164
165 if scope == 'public':
166 params += ' --public'
167
168 if namespace:
169 params += " --namespace " + namespace
170
171 wf = self.mistral_cli(
172 admin,
173 'workflow-create',
174 params=params
175 )
176
177 for workflow in wf:
178 self.addCleanup(
179 self.mistral_cli,
180 admin,
181 'workflow-delete',
182 params=workflow['ID']
183 )
184
185 return wf
186
187 def workflow_member_create(self, wf_id):
188 cmd_param = (
189 '%s workflow %s' % (wf_id, self.get_project_id("alt_demo"))
190 )
191 member = self.mistral_admin("member-create", params=cmd_param)
192
193 self.addCleanup(
194 self.mistral_admin,
195 'member-delete',
196 params=cmd_param
197 )
198
199 return member
200
201 def action_create(self, act_def, admin=True,
202 scope='private', namespace=''):
203 params = '{0}'.format(act_def)
204
205 if scope == 'public':
206 params += ' --public'
207
208 if namespace:
209 params += " --namespace " + namespace
210
211 acts = self.mistral_cli(
212 admin,
213 'action-create',
214 params=params
215 )
216
217 for action in acts:
218 self.addCleanup(
219 self.mistral_cli,
220 admin,
221 'action-delete',
222 params=action['Name']
223 )
224
225 return acts
226
227 def cron_trigger_create(self, name, wf_name, wf_input, pattern=None,
228 count=None, first_time=None, admin=True):
229 optional_params = ""
230
231 if pattern:
232 optional_params += ' --pattern "{}"'.format(pattern)
233 if count:
234 optional_params += ' --count {}'.format(count)
235 if first_time:
236 optional_params += ' --first-time "{}"'.format(first_time)
237
238 trigger = self.mistral_cli(
239 admin,
240 'cron-trigger-create',
241 params='{} {} {} {}'.format(name, wf_name, wf_input,
242 optional_params))
243
244 self.addCleanup(self.mistral_cli,
245 admin,
246 'cron-trigger-delete',
247 params=name)
248
249 return trigger
250
251 def event_trigger_create(self, name, wf_id, exchange,
252 topic, event, wf_input, admin=True):
253
254 trigger = self.mistral_cli(
255 admin,
256 'event-trigger-create',
257 params=' '.join((name, wf_id, exchange, topic, event, wf_input)))
258 ev_tr_id = self.get_field_value(trigger, 'ID')
259 self.addCleanup(self.mistral_cli,
260 admin,
261 'event-trigger-delete',
262 params=ev_tr_id)
263
264 return trigger
265
266 def execution_create(self, params, admin=True):
267 ex = self.mistral_cli(admin, 'execution-create', params=params)
268 exec_id = self.get_field_value(ex, 'ID')
269
270 self.addCleanup(
271 self.mistral_cli,
272 admin,
273 'execution-delete',
274 params="{} --force".format(exec_id)
275 )
276
277 return ex
278
279 def environment_create(self, params, admin=True):
280 env = self.mistral_cli(admin, 'environment-create', params=params)
281 env_name = self.get_field_value(env, 'Name')
282
283 self.addCleanup(
284 self.mistral_cli,
285 admin,
286 'environment-delete',
287 params=env_name
288 )
289
290 return env
291
292 def create_file(self, file_name, file_body=""):
293 f = open(file_name, 'w')
294 f.write(file_body)
295 f.close()
296
297 self.addCleanup(os.remove, file_name)
298
299 def wait_execution_success(self, exec_id, timeout=180):
300 start_time = time.time()
301
302 ex = self.mistral_admin('execution-get', params=exec_id)
303 exec_state = self.get_field_value(ex, 'State')
304
305 expected_states = ['SUCCESS', 'RUNNING']
306
307 while exec_state != 'SUCCESS':
308 if time.time() - start_time > timeout:
309 msg = ("Execution exceeds timeout {0} to change state "
310 "to SUCCESS. Execution: {1}".format(timeout, ex))
311
312 raise exceptions.TimeoutException(msg)
313
314 ex = self.mistral_admin('execution-get', params=exec_id)
315 exec_state = self.get_field_value(ex, 'State')
316
317 if exec_state not in expected_states:
318 msg = ("Execution state %s is not in expected "
319 "states: %s" % (exec_state, expected_states))
320
321 raise exceptions.TempestException(msg)
322
323 time.sleep(2)
324
325 return True