/
test_workflow.py
1268 lines (981 loc) · 45.2 KB
/
test_workflow.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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
#
# Copyright 2017-2022 - Swiss Data Science Center (SDSC)
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
# Eidgenössische Technische Hochschule Zürich (ETHZ).
#
# Licensed 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.
"""Test ``workflow`` commands."""
import datetime
import itertools
import logging
import os
import re
import sys
import tempfile
import uuid
from pathlib import Path
import pexpect
import pyte
import pytest
from cwl_utils.parser import cwl_v1_2 as cwlgen
from renku.core.plugin.provider import available_workflow_providers
from renku.core.util.yaml import write_yaml
from renku.infrastructure.database import Database
from renku.infrastructure.gateway.activity_gateway import ActivityGateway
from renku.ui.cli import cli
from tests.utils import format_result_exception, write_and_commit_file
def _execute(capsys, runner, args):
with capsys.disabled():
try:
cli.main(
args=args,
prog_name=runner.get_default_prog_name(cli),
)
except SystemExit as e:
assert e.code in {None, 0}
def test_workflow_list(runner, project, run_shell, client):
"""Test listing of workflows."""
# Run a shell command with pipe.
output = run_shell('renku run --name run1 --description desc1 -- echo "a" > output1')
# Assert expected empty stdout.
assert b"" == output[0]
# Assert not allocated stderr.
assert output[1] is None
# Run a shell command with pipe.
output = run_shell("renku run --name run2 --description desc2 -- cp output1 output2")
# Assert expected empty stdout.
assert b"" == output[0]
# Assert not allocated stderr.
assert output[1] is None
result = runner.invoke(cli, ["workflow", "ls"])
assert 0 == result.exit_code, format_result_exception(result)
assert "run1" in result.output
assert "run2" in result.output
assert "desc1" not in result.output
assert "desc2" not in result.output
assert "echo a > output1" in result.output
assert "cp output1 output2" in result.output
result = runner.invoke(cli, ["workflow", "ls", "-c", "id,description"])
assert 0 == result.exit_code, format_result_exception(result)
assert "run1" not in result.output
assert "run2" not in result.output
assert "desc1" in result.output
assert "desc2" in result.output
assert "echo a > output1" not in result.output
assert "cp output1 output2" not in result.output
result = runner.invoke(cli, ["workflow", "ls", "--format", "json"])
assert 0 == result.exit_code, format_result_exception(result)
assert "run1" in result.output
assert "run2" in result.output
assert "desc1" in result.output
assert "desc2" in result.output
assert "echo a > output1" in result.output
assert "cp output1 output2" in result.output
def test_workflow_compose(runner, project, run_shell, client):
"""Test renku workflow compose."""
# Run a shell command with pipe.
output = run_shell('renku run --name run1 -- echo "a" > output1')
# Assert expected empty stdout.
assert b"" == output[0]
# Assert not allocated stderr.
assert output[1] is None
# Run a shell command with pipe.
output = run_shell("renku run --name run2 -- cp output1 output2")
# Assert expected empty stdout.
assert b"" == output[0]
# Assert not allocated stderr.
assert output[1] is None
result = runner.invoke(
cli,
[
"workflow",
"compose",
"--map",
"input_str=@step1.@param1",
"--map",
"output_file=run2.@output1",
"--link",
"@step1.@output1=@step2.@input1",
"--set",
"input_str=b",
"--set",
"output_file=other_output.csv",
"-p",
"input_str=the input string for the workflow",
"-p",
"output_file=the final output file produced",
"composite_workflow",
"run1",
"run2",
],
)
assert 0 == result.exit_code, format_result_exception(result)
database = Database.from_path(client.database_path)
composite_plan = database["plans-by-name"]["composite_workflow"]
assert composite_plan
assert len(composite_plan.plans) == 2
assert len(composite_plan.mappings) == 2
assert composite_plan.mappings[0].name == "input_str"
assert composite_plan.mappings[0].default_value == "b"
assert composite_plan.mappings[0].description == "the input string for the workflow"
assert composite_plan.mappings[1].name == "output_file"
assert composite_plan.mappings[1].default_value == "other_output.csv"
assert composite_plan.mappings[1].description == "the final output file produced"
result = runner.invoke(cli, ["graph", "export", "--format", "json-ld", "--strict"])
assert 0 == result.exit_code, format_result_exception(result)
def test_workflow_compose_from_paths(runner, project, run_shell, client):
"""Test renku workflow compose with input/output paths."""
# Run a shell command with pipe.
output = run_shell('renku run --name run1 -- echo "a" > output1')
# Assert expected empty stdout.
assert b"" == output[0]
# Assert not allocated stderr.
assert output[1] is None
# Run a shell command with pipe.
output = run_shell("renku run --name run2 -- cp output1 output2")
# Assert expected empty stdout.
assert b"" == output[0]
# Assert not allocated stderr.
assert output[1] is None
# Run a shell command with pipe.
output = run_shell("renku run --name run3 -- cat output1 output2 > output3")
# Assert expected empty stdout.
assert b"" == output[0]
# Assert not allocated stderr.
assert output[1] is None
result = runner.invoke(
cli,
[
"workflow",
"compose",
"--to",
"output3",
"composite_workflow1",
],
)
assert 0 == result.exit_code, format_result_exception(result)
database = Database.from_path(client.database_path)
composite_plan = database["plans-by-name"]["composite_workflow1"]
assert composite_plan
assert len(composite_plan.plans) == 3
assert len(composite_plan.mappings) == 7
assert composite_plan.mappings[0].name == "1-output-2"
assert composite_plan.mappings[0].default_value == "output1"
assert composite_plan.mappings[6].name == "3-output-3"
assert composite_plan.mappings[6].default_value == "output3"
result = runner.invoke(
cli,
[
"workflow",
"compose",
"--from",
"output1",
"--to",
"output3",
"composite_workflow2",
],
)
assert 0 == result.exit_code, format_result_exception(result)
database = Database.from_path(client.database_path)
composite_plan = database["plans-by-name"]["composite_workflow2"]
assert composite_plan
assert len(composite_plan.plans) == 2
assert len(composite_plan.mappings) == 5
assert composite_plan.mappings[0].name == "1-input-1"
assert composite_plan.mappings[0].default_value == "output1"
assert composite_plan.mappings[4].name == "2-output-3"
assert composite_plan.mappings[4].default_value == "output3"
result = runner.invoke(
cli,
[
"workflow",
"compose",
"--from",
"output1",
"composite_workflow3",
],
)
assert 0 == result.exit_code, format_result_exception(result)
database = Database.from_path(client.database_path)
composite_plan = database["plans-by-name"]["composite_workflow3"]
assert composite_plan
assert len(composite_plan.plans) == 2
assert len(composite_plan.mappings) == 5
assert composite_plan.mappings[0].name == "1-input-1"
assert composite_plan.mappings[0].default_value == "output1"
assert composite_plan.mappings[4].name == "2-output-3"
assert composite_plan.mappings[4].default_value == "output3"
def test_workflow_show(runner, project, run_shell, client):
"""Test renku workflow show."""
# Run a shell command with pipe.
output = run_shell('renku run --name run1 --description "my workflow" --success-code 0 -- echo "a" > output1')
# Assert expected empty stdout.
assert b"" == output[0]
# Assert not allocated stderr.
assert output[1] is None
# Run a shell command with pipe.
output = run_shell("renku run --name run2 -- cp output1 output2")
# Assert expected empty stdout.
assert b"" == output[0]
# Assert not allocated stderr.
assert output[1] is None
result = runner.invoke(cli, ["workflow", "show", "run1"])
assert 0 == result.exit_code, format_result_exception(result)
assert "run1" in result.output
assert "output1" in result.output
assert "my workflow" in result.output
result = runner.invoke(
cli,
[
"workflow",
"compose",
"--description",
"My composite workflow",
"--map",
"input_str=@step1.@param1",
"--map",
"output_file=run2.@output1",
"--link",
"@step1.@output1=@step2.@input1",
"--set",
"input_str=b",
"--set",
"output_file=other_output.csv",
"-p",
"input_str=the input string for the workflow",
"-p",
"output_file=the final output file produced",
"composite_workflow",
"run1",
"run2",
],
)
assert 0 == result.exit_code, format_result_exception(result)
result = runner.invoke(cli, ["workflow", "show", "composite_workflow"])
assert 0 == result.exit_code, format_result_exception(result)
assert "composite_workflow" in result.output
assert "input_str" in result.output
assert "output_file" in result.output
assert "Links:" in result.output
assert "Mappings:" in result.output
assert "My composite workflow" in result.output
def test_workflow_remove_command(runner, project):
"""Test workflow remove with builder."""
workflow_name = "test_workflow"
result = runner.invoke(cli, ["workflow", "remove", workflow_name])
assert 2 == result.exit_code
result = runner.invoke(cli, ["run", "--success-code", "0", "--no-output", "--name", workflow_name, "echo", "foo"])
assert 0 == result.exit_code, format_result_exception(result)
result = runner.invoke(cli, ["workflow", "remove", "--force", workflow_name])
assert 0 == result.exit_code, format_result_exception(result)
def test_workflow_remove_with_composite_command(runner, project):
"""Test workflow remove with builder."""
workflow_name = "test_workflow"
result = runner.invoke(cli, ["workflow", "remove", workflow_name])
assert 2 == result.exit_code
result = runner.invoke(cli, ["run", "--success-code", "0", "--no-output", "--name", workflow_name, "echo", "foo"])
assert 0 == result.exit_code, format_result_exception(result)
result = runner.invoke(cli, ["workflow", "compose", "composed-workflow", workflow_name])
assert 0 == result.exit_code, format_result_exception(result)
result = runner.invoke(cli, ["workflow", "remove", workflow_name])
assert 2 == result.exit_code, format_result_exception(result)
assert (
"The specified workflow 'test_workflow' is part of the following composite workflows and won't be removed"
in result.stderr
)
result = runner.invoke(cli, ["workflow", "remove", "--force", workflow_name])
assert 0 == result.exit_code, format_result_exception(result)
assert "Removing 'test_workflow', which is still used in these workflows" in result.output
def test_workflow_export_command(runner, project):
"""Test workflow export with builder."""
result = runner.invoke(cli, ["run", "--success-code", "0", "--no-output", "--name", "run1", "touch", "data.csv"])
assert 0 == result.exit_code, format_result_exception(result)
result = runner.invoke(cli, ["workflow", "export", "run1", "-o", "run1.cwl"])
assert 0 == result.exit_code, format_result_exception(result)
assert Path("run1.cwl").exists()
workflow = cwlgen.load_document("run1.cwl")
assert workflow.baseCommand[0] == "touch"
assert len(workflow.inputs) == 2
assert len(workflow.outputs) == 1
def test_workflow_edit(runner, client, run_shell):
"""Test naming of CWL tools and workflows."""
def _get_plan_id(output):
return output.split("\n")[0].split(":")[1].strip()
workflow_name = "run1"
result = runner.invoke(cli, ["run", "--name", workflow_name, "touch", "data.txt"])
assert 0 == result.exit_code, format_result_exception(result)
database = Database.from_path(client.database_path)
test_plan = database["plans-by-name"][workflow_name]
cmd = ["workflow", "edit", workflow_name, "--name", "first"]
result = runner.invoke(cli, cmd)
assert 0 == result.exit_code, format_result_exception(result)
workflow_name = "first"
database = Database.from_path(client.database_path)
first_plan = database["plans-by-name"]["first"]
assert first_plan
assert first_plan.name == "first"
assert first_plan.derived_from == test_plan.id
cmd = ["workflow", "edit", workflow_name, "--description", "Test workflow"]
result = runner.invoke(cli, cmd)
assert 0 == result.exit_code, format_result_exception(result)
database = Database.from_path(client.database_path)
first_plan = database["plans"][_get_plan_id(result.stdout)]
assert first_plan.description == "Test workflow"
# edit parameter
cmd = ["workflow", "edit", workflow_name, "--rename-param", "param1=param2"]
result = runner.invoke(cli, cmd)
assert 0 != result.exit_code, format_result_exception(result)
cmd = ["workflow", "edit", workflow_name, "--set", "param1=0"]
result = runner.invoke(cli, cmd)
assert 0 == result.exit_code, format_result_exception(result)
edited_plan_id = _get_plan_id(result.output)
database = Database.from_path(client.database_path)
renamed_param_plan = database["plans"][_get_plan_id(result.output)]
assert len(renamed_param_plan.parameters) > 0
cmd = ["workflow", "edit", workflow_name, "--rename-param", "param1=param2"]
result = runner.invoke(cli, cmd)
assert 0 == result.exit_code, format_result_exception(result)
database = Database.from_path(client.database_path)
renamed_param_plan = database["plans"][_get_plan_id(result.output)]
parameter_names = list(map(lambda x: x.name, renamed_param_plan.parameters))
assert len(parameter_names) > 0
assert "param1" not in parameter_names
assert "param2" in parameter_names
param2_plan_id = _get_plan_id(result.stdout)
# edit parameter description
cmd = ["workflow", "edit", param2_plan_id, "--describe-param", "param1=Test"]
result = runner.invoke(cli, cmd)
assert 0 != result.exit_code, format_result_exception(result)
cmd = ["workflow", "edit", param2_plan_id, "--describe-param", 'param2="Test parameter"']
result = runner.invoke(cli, cmd)
assert 0 == result.exit_code, format_result_exception(result)
database = Database.from_path(client.database_path)
renamed_param_plan = database["plans"][_get_plan_id(result.output)]
assert "Test parameter" == renamed_param_plan.parameters[0].description
# edit parameter mapping
cmd = ["run", "--name", "run2", "cp", "data.txt", "data2.txt"]
result = runner.invoke(cli, cmd)
assert 0 == result.exit_code, format_result_exception(result)
result = runner.invoke(
cli,
[
"workflow",
"compose",
"--description",
"My composite workflow",
"--map",
"input_str=@step1.@param1",
"--map",
"output_file=run2.@output1",
"--link",
"@step1.@output1=@step2.@input1",
"--set",
"input_str=b",
"--set",
"output_file=other_output.csv",
"-p",
"input_str=the input string for the workflow",
"-p",
"output_file=the final output file produced",
"composite_workflow",
edited_plan_id,
"run2",
],
)
assert 0 == result.exit_code, format_result_exception(result)
# remove mapping
cmd = ["workflow", "edit", "composite_workflow", "--map", "output_file="]
result = runner.invoke(cli, cmd)
assert 0 == result.exit_code, format_result_exception(result)
database = Database.from_path(client.database_path)
edited_composite_plan = database["plans"][_get_plan_id(result.output)]
assert len(edited_composite_plan.mappings) == 1
assert edited_composite_plan.mappings[0].mapped_parameters[0].name == "param1"
result = runner.invoke(cli, ["graph", "export", "--format", "json-ld", "--strict"])
assert 0 == result.exit_code, format_result_exception(result)
def test_workflow_show_outputs_with_directory(runner, client, run):
"""Output files in directory are not shown as separate outputs."""
base_sh = ["bash", "-c", 'DIR="$0"; mkdir -p "$DIR"; ' 'for x in "$@"; do touch "$DIR/$x"; done']
assert 0 == run(args=["run"] + base_sh + ["output", "foo", "bar"])
assert (client.path / "output" / "foo").exists()
assert (client.path / "output" / "bar").exists()
cmd = ["workflow", "outputs"]
result = runner.invoke(cli, cmd)
assert 0 == result.exit_code, format_result_exception(result)
assert {"output"} == set(result.output.strip().split("\n"))
result = runner.invoke(cli, cmd + ["output"])
assert 0 == result.exit_code, format_result_exception(result)
assert {"output"} == set(result.output.strip().split("\n"))
result = runner.invoke(cli, cmd + ["output/foo"])
assert 0 == result.exit_code, format_result_exception(result)
assert {"output"} == set(result.output.strip().split("\n"))
result = runner.invoke(cli, cmd + ["output/foo", "output/bar"])
assert 0 == result.exit_code, format_result_exception(result)
assert {"output"} == set(result.output.strip().split("\n"))
@pytest.mark.parametrize("provider", available_workflow_providers())
@pytest.mark.parametrize("yaml", [False, True])
@pytest.mark.parametrize(
"workflows, parameters",
[
([("run", 'echo "a" > output1')], {}),
([("run", 'echo "a" > output1')], {"run": {"outputs": ["replaced"]}}),
([("run", 'echo "a" > output1')], {"run": {"parameters": ["foo"], "outputs": ["bar"]}}),
([("run1", "touch data.csv"), ("run2", "wc data.csv > output")], {}),
(
[("run1", "touch data.csv"), ("run2", "wc data.csv > output")],
{"run1": {"outputs": ["foo"]}, "run2": {"inputs": ["foo"], "outputs": ["bar"]}},
),
(
[("run1", "touch data.csv"), ("run2", "touch data2.csv"), ("run3", "wc data.csv data2.csv > output")],
{
"run1": {"outputs": ["foo"]},
"run2": {"outputs": ["bar"]},
"run3": {"inputs": ["foo", "bar"], "outputs": ["changed"]},
},
),
],
)
def test_workflow_execute_command(runner, run_shell, project, capsys, client, provider, yaml, workflows, parameters):
"""Test workflow execute."""
for wf in workflows:
output = run_shell(f"renku run --name {wf[0]} -- {wf[1]}")
# Assert expected empty stdout.
assert b"" == output[0]
# Assert not allocated stderr.
assert output[1] is None
is_composite = True if len(workflows) > 1 else False
if is_composite:
composed_name = uuid.uuid4().hex
cmd = itertools.chain(["workflow", "compose", composed_name], map(lambda x: x[0], workflows))
result = runner.invoke(cli, cmd)
assert 0 == result.exit_code, format_result_exception(result)
def _flatten_dict(obj, key_string=""):
if type(obj) == dict:
key_string = key_string + "." if key_string else key_string
for k in obj:
yield from _flatten_dict(obj[k], key_string + str(k))
else:
yield key_string, obj
workflow_name = composed_name if is_composite else workflows[0][0]
if not parameters:
execute_cmd = ["workflow", "execute", "-p", provider, workflow_name]
_execute(capsys, runner, execute_cmd)
else:
database = Database.from_path(client.database_path)
plan = database["plans-by-name"][workflow_name]
execute_cmd = ["workflow", "execute", "-p", provider]
overrides = dict()
outputs = []
if is_composite:
overrides = {}
for p in plan.plans:
if p.name not in parameters:
continue
overrides[p.name] = {}
for k, values in parameters[p.name].items():
for i, v in enumerate(values):
overrides[p.name][getattr(p, k)[i].name] = v
if k == "outputs":
outputs.append(v)
else:
for k, values in parameters[workflow_name].items():
for i, v in enumerate(values):
overrides[getattr(plan, k)[i].name] = v
if k == "outputs":
outputs.append(v)
if yaml:
fd, values_path = tempfile.mkstemp()
os.close(fd)
write_yaml(values_path, overrides)
execute_cmd += ["--values", values_path]
else:
override_generator = _flatten_dict(overrides) if is_composite else overrides.items()
[execute_cmd.extend(["--set", f"{k}={v}"]) for k, v in override_generator]
execute_cmd.append(workflow_name)
_execute(capsys, runner, execute_cmd)
# check whether parameters setting was effective
for o in outputs:
assert Path(o).resolve().exists()
result = runner.invoke(cli, ["graph", "export", "--format", "json-ld", "--strict"])
assert 0 == result.exit_code, format_result_exception(result)
@pytest.mark.parametrize("provider", available_workflow_providers())
def test_workflow_execute_command_with_api_parameter_set(runner, run_shell, project, capsys, client, provider):
"""Test executing a workflow with --set for a renku.ui.api.Parameter."""
script = client.path / "script.py"
output = client.path / "output"
with client.commit():
script.write_text("from renku.ui.api import Parameter\n" 'print(Parameter("test", "hello world").value)\n')
result = run_shell(f"renku run --name run1 -- python3 {script} > {output}")
# Assert expected empty stdout.
assert b"" == result[0]
# Assert not allocated stderr.
assert result[1] is None
assert "hello world\n" == output.read_text()
result = run_shell(f"renku workflow execute -p {provider} --set test=goodbye run1")
# Assert not allocated stderr.
assert result[1] is None
assert "goodbye\n" == output.read_text()
result = runner.invoke(cli, ["graph", "export", "--format", "json-ld", "--strict"])
assert 0 == result.exit_code, format_result_exception(result)
@pytest.mark.parametrize("provider", available_workflow_providers())
def test_workflow_execute_command_with_api_input_set(runner, run_shell, project, capsys, client, provider):
"""Test executing a workflow with --set for a renku.ui.api.Input."""
script = client.path / "script.py"
output = client.path / "output"
input = client.path / "input"
input.write_text("input string")
other_input = client.path / "other_input"
other_input.write_text("my other input string")
with client.commit():
script.write_text(
f"from renku.ui.api import Input\nwith open(Input('my-input', '{input.name}'), 'r') as f:\n"
" print(f.read())"
)
result = run_shell(f"renku run --name run1 -- python3 {script.name} > {output.name}")
# Assert expected empty stdout.
assert b"" == result[0]
# Assert not allocated stderr.
assert result[1] is None
assert "input string\n" == output.read_text()
result = run_shell(f"renku workflow execute -p {provider} --set my-input={other_input.name} run1")
# Assert not allocated stderr.
assert result[1] is None
assert "my other input string\n" == output.read_text()
result = runner.invoke(cli, ["graph", "export", "--format", "json-ld", "--strict"])
assert 0 == result.exit_code, format_result_exception(result)
@pytest.mark.parametrize("provider", available_workflow_providers())
def test_workflow_execute_command_with_api_output_set(runner, run_shell, project, capsys, client, provider):
"""Test executing a workflow with --set for a renku.ui.api.Output."""
script = client.path / "script.py"
output = client.path / "output"
other_output = client.path / "other_output"
with client.commit():
script.write_text(
f"from renku.ui.api import Output\nwith open(Output('my-output', '{output.name}'), 'w') as f:\n"
" f.write('test')"
)
result = run_shell(f"renku run --name run1 -- python3 {script.name}")
# Assert expected empty stdout.
assert b"" == result[0]
# Assert not allocated stderr.
assert result[1] is None
assert "test" == output.read_text()
result = run_shell(f"renku workflow execute -p {provider} --set my-output={other_output.name} run1")
# Assert not allocated stderr.
assert result[1] is None
assert "test" == other_output.read_text()
result = runner.invoke(cli, ["graph", "export", "--format", "json-ld", "--strict"])
assert 0 == result.exit_code, format_result_exception(result)
def test_workflow_execute_command_with_api_duplicate_output(runner, run_shell, project, capsys, client):
"""Test executing a workflow with duplicate output with differing path."""
script = client.path / "script.py"
output = client.path / "output"
other_output = client.path / "other_output"
with client.commit():
script.write_text(
f"from renku.ui.api import Output\nopen(Output('my-output', '{output.name}'), 'w')\n"
f"open(Output('my-output', '{other_output.name}'), 'w')"
)
result = run_shell(f"renku run --name run1 -- python {script.name}")
# Assert expected empty stdout.
assert b"Error: Invalid parameter value - Duplicate input/output name found: my-output\n" in result[0]
def test_workflow_execute_command_with_api_valid_duplicate_output(runner, run_shell, project, capsys, client):
"""Test executing a workflow with duplicate output with same path."""
script = client.path / "script.py"
output = client.path / "output"
with client.commit():
script.write_text(
f"from renku.ui.api import Output\nopen(Output('my-output', '{output.name}'), 'w')\n"
f"open(Output('my-output', '{output.name}'), 'w')"
)
result = run_shell(f"renku run --name run1 -- python3 {script.name}")
# Assert expected empty stdout.
assert b"" == result[0]
# Assert not allocated stderr.
assert result[1] is None
def test_workflow_execute_command_with_api_duplicate_input(runner, run_shell, project, capsys, client):
"""Test executing a workflow with duplicate input with differing path."""
script = client.path / "script.py"
input = client.path / "input"
other_input = client.path / "other_input"
with client.commit():
script.write_text(
f"from renku.ui.api import Input\nopen(Input('my-input', '{input.name}'), 'w')\n"
f"open(Input('my-input', '{other_input.name}'), 'w')"
)
result = run_shell(f"renku run --no-output --name run1 -- python {script.name}")
# Assert expected empty stdout.
assert b"Error: Invalid parameter value - Duplicate input/output name found: my-input\n" in result[0]
def test_workflow_execute_command_with_api_valid_duplicate_input(runner, run_shell, project, capsys, client):
"""Test executing a workflow with duplicate input with same path."""
script = client.path / "script.py"
input = client.path / "input"
with client.commit():
script.write_text(
f"from renku.ui.api import Input\nopen(Input('my-input', '{input.name}'), 'w')\n"
f"open(Input('my-input', '{input.name}'), 'w')"
)
result = run_shell(f"renku run --no-output --name run1 -- python {script.name}")
# Assert expected empty stdout.
assert b"" == result[0]
# Assert not allocated stderr.
assert result[1] is None
def test_workflow_visualize_non_interactive(runner, project, client, workflow_graph):
"""Test renku workflow visualize in non-interactive mode."""
# We don't use pytest paramtrization for performance reasons, so we don't need to build the workflow_graph fixture
# for each execution
columns = [[], ["-c", "command"], ["-c", "command,id,date,plan"]]
from_command = [
([], set()),
(["--from", "B"], {"A", "Z", "H", "J"}),
(["--from", "A"], {"H", "J"}),
(["--from", "B", "--from", "A"], {"H", "J"}),
(["--from", "H"], {"A", "S", "Z"}),
]
paths = [([], set()), (["X"], {"H", "J"}), (["B", "Z"], {"H", "J", "X", "Y"}), (["J"], {"A", "S", "Z"})]
commands = list(itertools.product(columns, from_command, paths))
for command in commands:
excludes = set()
base_command = ["workflow", "visualize", "--no-pager"]
columns, from_command, paths = command
if columns:
base_command.extend(columns)
if from_command[0]:
base_command.extend(from_command[0])
excludes |= from_command[1]
if paths[0]:
base_command.extend(paths[0])
excludes |= paths[1]
result = runner.invoke(cli, base_command)
assert 0 == result.exit_code, format_result_exception(result)
assert all(e not in result.output for e in excludes)
result = runner.invoke(
cli, ["workflow", "visualize", "--no-pager", "-x", "-a", "--no-color", "--revision", "HEAD^", "H", "S"]
)
assert 0 == result.exit_code, format_result_exception(result)
assert "A" in result.output
assert "J" not in result.output
assert "S" in result.output
assert "H" in result.output
@pytest.mark.skip(
"Doesn't actually work, not really a tty available in github actions, "
"see https://github.com/actions/runner/issues/241"
)
def test_workflow_visualize_interactive(runner, project, client, workflow_graph):
"""Test renku workflow visualize in interactive mode."""
dimensions = (120, 120)
screen = pyte.Screen(dimensions[1], dimensions[0])
stream = pyte.Stream(screen)
output = []
def _try_and_show_error(child):
# If there was an error, we'd get the 'Aaaaahhh' screen, so get it to print the exception and return the
# screen after that.
child.send("\n")
child.expect(pexpect.TIMEOUT, timeout=2)
return _update_screen(child.before)
def _update_screen(data):
output.append(data)
for chunk in output:
stream.feed(chunk)
lines = screen.display
screen.reset()
return "\n".join(lines)
env = os.environ.copy()
env.update({"LINES": str(dimensions[0]), "COLUMNS": str(dimensions[1])})
# we need bash so everything behaves properly
child = pexpect.spawn("renku workflow visualize --interactive", encoding="utf-8", dimensions=dimensions, echo=False)
child.expect(pexpect.TIMEOUT, timeout=10)
content = _update_screen(child.before)
assert "Navigate using arrow keys" in content, _try_and_show_error(child)
assert "Press <q> to exit" in content, _try_and_show_error(child)
assert "echo test > A" in content, _try_and_show_error(child)
# show activity details
child.send(chr(10))
child.expect(pexpect.TIMEOUT, timeout=5)
content = _update_screen(child.before)
assert "Plan Id:" in content, _try_and_show_error(child)
assert "Inputs:" in content, _try_and_show_error(child)
assert "Outputs:" in content, _try_and_show_error(child)
assert "Agents:" in content, _try_and_show_error(child)
assert "Press <q> to exit" not in content, _try_and_show_error(child)
# move cursor around a bit, we use letters because ANSI codes don't seem to work in pexpect
child.send("k")
child.expect(pexpect.TIMEOUT, timeout=2)
_update_screen(child.before)
child.send("l")
child.expect(pexpect.TIMEOUT, timeout=2)
_update_screen(child.before)
child.send("k")
child.expect(pexpect.TIMEOUT, timeout=2)
_update_screen(child.before)
child.send("j")
child.expect(pexpect.TIMEOUT, timeout=2)
_update_screen(child.before)
child.send("i")
child.expect(pexpect.TIMEOUT, timeout=2)
_update_screen(child.before)
child.send(chr(10))
child.expect(pexpect.TIMEOUT, timeout=5)
content = _update_screen(child.before)
assert "Plan Id:" in content, _try_and_show_error(child)
assert "Inputs:" in content, _try_and_show_error(child)
assert "Outputs:" in content, _try_and_show_error(child)
assert "Agents:" in content, _try_and_show_error(child)
assert "Press <q> to exit" not in content, _try_and_show_error(child)
child.send("\n")
child.expect(pexpect.TIMEOUT, timeout=5)
_update_screen(child.before)
child.send("h")
child.expect(pexpect.TIMEOUT, timeout=5)
content = _update_screen(child.before)
assert "Navigate using arrow keys" in content, _try_and_show_error(child)
assert "Press <q> to exit" in content, _try_and_show_error(child)
assert "echo test > A" in content, _try_and_show_error(child)
assert "Inputs:" not in content, _try_and_show_error(child)
assert "Outputs:" not in content, _try_and_show_error(child)
assert "Agents:" not in content, _try_and_show_error(child)
child.send("q")
child.expect(pexpect.EOF, timeout=2)
assert not child.isalive()
def test_workflow_compose_execute(runner, project, run_shell, client):
"""Test renku workflow compose with execute."""
# Run a shell command with pipe.
output = run_shell('renku run --name run1 -- echo "a" > output1')
# Assert expected empty stdout.
assert b"" == output[0]
# Assert not allocated stderr.
assert output[1] is None
# Run a shell command with pipe.
output = run_shell("renku run --name run2 -- cp output1 output2")
# Assert expected empty stdout.
assert b"" == output[0]
# Assert not allocated stderr.
assert output[1] is None
# Run a shell command with pipe.
output = run_shell('renku run --name run3 -- echo "b" > output3')
# Assert expected empty stdout.
assert b"" == output[0]
# Assert not allocated stderr.
assert output[1] is None
# Run a shell command with pipe.
output = run_shell("renku run --name run4 -- cp output3 output4")
# Assert expected empty stdout.
assert b"" == output[0]
# Assert not allocated stderr.
assert output[1] is None
# we need to run in a subprocess to ensure the execute below uses a clean Database, to test against
# issues with cached parameters
output = run_shell("renku workflow compose --link run2.output-2=run4.input-1 composite_workflow1 run1 run2 run4")
# Assert not allocated stderr.
assert output[1] is None
assert "b\n" == Path("output4").read_text()