forked from kubeflow/kfp-tekton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler_tests.py
380 lines (335 loc) · 14.1 KB
/
compiler_tests.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
# Copyright 2020 kubeflow.org
#
# 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.
import json
import logging
import os
import re
import shutil
import sys
import tempfile
import textwrap
import unittest
import yaml
from os import environ as env
from kfp_tekton import compiler
# temporarily set this flag to True in order to (re)generate new "golden" YAML
# files after making code changes that modify the expected YAML output.
# to (re)generate all "golden" YAML files from the command line run:
# GENERATE_GOLDEN_YAML=True sdk/python/tests/run_tests.sh
# or:
# make test GENERATE_GOLDEN_YAML=True
GENERATE_GOLDEN_YAML = env.get("GENERATE_GOLDEN_YAML", "False") == "True"
if GENERATE_GOLDEN_YAML:
logging.warning(
"The environment variable 'GENERATE_GOLDEN_YAML' was set to 'True'. Test cases will regenerate "
"the 'golden' YAML files instead of verifying the YAML produced by compiler.")
# License header for Kubeflow project
LICENSE_HEADER = textwrap.dedent("""\
# Copyright 2020 kubeflow.org
#
# 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.
""")
class TestTektonCompiler(unittest.TestCase):
def test_init_container_workflow(self):
"""
Test compiling a initial container workflow.
"""
from .testdata.init_container import init_container_pipeline
self._test_pipeline_workflow(init_container_pipeline, 'init_container.yaml')
def test_condition_workflow(self):
"""
Test compiling a conditional workflow
"""
from .testdata.condition import flipcoin
self._test_pipeline_workflow(flipcoin, 'condition.yaml')
def test_sequential_workflow(self):
"""
Test compiling a sequential workflow.
"""
from .testdata.sequential import sequential_pipeline
self._test_pipeline_workflow(sequential_pipeline, 'sequential.yaml')
def test_parallel_join_workflow_without_artifacts(self):
"""
Test compiling a parallel join workflow.
"""
from .testdata.parallel_join import download_and_join
self._test_pipeline_workflow(download_and_join, 'parallel_join.yaml', enable_artifacts=False)
def test_parallel_join_workflow_with_artifacts(self):
"""
Test compiling a parallel join workflow with artifacts and pipelineRun.
"""
from .testdata.parallel_join import download_and_join
self._test_pipeline_workflow(download_and_join,
'parallel_join_with_artifacts.yaml',
enable_artifacts=True)
def test_parallel_join_with_argo_vars_workflow(self):
"""
Test compiling a parallel join workflow.
"""
from .testdata.parallel_join_with_argo_vars import download_and_join_with_argo_vars
self._test_pipeline_workflow(download_and_join_with_argo_vars, 'parallel_join_with_argo_vars.yaml')
def test_sidecar_workflow(self):
"""
Test compiling a sidecar workflow.
"""
from .testdata.sidecar import sidecar_pipeline
self._test_pipeline_workflow(sidecar_pipeline, 'sidecar.yaml')
def test_loop_static_workflow(self):
"""
Test compiling a loop static params in workflow.
"""
from .testdata.loop_static import pipeline
self._test_pipeline_workflow(
pipeline,
'loop_static.yaml',
normalize_compiler_output_function=lambda f: re.sub(
"loop-item-param-.*-subvar", "loop-item-param-subvar", f))
def test_withitem_nested_workflow(self):
"""
Test compiling a withitem nested in workflow.
"""
from .testdata.withitem_nested import pipeline
self._test_pipeline_workflow(pipeline, 'withitem_nested.yaml')
def test_pipelineparams_workflow(self):
"""
Test compiling a pipelineparams workflow.
"""
from .testdata.pipelineparams import pipelineparams_pipeline
self._test_pipeline_workflow(pipelineparams_pipeline, 'pipelineparams.yaml')
def test_retry_workflow(self):
"""
Test compiling a retry task in workflow.
"""
from .testdata.retry import retry_sample_pipeline
self._test_pipeline_workflow(retry_sample_pipeline, 'retry.yaml')
def test_volume_workflow(self):
"""
Test compiling a volume workflow.
"""
from .testdata.volume import volume_pipeline
self._test_pipeline_workflow(volume_pipeline, 'volume.yaml')
def test_timeout_workflow(self):
"""
Test compiling a step level timeout workflow.
"""
from .testdata.timeout import timeout_sample_pipeline
self._test_pipeline_workflow(timeout_sample_pipeline, 'timeout.yaml')
def test_resourceOp_workflow(self):
"""
Test compiling a resourceOp basic workflow.
"""
from .testdata.resourceop_basic import resourceop_basic
self._test_pipeline_workflow(resourceop_basic, 'resourceop_basic.yaml')
def test_volumeOp_workflow(self):
"""
Test compiling a volumeOp basic workflow.
"""
from .testdata.volume_op import volumeop_basic
self._test_pipeline_workflow(volumeop_basic, 'volume_op.yaml')
def test_volumeSnapshotOp_workflow(self):
"""
Test compiling a volumeSnapshotOp basic workflow.
"""
from .testdata.volume_snapshot_op import volume_snapshotop_sequential
self._test_pipeline_workflow(volume_snapshotop_sequential, 'volume_snapshot_op.yaml')
def test_hidden_output_file_workflow(self):
"""
Test compiling a workflow with non configurable output file.
"""
from .testdata.hidden_output_file import hidden_output_file_pipeline
self._test_pipeline_workflow(hidden_output_file_pipeline, 'hidden_output_file.yaml')
def test_tolerations_workflow(self):
"""
Test compiling a tolerations workflow.
"""
from .testdata.tolerations import tolerations
self._test_pipeline_workflow(tolerations, 'tolerations.yaml')
def test_affinity_workflow(self):
"""
Test compiling a affinity workflow.
"""
from .testdata.affinity import affinity_pipeline
self._test_pipeline_workflow(affinity_pipeline, 'affinity.yaml')
def test_node_selector_workflow(self):
"""
Test compiling a node selector workflow.
"""
from .testdata.node_selector import node_selector_pipeline
self._test_pipeline_workflow(node_selector_pipeline, 'node_selector.yaml')
def test_pipeline_transformers_workflow(self):
"""
Test compiling a pipeline_transformers workflow with pod annotations and labels.
"""
from .testdata.pipeline_transformers import transform_pipeline
self._test_pipeline_workflow(transform_pipeline, 'pipeline_transformers.yaml')
def test_input_artifact_raw_value_workflow(self):
"""
Test compiling an input artifact workflow.
"""
from .testdata.input_artifact_raw_value import input_artifact_pipeline
self._test_pipeline_workflow(input_artifact_pipeline, 'input_artifact_raw_value.yaml')
def test_big_data_workflow(self):
"""
Test compiling a big data passing workflow.
"""
from .testdata.big_data_passing import file_passing_pipelines
self._test_pipeline_workflow(file_passing_pipelines, 'big_data_passing.yaml')
def test_katib_workflow(self):
"""
Test compiling a katib workflow.
"""
# dictionaries and lists do not preserve insertion order before Python 3.6.
# katib.py uses (string-serialized) dictionaries containing dsl.PipelineParam objects
# which can't be JSON-serialized so using json.dumps(sorted) is not an alternative
if sys.version_info < (3, 6, 0):
logging.warning("Skipping katib workflow test for Python version < 3.6.0")
else:
from .testdata.katib import mnist_hpo
self._test_pipeline_workflow(mnist_hpo, 'katib.yaml')
def test_load_from_yaml_workflow(self):
"""
Test compiling a pipeline with components loaded from yaml.
"""
from .testdata.load_from_yaml import component_yaml_pipeline
self._test_pipeline_workflow(component_yaml_pipeline, 'load_from_yaml.yaml')
def test_imagepullsecrets_workflow(self):
"""
Test compiling a imagepullsecrets workflow.
"""
from .testdata.imagepullsecrets import imagepullsecrets_pipeline
self._test_pipeline_workflow(imagepullsecrets_pipeline, 'imagepullsecrets.yaml')
def test_basic_no_decorator(self):
"""
Test compiling a basic workflow with no pipeline decorator
"""
from .testdata import basic_no_decorator
parameter_dict = {
"function": basic_no_decorator.save_most_frequent_word,
"name": 'Save Most Frequent Word',
"description": 'Get Most Frequent Word and Save to GCS',
"paramsList": [basic_no_decorator.message_param, basic_no_decorator.output_path_param]
}
self._test_workflow_without_decorator('basic_no_decorator.yaml', parameter_dict)
def test_exit_handler_workflow(self):
"""
Test compiling a exit handler workflow.
"""
from .testdata.exit_handler import download_and_print
self._test_pipeline_workflow(download_and_print, 'exit_handler.yaml')
def test_compose(self):
"""
Test compiling a simple workflow, and a bigger one composed from a simple one.
"""
from .testdata import compose
self._test_nested_workflow('compose.yaml', [compose.save_most_frequent_word, compose.download_save_most_frequent_word])
def _test_pipeline_workflow(self,
pipeline_function,
pipeline_yaml,
enable_artifacts=True,
normalize_compiler_output_function=None):
test_data_dir = os.path.join(os.path.dirname(__file__), 'testdata')
golden_yaml_file = os.path.join(test_data_dir, pipeline_yaml)
temp_dir = tempfile.mkdtemp()
compiled_yaml_file = os.path.join(temp_dir, 'workflow.yaml')
try:
compiler.TektonCompiler().compile(pipeline_function,
compiled_yaml_file,
enable_artifacts=enable_artifacts)
with open(compiled_yaml_file, 'r') as f:
f = normalize_compiler_output_function(
f.read()) if normalize_compiler_output_function else f
compiled = yaml.safe_load(f)
self._verify_compiled_workflow(golden_yaml_file, compiled)
finally:
shutil.rmtree(temp_dir)
def _test_workflow_without_decorator(self, pipeline_yaml, params_dict):
"""
Test compiling a workflow and appending pipeline params.
"""
test_data_dir = os.path.join(os.path.dirname(__file__), 'testdata')
golden_yaml_file = os.path.join(test_data_dir, pipeline_yaml)
temp_dir = tempfile.mkdtemp()
try:
compiled_workflow = compiler.TektonCompiler()._create_workflow(
params_dict['function'],
params_dict.get('name', None),
params_dict.get('description', None),
params_dict.get('paramsList', None),
params_dict.get('conf', None))
self._verify_compiled_workflow(golden_yaml_file, compiled_workflow)
finally:
shutil.rmtree(temp_dir)
def _test_nested_workflow(self,
pipeline_yaml,
pipeline_list,
enable_artifacts=True,
normalize_compiler_output_function=None):
"""
Test compiling a simple workflow, and a bigger one composed from the simple one.
"""
test_data_dir = os.path.join(os.path.dirname(__file__), 'testdata')
golden_yaml_file = os.path.join(test_data_dir, pipeline_yaml)
temp_dir = tempfile.mkdtemp()
compiled_yaml_file = os.path.join(temp_dir, 'nested' + str(len(pipeline_list) - 1) + '.yaml')
try:
for index, pipeline in enumerate(pipeline_list):
package_path = os.path.join(temp_dir, 'nested' + str(index) + '.yaml')
compiler.TektonCompiler().compile(pipeline,
package_path,
enable_artifacts=enable_artifacts)
with open(compiled_yaml_file, 'r') as f:
f = normalize_compiler_output_function(
f.read()) if normalize_compiler_output_function else f
compiled = yaml.safe_load(f)
self._verify_compiled_workflow(golden_yaml_file, compiled)
finally:
shutil.rmtree(temp_dir)
def _verify_compiled_workflow(self, golden_yaml_file, compiled_workflow):
"""
Tests if the compiled workflow matches the golden yaml.
"""
if GENERATE_GOLDEN_YAML:
with open(golden_yaml_file, 'w') as f:
f.write(LICENSE_HEADER)
with open(golden_yaml_file, 'a+') as f:
yaml.dump(compiled_workflow, f, default_flow_style=False)
else:
with open(golden_yaml_file, 'r') as f:
golden = yaml.safe_load(f)
self.maxDiff = None
# sort dicts and lists, insertion order was not guaranteed before Python 3.6
if sys.version_info < (3, 6, 0):
def sort_items(obj):
from collections import OrderedDict
if isinstance(obj, dict):
return OrderedDict({k: sort_items(v) for k, v in sorted(obj.items())})
elif isinstance(obj, list):
return sorted([sort_items(o) for o in obj], key=lambda x: str(x))
else:
return obj
golden = sort_items(golden)
compiled_workflow = sort_items(compiled_workflow)
self.assertEqual(golden, compiled_workflow,
msg="\n===[ " + golden_yaml_file.split(os.path.sep)[-1] + " ]===\n"
+ json.dumps(compiled_workflow, indent=2))