-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest_conductor.py
More file actions
422 lines (325 loc) · 16.2 KB
/
Copy pathtest_conductor.py
File metadata and controls
422 lines (325 loc) · 16.2 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
"""
Licensed to the Apache Software Foundation (ASF) 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.
"""
import composer
import conductor
import pytest
import os
name = 'TestAction'
wsk = conductor.openwhisk({ 'ignore_certs': 'IGNORE_CERTS' in os.environ and os.environ['IGNORE_CERTS'] == 'true' })
def define(action):
''' deploy action '''
try:
wsk.actions.delete(action['name'])
except:
pass
wsk.actions.create(action)
def invoke(composition, params = {}, blocking = True):
''' deploy and invoke composition '''
try:
extended = { 'name': name }
extended.update(composition.compile())
wsk.compositions.deploy(extended, True)
return wsk.actions.invoke({ 'name': name, 'params': params, 'blocking': blocking })
except Exception as err:
raise err
@pytest.fixture(scope="session", autouse=True)
def deploy_actions():
define({ 'name': 'echo', 'action': 'const main = x=>x', 'kind': 'nodejs:default' })
define({ 'name': 'DivideByTwo', 'action': 'function main({n}) { return { n: n / 2 } }', 'kind': 'nodejs:default'})
define({ 'name': 'TripleAndIncrement', 'action': 'function main({n}) { return { n: n * 3 + 1 } }', 'kind': 'nodejs:default' })
define({ 'name': 'isNotOne', 'action': 'function main({n}) { return { value: n != 1 } }', 'kind': 'nodejs:default' })
define({ 'name': 'isEven', 'action': 'function main({n}) { return { value: n % 2 == 0 } }', 'kind': 'nodejs:default'})
def isEven(env, args):
return args['n'] % 2 == 0
def set_then_true(env, args):
args['then'] = True
def set_else_true(env, args):
args['else'] = True
def dec_n(env, args):
return {'n': args['n'] - 1 }
def cond_false(env, args):
return False
def cond_nosave(env, args):
return { 'n': args['n'], 'value': args['n'] != 1 }
def cond_true(env, args):
return True
def return_error_message(env, args):
return {'message': args['error']}
def set_error(env, args):
return { 'error': 'foo' }
def set_p_4(env, args):
return { 'p': 4 }
def nest_params(env, args):
return { 'params': args }
def get_x(env, args):
return env['x']
def get_x_plus_y(env, args):
return env['x'] + env['y']
def get_value_plus_x(env, args):
return args['value'] + env['x']
def noop(env, args):
pass
class TestBlockingInvocations:
def test_action_true(self):
''' action must return true '''
activation = invoke(composer.action('isNotOne'), { 'n': 0 })
assert activation['response']['result'] == { 'value': True }
def test_action_false(self):
''' action must return false '''
activation = invoke(composer.action('isNotOne'), { 'n': 1 })
assert activation['response']['result'] == { 'value': False }
def test_action_activation_id(self):
''' action must return activationId '''
activation = invoke(composer.asynchronous('isNotOne'), { 'n': 1 }, False)
assert 'activationId' in activation
def test_parse_action_name(self):
combos = [
{ "n": 42, "s": False, "e": "Name must be a string" },
{ "n": "", "s": False, "e": "Name is not valid" },
{ "n": " ", "s": False, "e": "Name is not valid" },
{ "n": "/", "s": False, "e": "Name is not valid" },
{ "n": "//", "s": False, "e": "Name is not valid" },
{ "n": "/a", "s": False, "e": "Name is not valid" },
{ "n": "/a/b/c/d", "s": False, "e": "Name is not valid" },
{ "n": "/a/b/c/d/", "s": False, "e": "Name is not valid" },
{ "n": "a/b/c/d", "s": False, "e": "Name is not valid" },
{ "n": "/a/ /b", "s": False, "e": "Name is not valid" },
{ "n": "a", "e": False, "s": "/_/a" },
{ "n": "a/b", "e": False, "s": "/_/a/b" },
{ "n": "a/b/c", "e": False, "s": "/a/b/c" },
{ "n": "/a/b", "e": False, "s": "/a/b" },
{ "n": "/a/b/c", "e": False, "s": "/a/b/c" }
]
for combo in combos:
if combo["s"] is not False:
# good cases
assert composer.parse_action_name(combo["n"]) == combo["s"]
else:
# error cases
try:
composer.parse_action_name(combo["n"])
assert False
except composer.ComposerError as error:
assert error.message == combo["e"]
def test_invalid(self):
'''invalid options'''
try:
invoke(composer.action('foo', 42))
assert False
except composer.ComposerError as error:
assert error.message.startswith('Invalid argument')
class TestLiteral:
def test_boolean(self):
activation = invoke(composer.literal(True))
assert activation['response']['result'] == { 'value': True }
def test_number(self):
activation = invoke(composer.literal(42))
assert activation['response']['result'] == { 'value': 42 }
def test_invalid_arg(self):
try:
composer.literal(lambda x:x)
assert False
except composer.ComposerError as error:
assert error.message.startswith('Invalid argument')
class TestFunction:
def test_function_true(self):
activation = invoke(composer.function(isEven), { 'n': 4 })
assert activation['response']['result'] == { 'value': True }
def test_lambda(self):
activation = invoke(composer.function(lambda env, args: args['n'] % 2 == 0), { 'n': 4 })
assert activation['response']['result'] == { 'value': True }
class TestTasks:
def test_task_action(self):
activation = invoke(composer.task('isNotOne'), { 'n': 0 })
assert activation['response']['result'] == { 'value': True }
def test_task_function(self):
activation = invoke(composer.task(isEven), { 'n': 4 })
assert activation['response']['result'] == { 'value': True }
def test_task_none(self):
activation = invoke(composer.task(None), { 'foo': 'foo' })
assert activation['response']['result'] == { 'foo': 'foo' }
def test_task_fail(self):
'''none task must fail on error input'''
try:
invoke(composer.task(None), { 'error': 'foo' })
assert False
except Exception as error:
print(error)
class TestSequence:
def test_flat(self):
activation = invoke(composer.sequence('TripleAndIncrement', 'DivideByTwo', 'DivideByTwo'), { 'n': 5 })
assert activation['response']['result'] == { 'n': 4 }
def test_nested_right(self):
activation = invoke(composer.sequence('TripleAndIncrement', composer.sequence('DivideByTwo', 'DivideByTwo')), { 'n': 5 })
assert activation['response']['result'] == { 'n': 4 }
def test_nested_left(self):
activation = invoke(composer.sequence(composer.sequence('TripleAndIncrement', 'DivideByTwo'), 'DivideByTwo'), { 'n': 5 })
assert activation['response']['result'] == { 'n': 4 }
def test_seq(self):
activation = invoke(composer.seq('TripleAndIncrement', 'DivideByTwo', 'DivideByTwo'), { 'n': 5 })
assert activation['response']['result'] == { 'n': 4 }
class TestIf:
def test_condition_true(self):
activation = invoke(composer.when('isEven', 'DivideByTwo', 'TripleAndIncrement'), { 'n': 4 })
assert activation['response']['result'] == { 'n': 2 }
def test_condition_false(self):
activation = invoke(composer.when('isEven', 'DivideByTwo', 'TripleAndIncrement'), { 'n': 3 })
assert activation['response']['result'] == { 'n': 10 }
def test_condition_true_then_branch_only(self):
activation = invoke(composer.when('isEven', 'DivideByTwo'), { 'n': 4 })
assert activation['response']['result'] == { 'n': 2 }
def test_condition_false_then_branch_only(self):
activation = invoke(composer.when('isEven', 'DivideByTwo'), { 'n': 3 })
assert activation['response']['result'] == { 'n': 3 }
def test_condition_true_nosave_option(self):
activation = invoke(composer.when_nosave('isEven', set_then_true, set_else_true), { 'n': 2 })
assert activation['response']['result'] == { 'value': True, 'then': True }
def test_condition_false_nosave_option(self):
activation = invoke(composer.when_nosave('isEven', set_then_true, set_else_true), { 'n': 3 })
assert activation['response']['result'] == { 'value': False, 'else': True }
class TestLoop:
def test_a_few_iterations(self) :
activation = invoke(composer.loop('isNotOne', lambda env, args: {'n': args['n'] - 1 }), { 'n': 4 })
assert activation['response']['result'] == { 'n': 1 }
def test_no_iteration(self):
activation = invoke(composer.loop(cond_false, dec_n), { 'n': 1 })
assert activation['response']['result'] == { 'n': 1 }
def test_nosave_option(self) :
activation = invoke(composer.loop_nosave(cond_nosave, dec_n), { 'n': 4 })
assert activation['response']['result'] == { 'value': False, 'n': 1 }
class TestDoLoop:
def test_a_few_iterations(self) :
activation = invoke(composer.doloop(dec_n, 'isNotOne'), { 'n': 4 })
assert activation['response']['result'] == { 'n': 1 }
def test_one_iteration(self) :
activation = invoke(composer.doloop(dec_n, cond_false), { 'n': 1 })
assert activation['response']['result'] == { 'n': 0 }
def test_nosave_option(self) :
activation = invoke(composer.doloop_nosave(dec_n, cond_nosave), { 'n': 4 })
assert activation['response']['result'] == { 'value': False, 'n': 1 }
class TestDo: # Try
def test_no_error(self):
activation = invoke(composer.do(cond_true, return_error_message), {})
assert activation['response']['result'] == { 'value': True }
def test_error(self) :
activation = invoke(composer.do(set_error, return_error_message), {})
assert activation['response']['result'] == { 'message': 'foo' }
def test_try_must_throw(self) :
activation = invoke(composer.do(composer.task(None), return_error_message), { 'error': 'foo' })
assert activation['response']['result'] == { 'message': 'foo' }
def test_while_must_throw(self) :
activation = invoke(composer.do(composer.loop(composer.literal(False), None), return_error_message), { 'error': 'foo' })
assert activation['response']['result'] == { 'message': 'foo' }
def test_if_must_throw(self) :
activation = invoke(composer.do(composer.when(composer.literal(False), None), return_error_message), { 'error': 'foo' })
assert activation['response']['result'] == { 'message': 'foo' }
def test_retain(self) :
activation = invoke(composer.retain(composer.do(set_p_4, None)), { 'n': 3 })
assert activation['response']['result'] == { 'params': { 'n': 3 }, 'result': { 'p': 4 } }
class TestEnsure: # Finally
def test_no_error(self) :
activation = invoke(composer.ensure(cond_true, nest_params), {})
assert activation['response']['result'] == { 'params': { 'value': True } }
def test_error(self) :
activation = invoke(composer.ensure(set_error, nest_params), {})
assert activation['response']['result'] == { 'params': { 'error': 'foo' } }
class TestLet:
def test_one_variable(self) :
activation = invoke(composer.let({ 'x': 42 }, get_x), {})
assert activation['response']['result'] == { 'value': 42 }
def test_masking(self) :
activation = invoke(composer.let({ 'x': 42 }, composer.let({ 'x': 69 }, get_x)), {})
assert activation['response']['result'] == { 'value': 69 }
def test_two_variables(self) :
activation = invoke(composer.let({ 'x': 42 }, composer.let({ 'y': 69 }, get_x_plus_y)), {})
assert activation['response']['result'] == { 'value': 111 }
def test_two_variables_combined(self) :
activation = invoke(composer.let({ 'x': 42, 'y': 69 }, get_x_plus_y), {})
assert activation['response']['result'] == { 'value': 111 }
def test_scoping(self) :
activation = invoke(composer.let({ 'x': 42 }, composer.let({ 'x': 69 }, get_x), get_value_plus_x), {})
assert activation['response']['result'] == { 'value': 111 }
def test_invalid_argument(self):
try:
invoke(composer.let(invoke))
assert False
except composer.ComposerError as error:
assert error.message.startswith('Invalid argument')
class TestMask:
def test_let_let_mask(self) :
activation = invoke(composer.let({ 'x': 42 }, composer.let({ 'x': 69 }, composer.mask(get_x))), {})
assert activation['response']['result'] == { 'value': 42 }
def test_let_mask_let(self) :
activation = invoke(composer.let({ 'x': 42 }, composer.mask(composer.let({ 'x': 69 }, get_x))), {})
assert activation['response']['result'] == { 'value': 69 }
def test_let_let_try_mask(self) :
activation = invoke(composer.let({ 'x': 42 }, composer.let({ 'x': 69 },
composer.do(composer.mask(get_x), noop))))
assert activation['response']['result'] == { 'value': 42 }
def test_let_let_let_mask(self) :
activation = invoke(composer.let({ 'x': 42 }, composer.let({ 'x': 69 },
composer.let({ 'x': -1 }, composer.mask(get_x)))))
assert activation['response']['result'] == { 'value': 69 }
def test_let_let_let_mask_mask(self) :
activation = invoke(composer.let({ 'x': 42 }, composer.let({ 'x': 69 },
composer.let({ 'x': -1 }, composer.mask(composer.mask(get_x))))), {})
assert activation['response']['result'] == { 'value': 42 }
def test_let_let_mask_let_mask(self) :
activation = invoke(composer.let({ 'x': 42 }, composer.let({ 'x': 69 },
composer.mask(composer.let({ 'x': -1 }, composer.mask(get_x))))))
assert activation['response']['result'] == { 'value': 42 }
class TestRetain:
def test_base_case(self) :
activation = invoke(composer.retain('TripleAndIncrement'), { 'n': 3 })
assert activation['response']['result'] == { 'params': { 'n': 3 }, 'result': { 'n': 10 } }
def test_throw_error(self) :
try:
invoke(composer.retain(set_error), { 'n': 3 })
assert False
except Exception as err:
assert err.error['response']['result'] == { 'error': 'foo' }
def test_catch_error(self) :
activation = invoke(composer.retain_catch(set_error), { 'n': 3 })
assert activation['response']['result'] == { 'params': { 'n': 3 }, 'result': { 'error': 'foo' } }
class TestRepeat:
def test_a_few_iterations(self) :
activation = invoke(composer.repeat(3, 'DivideByTwo'), { 'n': 8 })
assert activation['response']['result'] == { 'n': 1 }
def test_invalid_argument(self) :
try:
invoke(composer.repeat('foo'))
assert False
except composer.ComposerError as error:
assert error.message.startswith('Invalid argument')
def retry_test(env, args):
x = env['x']
env['x'] -= 1
return { 'error': 'foo' } if x > 0 else 42
class TestRetry:
def test_success(self) :
activation = invoke(composer.let({ 'x': 2 }, composer.retry(2, retry_test)))
assert activation['response']['result'] == { 'value': 42 }
def test_failure(self) :
try:
invoke(composer.let({ 'x': 2 }, composer.retry(1, retry_test)))
assert False
except Exception as err:
assert err.error['response']['result'] == { 'error': 'foo' }
def test_invalid_argument(self) :
try:
invoke(composer.retry('foo'))
assert False
except composer.ComposerError as error:
assert error.message.startswith('Invalid argument')