-
Notifications
You must be signed in to change notification settings - Fork 873
/
test_pipeline_package.py
474 lines (407 loc) · 18 KB
/
test_pipeline_package.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
# Copyright 2021 QuantumBlack Visual Analytics Limited
#
# 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
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
# NONINFRINGEMENT. IN NO EVENT WILL THE LICENSOR OR OTHER CONTRIBUTORS
# BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# The QuantumBlack Visual Analytics Limited ("QuantumBlack") name and logo
# (either separately or in combination, "QuantumBlack Trademarks") are
# trademarks of QuantumBlack. The License does not grant you any right or
# license to the QuantumBlack Trademarks. You may not use the QuantumBlack
# Trademarks or any confusingly similar mark as a trademark for your product,
# or use the QuantumBlack Trademarks in any other manner that might cause
# confusion in the marketplace, including but not limited to in advertising,
# on websites, or on software.
#
# See the License for the specific language governing permissions and
# limitations under the License.
import textwrap
from pathlib import Path
from zipfile import ZipFile
import pytest
import toml
from click.testing import CliRunner
from kedro.framework.cli.pipeline import _get_wheel_name
PIPELINE_NAME = "my_pipeline"
LETTER_ERROR = "It must contain only letters, digits, and/or underscores."
FIRST_CHAR_ERROR = "It must start with a letter or underscore."
TOO_SHORT_ERROR = "It must be at least 2 characters long."
@pytest.mark.usefixtures("chdir_to_dummy_project", "patch_log", "cleanup_dist")
class TestPipelinePackageCommand:
def assert_wheel_contents_correct(
self, wheel_location, package_name=PIPELINE_NAME, version="0.1"
):
wheel_name = _get_wheel_name(name=package_name, version=version)
wheel_file = wheel_location / wheel_name
assert wheel_file.is_file()
assert len(list(wheel_location.iterdir())) == 1
# pylint: disable=consider-using-with
wheel_contents = set(ZipFile(str(wheel_file)).namelist())
expected_files = {
f"{package_name}/__init__.py",
f"{package_name}/README.md",
f"{package_name}/nodes.py",
f"{package_name}/pipeline.py",
f"{package_name}/config/parameters/{package_name}.yml",
"tests/__init__.py",
"tests/test_pipeline.py",
}
assert expected_files <= wheel_contents
@pytest.mark.parametrize(
"options,package_name,version,success_message",
[
([], PIPELINE_NAME, "0.1", f"Pipeline `{PIPELINE_NAME}` packaged!"),
(
["--alias", "alternative"],
"alternative",
"0.1",
f"Pipeline `{PIPELINE_NAME}` packaged as `alternative`!",
),
(
["--version", "0.3"],
PIPELINE_NAME,
"0.3",
f"Pipeline `{PIPELINE_NAME}` packaged!",
),
],
)
def test_package_pipeline(
self,
fake_repo_path,
fake_project_cli,
options,
package_name,
version,
success_message,
fake_metadata,
):
result = CliRunner().invoke(
fake_project_cli, ["pipeline", "create", PIPELINE_NAME], obj=fake_metadata
)
assert result.exit_code == 0
result = CliRunner().invoke(
fake_project_cli,
["pipeline", "package", PIPELINE_NAME] + options,
obj=fake_metadata,
)
assert result.exit_code == 0
assert success_message in result.output
wheel_location = fake_repo_path / "src" / "dist"
assert f"Location: {wheel_location}" in result.output
self.assert_wheel_contents_correct(
wheel_location=wheel_location, package_name=package_name, version=version
)
@pytest.mark.parametrize("existing_dir", [True, False])
def test_pipeline_package_to_destination(
self, fake_project_cli, existing_dir, tmp_path, fake_metadata
):
destination = (tmp_path / "in" / "here").resolve()
if existing_dir:
destination.mkdir(parents=True)
result = CliRunner().invoke(
fake_project_cli, ["pipeline", "create", PIPELINE_NAME], obj=fake_metadata
)
assert result.exit_code == 0
result = CliRunner().invoke(
fake_project_cli,
["pipeline", "package", PIPELINE_NAME, "--destination", str(destination)],
obj=fake_metadata,
)
assert result.exit_code == 0
success_message = (
f"Pipeline `{PIPELINE_NAME}` packaged! Location: {destination}"
)
assert success_message in result.output
self.assert_wheel_contents_correct(wheel_location=destination)
def test_pipeline_package_overwrites_wheel(
self, fake_project_cli, tmp_path, fake_metadata
):
destination = (tmp_path / "in" / "here").resolve()
destination.mkdir(parents=True)
wheel_file = destination / _get_wheel_name(name=PIPELINE_NAME, version="0.1")
wheel_file.touch()
result = CliRunner().invoke(
fake_project_cli, ["pipeline", "create", PIPELINE_NAME], obj=fake_metadata
)
assert result.exit_code == 0
result = CliRunner().invoke(
fake_project_cli,
["pipeline", "package", PIPELINE_NAME, "--destination", str(destination)],
obj=fake_metadata,
)
assert result.exit_code == 0
warning_message = f"Package file {wheel_file} will be overwritten!"
success_message = (
f"Pipeline `{PIPELINE_NAME}` packaged! Location: {destination}"
)
assert warning_message in result.output
assert success_message in result.output
self.assert_wheel_contents_correct(wheel_location=destination)
@pytest.mark.parametrize(
"bad_alias,error_message",
[
("bad name", LETTER_ERROR),
("bad%name", LETTER_ERROR),
("1bad", FIRST_CHAR_ERROR),
("a", TOO_SHORT_ERROR),
],
)
def test_package_pipeline_bad_alias(
self, fake_project_cli, bad_alias, error_message
):
result = CliRunner().invoke(
fake_project_cli,
["pipeline", "package", PIPELINE_NAME, "--alias", bad_alias],
)
assert result.exit_code
assert error_message in result.output
def test_package_pipeline_invalid_module_path(self, fake_project_cli):
result = CliRunner().invoke(
fake_project_cli,
["pipeline", "package", f"pipelines/{PIPELINE_NAME}"],
)
error_message = (
"The pipeline location you provided is not a valid Python module path"
)
assert result.exit_code
assert error_message in result.output
def test_package_pipeline_no_config(
self, fake_repo_path, fake_project_cli, fake_metadata
):
result = CliRunner().invoke(
fake_project_cli,
["pipeline", "create", PIPELINE_NAME, "--skip-config"],
obj=fake_metadata,
)
assert result.exit_code == 0
result = CliRunner().invoke(
fake_project_cli, ["pipeline", "package", PIPELINE_NAME], obj=fake_metadata
)
assert result.exit_code == 0
assert f"Pipeline `{PIPELINE_NAME}` packaged!" in result.output
wheel_location = fake_repo_path / "src" / "dist"
assert f"Location: {wheel_location}" in result.output
# the wheel contents are slightly different (config shouldn't be included),
# which is why we can't call self.assert_wheel_contents_correct here
wheel_file = wheel_location / _get_wheel_name(name=PIPELINE_NAME, version="0.1")
assert wheel_file.is_file()
assert len(list((fake_repo_path / "src" / "dist").iterdir())) == 1
# pylint: disable=consider-using-with
wheel_contents = set(ZipFile(str(wheel_file)).namelist())
expected_files = {
f"{PIPELINE_NAME}/__init__.py",
f"{PIPELINE_NAME}/README.md",
f"{PIPELINE_NAME}/nodes.py",
f"{PIPELINE_NAME}/pipeline.py",
"tests/__init__.py",
"tests/test_pipeline.py",
}
assert expected_files <= wheel_contents
assert f"{PIPELINE_NAME}/config/parameters.yml" not in wheel_contents
def test_package_non_existing_pipeline_dir(
self, fake_package_path, fake_project_cli, fake_metadata
):
result = CliRunner().invoke(
fake_project_cli, ["pipeline", "package", "non_existing"], obj=fake_metadata
)
assert result.exit_code == 1
pipeline_dir = fake_package_path / "pipelines" / "non_existing"
error_message = f"Error: Directory '{pipeline_dir}' doesn't exist."
assert error_message in result.output
def test_package_empty_pipeline_dir(
self, fake_project_cli, fake_package_path, fake_metadata
):
pipeline_dir = fake_package_path / "pipelines" / "empty_dir"
pipeline_dir.mkdir()
result = CliRunner().invoke(
fake_project_cli, ["pipeline", "package", "empty_dir"], obj=fake_metadata
)
assert result.exit_code == 1
error_message = f"Error: '{pipeline_dir}' is an empty directory."
assert error_message in result.output
def test_package_modular_pipeline_with_nested_parameters(
self, fake_repo_path, fake_project_cli, fake_metadata
):
"""
The setup for the test is as follows:
Create two modular pipelines, to verify that only the parameter file with matching pipeline
name will be packaged.
Add a directory with a parameter file to verify that if a project has parameters structured
like below, that the ones inside a directory with the pipeline name are packaged as well
when calling `kedro pipeline package` for a specific pipeline.
parameters
└── retail
└── params1.ym
"""
CliRunner().invoke(
fake_project_cli, ["pipeline", "create", "retail"], obj=fake_metadata
)
CliRunner().invoke(
fake_project_cli,
["pipeline", "create", "retail_banking"],
obj=fake_metadata,
)
nested_param_path = Path(
fake_repo_path / "conf" / "base" / "parameters" / "retail"
)
nested_param_path.mkdir(parents=True, exist_ok=True)
(nested_param_path / "params1.yml").touch()
result = CliRunner().invoke(
fake_project_cli, ["pipeline", "package", "retail"], obj=fake_metadata
)
assert result.exit_code == 0
assert "Pipeline `retail` packaged!" in result.output
wheel_location = fake_repo_path / "src" / "dist"
assert f"Location: {wheel_location}" in result.output
wheel_name = _get_wheel_name(name="retail", version="0.1")
wheel_file = wheel_location / wheel_name
assert wheel_file.is_file()
assert len(list(wheel_location.iterdir())) == 1
# pylint: disable=consider-using-with
wheel_contents = set(ZipFile(str(wheel_file)).namelist())
assert "retail/config/parameters/retail/params1.yml" in wheel_contents
assert "retail/config/parameters/retail.yml" in wheel_contents
assert "retail/config/parameters/retail_banking.yml" not in wheel_contents
def test_package_pipeline_with_deep_nested_parameters(
self, fake_repo_path, fake_project_cli, fake_metadata
):
CliRunner().invoke(
fake_project_cli, ["pipeline", "create", "retail"], obj=fake_metadata
)
deep_nested_param_path = Path(
fake_repo_path / "conf" / "base" / "parameters" / "deep" / "retail"
)
deep_nested_param_path.mkdir(parents=True, exist_ok=True)
(deep_nested_param_path / "params1.yml").touch()
deep_nested_param_path2 = Path(
fake_repo_path / "conf" / "base" / "parameters" / "retail" / "deep"
)
deep_nested_param_path2.mkdir(parents=True, exist_ok=True)
(deep_nested_param_path2 / "params1.yml").touch()
deep_nested_param_path3 = Path(
fake_repo_path / "conf" / "base" / "parameters" / "deep"
)
deep_nested_param_path3.mkdir(parents=True, exist_ok=True)
(deep_nested_param_path3 / "retail.yml").touch()
result = CliRunner().invoke(
fake_project_cli, ["pipeline", "package", "retail"], obj=fake_metadata
)
assert result.exit_code == 0
assert "Pipeline `retail` packaged!" in result.output
wheel_location = fake_repo_path / "src" / "dist"
assert f"Location: {wheel_location}" in result.output
wheel_name = _get_wheel_name(name="retail", version="0.1")
wheel_file = wheel_location / wheel_name
assert wheel_file.is_file()
assert len(list(wheel_location.iterdir())) == 1
# pylint: disable=consider-using-with
wheel_contents = set(ZipFile(str(wheel_file)).namelist())
assert "retail/config/parameters/deep/retail/params1.yml" in wheel_contents
assert "retail/config/parameters/retail/deep/params1.yml" in wheel_contents
assert "retail/config/parameters/retail.yml" in wheel_contents
assert "retail/config/parameters/deep/retail.yml" in wheel_contents
def test_pipeline_package_version(
self, fake_repo_path, fake_package_path, fake_project_cli, fake_metadata
):
_pipeline_name = "data_engineering"
# the test version value is set separately in
# features/steps/test_starter/<repo>/src/<package>/pipelines/data_engineering/__init__.py
_test_version = "4.20.69"
pipelines_dir = fake_package_path / "pipelines" / _pipeline_name
assert pipelines_dir.is_dir()
result = CliRunner().invoke(
fake_project_cli, ["pipeline", "package", _pipeline_name], obj=fake_metadata
)
assert result.exit_code == 0
# test for actual version
wheel_location = fake_repo_path / "src" / "dist"
wheel_name = _get_wheel_name(name=_pipeline_name, version=_test_version)
wheel_file = wheel_location / wheel_name
assert wheel_file.is_file()
assert len(list(wheel_location.iterdir())) == 1
@pytest.mark.usefixtures(
"chdir_to_dummy_project", "patch_log", "cleanup_dist", "cleanup_pyproject_toml"
)
class TestPipelinePackageFromManifest:
def test_pipeline_package_all( # pylint: disable=too-many-locals
self, fake_repo_path, fake_project_cli, fake_metadata, tmp_path, mocker
):
# pylint: disable=import-outside-toplevel
from kedro.framework.cli import pipeline
spy = mocker.spy(pipeline, "_package_pipeline")
pyproject_toml = fake_repo_path / "pyproject.toml"
other_dest = tmp_path / "here"
other_dest.mkdir()
project_toml_str = textwrap.dedent(
f"""
[tool.kedro.pipeline.package]
first = {{destination = "{other_dest.as_posix()}"}}
second = {{alias = "ds", env = "local"}}
third = {{}}
"""
)
with pyproject_toml.open(mode="a") as file:
file.write(project_toml_str)
for name in ("first", "second", "third"):
CliRunner().invoke(
fake_project_cli, ["pipeline", "create", name], obj=fake_metadata
)
result = CliRunner().invoke(
fake_project_cli, ["pipeline", "package", "--all"], obj=fake_metadata
)
assert result.exit_code == 0
assert "Pipelines packaged!" in result.output
assert spy.call_count == 3
build_config = toml.loads(project_toml_str)
package_manifest = build_config["tool"]["kedro"]["pipeline"]["package"]
for pipeline_name, packaging_specs in package_manifest.items():
expected_call = mocker.call(pipeline_name, fake_metadata, **packaging_specs)
assert expected_call in spy.call_args_list
def test_pipeline_package_all_empty_toml(
self, fake_repo_path, fake_project_cli, fake_metadata, mocker
):
# pylint: disable=import-outside-toplevel
from kedro.framework.cli import pipeline
spy = mocker.spy(pipeline, "_package_pipeline")
pyproject_toml = fake_repo_path / "pyproject.toml"
with pyproject_toml.open(mode="a") as file:
file.write("\n[tool.kedro.pipeline.package]\n")
result = CliRunner().invoke(
fake_project_cli, ["pipeline", "package", "--all"], obj=fake_metadata
)
assert result.exit_code == 0
expected_message = (
"Nothing to package. Please update the `pyproject.toml` "
"package manifest section."
)
assert expected_message in result.output
assert not spy.called
def test_invalid_toml(self, fake_repo_path, fake_project_cli, fake_metadata):
pyproject_toml = fake_repo_path / "pyproject.toml"
with pyproject_toml.open(mode="a") as file:
file.write("what/toml?")
result = CliRunner().invoke(
fake_project_cli, ["pipeline", "package", "--all"], obj=fake_metadata
)
assert result.exit_code
assert isinstance(result.exception, toml.TomlDecodeError)
def test_pipeline_package_no_arg_provided(self, fake_project_cli, fake_metadata):
result = CliRunner().invoke(
fake_project_cli, ["pipeline", "package"], obj=fake_metadata
)
assert result.exit_code
expected_message = (
"Please specify a pipeline name or add '--all' to package all pipelines in "
"the `pyproject.toml` package manifest section."
)
assert expected_message in result.output