/
test_template.py
329 lines (236 loc) · 13.1 KB
/
test_template.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
# -*- coding: utf-8 -*-
#
# Copyright 2018-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 ``template`` command."""
import re
import sys
import pytest
from packaging.version import Version
from renku.core.util.contexts import chdir
from renku.domain_model.template import TemplateMetadata, TemplateParameter
from renku.ui.cli import cli
from tests.utils import format_result_exception, write_and_commit_file
TEMPLATES_URL = "https://github.com/SwissDataScienceCenter/renku-project-template"
@pytest.mark.serial
def test_template_list(isolated_runner):
"""Test list Renku templates."""
command = ["template", "ls"]
argv = sys.argv
sys.argv = command
try:
result = isolated_runner.invoke(cli, command)
assert 0 == result.exit_code, format_result_exception(result)
assert "python-minimal" in result.output
finally:
sys.argv = argv
@pytest.mark.integration
def test_template_list_from_source(isolated_runner):
"""Test list templates from other sources."""
command = ["template", "ls"]
argv = sys.argv
sys.argv = command
try:
result = isolated_runner.invoke(cli, command + ["--source", TEMPLATES_URL])
assert 0 == result.exit_code, format_result_exception(result)
assert "python-minimal" in result.output
assert "julia-minimal" in result.output
result = isolated_runner.invoke(cli, command + ["-s", TEMPLATES_URL, "--reference", "0.1.10"])
assert 0 == result.exit_code, format_result_exception(result)
assert "python-minimal" in result.output
assert "julia-minimal" not in result.output
finally:
sys.argv = argv
def test_template_show(isolated_runner):
"""Test show detailed template info."""
command = ["template", "show"]
argv = sys.argv
sys.argv = command
try:
result = isolated_runner.invoke(cli, command + ["R-minimal"])
assert 0 == result.exit_code, format_result_exception(result)
assert re.search("^Name: Basic R (.*) Project$", result.output, re.MULTILINE) is not None
finally:
sys.argv = argv
def test_template_show_no_id(runner, client):
"""Test show current project's template."""
result = runner.invoke(cli, ["template", "show"])
assert 0 == result.exit_code, format_result_exception(result)
assert re.search("^Name: Basic Python (.*) Project$", result.output, re.MULTILINE) is not None
def test_template_show_no_id_outside_project(isolated_runner):
"""Test show with no id."""
command = ["template", "show"]
argv = sys.argv
sys.argv = command
try:
result = isolated_runner.invoke(cli, command)
assert 2 == result.exit_code, format_result_exception(result)
assert "No Renku project found" in result.output
finally:
sys.argv = argv
def test_template_set_outside_a_renku_project(isolated_runner):
"""Test setting template outside a project is not possible."""
result = isolated_runner.invoke(cli, ["template", "set"])
assert 2 == result.exit_code, format_result_exception(result)
assert "is not a renku repository" in result.output
def test_template_update_outside_a_renku_project(isolated_runner):
"""Test updating template outside a project is not possible."""
result = isolated_runner.invoke(cli, ["template", "update"])
assert 2 == result.exit_code, format_result_exception(result)
assert "is not a renku repository" in result.output
def test_template_set_failure(runner, client, client_database_injection_manager):
"""Test setting template in a project with template fails."""
result = runner.invoke(cli, ["template", "set"])
assert 1 == result.exit_code, format_result_exception(result)
assert "Project already has a template" in result.output
with client_database_injection_manager(client):
assert "python-minimal" == client.project.template_id
def test_template_set(runner, client, client_database_injection_manager):
"""Test setting a new template in a project."""
from renku.version import __template_version__
result = runner.invoke(cli, ["template", "set", "--force", "R-minimal"])
assert 0 == result.exit_code, format_result_exception(result)
with client_database_injection_manager(client):
assert "R-minimal" == client.project.template_id
assert __template_version__ == client.project.template_version
assert __template_version__ == client.project.template_ref
result = runner.invoke(cli, ["graph", "export", "--format", "json-ld", "--strict"])
assert 0 == result.exit_code, format_result_exception(result)
def test_template_set_overwrites_modified(runner, client, client_database_injection_manager):
"""Test setting a new template in a project overwrite modified files."""
write_and_commit_file(client.repository, "Dockerfile", "my-modifications")
result = runner.invoke(cli, ["template", "set", "--force", "R-minimal"])
assert 0 == result.exit_code, format_result_exception(result)
with client_database_injection_manager(client):
assert "R-minimal" == client.project.template_id
assert "my-modifications" not in (client.path / "Dockerfile").read_text()
assert not client.repository.is_dirty(untracked_files=True)
@pytest.mark.parametrize("overwrite, found", [["y", False], ["n", True]])
def test_template_set_interactive(runner, client, client_database_injection_manager, overwrite, found):
"""Test setting a template in interactive mode."""
write_and_commit_file(client.repository, "Dockerfile", "my-modifications")
result = runner.invoke(cli, ["template", "set", "-f", "R-minimal", "-i"], input=f"{overwrite}\n" * 420)
assert 0 == result.exit_code, format_result_exception(result)
with client_database_injection_manager(client):
assert "R-minimal" == client.project.template_id
assert ("my-modifications" in (client.path / "Dockerfile").read_text()) is found
assert not client.repository.is_dirty(untracked_files=True)
def test_template_set_preserve_renku_version(runner, client):
"""Test setting a template and overwriting Dockerfile still preserves Renku version."""
content = (client.path / "Dockerfile").read_text()
new_content = re.sub(r"^\s*ARG RENKU_VERSION=(.+)$", "ARG RENKU_VERSION=0.0.42", content, flags=re.MULTILINE)
write_and_commit_file(client.repository, "Dockerfile", new_content)
result = runner.invoke(cli, ["template", "set", "-f", "R-minimal", "--interactive"], input="y\n" * 420)
assert 0 == result.exit_code, format_result_exception(result)
content = (client.path / "Dockerfile").read_text()
assert new_content != content
assert "ARG RENKU_VERSION=0.0.42" in content
def test_template_set_dry_run(runner, client):
"""Test set dry-run doesn't make any changes."""
commit_sha_before = client.repository.head.commit.hexsha
result = runner.invoke(cli, ["template", "set", "-f", "R-minimal", "--dry-run"])
assert 0 == result.exit_code, format_result_exception(result)
assert not client.repository.is_dirty()
assert commit_sha_before == client.repository.head.commit.hexsha
@pytest.mark.integration
def test_template_update(runner, client, client_database_injection_manager):
"""Test updating a template."""
result = runner.invoke(
cli,
["template", "set", "-f", "python-minimal", "-s", TEMPLATES_URL, "-r", "0.1.10"]
+ ["-p", "description=fixed-version"],
)
assert 0 == result.exit_code, format_result_exception(result)
with client_database_injection_manager(client):
assert "python-minimal" == client.project.template_id
assert "0.1.10" == client.project.template_ref
assert "6c59d8863841baeca8f30062fd16c650cf67da3b" == client.project.template_version
result = runner.invoke(cli, ["template", "update"])
assert 0 == result.exit_code, format_result_exception(result)
assert "Template is up-to-date" not in result.output
with client_database_injection_manager(client):
assert "python-minimal" == client.project.template_id
assert Version(client.project.template_ref) > Version("0.1.10")
assert "6c59d8863841baeca8f30062fd16c650cf67da3b" != client.project.template_version
result = runner.invoke(cli, ["template", "update"])
assert 0 == result.exit_code, format_result_exception(result)
assert "Template is up-to-date" in result.output
result = runner.invoke(cli, ["graph", "export", "--format", "json-ld", "--strict"])
assert 0 == result.exit_code, format_result_exception(result)
def test_template_update_latest_version(runner, client):
"""Test updating template that is the latest version."""
result = runner.invoke(cli, ["template", "update"])
assert 0 == result.exit_code, format_result_exception(result)
assert "Template is up-to-date" in result.output
@pytest.mark.integration
def test_template_update_dry_run(runner, client):
"""Test update dry-run doesn't make any changes."""
result = runner.invoke(
cli,
["template", "set", "-f", "python-minimal", "-s", TEMPLATES_URL, "-r", "0.1.10"]
+ ["-p", "description=fixed-version"],
)
assert 0 == result.exit_code, format_result_exception(result)
commit_sha_before = client.repository.head.commit.hexsha
result = runner.invoke(cli, ["template", "update", "--dry-run"])
assert 0 == result.exit_code, format_result_exception(result)
assert not client.repository.is_dirty()
assert commit_sha_before == client.repository.head.commit.hexsha
def test_git_hook_for_modified_immutable_template_files(runner, client_with_template):
"""Test check for modified immutable template files."""
(client_with_template.path / "immutable.file").write_text("Locally modified immutable files")
with chdir(client_with_template.path):
result = runner.invoke(cli, ["check-immutable-template-files", "Dockerfile"])
assert result.exit_code == 0, result.output
result = runner.invoke(cli, ["check-immutable-template-files", "immutable.file"])
assert result.exit_code == 1
assert "immutable.file" in result.output
def test_template_update_with_parameters(
runner, client_with_template, templates_source, client_database_injection_manager
):
"""Test update prompts for new template parameters."""
parameter = TemplateParameter(name="new-parameter", description="", type="", possible_values=[], default=None)
templates_source.update(id="dummy", version="2.0.0", parameters=[parameter])
result = runner.invoke(cli, ["template", "update"], input="new-value\n")
assert result.exit_code == 0, result.output
with client_database_injection_manager(client_with_template):
template_metadata = TemplateMetadata.from_client(client=client_with_template)
assert "new-parameter" in template_metadata.metadata
assert "new-value" == template_metadata.metadata["new-parameter"]
def test_template_update_with_parameters_with_defaults(
runner, client_with_template, templates_source, client_database_injection_manager
):
"""Test update doesn't prompt for new template parameters with default value."""
parameter = TemplateParameter(name="new-parameter", description="", type="", possible_values=[], default="def-val")
templates_source.update(id="dummy", version="2.0.0", parameters=[parameter])
result = runner.invoke(cli, ["template", "update"])
assert result.exit_code == 0, result.output
with client_database_injection_manager(client_with_template):
template_metadata = TemplateMetadata.from_client(client=client_with_template)
assert "new-parameter" in template_metadata.metadata
assert "def-val" == template_metadata.metadata["new-parameter"]
def test_template_set_with_parameters(
runner, client_with_template, templates_source, client_database_injection_manager
):
"""Test template set doesn't prompts for new template parameters when passed on command line."""
parameter = TemplateParameter(name="new-parameter", description="", type="", possible_values=[], default=None)
templates_source.update(id="dummy", version="2.0.0", parameters=[parameter])
result = runner.invoke(cli, ["template", "set", "-f", "-s", "dummy", "dummy", "-p", "new-parameter=param-value"])
assert result.exit_code == 0, result.output
with client_database_injection_manager(client_with_template):
template_metadata = TemplateMetadata.from_client(client=client_with_template)
assert "new-parameter" in template_metadata.metadata
assert "param-value" == template_metadata.metadata["new-parameter"]